Skip to content

feat(tui-test): test terminal queries before startup completes - #37

Merged
factory-ain3sh merged 1 commit into
mainfrom
ainesh/cli-2240-startup-probe
Sep 10, 2026
Merged

factory-ain3sh merged 1 commit into
mainfrom
ainesh/cli-2240-startup-probe

Conversation

@factory-droid

@factory-droid factory-droid Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Description

What

Tests can observe and answer terminal queries from the first PTY output, before startup readiness completes. Five real-PTY cases pass; disabling raw-output delivery makes all four dependent cases fail.

Why

The runner can wait five seconds before beforeEach and test callbacks. Terminal probes with shorter deadlines have already timed out by then, so those callbacks cannot test startup interactions.

How

test.onSpawn installs synchronous observers before the terminal subscribes to PTY output. terminal.onData exposes raw chunks with an unsubscribe function. Trace recording and xterm rendering keep their existing path.

Change Shape

PTY created
  → onSpawn hooks, outermost first
  → subscribe to output: trace + render + onData
  → existing readiness wait
  → beforeEach / test
  → teardown removes output listeners

Related Issue

Part of CLI-2240.

This is the prerequisite harness change for the Factory CLI startup-overlap regression tests. The CLI pins 26d49bdb26d81102220738dc291f552046e6e207; merge this PR before its CLI consumer.

Reviewer Guide

Review depth: Standard. Additive public API at a timing-sensitive boundary.

  1. src/terminal/term.ts: observers register before output delivery; exit, kill, and setup failure clean up listeners.
  2. src/runner/worker.ts and src/test/: suite inheritance and hook execution order.
  3. test/terminal-startup.e2e.test.ts: immediate/delayed replies, silence, unsubscribe, fresh per-test observers, and setup failure.

The README documents the new contract. Its other whitespace changes come from the configured formatter.

Risk & Impact

Hooks must remain synchronous; asynchronous setup belongs in beforeSpawn. Chunks can split escape sequences, and late subscriptions do not replay previous output. Observers run on the output-delivery path, so expensive callbacks can delay a test.

Existing users need not register a hook or subscriber. No readiness timeout changes.

Contract Delta

  • test.onSpawn((terminal) => void): synchronous, outermost-first, once per terminal.
  • terminal.onData((chunk) => void): () => void: future raw output only; subscriptions end on exit/kill.

Verification

Behavior verified @ 26d49bdb: five targeted PTY tests pass on Linux. Removing the generated raw-output emit line fails four dependent tests while the setup-error control passes; rebuilding restores five passes.

Sentinel test: test/terminal-startup.e2e.test.ts, “answers a startup query before test callbacks and preserves rendering.”

Rerun: npm run build, then node index.js terminal-startup.e2e.test.ts.

Not tested locally: macOS, Windows, Bun, and the full Node version matrix. Existing CI owns those lanes.

Standard validators: TypeScript build, scoped ESLint and Prettier pass. Slop-scan exits 1 on three pre-existing catches shifted by seven lines; all three were verified inside an unchanged 371-line block, with unchanged score and no new findings.

Install synchronous onSpawn hooks before PTY output subscription so tests
can respond to terminal queries before the runner's readiness wait ends.
Expose raw output with disposable onData subscriptions and preserve the
existing trace and rendering path. Cover immediate, delayed, silent, and
failed-setup cases with real PTYs.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@factory-ain3sh
factory-ain3sh self-requested a review September 10, 2026 23:55
@factory-ain3sh
factory-ain3sh merged commit 4a0c705 into main Sep 10, 2026
4 of 34 checks passed
@factory-ain3sh
factory-ain3sh deleted the ainesh/cli-2240-startup-probe branch September 10, 2026 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant