forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs-render.test.ts
More file actions
232 lines (201 loc) · 6.9 KB
/
Copy pathdocs-render.test.ts
File metadata and controls
232 lines (201 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import fs from "fs";
import path from "path";
vi.mock("../registry", () => ({
getDocsMode: () => "generated",
}));
import {
buildFrameworkNav,
buildFrameworkOnlyNav,
CONTENT_DIR,
inlineSnippets,
loadDoc,
SNIPPET_MAP,
SNIPPETS_DIR,
} from "../docs-render";
import type { NavNode } from "../docs-render";
let tempDir = "";
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(SNIPPETS_DIR, "__pdx-208-"));
});
afterEach(() => {
delete SNIPPET_MAP.Pdx208Parent;
if (tempDir) fs.rmSync(tempDir, { recursive: true, force: true });
tempDir = "";
vi.restoreAllMocks();
});
function writeSnippet(filename: string, body: string): string {
const filePath = path.join(tempDir, filename);
fs.writeFileSync(filePath, body);
return path.relative(SNIPPETS_DIR, filePath);
}
function hasSectionPage(navTree: NavNode[], section: string, page: string) {
let inSection = false;
for (const node of navTree) {
if (node.type === "section") {
inSection = node.title === section;
continue;
}
if (inSection && node.type === "page" && node.title === page) return true;
}
return false;
}
function collectMdxFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const filePath = path.join(dir, entry.name);
if (entry.isDirectory()) return collectMdxFiles(filePath);
return entry.isFile() && entry.name.endsWith(".mdx") ? [filePath] : [];
});
}
describe("inlineSnippets", () => {
it("recursively inlines helper components imported from snippets", () => {
const helperRel = writeSnippet("helper.mdx", "Helper body\n");
const parentRel = writeSnippet(
"parent.mdx",
[
`import Pdx208Helper from "@/snippets/${helperRel}";`,
"",
"Before",
"<Pdx208Helper />",
"After",
].join("\n"),
);
SNIPPET_MAP.Pdx208Parent = parentRel;
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const rendered = inlineSnippets("<Pdx208Parent />", "pdx-208");
expect(rendered).toContain("Before");
expect(rendered).toContain("Helper body");
expect(rendered).toContain("After");
expect(rendered).not.toContain("<Pdx208Helper />");
expect(warnSpy).not.toHaveBeenCalledWith(
"[docs-render] snippet missing for component",
"Pdx208Helper",
"from slug",
"pdx-208",
);
});
it("preserves non-snippet component imports as runtime components", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const rendered = inlineSnippets(
[
'import RuntimeCard from "@/components/runtime-card";',
"",
"<RuntimeCard />",
].join("\n"),
"pdx-208-runtime",
);
expect(rendered).toContain("<RuntimeCard />");
expect(warnSpy).not.toHaveBeenCalledWith(
"[docs-render] snippet missing for component",
"RuntimeCard",
"from slug",
"pdx-208-runtime",
);
});
it("preserves multiline runtime component imports", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
const rendered = inlineSnippets(
[
"import {",
" RuntimeCard,",
'} from "@/components/runtime-card";',
"",
"<RuntimeCard />",
].join("\n"),
"pdx-208-runtime-multiline",
);
expect(rendered).toContain("<RuntimeCard />");
expect(warnSpy).not.toHaveBeenCalledWith(
"[docs-render] snippet missing for component",
"RuntimeCard",
"from slug",
"pdx-208-runtime-multiline",
);
});
});
describe("loadDoc", () => {
it("resolves clean URLs to files stored under route-group folders", () => {
const doc = loadDoc("integrations/aws-strands/telemetry");
expect(doc?.filePath).toContain(
"integrations/aws-strands/(other)/telemetry/index.mdx",
);
});
});
describe("migration docs", () => {
it("recommends CopilotKit from the v2 entrypoint instead of CopilotKitProvider", () => {
const snippet = fs.readFileSync(
path.join(SNIPPETS_DIR, "shared/troubleshooting/migrate-to-v2.mdx"),
"utf8",
);
expect(snippet).toContain(
"The `<CopilotKit>` provider name — keep using it, but import it from `@copilotkit/react-core/v2`",
);
expect(snippet).toContain(
"Keep the `<CopilotKit>` provider name, but import it from `@copilotkit/react-core/v2`.",
);
expect(snippet).toContain(
'import { CopilotKit, useAgent } from "@copilotkit/react-core/v2";',
);
expect(snippet).toContain(
'import { CopilotKit, CopilotPopup } from "@copilotkit/react-core/v2";',
);
expect(snippet).not.toContain("CopilotKitProvider");
});
it("keeps v2 reference pages aligned with the CopilotKit v2 entrypoint", () => {
const referenceIndex = fs.readFileSync(
path.join(CONTENT_DIR, "..", "reference/index.mdx"),
"utf8",
);
const componentReference = fs.readFileSync(
path.join(CONTENT_DIR, "..", "reference/components/CopilotKit.mdx"),
"utf8",
);
expect(referenceIndex).toContain(
'import { CopilotKit } from "@copilotkit/react-core/v2";',
);
expect(referenceIndex).not.toContain(
"CopilotKit is imported from the root package",
);
expect(referenceIndex).not.toContain(
"import `CopilotKit` from `@copilotkit/react-core`",
);
expect(componentReference).toContain("`@copilotkit/react-core/v2`");
expect(componentReference).not.toContain("not from the v2 subpackage");
});
it("does not recommend stale v2 package paths in authored docs", () => {
const authoredDocFiles = collectMdxFiles(CONTENT_DIR);
const allowedRootProviderImports = new Set([
path.join(CONTENT_DIR, "migrate/v2.mdx"),
]);
const rootProviderImports = authoredDocFiles.filter((filePath) => {
if (allowedRootProviderImports.has(filePath)) return false;
return fs
.readFileSync(filePath, "utf8")
.includes('import { CopilotKit } from "@copilotkit/react-core";');
});
const oldV2StyleImports = [
...authoredDocFiles,
...collectMdxFiles(SNIPPETS_DIR),
].filter((filePath) =>
fs
.readFileSync(filePath, "utf8")
.includes("@copilotkit/react-ui/v2/styles.css"),
);
expect(rootProviderImports).toEqual([]);
expect(oldV2StyleImports).toEqual([]);
});
});
describe("framework nav", () => {
it("includes the shared React Native platform guide in generated framework nav", () => {
const navTree = buildFrameworkNav(
"langgraph",
"LangGraph (Python)",
"langgraph-python",
);
expect(hasSectionPage(navTree, "Platforms", "React Native")).toBe(true);
});
it("includes the shared React Native platform guide in authored framework nav", () => {
const navTree = buildFrameworkOnlyNav("built-in-agent");
expect(hasSectionPage(navTree, "Platforms", "React Native")).toBe(true);
});
});