Internals TUI Testing - NormB/sipnab GitHub Wiki
sipnab tests its TUI at three levels.
These tests use ratatui's TestBackend to render views to an in-memory buffer. Then they use insta for snapshot comparison.
cargo test --features tui --test tui_snapshot_testUse this when rendering changes intentionally.
-
--acceptrewrites the.snapfiles in place. - Stage them in the same commit.
- That is what keeps CI comparing against the new rendering.
# Run all of these, in order.
cargo insta test --features tui --accept
git add tests/snapshots/- Create a test function that renders to
TestBackend - Call
insta::assert_snapshot!(buffer_to_string(&terminal)) - Run
cargo insta test --acceptto create the initial snapshot - Commit the
.snapfile
These tests check App state transitions without rendering. They use App::new_test() and App::handle_key().
cargo test --features tui --test tui_state_testThese tests drive the real binary inside a detached tmux session. They do not use a bare PTY.
- sipnab's TUI queries the terminal at startup.
- crossterm emits
ESC[6nto read the cursor position.
- crossterm emits
- A bare pseudo-terminal has no emulator behind it to answer.
- So the TUI aborts with
cursor position could not be read.
- So the TUI aborts with
- tmux is a terminal emulator. It does three things:
- It answers the query.
- It provides a real window size.
- It lets the test send keys and snapshot the screen with
capture-pane.
- Each test session runs on its own tmux server (
tmux -L <session>), and removes that server's socket when it ends.- A tmux server exits when its last session ends. On one shared server, a
test starting a session at that moment failed with
tmux new-session failed.
- A tmux server exits when its last session ends. On one shared server, a
test starting a session at that moment failed with
- These tests are
#[ignore]by default. - They need
tmuxonPATH.
cargo test --features tui --test tui_e2e_test -- --ignoredThese tests are slower (~2s each). They may be flaky in CI.
These tests check how the TUI stops: on SIGTERM, SIGINT and SIGHUP, and when its terminal goes away. They run the real binary on a pseudo-terminal the test owns, without tmux, because a hang-up is something the test does to that terminal.
- Linux. The pty set-up uses Linux's
openptyandTIOCSCTTYbehavior. - A build with the
tuifeature. The tests are not#[ignore]d, so the suite runs them.
cargo test --features tui --test tui_lost_terminal_test- A reader thread plays the terminal. It collects what sipnab writes and
answers the cursor-position query (
ESC[6n) that crossterm sends at startup. -
wait_until_drawnwaits for the call list (INVITE) on the terminal. Only then does a test send a signal or hang the terminal up, so sipnab has installed its signal handlers and drawn the first frame. -
wait_for_exitreturns the exit status once the child has ended AND the reader has read everything the child wrote. The reader ends when a read of the pty's master side fails withEIO, which Linux returns once no process holds the slave side open and no written byte remains.- Before this,
wait_for_exitreturned at the exit alone. The reader runs on its own schedule, so on a loaded host the terminal restore (ESC[?1049l) could still be unread when the test asked for it, andsigterm_stops_a_tui_and_restores_the_terminalcould fail although sipnab had written the restore.
- Before this,
-
stall_readerstops the reader for a set time and returns once it has stopped. The*_when_the_reader_lags_the_exittests use it to put the exit before the reader's next read, which is the order a loaded host can produce. -
spawn_commandruns any program on the same kind of pty, for the tests of the reader itself (the_last_bytes_before_an_exit_are_readand the two after it).
All tests pass, and the run takes a few seconds:
input_crossterm_leaves_unread_does_not_make_the_tui_spin measures CPU over
4 s, and the stalled-reader tests each wait out a 3 s stall.
-
the terminal was still open after sipnab exited: a process other than the child still holds the pty's slave side, so the reader never readsEIO. A test that spawns its own processes must not hand them the pty. -
sipnab did not draw the call list within 60 s: the TUI did not start. Runsipnab -I sip_call.pcapintests/fixturesin a terminal to see what it shows instead. -
the pty reader thread did not stall within 10 s: the reader thread is blocked outside its loop. It holds no lock across a read, so this points at a change toserve_terminal.