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
405 lines (355 loc) · 12.1 KB
/
Copy pathdocs-render.test.ts
File metadata and controls
405 lines (355 loc) · 12.1 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import fs from "fs";
import path from "path";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
vi.mock("../registry", () => ({
getDocsMode: () => "generated",
}));
import {
buildFrameworkNav,
buildFrameworkOnlyNav,
CONTENT_DIR,
inlineSnippets,
loadDoc,
readIcon,
SNIPPET_MAP,
SNIPPETS_DIR,
} from "../docs-render";
import type { NavNode } from "../docs-render";
import { buildCookbookNavTree } from "../cookbook-nav";
import { navTreeToPageTree } from "../page-tree-bridge";
import { buildReferencePageTree } from "../reference-items";
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 hasPageTitle(navTree: NavNode[], page: string): boolean {
return navTree.some((node) => {
if (node.type === "page") return node.title === page;
if (node.type === "group") return hasPageTitle(node.children, page);
return false;
});
}
function sectionPages(navTree: NavNode[], section: string): string[] {
const pages: string[] = [];
let inSection = false;
for (const node of navTree) {
if (node.type === "section") {
inSection = node.title === section;
continue;
}
if (!inSection) continue;
if (node.type === "page") pages.push(node.title);
}
return pages;
}
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("readIcon", () => {
it("only exposes page icons when frontmatter opts in with showIcon", () => {
const hiddenIconFile = path.join(tempDir, "hidden-icon.mdx");
const visibleIconFile = path.join(tempDir, "visible-icon.mdx");
fs.writeFileSync(
hiddenIconFile,
[
"---",
'title: "Hidden icon"',
'icon: "lucide/Bolt"',
"---",
"",
"Body",
].join("\n"),
);
fs.writeFileSync(
visibleIconFile,
[
"---",
'title: "Visible icon"',
'icon: "lucide/Bolt"',
"showIcon: true",
"---",
"",
"Body",
].join("\n"),
);
expect(readIcon(hiddenIconFile)).toBeNull();
expect(readIcon(visibleIconFile)).toBe("lucide/Bolt");
});
});
describe("reference nav", () => {
it("renders the Reference root entry with a book icon", () => {
const tree = buildReferencePageTree("v2");
const markup = renderToStaticMarkup(
React.createElement(React.Fragment, null, tree.name),
);
expect(markup).toContain("lucide-book-open");
expect(markup).toContain("Reference");
});
});
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(
"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("cookbook nav", () => {
it("renders overview and recipes as top-level entries without changing slugs", () => {
const navTree = buildCookbookNavTree();
expect(navTree).toHaveLength(5);
expect(navTree.map((node) => node.type)).toEqual([
"page",
"page",
"page",
"page",
"page",
]);
expect(
navTree.map((node) =>
node.type === "page" ? [node.title, node.slug] : null,
),
).toEqual([
["Overview", "cookbook/index"],
["Daytona", "cookbook/daytona"],
["Oracle Agent Memory", "cookbook/oracle-agent-spec-memory"],
["Arcade", "cookbook/arcade"],
["Angular + Google ADK", "cookbook/angular-adk-agentic-app"],
]);
const pageTree = navTreeToPageTree(navTree, "");
expect(pageTree.children.map((node) => node.type)).toEqual([
"page",
"page",
"page",
"page",
"page",
]);
expect(
pageTree.children.map((node) => (node.type === "page" ? node.url : null)),
).toEqual([
"/cookbook",
"/cookbook/daytona",
"/cookbook/oracle-agent-spec-memory",
"/cookbook/arcade",
"/cookbook/angular-adk-agentic-app",
]);
const overview = pageTree.children[0];
if (overview?.type !== "page") throw new Error("expected Overview page");
const overviewMarkup = renderToStaticMarkup(
React.createElement(React.Fragment, null, overview.name),
);
expect(overviewMarkup).toContain("lucide-book-open");
expect(overviewMarkup).toContain("Overview");
});
});
describe("framework nav", () => {
it("leaves Slack and Teams platform guides ungated", () => {
const slack = loadDoc("frontends/slack")?.fm;
const teams = loadDoc("frontends/teams")?.fm;
expect(slack?.earlyAccess).toBeUndefined();
expect(slack?.hideTOC).toBe(true);
expect(teams?.earlyAccess).toBeUndefined();
expect(teams?.hideTOC).toBe(true);
});
it("loads early-access frontmatter for gated platform guides", () => {
const whatsapp = loadDoc("frontends/whatsapp")?.fm;
expect(whatsapp?.earlyAccess).toBe("whatsapp");
expect(whatsapp?.hideTOC).toBe(true);
});
it("keeps frontend platform guides out of generated framework nav", () => {
const navTree = buildFrameworkNav(
"langgraph",
"LangGraph (Python)",
"langgraph-python",
);
expect(hasSectionPage(navTree, "Platforms", "React Native")).toBe(false);
expect(hasSectionPage(navTree, "Platforms", "Vue")).toBe(false);
});
it("keeps frontend platform guides out of authored framework nav", () => {
const navTree = buildFrameworkOnlyNav("built-in-agent");
expect(hasSectionPage(navTree, "Platforms", "React Native")).toBe(false);
expect(hasSectionPage(navTree, "Platforms", "Slack")).toBe(false);
});
it("shows the CLI page in generated and authored framework nav", () => {
const generatedNav = buildFrameworkNav(
"langgraph",
"LangGraph (Python)",
"langgraph-python",
);
const authoredNav = buildFrameworkOnlyNav("mastra");
const sharedFolderAuthoredNav = buildFrameworkOnlyNav("langgraph");
expect(hasPageTitle(generatedNav, "CopilotKit CLI")).toBe(true);
expect(hasPageTitle(authoredNav, "CopilotKit CLI")).toBe(true);
expect(hasPageTitle(sharedFolderAuthoredNav, "CopilotKit CLI")).toBe(true);
});
it("uses the generated Intelligence Platform section for authored framework nav", () => {
const navTree = buildFrameworkOnlyNav("ag2");
expect(navTree.some((node) => node.title === "Premium Features")).toBe(
false,
);
expect(navTree.some((node) => node.title === "Enterprise")).toBe(false);
expect(sectionPages(navTree, "Intelligence Platform")).toEqual([
"Enterprise Intelligence Platform",
"Cloud-Hosted Enterprise Intelligence",
"Self-Hosting Enterprise Intelligence",
"Enterprise Intelligence Architecture",
"Threads & Persistence Architecture",
"Threads",
]);
});
});