Skip to content

test(tui): wait for each key's effect in the line and color editor tests - #788

Merged
sirmalloc merged 2 commits into
sirmalloc:mainfrom
eric-engberg:test/wait-for-ink-keys
Nov 5, 2026
Merged

sirmalloc merged 2 commits into
sirmalloc:mainfrom
eric-engberg:test/wait-for-ink-keys

Conversation

@eric-engberg

Copy link
Copy Markdown
Contributor

What

The line editor tests (ItemsEditor.test.ts) and the bold/dim test in ColorMenu.test.tsx no longer sleep a fixed 25ms after rendering and after each key. Each step now waits for the key's visible effect, then lets React catch up before the next key, the same way #652 converted the other Ink tests. The tests still check everything they checked before, and they no longer fail when the machine is busy.

Why

These tests wrote a key, slept 25ms, then asserted (26 sleeps in ItemsEditor.test.ts, 2 in ColorMenu.test.tsx). Ink redraws on its own schedule. Its useInput also swaps in each render's handler from a passive effect, so a key that arrives before that effect runs goes to the previous render's handler and is applied to the old state. Under load, 25ms is sometimes not enough for either step.

Measured with Bun 1.4.2 on a 10-core Mac. Each run is bun test on the two files (16 tests). Runs were started N at a time, for 10 rounds (5 at 48):

Load main this branch
one at a time, 10 runs 0 of 10 failed 0 of 10 failed
4 in parallel 0 of 40 failed 0 of 40 failed
16 in parallel 1 of 160 failed 0 of 160 failed
32 in parallel 153 of 320 failed 0 of 320 failed
48 in parallel not run 0 of 240 failed

The failing tests on main at 32 in parallel, by count: number styles 106, label edit and raw value 45, reselecting Model 12, every key typed 8, reselecting Session Cost 3 (some runs failed more than one). A typical failure, the number style test after its second .:

Expected to contain: "1. Tokens Input (whole)"
Received: "Edit Line 1\n…\n▶  1. Tokens Input (compact)\n…"

The frame drawn after the second . still shows (compact).

How

  • flushInk is gone from both files. Each use is now waitFor from src/tui/__tests__/helpers/wait-for-ink.ts, which both files already import. waitFor retries its assertions every 10ms for up to 3s, then yields two setImmediate turns so React can swap in the new input handler before the next key.
  • What each wait checks:
    • Line editor list: the row's new style suffix after ., the saved label after Enter, (raw value) after r, the row again after Tab.
    • Label editor: its (default: …) prompt after e, then the draft after each backspace or typed key. The cursor at the end shows as a trailing space, so after the first backspace the editor shows "Model: " (default: "Model: ").
    • Widget picker: Search: (none) after →, then the Search: row after each typed character.
    • Color menu: the selected row in the newest frame (▶ 2: Cache Read) before the current-style line is read from that frame.
  • Before each picker character, and before the Tab test opens the label editor a second time, the test now clears the captured output, so each wait reads only frames drawn after the key. In the picker this matters for a typed space. The wait looks for the typed text without its trailing space, because Ink drops trailing spaces from each line when the output has no color, and the frame before the space already shows that text.
  • Assertions that only restated a wait moved into it. The negative assertions stay after the wait and check the same output as before.

Left as is:

  • "keeps every key typed into the label editor before a re-render" still sends its two backspaces one setImmediate apart, with no wait between them. That gap is what the test covers. It now waits for both edits to show before pressing Enter.
  • The waits in the two label-editing tests and the picker test read the editor's draft and the picker's search row, which main's versions didn't check. A change to how the editor shows the draft now fails those tests as well as the joined-emoji test, which already checks that format.

Demo

No demo: only tests change, so nothing in the TUI or the status line looks or works differently.

Testing

  • On main as of fix: use env override and settings for usable context ratio calculation #407 (71e8b42): merges cleanly, bun run lint is clean, and bun test gives 3938 pass, 0 fail. The built CLI under Bun 1.4.4 and Node 26.13.0 matches main in plain and Powerline mode with the baseline configs, and the TUI opens and exits cleanly. It adds no JavaScript or Node API newer than Node 14, the build's target.
  • bun test: 3168 pass, 0 fail (178 files), the same as main. bun run lint is clean.
  • The two files under load: the table above.
  • I checked that the waits don't change what the tests catch. I broke the code under test one change at a time: the text cursor reading stale state, the label keybind shown in raw value mode, a type change keeping or dropping the label, Tab keeping the label, the number style cycle not returning to the default, the missing bold marker. Each break failed the same test on main and on this branch.
  • Vitest on Node 26.10.0, the two files: 16 of 16 pass, the same as main.
  • The built CLI renders the same in plain and Powerline mode under Bun 1.4.2 and Node 26.10.0, and the same as main. The TUI opens and exits cleanly under both.

The line editor and color menu tests slept a fixed 25ms after rendering
and after each key, then asserted. Ink redraws, and React re-attaches
Ink's input listener, on their own schedule. On a loaded machine the
assertion ran before the redraw, or the next key reached the listener
left from before the last redraw and was applied to the old state.
Under load these tests failed intermittently, the number style and
label tests most often.

Each wait now retries until the key's visible effect shows (the row's
new suffix, the label editor's draft, the picker's search text, the
moved highlight), then yields two setImmediate turns before the next
key, through the shared helper in src/tui/__tests__/helpers/wait-for-ink.ts.
Assertions that only restated a wait move into it; the rest are
unchanged.
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.

2 participants