forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.test.ts
More file actions
516 lines (451 loc) · 14.7 KB
/
Copy pathrunner.test.ts
File metadata and controls
516 lines (451 loc) · 14.7 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
// ---------------------------------------------------------------------------
// Types under test (imported from runner.ts once it exists)
// ---------------------------------------------------------------------------
import type {
TierConfig,
TiersFile,
RunOptions,
TieredRunResult,
} from "./runner.js";
// ---------------------------------------------------------------------------
// Hoisted mocks — vi.hoisted() runs before vi.mock factories, so these
// variables are available inside the factory closures.
// ---------------------------------------------------------------------------
const { execFileMock, readFileSyncMock } = vi.hoisted(() => ({
execFileMock: vi.fn(),
readFileSyncMock: vi.fn(),
}));
vi.mock("node:child_process", () => ({
execFile: execFileMock,
}));
vi.mock("node:fs", () => ({
default: { readFileSync: (...args: unknown[]) => readFileSyncMock(...args) },
readFileSync: (...args: unknown[]) => readFileSyncMock(...args),
}));
// Now import the module under test
import { loadTiers, runSlug, runParallel, runTiered } from "./runner.js";
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Create a fake execFile callback invocation for a successful slug run. */
function fakeExecFileSuccess(
stdout: string,
{ delay = 0 }: { delay?: number } = {},
) {
execFileMock.mockImplementationOnce(
(
_cmd: string,
_args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
const timeout = delay
? setTimeout(() => cb(null, stdout, ""), delay)
: (cb(null, stdout, ""), undefined);
return {
pid: 1234,
kill: () => {
if (timeout) clearTimeout(timeout);
},
};
},
);
}
/** Create a fake execFile that exits with a non-zero code. */
function fakeExecFileFail(code: number, stderr = "") {
execFileMock.mockImplementationOnce(
(
_cmd: string,
_args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
const err = Object.assign(new Error(`exit code ${code}`), {
code,
killed: false,
signal: null,
});
cb(err, "", stderr);
return { pid: 1234, kill: () => {} };
},
);
}
/** Create a fake execFile that times out (never calls callback). */
function fakeExecFileTimeout() {
execFileMock.mockImplementationOnce(
(
_cmd: string,
_args: string[],
opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
// The runner should set a timeout. We simulate timeout by calling back
// with a timeout-like error after maxBuffer / timeout.
const err = Object.assign(new Error("Command timed out"), {
killed: true,
signal: "SIGTERM",
code: null,
});
// Call back async to simulate timeout
setTimeout(() => cb(err, "", ""), 10);
return { pid: 1234, kill: () => {} };
},
);
}
/** Sample Playwright JSON reporter output for a passing test. */
function playwrightJsonOutput(
slug: string,
tests: Array<{
title: string;
status: string;
duration: number;
error?: string;
}>,
): string {
const suites = [
{
title: slug,
specs: tests.map((t) => ({
title: t.title,
tests: [
{
results: [
{
status: t.status,
duration: t.duration,
error: t.error ? { message: t.error } : undefined,
},
],
},
],
})),
},
];
return JSON.stringify({ suites });
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("loadTiers", () => {
beforeEach(() => {
readFileSyncMock.mockReset();
});
it("reads tier config and resolves '*' wildcard against slug list", () => {
const tiersFile: TiersFile = {
tiers: [
{ name: "Gold Standard", slugs: ["langgraph-python"], fail_fast: true },
{
name: "Key Partners",
slugs: ["mastra", "crewai-crews"],
fail_fast: false,
},
{ name: "Full Matrix", slugs: "*", fail_fast: false },
],
};
readFileSyncMock.mockReturnValueOnce(JSON.stringify(tiersFile));
const allSlugs = [
"langgraph-python",
"mastra",
"crewai-crews",
"google-adk",
"langgraph-typescript",
"openai-swarm",
];
const result = loadTiers("/path/to/eval-tiers.json", allSlugs);
expect(result).toHaveLength(3);
// Tier 1: exact slugs
expect(result[0].name).toBe("Gold Standard");
expect(result[0].slugs).toEqual(["langgraph-python"]);
expect(result[0].fail_fast).toBe(true);
// Tier 2: exact slugs
expect(result[1].name).toBe("Key Partners");
expect(result[1].slugs).toEqual(["mastra", "crewai-crews"]);
// Tier 3: wildcard resolved — excludes slugs already in tiers 1 and 2
expect(result[2].name).toBe("Full Matrix");
expect(result[2].slugs).toEqual(
expect.arrayContaining([
"google-adk",
"langgraph-typescript",
"openai-swarm",
]),
);
expect(result[2].slugs).not.toContain("langgraph-python");
expect(result[2].slugs).not.toContain("mastra");
expect(result[2].slugs).not.toContain("crewai-crews");
});
it("handles missing file gracefully (returns single 'all' tier)", () => {
readFileSyncMock.mockImplementationOnce(() => {
const err = Object.assign(new Error("ENOENT"), {
code: "ENOENT",
});
throw err;
});
const allSlugs = ["langgraph-python", "mastra"];
const result = loadTiers("/nonexistent/eval-tiers.json", allSlugs);
expect(result).toHaveLength(1);
expect(result[0].name).toBe("all");
expect(result[0].slugs).toEqual(allSlugs);
expect(result[0].fail_fast).toBe(false);
});
});
describe("runSlug", () => {
beforeEach(() => {
execFileMock.mockReset();
});
it("parses Playwright JSON reporter output", async () => {
const jsonOut = playwrightJsonOutput("langgraph-python", [
{ title: "sends chat message", status: "passed", duration: 1200 },
{ title: "uses tool", status: "passed", duration: 800 },
]);
fakeExecFileSuccess(jsonOut);
const result = await runSlug("langgraph-python", "d5", 30000, "/showcase");
expect(result.slug).toBe("langgraph-python");
expect(result.status).toBe("pass");
expect(result.tests["langgraph-python > sends chat message"]).toBeDefined();
expect(result.tests["langgraph-python > sends chat message"].status).toBe(
"pass",
);
expect(
result.tests["langgraph-python > sends chat message"].duration_ms,
).toBe(1200);
expect(result.tests["langgraph-python > uses tool"].status).toBe("pass");
});
it("handles child process crash (non-zero exit, no JSON output)", async () => {
fakeExecFileFail(1, "Segmentation fault");
const result = await runSlug("crewai-crews", "d5", 30000, "/showcase");
expect(result.slug).toBe("crewai-crews");
expect(result.status).toBe("fail");
expect(result.duration_ms).toBeGreaterThanOrEqual(0);
expect(Object.keys(result.tests)).toHaveLength(0);
});
it("handles child process timeout", async () => {
fakeExecFileTimeout();
const result = await runSlug("slow-integration", "d5", 100, "/showcase");
expect(result.slug).toBe("slow-integration");
expect(result.status).toBe("fail");
});
});
describe("runParallel", () => {
beforeEach(() => {
execFileMock.mockReset();
});
it("collects results from all slugs", async () => {
const slugs = ["slug-a", "slug-b", "slug-c"];
for (const slug of slugs) {
const jsonOut = playwrightJsonOutput(slug, [
{ title: "basic test", status: "passed", duration: 500 },
]);
fakeExecFileSuccess(jsonOut);
}
const opts: RunOptions = {
level: "d5",
maxParallel: 3,
timeout: 30000,
showcaseDir: "/showcase",
};
const results = await runParallel(slugs, opts);
expect(results).toHaveLength(3);
const resultSlugs = results.map((r) => r.slug).sort();
expect(resultSlugs).toEqual(["slug-a", "slug-b", "slug-c"]);
expect(results.every((r) => r.status === "pass")).toBe(true);
});
it("respects concurrency limit", async () => {
let concurrentCount = 0;
let maxConcurrent = 0;
// Override execFile to track concurrency
execFileMock.mockImplementation(
(
_cmd: string,
_args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
concurrentCount++;
if (concurrentCount > maxConcurrent) {
maxConcurrent = concurrentCount;
}
// Simulate async work
setTimeout(() => {
concurrentCount--;
const jsonOut = playwrightJsonOutput("test", [
{ title: "t", status: "passed", duration: 100 },
]);
cb(null, jsonOut, "");
}, 50);
return { pid: 1234, kill: () => {} };
},
);
const slugs = ["a", "b", "c", "d", "e", "f"];
const opts: RunOptions = {
level: "d5",
maxParallel: 2,
timeout: 30000,
showcaseDir: "/showcase",
};
const results = await runParallel(slugs, opts);
expect(results).toHaveLength(6);
expect(maxConcurrent).toBeLessThanOrEqual(2);
});
});
describe("runTiered", () => {
beforeEach(() => {
execFileMock.mockReset();
readFileSyncMock.mockReset();
});
it("executes tiers in order (tier 1 before tier 2)", async () => {
const tiersFile: TiersFile = {
tiers: [
{ name: "Tier 1", slugs: ["slug-a"], fail_fast: true },
{ name: "Tier 2", slugs: ["slug-b"], fail_fast: false },
],
};
readFileSyncMock.mockReturnValue(JSON.stringify(tiersFile));
const executionOrder: string[] = [];
execFileMock.mockImplementation(
(
_cmd: string,
args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
// Extract slug from args — it's the argument after "test"
const testIdx = args.indexOf("test");
const slug = testIdx >= 0 ? args[testIdx + 1] : "unknown";
executionOrder.push(slug);
const jsonOut = playwrightJsonOutput(slug, [
{ title: "basic", status: "passed", duration: 100 },
]);
cb(null, jsonOut, "");
return { pid: 1234, kill: () => {} };
},
);
const opts: RunOptions = {
level: "d5",
maxParallel: 2,
timeout: 30000,
showcaseDir: "/showcase",
};
const result = await runTiered(
["slug-a", "slug-b"],
["slug-a", "slug-b"],
opts,
);
// Tier 1 slug should execute before tier 2 slug
expect(executionOrder.indexOf("slug-a")).toBeLessThan(
executionOrder.indexOf("slug-b"),
);
expect(result.tierSummaries).toHaveLength(2);
expect(result.tierSummaries[0].name).toBe("Tier 1");
expect(result.tierSummaries[1].name).toBe("Tier 2");
});
it("stops on tier 1 fail-fast when regression detected", async () => {
const tiersFile: TiersFile = {
tiers: [
{ name: "Gold Standard", slugs: ["slug-a"], fail_fast: true },
{ name: "Rest", slugs: ["slug-b"], fail_fast: false },
],
};
readFileSyncMock.mockReturnValue(JSON.stringify(tiersFile));
// slug-a fails
execFileMock.mockImplementation(
(
_cmd: string,
args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
const testIdx = args.indexOf("test");
const slug = testIdx >= 0 ? args[testIdx + 1] : "unknown";
if (slug === "slug-a") {
const jsonOut = playwrightJsonOutput(slug, [
{
title: "basic",
status: "failed",
duration: 100,
error: "assertion failed",
},
]);
cb(null, jsonOut, "");
} else {
const jsonOut = playwrightJsonOutput(slug, [
{ title: "basic", status: "passed", duration: 100 },
]);
cb(null, jsonOut, "");
}
return { pid: 1234, kill: () => {} };
},
);
const opts: RunOptions = {
level: "d5",
maxParallel: 2,
timeout: 30000,
showcaseDir: "/showcase",
};
const result = await runTiered(
["slug-a", "slug-b"],
["slug-a", "slug-b"],
opts,
);
// Should have stopped after tier 1
expect(result.abortedAtTier).toBe(0);
expect(result.tierSummaries).toHaveLength(1);
// slug-b should be skipped
const slugBResult = result.results.find((r) => r.slug === "slug-b");
expect(slugBResult).toBeUndefined();
});
it("continues through all tiers when noFailFast=true", async () => {
const tiersFile: TiersFile = {
tiers: [
{ name: "Gold Standard", slugs: ["slug-a"], fail_fast: true },
{ name: "Rest", slugs: ["slug-b"], fail_fast: false },
],
};
readFileSyncMock.mockReturnValue(JSON.stringify(tiersFile));
// slug-a fails
execFileMock.mockImplementation(
(
_cmd: string,
args: string[],
_opts: Record<string, unknown>,
cb: (err: Error | null, stdout: string, stderr: string) => void,
) => {
const testIdx = args.indexOf("test");
const slug = testIdx >= 0 ? args[testIdx + 1] : "unknown";
if (slug === "slug-a") {
const jsonOut = playwrightJsonOutput(slug, [
{
title: "basic",
status: "failed",
duration: 100,
error: "assertion failed",
},
]);
cb(null, jsonOut, "");
} else {
const jsonOut = playwrightJsonOutput(slug, [
{ title: "basic", status: "passed", duration: 100 },
]);
cb(null, jsonOut, "");
}
return { pid: 1234, kill: () => {} };
},
);
const opts: RunOptions = {
level: "d5",
maxParallel: 2,
timeout: 30000,
showcaseDir: "/showcase",
noFailFast: true,
};
const result = await runTiered(
["slug-a", "slug-b"],
["slug-a", "slug-b"],
opts,
);
// Should NOT have stopped — continued through both tiers
expect(result.abortedAtTier).toBeUndefined();
expect(result.tierSummaries).toHaveLength(2);
expect(result.results).toHaveLength(2);
});
});