@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 113 lines 3.62 KB
Raw
1 import { fileURLToPath } from "node:url";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as ChildProcess from "effect/process/ChildProcess";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { PlatformServices } from "@/Util/PlatformServices.ts";
9
10 const FIXTURE = fileURLToPath(new URL("./fixtures/shutdown-feedback-fixture.ts", import.meta.url));
11
12 /**
13 * Spawn the fixture (piped stdio, so the feedback takes the non-TTY log-line
14 * branch), wait for its ready line, and expose accumulated stderr.
15 */
16 const spawnFixture = (env: Record<string, string> = {}) =>
17 Effect.gen(function* () {
18 const handle = yield* ChildProcess.make("bun", [FIXTURE], {
19 env,
20 extendEnv: true,
21 stdin: "ignore",
22 stdout: "pipe",
23 stderr: "pipe",
24 killSignal: "SIGTERM",
25 forceKillAfter: "1 second",
26 });
27 const chunks: string[] = [];
28 yield* handle.stderr.pipe(
29 Stream.decodeText,
30 Stream.runForEach((chunk) =>
31 Effect.sync(() => {
32 chunks.push(chunk);
33 }),
34 ),
35 Effect.ignore,
36 Effect.forkScoped,
37 );
38 yield* handle.stdout.pipe(
39 Stream.decodeText,
40 Stream.splitLines,
41 Stream.filter((line) => line.includes("ready")),
42 Stream.take(1),
43 Stream.runDrain,
44 Effect.timeout(Duration.seconds(10)),
45 );
46 return {
47 handle,
48 sigint: Effect.sync(() => process.kill(handle.pid, "SIGINT")),
49 stderr: () => chunks.join(""),
50 };
51 });
52
53 const waitForStderr = (read: () => string, text: string) =>
54 Effect.sync(read).pipe(
55 Effect.repeat({
56 schedule: Schedule.spaced(Duration.millis(50)),
57 until: (output) => output.includes(text),
58 times: 100,
59 }),
60 Effect.flatMap((output) =>
61 output.includes(text)
62 ? Effect.void
63 : Effect.fail(new Error(`stderr never contained ${JSON.stringify(text)}: ${output}`)),
64 ),
65 );
66
67 describe("shutdown feedback", { tags: ["unit", "local"] }, () => {
68 it.live(
69 "a slow shutdown prints one delayed status line",
70 () =>
71 Effect.gen(function* () {
72 const fixture = yield* spawnFixture({
73 SHUTDOWN_FIXTURE_EXIT_AFTER_MS: "800",
74 });
75 yield* fixture.sigint;
76 yield* Effect.sleep(Duration.millis(300));
77 expect(fixture.stderr()).not.toContain("Shutting down");
78 yield* waitForStderr(fixture.stderr, "Shutting down");
79 expect(yield* fixture.handle.exitCode).toBe(0);
80 expect(fixture.stderr().match(/Shutting down/g)).toHaveLength(1);
81 }).pipe(Effect.scoped, Effect.provide(PlatformServices)),
82 { timeout: 30_000 },
83 );
84
85 it.live(
86 "a shutdown finishing within the delay stays silent",
87 () =>
88 Effect.gen(function* () {
89 const fixture = yield* spawnFixture({
90 SHUTDOWN_FIXTURE_EXIT_AFTER_MS: "50",
91 });
92 yield* fixture.sigint;
93 expect(yield* fixture.handle.exitCode).toBe(0);
94 expect(fixture.stderr()).not.toContain("Shutting down");
95 }).pipe(Effect.scoped, Effect.provide(PlatformServices)),
96 { timeout: 30_000 },
97 );
98
99 it.live(
100 "a suppressed process stays silent",
101 () =>
102 Effect.gen(function* () {
103 const fixture = yield* spawnFixture({
104 SHUTDOWN_FIXTURE_SUPPRESS: "1",
105 SHUTDOWN_FIXTURE_EXIT_AFTER_MS: "800",
106 });
107 yield* fixture.sigint;
108 expect(yield* fixture.handle.exitCode).toBe(0);
109 expect(fixture.stderr()).not.toContain("Shutting down");
110 }).pipe(Effect.scoped, Effect.provide(PlatformServices)),
111 { timeout: 30_000 },
112 );
113 });