Files
vue-tui/packages/runtime-tests/integration/pty/exit.test.ts
T
Yunfei He 2372b6b03b feat(runtime): own raw mode for the interactive lifetime by default (rawMode option) (#120)
Add a `rawMode?: 'always' | 'auto'` mount option (replacing the dead, unwired
`rawMode?: boolean`), defaulting to 'always'.

- 'always' (default): the App takes a lifetime raw-mode hold at mount (gated on
  interactive + a TTY stdin), so raw mode is held for the whole run regardless of
  which input composables are mounted. Keystrokes never echo into the rendered
  frame on a no-input/streaming screen, and Ctrl+C is handled consistently on
  every screen (e.g. it reaches an agent's "interrupt generation" handler instead
  of becoming a kernel SIGINT). Because owning raw mode ref()s stdin, the app
  stays alive until an explicit unmount()/exit() — it does NOT auto-exit when idle.
- 'auto': Ink's original lazy model — raw mode is acquired only while a useInput /
  useFocus / usePaste is mounted, so a no-input screen returns to cooked mode and a
  no-input app auto-exits. The opt-out for inline / render-and-exit tools.

This is a deliberate divergence from Ink (the cross-framework norm — Bubble Tea,
Textual, Ratatui, prompt_toolkit all own the terminal for the program lifetime;
Ink's hook-driven model is the outlier). Documented in
.agents/docs/ink-divergences.md.

Implementation: the App holds a `lifetimeFloor` ref via holdRawModeForLifetime();
input composables stack above it. The per-consumer clearInputState is re-based to
the floor so a buffered partial escape (e.g. a lone ESC at a screen transition)
can't bleed into the next consumer — cleared both when the last consumer releases
and when the first consumer re-acquires above the floor (covers same-tick swaps
AND a delayed idle→input transition). The data listener and raw toggle stay on
until teardown, where dispose() releases the floor ref (raw disabled + stdin
unref'd exactly once).

Tests: rawMode-lifecycle ('always' holds raw with no input; 'auto' stays cooked;
no mid-session oscillation; no partial-escape bleed across a swap or an idle gap);
PTY exit-rawmode-always (a no-input 'always' app stays alive and exits on Ctrl+C).
The 6 auto-exit PTY fixtures are pinned to 'auto' (they model render-and-exit).

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-01 17:00:09 +08:00

128 lines
4.9 KiB
TypeScript

import { test as it, expect } from "vite-plus/test";
import stripAnsi from "strip-ansi";
import { run } from "./helpers/run.ts";
import term from "./helpers/term.ts";
it("exit normally without unmount() or exit()", async () => {
const output = await run("exit-normally");
expect(output).toContain("exited");
});
it("exit on unmount()", async () => {
const output = await run("exit-on-unmount");
expect(output).toContain("exited");
});
it("exit when app finishes execution", async () => {
await expect(run("exit-on-finish")).resolves.toBeDefined();
});
it("exit on exit()", async () => {
const output = await run("exit-on-exit");
expect(output).toContain("exited");
});
it("exit on exit() with error", async () => {
const output = await run("exit-on-exit-with-error");
expect(output).toContain("errored");
});
it("exit on exit() with error with value property", async () => {
const output = await run("exit-on-exit-with-error-value-property");
expect(output).toContain("errored");
});
it("exit on exit() with result value", async () => {
const output = await run("exit-on-exit-with-result");
expect(output).toContain("result:hello from vue-tui");
});
it("exit on exit() with object result", async () => {
const output = await run("exit-on-exit-with-value-object");
expect(output).toContain("result:hello from vue-tui object");
});
it("exit on exit() with raw mode", async () => {
const output = await run("exit-raw-on-exit");
expect(output).toContain("exited");
});
it("exit on exit() with raw mode with error", async () => {
const output = await run("exit-raw-on-exit-with-error");
expect(output).toContain("errored");
});
it("exit on unmount() with raw mode", async () => {
const output = await run("exit-raw-on-unmount");
expect(output).toContain("exited");
});
it("exit with thrown error", async () => {
// run() rejects on a non-zero exit code; the fixture catches the rejected
// waitUntilExit, logs "errored", and exits 0 deterministically (Ink exit.tsx:71-74).
const output = await run("exit-with-thrown-error");
expect(output).toContain("errored");
});
it("don't exit while raw mode is active", async () => {
// Port of Ink exit.tsx:100-114 ("don't exit while raw mode is active"). The
// fixture keeps raw mode enabled and writes __READY__ after 500ms. With raw
// mode active and no input, the process must STAY ALIVE — Node's keep-alive
// (the raw-mode stdin handle) blocks exit. We wait ~500ms with NO input and
// confirm it has not exited, THEN send 'q' to trigger unmount + exit.
const ps = term("exit-double-raw-mode");
// After __READY__ (resolved internally by the term helper), wait 500ms and
// ensure the process has NOT exited (no input has been sent yet).
const exitedDuringWait = await Promise.race([
ps.waitForExitInfo().then(() => true),
new Promise<false>((resolve) => setTimeout(() => resolve(false), 500)),
]);
expect(exitedDuringWait).toBe(false);
// Now send 'q': the fixture unmounts and the process exits cleanly.
ps.write("q");
await ps.waitForExit();
expect(ps.output).toContain("exited");
});
it("rawMode 'always' (default): a no-input app stays alive and exits on Ctrl+C", async () => {
// The default rawMode 'always' holds raw mode for the whole interactive run, so
// even a no-input app (no useInput, no stdin listener) does NOT auto-exit — the
// lifetime raw-mode ref keeps the loop alive. (Under 'auto' the same app exits
// immediately; cf. exit-normally, pinned to 'auto'.) Ctrl+C still exits it
// cleanly because raw mode is held and exitOnCtrlC defaults to true — the
// headline benefit that Ctrl+C works on a no-input screen.
const ps = term("exit-rawmode-always");
const exitedDuringWait = await Promise.race([
ps.waitForExitInfo().then(() => true),
new Promise<false>((resolve) => setTimeout(() => resolve(false), 500)),
]);
expect(exitedDuringWait).toBe(false);
ps.write("\x03"); // Ctrl+C
await ps.waitForExit();
expect(ps.output).toContain("exited");
});
it("exit when DEV is set", async () => {
// Port of Ink exit.tsx:133-142 ("exit when DEV is set"). DEV is inert in
// vue-tui (no React DevTools hookup), so exit-normally must still exit cleanly.
const output = await run("exit-normally", { env: { DEV: "true" } });
expect(output).toContain("exited");
});
it("exit on exit() with error and static output", async () => {
const output = await run("exit-with-static");
expect(output).toContain("errored");
// Static items A/B/C must each render EXACTLY once — not duplicated (Ink #397,
// exit.tsx:144-155). With the fixture's function slots there are no Vue warns
// polluting stdout, so the body lines are clean single occurrences.
const lines = stripAnsi(output).split(/\r?\n/);
expect(lines.filter((line) => line === "A")).toHaveLength(1);
expect(lines.filter((line) => line === "B")).toHaveLength(1);
expect(lines.filter((line) => line === "C")).toHaveLength(1);
});