forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-code.test.ts
More file actions
314 lines (288 loc) · 8.74 KB
/
Copy pathdemo-code.test.ts
File metadata and controls
314 lines (288 loc) · 8.74 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
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "fs";
import os from "os";
import path from "path";
import { extractRegion, inferLanguage } from "../demo-code";
import { rewriteDemoCode } from "../rewrite-demo-code";
// `procEnv.NODE_ENV` is typed as readonly under @types/node's strict
// view. Vitest mutates it at runtime — that's how its `NODE_ENV=test`
// override works — so this writable handle reflects what's actually
// available at runtime. Casting through `Record<string, string>` keeps
// the test bodies legible while satisfying tsc.
const procEnv = process.env as Record<string, string | undefined>;
function restoreNodeEnv(value: string | undefined): void {
if (value === undefined) {
delete procEnv.NODE_ENV;
return;
}
procEnv.NODE_ENV = value;
}
describe("extractRegion (py comment syntax)", () => {
it("returns the bounded region content with markers stripped", () => {
const src = [
"import os",
"",
"# region: middleware",
"x = 1",
"y = 2",
"# endregion",
"",
"z = 3",
].join("\n");
expect(extractRegion(src, "middleware", "py")).toBe("x = 1\ny = 2");
});
it("returns null when the region is missing", () => {
const src = "# region: other\nfoo\n# endregion\n";
expect(extractRegion(src, "missing", "py")).toBeNull();
});
it("throws in dev mode when the same region appears twice", () => {
const src = [
"# region: dup",
"first",
"# endregion",
"# region: dup",
"second",
"# endregion",
].join("\n");
const origEnv = procEnv.NODE_ENV;
procEnv.NODE_ENV = "development";
try {
expect(() => extractRegion(src, "dup", "py")).toThrow(
/duplicate region/i,
);
} finally {
restoreNodeEnv(origEnv);
}
});
it("concatenates duplicate regions in production mode", () => {
const src = [
"# region: dup",
"first",
"# endregion",
"# region: dup",
"second",
"# endregion",
].join("\n");
const origEnv = procEnv.NODE_ENV;
procEnv.NODE_ENV = "production";
try {
expect(extractRegion(src, "dup", "py")).toBe("first\nsecond");
} finally {
restoreNodeEnv(origEnv);
}
});
it("throws in both modes when endregion is missing", () => {
const src = "# region: orphan\nfoo\nbar\n";
for (const env of ["development", "production"]) {
const orig = procEnv.NODE_ENV;
procEnv.NODE_ENV = env;
try {
expect(() => extractRegion(src, "orphan", "py")).toThrow(
/unterminated region/i,
);
} finally {
restoreNodeEnv(orig);
}
}
});
it("tolerates leading whitespace on marker lines", () => {
const src = [
"class Foo:",
" # region: inner",
" x = 1",
" # endregion",
].join("\n");
expect(extractRegion(src, "inner", "py")).toBe(" x = 1");
});
it("also reads bundle-style @region markers", () => {
const src = [
"# @region[subagent-setup]",
"graph = create_agent()",
"# @endregion[subagent-setup]",
].join("\n");
expect(extractRegion(src, "subagent-setup", "py")).toBe(
"graph = create_agent()",
);
});
it("does not close bundle-style regions on another region's end marker", () => {
const src = [
"# @region[outer]",
"before = True",
"# @region[inner]",
"inside = True",
"# @endregion[inner]",
"after = True",
"# @endregion[outer]",
].join("\n");
expect(extractRegion(src, "outer", "py")).toBe(
[
"before = True",
"# @region[inner]",
"inside = True",
"# @endregion[inner]",
"after = True",
].join("\n"),
);
});
it("does not close bundle-style regions on legacy end markers", () => {
const src = [
"# @region[outer]",
"before = True",
"# region: inner",
"inside = True",
"# endregion",
"after = True",
"# @endregion[outer]",
].join("\n");
expect(extractRegion(src, "outer", "py")).toBe(
[
"before = True",
"# region: inner",
"inside = True",
"# endregion",
"after = True",
].join("\n"),
);
});
});
describe("extractRegion (ts/js comment syntax)", () => {
it("uses // for the .ts dispatch", () => {
const src = [
"import { foo } from 'bar';",
"// region: setup",
"const x = 1;",
"// endregion",
"export {};",
].join("\n");
expect(extractRegion(src, "setup", "ts")).toBe("const x = 1;");
});
it("uses // for the .tsx dispatch", () => {
const src = "// region: r\nconst a = 1;\n// endregion\n";
expect(extractRegion(src, "r", "tsx")).toBe("const a = 1;");
});
it("uses // for the .js dispatch", () => {
const src = "// region: r\nconst a = 1;\n// endregion\n";
expect(extractRegion(src, "r", "js")).toBe("const a = 1;");
});
it("reads bundle-style @region markers with // comments", () => {
const src = [
"// @region[setup]",
"const x = 1;",
"// @endregion[setup]",
].join("\n");
expect(extractRegion(src, "setup", "ts")).toBe("const x = 1;");
});
it("returns null for an extension with no comment syntax registered", () => {
expect(extractRegion("region: r\nx\nendregion", "r", "unknown")).toBeNull();
});
});
describe("rewriteDemoCode", () => {
let tmp = "";
beforeEach(() => {
tmp = fs.mkdtempSync(path.join(os.tmpdir(), "rewrite-"));
});
afterEach(() => {
if (tmp) fs.rmSync(tmp, { recursive: true, force: true });
tmp = "";
});
function plantSource(rel: string, contents: string): void {
const abs = path.join(tmp, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, contents);
}
it("expands a static <DemoCode> reference into a fenced block", () => {
plantSource(
"src/agents/frontend_tools.py",
[
"# region: middleware",
"graph = create_agent(middleware=[CopilotKitMiddleware()])",
"# endregion",
].join("\n"),
);
const out = rewriteDemoCode(
'<DemoCode file="src/agents/frontend_tools.py" region="middleware" />',
tmp,
);
expect(out).toContain("~~~~python");
expect(out).toContain("CopilotKitMiddleware()");
expect(out).toContain('title="frontend_tools.py"');
});
it("honors explicit language + title overrides", () => {
plantSource(
"src/util.go",
["// region: helper", "func Helper() {}", "// endregion"].join("\n"),
);
const out = rewriteDemoCode(
'<DemoCode file="src/util.go" region="helper" language="golang" title="helper.go" />',
tmp,
);
expect(out).toContain("~~~~golang");
expect(out).toContain('title="helper.go"');
});
it("escapes quotes in fence titles", () => {
plantSource(
"src/quoted.ts",
["// region: setup", "export const ok = true;", "// endregion"].join(
"\n",
),
);
const out = rewriteDemoCode(
'<DemoCode file="src/quoted.ts" region="setup" title=\'agent "setup"\' />',
tmp,
);
expect(out).toContain('title="agent \\"setup\\""');
});
it("matches quoted attribute values that contain a greater-than sign", () => {
plantSource(
"src/compare.ts",
["// region: setup", "export const max = 2;", "// endregion"].join("\n"),
);
const out = rewriteDemoCode(
'<DemoCode file="src/compare.ts" region="setup" title="A > B" />',
tmp,
);
expect(out).toContain("~~~~typescript");
expect(out).toContain('title="A > B"');
expect(out).toContain("export const max = 2;");
});
it("leaves expression-valued <DemoCode> references intact", () => {
const input = '<DemoCode file={someVar} region="x" />';
expect(rewriteDemoCode(input, tmp)).toBe(input);
});
it("strips a missing-file reference to empty (logged)", () => {
const out = rewriteDemoCode(
'<DemoCode file="src/missing.py" region="x" />',
tmp,
);
expect(out).toBe("");
});
it("strips a reference whose region isn't found to empty", () => {
plantSource(
"src/agents/foo.py",
["# region: other", "x", "# endregion"].join("\n"),
);
const out = rewriteDemoCode(
'<DemoCode file="src/agents/foo.py" region="missing" />',
tmp,
);
expect(out).toBe("");
});
});
describe("inferLanguage", () => {
it.each([
["agent.py", "python"],
["app.ts", "typescript"],
["page.tsx", "typescript"],
["script.js", "javascript"],
["app.jsx", "javascript"],
["App.java", "java"],
["Program.cs", "csharp"],
["main.go", "go"],
["app.kt", "kotlin"],
["main.rs", "rust"],
["weird.zzz", "plaintext"],
["no-extension", "plaintext"],
])("infers %s as %s", (file, lang) => {
expect(inferLanguage(file)).toBe(lang);
});
});