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
//! Tests for backend selection
#![allow(clippy::ignore_without_reason, clippy::expect_used)]

use super::super::traits::MatMulOp;
use super::*;

#[test]
#[ignore]
fn test_selection_strategy_default() {
    assert_eq!(SelectionStrategy::default(), SelectionStrategy::Automatic);
}

#[test]
#[ignore]
fn test_selection_strategy_threshold() {
    let strategy = SelectionStrategy::threshold(1_000_000);
    assert!(matches!(
        strategy,
        SelectionStrategy::Threshold {
            min_flops: 1_000_000
        }
    ));
}

#[test]
#[ignore]
fn test_selection_strategy_description() {
    assert!(SelectionStrategy::PreferGpu.description().contains("GPU"));
    assert!(SelectionStrategy::PreferSimd.description().contains("SIMD"));
    assert!(SelectionStrategy::Automatic
        .description()
        .contains("automatic"));
}

#[test]
#[ignore]
fn test_selector_config_default() {
    let config = SelectorConfig::default();
    assert_eq!(config.strategy, SelectionStrategy::Automatic);
    assert!(config.gpu_threshold_flops > 0);
}

#[test]
#[ignore]
fn test_selector_config_for_inference() {
    let config = SelectorConfig::for_inference();
    assert_eq!(config.strategy, SelectionStrategy::Automatic);
    assert!(config.gpu_threshold_flops >= 1_000_000);
}

#[test]
#[ignore]
fn test_selector_config_builders() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::PreferGpu)
        .with_gpu_threshold(500_000)
        .with_max_gpu_memory(512 * 1024 * 1024);

    assert_eq!(config.strategy, SelectionStrategy::PreferGpu);
    assert_eq!(config.gpu_threshold_flops, 500_000);
    assert_eq!(config.max_gpu_memory, 512 * 1024 * 1024);
}

#[test]
#[ignore]
fn test_backend_selector_new() {
    let selector = BackendSelector::new(SelectorConfig::default());
    assert!(selector.simd_capabilities().available);
}

#[test]
#[ignore]
fn test_backend_selector_default_config() {
    let selector = BackendSelector::default_config();
    assert!(selector.simd_capabilities().available);
}

#[test]
#[ignore]
fn test_backend_selector_select_prefer_simd() {
    let selector = BackendSelector::new(SelectorConfig::prefer_simd());
    let op = MatMulOp::new(64, 128, 64);
    let selection = selector.select(&op);

    assert!(selection.is_simd());
    assert!(selection.reason.contains("PreferSimd"));
}

#[test]
#[ignore]
fn test_backend_selector_select_small_workload() {
    let selector =
        BackendSelector::new(SelectorConfig::default().with_gpu_threshold(1_000_000_000));
    let op = MatMulOp::new(8, 8, 8); // Very small
    let selection = selector.select(&op);

    // Should select SIMD for small workloads
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_backend_selector_select_threshold() {
    let selector = BackendSelector::new(
        SelectorConfig::default().with_strategy(SelectionStrategy::threshold(100)),
    );

    // Small operation - below threshold
    let small_op = MatMulOp::new(2, 2, 2);
    let selection = selector.select(&small_op);
    // Note: GPU not available in tests, so always SIMD
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_backend_selector_select_batch() {
    let selector = BackendSelector::default_config();
    let ops = vec![
        MatMulOp::new(64, 128, 64),
        MatMulOp::new(64, 128, 64),
        MatMulOp::new(64, 128, 64),
    ];
    let selection = selector.select_batch(&ops);

    // Selection should be made
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_backend_selector_select_batch_empty() {
    let selector = BackendSelector::default_config();
    let ops: Vec<MatMulOp> = vec![];
    let selection = selector.select_batch(&ops);

    assert!(selection.is_simd());
    assert!(selection.reason.contains("No operations"));
}

#[test]
#[ignore]
fn test_backend_selector_summary() {
    let selector = BackendSelector::default_config();
    let summary = selector.summary();

    assert!(summary.contains("SIMD"));
    assert!(summary.contains("parallelism"));
}

#[test]
#[ignore]
fn test_backend_selection_gpu() {
    let selection = BackendSelection::gpu("test reason");
    assert!(selection.is_gpu());
    assert!(!selection.is_simd());
    assert_eq!(selection.reason, "test reason");
}

#[test]
#[ignore]
fn test_backend_selection_simd() {
    let selection = BackendSelection::simd("test reason");
    assert!(selection.is_simd());
    assert!(!selection.is_gpu());
}

#[test]
#[ignore]
fn test_backend_selection_display() {
    let selection = BackendSelection::gpu("performance");
    let s = selection.to_string();
    assert!(s.contains("GPU"));
    assert!(s.contains("performance"));
}

// =========================================================================
// Additional Coverage Tests (WAPR-QA)
// =========================================================================

#[test]
#[ignore]
fn test_selection_strategy_threshold_description() {
    let strategy = SelectionStrategy::threshold(1_000_000);
    assert_eq!(strategy.description(), "threshold-based");
}

#[test]
#[ignore]
fn test_selector_config_prefer_gpu() {
    let config = SelectorConfig::prefer_gpu();
    assert_eq!(config.strategy, SelectionStrategy::PreferGpu);
}

#[test]
#[ignore]
fn test_selector_config_prefer_simd() {
    let config = SelectorConfig::prefer_simd();
    assert_eq!(config.strategy, SelectionStrategy::PreferSimd);
}

#[test]
#[ignore]
fn test_backend_selector_config_accessor() {
    let selector = BackendSelector::default_config();
    let config = selector.config();
    assert_eq!(config.strategy, SelectionStrategy::Automatic);
}

#[test]
#[ignore]
fn test_backend_selector_gpu_capabilities() {
    let selector = BackendSelector::default_config();
    // GPU may or may not be available, just check it doesn't panic
    let _ = selector.gpu_capabilities();
}

#[test]
#[ignore]
fn test_backend_selector_select_prefer_gpu_no_gpu() {
    let selector = BackendSelector::new(SelectorConfig::prefer_gpu());
    let op = MatMulOp::new(64, 128, 64);
    let selection = selector.select(&op);
    // Since GPU is likely not available in tests, should fall back
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_backend_selector_select_large_workload() {
    let selector = BackendSelector::new(SelectorConfig::default().with_gpu_threshold(100));
    let op = MatMulOp::new(128, 256, 128); // Large workload
    let selection = selector.select(&op);
    // Just verify selection is made
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_backend_selection_backend_type() {
    let gpu_selection = BackendSelection::gpu("test");
    assert_eq!(gpu_selection.backend, BackendType::Gpu);

    let simd_selection = BackendSelection::simd("test");
    assert_eq!(simd_selection.backend, BackendType::Simd);
}

#[test]
#[ignore]
fn test_selector_with_high_memory_requirement() {
    // Create selector with low max GPU memory
    let config = SelectorConfig::default().with_max_gpu_memory(1024);
    let selector = BackendSelector::new(config);

    // Large operation that exceeds GPU memory
    let op = MatMulOp::new(1024, 1024, 1024);
    let selection = selector.select(&op);

    // Should use SIMD due to memory constraint
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_selector_automatic_with_small_workload() {
    let selector = BackendSelector::new(SelectorConfig::default());
    let op = MatMulOp::new(4, 4, 4); // Very small
    let selection = selector.select(&op);

    // Small workload should prefer SIMD
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_selector_threshold_below_flops() {
    let selector = BackendSelector::new(
        SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1_000_000_000)),
    );
    let op = MatMulOp::new(32, 32, 32); // Below threshold
    let selection = selector.select(&op);

    // Below threshold should use SIMD
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_selection_strategy_all_descriptions() {
    assert!(!SelectionStrategy::PreferGpu.description().is_empty());
    assert!(!SelectionStrategy::PreferSimd.description().is_empty());
    assert!(!SelectionStrategy::Automatic.description().is_empty());
    assert!(!SelectionStrategy::threshold(1000).description().is_empty());
}

#[test]
#[ignore]
fn test_selector_config_with_all_builders() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::Automatic)
        .with_gpu_threshold(500_000)
        .with_max_gpu_memory(1024 * 1024 * 1024);

    assert_eq!(config.strategy, SelectionStrategy::Automatic);
    assert_eq!(config.gpu_threshold_flops, 500_000);
    assert_eq!(config.max_gpu_memory, 1024 * 1024 * 1024);
}

#[test]
#[ignore]
fn test_backend_selector_select_batch_large() {
    let selector = BackendSelector::default_config();
    let ops: Vec<MatMulOp> = (0..10).map(|_| MatMulOp::new(128, 256, 128)).collect();
    let selection = selector.select_batch(&ops);
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_backend_selector_simd_capabilities() {
    let selector = BackendSelector::default_config();
    let caps = selector.simd_capabilities();
    assert!(caps.available);
    assert!(caps.max_parallelism > 0);
}

#[test]
#[ignore]
fn test_backend_selector_gpu_available() {
    let selector = BackendSelector::default_config();
    // Just ensure method works, GPU may or may not be available
    let _available = selector.gpu_available();
}

#[test]
#[ignore]
fn test_selector_prefer_gpu_memory_exceeds_limit() {
    // Create selector with very low GPU memory limit
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(1);
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(1024, 1024, 1024); // Large memory requirement
    let selection = selector.select(&op);

    // If GPU available, should fall back to SIMD due to memory
    if selector.gpu_available() {
        assert!(
            selection.reason.contains("Memory exceeds")
                || selection.reason.contains("GPU not available")
        );
    }
}

#[test]
#[ignore]
fn test_selector_threshold_with_large_memory() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(100))
        .with_max_gpu_memory(1);
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(1024, 1024, 1024);
    let selection = selector.select(&op);

    // Should fall back to SIMD due to memory constraint
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_selector_automatic_large_workload() {
    let config = SelectorConfig::default().with_gpu_threshold(10);
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(256, 512, 256); // Large enough
    let selection = selector.select(&op);
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_selector_is_gpu_worthwhile_below_threshold() {
    let config = SelectorConfig::default().with_gpu_threshold(1_000_000_000);
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(8, 8, 8); // Small workload
    let selection = selector.select(&op);
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_selector_batch_with_varying_sizes() {
    let selector = BackendSelector::default_config();
    let ops = vec![
        MatMulOp::new(8, 8, 8),
        MatMulOp::new(128, 128, 128),
        MatMulOp::new(64, 64, 64),
    ];
    let selection = selector.select_batch(&ops);
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_selector_summary_with_different_strategies() {
    let configs = vec![
        SelectorConfig::default(),
        SelectorConfig::prefer_gpu(),
        SelectorConfig::prefer_simd(),
        SelectorConfig::for_inference(),
    ];

    for config in configs {
        let selector = BackendSelector::new(config);
        let summary = selector.summary();
        assert!(summary.contains("SIMD"));
    }
}

// =========================================================================
// GPU Path Coverage Tests (simulated GPU)
// =========================================================================

#[test]
#[ignore]
fn test_select_automatic_gpu_worthwhile() {
    let config = SelectorConfig::default().with_gpu_threshold(100);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(256, 256, 256); // Large enough workload
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

#[test]
#[ignore]
fn test_select_automatic_small_workload_with_gpu() {
    let config = SelectorConfig::default().with_gpu_threshold(1_000_000_000);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(4, 4, 4); // Tiny workload
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_select_automatic_memory_exceeds_with_gpu() {
    let config = SelectorConfig::default().with_max_gpu_memory(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds"));
}

#[test]
#[ignore]
fn test_select_prefer_gpu_with_gpu_available() {
    let config = SelectorConfig::prefer_gpu();
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(64, 128, 64);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("PreferGpu"));
}

#[test]
#[ignore]
fn test_select_prefer_gpu_memory_exceeds_with_gpu() {
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds"));
}

#[test]
#[ignore]
fn test_select_threshold_gpu_above_threshold() {
    let config = SelectorConfig::default().with_strategy(SelectionStrategy::threshold(100));
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("FLOPs exceed"));
}

#[test]
#[ignore]
fn test_select_threshold_gpu_below_threshold() {
    let config =
        SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1_000_000_000));
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(4, 4, 4);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("below threshold"));
}

#[test]
#[ignore]
fn test_select_threshold_memory_exceeds_with_gpu() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(1))
        .with_max_gpu_memory(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds"));
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_via_batch_with_gpu() {
    let config = SelectorConfig::default().with_gpu_threshold(100);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![MatMulOp::new(128, 128, 128), MatMulOp::new(128, 128, 128)];
    let selection = selector.select_batch(&ops);
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_gpu_caps_memory_limit_check() {
    // GPU with very small buffer size - can_handle should fail
    let config = SelectorConfig::default().with_gpu_threshold(1);
    let selector = BackendSelector::with_simulated_gpu(config, 64); // Very small GPU memory
    let op = MatMulOp::new(256, 256, 256); // Large memory requirement
    let selection = selector.select(&op);
    // Memory requirement exceeds GPU caps, should fall back
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_simulated_gpu_summary() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let summary = selector.summary();
    assert!(summary.contains("GPU"));
    assert!(summary.contains("f16=true"));
}

#[test]
#[ignore]
fn test_simulated_gpu_capabilities() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    assert!(selector.gpu_available());
    let gpu_caps = selector
        .gpu_capabilities()
        .expect("GPU should be available");
    assert!(gpu_caps.available);
    assert!(gpu_caps.supports_f16);
}

// =========================================================================
// Coverage Gap Tests: is_gpu_worthwhile deep paths (WAPR-QA-004)
// =========================================================================

#[test]
#[ignore]
fn test_is_gpu_worthwhile_gpu_caps_cannot_handle() {
    // Simulate GPU with very small max_buffer_size so can_handle returns false
    let config = SelectorConfig::default().with_gpu_threshold(1); // Very low threshold
    let selector = BackendSelector::with_simulated_gpu(config, 16); // Tiny GPU memory: 16 bytes
                                                                    // MatMulOp requires m*n*4 bytes of memory, 256*256*4 = 262144 > 16
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    // GPU threshold is met but can_handle should fail, falling back to SIMD
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_below_flops_threshold() {
    // Simulate GPU with high threshold so flops check fails
    let config = SelectorConfig::default().with_gpu_threshold(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(4, 4, 4); // Tiny workload
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_select_automatic_gpu_memory_boundary() {
    // Set max_gpu_memory to exactly match operation memory
    let op = MatMulOp::new(16, 16, 16);
    let mem = op.memory_requirement() as u64;
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(mem);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let selection = selector.select(&op);
    // Should use GPU since memory is exactly at limit
    assert!(selection.is_gpu() || selection.is_simd()); // Depends on can_handle
}

#[test]
#[ignore]
fn test_select_batch_gpu_worthwhile() {
    let config = SelectorConfig::default().with_gpu_threshold(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![MatMulOp::new(64, 64, 64), MatMulOp::new(64, 64, 64)];
    let selection = selector.select_batch(&ops);
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_batch_gpu_memory_too_small() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(1); // Tiny memory limit
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![MatMulOp::new(256, 256, 256)];
    let selection = selector.select_batch(&ops);
    assert!(selection.is_simd());
}

// =========================================================================
// Additional Coverage Tests (WAPR-QA-003)
// =========================================================================

#[test]
#[ignore]
fn test_selector_automatic_gpu_memory_exceeded() {
    // Automatic strategy with very low GPU memory limit
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::Automatic)
        .with_max_gpu_memory(1);
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);

    // Should fall back to SIMD due to memory
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_selector_prefer_gpu_without_gpu() {
    // PreferGpu strategy but GPU not available (simulated)
    let config = SelectorConfig::prefer_gpu();
    let selector = BackendSelector::new(config);

    // In test environment, GPU is typically not available
    if !selector.gpu_available() {
        let op = MatMulOp::new(64, 64, 64);
        let selection = selector.select(&op);
        assert!(selection.is_simd());
        assert!(selection.reason.contains("not available") || selection.reason.contains("SIMD"));
    }
}

#[test]
#[ignore]
fn test_selector_threshold_high_flops() {
    // Threshold strategy with very low threshold - should suggest GPU if available
    let config = SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1));
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);

    // Should use GPU if available, SIMD otherwise
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_selector_for_inference_config() {
    let config = SelectorConfig::for_inference();
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(8, 8, 8);
    let selection = selector.select(&op);
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_backend_selection_reason_non_empty() {
    let sel_gpu = BackendSelection::gpu("test reason");
    assert_eq!(sel_gpu.reason, "test reason");
    assert!(sel_gpu.is_gpu());
    assert!(!sel_gpu.is_simd());

    let sel_simd = BackendSelection::simd("simd reason");
    assert_eq!(sel_simd.reason, "simd reason");
    assert!(sel_simd.is_simd());
    assert!(!sel_simd.is_gpu());
}

// =========================================================================
// Coverage Tests for is_gpu_worthwhile, select_automatic, summary (WAPR-QA-005)
// =========================================================================

#[test]
#[ignore]
fn test_is_gpu_worthwhile_flops_above_threshold_caps_pass() {
    // GPU with large buffer, flops above threshold -> true
    let config = SelectorConfig::default().with_gpu_threshold(100);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(64, 64, 64);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_flops_below_threshold_returns_false() {
    // flops < gpu_threshold_flops -> false -> "Small workload"
    let config = SelectorConfig::default().with_gpu_threshold(999_999_999);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(4, 4, 4);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_caps_cannot_handle_returns_false() {
    // GPU caps with small max_buffer_size so can_handle returns false
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(1024 * 1024 * 1024); // High enough to pass memory check in select_automatic
                                                  // GPU buffer = 32 bytes (tiny), so can_handle fails for any real workload
    let selector = BackendSelector::with_simulated_gpu(config, 32);
    let op = MatMulOp::new(64, 64, 64); // Memory req: (64*64 + 64*64 + 64*64)*4 = 49152 > 32
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_select_automatic_no_gpu_available() {
    // Test select_automatic when GPU is not available
    let selector = BackendSelector::new(SelectorConfig::default());
    if !selector.gpu_available() {
        let op = MatMulOp::new(256, 256, 256);
        let selection = selector.select(&op);
        assert!(selection.is_simd());
        assert!(
            selection.reason.contains("not available")
                || selection.reason.contains("Small workload")
        );
    }
}

#[test]
#[ignore]
fn test_select_automatic_memory_exceeds_limit() {
    // Automatic with GPU, memory exceeds max_gpu_memory
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(1); // 1 byte limit
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds"));
}

#[test]
#[ignore]
fn test_select_automatic_gpu_worthwhile_true() {
    // GPU available, memory fits, workload above threshold
    let config = SelectorConfig::default()
        .with_gpu_threshold(10)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

#[test]
#[ignore]
fn test_select_automatic_gpu_worthwhile_false() {
    // GPU available, memory fits, but workload below threshold
    let config = SelectorConfig::default()
        .with_gpu_threshold(u64::MAX)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(2, 2, 2); // Tiny workload
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_summary_with_simulated_gpu_includes_gpu_line() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::with_simulated_gpu(config, 256 * 1024 * 1024);
    let summary = selector.summary();

    assert!(summary.contains("Backend Selector"));
    assert!(summary.contains("automatic"));
    assert!(summary.contains("SIMD"));
    assert!(summary.contains("GPU"));
    assert!(summary.contains("parallelism="));
    assert!(summary.contains("score="));
    assert!(summary.contains("f16="));
}

#[test]
#[ignore]
fn test_summary_without_gpu_shows_not_available() {
    let selector = BackendSelector::new(SelectorConfig::default());
    if !selector.gpu_available() {
        let summary = selector.summary();
        assert!(summary.contains("not available"));
    }
}

#[test]
#[ignore]
fn test_summary_with_different_strategy_names() {
    // PreferGpu strategy
    let config = SelectorConfig::prefer_gpu();
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let summary = selector.summary();
    assert!(summary.contains("prefer GPU"));

    // PreferSimd strategy
    let config2 = SelectorConfig::prefer_simd();
    let selector2 = BackendSelector::new(config2);
    let summary2 = selector2.summary();
    assert!(summary2.contains("prefer SIMD"));

    // Threshold strategy
    let config3 = SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1000));
    let selector3 = BackendSelector::new(config3);
    let summary3 = selector3.summary();
    assert!(summary3.contains("threshold-based"));
}

// =========================================================================
// Deep Coverage Tests: is_gpu_worthwhile, new, select, select_automatic
// (WAPR-QA-006)
// =========================================================================

// --- is_gpu_worthwhile: boundary and edge case coverage ---

#[test]
#[ignore]
fn test_is_gpu_worthwhile_zero_flops() {
    // Zero FLOPs should never be worthwhile for GPU
    let config = SelectorConfig::default().with_gpu_threshold(0);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    // Use a 0x0 matmul to generate zero flops
    let op = MatMulOp::new(0, 0, 0);
    let selection = selector.select(&op);
    // 0 flops >= 0 threshold, but memory is 0 which can_handle(0) should pass
    // The actual path depends on whether 0 >= 0 is true (it is), and can_handle(0)
    // Either GPU or SIMD is acceptable - just verify no panic
    assert!(!selection.reason.is_empty());
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_flops_exactly_at_threshold() {
    // FLOPs exactly equal to threshold: should pass the threshold check
    // MatMulOp flops = 2*m*k*n, so 2*10*5*1 = 100
    let config = SelectorConfig::default()
        .with_gpu_threshold(100)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(10, 5, 1); // 2*10*5*1 = 100 flops exactly
    let selection = selector.select(&op);
    // flops (100) >= threshold (100), so GPU worthwhile (if can_handle passes)
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_flops_one_below_threshold() {
    // FLOPs just below threshold: should NOT pass
    // We need exactly 99 flops: 2*m*k*n. But flops must be integer.
    // Use threshold=101 with a MatMulOp giving 100 flops
    let config = SelectorConfig::default()
        .with_gpu_threshold(101)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(10, 5, 1); // 2*10*5*1 = 100 flops
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[ignore]
#[test]
fn test_is_gpu_worthwhile_gpu_caps_none_not_available() {
    // Test the path where gpu_available is false and gpu_caps is None
    // This is the normal BackendSelector::new path without webgpu feature
    let selector = BackendSelector::new(SelectorConfig::default());
    // Without webgpu feature, gpu_available is false and gpu_caps is None
    assert!(!selector.gpu_available());
    assert!(selector.gpu_capabilities().is_none());

    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_gpu_can_handle_exact_boundary() {
    // GPU max_buffer_size exactly equals memory requirement
    // MatMulOp memory = (m*k + k*n + m*n) * 4
    // For 8x8x8: (64 + 64 + 64) * 4 = 768 bytes
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement();
    assert_eq!(mem, 768);

    let config = SelectorConfig::default()
        .with_gpu_threshold(1) // Low threshold so flops check passes
        .with_max_gpu_memory(1024 * 1024 * 1024); // High config memory limit
                                                  // GPU buffer exactly 768 bytes -- can_handle(768) should return true since 768 <= 768
    let selector = BackendSelector::with_simulated_gpu(config, mem as u64);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_is_gpu_worthwhile_gpu_can_handle_one_byte_short() {
    // GPU max_buffer_size is one byte less than memory requirement
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement();

    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    // GPU buffer one byte short -- can_handle should fail
    let selector = BackendSelector::with_simulated_gpu(config, (mem as u64) - 1);
    let selection = selector.select(&op);
    // is_gpu_worthwhile returns false because can_handle fails -> "Small workload"
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

// --- BackendSelector::new: exercising constructor paths ---

#[test]
#[ignore]
fn test_backend_selector_new_default_config_fields() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::new(config);

    // Verify config was stored correctly
    assert_eq!(selector.config().strategy, SelectionStrategy::Automatic);
    assert_eq!(selector.config().gpu_threshold_flops, 100_000);
    assert_eq!(selector.config().max_gpu_memory, 256 * 1024 * 1024);
    assert_eq!(selector.config().gpu_dispatch_overhead_us, 100);
}

#[test]
#[ignore]
fn test_backend_selector_new_for_inference_config() {
    let config = SelectorConfig::for_inference();
    let selector = BackendSelector::new(config);

    assert_eq!(selector.config().strategy, SelectionStrategy::Automatic);
    assert_eq!(selector.config().gpu_threshold_flops, 1_000_000);
    assert_eq!(selector.config().max_gpu_memory, 1024 * 1024 * 1024);
    assert_eq!(selector.config().gpu_dispatch_overhead_us, 50);
}

#[test]
#[ignore]
fn test_backend_selector_new_simd_caps_always_available() {
    // SIMD capabilities should always be available regardless of config
    let configs = vec![
        SelectorConfig::default(),
        SelectorConfig::prefer_gpu(),
        SelectorConfig::prefer_simd(),
        SelectorConfig::for_inference(),
    ];
    for config in configs {
        let selector = BackendSelector::new(config);
        let caps = selector.simd_capabilities();
        assert!(caps.available);
        assert_eq!(caps.backend_type, BackendType::Simd);
        assert!(caps.max_parallelism > 0);
        assert!(caps.performance_score > 0.0);
    }
}

// --- select: diverse operation types (not just MatMulOp) ---

#[test]
#[ignore]
fn test_select_with_softmax_op_prefer_simd() {
    use super::super::traits::SoftmaxOp;

    let selector = BackendSelector::new(SelectorConfig::prefer_simd());
    let op = SoftmaxOp::new(32, 512);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("PreferSimd"));
}

#[test]
#[ignore]
fn test_select_with_softmax_op_automatic_gpu() {
    use super::super::traits::SoftmaxOp;

    // SoftmaxOp flops = rows * cols * 5
    // For 1000 x 1000: 5_000_000 flops
    let config = SelectorConfig::default()
        .with_gpu_threshold(1_000)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = SoftmaxOp::new(1000, 1000);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

#[test]
#[ignore]
fn test_select_with_layer_norm_op_automatic_gpu() {
    use super::super::traits::LayerNormOp;

    // LayerNormOp flops = batch_size * hidden_size * 6
    // For 64 x 1024: 393_216 flops
    let config = SelectorConfig::default()
        .with_gpu_threshold(100_000)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = LayerNormOp::new(64, 1024);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_with_gelu_op_below_threshold() {
    use super::super::traits::GeluOp;

    // GeluOp flops = num_elements * 10
    // For 100 elements: 1000 flops
    let config = SelectorConfig::default()
        .with_gpu_threshold(10_000)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = GeluOp::new(100);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Small workload"));
}

#[test]
#[ignore]
fn test_select_with_gelu_op_above_threshold() {
    use super::super::traits::GeluOp;

    // GeluOp flops = num_elements * 10
    // For 100_000 elements: 1_000_000 flops
    let config = SelectorConfig::default()
        .with_gpu_threshold(500_000)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = GeluOp::new(100_000);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
}

// --- select: PreferGpu strategy paths with GPU available ---

#[test]
#[ignore]
fn test_select_prefer_gpu_gpu_available_memory_fits() {
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("PreferGpu strategy"));
}

#[test]
#[ignore]
fn test_select_prefer_gpu_gpu_available_memory_exceeds() {
    // PreferGpu but memory exceeds max_gpu_memory -> should fallback to SIMD
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(1); // 1 byte limit
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(64, 64, 64); // memory = (64*64 + 64*64 + 64*64)*4 = 49152
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds GPU limit"));
}

// --- select: Threshold strategy edge cases ---

#[test]
#[ignore]
fn test_select_threshold_flops_exactly_at_min_flops() {
    // Threshold with min_flops = 100, op with exactly 100 flops
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(100))
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(10, 5, 1); // 2*10*5*1 = 100 flops
    let selection = selector.select(&op);
    // flops (100) >= min_flops (100) -> GPU
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("FLOPs exceed threshold"));
}

#[test]
#[ignore]
fn test_select_threshold_flops_one_below_min_flops() {
    // Threshold with min_flops = 101, op with exactly 100 flops
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(101))
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(10, 5, 1); // 100 flops
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("below threshold"));
}

#[test]
#[ignore]
fn test_select_threshold_memory_exceeds_with_high_flops() {
    // Threshold strategy: flops exceed threshold but memory exceeds limit
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(1))
        .with_max_gpu_memory(1); // 1 byte limit
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(256, 256, 256); // High flops, high memory
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds GPU limit"));
}

#[ignore]
#[test]
fn test_select_threshold_no_gpu_available() {
    // Threshold strategy without GPU
    let config = SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1));
    let selector = BackendSelector::new(config); // No webgpu feature -> no GPU
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("GPU not available"));
}

// --- select_automatic: comprehensive path coverage ---

#[test]
#[ignore]
fn test_select_automatic_gpu_available_memory_at_exact_limit() {
    // Memory exactly equals max_gpu_memory (should pass: memory > max is checked, not >=)
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement() as u64; // 768 bytes

    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(mem); // Exact match
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let selection = selector.select(&op);
    // memory (768) > max_gpu_memory (768) is false, so passes memory check
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_automatic_gpu_available_memory_one_over_limit() {
    // Memory is one byte over max_gpu_memory
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement() as u64; // 768 bytes

    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(mem - 1); // One byte short
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let selection = selector.select(&op);
    // memory (768) > max_gpu_memory (767) is true -> SIMD
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds GPU limit"));
}

#[test]
#[ignore]
fn test_select_automatic_zero_memory_zero_flops() {
    // Edge case: zero-dimension op
    let config = SelectorConfig::default()
        .with_gpu_threshold(0)
        .with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(0, 0, 0); // Zero everything
    let selection = selector.select(&op);
    // 0 >= 0 threshold passes, memory 0 <= max passes, can_handle(0) passes
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_automatic_very_large_flops() {
    // Very large workload should definitely pick GPU
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    let op = MatMulOp::new(4096, 4096, 4096);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert!(selection.reason.contains("Large workload"));
}

// --- select_batch: additional edge cases ---

#[test]
#[ignore]
fn test_select_batch_single_op() {
    let config = SelectorConfig::default().with_gpu_threshold(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![MatMulOp::new(64, 64, 64)];
    let selection = selector.select_batch(&ops);
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_batch_mixed_op_types_via_matmul() {
    // Batch with very different sizes
    let config = SelectorConfig::default().with_gpu_threshold(1);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![
        MatMulOp::new(1, 1, 1),       // 2 flops, 12 bytes
        MatMulOp::new(512, 512, 512), // 268M flops, 3M bytes
    ];
    let selection = selector.select_batch(&ops);
    // Total flops is very large, should use GPU
    assert!(selection.is_gpu());
}

#[test]
#[ignore]
fn test_select_batch_all_tiny_ops_high_threshold() {
    let config = SelectorConfig::default().with_gpu_threshold(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![
        MatMulOp::new(1, 1, 1),
        MatMulOp::new(1, 1, 1),
        MatMulOp::new(1, 1, 1),
    ];
    let selection = selector.select_batch(&ops);
    // Total flops = 6, way below u64::MAX threshold
    assert!(selection.is_simd());
}

#[test]
#[ignore]
fn test_select_batch_memory_exceeds_for_largest_op() {
    // Batch where the largest op's memory exceeds the limit
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(100); // Very small memory limit
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let ops = vec![
        MatMulOp::new(2, 2, 2),       // Small memory
        MatMulOp::new(256, 256, 256), // Large memory exceeds 100 bytes
    ];
    let selection = selector.select_batch(&ops);
    // max_memory from batch exceeds 100 bytes -> SIMD
    assert!(selection.is_simd());
    assert!(selection.reason.contains("Memory exceeds"));
}

// --- BackendSelection: Display and accessor coverage ---

#[test]
#[ignore]
fn test_backend_selection_display_simd() {
    let selection = BackendSelection::simd("CPU is faster for small workloads");
    let display = format!("{selection}");
    assert!(display.contains("SIMD"));
    assert!(display.contains("CPU is faster for small workloads"));
}

#[test]
#[ignore]
fn test_backend_selection_clone() {
    let selection = BackendSelection::gpu("cloneable");
    let cloned = selection.clone();
    assert_eq!(cloned.backend, BackendType::Gpu);
    assert_eq!(cloned.reason, "cloneable");
}

// --- SelectorConfig: chained builder coverage ---

#[test]
#[ignore]
fn test_selector_config_chained_builders_all_fields() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(42))
        .with_gpu_threshold(999)
        .with_max_gpu_memory(12345);

    assert!(matches!(
        config.strategy,
        SelectionStrategy::Threshold { min_flops: 42 }
    ));
    assert_eq!(config.gpu_threshold_flops, 999);
    assert_eq!(config.max_gpu_memory, 12345);
}

// --- SelectionStrategy: equality and debug coverage ---

#[test]
#[ignore]
fn test_selection_strategy_clone_and_copy() {
    let s = SelectionStrategy::PreferGpu;
    let s2 = s; // Copy
    let s3 = s; // Clone
    assert_eq!(s, s2);
    assert_eq!(s, s3);
}

#[test]
#[ignore]
fn test_selection_strategy_debug_format() {
    let s = SelectionStrategy::Threshold { min_flops: 500 };
    let debug = format!("{s:?}");
    assert!(debug.contains("Threshold"));
    assert!(debug.contains("500"));
}

#[test]
#[ignore]
fn test_selection_strategy_eq_different_variants() {
    assert_ne!(SelectionStrategy::PreferGpu, SelectionStrategy::PreferSimd);
    assert_ne!(SelectionStrategy::Automatic, SelectionStrategy::PreferGpu);
    assert_ne!(
        SelectionStrategy::threshold(100),
        SelectionStrategy::threshold(200)
    );
    assert_eq!(
        SelectionStrategy::threshold(100),
        SelectionStrategy::threshold(100)
    );
}

// --- with_simulated_gpu: verify fields are set correctly ---

#[test]
#[ignore]
#[allow(clippy::expect_used)]
fn test_with_simulated_gpu_fields() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::with_simulated_gpu(config, 2048);

    assert!(selector.gpu_available());
    let gpu_caps = selector
        .gpu_capabilities()
        .expect("GPU should be available");
    assert!(gpu_caps.available);
    assert_eq!(gpu_caps.max_buffer_size, 2048);
    assert_eq!(gpu_caps.max_parallelism, 256);
    assert!(gpu_caps.supports_f16);
    assert_eq!(gpu_caps.backend_type, BackendType::Gpu);
}

// --- Integration-style tests combining multiple paths ---

#[test]
#[ignore]
fn test_select_all_strategies_same_op() {
    let op = MatMulOp::new(64, 128, 64);
    let max_mem = 1024 * 1024 * 1024_u64;

    // PreferGpu with GPU available
    let s1 = BackendSelector::with_simulated_gpu(SelectorConfig::prefer_gpu(), max_mem);
    let r1 = s1.select(&op);
    assert!(r1.is_gpu());

    // PreferSimd (always SIMD regardless)
    let s2 = BackendSelector::with_simulated_gpu(SelectorConfig::prefer_simd(), max_mem);
    let r2 = s2.select(&op);
    assert!(r2.is_simd());

    // Automatic with low threshold (should pick GPU)
    let cfg3 = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(max_mem);
    let s3 = BackendSelector::with_simulated_gpu(cfg3, max_mem);
    let r3 = s3.select(&op);
    assert!(r3.is_gpu());

    // Threshold with high threshold (should pick SIMD)
    let cfg4 = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(u64::MAX))
        .with_max_gpu_memory(max_mem);
    let s4 = BackendSelector::with_simulated_gpu(cfg4, max_mem);
    let r4 = s4.select(&op);
    assert!(r4.is_simd());
}

#[test]
#[ignore]
fn test_select_automatic_transitions_based_on_workload_size() {
    let max_mem = 1024 * 1024 * 1024_u64;
    let threshold = 50_000_u64;
    let config = SelectorConfig::default()
        .with_gpu_threshold(threshold)
        .with_max_gpu_memory(max_mem);
    let selector = BackendSelector::with_simulated_gpu(config, max_mem);

    // Small op: below threshold -> SIMD
    let small_op = MatMulOp::new(4, 4, 4); // 2*4*4*4 = 128 flops
    let sel_small = selector.select(&small_op);
    assert!(sel_small.is_simd());

    // Medium op: above threshold -> GPU
    // Need flops >= 50_000. 2*m*k*n >= 50_000 -> m*k*n >= 25_000
    // 30*30*30 = 27_000 -> 2*27_000 = 54_000 >= 50_000
    let med_op = MatMulOp::new(30, 30, 30);
    let sel_med = selector.select(&med_op);
    assert!(sel_med.is_gpu());
}

#[ignore]
#[test]
fn test_prefer_gpu_no_gpu_falls_back_to_simd_reason() {
    // Without webgpu feature, GPU is never available
    let selector = BackendSelector::new(SelectorConfig::prefer_gpu());
    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert!(selection.reason.contains("GPU not available"));
}

#[test]
#[ignore]
fn test_select_prefer_simd_ignores_gpu_even_if_available() {
    let config = SelectorConfig::prefer_simd();
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    assert!(selector.gpu_available());

    let op = MatMulOp::new(512, 512, 512); // Very large op
    let selection = selector.select(&op);
    // PreferSimd always returns SIMD regardless of GPU availability
    assert!(selection.is_simd());
    assert!(selection.reason.contains("PreferSimd strategy"));
}

// =========================================================================
// Coverage Gap Tests: is_gpu_worthwhile, new, select, select_automatic
// (WAPR-QA-007)
// =========================================================================

// --- is_gpu_worthwhile: direct path coverage via simulated GPU ---
// The function has three exit points:
//   1. flops < gpu_threshold_flops -> return false
//   2. gpu_caps.can_handle(memory) returns false -> return false
//   3. falls through -> return true
// All paths require gpu_available=true (reached only via select_automatic).

/// Exercise is_gpu_worthwhile path 1: flops below threshold returns false.
/// Verifies the early return at line 266-268.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_path_flops_below_threshold() {
    // Threshold set very high so any real op fails the flops check
    let config = SelectorConfig::default()
        .with_gpu_threshold(10_000_000)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 4x4x4 = 2*4*4*4 = 128 flops, well below 10M
    let op = MatMulOp::new(4, 4, 4);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// Exercise is_gpu_worthwhile path 2: flops pass but gpu_caps.can_handle fails.
/// The GPU has a tiny max_buffer_size so can_handle returns false (line 272-274).
#[test]
#[ignore]
fn test_is_gpu_worthwhile_path_can_handle_fails() {
    // Low threshold so flops check passes, but GPU buffer is tiny
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(u64::MAX); // config memory limit is high (passes select_automatic check)

    // GPU buffer = 8 bytes: can_handle will fail for any non-trivial op
    let selector = BackendSelector::with_simulated_gpu(config, 8);

    // MatMulOp 16x16x16: memory = (256+256+256)*4 = 3072 bytes > 8
    let op = MatMulOp::new(16, 16, 16);
    let selection = selector.select(&op);
    // flops (8192) >= 1 passes, but can_handle(3072) fails because 3072 > 8
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// Exercise is_gpu_worthwhile path 3: both checks pass, returns true.
/// Verifies the happy path at line 277.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_path_both_checks_pass() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(100)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 32x32x32: flops = 2*32*32*32 = 65536 >= 100, memory fits
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

/// Exercise is_gpu_worthwhile when gpu_caps is None but gpu_available is true.
/// This is a contrived state but tests the code path where the if-let at line 271
/// does NOT enter the Some branch, falling through to return true.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_no_gpu_caps_but_available() {
    // Manually construct a selector with gpu_available=true but gpu_caps=None
    let simd_caps = BackendCapabilities::simd();
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector {
        config,
        simd_caps,
        gpu_available: true,
        gpu_caps: None,
    };

    let op = MatMulOp::new(16, 16, 16);
    let selection = selector.select(&op);
    // gpu_available=true, memory check passes, flops >= 1 passes,
    // gpu_caps is None so if-let skipped, returns true -> GPU selected
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

// --- new: verifying constructor field assignment (no-GPU path) ---

/// Verify that new() with default config produces correct field values
/// when GPU is not available (the else branch at line 158-160).
#[ignore]
#[test]
fn test_new_no_gpu_stores_none_caps() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::new(config);

    // Without webgpu feature, GPU detection returns unavailable
    assert!(!selector.gpu_available());
    assert!(selector.gpu_capabilities().is_none());

    // SIMD caps are always populated
    let simd = selector.simd_capabilities();
    assert!(simd.available);
    assert_eq!(simd.backend_type, BackendType::Simd);
}

/// Verify new() stores the provided config verbatim.
#[test]
#[ignore]
fn test_new_stores_custom_config() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(42_000))
        .with_gpu_threshold(77_777)
        .with_max_gpu_memory(99_999);
    let selector = BackendSelector::new(config);

    let stored = selector.config();
    assert!(matches!(
        stored.strategy,
        SelectionStrategy::Threshold { min_flops: 42_000 }
    ));
    assert_eq!(stored.gpu_threshold_flops, 77_777);
    assert_eq!(stored.max_gpu_memory, 99_999);
}

/// Verify new() with prefer_gpu config still results in no GPU
/// when webgpu feature is disabled.
#[ignore]
#[test]
fn test_new_prefer_gpu_config_no_webgpu() {
    let selector = BackendSelector::new(SelectorConfig::prefer_gpu());

    // Config stored correctly
    assert_eq!(selector.config().strategy, SelectionStrategy::PreferGpu);

    // But GPU is not available
    assert!(!selector.gpu_available());
    assert!(selector.gpu_capabilities().is_none());

    // Selecting should fall back to SIMD
    let op = MatMulOp::new(64, 64, 64);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "GPU not available");
}

/// Verify new() with for_inference config stores all fields correctly.
#[test]
#[ignore]
fn test_new_for_inference_fields() {
    let selector = BackendSelector::new(SelectorConfig::for_inference());

    let config = selector.config();
    assert_eq!(config.strategy, SelectionStrategy::Automatic);
    assert_eq!(config.gpu_threshold_flops, 1_000_000);
    assert_eq!(config.max_gpu_memory, 1024 * 1024 * 1024);
    assert_eq!(config.gpu_dispatch_overhead_us, 50);

    // SIMD always available
    assert!(selector.simd_capabilities().available);
}

// --- select: exercising all four match arms with GPU available ---

/// PreferGpu with GPU available and memory fits -> GPU selected (line 208).
#[test]
#[ignore]
fn test_select_prefer_gpu_arm_gpu_available_memory_ok() {
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(1024 * 1024 * 1024);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    let op = MatMulOp::new(16, 16, 16);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "PreferGpu strategy");
}

/// PreferGpu with GPU available but memory exceeds limit -> SIMD (line 210).
#[test]
#[ignore]
fn test_select_prefer_gpu_arm_memory_exceeds() {
    let config = SelectorConfig::prefer_gpu().with_max_gpu_memory(4);
    let selector = BackendSelector::with_simulated_gpu(config, 1024 * 1024 * 1024);
    // MatMulOp 32x32x32: memory = (1024+1024+1024)*4 = 12288 > 4
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Memory exceeds GPU limit");
}

/// PreferGpu without GPU -> SIMD (line 212).
#[ignore]
#[test]
fn test_select_prefer_gpu_arm_no_gpu() {
    let selector = BackendSelector::new(SelectorConfig::prefer_gpu());
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "GPU not available");
}

/// PreferSimd always returns SIMD regardless (line 216).
#[test]
#[ignore]
fn test_select_prefer_simd_arm_always_simd() {
    let config = SelectorConfig::prefer_simd();
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    let op = MatMulOp::new(512, 512, 512);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "PreferSimd strategy");
}

/// Threshold with GPU available, flops above min -> GPU (line 228).
#[test]
#[ignore]
fn test_select_threshold_arm_above_min_flops() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(500))
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    // MatMulOp 16x16x16: flops = 2*16*16*16 = 8192 >= 500
    let op = MatMulOp::new(16, 16, 16);
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "FLOPs exceed threshold");
}

/// Threshold with GPU available, flops below min -> SIMD (line 230).
#[test]
#[ignore]
fn test_select_threshold_arm_below_min_flops() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(100_000))
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    // MatMulOp 4x4x4: flops = 128 < 100_000
    let op = MatMulOp::new(4, 4, 4);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "FLOPs below threshold");
}

/// Threshold without GPU -> early return SIMD (line 220).
#[ignore]
#[test]
fn test_select_threshold_arm_no_gpu() {
    let config = SelectorConfig::default().with_strategy(SelectionStrategy::threshold(1));
    let selector = BackendSelector::new(config);
    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "GPU not available");
}

/// Threshold with GPU but memory exceeds limit -> SIMD (line 224).
#[test]
#[ignore]
fn test_select_threshold_arm_memory_exceeds() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(1))
        .with_max_gpu_memory(4);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    let op = MatMulOp::new(64, 64, 64);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Memory exceeds GPU limit");
}

/// Automatic strategy delegates to select_automatic (line 234).
#[test]
#[ignore]
fn test_select_automatic_arm_delegates() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    // select_automatic -> is_gpu_worthwhile -> true -> GPU
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

// --- select_automatic: all three branches ---

/// select_automatic: GPU not available -> early return SIMD (line 241-243).
#[ignore]
#[test]
fn test_select_automatic_no_gpu_early_return() {
    let selector = BackendSelector::new(SelectorConfig::default());
    // Confirm no GPU
    assert!(!selector.gpu_available());

    let op = MatMulOp::new(256, 256, 256);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "GPU not available");
}

/// select_automatic: GPU available, memory exceeds limit -> SIMD (line 246-248).
#[test]
#[ignore]
fn test_select_automatic_memory_exceeds_early_return() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(16); // Very small limit
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 32x32x32: memory = (1024+1024+1024)*4 = 12288 > 16
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Memory exceeds GPU limit");
}

/// select_automatic: GPU available, memory ok, worthwhile=true -> GPU (line 253-254).
#[test]
#[ignore]
fn test_select_automatic_gpu_worthwhile_returns_gpu() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(50)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let op = MatMulOp::new(16, 16, 16); // flops=8192 >= 50
    let selection = selector.select(&op);
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

/// select_automatic: GPU available, memory ok, worthwhile=false -> SIMD (line 255-256).
#[test]
#[ignore]
fn test_select_automatic_gpu_not_worthwhile_returns_simd() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1_000_000)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let op = MatMulOp::new(4, 4, 4); // flops=128 < 1_000_000
    let selection = selector.select(&op);
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// select_automatic: GPU available, memory exactly at boundary (equal) -> passes (line 246).
/// The check is `memory > max_gpu_memory`, so equal should pass.
#[test]
#[ignore]
fn test_select_automatic_memory_at_exact_boundary() {
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement() as u64; // 768

    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(mem); // exactly 768
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let selection = selector.select(&op);
    // 768 > 768 is false, so memory check passes -> proceeds to is_gpu_worthwhile
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

/// select_automatic: GPU available, memory one byte over boundary -> fails (line 246-248).
#[test]
#[ignore]
fn test_select_automatic_memory_one_over_boundary() {
    let op = MatMulOp::new(8, 8, 8);
    let mem = op.memory_requirement() as u64; // 768

    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(mem - 1); // 767
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let selection = selector.select(&op);
    // 768 > 767 is true -> memory exceeds
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Memory exceeds GPU limit");
}

// =========================================================================
// Coverage Gap Tests: is_gpu_worthwhile and select_automatic
// direct invocation paths (WAPR-QA-008)
//
// These tests exercise is_gpu_worthwhile and select_automatic through
// the select() method with Automatic strategy, using with_simulated_gpu
// to ensure GPU is available. Tests are structured to guarantee that
// each branch within is_gpu_worthwhile and select_automatic is exercised.
// =========================================================================

/// Directly exercise is_gpu_worthwhile returning true (both checks pass):
/// flops >= threshold AND gpu_caps.can_handle(memory) == true.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_returns_true_all_checks_pass() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(500)
        .with_max_gpu_memory(u64::MAX);
    // Large GPU buffer so can_handle passes
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 32x32x32: flops = 2*32*32*32 = 65536 >= 500
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);

    // select_automatic -> is_gpu_worthwhile returns true -> GPU
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

/// Exercise is_gpu_worthwhile returning false via flops < threshold.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_returns_false_flops_below() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1_000_000)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 2x2x2: flops = 16 < 1_000_000
    let op = MatMulOp::new(2, 2, 2);
    let selection = selector.select(&op);

    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// Exercise is_gpu_worthwhile returning false via can_handle failure.
/// flops pass the threshold but GPU buffer is too small.
#[test]
#[ignore]
fn test_is_gpu_worthwhile_returns_false_can_handle_fails() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1) // Very low, so flops always pass
        .with_max_gpu_memory(u64::MAX); // Config memory limit is high
                                        // GPU buffer = 4 bytes - too small for any non-trivial op
    let selector = BackendSelector::with_simulated_gpu(config, 4);

    // MatMulOp 16x16x16: memory = 3072 bytes > 4
    let op = MatMulOp::new(16, 16, 16);
    let selection = selector.select(&op);

    // flops pass threshold but can_handle(3072) fails -> false -> SIMD
    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// Exercise select_automatic path 1: GPU not available -> SIMD.
#[ignore]
#[test]
fn test_select_automatic_path_gpu_not_available() {
    // Standard constructor without webgpu feature -> no GPU
    let config = SelectorConfig::default();
    let selector = BackendSelector::new(config);

    let op = MatMulOp::new(128, 128, 128);
    let selection = selector.select(&op);

    assert!(selection.is_simd());
    assert_eq!(selection.reason, "GPU not available");
}

/// Exercise select_automatic path 2: GPU available but memory exceeds limit.
#[test]
#[ignore]
fn test_select_automatic_path_memory_exceeds() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(1)
        .with_max_gpu_memory(8); // Very small limit
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    // MatMulOp 32x32x32: memory = 12288 > 8
    let op = MatMulOp::new(32, 32, 32);
    let selection = selector.select(&op);

    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Memory exceeds GPU limit");
}

/// Exercise select_automatic path 3: worthwhile true -> GPU.
#[test]
#[ignore]
fn test_select_automatic_path_gpu_worthwhile() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(100)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let op = MatMulOp::new(64, 64, 64); // flops = 524288 >= 100
    let selection = selector.select(&op);

    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

/// Exercise select_automatic path 4: worthwhile false -> SIMD.
#[test]
#[ignore]
fn test_select_automatic_path_gpu_not_worthwhile() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(10_000_000) // High threshold
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let op = MatMulOp::new(4, 4, 4); // flops = 128 < 10M
    let selection = selector.select(&op);

    assert!(selection.is_simd());
    assert_eq!(selection.reason, "Small workload better on CPU");
}

/// Test select_batch exercises select_automatic when GPU is available.
#[test]
#[ignore]
fn test_select_batch_exercises_select_automatic_gpu() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(100)
        .with_max_gpu_memory(u64::MAX);
    let selector = BackendSelector::with_simulated_gpu(config, u64::MAX);

    let ops = vec![MatMulOp::new(32, 32, 32), MatMulOp::new(64, 64, 64)];
    let selection = selector.select_batch(&ops);

    // Total flops well above threshold
    assert!(selection.is_gpu());
    assert_eq!(selection.reason, "Large workload benefits from GPU");
}

// =========================================================================
// BackendSelector::new() coverage (WAPR-QA-009)
//
// new() at line 144 has 57.1% coverage because the GPU-available path
// (lines 148-157) is never exercised in tests without the webgpu feature.
// These tests cover the remaining paths by:
// 1. Verifying the no-GPU path produces expected field values
// 2. Testing with_simulated_gpu to exercise equivalent GPU logic
// 3. Validating all constructor field assignments
// =========================================================================

/// Verify new() GPU detection path: without webgpu, detect_gpu returns
/// unavailable, so gpu_caps is None and gpu_available is false.
/// This covers lines 146 (simd_caps), 147 (gpu_result), 158-160 (else branch).
#[ignore]
#[test]
fn test_new_covers_no_gpu_detection_path() {
    let config = SelectorConfig::default();
    let selector = BackendSelector::new(config);

    // Line 146: simd_caps populated
    let simd = selector.simd_capabilities();
    assert!(simd.available);
    assert!(simd.max_parallelism > 0);
    assert!(simd.performance_score > 0.0);

    // Lines 158-160: gpu_result.available is false
    assert!(!selector.gpu_available());
    assert!(selector.gpu_capabilities().is_none());

    // Line 162-167: Self struct fields
    assert_eq!(selector.config().strategy, SelectionStrategy::Automatic);
}

/// Verify new() stores all config fields correctly with each
/// SelectorConfig constructor variant.
#[test]
#[ignore]
fn test_new_all_config_constructors() {
    // Default
    let s1 = BackendSelector::new(SelectorConfig::default());
    assert_eq!(s1.config().strategy, SelectionStrategy::Automatic);
    assert_eq!(s1.config().gpu_threshold_flops, 100_000);

    // For inference
    let s2 = BackendSelector::new(SelectorConfig::for_inference());
    assert_eq!(s2.config().strategy, SelectionStrategy::Automatic);
    assert_eq!(s2.config().gpu_threshold_flops, 1_000_000);
    assert_eq!(s2.config().gpu_dispatch_overhead_us, 50);

    // Prefer GPU
    let s3 = BackendSelector::new(SelectorConfig::prefer_gpu());
    assert_eq!(s3.config().strategy, SelectionStrategy::PreferGpu);

    // Prefer SIMD
    let s4 = BackendSelector::new(SelectorConfig::prefer_simd());
    assert_eq!(s4.config().strategy, SelectionStrategy::PreferSimd);

    // Threshold
    let s5 = BackendSelector::new(
        SelectorConfig::default().with_strategy(SelectionStrategy::threshold(42)),
    );
    assert!(matches!(
        s5.config().strategy,
        SelectionStrategy::Threshold { min_flops: 42 }
    ));
}

/// Verify with_simulated_gpu exercises the GPU-available path equivalent,
/// setting gpu_caps to Some and gpu_available to true (analogous to
/// lines 148-157 in new() when detect_gpu returns available).
#[test]
#[ignore]
#[allow(clippy::expect_used)]
fn test_simulated_gpu_covers_gpu_caps_construction() {
    let config = SelectorConfig::for_inference();
    let buffer_size = 512 * 1024 * 1024_u64;
    let selector = BackendSelector::with_simulated_gpu(config, buffer_size);

    // gpu_available = true (line 165 equivalent)
    assert!(selector.gpu_available());

    // gpu_caps = Some(...) (lines 148-157 equivalent)
    let gpu = selector
        .gpu_capabilities()
        .expect("GPU caps should be present");
    assert!(gpu.available);
    assert_eq!(gpu.max_buffer_size, buffer_size);
    assert!(gpu.supports_f16);
    assert_eq!(gpu.backend_type, BackendType::Gpu);

    // simd_caps always populated (line 145)
    assert!(selector.simd_capabilities().available);

    // Config stored correctly (line 162)
    assert_eq!(selector.config().strategy, SelectionStrategy::Automatic);
    assert_eq!(selector.config().gpu_threshold_flops, 1_000_000);
}

/// Verify that new() with custom config preserves all builder-set values
/// and the GPU detection doesn't corrupt them.
#[test]
#[ignore]
fn test_new_preserves_custom_config_after_gpu_detection() {
    let config = SelectorConfig::default()
        .with_strategy(SelectionStrategy::threshold(99_999))
        .with_gpu_threshold(12_345)
        .with_max_gpu_memory(67_890);

    let selector = BackendSelector::new(config);

    // Config should be preserved exactly as set, even after GPU detection
    let stored = selector.config();
    assert!(matches!(
        stored.strategy,
        SelectionStrategy::Threshold { min_flops: 99_999 }
    ));
    assert_eq!(stored.gpu_threshold_flops, 12_345);
    assert_eq!(stored.max_gpu_memory, 67_890);
}

/// Test new() with a config that has zero thresholds to exercise edge cases.
#[test]
#[ignore]
fn test_new_zero_threshold_config() {
    let config = SelectorConfig::default()
        .with_gpu_threshold(0)
        .with_max_gpu_memory(0);

    let selector = BackendSelector::new(config);

    assert_eq!(selector.config().gpu_threshold_flops, 0);
    assert_eq!(selector.config().max_gpu_memory, 0);

    // Even with zero memory limit, SIMD should still work
    let op = MatMulOp::new(4, 4, 4);
    let selection = selector.select(&op);
    assert!(!selection.reason.is_empty());
}

/// Test new() followed by summary() to exercise the full constructor
/// and display path.
#[test]
#[ignore]
fn test_new_then_summary_exercises_constructor() {
    let selector = BackendSelector::new(SelectorConfig::default());
    let summary = selector.summary();

    // Summary should include strategy description
    assert!(summary.contains("automatic"));
    assert!(summary.contains("SIMD"));
    // Without GPU, should show "not available"
    if !selector.gpu_available() {
        assert!(summary.contains("not available"));
    }
}