testty 0.7.5

Rust-native TUI end-to-end testing framework using PTY-driven semantic assertions and VHS screenshot capture.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Proof report collector and annotated text output.
//!
//! [`ProofReport`] accumulates [`ProofCapture`] entries during scenario
//! execution. Each capture records a terminal frame snapshot alongside its
//! label, description, dimensions, and optional assertion results.

use std::fmt::Write;
use std::path::Path;

use super::backend::ProofBackend;
use crate::diff::FrameDiff;
use crate::frame::TerminalFrame;

/// A single labeled capture collected during scenario execution.
///
/// Each capture preserves the full terminal frame data (text, colors,
/// and styles), dimensions, label, description, and optional assertion
/// results for proof rendering.
#[derive(Debug, Clone)]
pub struct ProofCapture {
    /// Short identifier for this capture step.
    pub label: String,
    /// Human-readable description of what this capture documents.
    pub description: String,
    /// Full terminal text at the moment of capture (plain text, no escapes).
    pub frame_text: String,
    /// ANSI-formatted bytes that reproduce the full frame state including
    /// colors and styles. Pass to [`TerminalFrame::new()`] with [`cols`]
    /// and [`rows`] to reconstruct a frame with full cell metadata.
    pub frame_bytes: Vec<u8>,
    /// Number of terminal columns at capture time.
    pub cols: u16,
    /// Number of terminal rows at capture time.
    pub rows: u16,
    /// Optional list of assertion results (pass/fail with description).
    pub assertions: Vec<AssertionResult>,
}

/// The outcome of a single assertion evaluated against a captured frame.
#[derive(Debug, Clone)]
pub struct AssertionResult {
    /// Whether the assertion passed.
    pub passed: bool,
    /// Human-readable description of the assertion.
    pub description: String,
}

/// Errors that can occur during proof report generation.
#[derive(Debug, thiserror::Error)]
pub enum ProofError {
    /// An I/O operation failed during proof output.
    #[error("proof I/O error: {0}")]
    Io(#[from] std::io::Error),

    /// A formatting error occurred during proof rendering.
    #[error("proof format error: {0}")]
    Format(String),
}

/// Collector for labeled captures produced during scenario execution.
///
/// Build a `ProofReport` by calling [`add_capture()`](ProofReport::add_capture)
/// for each labeled step, then render the report through a
/// [`ProofBackend`](super::backend::ProofBackend) or directly via
/// [`to_annotated_text()`](ProofReport::to_annotated_text).
#[derive(Debug, Clone)]
pub struct ProofReport {
    /// Human-readable name of the scenario that produced this report.
    pub scenario_name: String,
    /// Ordered list of captures collected during execution.
    pub captures: Vec<ProofCapture>,
    /// Diffs between consecutive captures, indexed by `(i, i+1)` pair.
    ///
    /// `diffs[i]` is the diff from `captures[i]` to `captures[i+1]`.
    /// The length is always `captures.len().saturating_sub(1)`.
    pub diffs: Vec<FrameDiff>,
}

impl ProofReport {
    /// Create an empty proof report for the given scenario name.
    pub fn new(scenario_name: impl Into<String>) -> Self {
        Self {
            scenario_name: scenario_name.into(),
            captures: Vec::new(),
            diffs: Vec::new(),
        }
    }

    /// Add a labeled capture from a terminal frame.
    ///
    /// If a previous capture exists, a [`FrameDiff`] between the previous
    /// and current frame is automatically computed and stored.
    pub fn add_capture(
        &mut self,
        label: impl Into<String>,
        description: impl Into<String>,
        frame: &TerminalFrame,
    ) {
        // Compute diff from previous capture's reconstructed frame.
        if let Some(previous) = self.captures.last() {
            let previous_frame =
                TerminalFrame::new(previous.cols, previous.rows, &previous.frame_bytes);
            self.diffs.push(FrameDiff::compute(&previous_frame, frame));
        }

        self.captures.push(ProofCapture {
            label: label.into(),
            description: description.into(),
            frame_text: frame.all_text(),
            frame_bytes: frame.contents_formatted(),
            cols: frame.cols(),
            rows: frame.rows(),
            assertions: Vec::new(),
        });
    }

    /// Attach an assertion result to the capture with the given label.
    ///
    /// Returns `true` if the label was found and the assertion was
    /// attached, `false` if no capture matches the label.
    pub fn add_assertion(
        &mut self,
        label: &str,
        passed: bool,
        description: impl Into<String>,
    ) -> bool {
        if let Some(capture) = self
            .captures
            .iter_mut()
            .find(|capture| capture.label == label)
        {
            capture.assertions.push(AssertionResult {
                passed,
                description: description.into(),
            });

            return true;
        }

        false
    }

    /// Render the report through a [`ProofBackend`] and write to `path`.
    ///
    /// This is the primary proof output method. Use
    /// [`to_annotated_text()`](Self::to_annotated_text) for in-memory text
    /// output.
    ///
    /// # Errors
    ///
    /// Returns a [`ProofError`] if rendering or I/O fails.
    pub fn save(&self, backend: &dyn ProofBackend, path: &Path) -> Result<(), ProofError> {
        backend.render(self, path)
    }

    /// Render the report as annotated plain text.
    ///
    /// Each capture is shown as a bordered frame dump with step number,
    /// label, description, and assertion markers.
    pub fn to_annotated_text(&self) -> String {
        let mut output = String::new();

        write_header(&mut output, &self.scenario_name);

        for (index, capture) in self.captures.iter().enumerate() {
            let step_number = index + 1;
            write_capture_section(&mut output, step_number, capture);
        }

        write_footer(&mut output, self.captures.len());

        output
    }
}

/// Write the report header with scenario name.
fn write_header(output: &mut String, scenario_name: &str) {
    let title = format!("Proof Report: {scenario_name}");
    let border = "=".repeat(title.len().max(60));

    let _ = writeln!(output, "{border}");
    let _ = writeln!(output, "{title}");
    let _ = writeln!(output, "{border}");
    let _ = writeln!(output);
}

/// Write one capture section with bordered frame dump and assertions.
fn write_capture_section(output: &mut String, step_number: usize, capture: &ProofCapture) {
    let heading = format!(
        "Step {step_number}: [{}] {}",
        capture.label, capture.description
    );
    let separator = "-".repeat(heading.len().max(60));

    let _ = writeln!(output, "{separator}");
    let _ = writeln!(output, "{heading}");
    let _ = writeln!(output, "  Terminal: {}x{}", capture.cols, capture.rows);
    let _ = writeln!(output, "{separator}");
    let _ = writeln!(output);

    // Frame text with left border.
    let frame_border = format!("+{}+", "-".repeat(usize::from(capture.cols) + 2));
    let _ = writeln!(output, "{frame_border}");
    for line in capture.frame_text.lines() {
        let padded = format!("{line:<width$}", width = usize::from(capture.cols));
        let _ = writeln!(output, "| {padded} |");
    }
    let _ = writeln!(output, "{frame_border}");
    let _ = writeln!(output);

    // Assertion results.
    if !capture.assertions.is_empty() {
        let _ = writeln!(output, "  Assertions:");
        for assertion in &capture.assertions {
            let marker = if assertion.passed { "PASS" } else { "FAIL" };
            let _ = writeln!(output, "    [{marker}] {}", assertion.description);
        }
        let _ = writeln!(output);
    }
}

/// Write the report footer with capture count.
fn write_footer(output: &mut String, capture_count: usize) {
    let _ = writeln!(output, "Total captures: {capture_count}");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn proof_report_collects_captures() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Hello, World!");

        // Act
        let mut report = ProofReport::new("test_scenario");
        report.add_capture("startup", "Application launched", &frame);
        report.add_capture("after_input", "User typed text", &frame);

        // Assert
        assert_eq!(report.captures.len(), 2);
        assert_eq!(report.captures[0].label, "startup");
        assert_eq!(report.captures[1].label, "after_input");
        assert!(report.captures[0].frame_text.contains("Hello, World!"));
    }

    #[test]
    fn add_assertion_attaches_to_labeled_capture() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Test");
        let mut report = ProofReport::new("assert_test");
        report.add_capture("check", "Checking state", &frame);

        // Act
        report.add_assertion("check", true, "Text 'Test' is visible");
        report.add_assertion("check", false, "Color is blue");

        // Assert
        let capture = &report.captures[0];
        assert_eq!(capture.assertions.len(), 2);
        assert!(capture.assertions[0].passed);
        assert!(!capture.assertions[1].passed);
    }

    #[test]
    fn add_assertion_targets_specific_capture() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Test");
        let mut report = ProofReport::new("targeted_test");
        report.add_capture("first", "First", &frame);
        report.add_capture("second", "Second", &frame);

        // Act
        let found = report.add_assertion("first", true, "targeted assertion");

        // Assert — assertion lands on "first", not "second".
        assert!(found);
        assert_eq!(report.captures[0].assertions.len(), 1);
        assert_eq!(
            report.captures[0].assertions[0].description,
            "targeted assertion"
        );
        assert!(report.captures[1].assertions.is_empty());
    }

    #[test]
    fn add_assertion_returns_false_for_unknown_label() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Test");
        let mut report = ProofReport::new("miss_test");
        report.add_capture("existing", "Existing", &frame);

        // Act
        let found = report.add_assertion("nonexistent", true, "orphan");

        // Assert
        assert!(!found);
        assert!(report.captures[0].assertions.is_empty());
    }

    #[test]
    fn annotated_text_contains_scenario_name() {
        // Arrange
        let report = ProofReport::new("my_scenario");

        // Act
        let text = report.to_annotated_text();

        // Assert
        assert!(text.contains("Proof Report: my_scenario"));
    }

    #[test]
    fn annotated_text_contains_step_labels_and_descriptions() {
        // Arrange
        let frame = TerminalFrame::new(40, 5, b"content");
        let mut report = ProofReport::new("labeled_test");
        report.add_capture("init", "Initial state", &frame);
        report.add_capture("done", "Final state", &frame);

        // Act
        let text = report.to_annotated_text();

        // Assert
        assert!(text.contains("Step 1: [init] Initial state"));
        assert!(text.contains("Step 2: [done] Final state"));
        assert!(text.contains("Total captures: 2"));
    }

    #[test]
    fn annotated_text_contains_frame_content() {
        // Arrange
        let frame = TerminalFrame::new(40, 5, b"Hello proof");
        let mut report = ProofReport::new("frame_test");
        report.add_capture("snap", "Snapshot", &frame);

        // Act
        let text = report.to_annotated_text();

        // Assert
        assert!(text.contains("Hello proof"));
        assert!(text.contains("Terminal: 40x5"));
    }

    #[test]
    fn annotated_text_contains_assertion_markers() {
        // Arrange
        let frame = TerminalFrame::new(40, 5, b"Test");
        let mut report = ProofReport::new("assert_output");
        report.add_capture("check", "Verify state", &frame);
        report.add_assertion("check", true, "text visible");
        report.add_assertion("check", false, "color match");

        // Act
        let text = report.to_annotated_text();

        // Assert
        assert!(text.contains("[PASS] text visible"));
        assert!(text.contains("[FAIL] color match"));
    }

    #[test]
    fn proof_capture_stores_dimensions() {
        // Arrange
        let frame = TerminalFrame::new(120, 40, b"wide");

        // Act
        let mut report = ProofReport::new("dims");
        report.add_capture("wide_term", "Wide terminal", &frame);

        // Assert
        assert_eq!(report.captures[0].cols, 120);
        assert_eq!(report.captures[0].rows, 40);
    }

    #[test]
    fn auto_diff_computed_between_consecutive_captures() {
        // Arrange
        let frame_a = TerminalFrame::new(80, 24, b"Hello");
        let frame_b = TerminalFrame::new(80, 24, b"World");

        // Act
        let mut report = ProofReport::new("diff_test");
        report.add_capture("before", "Before change", &frame_a);
        report.add_capture("after", "After change", &frame_b);

        // Assert — one diff between the two captures.
        assert_eq!(report.diffs.len(), 1);
        assert!(!report.diffs[0].is_identical());
    }

    #[test]
    fn no_diff_for_single_capture() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Solo");

        // Act
        let mut report = ProofReport::new("single");
        report.add_capture("only", "Only capture", &frame);

        // Assert
        assert!(report.diffs.is_empty());
    }

    #[test]
    fn identical_captures_produce_identical_diff() {
        // Arrange
        let frame = TerminalFrame::new(80, 24, b"Same");

        // Act
        let mut report = ProofReport::new("same");
        report.add_capture("first", "First", &frame);
        report.add_capture("second", "Second", &frame);

        // Assert
        assert_eq!(report.diffs.len(), 1);
        assert!(report.diffs[0].is_identical());
    }

    #[test]
    fn frame_bytes_preserves_style_for_diff() {
        // Arrange — same text but different style (plain vs bold).
        let frame_a = TerminalFrame::new(80, 24, b"Hello");
        let frame_b = TerminalFrame::new(80, 24, b"\x1b[1mHello\x1b[0m");

        // Act
        let mut report = ProofReport::new("style_diff");
        report.add_capture("plain", "Plain text", &frame_a);
        report.add_capture("bold", "Bold text", &frame_b);

        // Assert — style change should be detected because frame_bytes
        // preserves ANSI formatting for accurate reconstruction.
        assert_eq!(report.diffs.len(), 1);
        assert!(!report.diffs[0].is_identical());
    }

    #[test]
    fn frame_bytes_stores_formatted_output() {
        // Arrange — colored text.
        let frame = TerminalFrame::new(80, 24, b"\x1b[31mRed\x1b[0m");

        // Act
        let mut report = ProofReport::new("bytes_test");
        report.add_capture("colored", "Red text", &frame);

        // Assert — frame_bytes should contain ANSI escape sequences.
        let capture = &report.captures[0];
        assert!(!capture.frame_bytes.is_empty());
        assert!(capture.frame_bytes.len() > capture.frame_text.len());
    }
}