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

main
ts 171 lines 5.7 KB
Raw
1 /**
2 * The Reporter is an Effect service the runner emits structured events into.
3 * Two implementations ship with the CLI: a plain line-oriented reporter for
4 * non-interactive terminals / CI, and an opentui TUI for interactive use.
5 */
6 import * as Context from "effect/Context";
7 import type * as Effect from "effect/Effect";
8 import type { LogEntry } from "./Model.ts";
9
10 export type TestStatus = "pass" | "fail" | "skip" | "todo";
11
12 export interface TestMeta {
13 readonly tags: ReadonlyArray<string>;
14 readonly optInTags: ReadonlyArray<string>;
15 /** Unique test identity, stable across retries; use titlePath for display. */
16 readonly id: string;
17 readonly file: string;
18 readonly titlePath: ReadonlyArray<string>;
19 readonly name: string;
20 }
21
22 export interface TestResult {
23 readonly status: TestStatus;
24 readonly durationMs: number;
25 /** Pretty-printed failure (message + relevant stack), if failed. */
26 readonly error?: string | undefined;
27 /** Buffered Effect log / Console output captured during the test. */
28 readonly logs: ReadonlyArray<LogEntry>;
29 /** Number of retries that were attempted before this result. */
30 readonly retries: number;
31 }
32
33 export interface RunSummary {
34 readonly dryRun?: boolean;
35 /** Coverage within the command's path, name, .only and global tag filters. */
36 readonly plan?:
37 | {
38 readonly found: number;
39 readonly selected: number;
40 readonly excluded: number;
41 }
42 | undefined;
43 readonly files: number;
44 readonly passed: number;
45 readonly failed: number;
46 readonly skipped: number;
47 readonly todo: number;
48 readonly durationMs: number;
49 readonly failures: ReadonlyArray<{ meta: TestMeta; result: TestResult }>;
50 /**
51 * Files that failed as a whole (import error, or a `beforeAll`/`afterAll`
52 * hook failure) rather than through an individual test. Counted in
53 * {@link failed} alongside `failures`.
54 */
55 readonly fileFailures: ReadonlyArray<{ file: string; error: string }>;
56 }
57
58 export type TestEvent =
59 | {
60 readonly _tag: "PlanPreview";
61 readonly phases: ReadonlyArray<
62 ReadonlyArray<{
63 readonly tags: ReadonlyArray<string>;
64 readonly concurrency: number | "unbounded";
65 readonly tests: number;
66 readonly files: number;
67 readonly skipped: number;
68 }>
69 >;
70 }
71 | {
72 readonly _tag: "PlanPhaseStart";
73 readonly phase: number;
74 readonly phases: number;
75 readonly tests: number;
76 }
77 | { readonly _tag: "CollectStart"; readonly files: ReadonlyArray<string> }
78 | {
79 /** Import progress: one per file during the collection phase. */
80 readonly _tag: "FileCollected";
81 readonly file: string;
82 }
83 | {
84 /**
85 * Collection is complete; execution is about to begin. Carries the
86 * full (filtered) test list for the whole run.
87 */
88 readonly _tag: "RunStart";
89 readonly files: number;
90 readonly tests: ReadonlyArray<TestMeta>;
91 }
92 | {
93 readonly _tag: "FileStart";
94 readonly file: string;
95 /**
96 * LIVE reference to the file's hook log buffer (deploy/destroy). The
97 * runner appends to it as hooks execute — single-process, so reporters
98 * may read it incrementally (e.g. the TUI tailing a detail pane).
99 */
100 readonly logs?: ReadonlyArray<LogEntry>;
101 }
102 | {
103 /** A file-level hook (deploy/destroy) started running. */
104 readonly _tag: "HookStart";
105 readonly file: string;
106 readonly hook: "beforeAll" | "afterAll";
107 }
108 | {
109 readonly _tag: "HookEnd";
110 readonly file: string;
111 readonly hook: "beforeAll" | "afterAll";
112 }
113 | {
114 readonly _tag: "FileEnd";
115 readonly file: string;
116 /** Buffered output of file-level hooks (deploy/destroy). */
117 readonly logs: ReadonlyArray<LogEntry>;
118 readonly error?: string | undefined;
119 }
120 | {
121 readonly _tag: "TestStart";
122 readonly test: TestMeta;
123 /** LIVE reference to the test's captured-output buffer (see FileStart). */
124 readonly logs?: ReadonlyArray<LogEntry>;
125 }
126 | {
127 /**
128 * The test holds its run lock (`exclusive`) and starts executing. Until
129 * then it is queued: an exclusive test can wait out a busy phase.
130 */
131 readonly _tag: "TestLocked";
132 readonly test: TestMeta;
133 }
134 | {
135 readonly _tag: "TestEnd";
136 readonly test: TestMeta;
137 readonly result: TestResult;
138 }
139 | { readonly _tag: "RunEnd"; readonly summary: RunSummary };
140
141 /**
142 * Interactive control over a live (or finished) run. The runner hands this
143 * to the Reporter so the TUI can retry and kill tests.
144 *
145 * Retries re-run the test body as-is: for files whose state was torn down by
146 * `afterAll` (deploy-once suites) the retried body may fail on missing
147 * infrastructure — `test.provider`-style self-contained tests retry cleanly.
148 */
149 export interface RunController {
150 /** Re-run a finished test. No-op while the test is still running/queued. */
151 readonly retryTest: (id: string) => void;
152 /** Re-run every finished test of a file. */
153 readonly retryFile: (file: string) => void;
154 /** Interrupt a currently-running test (reported as failed/killed). */
155 readonly killTest: (id: string) => void;
156 }
157
158 export interface ReporterService {
159 readonly emit: (event: TestEvent) => Effect.Effect<void>;
160 /**
161 * Runs after RunEnd. The plain reporter returns immediately; the TUI blocks
162 * until the user quits so results can be inspected.
163 */
164 readonly waitForExit: (summary: RunSummary) => Effect.Effect<void>;
165 /** Called once by the runner when interactive control becomes available. */
166 readonly attachController?: (controller: RunController) => Effect.Effect<void>;
167 }
168
169 export class Reporter extends Context.Service<Reporter, ReporterService>()(
170 "alchemy-test/Reporter",
171 ) {}