Skip to content

TextFrame: Terminal UI testing primitive with pytest/syrupy integration - #613

Open
tony wants to merge 37 commits into
masterfrom
snapshots-2
Open

tony wants to merge 37 commits into
masterfrom
snapshots-2

Conversation

@tony

@tony tony commented Dec 6, 2025 •

Copy link
Copy Markdown
Member

Note

Master through v0.70.0 requires Python 3.11+ (#849), tmux >= 3.3a (#888), and ty (uv run ty check) with Ruff ANN (#682) in place of mypy.

Adds libtmux.textframe, a fixed-size ASCII frame simulator for testing terminal UI output. Useful for validating capture_pane() output and terminal rendering in tests.

Install: pip install libtmux[textframe]

Features

TextFrame primitive

A dataclass for creating fixed-dimension ASCII frames with overflow detection:

from libtmux.textframe import TextFrame

frame = TextFrame(content_width=10, content_height=2)
frame.set_content(["hello", "world"])
print(frame.render())
# +----------+
# |hello     |
# |world     |
# +----------+
  • Configurable overflow_behavior: "error" (raises with visual diagnostic) or "truncate" (clips silently)
  • Dimension validation via __post_init__

Pane.capture_frame()

High-level method that wraps capture_pane() and returns a TextFrame:

def test_cli_output(pane, textframe_snapshot):
    pane.send_keys("echo 'Hello'", enter=True)

    # Wait for output, then capture as frame
    frame = pane.capture_frame(content_width=40, content_height=10)
    assert frame == textframe_snapshot
  • Defaults to pane dimensions when width/height not specified
  • Uses overflow_behavior="truncate" by default for CI robustness

pytest assertion hook

Rich diff output when comparing TextFrame objects:

TextFrame comparison failed:
  width: 20 != 10
Content diff:
- +----------+
+ +--------------------+

syrupy snapshot extension

TextFrameExtension stores snapshots as .frame files - one file per test for cleaner git diffs:

def test_pane_output(textframe_snapshot):
    frame = TextFrame(content_width=20, content_height=5)
    frame.set_content(["Hello", "World"])
    assert frame == textframe_snapshot

Plugin discovery

Registered via pytest11 entry point - fixtures and hooks are auto-discovered when libtmux[textframe] is installed.

[project.entry-points.pytest11]
libtmux-textframe = "libtmux.textframe.plugin"

[project.optional-dependencies]
textframe = ["syrupy>=4.0.0"]

Files changed

Path Description
src/libtmux/pane.py Add capture_frame() method
src/libtmux/textframe/__init__.py Public API exports
src/libtmux/textframe/core.py TextFrame dataclass, ContentOverflowError
src/libtmux/textframe/plugin.py Syrupy extension, pytest hooks, textframe_snapshot fixture
pyproject.toml Entry point + optional dependency
docs/internals/textframe.md Documentation
tests/textframe/ TextFrame test suite with snapshot baselines
tests/test_pane_capture_frame.py capture_frame() integration tests (12 tests)

See also

@tony tony changed the title Yet another Snapshot PR Yet another Snapshot PR (TextFrame) Dec 6, 2025
@tony
tony force-pushed the snapshots-2 branch 2 times, most recently from 6b00c4f to 96d1823 Compare December 7, 2025 08:55
@codecov

codecov Bot commented Dec 7, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 54.72973% with 67 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.84%. Comparing base (efdbb56) to head (2495b5e).

Files with missing lines Patch % Lines
src/libtmux/textframe/core.py 59.81% 37 Missing and 6 partials ⚠️
src/libtmux/textframe/plugin.py 20.68% 22 Missing and 1 partial ⚠️
src/libtmux/pane.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #613      +/-   ##
==========================================
- Coverage   61.99%   61.84%   -0.15%     
==========================================
  Files          29       32       +3     
  Lines        5091     5239     +148     
  Branches     1082     1109      +27     
==========================================
+ Hits         3156     3240      +84     
- Misses       1595     1652      +57     
- Partials      340      347       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tony
tony marked this pull request as ready for review December 7, 2025 16:18
tony added a commit that referenced this pull request Dec 7, 2025
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
@tony tony changed the title Yet another Snapshot PR (TextFrame) TextFrame: Terminal UI testing primitive with pytest/syrupy integration Dec 7, 2025
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_frame() method for users.
what:
- Add Pane.capture_frame() section under New features
- Include usage example with textframe_snapshot
- Document key features (default dimensions, truncate mode)
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_pane() parameters for the changelog.

what:
- Add section for Pane.capture_pane() enhanced
- Document all 5 new parameters with flag mappings
- Add code examples for colored output and joined lines
- Note trim_trailing requires tmux 3.4+
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_pane() parameters for the changelog.

what:
- Add section for Pane.capture_pane() enhanced
- Document all 5 new parameters with flag mappings
- Add code examples for colored output and joined lines
- Note trim_trailing requires tmux 3.4+
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_pane() parameters for the changelog.

what:
- Add section for Pane.capture_pane() enhanced
- Document all 5 new parameters with flag mappings
- Add code examples for colored output and joined lines
- Note trim_trailing requires tmux 3.4+
tony added a commit that referenced this pull request Dec 7, 2025
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_frame() method for users.
what:
- Add Pane.capture_frame() section under New features
- Include usage example with textframe_snapshot
- Document key features (default dimensions, truncate mode)
@tony

tony commented Dec 7, 2025

Copy link
Copy Markdown
Member Author

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

tony added a commit that referenced this pull request Dec 7, 2025
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
tony added a commit that referenced this pull request Dec 7, 2025
why: Document the new capture_frame() method for users.
what:
- Add Pane.capture_frame() section under New features
- Include usage example with textframe_snapshot
- Document key features (default dimensions, truncate mode)
@tony
tony force-pushed the snapshots-2 branch 4 times, most recently from 94a6f8f to d335fc9 Compare December 8, 2025 01:40
@tony

tony commented Dec 8, 2025

Copy link
Copy Markdown
Member Author

Code review

Found 1 issue:

  1. ImportError for users without libtmux[textframe] installed: The __init__.py unconditionally imports TextFrameExtension from plugin.py, which requires syrupy. This causes pane.capture_frame() to fail with an ImportError for users who haven't installed the optional dependency.

from libtmux.textframe.core import ContentOverflowError, TextFrame
from libtmux.textframe.plugin import TextFrameExtension
__all__ = ["ContentOverflowError", "TextFrame", "TextFrameExtension"]

The import chain is:

  • pane.capture_frame() imports from libtmux.textframe import TextFrame
  • __init__.py line 6 imports TextFrameExtension from plugin.py
  • plugin.py lines 17-19 unconditionally import pytest and syrupy

Suggested fix: Make the TextFrameExtension import conditional, or move it to a separate module that's only imported when needed.

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

tony added a commit that referenced this pull request Dec 14, 2025
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
tony added a commit that referenced this pull request Dec 14, 2025
why: Document the new capture_frame() method for users.
what:
- Add Pane.capture_frame() section under New features
- Include usage example with textframe_snapshot
- Document key features (default dimensions, truncate mode)
tony added a commit that referenced this pull request Apr 26, 2026
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
tony added 29 commits November 28, 2026 20:37
why: Individual .frame files provide cleaner git diffs and
easier review than a single .ambr file with all snapshots.
what:
- Replace AmberSnapshotExtension with SingleFileSnapshotExtension
- Set file extension to .frame
- Simplify serialize() method (removed nested serializer class)
why: Replaced by individual .frame files from SingleFileSnapshotExtension.
what:
- Delete test_core.ambr
why: New snapshot format from SingleFileSnapshotExtension.
what:
- Add test_frame_rendering[basic_success].frame
- Add test_frame_rendering[overflow_width].frame
- Add test_frame_rendering[empty_frame].frame
- Add test_frame_rendering[truncate_width].frame
- Add test_frame_rendering[truncate_height].frame
- Add test_frame_rendering[truncate_both].frame
- Add test_nested_serialization.frame
why: Provide reference for TextFrame usage and architectural decisions.
what:
- Document syrupy integration (SingleFileSnapshotExtension)
- Document pytest_assertrepr_compare hook pattern
- Document overflow_behavior modes
- Include examples and architectural insights from syrupy/pytest/CPython
- Add to internals toctree
why: Enable distribution of TextFrame for downstream users.
what:
- Move tests/textframe/core.py → src/libtmux/textframe/core.py
- Create src/libtmux/textframe/__init__.py with public API exports
- Update test imports to use libtmux.textframe
why: Auto-register TextFrame assertion hooks and snapshot fixture
for downstream users who install libtmux[textframe].
what:
- Move tests/textframe/plugin.py → src/libtmux/textframe/plugin.py
- Add pytest_assertrepr_compare hook for rich diff output
- Add textframe_snapshot fixture for downstream users
- Export TextFrameExtension from __init__.py
- Simplify tests/textframe/conftest.py (hooks now in plugin)
why: Allow opt-in installation of textframe pytest plugin.
what:
- Add [project.optional-dependencies] textframe = ["syrupy>=4.0.0"]
- Add [project.entry-points.pytest11] libtmux-textframe entry point
- Downstream users can now: pip install libtmux[textframe]
why: Document opt-in mechanism for downstream users.
what:
- Update import paths from tests/ to src/libtmux/textframe/
- Add installation section: pip install libtmux[textframe]
- Document auto-discovered fixtures and hooks
- Add Plugin Discovery section explaining pytest11 entry points
- Update file paths table
why: Lockfile reflects new optional dependency.
what:
- Add syrupy to textframe extras in uv.lock
why: Document new features for release notes.
what:
- TextFrame primitive for terminal UI testing
- pytest assertion hook with rich diff output
- syrupy snapshot extension with .frame files
- Optional install via libtmux[textframe]
why: Enable capturing pane content as TextFrame for visualization
and snapshot testing. This bridges capture_pane() with the TextFrame
dataclass for a more ergonomic testing workflow.
what:
- Add capture_frame() method that wraps capture_pane()
- Default to pane dimensions when width/height not specified
- Default to truncate mode for robustness in CI environments
- Add comprehensive docstring with examples
why: Verify capture_frame() works with real tmux panes and integrates
properly with syrupy snapshot testing.
what:
- Add 12 comprehensive tests using NamedTuple parametrization
- Test basic usage, custom dimensions, overflow behavior
- Demonstrate retry_until integration pattern
why: Baseline snapshot for capture_frame() visual regression testing.
what:
- Add test_capture_frame_snapshot.frame baseline
why: Show users how to use capture_frame() for testing terminal output.
what:
- Add Pane.capture_frame() integration section
- Document parameters with table
- Explain design decisions (truncate default, refresh)
- Add retry_until usage example
why: Comprehensive visual regression testing for all capture_frame() variations.
what:
- Add SnapshotCase NamedTuple for parametrized snapshot testing
- Add 18 snapshot test cases covering:
  - Dimension variations: prompt_only, wide/narrow/tall/short frames
  - start/end parameters: start=0, end=0, end="-", start_end_range
  - Truncation: width and height truncation
  - Special characters and edge cases
- Use retry_until for robust async output handling
why: Baseline snapshots for exhaustive visual regression testing.
what:
- Add 18 .frame snapshot files for parametrized test cases
- Covers dimensions, start/end params, truncation, special chars
why: Enable doctest verification of capture_frame() output.
what:
- Create new pane with shell='sh' for predictable prompt
- Remove # doctest: +SKIP since output is now deterministic
- Follow established pattern from capture_pane() doctest
why: Allow users to control capture behavior when using capture_frame()
for snapshot testing, such as capturing colored output or joining
wrapped lines.

what:
- Add escape_sequences parameter for ANSI escape sequences
- Add escape_non_printable parameter for octal escapes
- Add join_wrapped parameter for joining wrapped lines
- Add preserve_trailing parameter for trailing spaces
- Add trim_trailing parameter with tmux 3.4+ version check
- Forward all flags to capture_pane() call
why: Verify that capture_frame() correctly forwards all capture_pane()
flags for proper behavior in snapshot testing scenarios.

what:
- Add CaptureFrameFlagCase NamedTuple for parametrized tests
- Add 4 test cases covering key flag behaviors
- Test escape_sequences, join_wrapped, preserve_trailing flags
- Verify flag absence behavior (no_escape_sequences)
why: Enable interactive exploration of large frame content in terminal
what:
- Add display() method with TTY detection
- Add _curses_display() with scrolling support
- Navigation: arrows, WASD, vim keys (hjkl)
- Page navigation: PgUp/PgDn, Home/End
- Exit: q, Esc, Ctrl-C
why: Enable users to discover interactive viewer feature
what:
- Add Interactive Display section with usage example
- Document all keyboard controls in table format
- Note TTY requirement and RuntimeError behavior
why: curses KEY_RESIZE only fires on getch(), missing resize events
     when terminal is resized but no key is pressed
what:
- Replace stdscr.getmaxyx() with shutil.get_terminal_size()
- Remove KEY_RESIZE handling (now redundant)

This follows Rich's approach: query terminal size directly via
ioctl(TIOCGWINSZ) on each loop iteration, which works reliably
in tmux and other terminal multiplexers.
why: Verify display() uses shutil.get_terminal_size() for resize
what:
- Add test_terminal_resize_via_shutil test
- Mock shutil.get_terminal_size to verify it's called
why: Users without libtmux[textframe] get ImportError on capture_frame()
what:
- Wrap TextFrameExtension import in try/except ImportError
- Only add to __all__ when syrupy is available
- Core TextFrame functionality works without optional dependency
why: Follow established exception pattern for libtmux exceptions
what:
- Add LibTmuxException as base class alongside ValueError
- Matches pattern of AdjustmentDirectionRequiresAdjustment, etc.
- Enables catching all libtmux exceptions with LibTmuxException
why: Follow CLAUDE.md guideline for stdlib namespace imports
what:
- Change from difflib import ndiff to import difflib
- Use difflib.ndiff() instead of ndiff()
why: Follow pytest best practices from CLAUDE.md guidelines
what:
- Use import unittest.mock namespace style
- Replace patch() context managers with monkeypatch.setattr()
- Document MagicMock necessity for curses window simulation
why: The branch's lockfile no longer matched its pyproject.toml once
master's dependency updates were under it.
what:
- Regenerate uv.lock with uv lock
why: Strict ty (`all = "error"`) and Ruff `ANN` reject `t.Any`
parameters on `TextFrameExtension.serialize` and
`pytest_assertrepr_compare`, flag conditional `TextFrameExtension`
re-exports as possibly missing, and `WRITING.md` forbids `# doctest:
+SKIP`.

what:
- Type `TextFrameExtension.serialize` with Syrupy's `SerializableData`,
  `SerializedData`, `PropertyName`, and `PropertyPath` aliases
- Define `AssertOperand` for `pytest_assertrepr_compare` and drop
  `t.Any` in `tests/textframe/test_core.py`
- Expose `TextFrameExtension` statically under `t.TYPE_CHECKING` while
  keeping runtime `ImportError` fallback
- Replace `# doctest: +SKIP` on `TextFrame.display` with `+HIDE` mocks
  and format fenced snippets in `docs/internals/textframe.md`

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: observation Waits, capture, snapshots, subscriptions enhancement pytest-plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant