Skip to content

Add boxed VT + SGR snapshot serialization in libvt.testing and syrupy adapter #64

Done

Description

@tony

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 background SGR sequence and splits \x1b[32m byte by byte into malformed double-escape output.
  • Calling format_screen_box() directly discards foreground, background, bold, underline, reverse, and OSC 8 hyperlink attributes, missing 1-column color or attribute regressions.
  • syrupy registers --snapshot-no-colors with default=not sys.stdout.isatty() (src/syrupy/__init__.py), stripping intra-line character highlights whenever stdout is piped in CI or pytest-xdist workers without emitting ASCII column carets, while pytest truncates default assertion output via materialize_with_truncation() before row-23 status 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 resurrects Ctrl-L-cleared scrollback when full history is coalesced, and reading Terminal.snapshot() instead of Terminal.committed tears active DEC mode ?2026 synchronized 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, and format_vt_diff_lines() accepting Terminal (via Terminal.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-column cursor, alt, and non-default terminal modes), a format_screen_box() Unicode viewport preserving right-border W soft-wrap markers, and an RLE styles: footer of half-open [start..end) display-column SGR and hyperlink spans built from RowView.runs.
    • Regime-aware ScrubberSpec normalization: keep row geometry fixed and absorb token width deltas into the nearest unstyled gap of 2 or more spaces within │ column dividers when alt=True or Mode.DECAWM is off, and coalesce only contiguous wrapped=True row chains on primary DECAWM streams so /tmp/... vs /private/var/folders/... wraps match without Ctrl-L scrollback bleed.
    • Truncation-safe format_vt_diff_lines() placing header:, row R cols C0..C1: 'old' -> 'new', and styles: delta lines at line 1 ahead of a 2-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 (AmberSnapshotExtension subclass) and get_vt_snapshot_extension() delegating serialize() and diff_lines() to libvt.testing (including nested dict / list / tuple containers) and memoizing VtSnapshotExtension.configure(...) subclasses with @functools.lru_cache so SnapshotSession write-queue batching on (extension.__class__, snapshot_location) stays linear.

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

Activity

  1. added
    enhancement
    on Nov 29, 2026
  2. changed the title [-]Add `libvt.contrib.syrupy` extension for boxed VT and SGR snapshots[/-] [+]Add boxed VT + SGR snapshot serialization in libvt.testing and syrupy adapter[/+] on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions