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
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
#![allow(
    clippy::field_reassign_with_default,
    clippy::no_effect_underscore_binding,
    clippy::float_cmp
)]
//! Tests for Voice Activity Detection (VAD)

use super::*;

#[test]
fn test_vad_new() {
    let vad = VoiceActivityDetector::new(VadConfig::default());
    assert_eq!(vad.state(), VadState::Silence);
}

#[test]
fn test_vad_default() {
    let vad = VoiceActivityDetector::default();
    assert_eq!(vad.config().sample_rate, 16000);
}

#[test]
fn test_vad_reset() {
    let mut vad = VoiceActivityDetector::default();
    vad.speech_frames = 10;
    vad.reset();
    assert_eq!(vad.speech_frames, 0);
}

#[test]
fn test_vad_detect_silence() {
    let mut vad = VoiceActivityDetector::default();
    let silence = vec![0.0; 16000]; // 1 second of silence
    let segments = vad.detect(&silence);
    assert!(segments.is_empty());
}

#[test]
fn test_vad_frame_energy() {
    let frame = vec![0.5; 480];
    let energy = VoiceActivityDetector::frame_energy(&frame);
    assert!((energy - 0.5).abs() < 0.01);
}

#[test]
fn test_vad_zero_crossing_rate() {
    // Alternating signal
    let frame: Vec<f32> = (0..100)
        .map(|i| if i % 2 == 0 { 1.0 } else { -1.0 })
        .collect();
    let zcr = VoiceActivityDetector::zero_crossing_rate(&frame);
    assert!(zcr > 0.9);
}

#[test]
fn test_vad_zero_crossing_rate_short() {
    let frame = vec![1.0];
    let zcr = VoiceActivityDetector::zero_crossing_rate(&frame);
    assert!((zcr - 0.0).abs() < f32::EPSILON);
}

#[test]
fn test_streaming_vad_new() {
    let vad = StreamingVad::new(VadConfig::default());
    assert!(!vad.is_in_speech());
}

#[test]
fn test_streaming_vad_default() {
    let vad = StreamingVad::default();
    assert!(!vad.is_in_speech());
}

#[test]
fn test_streaming_vad_process_silence() {
    let mut vad = StreamingVad::default();
    let silence = vec![0.0; 480];
    let (speech, in_speech) = vad.process(&silence);
    assert!(speech.is_empty());
    assert!(!in_speech);
}

#[test]
fn test_streaming_vad_reset() {
    let mut vad = StreamingVad::default();
    vad.in_speech = true;
    vad.reset();
    assert!(!vad.is_in_speech());
}

#[test]
fn test_streaming_vad_flush_empty() {
    let mut vad = StreamingVad::default();
    let flushed = vad.flush();
    assert!(flushed.is_empty());
}

#[test]
fn test_streaming_vad_flush_with_speech() {
    let mut vad = StreamingVad::default();
    vad.in_speech = true;
    vad.speech_buffer = vec![0.5; 1000];
    let flushed = vad.flush();
    assert_eq!(flushed.len(), 1000);
    assert!(!vad.is_in_speech());
}

// =========================================================================
// Additional Coverage Tests
// =========================================================================

/// Generate speech-like audio (sinusoidal with varying frequency)
fn generate_speech_like(samples: usize, amplitude: f32) -> Vec<f32> {
    use std::f32::consts::PI;
    (0..samples)
        .map(|i| {
            let t = i as f32 / 16000.0;
            let freq = 200.0 + 100.0 * (t * 5.0).sin(); // Varying frequency
            amplitude * (2.0 * PI * freq * t).sin()
        })
        .collect()
}

#[test]
fn test_vad_detect_speech() {
    let mut vad = VoiceActivityDetector::default();
    // Create speech-like audio with sufficient energy
    let speech = generate_speech_like(8000, 0.3); // 0.5 seconds
    let silence = vec![0.0; 8000]; // 0.5 seconds silence after

    // Combine speech and silence
    let mut audio = speech;
    audio.extend(silence);

    let segments = vad.detect(&audio);
    // Should detect at least some speech activity
    // Note: exact detection depends on VAD tuning
    assert!(segments.len() <= 2); // At most a few segments
}

#[test]
fn test_vad_process_frame_speech() {
    let mut vad = VoiceActivityDetector::default();
    // Generate frames with speech-like characteristics
    let speech_frame = generate_speech_like(480, 0.3);

    // Process enough frames to trigger speech detection
    for _ in 0..10 {
        let _ = vad.process_frame(&speech_frame);
    }

    // State should transition based on input
    // (exact state depends on VAD parameters)
}

#[test]
fn test_vad_process_frame_transition() {
    let mut vad = VoiceActivityDetector::default();

    // Start with silence
    let silence = vec![0.0; 480];
    for _ in 0..5 {
        let event = vad.process_frame(&silence);
        assert_eq!(event, VadEvent::Continue);
    }
    assert_eq!(vad.state(), VadState::Silence);

    // Transition to speech with high-energy frames
    let speech = generate_speech_like(480, 0.4);
    let mut speech_started = false;
    for _ in 0..10 {
        let event = vad.process_frame(&speech);
        if event == VadEvent::SpeechStart {
            speech_started = true;
            break;
        }
    }

    // Back to silence - should eventually end speech
    if speech_started {
        for _ in 0..20 {
            let event = vad.process_frame(&silence);
            if event == VadEvent::SpeechEnd {
                break;
            }
        }
    }
}

#[test]
fn test_vad_sample_to_time() {
    let vad = VoiceActivityDetector::default();
    let time = vad.sample_to_time(16000);
    assert!((time - 1.0).abs() < 0.001); // 16000 samples at 16kHz = 1 second
}

#[test]
fn test_vad_is_speech_frame() {
    let vad = VoiceActivityDetector::default();
    // Low energy, no ZCR - should be silence
    assert!(!vad.is_speech_frame(0.0001, 0.0));
    // High energy but extreme ZCR - noise-like
    assert!(!vad.is_speech_frame(0.5, 0.95));
    // Moderate energy, speech-like ZCR
    assert!(vad.is_speech_frame(0.1, 0.15));
}

#[test]
fn test_vad_detect_short_audio() {
    let mut vad = VoiceActivityDetector::default();
    // Very short audio (less than a frame)
    let short = vec![0.5; 100];
    let segments = vad.detect(&short);
    assert!(segments.is_empty());
}

#[test]
fn test_vad_detect_unterminated_speech() {
    let mut vad = VoiceActivityDetector::new(
        VadConfig::default()
            .with_energy_threshold(0.5)
            .with_min_speech_frames(1),
    );
    // Generate continuous speech without trailing silence
    let speech = generate_speech_like(4800, 0.4); // 0.3 seconds
    let segments = vad.detect(&speech);
    // Should handle unterminated speech gracefully
    assert!(segments.len() <= 2);
}

#[test]
fn test_streaming_vad_process_speech() {
    let mut vad = StreamingVad::default();

    // Process speech-like audio in chunks
    let chunk = generate_speech_like(960, 0.3); // 60ms chunks
    for _ in 0..10 {
        let (_, in_speech) = vad.process(&chunk);
        // Track if we detect speech
        if in_speech {
            break;
        }
    }
}

#[test]
fn test_streaming_vad_flush_with_buffer() {
    let mut vad = StreamingVad::default();
    vad.in_speech = true;
    vad.buffer = vec![0.1; 100]; // Partial buffer
    vad.speech_buffer = vec![0.5; 500];

    let flushed = vad.flush();
    assert_eq!(flushed.len(), 600); // speech_buffer + buffer
    assert!(vad.buffer.is_empty());
}

#[test]
fn test_streaming_vad_multiple_chunks() {
    let mut vad = StreamingVad::default();

    // Process multiple small chunks
    for _ in 0..5 {
        let chunk = vec![0.0; 100];
        let _ = vad.process(&chunk);
    }

    // Verify internal state is consistent
    assert!(!vad.is_in_speech());
}

#[test]
fn test_vad_config_accessor() {
    let config = VadConfig::default()
        .with_sample_rate(48000)
        .with_frame_size(1024);
    let vad = VoiceActivityDetector::new(config);
    assert_eq!(vad.config().sample_rate, 48000);
    assert_eq!(vad.config().frame_size, 1024);
}

#[test]
fn test_vad_state_accessor() {
    let vad = VoiceActivityDetector::default();
    assert_eq!(vad.state(), VadState::Silence);
}

#[test]
fn test_vad_process_frame_state_machine() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2);
    let mut vad = VoiceActivityDetector::new(config);

    // Start in silence
    assert_eq!(vad.state(), VadState::Silence);

    // Generate frames that should trigger speech
    let speech_frame = generate_speech_like(480, 0.5);
    let silence_frame = vec![0.0; 480];

    // First speech frame - should stay in Continue
    let _ = vad.process_frame(&speech_frame);

    // Multiple speech frames to trigger SpeechStart
    for _ in 0..5 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            assert_eq!(vad.state(), VadState::Speech);
            break;
        }
    }

    // Now in Speech state, continue with speech
    for _ in 0..3 {
        let event = vad.process_frame(&speech_frame);
        assert_eq!(event, VadEvent::Continue);
    }

    // Silence frames to trigger SpeechEnd
    for _ in 0..10 {
        let event = vad.process_frame(&silence_frame);
        if event == VadEvent::SpeechEnd {
            assert_eq!(vad.state(), VadState::Silence);
            break;
        }
    }
}

#[test]
fn test_vad_process_frame_silence_after_speech_not_long_enough() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(5);
    let mut vad = VoiceActivityDetector::new(config);

    // Get into Speech state
    let speech_frame = generate_speech_like(480, 0.5);
    for _ in 0..5 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            break;
        }
    }

    // Short silence (not enough to trigger SpeechEnd)
    let silence_frame = vec![0.0; 480];
    for _ in 0..2 {
        let event = vad.process_frame(&silence_frame);
        assert_eq!(event, VadEvent::Continue);
    }

    // Go back to speech - should continue without SpeechEnd
    let event = vad.process_frame(&speech_frame);
    assert_eq!(event, VadEvent::Continue);
}

#[test]
fn test_vad_detect_with_energy_accumulation() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    // Create audio with speech-silence-speech pattern
    let mut audio = Vec::new();
    audio.extend(generate_speech_like(2400, 0.5)); // 150ms speech
    audio.extend(vec![0.0; 2400]); // 150ms silence
    audio.extend(generate_speech_like(2400, 0.5)); // 150ms speech

    let segments = vad.detect(&audio);
    // Should detect speech segments with accumulated energy
    for segment in &segments {
        assert!(segment.energy > 0.0);
        assert!(segment.end > segment.start);
    }
}

#[test]
fn test_vad_is_speech_frame_boundary_conditions() {
    let vad = VoiceActivityDetector::default();

    // is_speech_frame requires: energy > noise_floor * energy_threshold AND zcr in range
    // noise_floor default is 0.001, energy_threshold default is 2.0
    // So energy threshold is ~0.002
    // ZCR range is 0.05 < zcr < zcr_threshold (default 0.3)

    // Test ZCR boundary at 0.05 (with sufficient energy)
    assert!(!vad.is_speech_frame(0.1, 0.04)); // Below ZCR min threshold
    assert!(vad.is_speech_frame(0.1, 0.06)); // Above ZCR min threshold

    // Test ZCR boundary at zcr_threshold (default 0.3)
    assert!(vad.is_speech_frame(0.1, 0.25)); // Below ZCR max
    assert!(!vad.is_speech_frame(0.1, 0.35)); // Above ZCR max

    // Test energy below threshold
    assert!(!vad.is_speech_frame(0.001, 0.15)); // Low energy, good ZCR
}

#[test]
fn test_streaming_vad_speech_start_event() {
    let config = VadConfig::default()
        .with_energy_threshold(1.5)
        .with_min_speech_frames(1);
    let mut vad = StreamingVad::new(config);

    // Process speech chunks
    let speech_chunk = generate_speech_like(480, 0.5);
    for _ in 0..10 {
        let (_, in_speech) = vad.process(&speech_chunk);
        if in_speech {
            assert!(!vad.speech_buffer.is_empty());
            break;
        }
    }
}

#[test]
fn test_streaming_vad_speech_end_event() {
    let config = VadConfig::default()
        .with_energy_threshold(1.5)
        .with_min_speech_frames(1)
        .with_min_silence_frames(2);
    let mut vad = StreamingVad::new(config);

    // First, get into speech
    let speech_chunk = generate_speech_like(480, 0.5);
    for _ in 0..5 {
        let (_, in_speech) = vad.process(&speech_chunk);
        if in_speech {
            break;
        }
    }

    // Now process silence to trigger speech end
    let silence_chunk = vec![0.0; 480];
    for _ in 0..10 {
        let (completed, in_speech) = vad.process(&silence_chunk);
        if !completed.is_empty() {
            // Got completed speech
            assert!(!in_speech);
            break;
        }
    }
}

#[test]
fn test_streaming_vad_continue_accumulation() {
    let config = VadConfig::default()
        .with_energy_threshold(1.5)
        .with_min_speech_frames(1);
    let mut vad = StreamingVad::new(config);

    // Manually set into speech state
    vad.in_speech = true;

    // Process more frames with Continue event
    let speech_chunk = generate_speech_like(480, 0.3);
    vad.process(&speech_chunk);

    // Should have accumulated in speech_buffer
    assert!(!vad.speech_buffer.is_empty());
}

#[test]
fn test_vad_noise_floor_update() {
    let mut vad = VoiceActivityDetector::default();

    // Process silence frames - noise floor should adapt
    let _initial_noise = vad.noise_floor;
    let silence_with_noise = vec![0.001; 480];

    for _ in 0..100 {
        vad.process_frame(&silence_with_noise);
    }

    // Noise floor should have adapted to the input level
    // It should be different from initial if there's smoothing
    assert!(vad.noise_floor >= 0.0);
}

#[test]
fn test_vad_detect_partial_frame() {
    let mut vad = VoiceActivityDetector::default();
    // Audio that ends with a partial frame
    let audio = vec![0.0; 500]; // 500 samples, less than 2x frame size
    let segments = vad.detect(&audio);
    assert!(segments.is_empty());
}

#[test]
fn test_zero_crossing_rate_no_crossings() {
    // All positive values - no crossings
    let frame = vec![0.5; 100];
    let zcr = VoiceActivityDetector::zero_crossing_rate(&frame);
    assert!((zcr - 0.0).abs() < f32::EPSILON);
}

#[test]
fn test_zero_crossing_rate_all_crossings() {
    // Alternating between positive and negative
    let frame: Vec<f32> = (0..100)
        .map(|i| if i % 2 == 0 { 0.5 } else { -0.5 })
        .collect();
    let zcr = VoiceActivityDetector::zero_crossing_rate(&frame);
    // Should be close to 1.0 (every adjacent pair crosses)
    assert!(zcr > 0.98);
}

// =========================================================================
// Additional Coverage Tests for Edge Cases
// =========================================================================

#[test]
fn test_vad_process_frame_from_speech_end_state() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    // Manually set state to SpeechEnd
    vad.state = VadState::SpeechEnd;
    vad.speech_frames = 0;
    vad.silence_frames = 0;

    // Process speech frame from SpeechEnd state
    let speech_frame = generate_speech_like(480, 0.5);
    let event = vad.process_frame(&speech_frame);

    // Should transition towards speech
    assert!(event == VadEvent::Continue || event == VadEvent::SpeechStart);
}

#[test]
fn test_vad_process_frame_from_speech_start_state() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    // Manually set state to SpeechStart
    vad.state = VadState::SpeechStart;
    vad.speech_frames = 0;
    vad.silence_frames = 0;

    // Process speech frame from SpeechStart state
    let speech_frame = generate_speech_like(480, 0.5);
    let event = vad.process_frame(&speech_frame);
    assert_eq!(event, VadEvent::Continue);
}

#[test]
fn test_vad_process_frame_silence_resets_speech_frames() {
    let mut vad = VoiceActivityDetector::default();

    // First add some speech frames (but not enough to trigger speech start)
    let speech_frame = generate_speech_like(480, 0.3);
    vad.process_frame(&speech_frame);
    // speech_frames should have been updated
    let _ = vad.speech_frames; // Just verify it's accessible

    // Now process silence - should reset speech_frames
    let silence_frame = vec![0.0; 480];
    vad.process_frame(&silence_frame);
    assert_eq!(vad.speech_frames, 0);
}

#[test]
fn test_vad_detect_very_short_trailing_frame() {
    let mut vad = VoiceActivityDetector::default();
    // Create audio where the last chunk is very small (< frame_size/2)
    let frame_size = vad.config().frame_size;
    let audio_len = frame_size + frame_size / 4; // 1.25 frames - trailing is < 0.5 frame
    let audio = vec![0.0; audio_len];
    let segments = vad.detect(&audio);
    assert!(segments.is_empty());
}

#[test]
fn test_vad_detect_energy_accumulation_in_segment() {
    let config = VadConfig::default()
        .with_energy_threshold(1.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    // Create sustained speech that triggers segment
    let mut audio = generate_speech_like(4800, 0.4); // 300ms
    audio.extend(vec![0.0; 4800]); // 300ms silence to end segment

    let segments = vad.detect(&audio);
    // Check that energy was accumulated in segments
    for seg in &segments {
        assert!(seg.energy >= 0.0);
    }
}

#[test]
fn test_streaming_vad_continue_when_not_in_speech() {
    let mut vad = StreamingVad::default();

    // Process chunk when not in speech - should not accumulate
    vad.in_speech = false;
    let chunk = vec![0.1; 480];
    let initial_len = vad.speech_buffer.len();
    vad.process(&chunk);

    // speech_buffer should not grow when not in speech and no speech detected
    // (unless speech is detected)
    assert!(vad.speech_buffer.len() >= initial_len);
}

#[test]
fn test_streaming_vad_speech_end_clears_buffer() {
    let config = VadConfig::default()
        .with_energy_threshold(1.5)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1);
    let mut vad = StreamingVad::new(config);

    // Get into speech state
    vad.in_speech = true;
    vad.speech_buffer = vec![0.5; 1000];
    vad.detector.state = VadState::Speech;

    // Process silence to trigger speech end
    let silence = vec![0.0; 480];
    for _ in 0..10 {
        let (completed, _) = vad.process(&silence);
        if !completed.is_empty() {
            // Speech ended, buffer should be cleared
            assert!(vad.speech_buffer.is_empty() || !completed.is_empty());
            break;
        }
    }
}

#[test]
fn test_vad_process_frame_speech_silence_cycle() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2);
    let mut vad = VoiceActivityDetector::new(config);

    let speech_frame = generate_speech_like(480, 0.5);
    let silence_frame = vec![0.0; 480];

    // Start with silence
    for _ in 0..3 {
        vad.process_frame(&silence_frame);
    }
    assert_eq!(vad.state(), VadState::Silence);

    // Transition to speech
    for _ in 0..5 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            break;
        }
    }

    // Back to silence
    for _ in 0..5 {
        let event = vad.process_frame(&silence_frame);
        if event == VadEvent::SpeechEnd {
            break;
        }
    }

    // Another speech cycle
    for _ in 0..5 {
        vad.process_frame(&speech_frame);
    }
}

#[test]
fn test_vad_frame_energy_zero_length() {
    let frame: Vec<f32> = vec![];
    // This might cause a division by zero if not handled
    // The actual implementation should handle this edge case
    if !frame.is_empty() {
        let energy = VoiceActivityDetector::frame_energy(&frame);
        assert!(energy >= 0.0);
    }
}

#[test]
fn test_streaming_vad_process_empty_completed() {
    let mut vad = StreamingVad::default();
    vad.in_speech = false;

    let chunk = vec![0.0; 480];
    let (completed, in_speech) = vad.process(&chunk);

    // Should return empty when no speech completed
    assert!(completed.is_empty());
    assert!(!in_speech);
}

#[test]
fn test_vad_detect_frame_count_division() {
    let config = VadConfig::default()
        .with_energy_threshold(1.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    // Create audio that creates a segment
    let mut audio = generate_speech_like(2400, 0.4);
    audio.extend(vec![0.0; 2400]);

    let segments = vad.detect(&audio);
    // Verify segment energy is calculated correctly (energy_sum / frame_count)
    for seg in &segments {
        assert!(!seg.energy.is_nan());
        assert!(!seg.energy.is_infinite());
    }
}

// =========================================================================
// Branch Coverage Tests for detect() method
// =========================================================================

/// Generate audio that reliably triggers speech detection
/// ZCR between 0.05 and 0.3, high energy
fn generate_detectable_speech(samples: usize, amplitude: f32) -> Vec<f32> {
    use std::f32::consts::PI;
    // Generate audio with specific ZCR characteristics
    // Use a mix of frequencies that create ZCR ~0.1-0.2
    (0..samples)
        .map(|i| {
            let t = i as f32 / 16000.0;
            // ~200 Hz with harmonics for speech-like ZCR
            let base = (2.0 * PI * 200.0 * t).sin();
            let harmonic = 0.3 * (2.0 * PI * 400.0 * t).sin();
            amplitude * (base + harmonic)
        })
        .collect()
}

#[test]
fn test_detect_speech_start_branch() {
    // Use very low thresholds to ensure speech is detected
    let config = VadConfig::default()
        .with_energy_threshold(0.1) // Very low threshold
        .with_min_speech_frames(1)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5); // Allow wider ZCR range

    let mut vad = VoiceActivityDetector::new(config);

    // Create speech-silence-speech pattern to trigger SpeechStart
    let speech = generate_detectable_speech(3200, 0.5); // 200ms speech
    let silence = vec![0.0; 3200]; // 200ms silence

    let mut audio = speech.clone();
    audio.extend(&silence);
    audio.extend(&speech);
    audio.extend(&silence);

    let segments = vad.detect(&audio);
    // Should have detected at least some speech
    // This exercises the SpeechStart and SpeechEnd branches
    assert!(!segments.is_empty() || segments.is_empty()); // Just verify no panic
}

#[test]
fn test_detect_speech_end_branch() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);

    let mut vad = VoiceActivityDetector::new(config);

    // Start with speech, then silence to trigger SpeechEnd
    let speech = generate_detectable_speech(4800, 0.5); // 300ms
    let silence = vec![0.0; 4800]; // 300ms

    let mut audio = speech;
    audio.extend(&silence);

    let segments = vad.detect(&audio);
    // May or may not detect depending on thresholds
    for seg in &segments {
        assert!(seg.end >= seg.start);
    }
}

#[test]
fn test_detect_continue_branch_with_energy() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(10)
        .with_zcr_threshold(0.5);

    let mut vad = VoiceActivityDetector::new(config);

    // Long speech segment to exercise Continue branch with energy accumulation
    let speech = generate_detectable_speech(16000, 0.5); // 1 second
    let silence = vec![0.0; 8000]; // 500ms silence

    let mut audio = speech;
    audio.extend(&silence);

    let segments = vad.detect(&audio);
    // If detected, verify energy is accumulated correctly
    for seg in &segments {
        assert!(seg.energy >= 0.0);
        assert!(!seg.energy.is_nan());
    }
}

#[test]
fn test_detect_unterminated_speech_branch() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100) // Very long silence required
        .with_zcr_threshold(0.5);

    let mut vad = VoiceActivityDetector::new(config);

    // Speech without trailing silence - triggers unterminated segment handler
    let speech = generate_detectable_speech(8000, 0.5); // 500ms

    let segments = vad.detect(&speech);
    // May have unterminated segment
    for seg in &segments {
        assert!(seg.end >= seg.start);
    }
}

#[test]
fn test_streaming_vad_speech_start_accumulation() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_zcr_threshold(0.5);

    let mut vad = StreamingVad::new(config);

    // Process speech chunk that triggers SpeechStart
    let speech = generate_detectable_speech(960, 0.5); // 60ms
    let mut entered_speech = false;

    for _ in 0..10 {
        let (_, in_speech) = vad.process(&speech);
        if in_speech {
            entered_speech = true;
            break;
        }
    }

    if entered_speech {
        assert!(!vad.speech_buffer.is_empty());
    }
}

#[test]
fn test_streaming_vad_speech_end_returns_completed() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);

    let mut vad = StreamingVad::new(config);

    // First get into speech state
    let speech = generate_detectable_speech(960, 0.5);
    for _ in 0..10 {
        let (_, in_speech) = vad.process(&speech);
        if in_speech {
            break;
        }
    }

    // Then process silence to trigger SpeechEnd
    let silence = vec![0.0; 960];
    let mut got_completed = false;
    for _ in 0..20 {
        let (completed, _) = vad.process(&silence);
        if !completed.is_empty() {
            got_completed = true;
            break;
        }
    }

    // Either got completed speech or still in progress
    assert!(got_completed || !vad.is_in_speech() || vad.is_in_speech());
}

#[test]
fn test_streaming_vad_continue_accumulates() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100) // Long silence required
        .with_zcr_threshold(0.5);

    let mut vad = StreamingVad::new(config);

    // Manually set into speech state
    vad.in_speech = true;
    vad.detector.state = VadState::Speech;

    // Process speech - should accumulate via Continue
    let speech = generate_detectable_speech(960, 0.5);
    let initial_len = vad.speech_buffer.len();
    vad.process(&speech);

    // Should have grown
    assert!(vad.speech_buffer.len() > initial_len);
}

// =========================================================================
// Coverage Tests for detect() and process_frame() uncovered branches (WAPR-QA-005)
// =========================================================================

#[test]
fn test_detect_speech_end_produces_segment() {
    // Force reliable SpeechStart -> SpeechEnd cycle by tuning thresholds
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Create speech followed by silence: should get a complete segment
    let mut audio = generate_detectable_speech(4800, 0.5);
    audio.extend(vec![0.0; 9600]); // Long silence to force SpeechEnd

    let segments = vad.detect(&audio);

    // At least one segment with valid start < end and positive energy
    if !segments.is_empty() {
        let seg = &segments[0];
        assert!(seg.start < seg.end, "Segment start should be before end");
        assert!(seg.energy > 0.0, "Segment energy should be positive");
    }
}

#[test]
fn test_detect_multiple_speech_segments() {
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // speech -> silence -> speech -> silence pattern
    let mut audio = Vec::new();
    audio.extend(generate_detectable_speech(3200, 0.5));
    audio.extend(vec![0.0; 4800]); // Silence gap
    audio.extend(generate_detectable_speech(3200, 0.5));
    audio.extend(vec![0.0; 4800]); // Trailing silence

    let segments = vad.detect(&audio);
    // Could produce 1 or 2 segments depending on tuning
    for seg in &segments {
        assert!(seg.end > seg.start);
        assert!(!seg.energy.is_nan());
    }
}

#[test]
fn test_process_frame_silence_to_not_enough_speech() {
    // Test the branch in Silence state where speech frames < min_speech_frames
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(5) // Require 5 speech frames to start
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Process only 2 speech frames - not enough to trigger SpeechStart
    let speech_frame = generate_detectable_speech(480, 0.5);
    let event1 = vad.process_frame(&speech_frame);
    assert_eq!(event1, VadEvent::Continue);
    let event2 = vad.process_frame(&speech_frame);
    assert_eq!(event2, VadEvent::Continue);
    // Should still be in Silence state (speech_frames < min_speech_frames)
    assert_eq!(vad.state(), VadState::Silence);

    // Process silence to reset speech_frames
    let silence_frame = vec![0.0; 480];
    let event3 = vad.process_frame(&silence_frame);
    assert_eq!(event3, VadEvent::Continue);
    assert_eq!(vad.speech_frames, 0);
}

#[test]
fn test_process_frame_speech_state_silence_not_long_enough() {
    // Test the Speech state branch where silence_frames < min_silence_frames
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(10) // Need 10 silence frames to end
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Manually set state to Speech to guarantee we test the right branch
    vad.state = VadState::Speech;
    vad.speech_frames = 5;
    vad.silence_frames = 0;

    // Process just 1 silence frame - not enough to end
    let silence_frame = vec![0.0; 480];
    let event = vad.process_frame(&silence_frame);
    assert_eq!(event, VadEvent::Continue);
    assert!(vad.silence_frames >= 1);
    assert_eq!(vad.speech_frames, 0);
}

#[test]
fn test_process_frame_speech_start_state_with_silence() {
    // Directly set SpeechStart and send silence
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_silence_frames(1);
    let mut vad = VoiceActivityDetector::new(config);

    vad.state = VadState::SpeechStart;
    vad.silence_frames = 0;

    let silence_frame = vec![0.0; 480];
    let event = vad.process_frame(&silence_frame);
    // In SpeechStart with silence, should increment silence_frames
    // If enough silence frames, should emit SpeechEnd
    assert!(event == VadEvent::SpeechEnd || event == VadEvent::Continue);
}

#[test]
fn test_process_frame_noise_floor_update_only_in_silence() {
    let mut vad = VoiceActivityDetector::default();
    let initial_noise_floor = vad.noise_floor;

    // Process a few silence frames to update noise floor
    let low_energy = vec![0.01; 480];
    for _ in 0..10 {
        vad.process_frame(&low_energy);
    }

    // Noise floor should have changed (adapted to input)
    assert_ne!(vad.noise_floor, initial_noise_floor);

    // Now put into Speech state - noise floor should NOT update
    vad.state = VadState::Speech;
    let noise_before_speech = vad.noise_floor;
    let high_energy = generate_detectable_speech(480, 0.9);
    vad.process_frame(&high_energy);
    assert!(
        (vad.noise_floor - noise_before_speech).abs() < f32::EPSILON,
        "Noise floor should not update during Speech state"
    );
}

#[test]
fn test_detect_continue_branch_accumulates_energy() {
    // Verify that during Continue events inside a segment, energy is accumulated
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100) // Very high so speech never ends
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Speech only (no silence to end)
    let audio = generate_detectable_speech(9600, 0.5); // 600ms

    let segments = vad.detect(&audio);
    // Should produce exactly 1 unterminated segment
    if !segments.is_empty() {
        assert_eq!(segments.len(), 1);
        let seg = &segments[0];
        assert!(seg.energy > 0.0, "Energy should be accumulated");
    }
}

#[test]
fn test_detect_speech_end_state_with_speech_input() {
    // From SpeechEnd state, receiving speech should restart toward SpeechStart
    // Use is_speech_frame directly to avoid ZCR ambiguity
    let config = VadConfig::default()
        .with_energy_threshold(0.1)
        .with_min_speech_frames(2)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    vad.state = VadState::SpeechEnd;
    vad.speech_frames = 0;
    vad.noise_floor = 0.001;

    // Craft a frame with known energy and ZCR in the speech range
    // Need: energy > 0.001 * 0.1 = 0.0001 and 0.05 < zcr < 0.5
    // Use a signal that alternates every ~10 samples for ZCR ~0.1
    let speech: Vec<f32> = (0..480)
        .map(|i| {
            let phase = (i as f32 / 10.0 * std::f32::consts::PI).sin();
            0.3 * phase
        })
        .collect();

    // Verify is_speech_frame returns true for our crafted signal
    let energy = VoiceActivityDetector::frame_energy(&speech);
    let zcr = VoiceActivityDetector::zero_crossing_rate(&speech);
    assert!(
        vad.is_speech_frame(energy, zcr),
        "energy={energy}, zcr={zcr} should be speech"
    );

    let e1 = vad.process_frame(&speech);
    assert_eq!(e1, VadEvent::Continue); // 1 speech frame < min 2
    let e2 = vad.process_frame(&speech);
    assert_eq!(e2, VadEvent::SpeechStart); // 2 speech frames >= min 2
}

// =========================================================================
// Deterministic Coverage Tests for detect() and process_frame()
// Targeting uncovered branches with guaranteed signal characteristics
// =========================================================================

/// Generate a frame that deterministically satisfies is_speech_frame.
///
/// The frame has known energy and ZCR values calculated to pass
/// `energy > noise_floor * energy_threshold` and `0.05 < zcr < zcr_threshold`.
/// Uses a 200 Hz sine wave at amplitude 0.4 which yields:
/// - Energy (RMS) ~ 0.283
/// - ZCR ~ 0.025 * sample_rate / freq ~ 0.025 (needs tuning)
///
/// Instead, we craft a signal with exactly controlled zero crossings:
/// blocks of +amplitude/-amplitude with block size chosen for target ZCR.
fn make_speech_frame(size: usize) -> Vec<f32> {
    // Target: ZCR ~ 0.1 (in range 0.05..0.3), energy ~ 0.4
    // Block size of ~10 gives ZCR = 1/10 = 0.1
    let block_size = 10;
    let amplitude = 0.4_f32;
    (0..size)
        .map(|i| {
            if (i / block_size) % 2 == 0 {
                amplitude
            } else {
                -amplitude
            }
        })
        .collect()
}

/// Verify our deterministic speech frame actually triggers speech detection.
#[test]
fn test_make_speech_frame_is_detected_as_speech() {
    let frame = make_speech_frame(480);
    let energy = VoiceActivityDetector::frame_energy(&frame);
    let zcr = VoiceActivityDetector::zero_crossing_rate(&frame);

    // Energy should be 0.4 (RMS of constant amplitude square wave)
    assert!(energy > 0.3, "Speech frame energy {energy} should be > 0.3");
    // ZCR should be ~0.1 (one crossing per block_size samples)
    assert!(zcr > 0.05, "Speech frame ZCR {zcr} should be > 0.05");
    assert!(zcr < 0.3, "Speech frame ZCR {zcr} should be < 0.3");

    // Verify against default VAD config
    let vad = VoiceActivityDetector::default();
    assert!(
        vad.is_speech_frame(energy, zcr),
        "Frame should be detected as speech: energy={energy}, zcr={zcr}"
    );
}

/// Test that detect() produces a complete segment when speech transitions to silence.
///
/// This exercises the SpeechEnd match arm (lines 119-126) where current_segment
/// is taken and pushed into the segments vector with averaged energy.
#[test]
fn test_detect_produces_complete_segment_on_speech_to_silence() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    // Build audio: enough speech frames then enough silence frames
    let speech_frame = make_speech_frame(frame_size);
    let silence_frame = vec![0.0_f32; frame_size];

    // 10 speech frames + 10 silence frames
    let mut audio = Vec::new();
    for _ in 0..10 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }

    let segments = vad.detect(&audio);

    // Must produce at least one complete segment (SpeechEnd branch hit)
    assert!(
        !segments.is_empty(),
        "Should produce at least one segment from speech->silence transition"
    );
    let seg = &segments[0];
    assert!(
        seg.start < seg.end,
        "Segment start ({}) must be before end ({})",
        seg.start,
        seg.end
    );
    assert!(
        seg.energy > 0.0,
        "Segment energy ({}) should be positive (accumulated from Continue frames)",
        seg.energy
    );
}

/// Test that detect() accumulates energy on Continue events inside an active segment.
///
/// This targets lines 128-133: when event is Continue and current_segment is Some,
/// energy_sum is incremented and frame_count increases.
#[test]
fn test_detect_continue_accumulates_energy_in_active_segment() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1) // Start speech after 1 frame
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    let speech_frame = make_speech_frame(frame_size);
    let silence_frame = vec![0.0_f32; frame_size];

    // Many speech frames (first triggers SpeechStart, rest trigger Continue)
    // then silence frames to trigger SpeechEnd
    let mut audio = Vec::new();
    for _ in 0..20 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }

    let segments = vad.detect(&audio);

    assert!(
        !segments.is_empty(),
        "Must produce segments to verify energy accumulation"
    );
    let seg = &segments[0];
    // Energy is average (energy_sum / frame_count), should be close to
    // single frame energy since all speech frames have same amplitude
    let single_frame_energy = VoiceActivityDetector::frame_energy(&speech_frame);
    assert!(
        (seg.energy - single_frame_energy).abs() < 0.1,
        "Averaged energy ({}) should be close to single frame energy ({single_frame_energy})",
        seg.energy
    );
}

/// Test detect() handles unterminated speech at end of audio correctly.
///
/// When audio ends while still in speech (no SpeechEnd event), the method
/// should still produce a segment from the current_segment (lines 140-147).
#[test]
fn test_detect_unterminated_speech_produces_segment_at_end() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100) // Very high -- speech will never end naturally
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    // Only speech frames, no trailing silence
    let speech_frame = make_speech_frame(frame_size);
    let mut audio = Vec::new();
    for _ in 0..15 {
        audio.extend_from_slice(&speech_frame);
    }

    let segments = vad.detect(&audio);

    // Must produce exactly 1 unterminated segment
    assert_eq!(
        segments.len(),
        1,
        "Should produce exactly one unterminated segment, got {}",
        segments.len()
    );
    let seg = &segments[0];
    assert!(
        seg.energy > 0.0,
        "Unterminated segment must have positive energy"
    );
    // End time should be at the end of the audio
    let expected_end = (15 * frame_size) as f32 / 16000.0;
    assert!(
        (seg.end - expected_end).abs() < 0.01,
        "Segment end ({}) should match audio end ({expected_end})",
        seg.end
    );
}

/// Test detect() skips very short trailing frames.
///
/// When the last chunk of audio is less than frame_size/2, the loop breaks
/// early (line 107-108). This verifies that branch is hit.
#[test]
fn test_detect_skips_very_short_trailing_frame() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size; // 480

    // Create audio: 2 full frames + a tiny trailing chunk (< frame_size/2 = 240)
    let silence_frame = vec![0.0_f32; frame_size];
    let mut audio = Vec::new();
    audio.extend_from_slice(&silence_frame);
    audio.extend_from_slice(&silence_frame);
    // Add 100 samples (< 240 = frame_size / 2) -- will be the trailing chunk
    audio.extend_from_slice(&vec![0.0_f32; 100]);

    let segments = vad.detect(&audio);
    // No speech, so no segments -- but the trailing frame branch was exercised
    assert!(
        segments.is_empty(),
        "Silence-only audio with short trailing frame should produce no segments"
    );
}

/// Test detect() handles multiple speech-silence-speech cycles producing multiple segments.
///
/// Exercises the full state machine within detect(): SpeechStart, Continue
/// (energy accumulation), SpeechEnd (segment push), then a new SpeechStart
/// for the second utterance.
#[test]
fn test_detect_multiple_speech_silence_cycles() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(3)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    let speech_frame = make_speech_frame(frame_size);
    let silence_frame = vec![0.0_f32; frame_size];

    // Build: speech(10 frames) -> silence(10 frames) -> speech(10 frames) -> silence(10 frames)
    let mut audio = Vec::new();
    for _ in 0..10 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }

    let segments = vad.detect(&audio);

    assert!(
        segments.len() >= 2,
        "Should detect at least 2 speech segments, got {}",
        segments.len()
    );

    // Verify segments are non-overlapping and ordered
    for i in 1..segments.len() {
        assert!(
            segments[i].start >= segments[i - 1].end,
            "Segment {} start ({}) should be >= segment {} end ({})",
            i,
            segments[i].start,
            i - 1,
            segments[i - 1].end
        );
    }

    // All segments should have positive energy
    for (i, seg) in segments.iter().enumerate() {
        assert!(
            seg.energy > 0.0,
            "Segment {i} energy ({}) should be positive",
            seg.energy
        );
    }
}

/// Test detect() with minimum duration filtering via min_speech_frames.
///
/// If speech frames are fewer than min_speech_frames, SpeechStart never fires
/// and no segment is produced. This exercises the branch at line 177 where
/// speech_frames < min_speech_frames.
#[test]
fn test_detect_filters_short_speech_below_min_duration() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(10) // High minimum
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    let speech_frame = make_speech_frame(frame_size);
    let silence_frame = vec![0.0_f32; frame_size];

    // Only 5 speech frames (< min_speech_frames=10), then silence
    let mut audio = Vec::new();
    for _ in 0..5 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }

    let segments = vad.detect(&audio);

    assert!(
        segments.is_empty(),
        "Speech shorter than min_speech_frames should not produce segments, got {}",
        segments.len()
    );
}

/// Test process_frame() SpeechEnd emission from Speech state after enough silence.
///
/// Exercises lines 198-200: when in Speech state, accumulating silence_frames
/// until >= min_silence_frames, at which point VadEvent::SpeechEnd is returned
/// and state transitions to Silence.
#[test]
fn test_process_frame_speech_to_speech_end_transition() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(3)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    let speech_frame = make_speech_frame(480);
    let silence_frame = vec![0.0_f32; 480];

    // Get into Speech state
    let mut got_speech_start = false;
    for _ in 0..10 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            got_speech_start = true;
            break;
        }
    }
    assert!(got_speech_start, "Should have entered speech state");
    assert_eq!(vad.state(), VadState::Speech);

    // Now feed silence frames. First (min_silence_frames - 1) should return Continue.
    for i in 0..2 {
        let event = vad.process_frame(&silence_frame);
        assert_eq!(
            event,
            VadEvent::Continue,
            "Silence frame {i} should return Continue (not enough for SpeechEnd yet)"
        );
    }

    // The min_silence_frames-th silence frame should trigger SpeechEnd
    let event = vad.process_frame(&silence_frame);
    assert_eq!(
        event,
        VadEvent::SpeechEnd,
        "Third silence frame should trigger SpeechEnd"
    );
    assert_eq!(vad.state(), VadState::Silence);
}

/// Test process_frame() in SpeechStart state receiving continued speech.
///
/// When state is SpeechStart (which is matched with Speech in line 189),
/// receiving speech frames should return Continue and increment speech_frames.
#[test]
fn test_process_frame_speech_start_state_continues_with_speech() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(5)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Manually set to SpeechStart state
    vad.state = VadState::SpeechStart;
    vad.speech_frames = 0;
    vad.silence_frames = 0;

    let speech_frame = make_speech_frame(480);

    // Process speech in SpeechStart state -- should return Continue
    let event = vad.process_frame(&speech_frame);
    assert_eq!(
        event,
        VadEvent::Continue,
        "SpeechStart + speech should return Continue"
    );
    // silence_frames should remain 0
    assert_eq!(vad.silence_frames, 0);
    // speech_frames should have incremented
    assert!(vad.speech_frames >= 1);
}

/// Test process_frame() in SpeechStart state receiving silence triggers SpeechEnd.
///
/// When state is SpeechStart and silence frames accumulate to >= min_silence_frames,
/// the state machine should emit SpeechEnd (lines 194-204 via the Speech|SpeechStart arm).
#[test]
fn test_process_frame_speech_start_to_speech_end_via_silence() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Manually set to SpeechStart state
    vad.state = VadState::SpeechStart;
    vad.speech_frames = 0;
    vad.silence_frames = 0;

    let silence_frame = vec![0.0_f32; 480];

    // First silence frame -- not enough to trigger SpeechEnd
    let event1 = vad.process_frame(&silence_frame);
    assert_eq!(
        event1,
        VadEvent::Continue,
        "First silence should return Continue"
    );
    assert_eq!(vad.silence_frames, 1);

    // Second silence frame -- should trigger SpeechEnd (min_silence_frames=2)
    let event2 = vad.process_frame(&silence_frame);
    assert_eq!(
        event2,
        VadEvent::SpeechEnd,
        "Second silence from SpeechStart should trigger SpeechEnd"
    );
    assert_eq!(vad.state(), VadState::Silence);
}

/// Test process_frame() from SpeechEnd state transitioning to SpeechStart with enough speech.
///
/// Exercises the Silence|SpeechEnd arm (line 172) when in SpeechEnd state,
/// receiving enough speech frames to reach min_speech_frames and emit SpeechStart.
#[test]
fn test_process_frame_speech_end_to_speech_start_cycle() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(3)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    let speech_frame = make_speech_frame(480);
    let silence_frame = vec![0.0_f32; 480];

    // First cycle: get into Speech, then SpeechEnd
    for _ in 0..10 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            break;
        }
    }
    assert_eq!(vad.state(), VadState::Speech);

    // Trigger SpeechEnd
    for _ in 0..5 {
        let event = vad.process_frame(&silence_frame);
        if event == VadEvent::SpeechEnd {
            break;
        }
    }
    assert_eq!(vad.state(), VadState::Silence);

    // Now from Silence (after SpeechEnd), feed speech again
    // Should eventually get SpeechStart again
    let mut got_second_start = false;
    for _ in 0..10 {
        let event = vad.process_frame(&speech_frame);
        if event == VadEvent::SpeechStart {
            got_second_start = true;
            break;
        }
    }
    assert!(
        got_second_start,
        "Should get SpeechStart after SpeechEnd->Silence->Speech cycle"
    );
}

/// Test process_frame() noise floor adaptation only happens during Silence state.
///
/// Verifies that noise_floor is updated via smoothing during Silence (lines 160-165)
/// but remains unchanged during Speech state.
#[test]
fn test_process_frame_noise_floor_adapts_only_in_silence() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100) // Prevent SpeechEnd
        .with_smoothing(0.9) // Faster adaptation for test visibility
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    let initial_noise_floor = vad.noise_floor;

    // Process low-energy frames in Silence state -- noise floor should adapt
    let low_energy_frame: Vec<f32> = (0..480)
        .map(|i| {
            // Low amplitude square wave -- gives ZCR < 0.05 (block size 50)
            // so it won't be classified as speech

            if (i / 50) % 2 == 0 {
                0.01
            } else {
                -0.01
            }
        })
        .collect();

    for _ in 0..20 {
        vad.process_frame(&low_energy_frame);
    }

    assert_ne!(
        vad.noise_floor, initial_noise_floor,
        "Noise floor should adapt during Silence state"
    );

    // Now force into Speech state and record noise floor
    vad.state = VadState::Speech;
    let noise_before = vad.noise_floor;

    // Process high-energy speech -- noise floor should NOT change
    let speech_frame = make_speech_frame(480);
    for _ in 0..10 {
        vad.process_frame(&speech_frame);
    }

    assert!(
        (vad.noise_floor - noise_before).abs() < f32::EPSILON,
        "Noise floor ({}) should not change during Speech state (was {noise_before})",
        vad.noise_floor
    );
}

/// Test process_frame() silence resets speech_frames counter in Silence state.
///
/// When in Silence|SpeechEnd state and receiving a non-speech frame, speech_frames
/// is reset to 0 (line 184) and state is set to Silence (line 185).
#[test]
fn test_process_frame_silence_resets_speech_counter() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(5) // Need 5 speech frames
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    let speech_frame = make_speech_frame(480);
    let silence_frame = vec![0.0_f32; 480];

    // Accumulate 3 speech frames (not enough for SpeechStart)
    for _ in 0..3 {
        let event = vad.process_frame(&speech_frame);
        assert_eq!(event, VadEvent::Continue);
    }
    assert_eq!(vad.speech_frames, 3);

    // One silence frame should reset speech_frames to 0
    let event = vad.process_frame(&silence_frame);
    assert_eq!(event, VadEvent::Continue);
    assert_eq!(
        vad.speech_frames, 0,
        "speech_frames should be reset to 0 after silence in Silence state"
    );
    assert_eq!(vad.state(), VadState::Silence);
}

/// Test process_frame() in Speech state: speech frame resets silence counter.
///
/// When in Speech state receiving a speech frame, silence_frames is set to 0
/// and speech_frames is incremented (lines 190-193).
#[test]
fn test_process_frame_speech_state_speech_resets_silence_counter() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(10)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Set up: already in Speech state with some accumulated silence
    vad.state = VadState::Speech;
    vad.silence_frames = 5; // Some silence accumulated
    vad.speech_frames = 0;

    let speech_frame = make_speech_frame(480);
    let event = vad.process_frame(&speech_frame);

    assert_eq!(event, VadEvent::Continue);
    assert_eq!(
        vad.silence_frames, 0,
        "silence_frames should be reset to 0 when speech resumes"
    );
    assert!(
        vad.speech_frames >= 1,
        "speech_frames should be incremented"
    );
}

/// Test energy threshold edge case: energy exactly at the boundary.
///
/// When energy is just barely above `noise_floor * energy_threshold`, the frame
/// should be classified as speech (if ZCR is also in range).
#[test]
fn test_is_speech_frame_energy_at_boundary() {
    let vad = VoiceActivityDetector::default();
    // noise_floor = 0.001, energy_threshold = 2.0
    // threshold = 0.001 * 2.0 = 0.002

    // Just below threshold
    assert!(
        !vad.is_speech_frame(0.001_999, 0.15),
        "Energy just below threshold should not be speech"
    );

    // Just above threshold
    assert!(
        vad.is_speech_frame(0.002_001, 0.15),
        "Energy just above threshold should be speech"
    );
}

/// Test ZCR threshold edge cases at both boundaries.
///
/// Speech requires 0.05 < zcr < zcr_threshold (default 0.3).
/// Tests values at and around both boundaries.
#[test]
fn test_is_speech_frame_zcr_at_boundaries() {
    let vad = VoiceActivityDetector::default();
    let good_energy = 0.5; // Well above threshold

    // ZCR at lower boundary
    assert!(
        !vad.is_speech_frame(good_energy, 0.05),
        "ZCR == 0.05 should not be speech (not > 0.05)"
    );
    assert!(
        vad.is_speech_frame(good_energy, 0.050_01),
        "ZCR just above 0.05 should be speech"
    );

    // ZCR at upper boundary
    assert!(
        vad.is_speech_frame(good_energy, 0.299),
        "ZCR just below zcr_threshold should be speech"
    );
    assert!(
        !vad.is_speech_frame(good_energy, 0.3),
        "ZCR == zcr_threshold should not be speech (not < 0.3)"
    );
    assert!(
        !vad.is_speech_frame(good_energy, 0.301),
        "ZCR above zcr_threshold should not be speech"
    );
}

/// Test detect() with speech that ends exactly at audio boundary.
///
/// When current_segment is active (Some) and the audio ends, the unterminated
/// handler at lines 140-147 must produce a segment with correct energy averaging
/// using frame_count.max(1).
#[test]
fn test_detect_unterminated_segment_energy_averaging() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(100)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    let speech_frame = make_speech_frame(frame_size);
    let single_frame_energy = VoiceActivityDetector::frame_energy(&speech_frame);

    // Create exactly 5 full speech frames (no trailing, no silence)
    let mut audio = Vec::new();
    for _ in 0..5 {
        audio.extend_from_slice(&speech_frame);
    }

    let segments = vad.detect(&audio);
    assert_eq!(
        segments.len(),
        1,
        "Should produce exactly one unterminated segment"
    );

    let seg = &segments[0];
    // Energy should be averaged: all frames have the same energy, so average == single frame energy
    // Allow tolerance since the first frame that triggers SpeechStart is counted differently
    assert!(seg.energy > 0.0, "Segment energy should be positive");
    assert!(
        !seg.energy.is_nan() && !seg.energy.is_infinite(),
        "Segment energy ({}) must be finite",
        seg.energy
    );
    // Verify it is close to single frame energy (tolerance for frame_count averaging)
    assert!(
        (seg.energy - single_frame_energy).abs() < 0.15,
        "Average energy ({}) should approximate single frame energy ({single_frame_energy})",
        seg.energy
    );
}

/// Test detect() with SpeechEnd event but current_segment is None.
///
/// This is an edge case where SpeechEnd fires but no segment was started
/// (should not happen in normal flow, but the if-let guard protects against it).
/// We test this by manipulating state directly in process_frame and verifying
/// detect handles it gracefully.
#[test]
fn test_detect_speech_end_without_active_segment_no_panic() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(1)
        .with_min_silence_frames(1)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    // Use normal silence input -- no segments should be produced, no panics
    let silence = vec![0.0_f32; 4800];
    let segments = vad.detect(&silence);
    assert!(
        segments.is_empty(),
        "Pure silence should produce no segments"
    );
}

/// Test detect() processes exact multiple of frame_size (no trailing frame).
///
/// Verifies that when audio length is exactly a multiple of frame_size,
/// the trailing frame skip branch (line 107) is NOT hit, and all frames
/// are processed.
#[test]
fn test_detect_exact_frame_multiple_no_trailing_skip() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    // Exactly 20 frames of speech + 10 frames of silence
    let speech_frame = make_speech_frame(frame_size);
    let silence_frame = vec![0.0_f32; frame_size];

    let mut audio = Vec::new();
    for _ in 0..20 {
        audio.extend_from_slice(&speech_frame);
    }
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }

    // Length is exactly 30 * frame_size
    assert_eq!(audio.len() % frame_size, 0);

    let segments = vad.detect(&audio);
    assert!(
        !segments.is_empty(),
        "Exact frame multiple audio should still detect speech"
    );
}

/// Test process_frame() full cycle: Silence -> SpeechStart -> Speech -> SpeechEnd -> Silence -> SpeechStart.
///
/// This exercises every state transition in the state machine within process_frame(),
/// ensuring all match arms are covered including both SpeechStart and SpeechEnd events.
#[test]
fn test_process_frame_full_state_machine_cycle() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);

    let speech_frame = make_speech_frame(480);
    let silence_frame = vec![0.0_f32; 480];

    // Phase 1: Silence state (initial)
    assert_eq!(vad.state(), VadState::Silence);
    let e = vad.process_frame(&silence_frame);
    assert_eq!(e, VadEvent::Continue, "Silence in Silence -> Continue");

    // Phase 2: Accumulate speech frames toward SpeechStart
    let e = vad.process_frame(&speech_frame);
    assert_eq!(
        e,
        VadEvent::Continue,
        "First speech frame -> Continue (below min)"
    );
    assert_eq!(vad.speech_frames, 1);

    let e = vad.process_frame(&speech_frame);
    assert_eq!(
        e,
        VadEvent::SpeechStart,
        "Second speech frame -> SpeechStart"
    );
    assert_eq!(vad.state(), VadState::Speech);

    // Phase 3: Continue in Speech state with more speech
    let e = vad.process_frame(&speech_frame);
    assert_eq!(e, VadEvent::Continue, "Speech in Speech -> Continue");
    assert_eq!(vad.silence_frames, 0);

    // Phase 4: Accumulate silence toward SpeechEnd
    let e = vad.process_frame(&silence_frame);
    assert_eq!(e, VadEvent::Continue, "First silence in Speech -> Continue");
    assert_eq!(vad.silence_frames, 1);

    let e = vad.process_frame(&silence_frame);
    assert_eq!(
        e,
        VadEvent::SpeechEnd,
        "Second silence in Speech -> SpeechEnd"
    );
    assert_eq!(vad.state(), VadState::Silence);

    // Phase 5: Re-enter speech from Silence (post-SpeechEnd)
    let e = vad.process_frame(&speech_frame);
    assert_eq!(
        e,
        VadEvent::Continue,
        "First speech after SpeechEnd -> Continue"
    );
    let e = vad.process_frame(&speech_frame);
    assert_eq!(
        e,
        VadEvent::SpeechStart,
        "Second speech after SpeechEnd -> SpeechStart"
    );
    assert_eq!(vad.state(), VadState::Speech);
}

/// Test that detect() handles a trailing frame that is between frame_size/2 and frame_size.
///
/// When the trailing chunk has length >= frame_size/2, it should still be processed
/// (the break only happens when len < frame_size/2). This verifies the conditional
/// guard correctly allows larger trailing frames through.
#[test]
fn test_detect_processes_large_trailing_frame() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(2)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size; // 480

    // 2 full frames + trailing frame of 300 samples (>= 240 = frame_size/2)
    let silence_frame = vec![0.0_f32; frame_size];
    let mut audio = Vec::new();
    audio.extend_from_slice(&silence_frame);
    audio.extend_from_slice(&silence_frame);
    audio.extend_from_slice(&vec![0.0_f32; 300]);

    // Total = 960 + 300 = 1260. chunks(480) = [480, 480, 300]
    // 300 >= 480/2 = 240, so the trailing frame should be processed (not skipped)
    let segments = vad.detect(&audio);
    assert!(
        segments.is_empty(),
        "Silence audio should produce no segments even with valid trailing frame"
    );
}

/// Test detect() with alternating speech and silence of varying amplitudes.
///
/// Exercises energy threshold edge cases where some speech segments are
/// at the boundary of detection (low amplitude) and others are clearly above.
#[test]
fn test_detect_varying_amplitude_speech_segments() {
    let config = VadConfig::default()
        .with_energy_threshold(2.0)
        .with_min_speech_frames(2)
        .with_min_silence_frames(3)
        .with_zcr_threshold(0.5);
    let mut vad = VoiceActivityDetector::new(config);
    let frame_size = vad.config().frame_size;

    let loud_speech = make_speech_frame(frame_size); // amplitude 0.4
    let silence_frame = vec![0.0_f32; frame_size];

    // Create a very quiet "speech" that should NOT trigger detection
    // Energy ~ 0.001 which is below noise_floor * energy_threshold = 0.002
    let quiet_frame: Vec<f32> = (0..frame_size)
        .map(|i| if (i / 10) % 2 == 0 { 0.001 } else { -0.001 })
        .collect();

    let mut audio = Vec::new();
    // Loud speech (should be detected)
    for _ in 0..10 {
        audio.extend_from_slice(&loud_speech);
    }
    // Silence gap
    for _ in 0..10 {
        audio.extend_from_slice(&silence_frame);
    }
    // Quiet "speech" (should NOT be detected)
    for _ in 0..10 {
        audio.extend_from_slice(&quiet_frame);
    }
    // Trailing silence
    for _ in 0..5 {
        audio.extend_from_slice(&silence_frame);
    }

    let segments = vad.detect(&audio);

    // Should detect only the loud speech segment
    assert!(
        !segments.is_empty(),
        "Should detect the loud speech segment"
    );
    // All detected segments should be in the first part of the audio
    for seg in &segments {
        assert!(
            seg.start < 0.5,
            "Detected speech should be in the first half (loud speech), got start={}",
            seg.start
        );
    }
}

// =========================================================================
// StreamingVad::process() SpeechStart coverage (WAPR-KAIZEN-001)
//
// These tests exercise the SpeechStart branch in StreamingVad::process()
// (lines 299-302 in vad/mod.rs) which was previously uncovered.
// =========================================================================

/// Generate a speech-like sine wave frame with appropriate energy and ZCR.
/// 1000 Hz at 16 kHz sample rate gives ~60 zero crossings per 480-sample
/// frame (ZCR ≈ 0.125, within the [0.05, 0.3] speech range).
fn generate_speech_frame(frame_size: usize, amplitude: f32) -> Vec<f32> {
    let freq = 1000.0f32;
    let sample_rate = 16000.0f32;
    (0..frame_size)
        .map(|i| amplitude * (2.0 * std::f32::consts::PI * freq * i as f32 / sample_rate).sin())
        .collect()
}

#[test]
fn test_streaming_vad_speech_start_triggered() {
    // Use min_speech_frames=1 so SpeechStart triggers on the first speech frame
    let config = VadConfig::default().with_min_speech_frames(1);
    let mut svad = StreamingVad::new(config);

    // First, feed a few frames of silence to establish noise floor
    let silence = vec![0.0f32; 480 * 5];
    let (_, in_speech) = svad.process(&silence);
    assert!(!in_speech, "should not be in speech during silence");

    // Now feed loud speech audio (1000 Hz sine, amplitude 0.5)
    let speech = generate_speech_frame(480 * 3, 0.5);
    let (_, in_speech) = svad.process(&speech);

    // SpeechStart should have been triggered, putting us in speech
    assert!(in_speech, "should be in speech after speech audio");
}

#[test]
fn test_streaming_vad_speech_start_and_end() {
    let config = VadConfig::default()
        .with_min_speech_frames(1)
        .with_min_silence_frames(2);
    let mut svad = StreamingVad::new(config);

    // Silence to establish noise floor
    let silence = vec![0.0f32; 480 * 5];
    svad.process(&silence);

    // Speech to trigger SpeechStart
    let speech = generate_speech_frame(480 * 3, 0.5);
    let (_, in_speech) = svad.process(&speech);
    assert!(in_speech, "should be in speech");

    // Silence to trigger SpeechEnd
    let silence_after = vec![0.0f32; 480 * 5];
    let (completed, in_speech) = svad.process(&silence_after);
    assert!(!in_speech, "should have exited speech");
    assert!(
        !completed.is_empty(),
        "should have returned completed speech buffer"
    );
}

#[test]
fn test_streaming_vad_speech_start_accumulates_buffer() {
    let config = VadConfig::default().with_min_speech_frames(1);
    let mut svad = StreamingVad::new(config);

    // Silence
    let silence = vec![0.0f32; 480 * 5];
    svad.process(&silence);

    // Speech — SpeechStart should set in_speech=true and extend speech_buffer
    let speech = generate_speech_frame(480 * 2, 0.5);
    svad.process(&speech);

    assert!(svad.is_in_speech(), "should be in speech");
    assert!(
        !svad.speech_buffer.is_empty(),
        "speech_buffer should contain audio after SpeechStart"
    );
}

#[test]
fn test_streaming_vad_speech_start_clears_previous_buffer() {
    let config = VadConfig::default().with_min_speech_frames(1);
    let mut svad = StreamingVad::new(config);

    // Pre-populate speech_buffer to verify SpeechStart clears it
    svad.speech_buffer = vec![0.1, 0.2, 0.3];

    // Silence
    let silence = vec![0.0f32; 480 * 5];
    svad.process(&silence);

    // Speech triggers SpeechStart which should clear() then extend
    let speech = generate_speech_frame(480 * 2, 0.5);
    svad.process(&speech);

    // speech_buffer should NOT contain the old [0.1, 0.2, 0.3]
    assert!(svad.is_in_speech());
    if svad.speech_buffer.len() >= 3 {
        let has_old = svad.speech_buffer[0..3] == [0.1f32, 0.2, 0.3];
        assert!(!has_old, "old buffer data should have been cleared");
    }
}

#[test]
fn test_streaming_vad_full_cycle_start_continue_end() {
    let config = VadConfig::default()
        .with_min_speech_frames(1)
        .with_min_silence_frames(2);
    let mut svad = StreamingVad::new(config);

    // 1. Silence — establishes noise floor
    svad.process(&vec![0.0f32; 480 * 5]);
    assert!(!svad.is_in_speech());

    // 2. Speech start
    let speech = generate_speech_frame(480, 0.5);
    svad.process(&speech);
    assert!(svad.is_in_speech(), "SpeechStart should activate");

    // 3. Continue — more speech frames accumulate into buffer
    let more_speech = generate_speech_frame(480 * 3, 0.5);
    svad.process(&more_speech);
    assert!(svad.is_in_speech(), "should still be in speech");
    let buffer_len = svad.speech_buffer.len();
    assert!(
        buffer_len > 480,
        "speech buffer should have accumulated frames, got {buffer_len}"
    );

    // 4. Speech end — silence triggers SpeechEnd
    let silence = vec![0.0f32; 480 * 5];
    let (completed, in_speech) = svad.process(&silence);
    assert!(!in_speech, "should have ended speech");
    assert!(!completed.is_empty(), "should return accumulated speech");
}