ftui-extras 0.4.0

Feature-gated extras for FrankenTUI (markdown, charts, clipboard, themes).
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
//! Tests for multi-layer backdrop composition (bd-l8x9.8.5).
//!
//! This module validates the correctness and determinism of the StackedFx compositor:
//! - Layer ordering semantics (A over B != B over A)
//! - Alpha compositing correctness vs explicit PackedRgba::over() math
//! - Determinism (fixed inputs produce identical outputs)
//! - Allocation stability (no buffer growth after warmup)

#![forbid(unsafe_code)]

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use ftui_extras::visual_fx::{
    BackdropFx, BlendMode, FxContext, FxLayer, FxQuality, StackedFx, ThemeInputs,
};
use ftui_render::cell::PackedRgba;

// =============================================================================
// Test Effects
// =============================================================================

/// Test effect that fills with a constant RGBA color.
struct ConstantColor {
    color: PackedRgba,
}

impl ConstantColor {
    fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self {
            color: PackedRgba::rgba(r, g, b, a),
        }
    }

    fn opaque(r: u8, g: u8, b: u8) -> Self {
        Self::new(r, g, b, 255)
    }
}

impl BackdropFx for ConstantColor {
    fn name(&self) -> &'static str {
        "constant-color"
    }

    fn render(&mut self, ctx: FxContext<'_>, out: &mut [PackedRgba]) {
        if ctx.is_empty() {
            return;
        }
        out[..ctx.len()].fill(self.color);
    }
}

/// Test effect that produces a pattern based on cell index.
/// Used to verify cell-by-cell composition.
struct PatternFx {
    base_r: u8,
    base_g: u8,
    base_b: u8,
    alpha: u8,
}

impl PatternFx {
    fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
        Self {
            base_r: r,
            base_g: g,
            base_b: b,
            alpha: a,
        }
    }
}

impl BackdropFx for PatternFx {
    fn name(&self) -> &'static str {
        "pattern"
    }

    fn render(&mut self, ctx: FxContext<'_>, out: &mut [PackedRgba]) {
        for (i, cell) in out[..ctx.len()].iter_mut().enumerate() {
            let idx = i as u8;
            *cell = PackedRgba::rgba(
                self.base_r.wrapping_add(idx),
                self.base_g.wrapping_add(idx.wrapping_mul(2)),
                self.base_b.wrapping_add(idx.wrapping_mul(3)),
                self.alpha,
            );
        }
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

fn make_context(width: u16, height: u16) -> (FxContext<'static>, ThemeInputs) {
    let theme = Box::leak(Box::new(ThemeInputs::default_dark()));
    let ctx = FxContext {
        width,
        height,
        frame: 0,
        time_seconds: 0.0,
        quality: FxQuality::Full,
        theme,
    };
    (ctx, *theme)
}

fn hash_output(out: &[PackedRgba]) -> u64 {
    let mut hasher = DefaultHasher::new();
    for c in out {
        c.r().hash(&mut hasher);
        c.g().hash(&mut hasher);
        c.b().hash(&mut hasher);
        c.a().hash(&mut hasher);
    }
    hasher.finish()
}

// =============================================================================
// ORDERING TESTS
// =============================================================================

/// **TEST: Layer ordering semantics - A over B != B over A**
///
/// Given two constant layers A and B with known RGBA + opacity, assert that
/// `[A, B]` (A on bottom, B on top) produces different output than `[B, A]`.
///
/// **API Order Documentation**: Layers are composited bottom-to-top (index 0 is
/// the base/bottom layer). This matches the "painter's algorithm" convention.
#[test]
fn ordering_a_over_b_not_equal_b_over_a() {
    let (ctx, _theme) = make_context(4, 4);
    let len = ctx.len();

    // Layer A: Semi-transparent red (opacity 0.6)
    let layer_a_color = PackedRgba::rgba(255, 0, 0, 153); // ~60% alpha
    let layer_a_opacity = 1.0; // No additional opacity scaling

    // Layer B: Semi-transparent blue (opacity 0.5)
    let layer_b_color = PackedRgba::rgba(0, 0, 255, 128); // ~50% alpha
    let layer_b_opacity = 1.0;

    // Stack 1: A (bottom) -> B (top)
    let mut stack_ab = StackedFx::new();
    stack_ab.push(FxLayer::with_opacity(
        Box::new(ConstantColor {
            color: layer_a_color,
        }),
        layer_a_opacity,
    ));
    stack_ab.push(FxLayer::with_opacity(
        Box::new(ConstantColor {
            color: layer_b_color,
        }),
        layer_b_opacity,
    ));

    let mut out_ab = vec![PackedRgba::TRANSPARENT; len];
    stack_ab.render(ctx, &mut out_ab);

    // Stack 2: B (bottom) -> A (top)
    let mut stack_ba = StackedFx::new();
    stack_ba.push(FxLayer::with_opacity(
        Box::new(ConstantColor {
            color: layer_b_color,
        }),
        layer_b_opacity,
    ));
    stack_ba.push(FxLayer::with_opacity(
        Box::new(ConstantColor {
            color: layer_a_color,
        }),
        layer_a_opacity,
    ));

    let mut out_ba = vec![PackedRgba::TRANSPARENT; len];
    stack_ba.render(ctx, &mut out_ba);

    // Assert: ordering matters
    assert_ne!(out_ab, out_ba, "Layer order should matter: [A,B] != [B,A]");

    // Document: verify the expected order (B on top in stack_ab)
    // B is blue with 50% alpha over A (red with 60% alpha)
    // The final result should have visible blue contribution
    let ab_sample = out_ab[0];
    let ba_sample = out_ba[0];

    // In stack_ab, blue (B) is on top, so blue channel should be more prominent
    // In stack_ba, red (A) is on top, so red channel should be more prominent
    assert!(
        ab_sample.b() > ba_sample.b() || ab_sample.r() < ba_sample.r(),
        "Order verification: ab_sample={:?}, ba_sample={:?}",
        ab_sample,
        ba_sample
    );
}

/// **TEST: Opaque top layer completely covers bottom**
///
/// Documents that a fully opaque top layer (index 1) completely obscures
/// the bottom layer (index 0).
#[test]
fn ordering_opaque_top_covers_bottom() {
    let (ctx, _theme) = make_context(2, 2);
    let len = ctx.len();

    let mut stack = StackedFx::new();
    // Bottom: green
    stack.push(FxLayer::new(Box::new(ConstantColor::opaque(0, 255, 0))));
    // Top: opaque red (should fully cover)
    stack.push(FxLayer::new(Box::new(ConstantColor::opaque(255, 0, 0))));

    let mut out = vec![PackedRgba::TRANSPARENT; len];
    stack.render(ctx, &mut out);

    // All cells should be pure red
    for (i, color) in out.iter().enumerate() {
        assert_eq!(
            *color,
            PackedRgba::rgb(255, 0, 0),
            "Cell {i} should be covered by opaque red top layer"
        );
    }
}

// =============================================================================
// ALPHA CORRECTNESS TESTS
// =============================================================================

/// **TEST: Alpha correctness - Compare stacked output against explicit over() math**
///
/// Renders a 2-layer stack and compares each cell against manually computed
/// `PackedRgba::over()` results.
#[test]
fn alpha_correctness_matches_explicit_over_math() {
    let (ctx, _theme) = make_context(4, 3);
    let len = ctx.len();

    // Layer 0 (bottom): opaque green
    let layer0_color = PackedRgba::rgb(0, 255, 0);
    // Layer 1 (top): semi-transparent red (50% opacity via layer setting)
    let layer1_base = PackedRgba::rgb(255, 0, 0);
    let layer1_opacity = 0.5;

    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(ConstantColor {
        color: layer0_color,
    })));
    stack.push(FxLayer::with_opacity(
        Box::new(ConstantColor { color: layer1_base }),
        layer1_opacity,
    ));

    let mut out = vec![PackedRgba::TRANSPARENT; len];
    stack.render(ctx, &mut out);

    // Manually compute expected: layer1.with_opacity(0.5).over(layer0)
    let layer1_with_opacity = layer1_base.with_opacity(layer1_opacity);
    let expected = layer1_with_opacity.over(layer0_color);

    for (i, actual) in out.iter().enumerate() {
        assert_eq!(
            *actual, expected,
            "Cell {i}: actual {:?} != expected {:?}",
            actual, expected
        );
    }
}

/// **TEST: Alpha correctness with three layers (opaque, semi-transparent, fully transparent)**
///
/// Tests the full range of alpha values in composition.
#[test]
fn alpha_correctness_three_layer_composition() {
    let (ctx, _theme) = make_context(2, 2);
    let len = ctx.len();

    // Layer 0: opaque base (blue)
    let l0 = PackedRgba::rgb(0, 0, 200);
    // Layer 1: 50% opacity (green)
    let l1_base = PackedRgba::rgb(0, 200, 0);
    let l1_opacity = 0.5;
    // Layer 2: fully transparent (should not affect result)
    let l2_base = PackedRgba::rgb(255, 255, 255);
    let l2_opacity = 0.0;

    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(ConstantColor { color: l0 })));
    stack.push(FxLayer::with_opacity(
        Box::new(ConstantColor { color: l1_base }),
        l1_opacity,
    ));
    stack.push(FxLayer::with_opacity(
        Box::new(ConstantColor { color: l2_base }),
        l2_opacity,
    ));

    let mut out = vec![PackedRgba::TRANSPARENT; len];
    stack.render(ctx, &mut out);

    // Manual calculation: l2 (opacity 0) doesn't contribute
    // Final = l1.with_opacity(0.5).over(l0)
    let l1_adjusted = l1_base.with_opacity(l1_opacity);
    let expected = l1_adjusted.over(l0);

    for (i, actual) in out.iter().enumerate() {
        assert_eq!(
            *actual, expected,
            "Cell {i}: three-layer composition mismatch"
        );
    }
}

/// **TEST: Alpha correctness cell-by-cell with varying patterns**
///
/// Uses a pattern-generating effect to verify per-cell composition is correct.
#[test]
fn alpha_correctness_cell_by_cell_patterns() {
    let (ctx, _theme) = make_context(8, 4);
    let len = ctx.len();

    // Layer 0: Pattern with base (100, 50, 25), opaque
    let mut l0_fx = PatternFx::new(100, 50, 25, 255);
    // Layer 1: Pattern with base (0, 100, 200), 70% alpha
    let mut l1_fx = PatternFx::new(0, 100, 200, 179); // 179 ~= 70% of 255

    // Get expected outputs by rendering each layer separately
    let mut l0_buf = vec![PackedRgba::TRANSPARENT; len];
    l0_fx.render(ctx, &mut l0_buf);

    let mut l1_buf = vec![PackedRgba::TRANSPARENT; len];
    l1_fx.render(ctx, &mut l1_buf);

    // Stack them
    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(PatternFx::new(100, 50, 25, 255))));
    stack.push(FxLayer::new(Box::new(PatternFx::new(0, 100, 200, 179))));

    let mut out = vec![PackedRgba::TRANSPARENT; len];
    stack.render(ctx, &mut out);

    // Verify each cell matches explicit over() computation
    for i in 0..len {
        let expected = l1_buf[i].over(l0_buf[i]);
        let actual = out[i];
        assert_eq!(
            actual, expected,
            "Cell {i}: pattern composition mismatch. \
             l0={:?}, l1={:?}, expected={:?}, actual={:?}",
            l0_buf[i], l1_buf[i], expected, actual
        );
    }
}

// =============================================================================
// DETERMINISM TESTS
// =============================================================================

/// **TEST: Determinism - Fixed inputs produce identical output hashes**
///
/// Renders the same stack multiple times and verifies the output hash is stable.
#[test]
fn determinism_fixed_inputs_produce_identical_hash() {
    let (ctx, _theme) = make_context(10, 8);
    let len = ctx.len();

    // Define a specific stack configuration
    fn make_test_stack() -> StackedFx {
        let mut stack = StackedFx::new();
        stack.push(FxLayer::new(Box::new(ConstantColor::opaque(30, 60, 90))));
        stack.push(FxLayer::with_opacity(
            Box::new(ConstantColor::new(200, 100, 50, 200)),
            0.7,
        ));
        stack.push(FxLayer::with_opacity_and_blend(
            Box::new(ConstantColor::opaque(10, 20, 30)),
            0.3,
            BlendMode::Additive,
        ));
        stack
    }

    // Run multiple times and collect hashes
    let mut hashes = Vec::new();
    for _ in 0..5 {
        let mut stack = make_test_stack();
        let mut out = vec![PackedRgba::TRANSPARENT; len];
        stack.render(ctx, &mut out);
        hashes.push(hash_output(&out));
    }

    // All hashes should be identical
    let first_hash = hashes[0];
    for (i, hash) in hashes.iter().enumerate() {
        assert_eq!(
            *hash, first_hash,
            "Render {i} produced different hash: {hash} != {first_hash}"
        );
    }
}

/// **TEST: Determinism across resize cycles**
///
/// Renders at size A, resizes to B, back to A, and verifies output matches.
#[test]
fn determinism_across_resize_cycles() {
    let theme = ThemeInputs::default_dark();

    // Context at 6x4
    let ctx_a = FxContext {
        width: 6,
        height: 4,
        frame: 0,
        time_seconds: 0.0,
        quality: FxQuality::Full,
        theme: &theme,
    };
    let len_a = ctx_a.len();

    // Context at 10x8
    let ctx_b = FxContext {
        width: 10,
        height: 8,
        frame: 0,
        time_seconds: 0.0,
        quality: FxQuality::Full,
        theme: &theme,
    };
    let len_b = ctx_b.len();

    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(ConstantColor::opaque(100, 150, 200))));
    stack.push(FxLayer::with_opacity(
        Box::new(ConstantColor::new(50, 100, 150, 180)),
        0.6,
    ));

    // First render at size A
    let mut out_a1 = vec![PackedRgba::TRANSPARENT; len_a];
    stack.resize(6, 4);
    stack.render(ctx_a, &mut out_a1);
    let hash_a1 = hash_output(&out_a1);

    // Render at size B
    let mut out_b = vec![PackedRgba::TRANSPARENT; len_b];
    stack.resize(10, 8);
    stack.render(ctx_b, &mut out_b);

    // Render again at size A
    let mut out_a2 = vec![PackedRgba::TRANSPARENT; len_a];
    stack.resize(6, 4);
    stack.render(ctx_a, &mut out_a2);
    let hash_a2 = hash_output(&out_a2);

    // Hashes should match for same size
    assert_eq!(
        hash_a1, hash_a2,
        "Output at size A should be identical before and after resize to B"
    );
}

// =============================================================================
// ALLOCATION STABILITY TESTS
// =============================================================================

/// **TEST: Allocation proxy - No buffer growth after warmup**
///
/// After initial render, subsequent renders at the same size should not
/// increase buffer capacity.
#[test]
fn allocation_no_growth_after_warmup() {
    let (ctx, _theme) = make_context(20, 15);
    let len = ctx.len();

    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(ConstantColor::opaque(100, 100, 100))));
    stack.push(FxLayer::with_opacity(
        Box::new(ConstantColor::new(50, 50, 50, 200)),
        0.5,
    ));

    let mut out = vec![PackedRgba::TRANSPARENT; len];

    // Warmup render
    stack.render(ctx, &mut out);

    // Multiple subsequent renders (should not allocate)
    for _ in 0..10 {
        stack.render(ctx, &mut out);
    }

    // The test passes if no panic occurs and output is consistent
    // (Buffer growth would cause observable effects in Debug builds)
    let final_hash = hash_output(&out);

    // Re-render and verify consistency
    stack.render(ctx, &mut out);
    let verify_hash = hash_output(&out);

    assert_eq!(
        final_hash, verify_hash,
        "Output should be stable across renders"
    );
}

/// **TEST: Allocation - Buffers grow only when needed**
///
/// Verifies that rendering at progressively larger sizes doesn't cause
/// unexpected allocation patterns.
#[test]
fn allocation_grows_only_when_needed() {
    let theme = ThemeInputs::default_dark();

    let mut stack = StackedFx::new();
    stack.push(FxLayer::new(Box::new(ConstantColor::opaque(80, 80, 80))));

    // Render at small size
    let ctx_small = FxContext {
        width: 4,
        height: 4,
        frame: 0,
        time_seconds: 0.0,
        quality: FxQuality::Full,
        theme: &theme,
    };
    let mut out_small = vec![PackedRgba::TRANSPARENT; ctx_small.len()];
    stack.resize(4, 4);
    stack.render(ctx_small, &mut out_small);

    // Render at same size - should not allocate
    for _ in 0..5 {
        stack.render(ctx_small, &mut out_small);
    }

    // Render at larger size - will allocate
    let ctx_large = FxContext {
        width: 20,
        height: 20,
        frame: 0,
        time_seconds: 0.0,
        quality: FxQuality::Full,
        theme: &theme,
    };
    let mut out_large = vec![PackedRgba::TRANSPARENT; ctx_large.len()];
    stack.resize(20, 20);
    stack.render(ctx_large, &mut out_large);

    // Render at original small size - should NOT shrink buffers
    // (grow-only policy)
    for _ in 0..5 {
        stack.resize(4, 4);
        stack.render(ctx_small, &mut out_small);
    }

    // Verify correctness at small size
    assert!(
        out_small.iter().all(|c| *c == PackedRgba::rgb(80, 80, 80)),
        "Small render should produce correct output after large render"
    );
}

// =============================================================================
// BLEND MODE TESTS
// =============================================================================

/// **TEST: Blend modes produce different results**
///
/// Verifies that Over, Additive, Multiply, and Screen produce distinct outputs.
#[test]
fn blend_modes_produce_distinct_results() {
    let (ctx, _theme) = make_context(2, 2);
    let len = ctx.len();

    let top_color = PackedRgba::rgb(100, 50, 150);
    let top_opacity = 0.8;

    let modes = [
        BlendMode::Over,
        BlendMode::Additive,
        BlendMode::Multiply,
        BlendMode::Screen,
    ];

    let mut results = Vec::new();
    for mode in modes {
        let mut stack = StackedFx::new();
        // Bottom layer: gray
        stack.push(FxLayer::new(Box::new(ConstantColor::opaque(100, 100, 100))));
        stack.push(FxLayer::with_opacity_and_blend(
            Box::new(ConstantColor { color: top_color }),
            top_opacity,
            mode,
        ));

        let mut out = vec![PackedRgba::TRANSPARENT; len];
        stack.render(ctx, &mut out);
        results.push((mode, out[0]));
    }

    // Each blend mode should produce a different result
    for i in 0..results.len() {
        for j in (i + 1)..results.len() {
            assert_ne!(
                results[i].1, results[j].1,
                "{:?} and {:?} should produce different results",
                results[i].0, results[j].0
            );
        }
    }
}