whisper-apr 0.3.3

WASM-first automatic speech recognition engine implementing OpenAI Whisper
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
//! Probar-style TUI tests for Whisper Pipeline Visualization
#![allow(
    clippy::float_cmp,
    clippy::field_reassign_with_default,
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::large_stack_arrays,
    dead_code
)]
//!
//! EXTREME TDD: All tests written FIRST before implementation.
//!
//! ## Test Categories
//!
//! 1. Frame Rendering - Each panel renders correctly
//! 2. State Machine - Correct state transitions
//! 3. Keyboard Handling - All bindings work
//! 4. UX Coverage - 100% element coverage
//! 5. Visualization - Correct ASCII art output

use super::*;
use ratatui::backend::TestBackend;
use ratatui::Terminal;

// ============================================================================
// Test Utilities (Probar-style)
// ============================================================================

/// Render a frame to test buffer
fn render_frame(app: &WhisperApp, width: u16, height: u16) -> TestFrame {
    let backend = TestBackend::new(width, height);
    let mut terminal = Terminal::new(backend).expect("terminal");
    terminal
        .draw(|f| render_whisper_dashboard(f, app))
        .expect("draw");
    TestFrame::from_buffer(terminal.backend().buffer())
}

/// Test frame wrapper for probar-style assertions
#[derive(Debug)]
pub struct TestFrame {
    content: String,
    width: u16,
    height: u16,
}

impl TestFrame {
    /// Create from ratatui buffer
    pub fn from_buffer(buffer: &ratatui::buffer::Buffer) -> Self {
        let mut content = String::new();
        for y in 0..buffer.area.height {
            for x in 0..buffer.area.width {
                let cell = buffer.cell((x, y)).expect("cell");
                content.push_str(cell.symbol());
            }
            content.push('\n');
        }
        Self {
            content,
            width: buffer.area.width,
            height: buffer.area.height,
        }
    }

    /// Check if frame contains text
    pub fn contains(&self, text: &str) -> bool {
        self.content.contains(text)
    }

    /// Get frame dimensions
    pub fn dimensions(&self) -> (u16, u16) {
        (self.width, self.height)
    }

    /// Get raw content for debugging
    pub fn as_text(&self) -> &str {
        &self.content
    }
}

/// Probar-style frame assertion builder
pub struct FrameAssertion<'a> {
    frame: &'a TestFrame,
    errors: Vec<String>,
    soft_mode: bool,
}

impl<'a> FrameAssertion<'a> {
    pub fn new(frame: &'a TestFrame) -> Self {
        Self {
            frame,
            errors: Vec::new(),
            soft_mode: false,
        }
    }

    /// Enable soft assertion mode (collect errors instead of failing)
    pub fn soft(mut self) -> Self {
        self.soft_mode = true;
        self
    }

    /// Assert frame contains text
    pub fn to_contain_text(&mut self, text: &str) -> &mut Self {
        if !self.frame.contains(text) {
            let err = format!("Expected frame to contain '{}'", text);
            if self.soft_mode {
                self.errors.push(err);
            } else {
                panic!("{}", err);
            }
        }
        self
    }

    /// Assert frame does NOT contain text
    pub fn not_to_contain_text(&mut self, text: &str) -> &mut Self {
        if self.frame.contains(text) {
            let err = format!("Expected frame NOT to contain '{}'", text);
            if self.soft_mode {
                self.errors.push(err);
            } else {
                panic!("{}", err);
            }
        }
        self
    }

    /// Assert frame has specific dimensions
    pub fn to_have_size(&mut self, width: u16, height: u16) -> &mut Self {
        let (w, h) = self.frame.dimensions();
        if w != width || h != height {
            let err = format!("Expected {}x{}, got {}x{}", width, height, w, h);
            if self.soft_mode {
                self.errors.push(err);
            } else {
                panic!("{}", err);
            }
        }
        self
    }

    /// Get collected errors (soft mode)
    pub fn errors(&self) -> &[String] {
        &self.errors
    }

    /// Finalize soft assertions
    pub fn finalize(&self) -> Result<(), String> {
        if self.errors.is_empty() {
            Ok(())
        } else {
            Err(format!(
                "{} assertions failed:\n{}",
                self.errors.len(),
                self.errors.join("\n")
            ))
        }
    }
}

/// Probar-style expect_frame helper
pub fn expect_frame(frame: &TestFrame) -> FrameAssertion<'_> {
    FrameAssertion::new(frame)
}

/// UX Coverage tracker (simplified probar-style)
pub struct UxCoverage {
    panels: Vec<String>,
    visited: std::collections::HashSet<String>,
    keys: Vec<char>,
    pressed: std::collections::HashSet<char>,
}

impl UxCoverage {
    pub fn new() -> Self {
        Self {
            panels: vec![
                "waveform".to_string(),
                "mel".to_string(),
                "encoder".to_string(),
                "decoder".to_string(),
                "attention".to_string(),
                "transcription".to_string(),
                "metrics".to_string(),
                "help".to_string(),
            ],
            visited: std::collections::HashSet::new(),
            keys: vec!['1', '2', '3', '4', '5', '6', '7', 'q', 'r', ' ', '?'],
            pressed: std::collections::HashSet::new(),
        }
    }

    pub fn visit_panel(&mut self, panel: &str) {
        self.visited.insert(panel.to_string());
    }

    pub fn press_key(&mut self, key: char) {
        self.pressed.insert(key);
    }

    pub fn panel_coverage(&self) -> f64 {
        self.visited.len() as f64 / self.panels.len() as f64
    }

    pub fn key_coverage(&self) -> f64 {
        self.pressed.len() as f64 / self.keys.len() as f64
    }

    pub fn overall_coverage(&self) -> f64 {
        (self.panel_coverage() + self.key_coverage()) / 2.0
    }

    pub fn is_complete(&self) -> bool {
        self.visited.len() >= self.panels.len() && self.pressed.len() >= self.keys.len()
    }
}

impl Default for UxCoverage {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Application State Tests
// ============================================================================

#[test]
fn test_whisper_app_initial_state() {
    let app = WhisperApp::new();

    assert_eq!(app.current_panel, WhisperPanel::Waveform);
    assert_eq!(app.state, WhisperState::Idle);
    assert!(!app.should_quit);
    assert!(!app.paused);
}

#[test]
fn test_whisper_app_panel_navigation() {
    let mut app = WhisperApp::new();

    // Navigate through all panels
    app.set_panel(WhisperPanel::Mel);
    assert_eq!(app.current_panel, WhisperPanel::Mel);

    app.set_panel(WhisperPanel::Encoder);
    assert_eq!(app.current_panel, WhisperPanel::Encoder);

    app.set_panel(WhisperPanel::Decoder);
    assert_eq!(app.current_panel, WhisperPanel::Decoder);

    app.set_panel(WhisperPanel::Attention);
    assert_eq!(app.current_panel, WhisperPanel::Attention);

    app.set_panel(WhisperPanel::Transcription);
    assert_eq!(app.current_panel, WhisperPanel::Transcription);

    app.set_panel(WhisperPanel::Metrics);
    assert_eq!(app.current_panel, WhisperPanel::Metrics);

    app.set_panel(WhisperPanel::Help);
    assert_eq!(app.current_panel, WhisperPanel::Help);
}

#[test]
fn test_whisper_app_state_machine() {
    let mut app = WhisperApp::new();

    // Start idle
    assert_eq!(app.state, WhisperState::Idle);

    // Load audio → Waveform state
    app.load_audio(&[0.0; 16000]);
    assert_eq!(app.state, WhisperState::WaveformReady);

    // Compute mel → Mel state
    app.compute_mel();
    assert_eq!(app.state, WhisperState::MelReady);

    // Encode → Encoding state
    app.start_encoding();
    assert_eq!(app.state, WhisperState::Encoding);

    // Decode → Decoding state
    app.start_decoding();
    assert_eq!(app.state, WhisperState::Decoding);

    // Complete → Complete state
    app.complete();
    assert_eq!(app.state, WhisperState::Complete);
}

#[test]
fn test_whisper_app_keyboard_handling() {
    let mut app = WhisperApp::new();

    // Number keys switch panels
    app.handle_key('1');
    assert_eq!(app.current_panel, WhisperPanel::Waveform);

    app.handle_key('2');
    assert_eq!(app.current_panel, WhisperPanel::Mel);

    app.handle_key('3');
    assert_eq!(app.current_panel, WhisperPanel::Encoder);

    app.handle_key('4');
    assert_eq!(app.current_panel, WhisperPanel::Decoder);

    app.handle_key('5');
    assert_eq!(app.current_panel, WhisperPanel::Attention);

    app.handle_key('6');
    assert_eq!(app.current_panel, WhisperPanel::Transcription);

    app.handle_key('7');
    assert_eq!(app.current_panel, WhisperPanel::Metrics);

    app.handle_key('?');
    assert_eq!(app.current_panel, WhisperPanel::Help);

    // Space toggles pause
    assert!(!app.paused);
    app.handle_key(' ');
    assert!(app.paused);
    app.handle_key(' ');
    assert!(!app.paused);

    // r resets
    app.load_audio(&[0.0; 16000]);
    assert_ne!(app.state, WhisperState::Idle);
    app.handle_key('r');
    assert_eq!(app.state, WhisperState::Idle);

    // q quits
    assert!(!app.should_quit);
    app.handle_key('q');
    assert!(app.should_quit);
}

#[test]
fn test_whisper_app_reset() {
    let mut app = WhisperApp::new();

    // Advance through pipeline
    app.load_audio(&[0.0; 16000]);
    app.compute_mel();
    app.start_encoding();

    // Reset
    app.reset();

    assert_eq!(app.state, WhisperState::Idle);
    assert!(app.audio_data.is_empty());
    assert!(app.mel_data.is_empty());
}

// ============================================================================
// Panel Rendering Tests
// ============================================================================

#[test]
fn test_waveform_panel_renders() {
    let mut app = WhisperApp::new();
    // Create sample audio data
    let pattern = [0.1_f32, -0.1, 0.2, -0.2, 0.0];
    let audio: Vec<f32> = pattern.iter().cycle().take(500).copied().collect();
    app.load_audio(&audio);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("WAVEFORM")
        .to_contain_text("samples");
}

#[test]
fn test_mel_panel_renders() {
    let mut app = WhisperApp::new();
    app.load_audio(&vec![0.0; 16000]);
    app.compute_mel();
    app.set_panel(WhisperPanel::Mel);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("MEL")
        .to_contain_text("80"); // 80 mel bins
}

#[test]
fn test_encoder_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Encoder);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("ENCODER")
        .to_contain_text("Layer");
}

#[test]
fn test_decoder_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Decoder);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("DECODER")
        .to_contain_text("Token");
}

#[test]
fn test_attention_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Attention);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("ATTENTION")
        .to_contain_text("Cross");
}

#[test]
fn test_transcription_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Transcription);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame).to_contain_text("TRANSCRIPTION");
}

#[test]
fn test_metrics_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Metrics);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("METRICS")
        .to_contain_text("RTF");
}

#[test]
fn test_help_panel_renders() {
    let mut app = WhisperApp::new();
    app.set_panel(WhisperPanel::Help);

    let frame = render_frame(&app, 80, 24);

    expect_frame(&frame)
        .to_contain_text("HELP")
        .to_contain_text("Keyboard");
}

#[test]
fn test_all_panels_render_without_panic() {
    let app = WhisperApp::new();

    for panel in [
        WhisperPanel::Waveform,
        WhisperPanel::Mel,
        WhisperPanel::Encoder,
        WhisperPanel::Decoder,
        WhisperPanel::Attention,
        WhisperPanel::Transcription,
        WhisperPanel::Metrics,
        WhisperPanel::Help,
    ] {
        let mut app = app.clone();
        app.set_panel(panel);
        let _frame = render_frame(&app, 80, 24);
        // Just verify no panic
    }
}

// ============================================================================
// Visualization Tests
// ============================================================================

#[test]
fn test_waveform_visualization() {
    // Test data with clear peaks
    let audio: Vec<f32> = (0..100).map(|i| (i as f32 * 0.1).sin()).collect();

    let display = WaveformDisplay::new(&audio, 40, 10);

    // Should produce ASCII art
    let output = display.render();
    assert!(!output.is_empty());

    // Should have correct dimensions
    assert_eq!(display.width(), 40);
    assert_eq!(display.height(), 10);
}

#[test]
fn test_mel_spectrogram_visualization() {
    // Mock mel data: 80 bins x 100 frames
    let mel: Vec<f32> = (0..8000)
        .map(|i| ((i % 80) as f32 / 80.0) * -4.0) // Log-mel scale
        .collect();

    let display = MelDisplay::new(&mel, 80, 100, 40, 20);

    // Should produce heatmap
    let output = display.render();
    assert!(!output.is_empty());
}

#[test]
fn test_waveform_handles_empty_input() {
    let display = WaveformDisplay::new(&[], 40, 10);
    let output = display.render();

    // Should handle gracefully, not panic
    assert!(output.is_empty() || output.contains("No data"));
}

#[test]
fn test_mel_handles_empty_input() {
    let display = MelDisplay::new(&[], 80, 0, 40, 20);
    let output = display.render();

    // Should handle gracefully
    assert!(output.is_empty() || output.contains("No data"));
}

// ============================================================================
// UX Coverage Tests
// ============================================================================

#[test]
fn test_ux_coverage_tracks_panels() {
    let mut coverage = UxCoverage::new();

    coverage.visit_panel("waveform");
    coverage.visit_panel("mel");
    coverage.visit_panel("encoder");

    assert!(coverage.panel_coverage() > 0.3);
    assert!(coverage.panel_coverage() < 0.5);
}

#[test]
fn test_ux_coverage_tracks_keys() {
    let mut coverage = UxCoverage::new();

    coverage.press_key('1');
    coverage.press_key('2');
    coverage.press_key('q');

    assert!(coverage.key_coverage() > 0.2);
}

#[test]
fn test_ux_coverage_complete() {
    let mut coverage = UxCoverage::new();

    // Visit all panels
    for panel in [
        "waveform",
        "mel",
        "encoder",
        "decoder",
        "attention",
        "transcription",
        "metrics",
        "help",
    ] {
        coverage.visit_panel(panel);
    }

    // Press all keys
    for key in ['1', '2', '3', '4', '5', '6', '7', 'q', 'r', ' ', '?'] {
        coverage.press_key(key);
    }

    assert!(coverage.is_complete());
    assert!((coverage.overall_coverage() - 1.0).abs() < f64::EPSILON);
}

#[test]
fn test_full_ux_coverage_via_app() {
    let mut app = WhisperApp::new();
    let mut coverage = UxCoverage::new();

    // Exercise all panels via keyboard
    for (key, panel) in [
        ('1', "waveform"),
        ('2', "mel"),
        ('3', "encoder"),
        ('4', "decoder"),
        ('5', "attention"),
        ('6', "transcription"),
        ('7', "metrics"),
        ('?', "help"),
    ] {
        app.handle_key(key);
        coverage.press_key(key);
        coverage.visit_panel(panel);

        // Verify panel changed
        let frame = render_frame(&app, 80, 24);
        assert!(frame.contains(&panel.to_uppercase()) || frame.contains(panel));
    }

    // Exercise other keys
    app.handle_key(' '); // pause
    coverage.press_key(' ');

    app.handle_key('r'); // reset
    coverage.press_key('r');

    app.handle_key('q'); // quit
    coverage.press_key('q');

    assert!(
        coverage.is_complete(),
        "UX coverage not complete: {:.0}%",
        coverage.overall_coverage() * 100.0
    );
}

// ============================================================================
// Soft Assertion Tests
// ============================================================================

#[test]
fn test_soft_assertions_collect_errors() {
    let app = WhisperApp::new();
    let frame = render_frame(&app, 80, 24);

    let mut assertion = expect_frame(&frame).soft();

    // These should pass
    assertion.to_contain_text("WAVEFORM");
    assertion.to_have_size(80, 24);

    // This should fail but not panic
    assertion.to_contain_text("NONEXISTENT_TEXT_12345");

    let errors = assertion.errors();
    assert_eq!(errors.len(), 1);
    assert!(errors[0].contains("NONEXISTENT_TEXT_12345"));
}

// ============================================================================
// Frame Sequence Tests
// ============================================================================

#[test]
fn test_panel_transition_sequence() {
    let mut app = WhisperApp::new();
    let mut frames = Vec::new();

    // Capture frames as we navigate
    for panel in [
        WhisperPanel::Waveform,
        WhisperPanel::Mel,
        WhisperPanel::Encoder,
        WhisperPanel::Decoder,
    ] {
        app.set_panel(panel);
        frames.push(render_frame(&app, 80, 24));
    }

    // All frames should be different
    for i in 0..frames.len() {
        for j in (i + 1)..frames.len() {
            assert_ne!(
                frames[i].as_text(),
                frames[j].as_text(),
                "Frames {} and {} should differ",
                i,
                j
            );
        }
    }
}

#[test]
fn test_state_transition_sequence() {
    let mut app = WhisperApp::new();
    let mut states = Vec::new();

    // Track state transitions
    states.push(app.state);

    app.load_audio(&[0.0; 16000]);
    states.push(app.state);

    app.compute_mel();
    states.push(app.state);

    app.start_encoding();
    states.push(app.state);

    app.start_decoding();
    states.push(app.state);

    app.complete();
    states.push(app.state);

    // Verify expected sequence
    assert_eq!(
        states,
        vec![
            WhisperState::Idle,
            WhisperState::WaveformReady,
            WhisperState::MelReady,
            WhisperState::Encoding,
            WhisperState::Decoding,
            WhisperState::Complete,
        ]
    );
}

// ============================================================================
// Jidoka Alerts Tests (WAPR-CLI-001 Section 3.3)
// ============================================================================

#[test]
fn test_jidoka_alerts_default() {
    let alerts = JidokaAlerts::default();
    assert_eq!(alerts.drift_ms, 0.0);
    assert_eq!(alerts.min_log_prob, 0.0);
    assert_eq!(alerts.max_amplitude, 0.0);
    assert!(!alerts.has_alerts());
}

#[test]
fn test_jidoka_alerts_high_drift() {
    let mut alerts = JidokaAlerts::default();

    // Below threshold
    alerts.drift_ms = 400.0;
    assert!(!alerts.is_high_drift());

    // At threshold
    alerts.drift_ms = 500.0;
    assert!(!alerts.is_high_drift());

    // Above threshold (YELLOW)
    alerts.drift_ms = 501.0;
    assert!(alerts.is_high_drift());
    assert!(alerts.has_alerts());
}

#[test]
fn test_jidoka_alerts_low_confidence() {
    let mut alerts = JidokaAlerts::default();

    // Good confidence
    alerts.min_log_prob = -0.5;
    assert!(!alerts.is_low_confidence());

    // At threshold
    alerts.min_log_prob = -1.0;
    assert!(!alerts.is_low_confidence());

    // Below threshold (RED)
    alerts.min_log_prob = -1.1;
    assert!(alerts.is_low_confidence());
    assert!(alerts.has_alerts());
}

#[test]
fn test_jidoka_alerts_clipping() {
    let mut alerts = JidokaAlerts::default();

    // Normal audio
    alerts.max_amplitude = 0.8;
    assert!(!alerts.is_clipping());

    // At threshold
    alerts.max_amplitude = 1.0;
    assert!(!alerts.is_clipping());

    // Clipping (RED)
    alerts.max_amplitude = 1.01;
    assert!(alerts.is_clipping());
    assert!(alerts.has_alerts());
}

#[test]
fn test_jidoka_alerts_multiple() {
    let mut alerts = JidokaAlerts::default();
    alerts.drift_ms = 600.0;
    alerts.min_log_prob = -2.0;
    alerts.max_amplitude = 1.5;

    assert!(alerts.is_high_drift());
    assert!(alerts.is_low_confidence());
    assert!(alerts.is_clipping());
    assert!(alerts.has_alerts());
}

#[test]
fn test_load_audio_computes_max_amplitude() {
    let mut app = WhisperApp::new();

    // Load audio with known max amplitude
    let audio: Vec<f32> = vec![0.1, -0.5, 0.8, -0.3, 0.2];
    app.load_audio(&audio);

    assert!((app.alerts.max_amplitude - 0.8).abs() < 0.001);
    assert!(!app.alerts.is_clipping());
}

#[test]
fn test_load_audio_detects_clipping() {
    let mut app = WhisperApp::new();

    // Load clipping audio
    let audio: Vec<f32> = vec![0.5, 1.2, -0.3];
    app.load_audio(&audio);

    assert!((app.alerts.max_amplitude - 1.2).abs() < 0.001);
    assert!(app.alerts.is_clipping());
}

#[test]
fn test_reset_clears_jidoka_alerts() {
    let mut app = WhisperApp::new();
    app.alerts.drift_ms = 1000.0;
    app.alerts.min_log_prob = -5.0;
    app.alerts.max_amplitude = 2.0;

    app.reset();

    assert_eq!(app.alerts.drift_ms, 0.0);
    assert_eq!(app.alerts.min_log_prob, 0.0);
    assert_eq!(app.alerts.max_amplitude, 0.0);
}

#[test]
fn test_trace_overlay_keybinding() {
    let mut app = WhisperApp::new();
    assert!(!app.show_trace_overlay);

    app.handle_key('t');
    assert!(app.show_trace_overlay);

    app.handle_key('t');
    assert!(!app.show_trace_overlay);
}

#[test]
fn test_vad_overlay_keybinding() {
    let mut app = WhisperApp::new();
    assert!(!app.show_vad_overlay);

    app.handle_key('v');
    assert!(app.show_vad_overlay);

    app.handle_key('v');
    assert!(!app.show_vad_overlay);
}

// ============================================================================
// Property-Based Tests
// ============================================================================

mod property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(50))]

        #[test]
        fn property_panel_index_roundtrip(index in 0usize..8) {
            // from_index -> index should round-trip for valid indices
            let panel = WhisperPanel::from_index(index);
            let back = panel.index();
            // from_index clamps to Help for index >= 7, so check valid range
            if index < 8 {
                prop_assert!(back <= 7, "panel index should be valid");
            }
        }

        #[test]
        fn property_load_audio_changes_state(len in 1usize..10000) {
            // Loading any non-empty audio should transition to WaveformReady
            let mut app = WhisperApp::new();
            let audio: Vec<f32> = (0..len).map(|i| (i as f32 * 0.01).sin()).collect();
            app.load_audio(&audio);
            prop_assert_eq!(app.state, WhisperState::WaveformReady);
            prop_assert_eq!(app.audio_data.len(), len);
        }

        #[test]
        fn property_reset_clears_state(len in 1usize..1000) {
            // Reset should always return to Idle with empty data
            let mut app = WhisperApp::new();
            let audio: Vec<f32> = vec![0.0; len];
            app.load_audio(&audio);
            app.compute_mel();
            app.reset();
            prop_assert_eq!(app.state, WhisperState::Idle);
            prop_assert!(app.audio_data.is_empty());
            prop_assert!(app.mel_data.is_empty());
        }

        #[test]
        fn property_waveform_render_no_panic(
            samples in prop::collection::vec(-1.0f32..1.0, 0..1000),
            width in 1usize..200,
            height in 1usize..50
        ) {
            // Waveform rendering should never panic
            let result = std::panic::catch_unwind(|| {
                super::super::visualization::render_waveform(&samples, width, height)
            });
            prop_assert!(result.is_ok(), "waveform render should not panic");
        }

        #[test]
        fn property_mel_render_no_panic(
            n_frames in 1usize..100,
            width in 1usize..100,
            height in 1usize..50
        ) {
            // Mel rendering should never panic with valid dimensions
            let mel_data: Vec<f32> = vec![0.0; 80 * n_frames];
            let result = std::panic::catch_unwind(|| {
                super::super::visualization::render_mel_spectrogram(
                    &mel_data, 80, n_frames, width, height
                )
            });
            prop_assert!(result.is_ok(), "mel render should not panic");
        }

        #[test]
        fn property_attention_render_no_panic(
            n_tokens in 1usize..20,
            n_frames in 1usize..50,
            width in 1usize..80,
            height in 1usize..30
        ) {
            // Attention rendering should never panic
            let attention: Vec<Vec<f32>> = (0..n_tokens)
                .map(|_| (0..n_frames).map(|f| f as f32 / n_frames as f32).collect())
                .collect();
            let result = std::panic::catch_unwind(|| {
                super::super::visualization::render_attention_heatmap(&attention, width, height)
            });
            prop_assert!(result.is_ok(), "attention render should not panic");
        }

        #[test]
        fn property_keyboard_valid_keys(key in "[1-7qr? ]") {
            // Valid keys should not crash and should change state appropriately
            let mut app = WhisperApp::new();
            for c in key.chars() {
                app.handle_key(c);
            }
            // App should still be in a valid state
            prop_assert!(matches!(
                app.state,
                WhisperState::Idle
                    | WhisperState::WaveformReady
                    | WhisperState::MelReady
                    | WhisperState::Encoding
                    | WhisperState::Decoding
                    | WhisperState::Complete
                    | WhisperState::Error
            ));
        }

        #[test]
        fn property_rtf_non_negative(
            audio_len in 1000usize..100000,
            processing_ms in 1.0f32..10000.0
        ) {
            // RTF should always be non-negative
            let mut metrics = super::super::app::PipelineMetrics::default();
            metrics.audio_duration_secs = audio_len as f32 / 16000.0;
            metrics.total_time_ms = processing_ms;
            metrics.compute_rtf();
            prop_assert!(metrics.rtf >= 0.0, "RTF should be non-negative");
        }
    }
}