@@ -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();