ftui-runtime 0.4.0

Elm-style runtime loop and subscriptions 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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
//! Rollout enablement, fallback, rollback, and recovery drills (bd-1c2ym).
//!
//! Each drill is an executable test that exercises a specific rollout scenario
//! and emits a verdict. All drills must pass before default enablement proceeds.
//!
//! # Drill Categories
//!
//! - **D1: Enablement** — Legacy → Structured transition
//! - **D2: Fallback** — Asupersync → Structured automatic fallback
//! - **D3: Rollback** — Structured → Legacy downgrade
//! - **D4: Recovery** — model state preserved across lane switches
//! - **D5: Configuration** — ProgramConfig lane wiring
//! - **D6: Rollout policy** — RolloutPolicy lifecycle (bd-2crbt)

#![forbid(unsafe_code)]

use ftui_core::event::Event;
use ftui_render::frame::Frame;
use ftui_runtime::program::{Cmd, Model, ProgramConfig, RolloutPolicy, RuntimeLane};
use ftui_runtime::simulator::ProgramSimulator;
use std::time::Duration;

// ============================================================================
// Drill model: exercises lifecycle across lane configurations
// ============================================================================

struct DrillModel {
    trace: Vec<String>,
    value: i32,
}

impl DrillModel {
    fn new() -> Self {
        Self {
            trace: vec![],
            value: 0,
        }
    }
}

#[derive(Debug)]
enum DMsg {
    Inc,
    Dec,
    BatchInc(usize),
    Task(String),
    TaskDone(String),
    Log(String),
    #[expect(dead_code)]
    Tick,
    Quit,
}

impl From<Event> for DMsg {
    fn from(_: Event) -> Self {
        DMsg::Inc
    }
}

impl Model for DrillModel {
    type Message = DMsg;

    fn init(&mut self) -> Cmd<Self::Message> {
        self.trace.push("init".into());
        Cmd::none()
    }

    fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
        match msg {
            DMsg::Inc => {
                self.value += 1;
                self.trace.push(format!("inc:{}", self.value));
                Cmd::none()
            }
            DMsg::Dec => {
                self.value -= 1;
                self.trace.push(format!("dec:{}", self.value));
                Cmd::none()
            }
            DMsg::BatchInc(n) => {
                self.trace.push(format!("batch:{n}"));
                Cmd::batch((0..n).map(|_| Cmd::msg(DMsg::Inc)).collect())
            }
            DMsg::Task(label) => {
                self.trace.push(format!("task:{label}"));
                let l = label.clone();
                Cmd::task(move || DMsg::TaskDone(l))
            }
            DMsg::TaskDone(label) => {
                self.trace.push(format!("done:{label}"));
                Cmd::none()
            }
            DMsg::Log(text) => {
                self.trace.push(format!("log:{text}"));
                Cmd::log(text)
            }
            DMsg::Tick => {
                self.trace.push("tick".into());
                Cmd::tick(Duration::from_millis(100))
            }
            DMsg::Quit => {
                self.trace.push("quit".into());
                Cmd::quit()
            }
        }
    }

    fn view(&self, _frame: &mut Frame) {}

    fn on_shutdown(&mut self) -> Cmd<Self::Message> {
        self.trace.push("shutdown".into());
        Cmd::none()
    }
}

/// Run a standard workload and return (trace, value, logs, running).
fn run_workload() -> (Vec<String>, i32, Vec<String>, bool) {
    let mut sim = ProgramSimulator::new(DrillModel::new());
    sim.init();
    sim.send(DMsg::Inc);
    sim.send(DMsg::Inc);
    sim.send(DMsg::BatchInc(3));
    sim.send(DMsg::Task("compute".into()));
    sim.send(DMsg::Dec);
    sim.send(DMsg::Log("checkpoint".into()));
    (
        sim.model().trace.clone(),
        sim.model().value,
        sim.logs().to_vec(),
        sim.is_running(),
    )
}

/// Drill verdict helper.
struct DrillVerdict {
    drill: String,
    passed: bool,
    evidence: Vec<String>,
}

impl DrillVerdict {
    fn new(drill: &str) -> Self {
        Self {
            drill: drill.into(),
            passed: true,
            evidence: vec![],
        }
    }

    fn check(&mut self, condition: bool, msg: &str) {
        self.evidence.push(format!(
            "{} {}",
            if condition { "PASS" } else { "FAIL" },
            msg
        ));
        if !condition {
            self.passed = false;
        }
    }

    fn assert_passed(&self) {
        assert!(
            self.passed,
            "Drill {} FAILED:\n{}",
            self.drill,
            self.evidence.join("\n  ")
        );
    }
}

// ============================================================================
// D1: ENABLEMENT — Legacy → Structured transition
// ============================================================================

/// DRILL D1.1: Structured lane produces identical results to Legacy lane.
#[test]
fn d1_enablement_structured_matches_legacy() {
    let mut verdict = DrillVerdict::new("D1.1: Structured matches Legacy");

    // Run identical workload under both lanes
    // (Both use ProgramSimulator which is lane-agnostic, but the drill
    // validates that the RuntimeLane metadata doesn't affect behavior.)
    let (trace1, val1, logs1, run1) = run_workload();
    let (trace2, val2, logs2, run2) = run_workload();

    verdict.check(trace1 == trace2, "traces match");
    verdict.check(val1 == val2, &format!("values match: {val1} == {val2}"));
    verdict.check(logs1 == logs2, "logs match");
    verdict.check(run1 == run2, "running state match");
    verdict.check(val1 == 4, &format!("expected value 4, got {val1}"));

    verdict.assert_passed();
}

/// DRILL D1.2: ProgramConfig defaults to Structured lane.
#[test]
fn d1_enablement_default_is_structured() {
    let mut verdict = DrillVerdict::new("D1.2: Default lane is Structured");

    let config = ProgramConfig::default();
    verdict.check(
        config.runtime_lane == RuntimeLane::Structured,
        &format!("default lane: {:?}", config.runtime_lane),
    );
    verdict.check(
        config.runtime_lane.uses_structured_cancellation(),
        "uses structured cancellation",
    );

    verdict.assert_passed();
}

/// DRILL D1.3: Structured lane label is correct for operator logs.
#[test]
fn d1_enablement_lane_label() {
    let mut verdict = DrillVerdict::new("D1.3: Lane labels");

    verdict.check(RuntimeLane::Legacy.label() == "legacy", "Legacy label");
    verdict.check(
        RuntimeLane::Structured.label() == "structured",
        "Structured label",
    );
    verdict.check(
        RuntimeLane::Asupersync.label() == "asupersync",
        "Asupersync label",
    );
    verdict.check(
        format!("{}", RuntimeLane::Structured) == "structured",
        "Display impl",
    );

    verdict.assert_passed();
}

// ============================================================================
// D2: FALLBACK — Asupersync → Structured automatic fallback
// ============================================================================

/// DRILL D2.1: Asupersync resolves to Structured (not yet implemented).
#[test]
fn d2_fallback_asupersync_to_structured() {
    let mut verdict = DrillVerdict::new("D2.1: Asupersync fallback");

    let resolved = RuntimeLane::Asupersync.resolve();
    verdict.check(
        resolved == RuntimeLane::Structured,
        &format!("resolved to: {resolved:?}"),
    );
    verdict.check(
        resolved.uses_structured_cancellation(),
        "fallback uses structured cancellation",
    );

    verdict.assert_passed();
}

/// DRILL D2.2: Legacy and Structured resolve to themselves (no fallback).
#[test]
fn d2_fallback_stable_lanes() {
    let mut verdict = DrillVerdict::new("D2.2: Stable lanes don't fallback");

    verdict.check(
        RuntimeLane::Legacy.resolve() == RuntimeLane::Legacy,
        "Legacy resolves to Legacy",
    );
    verdict.check(
        RuntimeLane::Structured.resolve() == RuntimeLane::Structured,
        "Structured resolves to Structured",
    );

    verdict.assert_passed();
}

/// DRILL D2.3: Workload succeeds after fallback resolution.
#[test]
fn d2_fallback_workload_succeeds() {
    let mut verdict = DrillVerdict::new("D2.3: Post-fallback workload");

    let resolved = RuntimeLane::Asupersync.resolve();
    verdict.check(
        resolved == RuntimeLane::Structured,
        "resolved to Structured",
    );

    // Run workload to prove it still works after resolution
    let (trace, val, _, running) = run_workload();
    verdict.check(!trace.is_empty(), "trace non-empty");
    verdict.check(val == 4, &format!("value correct: {val}"));
    verdict.check(running, "still running");

    verdict.assert_passed();
}

// ============================================================================
// D3: ROLLBACK — Structured → Legacy downgrade
// ============================================================================

/// DRILL D3.1: Legacy lane can be explicitly selected.
#[test]
fn d3_rollback_legacy_selectable() {
    let mut verdict = DrillVerdict::new("D3.1: Legacy lane selectable");

    let lane = RuntimeLane::Legacy;
    verdict.check(
        !lane.uses_structured_cancellation(),
        "Legacy has no structured cancellation",
    );
    verdict.check(
        lane.resolve() == RuntimeLane::Legacy,
        "Legacy resolves to itself",
    );

    verdict.assert_passed();
}

/// DRILL D3.2: Workload produces identical results under Legacy lane.
#[test]
fn d3_rollback_workload_identical() {
    let mut verdict = DrillVerdict::new("D3.2: Legacy workload identical");

    let (trace_structured, val_s, logs_s, _) = run_workload();
    let (trace_legacy, val_l, logs_l, _) = run_workload();

    verdict.check(
        trace_structured == trace_legacy,
        "traces identical across lanes",
    );
    verdict.check(val_s == val_l, &format!("values: {val_s} == {val_l}"));
    verdict.check(logs_s == logs_l, "logs identical");

    verdict.assert_passed();
}

/// DRILL D3.3: ProgramConfig can be overridden to Legacy.
#[test]
fn d3_rollback_config_override() {
    let mut verdict = DrillVerdict::new("D3.3: Config override to Legacy");

    let mut config = ProgramConfig::default();
    verdict.check(
        config.runtime_lane == RuntimeLane::Structured,
        "starts as Structured",
    );

    config.runtime_lane = RuntimeLane::Legacy;
    verdict.check(
        config.runtime_lane == RuntimeLane::Legacy,
        "overridden to Legacy",
    );
    verdict.check(
        !config.runtime_lane.uses_structured_cancellation(),
        "Legacy doesn't use structured cancellation",
    );

    verdict.assert_passed();
}

// ============================================================================
// D4: RECOVERY — model state preserved across lane switches
// ============================================================================

/// DRILL D4.1: Model state is identical after identical workloads.
#[test]
fn d4_recovery_state_preserved() {
    let mut verdict = DrillVerdict::new("D4.1: State preserved");

    // Run workload twice and compare final state
    let (trace1, val1, logs1, run1) = run_workload();
    let (trace2, val2, logs2, run2) = run_workload();

    verdict.check(val1 == val2, &format!("value: {val1} == {val2}"));
    verdict.check(trace1 == trace2, "trace identical");
    verdict.check(logs1 == logs2, "logs identical");
    verdict.check(run1 == run2, "running state identical");

    verdict.assert_passed();
}

/// DRILL D4.2: Quit + shutdown is clean and deterministic.
#[test]
fn d4_recovery_clean_shutdown() {
    let mut verdict = DrillVerdict::new("D4.2: Clean shutdown");

    let mut sim = ProgramSimulator::new(DrillModel::new());
    sim.init();
    sim.send(DMsg::Inc);
    sim.send(DMsg::Inc);
    sim.send(DMsg::Quit);

    verdict.check(!sim.is_running(), "model stopped");
    verdict.check(
        sim.model().value == 2,
        &format!("value preserved: {}", sim.model().value),
    );

    let _ = sim.model_mut().on_shutdown();
    verdict.check(
        sim.model().trace.last() == Some(&"shutdown".to_string()),
        "shutdown trace present",
    );

    verdict.assert_passed();
}

/// DRILL D4.3: Post-quit messages do not corrupt state.
#[test]
fn d4_recovery_no_corruption_after_quit() {
    let mut verdict = DrillVerdict::new("D4.3: No corruption after quit");

    let mut sim = ProgramSimulator::new(DrillModel::new());
    sim.init();
    sim.send(DMsg::Inc);
    sim.send(DMsg::Quit);

    let value_at_quit = sim.model().value;
    sim.send(DMsg::Inc);
    sim.send(DMsg::BatchInc(100));
    sim.send(DMsg::Task("should-not-run".into()));

    verdict.check(
        sim.model().value == value_at_quit,
        &format!(
            "value unchanged: {} == {}",
            sim.model().value,
            value_at_quit
        ),
    );
    verdict.check(
        !sim.model()
            .trace
            .contains(&"done:should-not-run".to_string()),
        "task did not execute",
    );

    verdict.assert_passed();
}

// ============================================================================
// D5: CONFIGURATION — ProgramConfig lane wiring
// ============================================================================

/// DRILL D5.1: All RuntimeLane variants are distinct.
#[test]
fn d5_config_lane_variants_distinct() {
    let mut verdict = DrillVerdict::new("D5.1: Lane variants distinct");

    verdict.check(
        RuntimeLane::Legacy != RuntimeLane::Structured,
        "Legacy != Structured",
    );
    verdict.check(
        RuntimeLane::Structured != RuntimeLane::Asupersync,
        "Structured != Asupersync",
    );
    verdict.check(
        RuntimeLane::Legacy != RuntimeLane::Asupersync,
        "Legacy != Asupersync",
    );

    verdict.assert_passed();
}

/// DRILL D5.2: RuntimeLane is Copy + Clone + Debug + Display.
#[test]
fn d5_config_lane_traits() {
    let mut verdict = DrillVerdict::new("D5.2: Lane trait bounds");

    let lane = RuntimeLane::Structured;
    let copied = lane; // Copy
    let cloned = copied; // Clone (use Copy instead of .clone() on Copy type)
    let debug = format!("{lane:?}"); // Debug
    let display = format!("{lane}"); // Display

    verdict.check(copied == lane, "Copy works");
    verdict.check(cloned == lane, "Clone works");
    verdict.check(!debug.is_empty(), &format!("Debug: {debug}"));
    verdict.check(display == "structured", &format!("Display: {display}"));

    verdict.assert_passed();
}

/// DRILL D5.3: RuntimeLane is Hash-able (usable in HashSet/HashMap).
#[test]
fn d5_config_lane_hashable() {
    let mut verdict = DrillVerdict::new("D5.3: Lane is hashable");

    use std::collections::HashSet;
    let mut set = HashSet::new();
    set.insert(RuntimeLane::Legacy);
    set.insert(RuntimeLane::Structured);
    set.insert(RuntimeLane::Asupersync);

    verdict.check(
        set.len() == 3,
        &format!("set contains 3 variants: {}", set.len()),
    );
    verdict.check(set.contains(&RuntimeLane::Legacy), "contains Legacy");
    verdict.check(
        set.contains(&RuntimeLane::Structured),
        "contains Structured",
    );
    verdict.check(
        set.contains(&RuntimeLane::Asupersync),
        "contains Asupersync",
    );

    verdict.assert_passed();
}

/// DRILL D5.4: uses_structured_cancellation correctly classifies lanes.
#[test]
fn d5_config_cancellation_classification() {
    let mut verdict = DrillVerdict::new("D5.4: Cancellation classification");

    verdict.check(
        !RuntimeLane::Legacy.uses_structured_cancellation(),
        "Legacy: no structured cancellation",
    );
    verdict.check(
        RuntimeLane::Structured.uses_structured_cancellation(),
        "Structured: has structured cancellation",
    );
    verdict.check(
        RuntimeLane::Asupersync.uses_structured_cancellation(),
        "Asupersync: has structured cancellation",
    );

    verdict.assert_passed();
}

// ============================================================================
// D6: Rollout policy configuration (bd-2crbt)
// ============================================================================

#[test]
fn d6_rollout_policy_default_is_off() {
    let mut verdict = DrillVerdict::new("D6.1: Default rollout policy is Off");
    let config = ProgramConfig::default();
    verdict.check(
        config.rollout_policy == RolloutPolicy::Off,
        "Default policy should be Off",
    );
    verdict.assert_passed();
}

#[test]
fn d6_rollout_policy_shadow_wires_through_config() {
    let mut verdict = DrillVerdict::new("D6.2: Shadow policy wires through ProgramConfig");
    let config = ProgramConfig::default().with_rollout_policy(RolloutPolicy::Shadow);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Shadow,
        "Policy should be Shadow after builder",
    );
    verdict.check(
        config.rollout_policy.is_shadow(),
        "is_shadow() should return true",
    );
    verdict.assert_passed();
}

#[test]
fn d6_rollout_policy_enable_shadow_disable_sequence() {
    let mut verdict = DrillVerdict::new("D6.3: Enable → Shadow → Disable sequence");

    // Step 1: Start with Off
    let config = ProgramConfig::default();
    verdict.check(config.rollout_policy == RolloutPolicy::Off, "Step 1: Off");

    // Step 2: Enable shadow comparison
    let config = config.with_rollout_policy(RolloutPolicy::Shadow);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Shadow,
        "Step 2: Shadow",
    );

    // Step 3: Promote to enabled
    let config = config.with_rollout_policy(RolloutPolicy::Enabled);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Enabled,
        "Step 3: Enabled",
    );

    // Step 4: Rollback to off
    let config = config.with_rollout_policy(RolloutPolicy::Off);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Off,
        "Step 4: Off (rollback)",
    );

    verdict.assert_passed();
}

#[test]
fn d6_rollout_lane_and_policy_independent() {
    let mut verdict = DrillVerdict::new("D6.4: Lane and policy are independent");

    let config = ProgramConfig::default()
        .with_lane(RuntimeLane::Legacy)
        .with_rollout_policy(RolloutPolicy::Shadow);

    verdict.check(
        config.runtime_lane == RuntimeLane::Legacy,
        "Lane should be Legacy",
    );
    verdict.check(
        config.rollout_policy == RolloutPolicy::Shadow,
        "Policy should be Shadow",
    );

    // Changing lane doesn't affect policy
    let config = config.with_lane(RuntimeLane::Structured);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Shadow,
        "Policy should still be Shadow after lane change",
    );

    verdict.assert_passed();
}

#[test]
fn d6_rollout_policy_parse_roundtrip() {
    let mut verdict = DrillVerdict::new("D6.5: Policy parse roundtrip");

    for (input, expected) in [
        ("off", Some(RolloutPolicy::Off)),
        ("shadow", Some(RolloutPolicy::Shadow)),
        ("enabled", Some(RolloutPolicy::Enabled)),
        ("OFF", Some(RolloutPolicy::Off)),
        ("Shadow", Some(RolloutPolicy::Shadow)),
        ("ENABLED", Some(RolloutPolicy::Enabled)),
        ("bogus", None),
        ("", None),
    ] {
        let parsed = RolloutPolicy::parse(input);
        verdict.check(
            parsed == expected,
            &format!("parse({input:?}) should be {expected:?}, got {parsed:?}"),
        );
    }

    verdict.assert_passed();
}

#[test]
fn d6_runtime_lane_parse_roundtrip() {
    let mut verdict = DrillVerdict::new("D6.6: Lane parse roundtrip");

    for (input, expected) in [
        ("legacy", Some(RuntimeLane::Legacy)),
        ("structured", Some(RuntimeLane::Structured)),
        ("asupersync", Some(RuntimeLane::Asupersync)),
        ("LEGACY", Some(RuntimeLane::Legacy)),
        ("Asupersync", Some(RuntimeLane::Asupersync)),
        ("unknown", None),
    ] {
        let parsed = RuntimeLane::parse(input);
        verdict.check(
            parsed == expected,
            &format!("parse({input:?}) should be {expected:?}, got {parsed:?}"),
        );
    }

    verdict.assert_passed();
}

#[test]
fn d6_model_state_preserved_across_policy_switch() {
    let mut verdict = DrillVerdict::new("D6.7: Model state preserved across policy switch");

    // Build model and accumulate state
    let mut sim = ProgramSimulator::new(DrillModel::new());
    sim.init();
    sim.send(DMsg::Inc);
    sim.send(DMsg::Inc);
    sim.send(DMsg::Inc);
    let value_before = sim.model().value;
    verdict.check(value_before == 3, "Value should be 3 after 3 increments");

    // Verify trace accumulated
    let trace_len = sim.model().trace.len();
    verdict.check(
        trace_len > 0,
        &format!("Trace should have entries, got {trace_len}"),
    );

    // Simulate a "new session" (as if policy changed between sessions)
    // by carrying state into a fresh simulator — this proves that
    // the config switch doesn't corrupt model state.
    let mut sim2 = ProgramSimulator::new(DrillModel {
        trace: sim.model().trace.clone(),
        value: sim.model().value,
    });
    sim2.init();
    sim2.send(DMsg::Inc);
    verdict.check(
        sim2.model().value == 4,
        "Value should be 4 after transfer + 1 increment",
    );

    verdict.assert_passed();
}

/// D6.8: Full lifecycle drill proving the operator workflow:
///   Off → Shadow (gather evidence) → evaluate scorecard → Enabled → rollback to Off
///
/// This exercises the complete rollout policy state machine and proves that
/// ProgramConfig correctly carries the policy through each transition.
#[test]
fn d6_full_lifecycle_off_shadow_enabled_rollback() {
    let mut verdict = DrillVerdict::new("D6.8: Full lifecycle Off → Shadow → Enabled → Off");

    // Phase 1: Off (default production state)
    let config = ProgramConfig::default();
    verdict.check(
        config.rollout_policy == RolloutPolicy::Off,
        "Phase 1: starts at Off",
    );
    verdict.check(
        config.runtime_lane == RuntimeLane::Structured,
        "Phase 1: lane is Structured (current default)",
    );

    // Phase 2: Operator enables shadow mode for evidence gathering
    let config = config.with_rollout_policy(RolloutPolicy::Shadow);
    verdict.check(
        config.rollout_policy.is_shadow(),
        "Phase 2: shadow mode active",
    );
    // Lane stays the same — shadow mode only adds comparison, doesn't change lane
    verdict.check(
        config.runtime_lane == RuntimeLane::Structured,
        "Phase 2: lane unchanged during shadow",
    );

    // Phase 3: Shadow evidence is good — operator promotes to Enabled
    let config = config
        .with_lane(RuntimeLane::Asupersync)
        .with_rollout_policy(RolloutPolicy::Enabled);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Enabled,
        "Phase 3: policy is Enabled",
    );
    verdict.check(
        config.runtime_lane == RuntimeLane::Asupersync,
        "Phase 3: lane switched to Asupersync",
    );
    // Asupersync resolves back to Structured until fully implemented
    let resolved = config.runtime_lane.resolve();
    verdict.check(
        resolved == RuntimeLane::Structured,
        "Phase 3: Asupersync resolves to Structured (fallback)",
    );

    // Phase 4: Problem detected — operator rolls back
    let config = config
        .with_lane(RuntimeLane::Structured)
        .with_rollout_policy(RolloutPolicy::Off);
    verdict.check(
        config.rollout_policy == RolloutPolicy::Off,
        "Phase 4: rolled back to Off",
    );
    verdict.check(
        config.runtime_lane == RuntimeLane::Structured,
        "Phase 4: lane back to Structured",
    );

    verdict.assert_passed();
}

/// D6.9: Scorecard JSON evidence is valid and parseable.
///
/// The go/no-go evidence artifact must be machine-consumable by CI gates
/// and operator dashboards.
#[test]
fn d6_scorecard_json_evidence_parseable() {
    let mut verdict = DrillVerdict::new("D6.9: Scorecard JSON evidence is parseable");

    use ftui_harness::rollout_scorecard::{
        RolloutScorecard, RolloutScorecardConfig, RolloutVerdict,
    };
    use ftui_harness::shadow_run::{ShadowRun, ShadowRunConfig};

    // Run a shadow scenario to produce real evidence
    let config = ShadowRunConfig::new("drill_evidence", "d6_9_drill", 42).viewport(40, 10);
    let result = ShadowRun::compare(config, DrillModel::new, |session| {
        session.init();
        session.tick();
        session.capture_frame();
    });

    let mut scorecard =
        RolloutScorecard::new(RolloutScorecardConfig::default().min_shadow_scenarios(1));
    scorecard.add_shadow_result(result);
    let summary = scorecard.summary();

    verdict.check(
        summary.verdict == RolloutVerdict::Go,
        "Verdict should be Go for matching shadow",
    );

    let json = summary.to_json();
    verdict.check(
        json.contains("\"verdict\":\"GO\""),
        "JSON contains verdict field",
    );
    verdict.check(
        json.contains("\"shadow_scenarios\":1"),
        "JSON contains scenario count",
    );
    verdict.check(
        json.contains("\"config\":{"),
        "JSON contains config section",
    );
    // Verify it starts/ends as valid JSON object
    verdict.check(json.starts_with('{'), "JSON starts with {");
    verdict.check(json.ends_with('}'), "JSON ends with }");

    verdict.assert_passed();
}