Repository navigation
Description
Summary
libvt provides format_screen_box() and assert_screen() in src/libvt/testing.py, yet has no tool-agnostic boxed VT + SGR snapshot serializer or optional syrupy .ambr adapter for Terminal, Snapshot, Screen, RegionView, TmuxCaptureResult, or duck-typed libtmux pane targets. Adding serialize_vt_snapshot(), vt_screen(), and format_vt_diff_lines() to core libvt.testing — paired with a thin VtSnapshotExtension adapter in libvt.contrib.syrupy (libvt[syrupy]) — gives assert, inline-snapshot, pytest-regressions, and syrupy users an escape-free boxed representation while keeping libvt's runtime dependencies = [] unchanged.
Motivation
Testing multi-row terminal layouts and styled TUIs against raw strings or naive syrupy AmberSnapshotExtension snapshots hits four failure modes across local PTYs and CI runners:
- Embedding raw ANSI escape sequences inside golden strings breaks character-by-character diff highlighters such as
SnapshotReporter.__format_line()(src/syrupy/extensions/base.py), which wraps each character in its own backgroundSGRsequence and splits\x1b[32mbyte by byte into malformed double-escape output. - Calling
format_screen_box()directly discards foreground, background, bold, underline, reverse, andOSC 8hyperlink attributes, missing1-column color or attribute regressions. syrupyregisters--snapshot-no-colorswithdefault=not sys.stdout.isatty()(src/syrupy/__init__.py), stripping intra-line character highlights wheneverstdoutis piped in CI orpytest-xdistworkers without emitting ASCII column carets, while pytest truncates default assertion output viamaterialize_with_truncation()before row-23status bar or style-footer changes appear.- Regex-replacing host-dependent tokens (
PID, clock strings, or temporary directory paths) on raw strings wraps│borders on alternate screens (alt=True) or resurrectsCtrl-L-cleared scrollback when full history is coalesced, and readingTerminal.snapshot()instead ofTerminal.committedtears active DEC mode?2026synchronized redraws.
Proposal
Separate tool-agnostic VT serialization and diffing from the optional syrupy .ambr adapter:
- Core
src/libvt/testing.py(zero third-party dependencies):serialize_vt_snapshot(),vt_screen(target, *, diff=prev, scrubbers=..., region=..., include_styles=..., include_cursor_shape=...)(VtScreenView),ScrubberSpec,VtTarget, andformat_vt_diff_lines()acceptingTerminal(viaTerminal.committed),Snapshot,Screen,RegionView,TmuxCaptureResult, and duck-typed pane handles exposing.terminal,.snapshot(), or.capture_vt().- Escape-free boxed serialization combining a canonicalized metadata header (
size, display-columncursor,alt, and non-default terminalmodes), aformat_screen_box()Unicode viewport preserving right-borderWsoft-wrap markers, and an RLEstyles:footer of half-open[start..end)display-columnSGRand hyperlink spans built fromRowView.runs. - Regime-aware
ScrubberSpecnormalization: keep row geometry fixed and absorb token width deltas into the nearest unstyled gap of2or more spaces within│column dividers whenalt=TrueorMode.DECAWMis off, and coalesce only contiguouswrapped=Truerow chains on primaryDECAWMstreams so/tmp/...vs/private/var/folders/...wraps match withoutCtrl-Lscrollback bleed. - Truncation-safe
format_vt_diff_lines()placingheader:,row R cols C0..C1: 'old' -> 'new', andstyles:delta lines at line1ahead of a2-line-context unified box diff with optional TTY styling callbacks or? ^^^ASCII carets on colorless pipes.
- Optional
src/libvt/contrib/syrupy.py(libvt[syrupy]):- Lazy
VtSnapshotExtension(AmberSnapshotExtensionsubclass) andget_vt_snapshot_extension()delegatingserialize()anddiff_lines()tolibvt.testing(including nesteddict/list/tuplecontainers) and memoizingVtSnapshotExtension.configure(...)subclasses with@functools.lru_cachesoSnapshotSessionwrite-queue batching on(extension.__class__, snapshot_location)stays linear.
- Lazy
Alternatives
Coupling serialize_vt_snapshot(), vt_screen(), and format_vt_diff_lines() exclusively inside libvt.contrib.syrupy was rejected because 95% of the serializer, scrubber, and diff engine uses zero syrupy APIs and serves assert, inline-snapshot, and pytest-regressions callers directly from libvt.testing. Serializing screens as nested Python dictionaries through AmberDataSerializer (structured-dict) or rendering 24-row side-by-side dual-frame ASCII boxes (side-by-side-custom) were also prototyped and rejected: SnapshotReporter.__limit_context() duplicates unchanged blocks of 2 to 2 * _context_line_count lines when _context_line_count > 1, regex scrubbing shifts dictionary style coordinates, and 24-row side-by-side frames add 50% more code while getting cut off by pytest's default assertion truncation before bottom-row or SGR deltas appear.
References
format_screen_box()andassert_screen()insrc/libvt/testing.py(v0.0.1a3)Terminal.committedsynchronized frame boundary insrc/libvt/terminal.py(v0.0.1a3)syrupycharacter-level styling and context slicing insrc/syrupy/extensions/base.py(v7.0.3),--snapshot-no-colorsTTY default insrc/syrupy/__init__.py(v7.0.3), and write-queue batching insrc/syrupy/session.py(v7.0.3)pytestassertion truncation insrc/_pytest/assertion/truncate.py(9.2.0)- Companion conditional
vt_snapshotfixture decouple in Decouple libvt.testing, libvt.pytest_plugin, and conditional syrupy hook #65 and downstreamlibtmux[vt]integration in libtmux#933 and libtmux#934
Activity
testing(feat): Decouple testing, plugin, syrupy
[-]Add `libvt.contrib.syrupy` extension for boxed VT and SGR snapshots[/-][+]Add boxed VT + SGR snapshot serialization in libvt.testing and syrupy adapter[/+]testing(feat): Decouple testing, plugin, syrupy
testing(feat): Decouple testing, plugin, syrupy (#67)