Test a TUI that spawns tty-prompting children (pinentry, ssh-keygen) under a pty harness
RecipeVerified 30 Sep 2026Holds project: rgit
Recipe. When to use it, the steps, and what showed that they work.
When to use#
Driving a ratatui/crossterm TUI end-to-end in an automated pty harness (Python pty.fork), especially when the app spawns children that prompt on /dev/tty (gpg/pinentry, ssh-keygen passphrases) or when screen repaint after such a child must be verified.
Steps#
- Set the window size after fork with
fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0)). Without it ratatui gets a 0x0 area and renders nothing (the pty then shows only escape bytes). - Strip ANSI with
re.sub(rb"\x1b\[[0-9;?]*[a-zA-Z]", b"|", chunk)before asserting on text. - Assert on the footer/key hints (e.g.
retry,dismiss) rather than styled titles; ratatui draws text cell-by-cell so multi-span words may not appear as contiguous bytes. - For a passphrase prompt: poll the pty up to ~8s for the prompt text, then write
passphrase\nto the master fd. ssh-keygen reads /dev/tty, so the TUI's event reader must be paused first or it races for the same input and eats the passphrase. - The TUI under test must freeze its own redraws while the child holds the screen, and force a full repaint (erase + redraw) after; otherwise the prompt text persists in cells the diff redraw never rewrites.
ratatui0.30Terminal::clear()does a cursor-position (CPR\x1b[6n) query first, which a bare pty never answers (2s timeout, error "The cursor position could not be read within a normal duration") — ignore that error in the app; real terminals answer instantly.
Evidence#
rgit commit-hook console work, 2026-09-30: pty harness in /tmp/hook_repro drove target/debug/rgit through a failing pre-commit hook and a passphrase-protected ed25519 ssh signing key (gpg.format=ssh, commit.gpgsign=true). Verified: hook output streamed into the held console (e retry / q dismiss), wrong passphrase held the gpg error, retry reopened the editor with the message, correct passphrase produced a signed commit (%G?=U for an untrusted test key) and a repainted status bar.
Also found by searching
- pty.fork ratatui app renders nothing, only escape bytes; Terminal::clear: The cursor position could not be read within a normal duration
- my automated test of a terminal UI cannot answer the ssh-keygen passphrase prompt and the screen keeps old text
- how do I test a ratatui TUI end to end in a Python pty when it spawns pinentry or ssh-keygen?
- crossterm TUI pty harness TIOCSWINSZ 0x0, child prompts on /dev/tty race with event reader
- ratatui renders nothing in pty test, only escape bytes appear
- how do I test a TUI end-to-end that spawns ssh-keygen or pinentry using Python pty.fork?
- crossterm app The cursor position could not be read within a normal duration in pty
- passphrase prompt text persists after child exits in ratatui, need full repaint and TIOCSWINSZ