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

main
ts 426 lines 16.2 KB
Raw
1 /**
2 * Line-oriented reporter for non-interactive terminals and CI.
3 *
4 * Reports progress through the collection phase (importing a large suite's
5 * files takes tens of seconds before any test can run), then logs one line per
6 * test as it finishes, prefixed with a `[done/total]` counter (failures include
7 * their error and captured output inline); if nothing finishes for 10s it
8 * prints the list of currently-running tests; at the end every failure is
9 * repeated consolidated.
10 */
11 import * as Effect from "effect/Effect";
12 import * as Layer from "effect/Layer";
13 import type { LogEntry } from "./Model.ts";
14 import { formatPlanPreview } from "./Plan.ts";
15 import { Reporter, type RunSummary, type TestEvent, type TestResult } from "./Reporter.ts";
16 import { writeDirect } from "./StrayOutput.ts";
17
18 const isTty = process.stdout.isTTY === true;
19
20 const useColor = isTty && process.env.NO_COLOR === undefined;
21
22 const paint = (code: string) => (text: string) =>
23 useColor ? `\u001B[${code}m${text}\u001B[0m` : text;
24
25 const red = paint("31");
26 const green = paint("32");
27 const yellow = paint("33");
28 const dim = paint("2");
29 const bold = paint("1");
30
31 const formatDuration = (ms: number): string =>
32 ms >= 1000 ? `${(ms / 1000).toFixed(1)}s` : `${Math.round(ms)}ms`;
33
34 // Through the REAL stream — while a run is active, bare process.stdout is
35 // diverted into the run log (see StrayOutput.ts).
36 const write = (line: string): Effect.Effect<void> =>
37 Effect.sync(() => {
38 writeDirect(`${line}\n`);
39 });
40
41 // Captured output is replayed verbatim — no timestamp/level prefixes, no
42 // indentation. It should read exactly as it would have on a normal terminal.
43 const formatLogs = (logs: ReadonlyArray<LogEntry>): string =>
44 logs.map((log) => log.message).join("\n");
45
46 const indent = (text: string, prefix = " "): string =>
47 text
48 .split("\n")
49 .map((line) => `${prefix}${line}`)
50 .join("\n");
51
52 /** Mutable per-run reporter state. */
53 interface ReporterState {
54 readonly hookLogs: Map<string, ReadonlyArray<LogEntry>>;
55 readonly failedFiles: Set<string>;
56 /**
57 * Currently-executing work items: tests AND file-level hooks. Hooks must
58 * be tracked too — a `beforeAll` deploy can legitimately run for many
59 * minutes with no test starting or finishing, and if the stall report
60 * only covered tests, such a run would print nothing at all and read as
61 * deadlocked.
62 */
63 /** `queued`: started, but still waiting for its `exclusive` run lock. */
64 readonly running: Map<string, { label: string; startedAt: number; queued?: boolean }>;
65 /** Total tests in the run, known once RunStart arrives. */
66 total: number;
67 /** Tests that have reported a result so far, and how they went. */
68 completed: number;
69 passed: number;
70 failed: number;
71 lastEnd: number;
72 /** Running-set fingerprint of the last stall report — dedupes repeats. */
73 lastStallKey: string;
74 stallTimer: ReturnType<typeof setInterval> | undefined;
75 /** Files discovered for the collection phase. */
76 collectTotal: number;
77 collectDone: number;
78 collectStartedAt: number;
79 /** Last file whose import finished — the next one is what's importing now. */
80 collectCurrent: string;
81 lastCollectRender: number;
82 /** An un-terminated in-place progress line is on the terminal. */
83 collectLinePending: boolean;
84 /** True between CollectStart and RunStart. */
85 collecting: boolean;
86 }
87
88 /** Redraw cadence of the collection progress line. */
89 const COLLECT_RENDER_MS = 100;
90 /** Cadence of collection progress LINES when stdout isn't a terminal. */
91 const COLLECT_LINE_MS = 5_000;
92
93 const clearCollectLine = (state: ReporterState): void => {
94 if (!state.collectLinePending) return;
95 state.collectLinePending = false;
96 writeDirect("\r\u001B[2K");
97 };
98
99 /**
100 * Importing every test file takes tens of seconds on a large suite, and it
101 * happens before a single test can be reported — without progress the run is
102 * indistinguishable from a deadlock. On a TTY this repaints one line in place;
103 * otherwise it prints a line every few seconds. Either way the current file is
104 * shown, so a genuinely stuck import is identifiable by name.
105 *
106 * Also called from the reporter's 1s timer, not just on each imported file, so
107 * the elapsed time keeps ticking when an import is what's hanging.
108 */
109 const renderCollect = (state: ReporterState, force: boolean): void => {
110 const now = Date.now();
111 const cadence = isTty ? COLLECT_RENDER_MS : COLLECT_LINE_MS;
112 if (!force && now - state.lastCollectRender < cadence) return;
113 state.lastCollectRender = now;
114 const head = `collecting ${state.collectDone}/${state.collectTotal} test files (${formatDuration(now - state.collectStartedAt)})`;
115 // A pty that reports 0 columns (e.g. under `script`) is unknown, not narrow.
116 const columns = process.stdout.columns || 120;
117 const room = columns - head.length - 2;
118 const tail =
119 state.collectCurrent !== "" && room > 12 ? ` ${state.collectCurrent.slice(-room)}` : "";
120 if (isTty) {
121 state.collectLinePending = true;
122 writeDirect(`\r\u001B[2K${dim(head + tail)}`);
123 } else {
124 writeDirect(`${dim(head + tail)}\n`);
125 }
126 };
127
128 /** Settle the progress line on the final count so the total cost stays visible. */
129 const finishCollect = (state: ReporterState): void => {
130 state.collecting = false;
131 clearCollectLine(state);
132 state.collectCurrent = "";
133 renderCollect(state, true);
134 if (state.collectLinePending) {
135 state.collectLinePending = false;
136 writeDirect("\n");
137 }
138 };
139
140 /**
141 * `[ 12/240 ✓10 ✗2]` counter prefixed to every result line: how much of the
142 * run is left, and the running pass/fail tally. Every number is right-aligned
143 * on the total's width so the columns after it stay aligned, and each tally is
144 * only colored once it is non-zero (a red `✗0` reads as a failure at a glance).
145 */
146 const progress = (state: ReporterState, status: TestResult["status"]): string => {
147 state.completed += 1;
148 if (status === "pass") state.passed += 1;
149 else if (status === "fail") state.failed += 1;
150 const total = Math.max(state.total, state.completed);
151 const width = String(total).length;
152 const pad = (n: number) => String(n).padStart(width, " ");
153 const pass = `✓${pad(state.passed)}`;
154 const fail = `✗${pad(state.failed)}`;
155 return [
156 dim(`[${pad(state.completed)}/${total}`),
157 state.passed > 0 ? green(pass) : dim(pass),
158 `${state.failed > 0 ? red(fail) : dim(fail)}${dim("]")}`,
159 ].join(" ");
160 };
161
162 const STALL_AFTER_MS = 10_000;
163 const STALL_LIST_MAX = 25;
164
165 /**
166 * Print the currently-running tests when nothing has finished for a while.
167 * Printed AT MOST ONCE per distinct running set: while the exact same tests
168 * stay stuck, nothing is re-printed — the timestamp on the report tells the
169 * reader how long ago it was taken.
170 */
171 const printStalled = (state: ReporterState): void => {
172 if (state.running.size === 0) return;
173 if (Date.now() - state.lastEnd < STALL_AFTER_MS) return;
174 const key = [...state.running.entries()]
175 .map(([id, item]) => `${id}${item.queued ? " (queued)" : ""}`)
176 .sort()
177 .join("\n");
178 if (key === state.lastStallKey) return;
179 state.lastStallKey = key;
180 const now = Date.now();
181 const timestamp = new Date(now).toTimeString().slice(0, 8);
182 const items = [...state.running.values()].sort((a, b) => a.startedAt - b.startedAt);
183 const queued = items.filter((item) => item.queued).length;
184 const lines = [
185 dim(
186 `⧗ [${timestamp}] no tests finished in the last 10s — ${items.length - queued} still running` +
187 (queued > 0 ? `, ${queued} queued for an exclusive lock:` : ":"),
188 ),
189 ];
190 for (const item of items.slice(0, STALL_LIST_MAX)) {
191 const elapsed = formatDuration(now - item.startedAt);
192 lines.push(dim(` ${item.label} (${item.queued ? `queued ${elapsed}` : elapsed})`));
193 }
194 if (items.length > STALL_LIST_MAX) {
195 lines.push(dim(` … ${items.length - STALL_LIST_MAX} more`));
196 }
197 writeDirect(`${lines.join("\n")}\n`);
198 };
199
200 const onEvent = (event: TestEvent, state: ReporterState): Effect.Effect<void> => {
201 switch (event._tag) {
202 case "PlanPreview":
203 return Effect.sync(() => finishCollect(state)).pipe(
204 Effect.andThen(write(formatPlanPreview(event.phases))),
205 );
206 case "PlanPhaseStart":
207 return write(`\nPlan phase ${event.phase}/${event.phases}: ${event.tests} tests`);
208 case "CollectStart":
209 state.collectTotal = event.files.length;
210 state.collectStartedAt = Date.now();
211 state.collecting = true;
212 return write(dim(`collecting ${event.files.length} test files...`));
213 case "FileCollected":
214 return Effect.sync(() => {
215 state.collectDone += 1;
216 state.collectCurrent = event.file;
217 renderCollect(state, false);
218 });
219 case "RunStart":
220 state.total = event.tests.length;
221 return Effect.sync(() => finishCollect(state)).pipe(
222 Effect.andThen(
223 write(dim(`running ${event.tests.length} tests from ${event.files} files\n`)),
224 ),
225 );
226 case "TestStart":
227 return Effect.sync(() => {
228 state.running.set(event.test.id, {
229 label: `${event.test.file} > ${event.test.titlePath.join(" > ")}`,
230 startedAt: Date.now(),
231 queued: true,
232 });
233 });
234 case "TestLocked":
235 return Effect.sync(() => {
236 const item = state.running.get(event.test.id);
237 if (item !== undefined) {
238 state.running.set(event.test.id, { label: item.label, startedAt: Date.now() });
239 }
240 });
241 case "HookStart":
242 return Effect.sync(() => {
243 state.running.set(`${event.file} :: ${event.hook}`, {
244 label: `${event.file} > [${event.hook} hook]`,
245 startedAt: Date.now(),
246 });
247 });
248 case "HookEnd":
249 return Effect.sync(() => {
250 state.running.delete(`${event.file} :: ${event.hook}`);
251 state.lastEnd = Date.now();
252 });
253 case "TestEnd": {
254 state.running.delete(event.test.id);
255 state.lastEnd = Date.now();
256 const title = `${dim(event.test.file)} ${dim(">")} ${event.test.titlePath.join(` ${dim(">")} `)}`;
257 const duration = dim(`(${formatDuration(event.result.durationMs)})`);
258 const retries = event.result.retries > 0 ? yellow(` [retried x${event.result.retries}]`) : "";
259 const count = progress(state, event.result.status);
260 switch (event.result.status) {
261 case "pass":
262 return write(`${count} ${green("✓")} ${title} ${duration}${retries}`);
263 case "fail": {
264 state.failedFiles.add(event.test.file);
265 // Show the failure's details immediately so it can be inspected
266 // while the run continues (passed tests stay silent). The end-of-run
267 // Failures section repeats them consolidated, with file hook logs.
268 const lines = [`${count} ${red("✗")} ${title} ${duration}${retries}`];
269 if (event.result.error !== undefined) {
270 lines.push(indent(red(event.result.error)));
271 }
272 if (event.result.logs.length > 0) {
273 lines.push(dim("--- captured output ---"));
274 lines.push(formatLogs(event.result.logs));
275 lines.push(dim("--- end output ---"));
276 }
277 return write(lines.join("\n"));
278 }
279 case "skip":
280 return write(`${count} ${yellow("↓")} ${title} ${dim("[skipped]")}`);
281 case "todo":
282 // Warning-styled: a todo is unimplemented coverage, not a pass —
283 // it must not blend in with the dim noise around it.
284 return write(
285 `${count} ${yellow(
286 `○ ${event.test.file} > ${event.test.titlePath.join(" > ")} ${bold("[todo]")}`,
287 )}`,
288 );
289 }
290 }
291 case "FileEnd": {
292 if (
293 event.logs.length > 0 &&
294 (event.error !== undefined || state.failedFiles.has(event.file))
295 ) {
296 state.hookLogs.set(event.file, event.logs);
297 }
298 if (event.error !== undefined) {
299 return write(
300 `${red("✗")} ${bold(event.file)} ${red("failed to run")}\n${indent(red(event.error))}` +
301 (event.logs.length > 0
302 ? `\n${dim("--- file hook output (deploy/destroy) ---")}\n${formatLogs(event.logs)}`
303 : ""),
304 );
305 }
306 return Effect.void;
307 }
308 case "RunEnd":
309 return printSummary(event.summary, state.hookLogs);
310 default:
311 return Effect.void;
312 }
313 };
314
315 export const printSummary = (
316 summary: RunSummary,
317 /** Buffered deploy/destroy output per file, printed once per failing file. */
318 hookLogs?: ReadonlyMap<string, ReadonlyArray<LogEntry>>,
319 ): Effect.Effect<void> =>
320 Effect.gen(function* () {
321 if (summary.failures.length > 0) {
322 yield* write(`\n${bold(red(`Failures (${summary.failures.length})`))}\n`);
323 const hookLogsPrinted = new Set<string>();
324 for (const { meta, result } of summary.failures) {
325 yield* write(
326 `${red("✗")} ${bold(meta.file)} ${dim(">")} ${meta.titlePath.join(` ${dim(">")} `)}`,
327 );
328 if (result.error !== undefined) {
329 yield* write(indent(red(result.error)));
330 }
331 if (result.logs.length > 0) {
332 yield* write(dim("--- captured output ---"));
333 yield* write(formatLogs(result.logs));
334 yield* write(dim("--- end output ---"));
335 }
336 const fileHookLogs = hookLogs?.get(meta.file);
337 if (
338 fileHookLogs !== undefined &&
339 fileHookLogs.length > 0 &&
340 !hookLogsPrinted.has(meta.file)
341 ) {
342 hookLogsPrinted.add(meta.file);
343 yield* write(dim("--- file hook output (deploy/destroy) ---"));
344 yield* write(formatLogs(fileHookLogs));
345 yield* write(dim("--- end output ---"));
346 }
347 yield* write("");
348 }
349 }
350 // A bare list of what failed, immediately above the counts — the failure
351 // details above are long enough that the names scroll out of reach, and
352 // each line here is exactly what `-t` matches for a re-run.
353 const failedNames = [
354 ...summary.failures.map(({ meta }) => `${meta.file} > ${meta.titlePath.join(" > ")}`),
355 ...summary.fileFailures.map(({ file }) => `${file} (whole file)`),
356 ];
357 if (failedNames.length > 0) {
358 yield* write(bold(red(`Failed (${failedNames.length})`)));
359 for (const name of failedNames) {
360 yield* write(` ${red("✗")} ${name}`);
361 }
362 }
363 const parts = [
364 summary.failed > 0 ? red(`${summary.failed} failed`) : green("0 failed"),
365 ...(summary.dryRun ? [] : [green(`${summary.passed} passed`)]),
366 ...(summary.skipped > 0 ? [yellow(`${summary.skipped} skipped`)] : []),
367 ...(summary.todo > 0 ? [yellow(`${summary.todo} todo`)] : []),
368 ...(summary.plan
369 ? [
370 `${summary.plan.found} found`,
371 `${summary.plan.selected} selected by plan`,
372 yellow(`${summary.plan.excluded} excluded by plan`),
373 ]
374 : []),
375 ];
376 yield* write(
377 `\n${bold("Tests:")} ${parts.join(dim(" | "))} ${dim(`(${summary.files} files, ${formatDuration(summary.durationMs)})`)}`,
378 );
379 });
380
381 export const PlainReporterLive: Layer.Layer<Reporter> = Layer.sync(Reporter)(() => {
382 const state: ReporterState = {
383 hookLogs: new Map(),
384 failedFiles: new Set(),
385 running: new Map(),
386 total: 0,
387 completed: 0,
388 passed: 0,
389 failed: 0,
390 lastEnd: Date.now(),
391 lastStallKey: "",
392 stallTimer: undefined,
393 collectTotal: 0,
394 collectDone: 0,
395 collectStartedAt: Date.now(),
396 collectCurrent: "",
397 lastCollectRender: 0,
398 collectLinePending: false,
399 collecting: false,
400 };
401 // Ticks through both phases: repaints the collection line while files are
402 // importing, reports stalled tests once the run is executing.
403 const tick = (): void => {
404 if (state.collecting) renderCollect(state, false);
405 else printStalled(state);
406 };
407 return {
408 emit: (event) =>
409 Effect.suspend(() => {
410 if (
411 (event._tag === "CollectStart" || event._tag === "RunStart") &&
412 state.stallTimer === undefined
413 ) {
414 state.stallTimer = setInterval(tick, 1000);
415 // Don't hold the process open once the run's fibers finish.
416 (state.stallTimer as unknown as { unref?: () => void }).unref?.();
417 }
418 if (event._tag === "RunEnd" && state.stallTimer !== undefined) {
419 clearInterval(state.stallTimer);
420 state.stallTimer = undefined;
421 }
422 return onEvent(event, state);
423 }),
424 waitForExit: () => Effect.void,
425 };
426 });