ftui-harness 0.4.0

Test harness and reference fixtures for FrankenTUI.
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#![forbid(unsafe_code)]

//! Shadow-run comparison harness for validating execution-path equivalence.
//!
//! Runs the same [`Model`] and event sequence through two independent
//! [`LabSession`] instances (baseline vs candidate) and compares frame
//! checksums, event counts, and timing. This is the primary mechanism for
//! proving that a runtime migration (e.g., threading → Asupersync executor)
//! preserves rendering determinism.
//!
//! # Design
//!
//! A [`ShadowRun`] takes two [`LabConfig`]s (baseline and candidate), a
//! model factory, and a scenario closure. It executes the scenario twice—
//! once per lane—under deterministic seeds, then compares the frame records.
//! All comparison evidence is emitted to JSONL via [`TestJsonlLogger`].
//!
//! # Example
//!
//! ```ignore
//! use ftui_harness::shadow_run::{ShadowRun, ShadowRunConfig, ShadowVerdict};
//!
//! let config = ShadowRunConfig::new("migration_test", "tick_counter", 42)
//!     .viewport(80, 24);
//!
//! let result = ShadowRun::compare(config, || MyModel::new(), |session| {
//!     session.init();
//!     session.tick();
//!     session.capture_frame();
//! });
//!
//! assert_eq!(result.verdict, ShadowVerdict::Match);
//! ```

use std::sync::atomic::{AtomicU64, Ordering};

use crate::determinism::{JsonValue, TestJsonlLogger};
use crate::lab_integration::{Lab, LabConfig, LabOutput, LabSession};
use ftui_runtime::program::Model;
use tracing::info_span;

/// Global counter for shadow runs executed.
static SHADOW_RUNS_TOTAL: AtomicU64 = AtomicU64::new(0);

/// Read the total number of shadow runs executed in-process.
#[must_use]
pub fn shadow_runs_total() -> u64 {
    SHADOW_RUNS_TOTAL.load(Ordering::Relaxed)
}

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for a shadow-run comparison.
#[derive(Debug, Clone)]
pub struct ShadowRunConfig {
    /// Shared prefix for JSONL logger and run IDs.
    pub prefix: String,
    /// Scenario name used in tracing spans and JSONL.
    pub scenario_name: String,
    /// Deterministic seed (shared across both lanes).
    pub seed: u64,
    /// Viewport width for frame captures.
    pub viewport_width: u16,
    /// Viewport height for frame captures.
    pub viewport_height: u16,
    /// Time step in milliseconds for deterministic clocks.
    pub time_step_ms: u64,
    /// Label for the baseline lane (default: "baseline").
    pub baseline_label: String,
    /// Label for the candidate lane (default: "candidate").
    pub candidate_label: String,
}

impl ShadowRunConfig {
    /// Create a new shadow-run configuration with defaults.
    ///
    /// Defaults: 80×24 viewport, 16ms time step.
    pub fn new(prefix: &str, scenario_name: &str, seed: u64) -> Self {
        Self {
            prefix: prefix.to_string(),
            scenario_name: scenario_name.to_string(),
            seed,
            viewport_width: 80,
            viewport_height: 24,
            time_step_ms: 16,
            baseline_label: "baseline".to_string(),
            candidate_label: "candidate".to_string(),
        }
    }

    /// Set the viewport dimensions.
    #[must_use]
    pub fn viewport(mut self, width: u16, height: u16) -> Self {
        self.viewport_width = width;
        self.viewport_height = height;
        self
    }

    /// Set the deterministic time step in milliseconds.
    #[must_use]
    pub fn time_step_ms(mut self, ms: u64) -> Self {
        self.time_step_ms = ms;
        self
    }

    /// Set custom lane labels.
    #[must_use]
    pub fn lane_labels(mut self, baseline: &str, candidate: &str) -> Self {
        self.baseline_label = baseline.to_string();
        self.candidate_label = candidate.to_string();
        self
    }

    /// Build a [`LabConfig`] for a given lane.
    fn lab_config(&self, lane: &str) -> LabConfig {
        LabConfig::new(
            &format!("{}_{}", self.prefix, lane),
            &self.scenario_name,
            self.seed,
        )
        .viewport(self.viewport_width, self.viewport_height)
        .time_step_ms(self.time_step_ms)
    }
}

// ============================================================================
// Verdict and result
// ============================================================================

/// Outcome of a shadow-run comparison.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShadowVerdict {
    /// All frame checksums matched between baseline and candidate.
    Match,
    /// Frame checksums diverged at one or more positions.
    Diverged,
}

/// Per-frame comparison detail.
#[derive(Debug, Clone)]
pub struct FrameComparison {
    /// Frame index (0-based).
    pub index: usize,
    /// Baseline frame checksum.
    pub baseline_checksum: u64,
    /// Candidate frame checksum.
    pub candidate_checksum: u64,
    /// Whether this frame matched.
    pub matched: bool,
}

/// Full result of a shadow-run comparison.
#[derive(Debug, Clone)]
pub struct ShadowRunResult {
    /// Overall verdict.
    pub verdict: ShadowVerdict,
    /// Scenario name.
    pub scenario_name: String,
    /// Seed used for both lanes.
    pub seed: u64,
    /// Per-frame comparison details.
    pub frame_comparisons: Vec<FrameComparison>,
    /// Index of the first divergent frame (if any).
    pub first_divergence: Option<usize>,
    /// Number of frames compared.
    pub frames_compared: usize,
    /// Baseline lane output.
    pub baseline: LabOutput,
    /// Candidate lane output.
    pub candidate: LabOutput,
    /// Baseline lane label.
    pub baseline_label: String,
    /// Candidate lane label.
    pub candidate_label: String,
    /// Total shadow runs executed in-process (including this one).
    pub run_total: u64,
}

impl ShadowRunResult {
    /// Number of frames that diverged.
    #[must_use]
    pub fn diverged_count(&self) -> usize {
        self.frame_comparisons.iter().filter(|c| !c.matched).count()
    }

    /// Fraction of frames that matched (0.0–1.0).
    #[must_use]
    pub fn match_ratio(&self) -> f64 {
        if self.frames_compared == 0 {
            return 1.0;
        }
        let matched = self.frame_comparisons.iter().filter(|c| c.matched).count();
        matched as f64 / self.frames_compared as f64
    }
}

// ============================================================================
// Shadow-run executor
// ============================================================================

/// Shadow-run comparison harness.
///
/// Runs the same model and event sequence through two independent LabSession
/// instances and compares their frame outputs.
pub struct ShadowRun;

impl ShadowRun {
    /// Run a shadow comparison between baseline and candidate lanes.
    ///
    /// Both lanes execute the same `scenario_fn` with the same seed and
    /// configuration. Frame checksums are compared after both runs complete.
    ///
    /// The `model_factory` is called twice (once per lane) to produce
    /// independent model instances.
    ///
    /// # Evidence
    ///
    /// Emits structured JSONL to stderr with events:
    /// - `shadow.start`: comparison parameters
    /// - `shadow.lane.done`: per-lane summary
    /// - `shadow.frame.diverged`: each divergent frame
    /// - `shadow.verdict`: final pass/fail with statistics
    pub fn compare<M, MF, SF>(
        config: ShadowRunConfig,
        model_factory: MF,
        scenario_fn: SF,
    ) -> ShadowRunResult
    where
        M: Model,
        MF: Fn() -> M,
        SF: Fn(&mut LabSession<M>),
    {
        let _span = info_span!(
            "shadow_run",
            scenario_name = config.scenario_name.as_str(),
            seed = config.seed,
            baseline = config.baseline_label.as_str(),
            candidate = config.candidate_label.as_str(),
        )
        .entered();

        let mut logger = TestJsonlLogger::new_with(
            &format!("{}_shadow", config.prefix),
            config.seed,
            true,
            config.time_step_ms,
        );
        logger.add_context_str("scenario_name", &config.scenario_name);
        logger.add_context_str("baseline_label", &config.baseline_label);
        logger.add_context_str("candidate_label", &config.candidate_label);

        // Log start
        logger.log(
            "shadow.start",
            &[
                ("scenario_name", JsonValue::str(&config.scenario_name)),
                ("seed", JsonValue::u64(config.seed)),
                (
                    "viewport",
                    JsonValue::raw(format!(
                        "[{},{}]",
                        config.viewport_width, config.viewport_height
                    )),
                ),
            ],
        );

        // Run baseline lane
        let baseline_config = config.lab_config(&config.baseline_label);
        let baseline_run = Lab::run_scenario(baseline_config, model_factory(), |s| scenario_fn(s));

        logger.log(
            "shadow.lane.done",
            &[
                ("lane", JsonValue::str(&config.baseline_label)),
                (
                    "frame_count",
                    JsonValue::u64(baseline_run.output.frame_count as u64),
                ),
                (
                    "event_count",
                    JsonValue::u64(baseline_run.output.event_count as u64),
                ),
                ("tick_count", JsonValue::u64(baseline_run.output.tick_count)),
                (
                    "anomaly_count",
                    JsonValue::u64(baseline_run.output.anomaly_count),
                ),
            ],
        );

        // Run candidate lane
        let candidate_config = config.lab_config(&config.candidate_label);
        let candidate_run =
            Lab::run_scenario(candidate_config, model_factory(), |s| scenario_fn(s));

        logger.log(
            "shadow.lane.done",
            &[
                ("lane", JsonValue::str(&config.candidate_label)),
                (
                    "frame_count",
                    JsonValue::u64(candidate_run.output.frame_count as u64),
                ),
                (
                    "event_count",
                    JsonValue::u64(candidate_run.output.event_count as u64),
                ),
                (
                    "tick_count",
                    JsonValue::u64(candidate_run.output.tick_count),
                ),
                (
                    "anomaly_count",
                    JsonValue::u64(candidate_run.output.anomaly_count),
                ),
            ],
        );

        // Compare frame checksums
        let baseline_frames = &baseline_run.output.frame_records;
        let candidate_frames = &candidate_run.output.frame_records;
        let frames_compared = baseline_frames.len().min(candidate_frames.len());
        let mut frame_comparisons = Vec::with_capacity(frames_compared);
        let mut first_divergence: Option<usize> = None;

        for i in 0..frames_compared {
            let matched = baseline_frames[i].checksum == candidate_frames[i].checksum;
            frame_comparisons.push(FrameComparison {
                index: i,
                baseline_checksum: baseline_frames[i].checksum,
                candidate_checksum: candidate_frames[i].checksum,
                matched,
            });
            if !matched && first_divergence.is_none() {
                first_divergence = Some(i);
                logger.log(
                    "shadow.frame.diverged",
                    &[
                        ("frame_idx", JsonValue::u64(i as u64)),
                        (
                            "baseline_checksum",
                            JsonValue::str(format!("{:016x}", baseline_frames[i].checksum)),
                        ),
                        (
                            "candidate_checksum",
                            JsonValue::str(format!("{:016x}", candidate_frames[i].checksum)),
                        ),
                    ],
                );
            }
        }

        // Handle frame count mismatch (also counts as divergence)
        if baseline_frames.len() != candidate_frames.len() && first_divergence.is_none() {
            first_divergence = Some(frames_compared);
        }

        let verdict = if first_divergence.is_some() {
            ShadowVerdict::Diverged
        } else {
            ShadowVerdict::Match
        };

        let diverged_count = frame_comparisons.iter().filter(|c| !c.matched).count();

        // Log verdict
        logger.log(
            "shadow.verdict",
            &[
                (
                    "verdict",
                    JsonValue::str(match verdict {
                        ShadowVerdict::Match => "match",
                        ShadowVerdict::Diverged => "diverged",
                    }),
                ),
                ("frames_compared", JsonValue::u64(frames_compared as u64)),
                ("diverged_count", JsonValue::u64(diverged_count as u64)),
                (
                    "baseline_frames",
                    JsonValue::u64(baseline_frames.len() as u64),
                ),
                (
                    "candidate_frames",
                    JsonValue::u64(candidate_frames.len() as u64),
                ),
            ],
        );

        let run_total = SHADOW_RUNS_TOTAL
            .fetch_add(1, Ordering::Relaxed)
            .saturating_add(1);

        ShadowRunResult {
            verdict,
            scenario_name: config.scenario_name,
            seed: config.seed,
            frame_comparisons,
            first_divergence,
            frames_compared,
            baseline: baseline_run.output,
            candidate: candidate_run.output,
            baseline_label: config.baseline_label,
            candidate_label: config.candidate_label,
            run_total,
        }
    }

    /// Assert that baseline and candidate produce identical frames.
    ///
    /// Convenience wrapper around [`compare`](Self::compare) that panics
    /// on divergence with a diagnostic message.
    ///
    /// # Panics
    ///
    /// Panics if any frame checksum diverges between lanes.
    pub fn assert_match<M, MF, SF>(
        config: ShadowRunConfig,
        model_factory: MF,
        scenario_fn: SF,
    ) -> ShadowRunResult
    where
        M: Model,
        MF: Fn() -> M,
        SF: Fn(&mut LabSession<M>),
    {
        let result = Self::compare(config, model_factory, scenario_fn);
        if result.verdict == ShadowVerdict::Diverged {
            let diverged = result.diverged_count();
            let first = result
                .first_divergence
                .map(|i| format!("frame {i}"))
                .unwrap_or_else(|| "frame count mismatch".to_string());
            panic!(
                "shadow-run divergence: {} of {} frames diverged, first at {}",
                diverged, result.frames_compared, first
            );
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ftui_core::event::Event;
    use ftui_render::frame::Frame;
    use ftui_runtime::program::{Cmd, Model};

    // Minimal counter model for testing.
    struct Counter {
        value: u64,
    }

    #[derive(Debug, Clone)]
    enum CounterMsg {
        Increment,
        Quit,
    }

    impl From<Event> for CounterMsg {
        fn from(e: Event) -> Self {
            match e {
                Event::Tick => CounterMsg::Increment,
                _ => CounterMsg::Quit,
            }
        }
    }

    impl Model for Counter {
        type Message = CounterMsg;

        fn update(&mut self, msg: CounterMsg) -> Cmd<CounterMsg> {
            match msg {
                CounterMsg::Increment => {
                    self.value += 1;
                    Cmd::none()
                }
                CounterMsg::Quit => Cmd::quit(),
            }
        }

        fn view(&self, frame: &mut Frame) {
            use ftui_core::geometry::Rect;
            use ftui_widgets::paragraph::Paragraph;
            let text = format!("Count: {}", self.value);
            let area = Rect::new(0, 0, frame.width(), 1);
            Paragraph::new(text).render(area, frame);
        }
    }

    // Helper trait for rendering in view
    use ftui_widgets::Widget;

    #[test]
    fn shadow_run_identical_models_match() {
        let config = ShadowRunConfig::new("test_shadow", "counter_match", 42);
        let result = ShadowRun::compare(
            config,
            || Counter { value: 0 },
            |session| {
                session.init();
                session.tick();
                session.capture_frame();
                session.tick();
                session.capture_frame();
            },
        );
        assert_eq!(result.verdict, ShadowVerdict::Match);
        assert_eq!(result.frames_compared, 2);
        assert_eq!(result.diverged_count(), 0);
        assert!((result.match_ratio() - 1.0).abs() < f64::EPSILON);
        assert!(result.first_divergence.is_none());
    }

    #[test]
    fn shadow_run_assert_match_succeeds_for_identical() {
        let config = ShadowRunConfig::new("test_assert", "counter_assert", 42);
        let result = ShadowRun::assert_match(
            config,
            || Counter { value: 0 },
            |session| {
                session.init();
                session.tick();
                session.capture_frame();
            },
        );
        assert_eq!(result.verdict, ShadowVerdict::Match);
    }

    #[test]
    fn shadow_run_config_custom_labels() {
        let config = ShadowRunConfig::new("test_labels", "label_test", 7)
            .lane_labels("threading", "asupersync");
        assert_eq!(config.baseline_label, "threading");
        assert_eq!(config.candidate_label, "asupersync");
    }

    #[test]
    fn shadow_run_config_viewport() {
        let config = ShadowRunConfig::new("test_vp", "vp_test", 0)
            .viewport(120, 40)
            .time_step_ms(8);
        assert_eq!(config.viewport_width, 120);
        assert_eq!(config.viewport_height, 40);
        assert_eq!(config.time_step_ms, 8);
    }

    #[test]
    fn shadow_runs_total_increments() {
        let before = shadow_runs_total();
        let config = ShadowRunConfig::new("test_total", "total_test", 1);
        let _ = ShadowRun::compare(
            config,
            || Counter { value: 0 },
            |session| {
                session.init();
                session.capture_frame();
            },
        );
        assert!(shadow_runs_total() > before);
    }

    #[test]
    fn lab_assert_outputs_match_succeeds_for_identical() {
        let config = ShadowRunConfig::new("test_outputs", "outputs_test", 99);
        let result = ShadowRun::compare(
            config,
            || Counter { value: 0 },
            |session| {
                session.init();
                session.tick();
                session.capture_frame();
            },
        );
        // Both outputs came from identical runs so they should match.
        crate::lab_integration::assert_outputs_match(&result.baseline, &result.candidate);
    }

    #[test]
    fn match_ratio_empty_frames() {
        let result = ShadowRunResult {
            verdict: ShadowVerdict::Match,
            scenario_name: "empty".to_string(),
            seed: 0,
            frame_comparisons: vec![],
            first_divergence: None,
            frames_compared: 0,
            baseline: LabOutput {
                frame_count: 0,
                frame_records: vec![],
                event_count: 0,
                event_log: vec![],
                tick_count: 0,
                anomaly_count: 0,
            },
            candidate: LabOutput {
                frame_count: 0,
                frame_records: vec![],
                event_count: 0,
                event_log: vec![],
                tick_count: 0,
                anomaly_count: 0,
            },
            baseline_label: "baseline".to_string(),
            candidate_label: "candidate".to_string(),
            run_total: 1,
        };
        assert!((result.match_ratio() - 1.0).abs() < f64::EPSILON);
    }
}