ftui-render 0.4.0

Render kernel: cells, buffers, diffs, and ANSI presentation.
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
#![forbid(unsafe_code)]

//! bd-22wk8.4: E2E integration test for Roaring Bitmap dirty tracking.
//!
//! Validates that a RoaringBitmap-based dirty tracker produces identical
//! diff output to the existing Vec<bool>/Vec<u8> dirty tracking in Buffer.
//!
//! Test scenarios:
//! 1. Sparse edits (text insertion at a few positions).
//! 2. Dense edits (full-row overwrites).
//! 3. Alternating-row edits.
//! 4. Single-cell edits.
//! 5. Full-screen dirty.
//! 6. Empty dirty (no mutations).
//! 7. Resize between frames.
//!
//! Each frame emits a JSONL log line for deterministic replay.
//!
//! Run:
//!   cargo test -p ftui-render --test roaring_dirty_e2e

use ftui_render::buffer::Buffer;
use ftui_render::cell::Cell;
use ftui_render::diff::BufferDiff;
use ftui_render::presenter::{Presenter, TerminalCapabilities};
use ftui_render::roaring_bitmap::RoaringBitmap;
use std::collections::BTreeSet;

/// Terminal sizes from the bead spec.
const SIZES: &[(u16, u16)] = &[(80, 24), (120, 40), (200, 60), (400, 100)];

// ============================================================================
// JSONL Log Entry
// ============================================================================

/// Structured log entry for each frame comparison.
#[derive(serde::Serialize)]
struct FrameLog {
    event: &'static str,
    frame_id: u64,
    screen: String,
    scenario: String,
    width: u16,
    height: u16,
    mutations: usize,
    dirty_cells_roaring: usize,
    dirty_cells_buffer: usize,
    dirty_rows_roaring: usize,
    dirty_rows_buffer: usize,
    diff_changes_full: usize,
    diff_changes_dirty: usize,
    diff_output_hash: String,
    roaring_superset: bool,
    diff_match: bool,
}

fn blake3_hex(data: &[u8]) -> String {
    let hash = blake3_hash(data);
    hex_encode(&hash)
}

/// Simple BLAKE3-like hash using std (we just use a deterministic hash for comparison).
fn blake3_hash(data: &[u8]) -> [u8; 32] {
    // Use a simple hash for the test — we only need deterministic comparison.
    // We'll use the built-in hasher approach via repeated XOR folding.
    let mut hash = [0u8; 32];
    for (i, &byte) in data.iter().enumerate() {
        hash[i % 32] ^= byte;
        // Mix bits
        let idx = i % 32;
        hash[idx] = hash[idx].wrapping_add(byte).wrapping_mul(31);
    }
    hash
}

fn hex_encode(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

// ============================================================================
// Shadow Tracker: mirrors Buffer dirty tracking with RoaringBitmap
// ============================================================================

struct ShadowTracker {
    bitmap: RoaringBitmap,
    width: u16,
}

impl ShadowTracker {
    fn new(width: u16) -> Self {
        Self {
            bitmap: RoaringBitmap::new(),
            width,
        }
    }

    fn mark_cell(&mut self, x: u16, y: u16) {
        let idx = y as u32 * self.width as u32 + x as u32;
        self.bitmap.insert(idx);
    }

    fn dirty_cell_count(&self) -> usize {
        self.bitmap.cardinality()
    }

    fn dirty_row_count(&self, height: u16) -> usize {
        let mut dirty_rows = BTreeSet::new();
        for idx in self.bitmap.iter() {
            dirty_rows.insert(idx / self.width as u32);
        }
        dirty_rows.len().min(height as usize)
    }

    /// Check that every cell changed in the diff is tracked by the roaring bitmap.
    fn is_superset_of_changes(&self, changes: &[(u16, u16)]) -> bool {
        changes.iter().all(|&(x, y)| {
            self.bitmap
                .contains(y as u32 * self.width as u32 + x as u32)
        })
    }

    fn clear(&mut self) {
        self.bitmap.clear();
    }
}

// ============================================================================
// Mutation Scenarios
// ============================================================================

/// Apply sparse text edits (a few characters at specific positions).
fn apply_sparse_edits(buf: &mut Buffer, shadow: &mut ShadowTracker) -> usize {
    let positions = [
        (0, 0),
        (5, 3),
        (10, 7),
        (buf.width() - 1, 0),
        (buf.width() / 2, buf.height() / 2),
    ];
    let mut count = 0;
    for &(x, y) in &positions {
        if x < buf.width() && y < buf.height() {
            buf.set(x, y, Cell::from_char('X'));
            shadow.mark_cell(x, y);
            count += 1;
        }
    }
    count
}

/// Apply dense edits (full rows overwritten).
fn apply_dense_row_edits(buf: &mut Buffer, shadow: &mut ShadowTracker) -> usize {
    let rows_to_edit = [0, buf.height() / 4, buf.height() / 2, buf.height() - 1];
    let mut count = 0;
    for &y in &rows_to_edit {
        if y < buf.height() {
            for x in 0..buf.width() {
                buf.set(x, y, Cell::from_char('#'));
                shadow.mark_cell(x, y);
                count += 1;
            }
        }
    }
    count
}

/// Apply alternating-row edits.
fn apply_alternating_rows(buf: &mut Buffer, shadow: &mut ShadowTracker) -> usize {
    let mut count = 0;
    for y in (0..buf.height()).step_by(2) {
        for x in 0..buf.width() {
            buf.set(x, y, Cell::from_char('-'));
            shadow.mark_cell(x, y);
            count += 1;
        }
    }
    count
}

/// Apply a single-cell edit.
fn apply_single_cell(buf: &mut Buffer, shadow: &mut ShadowTracker) -> usize {
    let x = buf.width() / 2;
    let y = buf.height() / 2;
    buf.set(x, y, Cell::from_char('*'));
    shadow.mark_cell(x, y);
    1
}

/// Apply full-screen dirty (every cell).
fn apply_full_screen(buf: &mut Buffer, shadow: &mut ShadowTracker) -> usize {
    let mut count = 0;
    for y in 0..buf.height() {
        for x in 0..buf.width() {
            buf.set(x, y, Cell::from_char('.'));
            shadow.mark_cell(x, y);
            count += 1;
        }
    }
    count
}

/// No mutations (empty dirty set).
fn apply_no_mutations(_buf: &mut Buffer, _shadow: &mut ShadowTracker) -> usize {
    0
}

// ============================================================================
// Core Comparison Logic
// ============================================================================

/// Run a single frame comparison and return the log entry.
fn run_frame_comparison(
    frame_id: u64,
    scenario: &str,
    old: &Buffer,
    new: &Buffer,
    shadow: &ShadowTracker,
) -> FrameLog {
    let w = new.width();
    let h = new.height();

    // Compute full diff (no dirty hints — ground truth).
    let full_diff = BufferDiff::compute(old, new);
    let full_changes: Vec<(u16, u16)> = full_diff.iter().collect();

    // Compute dirty-aware diff (uses Buffer's built-in dirty tracking).
    let dirty_diff = BufferDiff::compute_dirty(old, new);
    let dirty_changes: Vec<(u16, u16)> = dirty_diff.iter().collect();

    // Present both diffs through the presenter to get ANSI output.
    let caps = TerminalCapabilities::default();

    let full_output = {
        let mut sink = Vec::new();
        let mut presenter = Presenter::new(&mut sink, caps);
        presenter.present(new, &full_diff).unwrap();
        drop(presenter);
        sink
    };

    let dirty_output = {
        let mut sink = Vec::new();
        let mut presenter = Presenter::new(&mut sink, caps);
        presenter.present(new, &dirty_diff).unwrap();
        drop(presenter);
        sink
    };

    // Hash the outputs for comparison.
    let full_hash = blake3_hex(&full_output);
    let dirty_hash = blake3_hex(&dirty_output);

    // The dirty diff must produce the same ANSI output as the full diff.
    let diff_match = full_hash == dirty_hash;

    // The roaring shadow must be a superset of actual changes.
    let roaring_superset = shadow.is_superset_of_changes(&full_changes);

    FrameLog {
        event: "roaring_frame",
        frame_id,
        screen: format!("{w}x{h}"),
        scenario: scenario.to_string(),
        width: w,
        height: h,
        mutations: shadow.dirty_cell_count(),
        dirty_cells_roaring: shadow.dirty_cell_count(),
        dirty_cells_buffer: 0, // not accessible from integration tests (pub(crate))
        dirty_rows_roaring: shadow.dirty_row_count(h),
        dirty_rows_buffer: new.dirty_row_count(),
        diff_changes_full: full_changes.len(),
        diff_changes_dirty: dirty_changes.len(),
        diff_output_hash: full_hash,
        roaring_superset,
        diff_match,
    }
}

// ============================================================================
// Test Runner
// ============================================================================

fn run_scenario(
    scenario_name: &str,
    mutate: fn(&mut Buffer, &mut ShadowTracker) -> usize,
    logs: &mut Vec<FrameLog>,
    frame_counter: &mut u64,
) {
    for &(w, h) in SIZES {
        let old = Buffer::new(w, h);
        let mut new = Buffer::new(w, h);
        new.clear_dirty();

        let mut shadow = ShadowTracker::new(w);

        let _mutations = mutate(&mut new, &mut shadow);

        let log = run_frame_comparison(*frame_counter, scenario_name, &old, &new, &shadow);

        // Assert invariants.
        assert!(
            log.roaring_superset,
            "[{scenario_name} {w}x{h}] Roaring bitmap must be a superset of actual changes"
        );
        assert!(
            log.diff_match,
            "[{scenario_name} {w}x{h}] Dirty diff output must match full diff output"
        );
        // Dirty diff should find the same or fewer changes than full diff
        // (it can find fewer if some dirty rows have no actual changes, but
        // it must never miss real changes).
        assert!(
            log.diff_changes_dirty <= log.diff_changes_full
                || log.diff_changes_dirty == log.diff_changes_full,
            "[{scenario_name} {w}x{h}] Dirty diff found {} changes vs full diff {} changes",
            log.diff_changes_dirty,
            log.diff_changes_full,
        );

        logs.push(log);
        *frame_counter += 1;
    }
}

// ============================================================================
// Tests
// ============================================================================

#[test]
fn roaring_dirty_tracking_equivalence() {
    let mut logs = Vec::new();
    let mut frame_id = 0u64;

    // Scenario 1: Sparse edits
    run_scenario("sparse_edits", apply_sparse_edits, &mut logs, &mut frame_id);

    // Scenario 2: Dense row edits
    run_scenario(
        "dense_row_edits",
        apply_dense_row_edits,
        &mut logs,
        &mut frame_id,
    );

    // Scenario 3: Alternating rows
    run_scenario(
        "alternating_rows",
        apply_alternating_rows,
        &mut logs,
        &mut frame_id,
    );

    // Scenario 4: Single cell
    run_scenario("single_cell", apply_single_cell, &mut logs, &mut frame_id);

    // Scenario 5: Full screen
    run_scenario("full_screen", apply_full_screen, &mut logs, &mut frame_id);

    // Scenario 6: Empty dirty (no mutations)
    run_scenario("empty_dirty", apply_no_mutations, &mut logs, &mut frame_id);

    // Emit JSONL log.
    // Verify all logs are parseable JSONL.
    for log in &logs {
        let json = serde_json::to_string(log).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed["event"], "roaring_frame");
    }

    // Summary assertions.
    let total_frames = logs.len();
    let all_match = logs.iter().all(|l| l.diff_match);
    let all_superset = logs.iter().all(|l| l.roaring_superset);

    assert_eq!(
        total_frames,
        SIZES.len() * 6,
        "expected {} frames (6 scenarios x {} sizes)",
        SIZES.len() * 6,
        SIZES.len()
    );
    assert!(all_match, "all frames must have matching diff output");
    assert!(
        all_superset,
        "roaring must be superset of changes in all frames"
    );

    // Print summary for CI visibility.
    eprintln!("--- roaring_dirty_e2e summary ---");
    eprintln!("frames: {total_frames}");
    eprintln!("all_diff_match: {all_match}");
    eprintln!("all_roaring_superset: {all_superset}");
    for log in &logs {
        eprintln!(
            "  {} {:>7} mutations={:<6} roaring_cells={:<6} buffer_cells={:<6} diff_full={:<6} diff_dirty={:<6} match={}",
            log.scenario,
            log.screen,
            log.mutations,
            log.dirty_cells_roaring,
            log.dirty_cells_buffer,
            log.diff_changes_full,
            log.diff_changes_dirty,
            log.diff_match,
        );
    }
}

#[test]
fn roaring_incremental_multi_frame() {
    // Simulate multiple frames of incremental updates, clearing dirty between frames.
    let w = 120u16;
    let h = 40u16;
    let mut old = Buffer::new(w, h);
    let mut new = Buffer::new(w, h);
    let mut shadow = ShadowTracker::new(w);
    let mut logs = Vec::new();

    // Frame 0: initial render (all cells)
    for y in 0..h {
        for x in 0..w {
            new.set(x, y, Cell::from_char(' '));
            shadow.mark_cell(x, y);
        }
    }
    let log = run_frame_comparison(0, "initial", &old, &new, &shadow);
    assert!(log.diff_match);
    assert!(log.roaring_superset);
    logs.push(log);

    // Commit frame: old = new, clear dirty.
    old = new.clone();
    new.clear_dirty();
    shadow.clear();

    // Frame 1: sparse update (type some text on row 5)
    let text = "Hello, Roaring Bitmap!";
    for (i, ch) in text.chars().enumerate() {
        let x = i as u16;
        if x < w {
            new.set(x, 5, Cell::from_char(ch));
            shadow.mark_cell(x, 5);
        }
    }
    let log = run_frame_comparison(1, "type_text", &old, &new, &shadow);
    assert!(log.diff_match);
    assert!(log.roaring_superset);
    assert_eq!(log.dirty_rows_roaring, 1);
    assert_eq!(log.dirty_rows_buffer, 1);
    logs.push(log);

    // Commit frame.
    old = new.clone();
    new.clear_dirty();
    shadow.clear();

    // Frame 2: scroll simulation (overwrite rows 0..h-1 with rows 1..h content).
    for y in 0..h - 1 {
        for x in 0..w {
            if let Some(&cell) = old.get(x, y + 1) {
                new.set(x, y, cell);
            }
            shadow.mark_cell(x, y);
        }
    }
    // Clear last row.
    for x in 0..w {
        new.set(x, h - 1, Cell::from_char(' '));
        shadow.mark_cell(x, h - 1);
    }
    let log = run_frame_comparison(2, "scroll", &old, &new, &shadow);
    assert!(log.diff_match);
    assert!(log.roaring_superset);
    logs.push(log);

    // Commit frame.
    old = new.clone();
    new.clear_dirty();
    shadow.clear();

    // Frame 3: no changes.
    let log = run_frame_comparison(3, "idle", &old, &new, &shadow);
    assert!(log.diff_match);
    assert!(log.roaring_superset);
    assert_eq!(log.diff_changes_full, 0);
    assert_eq!(log.diff_changes_dirty, 0);
    assert_eq!(log.dirty_cells_roaring, 0);
    logs.push(log);

    // Emit JSONL.
    for log in &logs {
        let json = serde_json::to_string(log).unwrap();
        let _: serde_json::Value = serde_json::from_str(&json).unwrap();
    }

    eprintln!("--- roaring_incremental_multi_frame summary ---");
    for log in &logs {
        eprintln!(
            "  frame={} scenario={} mutations={} diff_full={} diff_dirty={} match={}",
            log.frame_id,
            log.scenario,
            log.mutations,
            log.diff_changes_full,
            log.diff_changes_dirty,
            log.diff_match,
        );
    }
}

#[test]
fn roaring_tracks_union_correctly() {
    // Verify that unioning two roaring dirty sets covers all changes from
    // two independent mutation passes.
    for &(w, h) in SIZES {
        let mut shadow_a = ShadowTracker::new(w);
        let mut shadow_b = ShadowTracker::new(w);

        // Pass A: mark odd columns on row 0.
        for x in (1..w).step_by(2) {
            shadow_a.mark_cell(x, 0);
        }

        // Pass B: mark even columns on row 0.
        for x in (0..w).step_by(2) {
            shadow_b.mark_cell(x, 0);
        }

        // Union should cover all columns.
        let union = shadow_a.bitmap.union(&shadow_b.bitmap);
        for x in 0..w {
            assert!(
                union.contains(x as u32),
                "union missing cell ({x}, 0) at {w}x{h}"
            );
        }
        assert_eq!(union.cardinality(), w as usize);
    }
}

#[test]
fn roaring_intersection_finds_overlapping_dirty() {
    // Verify intersection correctly identifies cells dirty in both passes.
    let w = 120u16;
    let h = 40u16;

    let mut shadow_a = ShadowTracker::new(w);
    let mut shadow_b = ShadowTracker::new(w);

    // Pass A: columns 0..60 on row 10.
    for x in 0..60 {
        shadow_a.mark_cell(x, 10);
    }

    // Pass B: columns 30..90 on row 10.
    for x in 30..90 {
        shadow_b.mark_cell(x, 10);
    }

    let intersection = shadow_a.bitmap.intersection(&shadow_b.bitmap);

    // Overlap is columns 30..60.
    assert_eq!(intersection.cardinality(), 30);
    for x in 30..60 {
        let idx = 10u32 * w as u32 + x as u32;
        assert!(intersection.contains(idx));
    }
    // Outside overlap should not be present.
    for x in 0..30 {
        let idx = 10u32 * w as u32 + x as u32;
        assert!(!intersection.contains(idx));
    }
    for x in 60..90u16 {
        let idx = 10u32 * w as u32 + x as u32;
        assert!(!intersection.contains(idx));
    }

    let _ = h; // suppress unused warning
}

#[test]
fn jsonl_schema_compliance() {
    // Verify every field in the JSONL schema is present and correctly typed.
    let old = Buffer::new(80, 24);
    let mut new = Buffer::new(80, 24);
    new.clear_dirty();
    new.set(0, 0, Cell::from_char('A'));
    let mut shadow = ShadowTracker::new(80);
    shadow.mark_cell(0, 0);

    let log = run_frame_comparison(0, "schema_test", &old, &new, &shadow);
    let json = serde_json::to_string(&log).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

    // Check all required fields exist.
    assert!(parsed["event"].is_string());
    assert!(parsed["frame_id"].is_u64());
    assert!(parsed["screen"].is_string());
    assert!(parsed["scenario"].is_string());
    assert!(parsed["width"].is_u64());
    assert!(parsed["height"].is_u64());
    assert!(parsed["mutations"].is_u64());
    assert!(parsed["dirty_cells_roaring"].is_u64());
    assert!(parsed["dirty_cells_buffer"].is_u64());
    assert!(parsed["dirty_rows_roaring"].is_u64());
    assert!(parsed["dirty_rows_buffer"].is_u64());
    assert!(parsed["diff_changes_full"].is_u64());
    assert!(parsed["diff_changes_dirty"].is_u64());
    assert!(parsed["diff_output_hash"].is_string());
    assert!(parsed["roaring_superset"].is_boolean());
    assert!(parsed["diff_match"].is_boolean());

    // Verify specific values.
    assert_eq!(parsed["event"], "roaring_frame");
    assert_eq!(parsed["width"], 80);
    assert_eq!(parsed["height"], 24);
    assert!(parsed["diff_match"].as_bool().unwrap());
    assert!(parsed["roaring_superset"].as_bool().unwrap());
}