Skip to content
137 changes: 137 additions & 0 deletions docs/topics/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,143 @@ styles: [' r0[0..10]: fg=green'] -> [' r0[0..10]: fg=red']

---

## Direct FIRE tests

Run the Fidelity, Isolation, Recovery, and Evidence (FIRE) cases from a
libvt checkout. Pytest lists the name and delivery schedule of each case with
`--collect-only -q`.

```console
$ uv run pytest tests/test_fire.py
```

Each attempt uses a fresh in-memory `Terminal`. Cases feed application bytes,
encode semantic input with `Terminal.encode()`, or change geometry with
`Terminal.resize()`. Cases with feed bytes run in whole-action and one-byte
delivery schedules and observe state at the same action boundaries.
Encoding and resize calls remain indivisible. These runs require Python and
pytest. They use UTF-8 and `DEFAULT_PROFILE`, with per-case settings recorded
in the report. The profile gives the East Asian Ambiguous character `è`
(U+00E8) one cell.
[Unicode East Asian Width][fire-width]

The suite has 210 distinct behavior vectors. Of these, 201 run under both feed
schedules and nine semantic-input cases run once. Two fault controls run under
both feed schedules, for 415 collected attempts in all.

| Family | Vectors | Observations |
| --- | ---: | --- |
| Parsing and Unicode | 23 | Incomplete and malformed UTF-8, CSI cancellation and restart, OSC/DCS/APC boundaries, DEC graphics, combining and wide cells. [ECMA-48][fire-ecma48], [DEC parser][fire-ansi-parser], [UTF-8 decoder][fire-utf8], [Unicode segmentation][fire-graphemes] |
| Cursor and margins | 29 | Absolute and relative movement, tabs, both margin axes, saved cursor and deferred wrap. [ECMA-48][fire-ecma48], [xterm 413][fire-xterm-source] |
| Grid edits and scrolling | 26 | Partial erases, insertion and deletion, protected cells, region scrolling, history and wide-cell repair. [ECMA-48][fire-ecma48], [xterm 413][fire-xterm-source] |
| Rendition | 21 | Cell attributes, selective resets, indexed and RGB colors, underlines, hyperlinks and background erasure. [ECMA-48][fire-ecma48], [ITU T.416][fire-t416], [styled underlines][fire-underlines] |
| Replies | 15 | Exact ordered DA, DSR, CPR, DECRQM, DECRQSS, color and geometry responses, with drain checkpoints. [ECMA-48][fire-ecma48], [xterm 413][fire-xterm-source], [color scheme replies][fire-color-scheme] |
| Buffer isolation | 7 | Alternate-mode reconciliation, primary cursor and pen restoration, viewport and history separation, reset. [xterm 413][fire-xterm-source], [libvt mode reconciliation](../project/adr/0006-charset-designations-and-single-truth-mode-reconciliation.md) |
| Synchronized publication | 8 | Working versus committed rows, cursor, history, title and appearance across holds and releases. [Synchronized output][fire-sync] |
| Snapshots and replica recovery | 19 | Immutable saved views, applying dirty rows, following working and committed tokens through successive edits, history eviction and full resync. [Snapshots and scrollback](snapshots.md) |
| Semantic input | 41 | Legacy keys, mode and flag negotiation, per-screen keyboard stacks, exact mouse and focus reports, paste framing and fail-closed paste policy. [xterm 413][fire-xterm-source], [keyboard protocol notes][fire-keyboard], [Ghostty 1.4.0 key codes][fire-key-codes], [libvt input contract](../project/adr/0008-mode-aware-input-encoding-and-paste-safety.md) |
| Resize and geometry | 21 | Hard breaks, soft wraps across scrollback and viewport, cursor and style remapping, alternate-screen cropping, held geometry, replica resync and exact size reports after resize. [libvt resize contract](../project/adr/0004-zero-based-row-col-coordinates-and-sync-stepping.md), [xterm 413][fire-xterm-source], [in-band resize][fire-in-band-resize] |

CUP places the cursor at wire position (2, 4). CUD moves it two rows down.
Subtract one from each wire coordinate to get libvt's zero-based position.
[ECMA-48, fifth edition, §§8.3.19 and 8.3.21][fire-ecma48]

```python
>>> from libvt import Terminal
>>> terminal = Terminal(rows=6, cols=9)
>>> terminal.feed(b"\x1b[2;4H")
>>> terminal.feed(b"\x1b[2B")
>>> terminal.cursor.row, terminal.cursor.col
(3, 3)
```

The publication case erases `base` and prints `è` during a synchronized
update. The working row changes immediately. The committed row changes on
release. A cursor query returns `1b5b313b3252` once (row 1, column 2 in
wire coordinates). A second terminal sees an empty reply queue while that
reply is pending. The saved snapshot and the second terminal retain their
original rows. [Synchronized output][fire-sync]
[ECMA-48, §§8.3.14, 8.3.15, 8.3.35 and 8.3.41][fire-ecma48]

The recovery case rebuilds a replica when a token belongs to another terminal.
It then leaves the replica on the alternate screen while the terminal returns
to the primary screen. A full resync replaces the alternate rows, and the new
token identifies the starting point for the next incremental update.
[XTerm alternate-screen mode][fire-xterm]
{meth}`~libvt.terminal.Terminal.changes_since`

Changing the CUD count to one reports only `cursor_after_move`. Omitting the
release reports only `committed_after_release`. Pytest prints expected and
observed values under the failing case and feed schedule.

The following implementation faults were each injected in an isolated
checkout. Both delivery schedules failed at the named check for each fault.

| Fault | Case and failing check |
| --- | --- |
| Substitute `?` for an incomplete UTF-8 replacement character | `parser-invalid-utf8-reprocesses-escape`, `exercise.rows` |
| Make CNL keep its old column | `cursor-cnl-selects-next-left`, `exercise.cursor` |
| Make DCH delete one cell regardless of its count | `grid-dch-shifts-tail-left`, `exercise.rows` |
| Leave bold active after SGR 22 | `rendition-normal-intensity-keeps-italic`, `exercise.cell[0,1].bold` |
| Drop the primary DA reply | `reply-da-primary-and-secondary-identity`, `exercise.replies` |
| Leave alternate rows intact on `?1047l` | `buffer-1047-exit-clears-alternate`, `return-primary.alternate_buffer_rows` |
| Ignore synchronized-output release | `publication-holds-title-while-working-metadata-changes`, `release-title.committed_title` |
| Skip copying changed rows into the replica | `delta-one-dirty-row-with-restored-cursor`, `after.rows` |
| Return the previous token after a working edit or a committed publication | `test_fire_incremental_token_tracks_each_view`, `working_read_again` or `published_read_again` |
| Skip copying a repainted row when the word stays the same | `test_fire_incremental_token_tracks_each_view`, `repainted_replica_ink` |
| Leave an empty hyperlink on the cell after closing OSC 8 | `rendition-osc8-sgr-reset-keeps-link-until-osc8-close`, `exercise.cell[0,2].has_hyperlink` |
| Send the normal cursor-key sequence while application mode is active | `input-cursor-mode-set-and-reset`, `application.encoded_input` |
| Replace active keyboard flags when the update requests bitwise OR | `input-kitty-bitwise-update-and-clear`, `or-kept-escape.encoded_input` |
| Permit BEL in pasted text | `input-paste-rejects-bell-while-bracketed`, `encode.encoded_input` |
| Disable reflow during resize | `resize-widen-joins-soft-wrapped-rows`, `after.rows` |
| Leave the pre-resize token valid after width changes | `resize-widen-joins-soft-wrapped-rows`, `replica.full_resync` and `replica.reason` |
| Drop the in-band size report on resize | `resize-queues-enabled-in-band-geometry`, `after.replies` |

Each case writes `fire.json` to its pytest temporary directory, including
successful runs and the two fault controls. The file records the profile,
partition, terminal dimensions, bytes passed to each feed call,
and expected and actual values for each evaluated check. `operations`
records input-event arguments and explicit geometry changes. Its
`after_feeds` count records how many feed calls preceded each operation.
The `main` and `peer` feed targets name terminal instances. A feed to
`main` reaches its active buffer.
Pytest clears an existing base directory before the run. Choose a fresh
directory name each time to keep earlier reports.

```console
$ uv run pytest \
--basetemp fire-evidence-01 \
tests/test_fire.py
$ rg --files --hidden --no-ignore \
--glob fire.json \
fire-evidence-01
```

Pass the quoted `pytest_node` value to pytest to replay one case and
partition from the same checkout.

```console
$ uv run pytest \
'tests/test_fire.py::test_fire_working_and_committed_frames[one-byte]'
```

[fire-color-scheme]: https://github.com/contour-terminal/contour/blob/d5541aa85daa1b5df77e90eb91f159cc91f12a61/docs/vt-extensions/color-palette-update-notifications.md
[fire-ansi-parser]: https://vt100.net/emu/dec_ansi_parser
[fire-ecma48]: https://ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf
[fire-graphemes]: https://www.unicode.org/reports/tr29/tr29-47.html
[fire-in-band-resize]: https://github.com/toppk/revenant/blob/3188a7034222fead92501d6103e5260eff4491c8/tdn/docs/csi/window-ops.md#size-reports
[fire-key-codes]: https://github.com/ghostty-org/ghostty/blob/83fb95624b0439ee660c2679b43b38c8b63769cd/src/input/kitty.zig#L91-L108
[fire-keyboard]: https://github.com/toppk/revenant/blob/3188a7034222fead92501d6103e5260eff4491c8/tdn/docs/input/kitty-keyboard.md
[fire-sync]: https://github.com/contour-terminal/contour/blob/d5541aa85daa1b5df77e90eb91f159cc91f12a61/docs/vt-extensions/synchronized-output.md
[fire-t416]: https://www.itu.int/rec/T-REC-T.416-199303-I/en
[fire-underlines]: https://github.com/wezterm/wezterm/blob/20240203-110809-5046fc22/docs/escape-sequences.md#graphic-rendition-sgr
[fire-utf8]: https://github.com/whatwg/encoding/blob/7d2e3e08e11f57ba1b080390b490d53648c6f69c/encoding.bs
[fire-width]: https://www.unicode.org/reports/tr11/tr11-44.html
[fire-xterm]: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
[fire-xterm-source]: https://github.com/ThomasDickey/xterm-snapshots/blob/517713af0e45745f7405d8d3154e00fcc625c6b2/ctlseqs.ms

---

## Pytest plugin (`libvt.pytest_plugin`)

Install `libvt[pytest]` when running under
Expand Down
Loading
Loading