testty 0.10.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
//! Declarative scenario specification — the pure-data model that a YAML
//! scenario file deserializes into.
//!
//! [`ScenarioSpec`] and its children carry no behavior; they are lowered onto
//! the runtime engine (`Scenario`, `Step`, matcher functions) by
//! [`ScenarioSpec::lower`](super::runtime). Keeping the spec as plain data lets
//! the same engine power both the Rust authoring API and the language-agnostic
//! `testty run scenario.yaml` front end.
//!
//! [`StepSpec`] and [`ExpectSpec`] use a hand-written `Deserialize` so each
//! YAML list item is a friendly **single-key map** (`press_key: Tab`) rather
//! than serde's default YAML-tag form (`!press_key Tab`), which non-expert
//! authors rarely recognize.

use std::collections::BTreeMap;
use std::fmt;
use std::path::PathBuf;

use serde::de::{self, IgnoredAny, MapAccess, Visitor};
use serde::{Deserialize, Deserializer};

/// The scenario-file format version this build understands.
///
/// A file whose `version:` differs is rejected by
/// [`ScenarioSpec::from_yaml`](super::runtime) so non-Rust authors get a clear
/// error instead of silent misbehavior.
pub const SUPPORTED_VERSION: u32 = 1;

/// Default `version` when a scenario file omits the field.
fn default_version() -> u32 {
    SUPPORTED_VERSION
}

/// A complete declarative scenario: how to launch the binary, the steps to
/// drive it, and the expectations to assert against the final frame.
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ScenarioSpec {
    /// Scenario-file format version. Defaults to [`SUPPORTED_VERSION`].
    #[serde(default = "default_version")]
    pub version: u32,
    /// Optional human-readable scenario name used in proof output.
    #[serde(default)]
    pub name: Option<String>,
    /// How to launch the binary under test.
    pub session: SessionSpec,
    /// Ordered steps that drive the session.
    #[serde(default)]
    pub steps: Vec<StepSpec>,
    /// Expectations asserted against the final frame after the steps run.
    #[serde(default)]
    pub expect: Vec<ExpectSpec>,
}

/// How to launch the binary under test.
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionSpec {
    /// Path to the binary under test. The CLI `--bin` flag overrides this.
    pub bin: PathBuf,
    /// Terminal size as `[cols, rows]`. Defaults to the engine default.
    #[serde(default)]
    pub size: Option<[u16; 2]>,
    /// Command-line arguments passed to the binary.
    #[serde(default)]
    pub args: Vec<String>,
    /// Environment variables set for the binary.
    #[serde(default)]
    pub env: BTreeMap<String, String>,
    /// Working directory the binary launches in.
    #[serde(default)]
    pub workdir: Option<PathBuf>,
}

/// A single declarative step. In YAML each list item is a single-key map
/// (`press_key: Tab`) or the bare word `capture`.
///
/// `#[non_exhaustive]`: new step kinds can be added without breaking external
/// Rust code that matches this enum (such code keeps a `_` arm).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum StepSpec {
    /// Press a named key (for example `Tab`, `Enter`, `Esc`).
    PressKey(String),
    /// Type literal text.
    WriteText(String),
    /// Sleep for the given number of milliseconds.
    SleepMs(u64),
    /// Wait until `needle` appears, up to `timeout_ms`.
    WaitForText {
        /// Text to wait for.
        needle: String,
        /// Maximum time to wait, in milliseconds.
        timeout_ms: u32,
    },
    /// Wait until the screen is unchanged for `stable_ms`, up to `timeout_ms`.
    WaitForStableFrame {
        /// Quiet period the frame must hold, in milliseconds.
        stable_ms: u32,
        /// Maximum time to wait, in milliseconds.
        timeout_ms: u32,
    },
    /// Poll an expectation until it passes or `timeout_ms` elapses.
    Eventually {
        /// The expectation to poll for (the `match` key in YAML).
        matcher: ExpectSpec,
        /// Maximum time to poll, in milliseconds.
        timeout_ms: u64,
        /// Delay between polls, in milliseconds.
        poll_ms: u64,
    },
    /// Capture the current frame for proof output.
    Capture,
    /// Capture the current frame with a label and description.
    CaptureLabeled {
        /// Short identifier for the capture.
        label: String,
        /// Human-readable description of the capture.
        description: String,
    },
}

/// A declarative expectation. In YAML each entry is a single-key map
/// (`selected_tab: Sessions`). Each variant maps to one `recipe::match_*` or
/// `assertion::match_*` matcher when evaluated against a frame.
///
/// `#[non_exhaustive]`: new matcher keys can be added without breaking external
/// Rust matchers.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ExpectSpec {
    /// The named tab is selected.
    SelectedTab(String),
    /// The named tab is present but not selected.
    UnselectedTab(String),
    /// An instruction line with the given text is visible.
    InstructionVisible(String),
    /// A keybinding hint with the given text is visible.
    KeybindingHint(String),
    /// A footer action with the given label is visible.
    FooterAction(String),
    /// A dialog with the given title is visible.
    DialogTitle(String),
    /// A status message with the given text is visible.
    StatusMessage(String),
    /// The given text is not visible anywhere on screen.
    NotVisible(String),
    /// The given text appears within a `[col, row, width, height]` region.
    TextInRegion {
        /// Text expected within the region.
        text: String,
        /// Region as `[col, row, width, height]`.
        region: RegionSpec,
    },
}

/// A rectangular region expressed as `[col, row, width, height]`.
#[derive(Debug, Clone, Copy, Deserialize)]
pub struct RegionSpec(pub u16, pub u16, pub u16, pub u16);

// Typed payloads for the struct-shaped step/expect variants. Each rejects
// unknown fields so author typos surface immediately.

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct WaitForTextArgs {
    needle: String,
    timeout_ms: u32,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct WaitForStableFrameArgs {
    stable_ms: u32,
    timeout_ms: u32,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct EventuallyArgs {
    #[serde(rename = "match")]
    matcher: ExpectSpec,
    timeout_ms: u64,
    poll_ms: u64,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CaptureLabeledArgs {
    label: String,
    description: String,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TextInRegionArgs {
    text: String,
    region: RegionSpec,
}

/// Reject a second key in a single-key step/expect map.
fn reject_extra_key<'de, A: MapAccess<'de>>(map: &mut A, kind: &str) -> Result<(), A::Error> {
    if map.next_key::<IgnoredAny>()?.is_some() {
        return Err(de::Error::custom(format!(
            "a {kind} must have exactly one key"
        )));
    }

    Ok(())
}

impl<'de> Deserialize<'de> for StepSpec {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct StepVisitor;

        impl<'de> Visitor<'de> for StepVisitor {
            type Value = StepSpec;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter
                    .write_str("a step: `capture` or a single-key map such as `press_key: Tab`")
            }

            fn visit_str<E: de::Error>(self, value: &str) -> Result<StepSpec, E> {
                match value {
                    "capture" => Ok(StepSpec::Capture),
                    other => Err(E::custom(format!("unknown step `{other}`"))),
                }
            }

            fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<StepSpec, A::Error> {
                let Some(key) = map.next_key::<String>()? else {
                    return Err(de::Error::custom("a step map must have one key"));
                };

                let step = match key.as_str() {
                    "press_key" => StepSpec::PressKey(map.next_value()?),
                    "write_text" => StepSpec::WriteText(map.next_value()?),
                    "sleep_ms" => StepSpec::SleepMs(map.next_value()?),
                    "wait_for_text" => {
                        let args: WaitForTextArgs = map.next_value()?;

                        StepSpec::WaitForText {
                            needle: args.needle,
                            timeout_ms: args.timeout_ms,
                        }
                    }
                    "wait_for_stable_frame" => {
                        let args: WaitForStableFrameArgs = map.next_value()?;

                        StepSpec::WaitForStableFrame {
                            stable_ms: args.stable_ms,
                            timeout_ms: args.timeout_ms,
                        }
                    }
                    "eventually" => {
                        let args: EventuallyArgs = map.next_value()?;

                        StepSpec::Eventually {
                            matcher: args.matcher,
                            timeout_ms: args.timeout_ms,
                            poll_ms: args.poll_ms,
                        }
                    }
                    "capture_labeled" => {
                        let args: CaptureLabeledArgs = map.next_value()?;

                        StepSpec::CaptureLabeled {
                            label: args.label,
                            description: args.description,
                        }
                    }
                    other => return Err(de::Error::custom(format!("unknown step `{other}`"))),
                };

                reject_extra_key(&mut map, "step")?;

                Ok(step)
            }
        }

        deserializer.deserialize_any(StepVisitor)
    }
}

impl<'de> Deserialize<'de> for ExpectSpec {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ExpectVisitor;

        impl<'de> Visitor<'de> for ExpectVisitor {
            type Value = ExpectSpec;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("an expectation map such as `selected_tab: Sessions`")
            }

            fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<ExpectSpec, A::Error> {
                let Some(key) = map.next_key::<String>()? else {
                    return Err(de::Error::custom("an expectation map must have one key"));
                };

                let expect = match key.as_str() {
                    "selected_tab" => ExpectSpec::SelectedTab(map.next_value()?),
                    "unselected_tab" => ExpectSpec::UnselectedTab(map.next_value()?),
                    "instruction_visible" => ExpectSpec::InstructionVisible(map.next_value()?),
                    "keybinding_hint" => ExpectSpec::KeybindingHint(map.next_value()?),
                    "footer_action" => ExpectSpec::FooterAction(map.next_value()?),
                    "dialog_title" => ExpectSpec::DialogTitle(map.next_value()?),
                    "status_message" => ExpectSpec::StatusMessage(map.next_value()?),
                    "not_visible" => ExpectSpec::NotVisible(map.next_value()?),
                    "text_in_region" => {
                        let args: TextInRegionArgs = map.next_value()?;

                        ExpectSpec::TextInRegion {
                            text: args.text,
                            region: args.region,
                        }
                    }
                    other => {
                        return Err(de::Error::custom(format!("unknown expectation `{other}`")));
                    }
                };

                reject_extra_key(&mut map, "expectation")?;

                Ok(expect)
            }
        }

        deserializer.deserialize_any(ExpectVisitor)
    }
}

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

    #[test]
    fn deserializes_press_key_step() {
        // Arrange
        let yaml = "
session:
  bin: ./app
steps:
  - press_key: Tab
";

        // Act
        let spec: ScenarioSpec = serde_yaml_ng::from_str(yaml).expect("parse");

        // Assert
        assert_eq!(spec.version, SUPPORTED_VERSION);
        assert_eq!(spec.session.bin, PathBuf::from("./app"));
        assert_eq!(spec.steps.len(), 1);
        assert!(matches!(&spec.steps[0], StepSpec::PressKey(key) if key == "Tab"));
    }

    #[test]
    fn deserializes_session_size_args_and_expect() {
        // Arrange
        let yaml = "
version: 1
session:
  bin: ./app
  size: [80, 24]
  args: [--flag, value]
steps:
  - write_text: hello
  - wait_for_stable_frame: { stable_ms: 500, timeout_ms: 5000 }
expect:
  - selected_tab: Sessions
  - text_in_region: { text: \"Counter: 3\", region: [0, 0, 80, 24] }
";

        // Act
        let spec: ScenarioSpec = serde_yaml_ng::from_str(yaml).expect("parse");

        // Assert
        assert_eq!(spec.session.size, Some([80, 24]));
        assert_eq!(spec.session.args, vec!["--flag", "value"]);
        assert!(matches!(&spec.steps[0], StepSpec::WriteText(text) if text == "hello"));
        assert!(matches!(
            spec.steps[1],
            StepSpec::WaitForStableFrame {
                stable_ms: 500,
                timeout_ms: 5000
            }
        ));
        assert!(matches!(&spec.expect[0], ExpectSpec::SelectedTab(tab) if tab == "Sessions"));
        assert!(matches!(
            &spec.expect[1],
            ExpectSpec::TextInRegion { text, region }
                if text == "Counter: 3" && (region.0, region.1, region.2, region.3) == (0, 0, 80, 24)
        ));
    }

    #[test]
    fn deserializes_bare_capture_step() {
        // Arrange
        let yaml = "
session:
  bin: ./app
steps:
  - capture
";

        // Act
        let spec: ScenarioSpec = serde_yaml_ng::from_str(yaml).expect("parse");

        // Assert
        assert!(matches!(spec.steps[0], StepSpec::Capture));
    }

    #[test]
    fn deserializes_eventually_step_with_nested_matcher() {
        // Arrange
        let yaml = "
session:
  bin: ./app
steps:
  - eventually:
      match: { not_visible: Loading }
      timeout_ms: 3000
      poll_ms: 50
";

        // Act
        let spec: ScenarioSpec = serde_yaml_ng::from_str(yaml).expect("parse");

        // Assert
        assert!(matches!(
            &spec.steps[0],
            StepSpec::Eventually { matcher, timeout_ms: 3000, poll_ms: 50 }
                if matches!(matcher, ExpectSpec::NotVisible(text) if text == "Loading")
        ));
    }

    #[test]
    fn rejects_unknown_top_level_field() {
        // Arrange — `step` (singular) is a typo for `steps`.
        let yaml = "
session:
  bin: ./app
step:
  - press_key: Tab
";

        // Act
        let result: Result<ScenarioSpec, _> = serde_yaml_ng::from_str(yaml);

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

    #[test]
    fn rejects_unknown_step_key() {
        // Arrange
        let yaml = "
session:
  bin: ./app
steps:
  - press_buttn: Tab
";

        // Act
        let result: Result<ScenarioSpec, _> = serde_yaml_ng::from_str(yaml);

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