xonecas.io
64875
Mc

mini-coder / commit edf6ca8

fix: prevent tests from launching the TUI

xonecas2026-04-07 edf6ca8f38dd1f8516fbbf36e07fc7f734da8175 parent db4e871

4 files changed

AGENTS.md+1−0
@@ -52,6 +52,7 @@
5252 - **Compatibility policy is a product decision.** Before 1.0, prefer correct, simple semantics over speculative backward-compatibility shims. If a change may affect existing data or behavior, raise it explicitly, but do not implement migrations or compatibility layers unless the spec or user asks for them.
5353 - **Breaking changes still need to be called out.** Pre-1.0 is not a license to surprise the user. If a change intentionally trades compatibility for correctness or simplicity, say so explicitly in discussion and keep TODO/release notes aligned.
5454 - **TUI changes need real terminal validation.** For rendering, streaming, scrolling, resize, focus, or input changes, passing tests is not enough. Validate the behavior manually in `tmux` before calling the change done.
55+- **Work must stay within the agreed scope.** If you discussed and agreed with a user on the work to be done, don't deviate from it. Don't add scope, or deviate from the agreement.
5556
5657 ## Testing strategy
5758
src/index.test.ts+57−0
@@ -0,0 +1,57 @@
1+import { expect, test } from "bun:test";
2+import { join } from "node:path";
3+import { pathToFileURL } from "node:url";
4+
5+const REPO_ROOT = join(import.meta.dir, "..");
6+const INDEX_MODULE_URL = pathToFileURL(join(import.meta.dir, "index.ts")).href;
7+
8+async function importIndexInChild(): Promise<{
9+ exitCode: number;
10+ stdout: string;
11+ stderr: string;
12+}> {
13+ const proc = Bun.spawn(
14+ [
15+ process.execPath,
16+ "--eval",
17+ `await import(${JSON.stringify(INDEX_MODULE_URL)}); process.stdout.write("imported\\n");`,
18+ ],
19+ {
20+ cwd: REPO_ROOT,
21+ stdin: "ignore",
22+ stdout: "pipe",
23+ stderr: "pipe",
24+ env: process.env,
25+ },
26+ );
27+
28+ let timeoutId: ReturnType<typeof setTimeout> | null = null;
29+ const timeout = new Promise<never>((_, reject) => {
30+ timeoutId = setTimeout(() => {
31+ proc.kill();
32+ reject(new Error("Timed out waiting for index import to exit"));
33+ }, 1_000);
34+ });
35+
36+ let exitCode: number;
37+ try {
38+ exitCode = await Promise.race([proc.exited, timeout]);
39+ } finally {
40+ if (timeoutId) {
41+ clearTimeout(timeoutId);
42+ }
43+ }
44+
45+ const stdout = await new Response(proc.stdout).text();
46+ const stderr = await new Response(proc.stderr).text();
47+
48+ return { exitCode, stdout, stderr };
49+}
50+
51+test("importing index.ts does not start the CLI", async () => {
52+ const result = await importIndexInChild();
53+
54+ expect(result.exitCode).toBe(0);
55+ expect(result.stdout).toBe("imported\n");
56+ expect(result.stderr).toBe("");
57+});
src/index.ts+6−4
@@ -485,7 +485,9 @@ async function main(): Promise<void> {
485485 startUI(state);
486486 }
487487
488-main().catch((err) => {
489- console.error(err);
490- process.exit(1);
491-});
488+if (import.meta.main) {
489+ main().catch((err) => {
490+ console.error(err);
491+ process.exit(1);
492+ });
493+}
src/ui.test.ts+37−6
@@ -14,18 +14,18 @@ import {
1414 registerFauxProvider,
1515 unregisterApiProviders,
1616 } from "@mariozechner/pi-ai";
17-import {
18- type AppState,
19- DEFAULT_SHOW_REASONING,
20- DEFAULT_VERBOSE,
21-} from "./index.ts";
17+import type { AppState } from "./index.ts";
2218 import {
2319 createSession,
2420 createUiMessage,
2521 loadMessages,
2622 openDatabase,
2723 } from "./session.ts";
28-import { loadSettings } from "./settings.ts";
24+import {
25+ DEFAULT_SHOW_REASONING,
26+ DEFAULT_VERBOSE,
27+ loadSettings,
28+} from "./settings.ts";
2929 import { DEFAULT_THEME, type Theme } from "./theme.ts";
3030 import {
3131 applyEffortSelection,
@@ -170,6 +170,21 @@ async function waitFor(
170170 throw new Error("Timed out waiting for condition");
171171 }
172172
173+async function stopRunningTurn(
174+ state: Pick<AppState, "running" | "abortController">,
175+): Promise<void> {
176+ if (!state.running || !state.abortController) {
177+ return;
178+ }
179+
180+ state.abortController.abort();
181+ try {
182+ await waitFor(() => !state.running);
183+ } catch {
184+ // Ignore cleanup timeouts so test failures surface the original cause.
185+ }
186+}
187+
173188 describe("ui rendering", () => {
174189 test("reasoning defaults on and verbose defaults off", () => {
175190 expect(DEFAULT_SHOW_REASONING).toBe(true);
@@ -406,6 +421,7 @@ describe("ui rendering", () => {
406421 ),
407422 );
408423 } finally {
424+ await stopRunningTurn(state);
409425 process.env.PATH = originalPath;
410426 faux.unregister();
411427 state.db.close();
@@ -445,7 +461,19 @@ describe("ui rendering", () => {
445461 await waitFor(() => state.session !== null);
446462 expect(countSessions(state)).toBe(2);
447463 expect(state.session?.id).not.toBe(previousSession.id);
464+
465+ const sessionId = state.session?.id;
466+ if (!sessionId) {
467+ throw new Error("Expected a replacement session to be created");
468+ }
469+
470+ await waitFor(() =>
471+ loadMessages(state.db, sessionId).some(
472+ (message) => message.role === "assistant",
473+ ),
474+ );
448475 } finally {
476+ await stopRunningTurn(state);
449477 process.env.PATH = originalPath;
450478 faux.unregister();
451479 state.db.close();
@@ -485,6 +513,7 @@ describe("ui rendering", () => {
485513 });
486514 expect(logText).toContain("Done.");
487515 } finally {
516+ await stopRunningTurn(state);
488517 process.env.PATH = originalPath;
489518 faux.unregister();
490519 state.db.close();
@@ -532,6 +561,7 @@ describe("ui rendering", () => {
532561 expect(logText).toContain("$ sleep 0.2; echo tool-output");
533562 expect(logText).toContain("Running...");
534563 } finally {
564+ await stopRunningTurn(state);
535565 process.env.PATH = originalPath;
536566 faux.unregister();
537567 state.db.close();
@@ -683,6 +713,7 @@ describe("ui rendering", () => {
683713 expect(logText).toContain("tool-output");
684714 expect(logText).toContain("Done streaming");
685715 } finally {
716+ await stopRunningTurn(state);
686717 process.env.PATH = originalPath;
687718 unregisterApiProviders(sourceId);
688719 state.db.close();