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
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
//! # Sync Bracket Flicker-Free Completeness Proof (bd-1q5.5)
//!
//! Formal verification that DEC 2026 sync bracket usage guarantees no partial
//! frames are displayed. Covers three completeness properties:
//!
//! 1. **Pairing**: Every `sync_begin` (\x1b[?2026h) is paired with exactly one
//!    `sync_end` (\x1b[?2026l), and vice versa.
//!
//! 2. **Enclosure**: No escape sequences leak outside sync brackets when
//!    sync_output is enabled. All ANSI content is emitted within the bracket.
//!
//! 3. **Fallback**: When sync brackets are unsupported, the presenter falls
//!    back to cursor-hiding (DECTCEM `?25l` / `?25h`) to reduce visual flicker,
//!    and no sync sequences appear in the output.
//!
//! Additionally verifies:
//! - InlineRenderer bracket pairing and cleanup
//! - Multi-frame consistency: every frame individually satisfies pairing
//! - State machine exhaustive enumeration of bracket transitions
//! - Determinism: identical inputs always produce identical bracket placement

use ftui_core::terminal_capabilities::TerminalCapabilities;
use ftui_harness::flicker_detection::{analyze_stream, assert_flicker_free};
use ftui_render::ansi;
use ftui_render::buffer::Buffer;
use ftui_render::cell::{Cell, PackedRgba};
use ftui_render::diff::BufferDiff;
use ftui_render::presenter::Presenter;

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

fn caps_sync() -> TerminalCapabilities {
    let mut caps = TerminalCapabilities::basic();
    caps.sync_output = true;
    caps
}

fn caps_no_sync() -> TerminalCapabilities {
    let mut caps = TerminalCapabilities::basic();
    caps.sync_output = false;
    caps
}

fn present_frame(buffer: &Buffer, old: &Buffer, caps: TerminalCapabilities) -> Vec<u8> {
    let diff = BufferDiff::compute(old, buffer);
    let mut sink = Vec::new();
    let mut presenter = Presenter::new(&mut sink, caps);
    presenter.present(buffer, &diff).unwrap();
    drop(presenter);
    sink
}

fn count_occurrences(haystack: &[u8], needle: &[u8]) -> usize {
    haystack
        .windows(needle.len())
        .filter(|w| *w == needle)
        .count()
}

fn find_all_positions(haystack: &[u8], needle: &[u8]) -> Vec<usize> {
    haystack
        .windows(needle.len())
        .enumerate()
        .filter(|(_, w)| *w == needle)
        .map(|(i, _)| i)
        .collect()
}

/// Simple LCG for deterministic test data.
struct Lcg(u64);
impl Lcg {
    fn new(seed: u64) -> Self {
        Self(seed)
    }
    fn next_u64(&mut self) -> u64 {
        self.0 = self
            .0
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1);
        self.0
    }
    fn next_u16(&mut self, max: u16) -> u16 {
        (self.next_u64() >> 16) as u16 % max
    }
    fn next_char(&mut self) -> char {
        char::from_u32('A' as u32 + (self.next_u64() % 26) as u32).unwrap()
    }
}

fn random_buffer(width: u16, height: u16, seed: u64, fill_fraction: f64) -> Buffer {
    let mut buf = Buffer::new(width, height);
    let mut rng = Lcg::new(seed);
    let total = (width as usize) * (height as usize);
    let fill_count = (total as f64 * fill_fraction) as usize;
    for _ in 0..fill_count {
        let x = rng.next_u16(width);
        let y = rng.next_u16(height);
        let ch = rng.next_char();
        let fg = PackedRgba::rgb(
            (rng.next_u64() % 256) as u8,
            (rng.next_u64() % 256) as u8,
            (rng.next_u64() % 256) as u8,
        );
        buf.set_raw(x, y, Cell::from_char(ch).with_fg(fg));
    }
    buf
}

// =============================================================================
// Property 1: Sync Bracket Pairing
// =============================================================================

/// Every frame emits exactly one sync_begin and one sync_end when sync is on.
#[test]
fn pairing_single_frame_exact_count() {
    let buf = random_buffer(80, 24, 0xAA01_0001, 0.5);
    let old = Buffer::new(80, 24);
    let output = present_frame(&buf, &old, caps_sync());

    assert_eq!(
        count_occurrences(&output, ansi::SYNC_BEGIN),
        1,
        "exactly one sync_begin per frame"
    );
    assert_eq!(
        count_occurrences(&output, ansi::SYNC_END),
        1,
        "exactly one sync_end per frame"
    );
}

/// sync_begin always precedes sync_end in the byte stream.
#[test]
fn pairing_begin_before_end() {
    let buf = random_buffer(120, 40, 0xAA01_0002, 0.8);
    let old = Buffer::new(120, 40);
    let output = present_frame(&buf, &old, caps_sync());

    let begin_pos = find_all_positions(&output, ansi::SYNC_BEGIN);
    let end_pos = find_all_positions(&output, ansi::SYNC_END);
    assert_eq!(begin_pos.len(), 1);
    assert_eq!(end_pos.len(), 1);
    assert!(
        begin_pos[0] < end_pos[0],
        "sync_begin ({}) must precede sync_end ({})",
        begin_pos[0],
        end_pos[0]
    );
}

/// Empty diff still emits balanced sync brackets.
#[test]
fn pairing_empty_diff_balanced() {
    let buf = Buffer::new(40, 10);
    let output = present_frame(&buf, &buf, caps_sync());

    assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
    assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);
    assert_flicker_free(&output);
}

/// Multi-frame sequence: each frame independently has balanced brackets.
#[test]
fn pairing_multi_frame_each_balanced() {
    let caps = caps_sync();
    let mut rng = Lcg::new(0xAA01_0003);
    let mut prev = Buffer::new(80, 24);

    for frame_id in 0..20 {
        let mut current = prev.clone();
        let n = rng.next_u64() as usize % 100;
        for _ in 0..n {
            let x = rng.next_u16(80);
            let y = rng.next_u16(24);
            current.set_raw(x, y, Cell::from_char(rng.next_char()));
        }

        let output = present_frame(&current, &prev, caps);

        assert_eq!(
            count_occurrences(&output, ansi::SYNC_BEGIN),
            1,
            "frame {}: expected 1 sync_begin",
            frame_id
        );
        assert_eq!(
            count_occurrences(&output, ansi::SYNC_END),
            1,
            "frame {}: expected 1 sync_end",
            frame_id
        );

        let positions_begin = find_all_positions(&output, ansi::SYNC_BEGIN);
        let positions_end = find_all_positions(&output, ansi::SYNC_END);
        assert!(
            positions_begin[0] < positions_end[0],
            "frame {}: begin must precede end",
            frame_id
        );

        prev = current;
    }
}

/// Large buffer (200x60) with full-screen content has balanced brackets.
#[test]
fn pairing_large_buffer_balanced() {
    let buf = random_buffer(200, 60, 0xAA01_B161, 1.0);
    let old = Buffer::new(200, 60);
    let output = present_frame(&buf, &old, caps_sync());

    assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
    assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);
    assert_flicker_free(&output);
}

/// Single-cell buffer has balanced brackets.
#[test]
fn pairing_single_cell_balanced() {
    let mut buf = Buffer::new(1, 1);
    buf.set_raw(0, 0, Cell::from_char('X'));
    let old = Buffer::new(1, 1);
    let output = present_frame(&buf, &old, caps_sync());

    assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
    assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);
}

// =============================================================================
// Property 2: No Escape Leakage Outside Sync Brackets
// =============================================================================

/// All escape sequences (CSI, OSC, ESC) appear between sync_begin and sync_end.
#[test]
fn enclosure_no_escapes_outside_brackets() {
    let buf = random_buffer(80, 24, 0xBB01_0001, 0.6);
    let old = Buffer::new(80, 24);
    let output = present_frame(&buf, &old, caps_sync());

    let begin_pos = output
        .windows(ansi::SYNC_BEGIN.len())
        .position(|w| w == ansi::SYNC_BEGIN)
        .expect("sync_begin missing");
    let end_pos = output
        .windows(ansi::SYNC_END.len())
        .rposition(|w| w == ansi::SYNC_END)
        .expect("sync_end missing");

    // Check bytes before sync_begin: should be empty
    let before = &output[..begin_pos];
    assert!(
        before.is_empty(),
        "no bytes should appear before sync_begin, found {} bytes",
        before.len()
    );

    // Check bytes after sync_end: should be empty
    let after = &output[end_pos + ansi::SYNC_END.len()..];
    assert!(
        after.is_empty(),
        "no bytes should appear after sync_end, found {} bytes",
        after.len()
    );
}

/// Content bytes (printable characters) are enclosed within sync brackets.
#[test]
fn enclosure_content_within_brackets() {
    let mut buf = Buffer::new(10, 1);
    for x in 0..10 {
        buf.set_raw(
            x,
            0,
            Cell::from_char(char::from_u32('A' as u32 + x as u32).unwrap()),
        );
    }
    let old = Buffer::new(10, 1);
    let output = present_frame(&buf, &old, caps_sync());

    let begin_pos = output
        .windows(ansi::SYNC_BEGIN.len())
        .position(|w| w == ansi::SYNC_BEGIN)
        .unwrap();
    let end_pos = output
        .windows(ansi::SYNC_END.len())
        .rposition(|w| w == ansi::SYNC_END)
        .unwrap();

    // Find all printable content bytes
    let content_start = begin_pos + ansi::SYNC_BEGIN.len();
    for (i, &b) in output.iter().enumerate() {
        if b.is_ascii_uppercase() {
            assert!(
                i > begin_pos && i < end_pos,
                "content byte '{}' at position {} is outside sync brackets ({}, {})",
                b as char,
                i,
                begin_pos,
                end_pos
            );
        }
    }

    // Verify all characters A-J appear within the bracketed region
    let inner = &output[content_start..end_pos];
    for ch in b'A'..=b'J' {
        assert!(
            inner.contains(&ch),
            "character '{}' should appear within sync brackets",
            ch as char
        );
    }
}

/// SGR reset sequence is enclosed within sync brackets.
#[test]
fn enclosure_sgr_reset_within_brackets() {
    let mut buf = Buffer::new(5, 1);
    buf.set_raw(
        0,
        0,
        Cell::from_char('X').with_fg(PackedRgba::rgb(255, 0, 0)),
    );
    let old = Buffer::new(5, 1);
    let output = present_frame(&buf, &old, caps_sync());

    let begin_pos = output
        .windows(ansi::SYNC_BEGIN.len())
        .position(|w| w == ansi::SYNC_BEGIN)
        .unwrap();
    let end_pos = output
        .windows(ansi::SYNC_END.len())
        .rposition(|w| w == ansi::SYNC_END)
        .unwrap();

    // SGR reset (\x1b[0m) must be between brackets
    let sgr_pos = output
        .windows(b"\x1b[0m".len())
        .rposition(|w| w == b"\x1b[0m")
        .expect("SGR reset missing");

    assert!(
        sgr_pos > begin_pos && sgr_pos < end_pos,
        "SGR reset at {} must be within brackets ({}, {})",
        sgr_pos,
        begin_pos,
        end_pos
    );
}

/// Cursor positioning sequences are enclosed within brackets.
#[test]
fn enclosure_cursor_positioning_within_brackets() {
    // Sparse changes force cursor positioning sequences
    let mut buf = Buffer::new(80, 24);
    buf.set_raw(0, 0, Cell::from_char('A'));
    buf.set_raw(79, 23, Cell::from_char('Z'));
    let old = Buffer::new(80, 24);
    let output = present_frame(&buf, &old, caps_sync());

    let begin_pos = output
        .windows(ansi::SYNC_BEGIN.len())
        .position(|w| w == ansi::SYNC_BEGIN)
        .unwrap();
    let end_pos = output
        .windows(ansi::SYNC_END.len())
        .rposition(|w| w == ansi::SYNC_END)
        .unwrap();

    // Find all ESC bytes (0x1b) — every one must be within brackets
    for (i, &b) in output.iter().enumerate() {
        if b == 0x1b {
            assert!(
                i >= begin_pos && i <= end_pos,
                "ESC at position {} is outside brackets ({}, {})",
                i,
                begin_pos,
                end_pos
            );
        }
    }
}

// =============================================================================
// Property 3: Fallback Behavior (No Sync Support)
// =============================================================================

/// Without sync support, cursor hide/show is used instead.
#[test]
fn fallback_cursor_hide_show_used() {
    let mut buf = Buffer::new(40, 10);
    buf.set_raw(5, 3, Cell::from_char('X'));
    let old = Buffer::new(40, 10);
    let output = present_frame(&buf, &old, caps_no_sync());

    // No sync sequences present
    assert_eq!(
        count_occurrences(&output, ansi::SYNC_BEGIN),
        0,
        "sync_begin should not appear"
    );
    assert_eq!(
        count_occurrences(&output, ansi::SYNC_END),
        0,
        "sync_end should not appear"
    );

    // Cursor hide/show used as fallback
    assert!(
        output.starts_with(ansi::CURSOR_HIDE),
        "fallback should start with cursor hide"
    );
    assert!(
        output.ends_with(ansi::CURSOR_SHOW),
        "fallback should end with cursor show"
    );
}

/// Fallback cursor brackets are balanced: exactly one hide and one show.
#[test]
fn fallback_cursor_brackets_balanced() {
    let buf = random_buffer(80, 24, 0xCC01_0001, 0.5);
    let old = Buffer::new(80, 24);
    let output = present_frame(&buf, &old, caps_no_sync());

    assert_eq!(
        count_occurrences(&output, ansi::CURSOR_HIDE),
        1,
        "exactly one cursor_hide"
    );
    assert_eq!(
        count_occurrences(&output, ansi::CURSOR_SHOW),
        1,
        "exactly one cursor_show"
    );
}

/// Fallback: cursor_hide precedes cursor_show.
#[test]
fn fallback_hide_before_show() {
    let buf = random_buffer(60, 20, 0xCC01_0002, 0.7);
    let old = Buffer::new(60, 20);
    let output = present_frame(&buf, &old, caps_no_sync());

    let hide_pos = find_all_positions(&output, ansi::CURSOR_HIDE);
    let show_pos = find_all_positions(&output, ansi::CURSOR_SHOW);
    assert_eq!(hide_pos.len(), 1);
    assert_eq!(show_pos.len(), 1);
    assert!(
        hide_pos[0] < show_pos[0],
        "cursor_hide must precede cursor_show"
    );
}

/// Fallback: all content is between cursor hide and show.
#[test]
fn fallback_content_within_cursor_brackets() {
    let mut buf = Buffer::new(10, 1);
    for x in 0..10 {
        buf.set_raw(x, 0, Cell::from_char('A'));
    }
    let old = Buffer::new(10, 1);
    let output = present_frame(&buf, &old, caps_no_sync());

    let hide_pos = output
        .windows(ansi::CURSOR_HIDE.len())
        .position(|w| w == ansi::CURSOR_HIDE)
        .unwrap();
    let show_pos = output
        .windows(ansi::CURSOR_SHOW.len())
        .rposition(|w| w == ansi::CURSOR_SHOW)
        .unwrap();

    // Content bytes should be between cursor brackets
    for (i, &b) in output.iter().enumerate() {
        if b == b'A' {
            assert!(
                i > hide_pos && i < show_pos,
                "content byte at {} outside cursor brackets ({}, {})",
                i,
                hide_pos,
                show_pos
            );
        }
    }
}

/// Fallback: empty diff still produces balanced cursor brackets.
#[test]
fn fallback_empty_diff_balanced() {
    let buf = Buffer::new(20, 5);
    let output = present_frame(&buf, &buf, caps_no_sync());

    assert_eq!(count_occurrences(&output, ansi::CURSOR_HIDE), 1);
    assert_eq!(count_occurrences(&output, ansi::CURSOR_SHOW), 1);
    assert!(output.starts_with(ansi::CURSOR_HIDE));
    assert!(output.ends_with(ansi::CURSOR_SHOW));
}

/// Flicker detector correctly flags fallback output as having sync gaps.
#[test]
fn fallback_detected_as_sync_gap() {
    let mut buf = Buffer::new(40, 10);
    buf.set_raw(5, 3, Cell::from_char('X'));
    let old = Buffer::new(40, 10);
    let output = present_frame(&buf, &old, caps_no_sync());

    let analysis = analyze_stream(&output);
    assert!(
        !analysis.stats.is_flicker_free(),
        "fallback output should not be reported as flicker-free by detector"
    );
    assert!(
        analysis.stats.sync_gaps > 0,
        "detector should report sync gaps for fallback output"
    );
}

// =============================================================================
// Multi-frame Consistency
// =============================================================================

/// Multi-frame: each frame independently has balanced brackets via detector.
#[test]
fn multi_frame_detector_validates_each() {
    let caps = caps_sync();
    let mut rng = Lcg::new(0xDD01_0001);
    let mut prev = Buffer::new(80, 24);

    for frame_id in 0..50 {
        let mut current = prev.clone();
        let n = rng.next_u64() as usize % 80;
        for _ in 0..n {
            let x = rng.next_u16(80);
            let y = rng.next_u16(24);
            current.set_raw(x, y, Cell::from_char(rng.next_char()));
        }

        let output = present_frame(&current, &prev, caps);
        let analysis = analyze_stream(&output);

        assert!(
            analysis.stats.is_flicker_free(),
            "frame {}: flicker detected — gaps={}, clears={}, complete={}/{}",
            frame_id,
            analysis.stats.sync_gaps,
            analysis.stats.partial_clears,
            analysis.stats.complete_frames,
            analysis.stats.total_frames,
        );

        prev = current;
    }
}

/// Concatenated frames: detector sees N balanced bracket pairs.
#[test]
fn multi_frame_concatenated_stream_balanced() {
    let caps = caps_sync();
    let mut rng = Lcg::new(0xDD01_0002);
    let mut prev = Buffer::new(60, 20);
    let mut all_output = Vec::new();

    let num_frames = 50;
    for _ in 0..num_frames {
        let mut current = prev.clone();
        let n = rng.next_u64() as usize % 40;
        for _ in 0..n {
            let x = rng.next_u16(60);
            let y = rng.next_u16(20);
            current.set_raw(x, y, Cell::from_char(rng.next_char()));
        }

        let output = present_frame(&current, &prev, caps);
        all_output.extend_from_slice(&output);
        prev = current;
    }

    // Total sync bracket counts should equal frame count
    assert_eq!(count_occurrences(&all_output, ansi::SYNC_BEGIN), num_frames);
    assert_eq!(count_occurrences(&all_output, ansi::SYNC_END), num_frames);

    // Every begin should have a corresponding end before the next begin
    let begins = find_all_positions(&all_output, ansi::SYNC_BEGIN);
    let ends = find_all_positions(&all_output, ansi::SYNC_END);
    for (i, (&b, &e)) in begins.iter().zip(ends.iter()).enumerate() {
        assert!(b < e, "frame {}: begin ({}) must precede end ({})", i, b, e);
        if i + 1 < begins.len() {
            assert!(
                e < begins[i + 1],
                "frame {}: end ({}) must precede next begin ({})",
                i,
                e,
                begins[i + 1]
            );
        }
    }
}

// =============================================================================
// State Machine Exhaustive Enumeration
// =============================================================================

/// Exhaustive: test all terminal capability combinations for bracket behavior.
#[test]
fn state_machine_all_capability_combos() {
    let mut buf = Buffer::new(20, 5);
    buf.set_raw(0, 0, Cell::from_char('X'));
    let old = Buffer::new(20, 5);

    // sync=true: must have sync brackets, no cursor fallback
    {
        let output = present_frame(&buf, &old, caps_sync());
        assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
        assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);
        // No cursor hide/show (sync brackets handle flicker prevention)
        assert_eq!(count_occurrences(&output, ansi::CURSOR_HIDE), 0);
        assert_eq!(count_occurrences(&output, ansi::CURSOR_SHOW), 0);
    }

    // sync=false: no sync brackets, must have cursor fallback
    {
        let output = present_frame(&buf, &old, caps_no_sync());
        assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 0);
        assert_eq!(count_occurrences(&output, ansi::SYNC_END), 0);
        assert_eq!(count_occurrences(&output, ansi::CURSOR_HIDE), 1);
        assert_eq!(count_occurrences(&output, ansi::CURSOR_SHOW), 1);
    }

    // sync=true, in_tmux: capabilities.use_sync_output() returns false
    // but Presenter uses .sync_output directly — runtime must honor policy
    {
        let mut caps = TerminalCapabilities::basic();
        caps.sync_output = true;
        caps.in_tmux = true;
        assert!(
            !caps.use_sync_output(),
            "policy should disable sync in tmux"
        );
    }

    // sync=true, in_screen
    {
        let mut caps = TerminalCapabilities::basic();
        caps.sync_output = true;
        caps.in_screen = true;
        assert!(
            !caps.use_sync_output(),
            "policy should disable sync in screen"
        );
    }

    // sync=true, in_zellij
    {
        let mut caps = TerminalCapabilities::basic();
        caps.sync_output = true;
        caps.in_zellij = true;
        assert!(
            !caps.use_sync_output(),
            "policy should disable sync in zellij"
        );
    }
}

/// Exhaustive: all known sync-supporting terminals produce flicker-free output.
#[test]
fn state_machine_known_sync_terminals() {
    let sync_terminals = ["WezTerm", "Alacritty", "Ghostty", "kitty", "Contour"];

    for term in &sync_terminals {
        let mut caps = TerminalCapabilities::basic();
        caps.sync_output = true;
        let mut buf = Buffer::new(10, 3);
        buf.set_raw(0, 0, Cell::from_char('T'));
        let old = Buffer::new(10, 3);
        let output = present_frame(&buf, &old, caps);

        assert_eq!(
            count_occurrences(&output, ansi::SYNC_BEGIN),
            1,
            "{}: should have sync brackets",
            term
        );
        assert_flicker_free(&output);
    }
}

// =============================================================================
// InlineRenderer Bracket Safety
// =============================================================================

/// InlineRenderer emits balanced sync brackets via present_ui.
#[test]
fn inline_renderer_sync_brackets_balanced() {
    use ftui_core::inline_mode::{InlineConfig, InlineRenderer};
    use std::io::{Cursor, Write};

    let mut config = InlineConfig::new(6, 24, 80);
    config.use_sync_output = true;
    let writer = Cursor::new(Vec::new());
    let mut renderer = InlineRenderer::new(writer, config);

    renderer
        .present_ui(|w, _config| {
            w.write_all(b"Hello, world!")?;
            Ok(())
        })
        .unwrap();

    // present_ui another frame to test multi-frame balance
    renderer
        .present_ui(|w, _config| {
            w.write_all(b"Frame 2")?;
            Ok(())
        })
        .unwrap();

    // We can't get inner from InlineRenderer, so test is just
    // that present_ui doesn't panic and completes successfully.
    // The underlying sync bracket balance is verified by the
    // inline_mode unit tests in ftui-core.
}

/// InlineRenderer without sync completes without panic.
#[test]
fn inline_renderer_no_sync_no_panic() {
    use ftui_core::inline_mode::{InlineConfig, InlineRenderer};
    use std::io::{Cursor, Write};

    let config = InlineConfig::new(6, 24, 80);
    let writer = Cursor::new(Vec::new());
    let mut renderer = InlineRenderer::new(writer, config);

    renderer
        .present_ui(|w, _config| {
            w.write_all(b"Hello")?;
            Ok(())
        })
        .unwrap();
}

// =============================================================================
// Determinism
// =============================================================================

/// Same inputs always produce identical bracket placement.
#[test]
fn determinism_bracket_positions_stable() {
    let buf = random_buffer(80, 24, 0xEE01_0001, 0.5);
    let old = Buffer::new(80, 24);

    let output1 = present_frame(&buf, &old, caps_sync());
    let output2 = present_frame(&buf, &old, caps_sync());

    assert_eq!(
        output1, output2,
        "identical inputs must produce identical output including bracket positions"
    );

    let pos1 = find_all_positions(&output1, ansi::SYNC_BEGIN);
    let pos2 = find_all_positions(&output2, ansi::SYNC_BEGIN);
    assert_eq!(pos1, pos2, "bracket positions must be deterministic");
}

/// Determinism across 100 seeds.
#[test]
fn determinism_100_seeds() {
    for seed in 0..100u64 {
        let buf = random_buffer(40, 12, seed, 0.3);
        let old = Buffer::new(40, 12);

        let a = present_frame(&buf, &old, caps_sync());
        let b = present_frame(&buf, &old, caps_sync());
        assert_eq!(a, b, "seed {}: output not deterministic", seed);
    }
}

// =============================================================================
// Adversarial: Interleaved Content Types
// =============================================================================

/// Mixed styles + colors + hyperlinks: brackets still balanced and enclosing.
#[test]
fn adversarial_mixed_content_brackets_sound() {
    use ftui_render::cell::{CellAttrs, StyleFlags};
    use ftui_render::link_registry::LinkRegistry;

    let mut buf = Buffer::new(30, 5);
    let mut links = LinkRegistry::new();
    let link_id = links.register("https://example.com");

    // Plain text
    buf.set_raw(0, 0, Cell::from_char('P'));
    // Styled text
    buf.set_raw(
        1,
        0,
        Cell::from_char('S')
            .with_fg(PackedRgba::rgb(255, 0, 0))
            .with_bg(PackedRgba::rgb(0, 0, 255)),
    );
    // Bold
    let mut bold = Cell::from_char('B');
    bold.attrs = CellAttrs::new(StyleFlags::BOLD, 0);
    buf.set_raw(2, 0, bold);
    // Linked
    buf.set_raw(
        3,
        0,
        Cell::from_char('L').with_attrs(CellAttrs::new(StyleFlags::empty(), link_id)),
    );

    let old = Buffer::new(30, 5);
    let diff = BufferDiff::compute(&old, &buf);

    let mut sink = Vec::new();
    let caps = caps_sync();
    let mut presenter = Presenter::new(&mut sink, caps);
    presenter
        .present_with_pool(&buf, &diff, None, Some(&links))
        .unwrap();
    drop(presenter);

    assert_eq!(count_occurrences(&sink, ansi::SYNC_BEGIN), 1);
    assert_eq!(count_occurrences(&sink, ansi::SYNC_END), 1);
    assert_flicker_free(&sink);
}

/// Unicode content (wide chars, combining marks) doesn't affect bracket pairing.
#[test]
fn adversarial_unicode_brackets_sound() {
    let mut buf = Buffer::new(20, 3);
    // Wide character (CJK)
    buf.set_raw(0, 0, Cell::from_char('\u{4e16}')); // 世
    buf.set_raw(2, 0, Cell::from_char('\u{754c}')); // 界
    // ASCII
    buf.set_raw(0, 1, Cell::from_char('A'));
    buf.set_raw(1, 1, Cell::from_char('B'));

    let old = Buffer::new(20, 3);
    let output = present_frame(&buf, &old, caps_sync());

    assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
    assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);
    assert_flicker_free(&output);
}

// =============================================================================
// Stress Tests
// =============================================================================

/// 1000-frame soak: every frame has balanced brackets.
#[test]
fn stress_1000_frames_all_balanced() {
    let caps = caps_sync();
    let mut rng = Lcg::new(0xFF01_0001);
    let mut prev = Buffer::new(80, 24);

    for _ in 0..1000 {
        let mut current = prev.clone();
        let n = rng.next_u64() as usize % 60;
        for _ in 0..n {
            let x = rng.next_u16(80);
            let y = rng.next_u16(24);
            current.set_raw(x, y, Cell::from_char(rng.next_char()));
        }

        let output = present_frame(&current, &prev, caps);
        assert_eq!(count_occurrences(&output, ansi::SYNC_BEGIN), 1);
        assert_eq!(count_occurrences(&output, ansi::SYNC_END), 1);

        prev = current;
    }
}

/// Full-screen rewrite 100 frames: all bracket-enclosed.
#[test]
fn stress_full_screen_100_frames() {
    let caps = caps_sync();
    let mut prev = Buffer::new(120, 40);

    for seed in 0..100u64 {
        let current = random_buffer(120, 40, seed, 1.0);
        let output = present_frame(&current, &prev, caps);
        assert_flicker_free(&output);
        prev = current;
    }
}

/// Alternating sync/no-sync capabilities: each frame uses correct strategy.
#[test]
fn stress_alternating_capabilities() {
    let mut rng = Lcg::new(0xFF01_0002);
    let mut prev = Buffer::new(60, 20);

    for frame_id in 0..100 {
        let use_sync = frame_id % 2 == 0;
        let caps = if use_sync {
            caps_sync()
        } else {
            caps_no_sync()
        };

        let mut current = prev.clone();
        let n = rng.next_u64() as usize % 30;
        for _ in 0..n {
            let x = rng.next_u16(60);
            let y = rng.next_u16(20);
            current.set_raw(x, y, Cell::from_char(rng.next_char()));
        }

        let output = present_frame(&current, &prev, caps);

        if use_sync {
            assert_eq!(
                count_occurrences(&output, ansi::SYNC_BEGIN),
                1,
                "frame {}: sync should have brackets",
                frame_id
            );
            assert_eq!(count_occurrences(&output, ansi::CURSOR_HIDE), 0);
        } else {
            assert_eq!(
                count_occurrences(&output, ansi::SYNC_BEGIN),
                0,
                "frame {}: no-sync should not have brackets",
                frame_id
            );
            assert_eq!(count_occurrences(&output, ansi::CURSOR_HIDE), 1);
        }

        prev = current;
    }
}

// =============================================================================
// JSONL Structured Evidence
// =============================================================================

/// Generate JSONL evidence for all proof properties.
#[test]
fn evidence_jsonl_all_properties() {
    use std::time::Instant;

    let scenarios: &[(u16, u16, u64, &str)] = &[
        (80, 24, 0xABCD_0001, "standard_80x24"),
        (120, 40, 0xABCD_0002, "large_120x40"),
        (40, 10, 0xABCD_0003, "small_40x10"),
        (200, 60, 0xABCD_0004, "ultrawide_200x60"),
        (1, 1, 0xABCD_0005, "minimal_1x1"),
    ];

    for &(width, height, seed, label) in scenarios {
        let start = Instant::now();
        let buf = random_buffer(width, height, seed, 0.5);
        let old = Buffer::new(width, height);

        // Test sync mode
        let output_sync = present_frame(&buf, &old, caps_sync());
        let sync_begins = count_occurrences(&output_sync, ansi::SYNC_BEGIN);
        let sync_ends = count_occurrences(&output_sync, ansi::SYNC_END);
        let sync_flicker_free = analyze_stream(&output_sync).stats.is_flicker_free();

        // Test fallback mode
        let output_fallback = present_frame(&buf, &old, caps_no_sync());
        let cursor_hides = count_occurrences(&output_fallback, ansi::CURSOR_HIDE);
        let cursor_shows = count_occurrences(&output_fallback, ansi::CURSOR_SHOW);
        let fallback_no_sync = count_occurrences(&output_fallback, ansi::SYNC_BEGIN) == 0;

        let elapsed_us = start.elapsed().as_micros();

        eprintln!(
            concat!(
                "{{\"test\":\"sync_bracket_completeness\",",
                "\"scenario\":\"{}\",",
                "\"width\":{},\"height\":{},\"seed\":{},",
                "\"sync_begins\":{},\"sync_ends\":{},\"sync_balanced\":{},\"flicker_free\":{},",
                "\"cursor_hides\":{},\"cursor_shows\":{},\"cursor_balanced\":{},\"fallback_no_sync\":{},",
                "\"elapsed_us\":{}}}"
            ),
            label,
            width,
            height,
            seed,
            sync_begins,
            sync_ends,
            sync_begins == sync_ends,
            sync_flicker_free,
            cursor_hides,
            cursor_shows,
            cursor_hides == cursor_shows,
            fallback_no_sync,
            elapsed_us
        );

        // Assert all properties
        assert_eq!(sync_begins, 1, "{}: sync_begin count", label);
        assert_eq!(sync_ends, 1, "{}: sync_end count", label);
        assert!(sync_flicker_free, "{}: must be flicker free", label);
        assert_eq!(cursor_hides, 1, "{}: cursor_hide count", label);
        assert_eq!(cursor_shows, 1, "{}: cursor_show count", label);
        assert!(fallback_no_sync, "{}: fallback must not use sync", label);
    }
}