@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 495 lines 18.6 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import { expect, it } from "@alchemy.run/test";
3 import * as BunServices from "@effect/platform-bun/BunServices";
4 import * as Deferred from "effect/Deferred";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
9 import * as Path from "effect/Path";
10 import * as ChildProcess from "effect/process/ChildProcess";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import type { FileSuite } from "../src/Model.ts";
14 import {
15 Reporter,
16 type ReporterService,
17 type RunController,
18 type RunSummary,
19 type TestMeta,
20 type TestResult,
21 } from "../src/Reporter.ts";
22 import { run } from "../src/Runner.ts";
23
24 const api = new URL("../src/index.ts", import.meta.url).href;
25 const registry = new URL("../src/Registry.ts", import.meta.url).href;
26 const effect = import.meta.resolve("effect/Effect");
27
28 const setup = Effect.fn(function* (files: Record<string, string>) {
29 const fs = yield* FileSystem.FileSystem;
30 const path = yield* Path.Path;
31 const root = yield* fs.makeTempDirectoryScoped({
32 prefix: "alchemy-runner-lifecycle-",
33 });
34 yield* fs.writeFileString(
35 path.join(root, "state.ts"),
36 "export const events = []; export const roots = []; export const pids = new Set();",
37 );
38 for (const [name, body] of Object.entries(files)) {
39 yield* fs.writeFileString(
40 path.join(root, name),
41 `
42 import { it, describe, registerHook, registerFileCleanup } from ${JSON.stringify(api)};
43 import { currentFileSuite } from ${JSON.stringify(registry)};
44 import * as Effect from ${JSON.stringify(effect)};
45 import { events, roots, pids } from "./state.ts";
46 roots.push(currentFileSuite());
47 pids.add(process.pid);
48 const mark = (label) => Effect.sync(() => { events.push(label); });
49 ${body}
50 `,
51 );
52 }
53 const state = yield* Effect.promise(() => import(path.join(root, "state.ts"))) as Effect.Effect<{
54 events: string[];
55 roots: FileSuite[];
56 pids: Set<number>;
57 }>;
58 return {
59 state,
60 options: {
61 root,
62 paths: [root],
63 timeout: 1000,
64 retry: 0,
65 concurrency: 1,
66 sequential: true,
67 logFile: path.join(root, "run.log"),
68 } as const,
69 };
70 });
71
72 it.live("interrupts active bodies before cleanup and disposes queued files", () =>
73 Effect.gen(function* () {
74 const { state, options } = yield* setup({
75 "a.test.ts": `
76 registerFileCleanup({ body: () => mark("active-clean") });
77 registerHook("afterAll", { body: () => mark("after") });
78 it.live("active", () => mark("body").pipe(
79 Effect.andThen(Effect.never),
80 Effect.ensuring(mark("body-finalized")),
81 ));`,
82 "b.test.ts": `
83 registerFileCleanup({ body: () => mark("queued-clean") });
84 registerHook("afterAll", { body: () => mark("queued-after") });
85 it("queued", () => events.push("queued-body"));`,
86 });
87 const fiber = yield* Effect.scoped(run(options)).pipe(Effect.forkChild);
88 const started = yield* Effect.sync(() => state.events.includes("body")).pipe(
89 Effect.repeat({
90 schedule: Schedule.spaced("10 millis"),
91 times: 100,
92 until: (value) => value,
93 }),
94 );
95 expect(started).toBe(true);
96 yield* Fiber.interrupt(fiber);
97 expect(state.events).toEqual([
98 "body",
99 "body-finalized",
100 "after",
101 "active-clean",
102 "queued-clean",
103 ]);
104 expect(state.roots.every((root) => root.children.length === 0)).toBe(true);
105 }).pipe(
106 Effect.provide(
107 Layer.mergeAll(
108 BunServices.layer,
109 Layer.succeed(Reporter, {
110 emit: () => Effect.void,
111 waitForExit: () => Effect.void,
112 }),
113 ),
114 ),
115 ),
116 );
117
118 const silent: ReporterService = {
119 emit: () => Effect.void,
120 waitForExit: () => Effect.void,
121 };
122
123 it.live("preserves duplicate-title failures and the CLI exit code", () => {
124 let announced: ReadonlyArray<TestMeta> = [];
125 const ended: Array<{ meta: TestMeta; result: TestResult }> = [];
126 const reporter: ReporterService = {
127 waitForExit: () => Effect.void,
128 emit: (event) =>
129 Effect.sync(() => {
130 if (event._tag === "RunStart") announced = event.tests;
131 if (event._tag === "TestEnd") ended.push({ meta: event.test, result: event.result });
132 }),
133 };
134 return Effect.gen(function* () {
135 const { state, options } = yield* setup({
136 "duplicates.test.ts": `
137 it.live("duplicate", () => Effect.log("duplicate-failure-output").pipe(
138 Effect.andThen(Effect.fail(new Error("duplicate-failure"))),
139 ));
140 it("duplicate", () => {});
141 it.skip("duplicate", () => {});
142 it.todo("duplicate");
143 it.each([{ label: "same", fail: true }, { label: "same", fail: false }])(
144 "parameter $label",
145 ({ fail }) => { if (fail) throw new Error("parameter-failure"); },
146 );
147 it("filtered", () => { throw new Error("must-not-run"); });`,
148 });
149 const summary = yield* Effect.scoped(
150 run({
151 ...options,
152 filter: (title) => /duplicate$|parameter same$/.test(title),
153 }),
154 );
155 expect(announced.map((meta) => meta.titlePath)).toEqual([
156 ["duplicate"],
157 ["duplicate"],
158 ["duplicate"],
159 ["duplicate"],
160 ["parameter same"],
161 ["parameter same"],
162 ]);
163 expect(new Set(announced.map((meta) => meta.id)).size).toBe(6);
164 expect(ended.map(({ meta }) => meta.id)).toEqual(announced.map((meta) => meta.id));
165 expect(ended.map(({ result }) => result.status)).toEqual([
166 "fail",
167 "pass",
168 "skip",
169 "todo",
170 "fail",
171 "pass",
172 ]);
173 expect(summary.passed).toBe(2);
174 expect(summary.failed).toBe(2);
175 expect(summary.skipped).toBe(1);
176 expect(summary.todo).toBe(1);
177 expect(summary.failures.map(({ meta }) => meta.id)).toEqual([
178 announced[0]!.id,
179 announced[4]!.id,
180 ]);
181 expect(state.roots[0]!.children.length).toBe(0);
182 const fs = yield* FileSystem.FileSystem;
183 const log = yield* fs.readFileString(options.logFile);
184 expect(log).toContain("[test duplicates.test.ts > duplicate] duplicate-failure-output");
185
186 const executable = yield* Effect.sync(() => process.execPath);
187 const cli = yield* Effect.sync(() =>
188 fileURLToPath(new URL("../bin/alchemy-test.ts", import.meta.url)),
189 );
190 const output = yield* Effect.scoped(
191 Effect.gen(function* () {
192 const child = yield* ChildProcess.make(
193 executable,
194 [
195 cli,
196 options.root,
197 "--retry",
198 "0",
199 "--concurrency",
200 "1",
201 "-t",
202 "duplicate$|parameter same$",
203 ],
204 {
205 cwd: options.root,
206 env: { NO_COLOR: "1" },
207 extendEnv: true,
208 stdin: "ignore",
209 stdout: "pipe",
210 stderr: "pipe",
211 killSignal: "SIGTERM",
212 forceKillAfter: "1 second",
213 },
214 );
215 return yield* Effect.all(
216 {
217 code: child.exitCode,
218 stdout: child.stdout.pipe(
219 Stream.decodeText,
220 Stream.runCollect,
221 Effect.map((chunks) => chunks.join("")),
222 ),
223 stderr: child.stderr.pipe(
224 Stream.decodeText,
225 Stream.runCollect,
226 Effect.map((chunks) => chunks.join("")),
227 ),
228 },
229 { concurrency: "unbounded" },
230 );
231 }).pipe(Effect.timeout("20 seconds")),
232 );
233 expect(output.code).toBe(1);
234 expect(`${output.stdout}\n${output.stderr}`).toContain(
235 "Tests: 2 failed | 2 passed | 1 skipped | 1 todo",
236 );
237 expect(output.stdout).toContain("duplicate-failure");
238 expect(output.stdout).toContain("parameter-failure");
239 expect(output.stdout).not.toContain("must-not-run");
240 }).pipe(Effect.provide(Layer.mergeAll(BunServices.layer, Layer.succeed(Reporter, reporter))));
241 });
242
243 it.live("retries only the selected duplicate-title test", () =>
244 Effect.gen(function* () {
245 const retried = yield* Deferred.make<void>();
246 let controller: RunController | undefined;
247 let announced: ReadonlyArray<TestMeta> = [];
248 const started: string[] = [];
249 const ended: Array<{ meta: TestMeta; result: TestResult }> = [];
250 const reporter: ReporterService = {
251 waitForExit: () => Effect.void,
252 attachController: (value) =>
253 Effect.sync(() => {
254 controller = value;
255 }),
256 emit: (event) =>
257 Effect.gen(function* () {
258 if (event._tag === "RunStart") announced = event.tests;
259 if (event._tag === "TestStart") started.push(event.test.id);
260 if (event._tag === "TestEnd") {
261 ended.push({ meta: event.test, result: event.result });
262 if (ended.length === 3) yield* Deferred.succeed(retried, undefined);
263 }
264 }),
265 };
266 yield* Effect.gen(function* () {
267 const { state, options } = yield* setup({
268 "duplicates.test.ts": `
269 registerFileCleanup({ body: () => mark("clean") });
270 let attempts = 0;
271 it("duplicate", () => {
272 events.push("first");
273 if (++attempts <= 2) throw new Error("first-attempts");
274 }, { retry: 1 });
275 it("duplicate", () => events.push("second"));`,
276 });
277 yield* Effect.scoped(
278 Effect.gen(function* () {
279 const summary = yield* run(options);
280 expect(announced).toHaveLength(2);
281 expect(announced[0]!.id).not.toBe(announced[1]!.id);
282 expect(summary.failed).toBe(1);
283 expect(summary.passed).toBe(1);
284 expect(ended[0]!.result.retries).toBe(1);
285 expect(state.events).toEqual(["first", "first", "second"]);
286 yield* Effect.sync(() => controller!.retryTest(announced[0]!.id));
287 yield* Deferred.await(retried).pipe(Effect.timeout("5 seconds"));
288 const ids = [announced[0]!.id, announced[1]!.id, announced[0]!.id];
289 expect(started).toEqual(ids);
290 expect(ended.map(({ meta }) => meta.id)).toEqual(ids);
291 expect(ended.map(({ result }) => result.status)).toEqual(["fail", "pass", "pass"]);
292 expect(state.events).toEqual(["first", "first", "second", "first"]);
293 expect(summary.failed).toBe(0);
294 expect(summary.passed).toBe(2);
295 expect(summary.failures).toHaveLength(0);
296 }),
297 );
298 expect(state.events).toEqual(["first", "first", "second", "first", "clean"]);
299 expect(state.roots[0]!.children.length).toBe(0);
300 }).pipe(Effect.provide(Layer.mergeAll(BunServices.layer, Layer.succeed(Reporter, reporter))));
301 }),
302 );
303
304 it.live("persists full output while keeping reporter previews bounded", () =>
305 Effect.gen(function* () {
306 const { options } = yield* setup({
307 "a.test.ts": `it.live("large-output", () => Effect.log("START-OF-OUTPUT:" + "x".repeat(600000) + ":END-OF-OUTPUT"));`,
308 });
309 let preview = "";
310 const reporter: ReporterService = {
311 waitForExit: () => Effect.void,
312 emit: (event) =>
313 Effect.sync(() => {
314 if (event._tag === "TestEnd")
315 preview = event.result.logs.map((entry) => entry.message).join("\n");
316 }),
317 };
318 yield* Effect.scoped(run(options).pipe(Effect.provide(Layer.succeed(Reporter, reporter))));
319 const fs = yield* FileSystem.FileSystem;
320 const full = yield* fs.readFileString(options.logFile);
321 expect(full).toContain("START-OF-OUTPUT:" + "x".repeat(600000) + ":END-OF-OUTPUT");
322 expect(preview.length).toBeLessThan(256 * 1024);
323 expect(preview).toContain("omitted from preview");
324 expect(preview).toContain(":END-OF-OUTPUT");
325 }).pipe(Effect.provide(BunServices.layer)),
326 );
327
328 it.live("finishes all file cleanups when interrupted during disposal", () =>
329 Effect.gen(function* () {
330 const { state, options } = yield* setup({
331 "a.test.ts": `
332 registerFileCleanup({ body: () => mark("cleanup-start").pipe(Effect.andThen(Effect.sleep("100 millis")), Effect.andThen(mark("first-clean"))) });
333 registerFileCleanup({ body: () => mark("second-clean") });
334 it("body", () => {});`,
335 });
336 const fiber = yield* Effect.scoped(run(options)).pipe(Effect.forkChild);
337 yield* Effect.sync(() => state.events.includes("cleanup-start")).pipe(
338 Effect.repeat({
339 schedule: Schedule.spaced("5 millis"),
340 times: 100,
341 until: (value) => value,
342 }),
343 );
344 yield* Fiber.interrupt(fiber);
345 expect(state.events).toEqual(["cleanup-start", "first-clean", "second-clean"]);
346 expect(state.roots[0]!.children.length).toBe(0);
347 }).pipe(Effect.provide(Layer.mergeAll(BunServices.layer, Layer.succeed(Reporter, silent)))),
348 );
349
350 it.live("queues interactive retries until the original file finishes user teardown", () =>
351 Effect.gen(function* () {
352 const { state, options } = yield* setup({
353 "a.test.ts": `
354 registerHook("afterAll", { body: () => mark("after") });
355 registerFileCleanup({ body: () => mark("clean") });
356 it("a", () => events.push("a"));
357 it.live("b", () => mark("b").pipe(Effect.andThen(Effect.sleep("50 millis"))));`,
358 });
359 const retried = yield* Deferred.make<void>();
360 let controller: RunController | undefined;
361 let completions = 0;
362 const reporter: ReporterService = {
363 waitForExit: () => Effect.void,
364 attachController: (value) =>
365 Effect.sync(() => {
366 controller = value;
367 }),
368 emit: (event) =>
369 Effect.gen(function* () {
370 if (event._tag === "TestEnd" && event.test.name === "a") {
371 if (++completions === 1) controller!.retryTest(event.test.id);
372 else yield* Deferred.succeed(retried, undefined);
373 }
374 }),
375 };
376 yield* Effect.scoped(
377 Effect.gen(function* () {
378 const summary = yield* run(options);
379 yield* Deferred.await(retried).pipe(Effect.timeout("5 seconds"));
380 expect(summary.passed).toBe(2);
381 }).pipe(Effect.provide(Layer.succeed(Reporter, reporter))),
382 );
383 expect(state.events).toEqual(["a", "b", "after", "a", "clean"]);
384 }).pipe(Effect.provide(BunServices.layer)),
385 );
386
387 it.live("cleans every file, including skipped, empty, filtered and failed imports", () =>
388 Effect.gen(function* () {
389 const { state, options } = yield* setup({
390 "a.test.ts": `
391 registerFileCleanup({ body: () => mark("a-clean") });
392 registerHook("afterAll", { body: () => mark("a-after") });
393 it("runs", () => events.push("a-body"));`,
394 "b.test.ts": `
395 registerFileCleanup({ body: () => mark("b-clean") });
396 registerHook("afterAll", { body: () => mark("b-after") });
397 it.skip("skipped", () => {});`,
398 "c.test.ts": `registerFileCleanup({ body: () => mark("c-clean") }); it.todo("todo");`,
399 "d.test.ts": `registerFileCleanup({ body: () => mark("d-clean") });`,
400 "e.test.ts": `
401 registerFileCleanup({ body: () => mark("e-clean") });
402 registerHook("afterAll", { body: () => mark("e-after") });
403 it.only("partial", () => events.push("partial-body"));
404 throw new Error("import-sentinel");`,
405 "f.test.ts": `
406 registerFileCleanup({ body: () => Effect.fail(new Error("cleanup-sentinel")) });
407 registerFileCleanup({ body: () => mark("f-clean") });
408 it("filtered", () => events.push("filtered-body"));`,
409 });
410 let summary: RunSummary | undefined;
411 yield* Effect.scoped(
412 Effect.gen(function* () {
413 summary = yield* run({
414 ...options,
415 filter: (title) => !title.includes("filtered"),
416 });
417 }),
418 ).pipe(Effect.exit);
419 expect(state.events).toEqual([
420 "a-body",
421 "a-after",
422 "a-clean",
423 "b-clean",
424 "c-clean",
425 "d-clean",
426 "e-clean",
427 "f-clean",
428 ]);
429 expect(
430 state.roots.every((root) => root.children.length === 0 && root.cleanups.length === 0),
431 ).toBe(true);
432 expect(summary?.passed).toBe(1);
433 expect(summary?.skipped).toBe(1);
434 expect(summary?.todo).toBe(1);
435 expect(summary?.fileFailures.length).toBe(2);
436 expect(state.pids.size).toBe(1);
437 expect(state.events.filter((event) => event.endsWith("clean")).length).toBe(6);
438 }).pipe(Effect.provide(Layer.mergeAll(BunServices.layer, Layer.succeed(Reporter, silent)))),
439 );
440
441 it.live("preserves global only selection while disposing excluded file roots", () =>
442 Effect.gen(function* () {
443 const { state, options } = yield* setup({
444 "a.test.ts": `registerFileCleanup({ body: () => mark("a-clean") }); it("excluded", () => events.push("excluded"));`,
445 "b.test.ts": `registerFileCleanup({ body: () => mark("b-clean") }); it.only("selected", () => events.push("selected"));`,
446 });
447 const summary = yield* Effect.scoped(run(options));
448 expect(summary.passed).toBe(1);
449 expect(state.events).toEqual(["a-clean", "selected", "b-clean"]);
450 expect(state.roots.every((root) => root.children.length === 0)).toBe(true);
451 }).pipe(Effect.provide(Layer.mergeAll(BunServices.layer, Layer.succeed(Reporter, silent)))),
452 );
453
454 it.live("keeps interactive retries alive until scope exit and then releases their roots", () =>
455 Effect.gen(function* () {
456 const { state, options } = yield* setup({
457 "a.test.ts": `registerFileCleanup({ body: () => mark("clean") }); it("retryable", () => { events.push("body"); if (events.length === 1) throw new Error("first-attempt"); });`,
458 });
459 const retried = yield* Deferred.make<void>();
460 let controller: RunController | undefined;
461 let id = "";
462 let ends = 0;
463 const reporter: ReporterService = {
464 waitForExit: () => Effect.void,
465 attachController: (value) =>
466 Effect.sync(() => {
467 controller = value;
468 }),
469 emit: (event) =>
470 Effect.gen(function* () {
471 if (event._tag === "TestEnd") {
472 id = event.test.id;
473 if (++ends === 2) yield* Deferred.succeed(retried, undefined);
474 }
475 }),
476 };
477 yield* Effect.scoped(
478 Effect.gen(function* () {
479 const summary = yield* run(options);
480 expect(summary.failed).toBe(1);
481 expect(state.events).toEqual(["body"]);
482 yield* Effect.sync(() => controller!.retryTest(id));
483 yield* Deferred.await(retried).pipe(Effect.timeout("5 seconds"));
484 expect(state.events).toEqual(["body", "body"]);
485 expect(summary.failed).toBe(0);
486 expect(summary.passed).toBe(1);
487 expect(summary.failures.length).toBe(0);
488 }).pipe(Effect.provide(Layer.succeed(Reporter, reporter))),
489 );
490 expect(state.events).toEqual(["body", "body", "clean"]);
491 expect(state.roots[0]!.children.length).toBe(0);
492 yield* Effect.sync(() => controller!.retryTest(id));
493 expect(state.events).toEqual(["body", "body", "clean"]);
494 }).pipe(Effect.provide(BunServices.layer)),
495 );