ftui-harness 0.3.1

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
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
#![forbid(unsafe_code)]

//! Capability Simulator E2E Test Suite (bd-k4lj.6)
//!
//! End-to-end validation of terminal capability simulation for testing.
//! Tests profile accuracy, override stacking, degradation behavior,
//! quirk simulation, and cross-profile integration.
//!
//! # Invariants
//!
//! | ID       | Invariant                                              |
//! |----------|--------------------------------------------------------|
//! | PROF-1   | Profile capabilities match specification exactly        |
//! | PROF-2   | Profile identity is preserved through from_profile()    |
//! | OVER-1   | Override stacking applies inner over outer correctly    |
//! | OVER-2   | Override cleanup is guaranteed (RAII)                   |
//! | OVER-3   | Thread-local isolation between concurrent tests         |
//! | DEG-1    | Mux-aware policies degrade features conservatively      |
//! | DEG-2    | Color fallback follows depth ordering                   |
//! | QUIRK-1  | Multiplexer quirks disable passthrough-unsafe features  |
//! | INT-1    | Profile matrix produces deterministic snapshots         |
//! | INT-2    | Cross-profile diffs are consistent                      |
//!
//! # Running
//!
//! ```sh
//! cargo test -p ftui-harness capability_sim_
//! ```
//!
//! # JSONL Logging
//!
//! ```sh
//! CAP_SIM_LOG=1 cargo test -p ftui-harness capability_sim_
//! ```

use ftui_core::capability_override::{
    CapabilityOverride, clear_all_overrides, has_active_overrides, override_depth, push_override,
    with_capability_override,
};
use ftui_core::terminal_capabilities::{TerminalCapabilities, TerminalProfile};
use std::io::Write;

// ============================================================================
// JSONL Logger
// ============================================================================

struct CapSimLogger {
    writer: Option<Box<dyn Write>>,
    run_id: String,
}

impl CapSimLogger {
    fn new(case_name: &str) -> Self {
        let writer = if std::env::var("CAP_SIM_LOG").is_ok() {
            let dir = std::env::temp_dir().join("ftui_cap_sim_e2e");
            let _ = std::fs::create_dir_all(&dir);
            let path = dir.join(format!("{case_name}.jsonl"));
            std::fs::File::create(path)
                .ok()
                .map(|f| Box::new(f) as Box<dyn Write>)
        } else {
            None
        };
        Self {
            writer,
            run_id: format!(
                "{}-{}",
                case_name,
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis()
            ),
        }
    }

    fn log_event(&mut self, event: &str, data: &str) {
        if let Some(ref mut w) = self.writer {
            let ts = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis();
            let _ = writeln!(
                w,
                r#"{{"run_id":"{}","event":"{}","ts_ms":{},"data":{}}}"#,
                self.run_id, event, ts, data
            );
        }
    }

    fn log_invariant(&mut self, id: &str, passed: bool, detail: &str) {
        self.log_event(
            "invariant",
            &format!(
                r#"{{"id":"{}","passed":{},"detail":"{}"}}"#,
                id, passed, detail
            ),
        );
    }

    fn log_profile(&mut self, profile: &str, caps_summary: &str) {
        self.log_event(
            "profile",
            &format!(
                r#"{{"profile":"{}","capabilities":"{}"}}"#,
                profile, caps_summary
            ),
        );
    }

    fn log_complete(&mut self, passed: bool, checks: usize) {
        self.log_event(
            "complete",
            &format!(r#"{{"passed":{},"total_checks":{}}}"#, passed, checks),
        );
    }
}

// ============================================================================
// Helpers
// ============================================================================

/// Summarize capabilities as a compact string for logging.
fn caps_summary(caps: &TerminalCapabilities) -> String {
    format!(
        "tc={},256={},sync={},hyper={},scroll={},mux={},kitty={},focus={},paste={},mouse={},clip={}",
        caps.true_color as u8,
        caps.colors_256 as u8,
        caps.sync_output as u8,
        caps.osc8_hyperlinks as u8,
        caps.scroll_region as u8,
        caps.in_any_mux() as u8,
        caps.kitty_keyboard as u8,
        caps.focus_events as u8,
        caps.bracketed_paste as u8,
        caps.mouse_sgr as u8,
        caps.osc52_clipboard as u8,
    )
}

/// All profiles that have predefined capability sets.
const ALL_PROFILES: [TerminalProfile; 11] = [
    TerminalProfile::Modern,
    TerminalProfile::Xterm256Color,
    TerminalProfile::Xterm,
    TerminalProfile::Vt100,
    TerminalProfile::Dumb,
    TerminalProfile::Screen,
    TerminalProfile::Tmux,
    TerminalProfile::Zellij,
    TerminalProfile::WindowsConsole,
    TerminalProfile::Kitty,
    TerminalProfile::LinuxConsole,
];

// ============================================================================
// PROF-1: Profile Accuracy
// ============================================================================

#[test]
fn capability_sim_profile_modern() {
    let mut logger = CapSimLogger::new("profile_modern");
    let caps = TerminalCapabilities::modern();

    assert!(caps.true_color, "PROF-1: modern should have true color");
    assert!(caps.colors_256, "PROF-1: modern should have 256 colors");
    assert!(caps.sync_output, "PROF-1: modern should have sync output");
    assert!(
        caps.osc8_hyperlinks,
        "PROF-1: modern should have hyperlinks"
    );
    assert!(
        caps.scroll_region,
        "PROF-1: modern should have scroll region"
    );
    assert!(!caps.in_any_mux(), "PROF-1: modern should not be in mux");
    assert!(
        caps.kitty_keyboard,
        "PROF-1: modern should have kitty keyboard"
    );
    assert!(caps.focus_events, "PROF-1: modern should have focus events");
    assert!(
        caps.bracketed_paste,
        "PROF-1: modern should have bracketed paste"
    );
    assert!(caps.mouse_sgr, "PROF-1: modern should have mouse sgr");
    assert!(caps.osc52_clipboard, "PROF-1: modern should have clipboard");

    logger.log_profile("modern", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "modern_all_features");
    logger.log_complete(true, 11);
}

#[test]
fn capability_sim_profile_xterm_256color() {
    let mut logger = CapSimLogger::new("profile_xterm256");
    let caps = TerminalCapabilities::xterm_256color();

    assert!(!caps.true_color, "PROF-1: xterm256 no true color");
    assert!(caps.colors_256, "PROF-1: xterm256 has 256 colors");
    assert!(!caps.sync_output, "PROF-1: xterm256 no sync output");
    assert!(!caps.osc8_hyperlinks, "PROF-1: xterm256 no hyperlinks");
    assert!(caps.scroll_region, "PROF-1: xterm256 has scroll region");
    assert!(!caps.kitty_keyboard, "PROF-1: xterm256 no kitty keyboard");
    assert!(caps.bracketed_paste, "PROF-1: xterm256 has bracketed paste");
    assert!(caps.mouse_sgr, "PROF-1: xterm256 has mouse sgr");

    logger.log_profile("xterm-256color", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "xterm256_capabilities");
    logger.log_complete(true, 8);
}

#[test]
fn capability_sim_profile_xterm_16() {
    let mut logger = CapSimLogger::new("profile_xterm16");
    let caps = TerminalCapabilities::xterm();

    assert!(!caps.true_color, "PROF-1: xterm no true color");
    assert!(!caps.colors_256, "PROF-1: xterm no 256 colors");
    assert!(caps.scroll_region, "PROF-1: xterm has scroll region");
    assert!(caps.bracketed_paste, "PROF-1: xterm has bracketed paste");
    assert!(caps.mouse_sgr, "PROF-1: xterm has mouse sgr");
    assert!(!caps.kitty_keyboard, "PROF-1: xterm no kitty keyboard");
    assert!(!caps.focus_events, "PROF-1: xterm no focus events");

    logger.log_profile("xterm", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "xterm16_capabilities");
    logger.log_complete(true, 7);
}

#[test]
fn capability_sim_profile_vt100() {
    let mut logger = CapSimLogger::new("profile_vt100");
    let caps = TerminalCapabilities::vt100();

    assert!(!caps.true_color, "PROF-1: vt100 no true color");
    assert!(!caps.colors_256, "PROF-1: vt100 no 256 colors");
    assert!(!caps.has_color(), "PROF-1: vt100 has no color at all");
    assert!(caps.scroll_region, "PROF-1: vt100 has scroll region");
    assert!(!caps.bracketed_paste, "PROF-1: vt100 no bracketed paste");
    assert!(!caps.mouse_sgr, "PROF-1: vt100 no mouse");

    logger.log_profile("vt100", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "vt100_minimal");
    logger.log_complete(true, 6);
}

#[test]
fn capability_sim_profile_dumb() {
    let mut logger = CapSimLogger::new("profile_dumb");
    let caps = TerminalCapabilities::dumb();

    // Dumb terminal: absolutely nothing
    assert!(!caps.true_color);
    assert!(!caps.colors_256);
    assert!(!caps.sync_output);
    assert!(!caps.osc8_hyperlinks);
    assert!(!caps.scroll_region);
    assert!(!caps.kitty_keyboard);
    assert!(!caps.focus_events);
    assert!(!caps.bracketed_paste);
    assert!(!caps.mouse_sgr);
    assert!(!caps.osc52_clipboard);
    assert!(!caps.in_any_mux());
    assert!(!caps.has_color());
    assert_eq!(caps.color_depth(), "mono");

    logger.log_profile("dumb", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "dumb_nothing_enabled");
    logger.log_complete(true, 13);
}

#[test]
fn capability_sim_profile_kitty() {
    let mut logger = CapSimLogger::new("profile_kitty");
    let caps = TerminalCapabilities::kitty();

    // Kitty should match Modern's features
    assert!(caps.true_color);
    assert!(caps.colors_256);
    assert!(caps.sync_output);
    assert!(caps.osc8_hyperlinks);
    assert!(caps.kitty_keyboard);
    assert!(caps.focus_events);
    assert!(caps.osc52_clipboard);
    assert_eq!(caps.profile(), TerminalProfile::Kitty);

    logger.log_profile("kitty", &caps_summary(&caps));
    logger.log_invariant("PROF-1", true, "kitty_full_features");
    logger.log_complete(true, 8);
}

// ============================================================================
// PROF-2: Profile Identity Preservation
// ============================================================================

#[test]
fn capability_sim_profile_identity_roundtrip() {
    let mut logger = CapSimLogger::new("profile_identity");

    for profile in &ALL_PROFILES {
        let caps = TerminalCapabilities::from_profile(*profile);
        assert_eq!(
            caps.profile(),
            *profile,
            "PROF-2: from_profile({profile:?}) should preserve profile identity"
        );
        logger.log_invariant("PROF-2", true, &format!("{profile:?}"));
    }

    logger.log_complete(true, ALL_PROFILES.len());
}

#[test]
fn capability_sim_profile_from_profile_deterministic() {
    let mut logger = CapSimLogger::new("profile_deterministic");

    for profile in &ALL_PROFILES {
        let caps1 = TerminalCapabilities::from_profile(*profile);
        let caps2 = TerminalCapabilities::from_profile(*profile);
        assert_eq!(
            caps1, caps2,
            "PROF-2: from_profile({profile:?}) should be deterministic"
        );
    }

    logger.log_invariant("PROF-2", true, "deterministic_all_profiles");
    logger.log_complete(true, ALL_PROFILES.len());
}

// ============================================================================
// DEG-2: Color Depth Ordering
// ============================================================================

#[test]
fn capability_sim_color_depth_ordering() {
    let mut logger = CapSimLogger::new("color_depth_ordering");

    // Color depth should follow: mono < 256 < truecolor
    let dumb = TerminalCapabilities::dumb();
    let xterm256 = TerminalCapabilities::xterm_256color();
    let modern = TerminalCapabilities::modern();

    assert_eq!(dumb.color_depth(), "mono");
    assert_eq!(xterm256.color_depth(), "256");
    assert_eq!(modern.color_depth(), "truecolor");

    // has_color() should be false for mono
    assert!(!dumb.has_color());
    assert!(xterm256.has_color());
    assert!(modern.has_color());

    logger.log_invariant("DEG-2", true, "color_depth_ordering");
    logger.log_complete(true, 6);
}

#[test]
fn capability_sim_color_depth_all_profiles() {
    let mut logger = CapSimLogger::new("color_depth_profiles");

    for profile in &ALL_PROFILES {
        let caps = TerminalCapabilities::from_profile(*profile);
        let depth = caps.color_depth();

        // Verify depth is valid
        assert!(
            ["mono", "256", "truecolor"].contains(&depth),
            "PROF-1: {profile:?} has invalid color_depth: {depth}"
        );

        // Verify depth consistency with flags
        match depth {
            "truecolor" => assert!(
                caps.true_color,
                "{profile:?}: truecolor but true_color=false"
            ),
            "256" => {
                assert!(!caps.true_color, "{profile:?}: 256 but true_color=true");
                assert!(caps.colors_256, "{profile:?}: 256 but colors_256=false");
            }
            "mono" => assert!(!caps.has_color(), "{profile:?}: mono but has_color()=true"),
            _ => unreachable!(),
        }

        logger.log_profile(&format!("{profile:?}"), &format!("depth={depth}"));
    }

    logger.log_invariant("DEG-2", true, "color_depth_consistency");
    logger.log_complete(true, ALL_PROFILES.len());
}

// ============================================================================
// OVER-1: Override Stacking
// ============================================================================

#[test]
fn capability_sim_override_apply_single() {
    let mut logger = CapSimLogger::new("override_single");

    let base = TerminalCapabilities::modern();
    let override_cfg = CapabilityOverride::new().true_color(Some(false));
    let result = override_cfg.apply_to(base);

    assert!(
        !result.true_color,
        "OVER-1: override should disable true color"
    );
    // Other fields should remain from base
    assert!(result.colors_256, "OVER-1: unoverridden field preserved");
    assert!(result.sync_output, "OVER-1: unoverridden field preserved");

    logger.log_invariant("OVER-1", true, "single_override");
    logger.log_complete(true, 3);
}

#[test]
fn capability_sim_override_stacking_precedence() {
    let mut logger = CapSimLogger::new("override_stacking");

    let base = TerminalCapabilities::dumb();

    // First override: enable true color
    let over1 = CapabilityOverride::new().true_color(Some(true));
    let after1 = over1.apply_to(base);
    assert!(
        after1.true_color,
        "OVER-1: first override enables true color"
    );

    // Second override: disable it again
    let over2 = CapabilityOverride::new().true_color(Some(false));
    let after2 = over2.apply_to(after1);
    assert!(
        !after2.true_color,
        "OVER-1: second override should win (inner over outer)"
    );

    // Third override: None (no change)
    let over3 = CapabilityOverride::new(); // all None
    let after3 = over3.apply_to(after2);
    assert!(
        !after3.true_color,
        "OVER-1: None override preserves previous value"
    );

    logger.log_invariant("OVER-1", true, "stacking_precedence");
    logger.log_complete(true, 3);
}

#[test]
fn capability_sim_override_dumb_disables_all() {
    let mut logger = CapSimLogger::new("override_dumb");

    let base = TerminalCapabilities::modern();
    let dumb_override = CapabilityOverride::dumb();
    let result = dumb_override.apply_to(base);

    assert!(!result.true_color);
    assert!(!result.colors_256);
    assert!(!result.sync_output);
    assert!(!result.osc8_hyperlinks);
    assert!(!result.scroll_region);
    assert!(!result.kitty_keyboard);
    assert!(!result.focus_events);
    assert!(!result.bracketed_paste);
    assert!(!result.mouse_sgr);
    assert!(!result.osc52_clipboard);
    assert!(!result.in_tmux);
    assert!(!result.in_screen);
    assert!(!result.in_zellij);

    logger.log_invariant("OVER-1", true, "dumb_disables_all");
    logger.log_complete(true, 13);
}

#[test]
fn capability_sim_override_modern_enables_all() {
    let mut logger = CapSimLogger::new("override_modern");

    let base = TerminalCapabilities::dumb();
    let modern_override = CapabilityOverride::modern();
    let result = modern_override.apply_to(base);

    assert!(result.true_color);
    assert!(result.colors_256);
    assert!(result.sync_output);
    assert!(result.osc8_hyperlinks);
    assert!(result.scroll_region);
    assert!(result.kitty_keyboard);
    assert!(result.focus_events);
    assert!(result.bracketed_paste);
    assert!(result.mouse_sgr);
    assert!(result.osc52_clipboard);
    // Modern override explicitly sets mux to false
    assert!(!result.in_tmux);
    assert!(!result.in_screen);
    assert!(!result.in_zellij);

    logger.log_invariant("OVER-1", true, "modern_enables_all");
    logger.log_complete(true, 13);
}

#[test]
fn capability_sim_override_tmux_simulation() {
    let mut logger = CapSimLogger::new("override_tmux");

    let base = TerminalCapabilities::modern();
    let tmux_override = CapabilityOverride::tmux();
    let result = tmux_override.apply_to(base);

    assert!(result.in_tmux, "OVER-1: tmux override enables in_tmux");
    assert!(!result.sync_output, "OVER-1: tmux disables sync output");
    assert!(!result.osc8_hyperlinks, "OVER-1: tmux disables hyperlinks");
    assert!(
        !result.kitty_keyboard,
        "OVER-1: tmux disables kitty keyboard"
    );
    assert!(result.colors_256, "OVER-1: tmux enables 256 colors");
    assert!(result.bracketed_paste, "OVER-1: tmux keeps bracketed paste");
    // true_color is None in tmux override, so inherits from base (modern = true)
    assert!(
        result.true_color,
        "OVER-1: tmux inherits true_color from base"
    );

    logger.log_invariant("OVER-1", true, "tmux_simulation");
    logger.log_complete(true, 7);
}

#[test]
fn capability_sim_override_empty_is_identity() {
    let mut logger = CapSimLogger::new("override_identity");

    let empty = CapabilityOverride::new();
    assert!(empty.is_empty(), "Empty override should report is_empty()");

    for profile in &ALL_PROFILES {
        let base = TerminalCapabilities::from_profile(*profile);
        let result = empty.apply_to(base);
        assert_eq!(
            base, result,
            "OVER-1: empty override should be identity for {profile:?}"
        );
    }

    logger.log_invariant("OVER-1", true, "empty_identity");
    logger.log_complete(true, ALL_PROFILES.len());
}

// ============================================================================
// OVER-2: Override RAII Cleanup
// ============================================================================

#[test]
fn capability_sim_override_guard_cleanup() {
    let mut logger = CapSimLogger::new("guard_cleanup");

    // Ensure clean state
    clear_all_overrides();
    assert!(!has_active_overrides(), "Should start clean");
    assert_eq!(override_depth(), 0);

    {
        let _guard = push_override(CapabilityOverride::dumb());
        assert!(
            has_active_overrides(),
            "OVER-2: guard should activate override"
        );
        assert_eq!(override_depth(), 1, "OVER-2: depth should be 1");
    }
    // Guard dropped

    assert!(
        !has_active_overrides(),
        "OVER-2: override should be cleaned up after guard drop"
    );
    assert_eq!(override_depth(), 0, "OVER-2: depth should return to 0");

    logger.log_invariant("OVER-2", true, "guard_cleanup");
    logger.log_complete(true, 4);
}

#[test]
fn capability_sim_override_nested_guards() {
    let mut logger = CapSimLogger::new("nested_guards");

    clear_all_overrides();

    {
        let _g1 = push_override(CapabilityOverride::new().true_color(Some(true)));
        assert_eq!(override_depth(), 1);

        {
            let _g2 = push_override(CapabilityOverride::new().true_color(Some(false)));
            assert_eq!(override_depth(), 2);

            {
                let _g3 = push_override(CapabilityOverride::new().colors_256(Some(true)));
                assert_eq!(override_depth(), 3, "OVER-2: three nested overrides");
            }
            assert_eq!(override_depth(), 2, "OVER-2: back to 2 after inner drop");
        }
        assert_eq!(override_depth(), 1, "OVER-2: back to 1");
    }
    assert_eq!(override_depth(), 0, "OVER-2: fully cleaned up");

    logger.log_invariant("OVER-2", true, "nested_guards");
    logger.log_complete(true, 4);
}

#[test]
fn capability_sim_override_with_closure() {
    let mut logger = CapSimLogger::new("with_closure");

    clear_all_overrides();

    let result = with_capability_override(CapabilityOverride::dumb(), || {
        assert!(has_active_overrides(), "OVER-2: active inside closure");
        assert_eq!(override_depth(), 1);
        42
    });

    assert_eq!(result, 42, "Closure should return value");
    assert!(!has_active_overrides(), "OVER-2: cleaned up after closure");

    logger.log_invariant("OVER-2", true, "with_closure");
    logger.log_complete(true, 3);
}

#[test]
fn capability_sim_override_clear_all() {
    let mut logger = CapSimLogger::new("clear_all");

    let _g1 = push_override(CapabilityOverride::dumb());
    let _g2 = push_override(CapabilityOverride::modern());
    assert_eq!(override_depth(), 2);

    clear_all_overrides();
    assert_eq!(override_depth(), 0, "OVER-2: clear_all removes everything");
    assert!(!has_active_overrides());

    // Guards dropping after clear should not panic
    drop(_g2);
    drop(_g1);

    logger.log_invariant("OVER-2", true, "clear_all_safe");
    logger.log_complete(true, 3);
}

// ============================================================================
// DEG-1: Mux-Aware Degradation Policies
// ============================================================================

#[test]
fn capability_sim_mux_sync_output_disabled() {
    let mut logger = CapSimLogger::new("mux_sync_output");

    // All multiplexers should disable sync output via use_sync_output()
    let mux_profiles = [
        TerminalProfile::Tmux,
        TerminalProfile::Screen,
        TerminalProfile::Zellij,
    ];

    for profile in &mux_profiles {
        let caps = TerminalCapabilities::from_profile(*profile);
        assert!(
            !caps.use_sync_output(),
            "DEG-1: {profile:?} should disable sync output via policy"
        );
        logger.log_invariant("DEG-1", true, &format!("{profile:?}_no_sync"));
    }

    // Non-mux profiles with sync_output=true should allow it
    let modern = TerminalCapabilities::modern();
    assert!(
        modern.use_sync_output(),
        "DEG-1: modern (non-mux) should allow sync output"
    );

    logger.log_complete(true, mux_profiles.len() + 1);
}

#[test]
fn capability_sim_mux_scroll_region_policy() {
    let mut logger = CapSimLogger::new("mux_scroll_region");

    let mux_profiles = [
        TerminalProfile::Tmux,
        TerminalProfile::Screen,
        TerminalProfile::Zellij,
    ];

    for profile in &mux_profiles {
        let caps = TerminalCapabilities::from_profile(*profile);
        // Mux profiles should disable scroll region via use_scroll_region()
        assert!(
            !caps.use_scroll_region(),
            "DEG-1: {profile:?} should disable scroll region via policy"
        );
    }

    // Modern non-mux should allow scroll region
    let modern = TerminalCapabilities::modern();
    assert!(
        modern.use_scroll_region(),
        "DEG-1: modern should allow scroll region"
    );

    logger.log_invariant("DEG-1", true, "mux_scroll_region_policy");
    logger.log_complete(true, mux_profiles.len() + 1);
}

#[test]
fn capability_sim_mux_detection_flags() {
    let mut logger = CapSimLogger::new("mux_detection");

    let cases = [
        (TerminalProfile::Tmux, true, false, false),
        (TerminalProfile::Screen, false, true, false),
        (TerminalProfile::Zellij, false, false, true),
        (TerminalProfile::Modern, false, false, false),
        (TerminalProfile::Dumb, false, false, false),
    ];

    for (profile, tmux, screen, zellij) in &cases {
        let caps = TerminalCapabilities::from_profile(*profile);
        assert_eq!(caps.in_tmux, *tmux, "QUIRK-1: {profile:?} in_tmux");
        assert_eq!(caps.in_screen, *screen, "QUIRK-1: {profile:?} in_screen");
        assert_eq!(caps.in_zellij, *zellij, "QUIRK-1: {profile:?} in_zellij");
        assert_eq!(
            caps.in_any_mux(),
            *tmux || *screen || *zellij || caps.in_wezterm_mux,
            "QUIRK-1: {profile:?} in_any_mux"
        );
        logger.log_invariant("QUIRK-1", true, &format!("{profile:?}_mux_flags"));
    }

    logger.log_complete(true, cases.len());
}

// ============================================================================
// QUIRK-1: Multiplexer Quirk Simulation
// ============================================================================

#[test]
fn capability_sim_tmux_disables_advanced_features() {
    let mut logger = CapSimLogger::new("tmux_quirks");

    let caps = TerminalCapabilities::tmux();

    // tmux limitations
    assert!(
        !caps.sync_output,
        "QUIRK-1: tmux no sync (passthrough unreliable)"
    );
    assert!(!caps.osc8_hyperlinks, "QUIRK-1: tmux no hyperlinks");
    assert!(!caps.kitty_keyboard, "QUIRK-1: tmux no kitty keyboard");
    assert!(!caps.focus_events, "QUIRK-1: tmux no focus events");
    assert!(!caps.osc52_clipboard, "QUIRK-1: tmux no clipboard");
    assert!(!caps.true_color, "QUIRK-1: tmux no true color by default");

    // tmux does support some features
    assert!(caps.colors_256, "QUIRK-1: tmux has 256 colors");
    assert!(caps.scroll_region, "QUIRK-1: tmux has scroll region (raw)");
    assert!(caps.bracketed_paste, "QUIRK-1: tmux has bracketed paste");
    assert!(caps.mouse_sgr, "QUIRK-1: tmux has mouse sgr");

    logger.log_invariant("QUIRK-1", true, "tmux_quirks_verified");
    logger.log_complete(true, 10);
}

#[test]
fn capability_sim_screen_quirks() {
    let mut logger = CapSimLogger::new("screen_quirks");

    let caps = TerminalCapabilities::screen();

    assert!(caps.in_screen);
    assert!(!caps.sync_output, "QUIRK-1: screen no sync output");
    assert!(!caps.osc8_hyperlinks, "QUIRK-1: screen no hyperlinks");
    assert!(!caps.true_color, "QUIRK-1: screen no true color");
    assert!(caps.colors_256, "QUIRK-1: screen has 256 colors");

    logger.log_invariant("QUIRK-1", true, "screen_quirks");
    logger.log_complete(true, 5);
}

#[test]
fn capability_sim_zellij_quirks() {
    let mut logger = CapSimLogger::new("zellij_quirks");

    let caps = TerminalCapabilities::zellij();

    // Zellij is more capable than tmux/screen
    assert!(caps.in_zellij);
    assert!(caps.true_color, "QUIRK-1: zellij supports true color");
    assert!(caps.focus_events, "QUIRK-1: zellij supports focus events");
    assert!(!caps.sync_output, "QUIRK-1: zellij still no sync output");
    assert!(!caps.osc8_hyperlinks, "QUIRK-1: zellij no hyperlinks");
    assert!(!caps.kitty_keyboard, "QUIRK-1: zellij no kitty keyboard");

    logger.log_invariant("QUIRK-1", true, "zellij_better_than_tmux");
    logger.log_complete(true, 6);
}

#[test]
fn capability_sim_windows_console_quirks() {
    let mut logger = CapSimLogger::new("windows_quirks");

    let caps = TerminalCapabilities::windows_console();

    assert!(caps.true_color, "QUIRK-1: windows has true color");
    assert!(caps.osc8_hyperlinks, "QUIRK-1: windows has hyperlinks");
    assert!(caps.focus_events, "QUIRK-1: windows has focus events");
    assert!(!caps.sync_output, "QUIRK-1: windows no sync output");
    assert!(!caps.kitty_keyboard, "QUIRK-1: windows no kitty keyboard");

    logger.log_invariant("QUIRK-1", true, "windows_console_quirks");
    logger.log_complete(true, 5);
}

#[test]
fn capability_sim_linux_console_quirks() {
    let mut logger = CapSimLogger::new("linux_console_quirks");

    let caps = TerminalCapabilities::linux_console();

    assert!(!caps.true_color, "QUIRK-1: linux console no true color");
    assert!(!caps.colors_256, "QUIRK-1: linux console no 256 colors");
    assert!(!caps.has_color(), "QUIRK-1: linux console no color");
    assert!(
        caps.scroll_region,
        "QUIRK-1: linux console has scroll region"
    );
    assert!(caps.bracketed_paste, "QUIRK-1: linux console has paste");
    assert!(caps.mouse_sgr, "QUIRK-1: linux console has mouse");

    logger.log_invariant("QUIRK-1", true, "linux_console_no_color");
    logger.log_complete(true, 6);
}

// ============================================================================
// INT-1: Profile Matrix Integration
// ============================================================================

#[test]
fn capability_sim_profile_matrix_deterministic() {
    let mut logger = CapSimLogger::new("matrix_deterministic");

    // Generate capability snapshots for all profiles twice
    let snapshots1: Vec<(TerminalProfile, String)> = ALL_PROFILES
        .iter()
        .map(|p| (*p, caps_summary(&TerminalCapabilities::from_profile(*p))))
        .collect();

    let snapshots2: Vec<(TerminalProfile, String)> = ALL_PROFILES
        .iter()
        .map(|p| (*p, caps_summary(&TerminalCapabilities::from_profile(*p))))
        .collect();

    assert_eq!(
        snapshots1, snapshots2,
        "INT-1: profile matrix should be deterministic"
    );

    logger.log_invariant("INT-1", true, "matrix_deterministic");
    logger.log_complete(true, 1);
}

#[test]
fn capability_sim_profile_matrix_uniqueness() {
    let mut logger = CapSimLogger::new("matrix_uniqueness");

    // Most profiles should produce distinct capability sets
    // (some might overlap, but their profile identifiers differ)
    let caps_list: Vec<(TerminalProfile, TerminalCapabilities)> = ALL_PROFILES
        .iter()
        .map(|p| (*p, TerminalCapabilities::from_profile(*p)))
        .collect();

    // Profile identifiers must be unique
    for i in 0..caps_list.len() {
        for j in (i + 1)..caps_list.len() {
            assert_ne!(
                caps_list[i].0, caps_list[j].0,
                "INT-1: profile identifiers should be unique"
            );
        }
    }

    logger.log_invariant("INT-1", true, "profile_uniqueness");
    logger.log_complete(true, 1);
}

// ============================================================================
// INT-2: Cross-Profile Capability Comparison
// ============================================================================

#[test]
fn capability_sim_cross_profile_feature_hierarchy() {
    let mut logger = CapSimLogger::new("feature_hierarchy");

    let dumb = TerminalCapabilities::dumb();
    let vt100 = TerminalCapabilities::vt100();
    let xterm = TerminalCapabilities::xterm();
    let xterm256 = TerminalCapabilities::xterm_256color();
    let modern = TerminalCapabilities::modern();

    // Feature count should generally increase up the hierarchy
    let dumb_features = count_features(&dumb);
    let vt100_features = count_features(&vt100);
    let xterm_features = count_features(&xterm);
    let xterm256_features = count_features(&xterm256);
    let modern_features = count_features(&modern);

    assert!(
        dumb_features <= vt100_features,
        "INT-2: dumb ({dumb_features}) <= vt100 ({vt100_features})"
    );
    assert!(
        vt100_features <= xterm_features,
        "INT-2: vt100 ({vt100_features}) <= xterm ({xterm_features})"
    );
    assert!(
        xterm_features <= xterm256_features,
        "INT-2: xterm ({xterm_features}) <= xterm256 ({xterm256_features})"
    );
    assert!(
        xterm256_features <= modern_features,
        "INT-2: xterm256 ({xterm256_features}) <= modern ({modern_features})"
    );

    logger.log_invariant("INT-2", true, "feature_hierarchy");
    logger.log_complete(true, 4);
}

fn count_features(caps: &TerminalCapabilities) -> u32 {
    let mut count = 0u32;
    if caps.true_color {
        count += 1;
    }
    if caps.colors_256 {
        count += 1;
    }
    if caps.sync_output {
        count += 1;
    }
    if caps.osc8_hyperlinks {
        count += 1;
    }
    if caps.scroll_region {
        count += 1;
    }
    if caps.kitty_keyboard {
        count += 1;
    }
    if caps.focus_events {
        count += 1;
    }
    if caps.bracketed_paste {
        count += 1;
    }
    if caps.mouse_sgr {
        count += 1;
    }
    if caps.osc52_clipboard {
        count += 1;
    }
    count
}

#[test]
fn capability_sim_mux_feature_subset() {
    let mut logger = CapSimLogger::new("mux_feature_subset");

    // Mux profiles should have fewer effective features than modern
    let modern = TerminalCapabilities::modern();
    let modern_effective = count_effective_features(&modern);

    let mux_profiles = [
        TerminalProfile::Tmux,
        TerminalProfile::Screen,
        TerminalProfile::Zellij,
    ];

    for profile in &mux_profiles {
        let caps = TerminalCapabilities::from_profile(*profile);
        let effective = count_effective_features(&caps);
        assert!(
            effective <= modern_effective,
            "INT-2: {profile:?} effective features ({effective}) should <= modern ({modern_effective})"
        );
        logger.log_invariant("INT-2", true, &format!("{profile:?}_effective={effective}"));
    }

    logger.log_complete(true, mux_profiles.len());
}

fn count_effective_features(caps: &TerminalCapabilities) -> u32 {
    let mut count = 0u32;
    if caps.true_color {
        count += 1;
    }
    if caps.colors_256 {
        count += 1;
    }
    if caps.use_sync_output() {
        count += 1;
    }
    if caps.osc8_hyperlinks {
        count += 1;
    }
    if caps.use_scroll_region() {
        count += 1;
    }
    if caps.kitty_keyboard {
        count += 1;
    }
    if caps.focus_events {
        count += 1;
    }
    if caps.bracketed_paste {
        count += 1;
    }
    if caps.mouse_sgr {
        count += 1;
    }
    if caps.osc52_clipboard {
        count += 1;
    }
    count
}

// ============================================================================
// Builder Pattern Tests
// ============================================================================

#[test]
fn capability_sim_builder_custom_profile() {
    let mut logger = CapSimLogger::new("builder_custom");

    let caps = TerminalCapabilities::builder()
        .colors_256(true)
        .true_color(true)
        .mouse_sgr(true)
        .bracketed_paste(true)
        .build();

    assert!(caps.true_color);
    assert!(caps.colors_256);
    assert!(caps.mouse_sgr);
    assert!(caps.bracketed_paste);
    // Everything else should be default (false)
    assert!(!caps.sync_output);
    assert!(!caps.osc8_hyperlinks);
    assert!(!caps.kitty_keyboard);
    assert_eq!(caps.profile(), TerminalProfile::Custom);

    logger.log_invariant("PROF-1", true, "builder_custom");
    logger.log_complete(true, 8);
}

// ============================================================================
// Override + Profile Composition
// ============================================================================

#[test]
fn capability_sim_override_on_each_profile() {
    let mut logger = CapSimLogger::new("override_all_profiles");

    // Apply a selective override on each base profile
    let override_cfg = CapabilityOverride::new()
        .true_color(Some(true))
        .mouse_sgr(Some(false));

    for profile in &ALL_PROFILES {
        let base = TerminalCapabilities::from_profile(*profile);
        let result = override_cfg.apply_to(base);

        assert!(
            result.true_color,
            "Override should enable true_color on {profile:?}"
        );
        assert!(
            !result.mouse_sgr,
            "Override should disable mouse_sgr on {profile:?}"
        );

        // Non-overridden fields should match base
        assert_eq!(
            result.sync_output, base.sync_output,
            "Non-overridden sync_output should match base for {profile:?}"
        );
        assert_eq!(
            result.colors_256, base.colors_256,
            "Non-overridden colors_256 should match base for {profile:?}"
        );

        logger.log_invariant("OVER-1", true, &format!("override_on_{profile:?}"));
    }

    logger.log_complete(true, ALL_PROFILES.len());
}

// ============================================================================
// Mux Override Integration
// ============================================================================

#[test]
fn capability_sim_mux_override_chain() {
    let mut logger = CapSimLogger::new("mux_override_chain");

    // Modern base -> tmux override -> verify policies
    let base = TerminalCapabilities::modern();
    let tmux_over = CapabilityOverride::tmux();
    let result = tmux_over.apply_to(base);

    // Should be in tmux
    assert!(result.in_tmux);

    // Policies should gate features
    assert!(!result.use_sync_output(), "DEG-1: mux disables sync policy");
    assert!(
        !result.use_scroll_region(),
        "DEG-1: mux disables scroll policy"
    );

    // But the raw flags might differ from policy
    // (scroll_region raw = true from tmux override, but policy = false)
    assert!(result.scroll_region, "Raw scroll_region is true");

    logger.log_invariant("DEG-1", true, "mux_override_chain");
    logger.log_complete(true, 4);
}

// ============================================================================
// Suite Summary
// ============================================================================

#[test]
fn capability_sim_suite_summary() {
    let mut logger = CapSimLogger::new("suite_summary");

    let mut total_checks = 0u32;

    // Verify all profiles can be instantiated
    for profile in &ALL_PROFILES {
        let caps = TerminalCapabilities::from_profile(*profile);
        let _summary = caps_summary(&caps);
        total_checks += 1;
    }

    // Verify override round-trip on all profiles
    for profile in &ALL_PROFILES {
        let base = TerminalCapabilities::from_profile(*profile);
        let result = CapabilityOverride::new().apply_to(base);
        assert_eq!(base, result, "Empty override is identity for {profile:?}");
        total_checks += 1;
    }

    // Verify dumb -> modern -> dumb round-trip
    let base = TerminalCapabilities::dumb();
    let upgraded = CapabilityOverride::modern().apply_to(base);
    let downgraded = CapabilityOverride::dumb().apply_to(upgraded);
    // downgraded should match dumb except profile field
    assert!(!downgraded.true_color);
    assert!(!downgraded.has_color());
    total_checks += 2;

    logger.log_event(
        "summary",
        &format!(
            r#"{{"profiles_tested":{},"total_checks":{}}}"#,
            ALL_PROFILES.len(),
            total_checks
        ),
    );
    logger.log_complete(true, total_checks as usize);
}