|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 | 5 | import datetime
|
| 6 | +import json |
6 | 7 | import shutil
|
7 | 8 | import time
|
8 | 9 | import typing as t
|
9 | 10 |
|
10 |
| -from libtmux.snapshot import PaneRecording, PaneSnapshot |
| 11 | +from libtmux.snapshot import ( |
| 12 | + CLIOutputAdapter, |
| 13 | + PaneRecording, |
| 14 | + PaneSnapshot, |
| 15 | + PytestDiffAdapter, |
| 16 | + SyrupySnapshotAdapter, |
| 17 | + TerminalOutputAdapter, |
| 18 | +) |
11 | 19 |
|
12 | 20 | if t.TYPE_CHECKING:
|
13 | 21 | from libtmux.session import Session
|
@@ -105,3 +113,121 @@ def test_pane_recording(session: Session) -> None:
|
105 | 113 |
|
106 | 114 | assert len(recording.get_snapshots_between(start_time, end_time)) == 3
|
107 | 115 | assert len(recording.get_snapshots_between(mid_time, end_time)) == 2
|
| 116 | + |
| 117 | + |
| 118 | +def test_snapshot_output_adapters(session: Session) -> None: |
| 119 | + """Test the various output adapters for PaneSnapshot.""" |
| 120 | + env = shutil.which("env") |
| 121 | + assert env is not None, "Cannot find usable `env` in PATH." |
| 122 | + |
| 123 | + session.new_window( |
| 124 | + attach=True, |
| 125 | + window_name="adapter_test", |
| 126 | + window_shell=f"{env} PS1='$ ' sh", |
| 127 | + ) |
| 128 | + pane = session.active_window.active_pane |
| 129 | + assert pane is not None |
| 130 | + |
| 131 | + # Create a snapshot with some content |
| 132 | + pane.send_keys("echo 'Test Content'") |
| 133 | + time.sleep(0.1) |
| 134 | + snapshot = pane.snapshot() |
| 135 | + |
| 136 | + # Test Terminal Output |
| 137 | + terminal_output = snapshot.format(TerminalOutputAdapter()) |
| 138 | + assert "\033[1;34m=== Pane Snapshot ===\033[0m" in terminal_output |
| 139 | + assert "\033[1;36mPane:\033[0m" in terminal_output |
| 140 | + assert "Test Content" in terminal_output |
| 141 | + |
| 142 | + # Test CLI Output |
| 143 | + cli_output = snapshot.format(CLIOutputAdapter()) |
| 144 | + assert "=== Pane Snapshot ===" in cli_output |
| 145 | + assert "Pane: " in cli_output |
| 146 | + assert "\033" not in cli_output # No ANSI codes |
| 147 | + assert "Test Content" in cli_output |
| 148 | + |
| 149 | + # Test Pytest Diff Output |
| 150 | + pytest_output = snapshot.format(PytestDiffAdapter()) |
| 151 | + assert "PaneSnapshot(" in pytest_output |
| 152 | + assert " pane_id=" in pytest_output |
| 153 | + assert " content=[" in pytest_output |
| 154 | + assert " metadata={" in pytest_output |
| 155 | + assert "'Test Content'" in pytest_output |
| 156 | + |
| 157 | + # Test Syrupy Output |
| 158 | + syrupy_output = snapshot.format(SyrupySnapshotAdapter()) |
| 159 | + data = json.loads(syrupy_output) |
| 160 | + assert isinstance(data, dict) |
| 161 | + assert "pane_id" in data |
| 162 | + assert "content" in data |
| 163 | + assert "metadata" in data |
| 164 | + assert "Test Content" in str(data["content"]) |
| 165 | + |
| 166 | + # Test default format (no adapter) |
| 167 | + default_output = snapshot.format() |
| 168 | + assert default_output == str(snapshot) |
| 169 | + |
| 170 | + |
| 171 | +def test_pane_snapshot_convenience_method(session: Session) -> None: |
| 172 | + """Test the Pane.snapshot() convenience method.""" |
| 173 | + env = shutil.which("env") |
| 174 | + assert env is not None, "Cannot find usable `env` in PATH." |
| 175 | + |
| 176 | + session.new_window( |
| 177 | + attach=True, |
| 178 | + window_name="snapshot_convenience_test", |
| 179 | + window_shell=f"{env} PS1='$ ' sh", |
| 180 | + ) |
| 181 | + pane = session.active_window.active_pane |
| 182 | + assert pane is not None |
| 183 | + |
| 184 | + # Take snapshot using convenience method |
| 185 | + snapshot = pane.snapshot() |
| 186 | + assert snapshot.content == ["$"] |
| 187 | + assert snapshot.pane_id == pane.pane_id |
| 188 | + assert snapshot.window_id == pane.window.window_id |
| 189 | + assert snapshot.session_id == pane.session.session_id |
| 190 | + assert snapshot.server_name == pane.server.socket_name |
| 191 | + |
| 192 | + # Test with start/end parameters |
| 193 | + pane.send_keys("echo 'Line 1'") |
| 194 | + time.sleep(0.1) |
| 195 | + pane.send_keys("echo 'Line 2'") |
| 196 | + time.sleep(0.1) |
| 197 | + pane.send_keys("echo 'Line 3'") |
| 198 | + time.sleep(0.1) |
| 199 | + |
| 200 | + snapshot_partial = pane.snapshot(start=1, end=2) |
| 201 | + assert len(snapshot_partial.content) == 2 |
| 202 | + assert "Line 1" in snapshot_partial.content_str |
| 203 | + assert "Line 2" in snapshot_partial.content_str |
| 204 | + assert "Line 3" not in snapshot_partial.content_str |
| 205 | + |
| 206 | + |
| 207 | +def test_pane_record_convenience_method(session: Session) -> None: |
| 208 | + """Test the Pane.record() convenience method.""" |
| 209 | + env = shutil.which("env") |
| 210 | + assert env is not None, "Cannot find usable `env` in PATH." |
| 211 | + |
| 212 | + session.new_window( |
| 213 | + attach=True, |
| 214 | + window_name="record_convenience_test", |
| 215 | + window_shell=f"{env} PS1='$ ' sh", |
| 216 | + ) |
| 217 | + pane = session.active_window.active_pane |
| 218 | + assert pane is not None |
| 219 | + |
| 220 | + # Create recording using convenience method |
| 221 | + recording = pane.record() |
| 222 | + assert isinstance(recording, PaneRecording) |
| 223 | + assert len(recording) == 0 |
| 224 | + |
| 225 | + # Add snapshots to recording |
| 226 | + recording.add_snapshot(pane) |
| 227 | + pane.send_keys("echo 'Test'") |
| 228 | + time.sleep(0.1) |
| 229 | + recording.add_snapshot(pane) |
| 230 | + |
| 231 | + assert len(recording) == 2 |
| 232 | + assert recording[0].content == ["$"] |
| 233 | + assert "Test" in recording[1].content_str |
0 commit comments