testty 0.12.5

Rust-native TUI end-to-end testing framework using PTY-driven semantic assertions, native frame rendering, and VHS-driven GIF 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Language-agnostic command-line front end for the `testty` framework.
//!
//! This binary ships inside the `testty` crate so non-Rust projects can drive
//! TUI end-to-end scenarios without writing Rust test harnesses. Installing the
//! crate (`cargo install testty`) provides the `testty` executable, which Cargo
//! auto-detects from this `src/main.rs` alongside the library `src/lib.rs`.
//!
//! The binary exposes the full `testty` verb tree (`run`, `schema`,
//! `proof open`, `proof gallery`, `update`).
//!
//! `run` is implemented: it loads a YAML scenario, lowers it onto the engine
//! via [`testty::spec`], drives the binary under test, and reports pass/fail
//! through the process exit code. The remaining verbs are still stubbed — each
//! reports "not yet implemented" on stderr and exits non-zero so callers and CI
//! can detect that the behavior is not wired up yet. Later tasks replace each
//! remaining [`Command::dispatch`] arm with real logic that delegates into the
//! `testty` library API.

use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use clap::{Parser, Subcommand};
use testty::spec::model::ScenarioSpec;

/// Top-level command-line interface for the `testty` runner.
#[derive(Parser)]
#[command(name = "testty", about, version)]
struct Cli {
    /// Selected verb to execute.
    #[command(subcommand)]
    command: Command,
}

/// Supported `testty` verbs.
///
/// Every variant is dispatched through [`Command::dispatch`], which currently
/// returns [`ExitCode::FAILURE`] for all verbs until the behavior is filled in.
#[derive(Subcommand)]
enum Command {
    /// Runs a scenario file against a TUI binary.
    Run {
        /// Path to the scenario definition (for example, `scenario.yaml`).
        scenario: PathBuf,
        /// Path to the binary under test, overriding the scenario default.
        #[arg(long)]
        bin: Option<PathBuf>,
        /// Output directory for generated proof artifacts.
        #[arg(long)]
        proof: Option<PathBuf>,
    },
    /// Prints the JSON schema for scenario files.
    Schema,
    /// Inspects proof artifacts produced by a run.
    Proof {
        /// Selected proof subcommand.
        #[command(subcommand)]
        command: ProofCommand,
    },
    /// Updates stored scenario snapshots to match current output.
    Update,
}

impl Command {
    /// Executes the selected verb and returns the process exit code.
    ///
    /// `run` executes a YAML scenario via [`run_scenario`]; the remaining arms
    /// are stubbed and return [`ExitCode::FAILURE`] with a "not yet
    /// implemented" notice. Replace the stubbed arms as the verbs are
    /// implemented.
    fn dispatch(self) -> ExitCode {
        match self {
            Command::Run {
                scenario,
                bin,
                proof,
            } => run_scenario(&scenario, bin.as_deref(), proof.as_deref()),
            Command::Schema => not_implemented("schema"),
            Command::Proof {
                command: ProofCommand::Open { .. },
            } => not_implemented("proof open"),
            Command::Proof {
                command: ProofCommand::Gallery { .. },
            } => not_implemented("proof gallery"),
            Command::Update => not_implemented("update"),
        }
    }
}

/// Subcommands for inspecting proof artifacts.
#[derive(Subcommand)]
enum ProofCommand {
    /// Opens a single proof report in the browser.
    Open {
        /// Path to the proof HTML report.
        html: PathBuf,
    },
    /// Builds a gallery from a directory of proof reports.
    Gallery {
        /// Directory containing proof reports.
        dir: PathBuf,
    },
}

/// Parses arguments and dispatches the selected verb.
fn main() -> ExitCode {
    let cli = Cli::parse();

    cli.command.dispatch()
}

/// Loads a YAML scenario, runs it against the binary, and reports the outcome.
///
/// Binds the production diagnostic sink (locked stderr) and delegates to
/// [`run_scenario_reporting`]. The process exit code is the pass/fail signal
/// for non-Rust CI.
fn run_scenario(
    scenario_path: &Path,
    bin_override: Option<&Path>,
    proof: Option<&Path>,
) -> ExitCode {
    let mut stderr = io::stderr().lock();

    run_scenario_reporting(scenario_path, bin_override, proof, &mut stderr)
}

/// Runs a YAML scenario and writes every diagnostic to `out`.
///
/// `bin_override` replaces the scenario's `session.bin` (the `--bin` flag).
/// Routing diagnostics through an injected writer (instead of writing to
/// stderr directly) keeps the messages observable in tests while still
/// avoiding the `print_stderr` lint. The exit code is `SUCCESS` when every
/// expectation passes, `FAILURE` on any failed expectation, parse error, or
/// run error.
fn run_scenario_reporting(
    scenario_path: &Path,
    bin_override: Option<&Path>,
    proof: Option<&Path>,
    out: &mut dyn Write,
) -> ExitCode {
    let text = match fs::read_to_string(scenario_path) {
        Ok(text) => text,
        Err(err) => {
            let _ = writeln!(
                out,
                "testty: cannot read {}: {err}",
                scenario_path.display()
            );

            return ExitCode::FAILURE;
        }
    };

    let mut spec = match ScenarioSpec::from_yaml(&text) {
        Ok(spec) => spec,
        Err(err) => {
            let _ = writeln!(out, "testty: {err}");

            return ExitCode::FAILURE;
        }
    };

    if let Some(bin) = bin_override {
        spec.session.bin = bin.to_path_buf();
    }

    if proof.is_some() {
        let _ = writeln!(
            out,
            "testty: --proof is not yet supported; running without proof output"
        );
    }

    let (_frame, failures) = match spec.lower().run() {
        Ok(result) => result,
        Err(err) => {
            let _ = writeln!(out, "testty: scenario failed to run: {err}");

            return ExitCode::FAILURE;
        }
    };

    if failures.is_empty() {
        let _ = writeln!(out, "testty: scenario passed");

        return ExitCode::SUCCESS;
    }

    for failure in &failures {
        let _ = writeln!(out, "FAIL: {}", failure.message);
    }
    let _ = writeln!(out, "testty: {} expectation(s) failed", failures.len());

    ExitCode::FAILURE
}

/// Reports an unimplemented verb on stderr and returns a failing exit code.
///
/// Writing directly to stderr (instead of `eprintln!`) keeps the stub free of
/// the `print_stderr` lint while still surfacing the notice to callers.
fn not_implemented(verb: &str) -> ExitCode {
    let mut stderr = io::stderr().lock();
    let _ = writeln!(stderr, "testty: `{verb}` is not yet implemented");

    ExitCode::FAILURE
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::*;

    #[test]
    fn parses_run_with_scenario_path() {
        // Arrange
        let argv = ["testty", "run", "scenario.yaml"];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(
            cli.command,
            Command::Run { scenario, bin: None, proof: None }
                if scenario.as_path() == Path::new("scenario.yaml")
        ));
    }

    #[test]
    fn parses_run_with_bin_and_proof_options() {
        // Arrange
        let argv = [
            "testty",
            "run",
            "scenario.yaml",
            "--bin",
            "./app",
            "--proof",
            "out",
        ];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(
            cli.command,
            Command::Run { bin: Some(bin), proof: Some(proof), .. }
                if bin.as_path() == Path::new("./app") && proof.as_path() == Path::new("out")
        ));
    }

    #[test]
    fn parses_schema_verb() {
        // Arrange
        let argv = ["testty", "schema"];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(cli.command, Command::Schema));
    }

    #[test]
    fn parses_proof_open_with_html_path() {
        // Arrange
        let argv = ["testty", "proof", "open", "report.html"];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(
            cli.command,
            Command::Proof { command: ProofCommand::Open { html } }
                if html.as_path() == Path::new("report.html")
        ));
    }

    #[test]
    fn parses_proof_gallery_with_dir_path() {
        // Arrange
        let argv = ["testty", "proof", "gallery", "proofs"];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(
            cli.command,
            Command::Proof { command: ProofCommand::Gallery { dir } }
                if dir.as_path() == Path::new("proofs")
        ));
    }

    #[test]
    fn parses_update_verb() {
        // Arrange
        let argv = ["testty", "update"];

        // Act
        let cli = Cli::parse_from(argv);

        // Assert
        assert!(matches!(cli.command, Command::Update));
    }

    #[test]
    fn run_without_scenario_is_rejected() {
        // Arrange
        let argv = ["testty", "run"];

        // Act
        let result = Cli::try_parse_from(argv);

        // Assert
        assert!(result.is_err());
    }

    #[test]
    fn every_remaining_stub_reports_unimplemented_and_fails() {
        // Arrange — verbs whose behavior is not yet wired up.
        let verbs: [Command; 4] = [
            Command::Schema,
            Command::Proof {
                command: ProofCommand::Open {
                    html: PathBuf::from("r.html"),
                },
            },
            Command::Proof {
                command: ProofCommand::Gallery {
                    dir: PathBuf::from("d"),
                },
            },
            Command::Update,
        ];

        // Act + Assert
        for verb in verbs {
            assert_eq!(verb.dispatch(), ExitCode::FAILURE);
        }
    }

    #[test]
    fn run_scenario_fails_when_file_missing() {
        // Arrange
        let missing = Path::new("/nonexistent/testty-scenario.yaml");

        // Act
        let code = run_scenario(missing, None, None);

        // Assert
        assert_eq!(code, ExitCode::FAILURE);
    }

    #[test]
    fn run_scenario_fails_on_unsupported_version() {
        // Arrange — an otherwise valid scenario with an unknown version.
        let temp = tempfile::tempdir().expect("temp dir");
        let path = temp.path().join("bad.yaml");
        std::fs::write(&path, "version: 999\nsession:\n  bin: /bin/echo\n").expect("write");

        // Act
        let code = run_scenario(&path, None, None);

        // Assert
        assert_eq!(code, ExitCode::FAILURE);
    }

    /// The `Run` dispatch arm actually routes through `run_scenario`: a valid
    /// scenario against a real fixture reports success, which a stub that
    /// merely returned `FAILURE` could not produce.
    #[cfg(unix)]
    #[test]
    fn dispatch_runs_scenario_for_run_verb() {
        use std::os::unix::fs::PermissionsExt;

        // Arrange — a fixture that renders deterministic text and a scenario
        // that expects it, wired through the `Run` verb.
        let temp = tempfile::tempdir().expect("temp dir");
        let script = temp.path().join("greet.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf 'Hello World'\nsleep 60\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("perms");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            format!(
                "session:\n  bin: {bin}\n  size: [80, 24]\nsteps:\n  - wait_for_stable_frame: {{ \
                 stable_ms: 300, timeout_ms: 5000 }}\nexpect:\n  - text_in_region: {{ text: \
                 \"Hello World\", region: [0, 0, 80, 1] }}\n",
                bin = script.display()
            ),
        )
        .expect("write scenario");
        let verb = Command::Run {
            scenario,
            bin: None,
            proof: None,
        };

        // Act
        let code = verb.dispatch();

        // Assert
        assert_eq!(code, ExitCode::SUCCESS);
    }

    #[test]
    fn run_scenario_fails_when_binary_cannot_spawn() {
        // Arrange — a parseable scenario whose binary does not exist, so the
        // engine errors while spawning rather than producing expectations.
        let temp = tempfile::tempdir().expect("temp dir");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            "session:\n  bin: /nonexistent/testty-missing-binary\n  size: [80, 24]\nsteps:\n  - \
             wait_for_stable_frame: { stable_ms: 100, timeout_ms: 1000 }\n",
        )
        .expect("write scenario");

        // Act
        let code = run_scenario(&scenario, None, None);

        // Assert
        assert_eq!(code, ExitCode::FAILURE);
    }

    /// End-to-end: the `run` verb drives a real binary and reports success.
    #[cfg(unix)]
    #[test]
    fn run_scenario_passes_against_fixture_binary() {
        use std::os::unix::fs::PermissionsExt;

        // Arrange — a fixture that renders deterministic text and stays alive.
        let temp = tempfile::tempdir().expect("temp dir");
        let script = temp.path().join("greet.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf 'Hello World'\nsleep 60\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("perms");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            format!(
                "session:\n  bin: {bin}\n  size: [80, 24]\nsteps:\n  - wait_for_stable_frame: {{ \
                 stable_ms: 300, timeout_ms: 5000 }}\nexpect:\n  - text_in_region: {{ text: \
                 \"Hello World\", region: [0, 0, 80, 1] }}\n",
                bin = script.display()
            ),
        )
        .expect("write scenario");

        // Act
        let code = run_scenario(&scenario, None, None);

        // Assert
        assert_eq!(code, ExitCode::SUCCESS);
    }

    /// `bin_override` (the `--bin` flag) replaces the scenario's `session.bin`:
    /// the scenario points at a binary that cannot spawn, so success is only
    /// possible when the override binary is the one actually driven.
    #[cfg(unix)]
    #[test]
    fn run_scenario_honors_bin_override() {
        use std::os::unix::fs::PermissionsExt;

        // Arrange — the real fixture is supplied through `bin_override`, while
        // the scenario points at a placeholder binary that must never spawn.
        let temp = tempfile::tempdir().expect("temp dir");
        let script = temp.path().join("greet.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf 'Hello World'\nsleep 60\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("perms");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            "session:\n  bin: /nonexistent/placeholder\n  size: [80, 24]\nsteps:\n  - \
             wait_for_stable_frame: { stable_ms: 300, timeout_ms: 5000 }\nexpect:\n  - \
             text_in_region: { text: \"Hello World\", region: [0, 0, 80, 1] }\n",
        )
        .expect("write scenario");

        // Act
        let code = run_scenario(&scenario, Some(&script), None);

        // Assert
        assert_eq!(code, ExitCode::SUCCESS);
    }

    /// A `--proof` directory is not yet supported, so the run emits a notice on
    /// the diagnostic sink and still drives the scenario to completion.
    #[cfg(unix)]
    #[test]
    fn run_scenario_warns_when_proof_requested() {
        use std::os::unix::fs::PermissionsExt;

        // Arrange — a passing fixture scenario plus a requested proof directory.
        let temp = tempfile::tempdir().expect("temp dir");
        let script = temp.path().join("greet.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf 'Hello World'\nsleep 60\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("perms");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            format!(
                "session:\n  bin: {bin}\n  size: [80, 24]\nsteps:\n  - wait_for_stable_frame: {{ \
                 stable_ms: 300, timeout_ms: 5000 }}\nexpect:\n  - text_in_region: {{ text: \
                 \"Hello World\", region: [0, 0, 80, 1] }}\n",
                bin = script.display()
            ),
        )
        .expect("write scenario");
        let proof = temp.path().join("proof-out");
        let mut out = Vec::new();

        // Act
        let code = run_scenario_reporting(&scenario, None, Some(&proof), &mut out);

        // Assert
        let log = String::from_utf8(out).expect("utf8 log");
        assert_eq!(code, ExitCode::SUCCESS);
        assert!(log.contains("--proof is not yet supported"));
    }

    /// A scenario whose expectation does not match the rendered frame reports
    /// the failed expectation on the diagnostic sink and exits with failure.
    #[cfg(unix)]
    #[test]
    fn run_scenario_reports_failed_expectations() {
        use std::os::unix::fs::PermissionsExt;

        // Arrange — the fixture renders "Hello World" but the scenario expects
        // different text.
        let temp = tempfile::tempdir().expect("temp dir");
        let script = temp.path().join("greet.sh");
        std::fs::write(&script, "#!/bin/sh\nprintf 'Hello World'\nsleep 60\n").expect("write");
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("perms");
        let scenario = temp.path().join("scenario.yaml");
        std::fs::write(
            &scenario,
            format!(
                "session:\n  bin: {bin}\n  size: [80, 24]\nsteps:\n  - wait_for_stable_frame: {{ \
                 stable_ms: 300, timeout_ms: 5000 }}\nexpect:\n  - text_in_region: {{ text: \
                 \"Goodbye Moon\", region: [0, 0, 80, 1] }}\n",
                bin = script.display()
            ),
        )
        .expect("write scenario");
        let mut out = Vec::new();

        // Act
        let code = run_scenario_reporting(&scenario, None, None, &mut out);

        // Assert
        let log = String::from_utf8(out).expect("utf8 log");
        assert_eq!(code, ExitCode::FAILURE);
        assert!(log.contains("expectation(s) failed"));
    }
}