tldr-core 0.1.6

Core analysis engine for TLDR code analysis tool
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
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
//! Dataflow Analysis Tests
//!
//! Comprehensive test suite for Available Expressions and Abstract Interpretation
//! analyses as defined in spec.md. These tests define expected behavior for:
//!
//! ## Available Expressions (32 tests) - CAP-AE-01 through CAP-AE-12
//! 1. Expression struct: frozen, hashable, equality, is_killed_by
//! 2. AvailableExprsInfo: fields, is_available, is_available_at_exit, to_dict
//! 3. MUST analysis: diamond patterns, entry block, kill semantics
//! 4. redundant_computations: CSE detection, no false positives
//! 5. CFG patterns: linear, loop, unreachable, self-loop
//! 6. Edge cases: empty function, multiple expressions
//!
//! ## Abstract Interpretation (56 tests) - CAP-AI-01 through CAP-AI-22
//! 1. Nullability: enum values
//! 2. AbstractValue: fields, hashable, frozen, top, bottom, from_constant
//! 3. may_be_zero, may_be_null, is_constant
//! 4. AbstractState: empty, get, set, copy, equality
//! 5. AbstractInterpInfo: value_at, value_at_exit, range_at, type_at
//! 6. Compute: constants, arithmetic, variable copy, None assignment
//! 7. Join: range union, constant disagreement
//! 8. Widening: loop upper bound
//! 9. Division-by-zero: detected for 0, range including 0, safe cases
//! 10. Null dereference: detected at attribute access, safe for non-null
//! 11. Edge cases: empty function, unknown RHS, parameter starts as top
//! 12. Multi-language: Python None, TS null/undefined, Go nil, Rust no null
//!
//! Most mock tests pass directly. Tests for compute functions that require
//! CFG/DFG integration are implemented in Section 19.
//! Reference: spec.md, gap-analysis/session13-available-abstract-gap.yaml

use std::collections::hash_map::DefaultHasher;
use std::collections::{HashMap, HashSet};
use std::f64::consts::PI;
use std::hash::{Hash, Hasher};

// =============================================================================
// Type Imports (will be enabled when types are implemented)
// =============================================================================

// Phase 1: Available Expressions types (to be implemented)
// use super::{
//     Expression, AvailableExprsInfo, normalize_expression,
//     compute_available_exprs, COMMUTATIVE_OPS,
// };

// Phase 2: Abstract Interpretation types (to be implemented)
// use super::{
//     Nullability, AbstractValue, ConstantValue, AbstractState,
//     AbstractInterpInfo, compute_abstract_interp,
// };

// Phase 3: CFG/DFG types (existing)
// use crate::types::{CfgInfo, CfgBlock, CfgEdge, VarRef, RefType, Language};
// use crate::cfg::get_cfg_context;
// use crate::dfg::get_dfg_context;

// =============================================================================
// Mock Types for Available Expressions
// =============================================================================

/// Mock Expression for testing (mirrors spec CAP-AE-01)
///
/// Immutable and hashable by text only (line-independent equality).
#[derive(Debug, Clone)]
pub struct MockExpression {
    /// Normalized expression string (e.g., "a + b")
    pub text: String,
    /// Variables used in this expression (sorted for consistency)
    pub operands: Vec<String>,
    /// Line where expression first appears
    pub line: u32,
}

impl PartialEq for MockExpression {
    fn eq(&self, other: &Self) -> bool {
        // CAP-AE-01: Equality based on text only
        self.text == other.text
    }
}

impl Eq for MockExpression {}

impl Hash for MockExpression {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // CAP-AE-01: Hash based on text only
        self.text.hash(state);
    }
}

impl MockExpression {
    pub fn new(text: &str, operands: Vec<&str>, line: u32) -> Self {
        let mut ops: Vec<String> = operands.iter().map(|s| s.to_string()).collect();
        ops.sort();
        Self {
            text: text.to_string(),
            operands: ops,
            line,
        }
    }

    /// CAP-AE-01: Check if redefining var kills this expression
    pub fn is_killed_by(&self, var: &str) -> bool {
        self.operands.iter().any(|op| op == var)
    }

    /// Convert to JSON-serializable format
    pub fn to_json_value(&self) -> serde_json::Value {
        serde_json::json!({
            "text": self.text,
            "operands": self.operands,
            "line": self.line,
        })
    }
}

/// CAP-AE-02: Commutative operators for normalization
pub const COMMUTATIVE_OPS: &[&str] = &["+", "*", "==", "!=", "and", "or", "&", "|", "^"];

/// CAP-AE-02: Normalize binary expression to canonical form
pub fn normalize_expression(op: &str, left: &str, right: &str) -> String {
    if COMMUTATIVE_OPS.contains(&op) {
        let mut operands = [left.trim(), right.trim()];
        operands.sort();
        format!("{} {} {}", operands[0], op, operands[1])
    } else {
        format!("{} {} {}", left.trim(), op, right.trim())
    }
}

/// Mock AvailableExprsInfo for testing (mirrors spec CAP-AE-08 through CAP-AE-11)
#[derive(Debug, Clone, Default)]
pub struct MockAvailableExprsInfo {
    /// Expressions available at block entry
    pub avail_in: HashMap<u32, HashSet<MockExpression>>,
    /// Expressions available at block exit
    pub avail_out: HashMap<u32, HashSet<MockExpression>>,
    /// All unique expressions found in the function
    pub all_exprs: HashSet<MockExpression>,
    /// Entry block ID
    pub entry_block: u32,
    /// All expression instances including duplicates (for CSE detection)
    pub expr_instances: Vec<MockExpression>,
}

impl MockAvailableExprsInfo {
    pub fn new(entry_block: u32) -> Self {
        Self {
            entry_block,
            ..Default::default()
        }
    }

    /// CAP-AE-08: Check if expression is available at entry to block
    pub fn is_available(&self, block: u32, expr: &MockExpression) -> bool {
        self.avail_in
            .get(&block)
            .is_some_and(|set| set.contains(expr))
    }

    /// CAP-AE-09: Check if expression is available at exit of block
    pub fn is_available_at_exit(&self, block: u32, expr: &MockExpression) -> bool {
        self.avail_out
            .get(&block)
            .is_some_and(|set| set.contains(expr))
    }

    /// CAP-AE-06: Find expressions computed when already available (CSE opportunities)
    /// Returns: Vec<(expr_text, original_line, redundant_line)>
    pub fn redundant_computations(&self) -> Vec<(String, u32, u32)> {
        let mut redundant = Vec::new();
        let mut seen: HashMap<String, u32> = HashMap::new();

        for expr in &self.expr_instances {
            if let Some(&first_line) = seen.get(&expr.text) {
                // Check if this computation is redundant (expression was available)
                // For now, simple implementation: if seen before and available
                redundant.push((expr.text.clone(), first_line, expr.line));
            } else {
                seen.insert(expr.text.clone(), expr.line);
            }
        }

        redundant.sort_by_key(|(_, _, line)| *line);
        redundant
    }

    /// CAP-AE-11: Serialize to JSON-compatible structure
    pub fn to_json_value(&self) -> serde_json::Value {
        let avail_in: HashMap<String, Vec<serde_json::Value>> = self
            .avail_in
            .iter()
            .map(|(k, v)| {
                let exprs: Vec<_> = v.iter().map(|e| e.to_json_value()).collect();
                (k.to_string(), exprs)
            })
            .collect();

        let avail_out: HashMap<String, Vec<serde_json::Value>> = self
            .avail_out
            .iter()
            .map(|(k, v)| {
                let exprs: Vec<_> = v.iter().map(|e| e.to_json_value()).collect();
                (k.to_string(), exprs)
            })
            .collect();

        let all_exprs: Vec<_> = self.all_exprs.iter().map(|e| e.to_json_value()).collect();

        let redundant: Vec<_> = self
            .redundant_computations()
            .iter()
            .map(|(expr, first, redundant)| {
                serde_json::json!({
                    "expr": expr,
                    "first_at": first,
                    "redundant_at": redundant,
                })
            })
            .collect();

        serde_json::json!({
            "avail_in": avail_in,
            "avail_out": avail_out,
            "all_expressions": all_exprs,
            "entry_block": self.entry_block,
            "redundant_computations": redundant,
        })
    }
}

// =============================================================================
// Mock Types for Abstract Interpretation
// =============================================================================

/// CAP-AI-01: Nullability lattice
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum MockNullability {
    /// Definitely not null
    Never,
    /// Could be null or non-null (default/unknown)
    #[default]
    Maybe,
    /// Definitely null
    Always,
}

impl MockNullability {
    pub fn as_str(&self) -> &'static str {
        match self {
            MockNullability::Never => "never",
            MockNullability::Maybe => "maybe",
            MockNullability::Always => "always",
        }
    }
}

/// Constant values that can be tracked (CAP-AI-03)
#[derive(Debug, Clone, PartialEq)]
pub enum MockConstantValue {
    Int(i64),
    Float(f64),
    String(String),
    Bool(bool),
    Null,
}

impl MockConstantValue {
    pub fn to_json_value(&self) -> serde_json::Value {
        match self {
            MockConstantValue::Int(v) => serde_json::json!(v),
            MockConstantValue::Float(v) => serde_json::json!(v),
            MockConstantValue::String(v) => serde_json::json!(v),
            MockConstantValue::Bool(v) => serde_json::json!(v),
            MockConstantValue::Null => serde_json::Value::Null,
        }
    }
}

/// CAP-AI-02 through CAP-AI-06: AbstractValue
#[derive(Debug, Clone, PartialEq)]
pub struct MockAbstractValue {
    /// Inferred type name (e.g., "int", "str") or None if unknown
    pub type_: Option<String>,
    /// Value range [min, max] for numerics. None bounds mean infinity.
    pub range_: Option<(Option<i64>, Option<i64>)>,
    /// Nullability status
    pub nullable: MockNullability,
    /// Known constant value
    pub constant: Option<MockConstantValue>,
}

impl Eq for MockAbstractValue {}

impl Hash for MockAbstractValue {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.type_.hash(state);
        // Note: range and nullable contribute to hash, constant doesn't (per spec)
        self.range_.hash(state);
        self.nullable.hash(state);
    }
}

impl MockAbstractValue {
    /// CAP-AI-04: Top of lattice - no information known
    pub fn top() -> Self {
        MockAbstractValue {
            type_: None,
            range_: None,
            nullable: MockNullability::Maybe,
            constant: None,
        }
    }

    /// CAP-AI-04: Bottom of lattice - contradiction (unreachable)
    pub fn bottom() -> Self {
        MockAbstractValue {
            type_: Some("<bottom>".to_string()),
            range_: Some((None, None)),
            nullable: MockNullability::Never,
            constant: None,
        }
    }

    /// CAP-AI-03: Create from known constant
    pub fn from_constant(value: MockConstantValue) -> Self {
        match value {
            MockConstantValue::Int(v) => MockAbstractValue {
                type_: Some("int".to_string()),
                range_: Some((Some(v), Some(v))),
                nullable: MockNullability::Never,
                constant: Some(MockConstantValue::Int(v)),
            },
            MockConstantValue::Float(v) => MockAbstractValue {
                type_: Some("float".to_string()),
                range_: None,
                nullable: MockNullability::Never,
                constant: Some(MockConstantValue::Float(v)),
            },
            MockConstantValue::String(ref s) => MockAbstractValue {
                type_: Some("str".to_string()),
                // CAP-AI-18: Track string length in range
                range_: Some((Some(s.len() as i64), Some(s.len() as i64))),
                nullable: MockNullability::Never,
                constant: Some(value),
            },
            MockConstantValue::Bool(v) => MockAbstractValue {
                type_: Some("bool".to_string()),
                range_: Some((Some(v as i64), Some(v as i64))),
                nullable: MockNullability::Never,
                constant: Some(MockConstantValue::Bool(v)),
            },
            MockConstantValue::Null => MockAbstractValue {
                type_: Some("NoneType".to_string()),
                range_: None,
                nullable: MockNullability::Always,
                constant: None,
            },
        }
    }

    /// CAP-AI-05: Check if value could be zero (for division check)
    pub fn may_be_zero(&self) -> bool {
        match &self.range_ {
            None => true, // Unknown range, conservatively true
            Some((low, high)) => {
                let low = low.unwrap_or(i64::MIN);
                let high = high.unwrap_or(i64::MAX);
                low <= 0 && 0 <= high
            }
        }
    }

    /// CAP-AI-06: Check if value could be null/None
    pub fn may_be_null(&self) -> bool {
        self.nullable != MockNullability::Never
    }

    /// Check if this is a known constant value
    pub fn is_constant(&self) -> bool {
        self.constant.is_some()
    }

    /// Convert to JSON-serializable format
    pub fn to_json_value(&self) -> serde_json::Value {
        let mut obj = serde_json::Map::new();

        if let Some(ref t) = self.type_ {
            obj.insert("type".to_string(), serde_json::json!(t));
        }

        if let Some((low, high)) = &self.range_ {
            let range = serde_json::json!([low, high]);
            obj.insert("range".to_string(), range);
        }

        obj.insert(
            "nullable".to_string(),
            serde_json::json!(self.nullable.as_str()),
        );

        if let Some(ref c) = self.constant {
            obj.insert("constant".to_string(), c.to_json_value());
        }

        serde_json::Value::Object(obj)
    }
}

/// CAP-AI-07: AbstractState - mapping from variables to abstract values
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MockAbstractState {
    pub values: HashMap<String, MockAbstractValue>,
}

impl MockAbstractState {
    pub fn new() -> Self {
        Self::default()
    }

    /// Get abstract value for variable, defaulting to top (unknown)
    pub fn get(&self, var: &str) -> MockAbstractValue {
        self.values
            .get(var)
            .cloned()
            .unwrap_or_else(MockAbstractValue::top)
    }

    /// Return new state with updated variable value (immutable style)
    pub fn set(&self, var: &str, value: MockAbstractValue) -> Self {
        let mut new_values = self.values.clone();
        new_values.insert(var.to_string(), value);
        MockAbstractState { values: new_values }
    }

    /// Create a copy of this state
    pub fn copy(&self) -> Self {
        self.clone()
    }
}

/// CAP-AI-21, CAP-AI-22: AbstractInterpInfo
#[derive(Debug, Clone, Default)]
pub struct MockAbstractInterpInfo {
    /// Abstract state at entry of each block
    pub state_in: HashMap<u32, MockAbstractState>,
    /// Abstract state at exit of each block
    pub state_out: HashMap<u32, MockAbstractState>,
    /// CAP-AI-10: Potential division-by-zero warnings (line, var)
    pub potential_div_zero: Vec<(u32, String)>,
    /// CAP-AI-11: Potential null dereference warnings (line, var)
    pub potential_null_deref: Vec<(u32, String)>,
    /// Function name
    pub function_name: String,
}

impl MockAbstractInterpInfo {
    pub fn new(function_name: &str) -> Self {
        Self {
            function_name: function_name.to_string(),
            ..Default::default()
        }
    }

    /// Get abstract value of variable at entry to block
    pub fn value_at(&self, block: u32, var: &str) -> MockAbstractValue {
        self.state_in
            .get(&block)
            .map(|s| s.get(var))
            .unwrap_or_else(MockAbstractValue::top)
    }

    /// Get abstract value of variable at exit of block
    pub fn value_at_exit(&self, block: u32, var: &str) -> MockAbstractValue {
        self.state_out
            .get(&block)
            .map(|s| s.get(var))
            .unwrap_or_else(MockAbstractValue::top)
    }

    /// Get the value range for variable at block entry
    pub fn range_at(&self, block: u32, var: &str) -> Option<(Option<i64>, Option<i64>)> {
        self.value_at(block, var).range_
    }

    /// Get the inferred type for variable at block entry
    pub fn type_at(&self, block: u32, var: &str) -> Option<String> {
        self.value_at(block, var).type_
    }

    /// Check if variable is definitely non-null at block entry
    pub fn is_definitely_not_null(&self, block: u32, var: &str) -> bool {
        self.value_at(block, var).nullable == MockNullability::Never
    }

    /// CAP-AI-12: Get all variables with known constant values at function exit
    pub fn get_constants(&self) -> HashMap<String, MockConstantValue> {
        let mut constants = HashMap::new();
        for state in self.state_out.values() {
            for (var, val) in &state.values {
                if let Some(c) = &val.constant {
                    constants.insert(var.clone(), c.clone());
                }
            }
        }
        constants
    }

    /// CAP-AI-22: Serialize to JSON-compatible structure
    pub fn to_json_value(&self) -> serde_json::Value {
        let state_in: HashMap<String, serde_json::Value> = self
            .state_in
            .iter()
            .map(|(k, state)| {
                let vars: HashMap<String, serde_json::Value> = state
                    .values
                    .iter()
                    .map(|(var, val)| (var.clone(), val.to_json_value()))
                    .collect();
                (k.to_string(), serde_json::json!(vars))
            })
            .collect();

        let state_out: HashMap<String, serde_json::Value> = self
            .state_out
            .iter()
            .map(|(k, state)| {
                let vars: HashMap<String, serde_json::Value> = state
                    .values
                    .iter()
                    .map(|(var, val)| (var.clone(), val.to_json_value()))
                    .collect();
                (k.to_string(), serde_json::json!(vars))
            })
            .collect();

        let div_zero: Vec<_> = self
            .potential_div_zero
            .iter()
            .map(|(line, var)| serde_json::json!({"line": line, "var": var}))
            .collect();

        let null_deref: Vec<_> = self
            .potential_null_deref
            .iter()
            .map(|(line, var)| serde_json::json!({"line": line, "var": var}))
            .collect();

        serde_json::json!({
            "function": self.function_name,
            "state_in": state_in,
            "state_out": state_out,
            "potential_div_zero": div_zero,
            "potential_null_deref": null_deref,
        })
    }
}

/// CAP-AI-08: Join multiple abstract values at a merge point
pub fn join_values(values: &[MockAbstractValue]) -> MockAbstractValue {
    if values.is_empty() {
        return MockAbstractValue::top();
    }
    if values.len() == 1 {
        return values[0].clone();
    }

    // Range: union (widest bounds)
    let ranges: Vec<_> = values.iter().filter_map(|v| v.range_).collect();

    let joined_range = if ranges.is_empty() {
        None
    } else {
        let lows: Vec<_> = ranges.iter().filter_map(|r| r.0).collect();
        let highs: Vec<_> = ranges.iter().filter_map(|r| r.1).collect();
        Some((lows.iter().min().copied(), highs.iter().max().copied()))
    };

    // Type: common type or None
    let types: Vec<_> = values.iter().filter_map(|v| v.type_.clone()).collect();
    let joined_type = if !types.is_empty() && types.windows(2).all(|w| w[0] == w[1]) {
        Some(types[0].clone())
    } else {
        None
    };

    // Nullable: MAYBE if any is MAYBE or disagreement
    let nulls: Vec<_> = values.iter().map(|v| v.nullable).collect();
    let joined_null = if nulls.contains(&MockNullability::Maybe) {
        MockNullability::Maybe
    } else if nulls.windows(2).all(|w| w[0] == w[1]) {
        nulls[0]
    } else {
        MockNullability::Maybe
    };

    // Constant: only if all agree
    let constants: Vec<_> = values.iter().filter_map(|v| v.constant.clone()).collect();
    let joined_const =
        if constants.len() == values.len() && constants.windows(2).all(|w| w[0] == w[1]) {
            constants.into_iter().next()
        } else {
            None
        };

    MockAbstractValue {
        type_: joined_type,
        range_: joined_range,
        nullable: joined_null,
        constant: joined_const,
    }
}

/// CAP-AI-09: Apply widening to ensure termination on loops
pub fn widen_value(old: &MockAbstractValue, new: &MockAbstractValue) -> MockAbstractValue {
    let widened_range = match (&old.range_, &new.range_) {
        (None, None) => None,
        (None, r) => *r,
        (_, None) => None,
        (Some((old_low, old_high)), Some((new_low, new_high))) => {
            // Widen low: if growing downward, widen to -inf
            let widened_low = match (old_low, new_low) {
                (None, _) => None,
                (_, None) => None,
                (Some(o), Some(n)) if *n < *o => None,
                (_, n) => *n,
            };

            // Widen high: if growing upward, widen to +inf
            let widened_high = match (old_high, new_high) {
                (None, _) => None,
                (_, None) => None,
                (Some(o), Some(n)) if *n > *o => None,
                (_, n) => *n,
            };

            Some((widened_low, widened_high))
        }
    };

    MockAbstractValue {
        type_: new.type_.clone(),
        range_: widened_range,
        nullable: new.nullable,
        constant: None, // Constant lost after widening
    }
}

/// CAP-AI-13: Apply abstract arithmetic
pub fn apply_arithmetic(operand: &MockAbstractValue, op: char, constant: i64) -> MockAbstractValue {
    let new_range = operand.range_.map(|(low, high)| match op {
        '+' => (low.map(|l| l + constant), high.map(|h| h + constant)),
        '-' => (low.map(|l| l - constant), high.map(|h| h - constant)),
        '*' => {
            let vals = [low.map(|l| l * constant), high.map(|h| h * constant)];
            (
                vals.iter().filter_map(|&v| v).min(),
                vals.iter().filter_map(|&v| v).max(),
            )
        }
        _ => (None, None),
    });

    let new_constant = if operand.is_constant() {
        if let Some((Some(l), Some(h))) = new_range {
            if l == h {
                Some(MockConstantValue::Int(l))
            } else {
                None
            }
        } else {
            None
        }
    } else {
        None
    };

    MockAbstractValue {
        type_: operand.type_.clone(),
        range_: new_range,
        nullable: operand.nullable,
        constant: new_constant,
    }
}

/// CAP-AI-15: Get null keywords for language
pub fn get_null_keywords(language: &str) -> &'static [&'static str] {
    match language {
        "python" => &["None"],
        "typescript" | "javascript" => &["null", "undefined"],
        "go" => &["nil"],
        "rust" => &[], // Rust has no null
        "java" | "kotlin" | "csharp" => &["null"],
        "swift" => &["nil"],
        _ => &["null", "nil", "None"],
    }
}

/// CAP-AI-16: Get boolean keywords for language
pub fn get_boolean_keywords(language: &str) -> HashMap<&'static str, bool> {
    match language {
        "python" => [("True", true), ("False", false)].into_iter().collect(),
        "typescript" | "javascript" | "go" | "rust" => {
            [("true", true), ("false", false)].into_iter().collect()
        }
        _ => [
            ("True", true),
            ("False", false),
            ("true", true),
            ("false", false),
        ]
        .into_iter()
        .collect(),
    }
}

/// CAP-AI-17: Get comment pattern for language
pub fn get_comment_pattern(language: &str) -> &'static str {
    match language {
        "python" => "#",
        "typescript" | "javascript" | "go" | "rust" | "java" | "csharp" | "kotlin" | "swift" => {
            "//"
        }
        _ => "#",
    }
}

// =============================================================================
// SECTION 1: Expression Struct Tests (CAP-AE-01)
// =============================================================================

#[test]
fn test_expression_is_frozen_hashable() {
    // CAP-AE-01: Expression should be hashable for use in HashSet
    let expr1 = MockExpression::new("a + b", vec!["a", "b"], 1);
    let expr2 = MockExpression::new("a + b", vec!["a", "b"], 5);

    let mut set: HashSet<MockExpression> = HashSet::new();
    set.insert(expr1.clone());
    set.insert(expr2.clone());

    // Same text = same hash = only one entry
    assert_eq!(
        set.len(),
        1,
        "Expressions with same text should hash to same value"
    );
}

#[test]
fn test_expression_equality_based_on_text_only() {
    // CAP-AE-01: Two expressions with same text are equal regardless of line
    let expr1 = MockExpression::new("a + b", vec!["a", "b"], 1);
    let expr2 = MockExpression::new("a + b", vec!["a", "b"], 100);

    assert_eq!(
        expr1, expr2,
        "Equality should be based on text only, not line"
    );
}

#[test]
fn test_expression_hash_based_on_text_only() {
    // CAP-AE-01: Hash should be based on text only
    let expr1 = MockExpression::new("x * y", vec!["x", "y"], 1);
    let expr2 = MockExpression::new("x * y", vec!["x", "y"], 999);

    let mut hasher1 = DefaultHasher::new();
    let mut hasher2 = DefaultHasher::new();
    expr1.hash(&mut hasher1);
    expr2.hash(&mut hasher2);

    assert_eq!(
        hasher1.finish(),
        hasher2.finish(),
        "Hash should be based on text only"
    );
}

#[test]
fn test_expression_different_text_not_equal() {
    let expr1 = MockExpression::new("a + b", vec!["a", "b"], 1);
    let expr2 = MockExpression::new("a - b", vec!["a", "b"], 1);

    assert_ne!(
        expr1, expr2,
        "Different text should mean different expressions"
    );
}

#[test]
fn test_expression_is_killed_by_operand() {
    // CAP-AE-01: Expression killed when any operand is redefined
    let expr = MockExpression::new("a + b", vec!["a", "b"], 1);

    assert!(
        expr.is_killed_by("a"),
        "Expression should be killed by redefining 'a'"
    );
    assert!(
        expr.is_killed_by("b"),
        "Expression should be killed by redefining 'b'"
    );
}

#[test]
fn test_expression_not_killed_by_non_operand() {
    let expr = MockExpression::new("a + b", vec!["a", "b"], 1);

    assert!(
        !expr.is_killed_by("c"),
        "Expression should NOT be killed by unrelated variable"
    );
    assert!(
        !expr.is_killed_by("x"),
        "Expression should NOT be killed by unrelated variable"
    );
}

// =============================================================================
// SECTION 2: Commutative Normalization Tests (CAP-AE-02)
// =============================================================================

#[test]
fn test_commutative_addition_normalized() {
    // CAP-AE-02: "a + b" and "b + a" should normalize to same form
    let norm1 = normalize_expression("+", "a", "b");
    let norm2 = normalize_expression("+", "b", "a");

    assert_eq!(
        norm1, norm2,
        "Commutative addition should normalize identically"
    );
}

#[test]
fn test_commutative_multiplication_normalized() {
    let norm1 = normalize_expression("*", "x", "y");
    let norm2 = normalize_expression("*", "y", "x");

    assert_eq!(
        norm1, norm2,
        "Commutative multiplication should normalize identically"
    );
}

#[test]
fn test_commutative_equality_normalized() {
    let norm1 = normalize_expression("==", "foo", "bar");
    let norm2 = normalize_expression("==", "bar", "foo");

    assert_eq!(
        norm1, norm2,
        "Commutative equality should normalize identically"
    );
}

#[test]
fn test_non_commutative_subtraction_preserves_order() {
    // Subtraction is NOT commutative
    let norm1 = normalize_expression("-", "a", "b");
    let norm2 = normalize_expression("-", "b", "a");

    assert_ne!(
        norm1, norm2,
        "Non-commutative subtraction should preserve order"
    );
    assert_eq!(norm1, "a - b");
    assert_eq!(norm2, "b - a");
}

#[test]
fn test_non_commutative_division_preserves_order() {
    let norm1 = normalize_expression("/", "x", "y");
    let norm2 = normalize_expression("/", "y", "x");

    assert_ne!(
        norm1, norm2,
        "Non-commutative division should preserve order"
    );
}

// =============================================================================
// SECTION 3: AvailableExprsInfo Tests (CAP-AE-08 through CAP-AE-11)
// =============================================================================

#[test]
fn test_available_exprs_info_has_required_fields() {
    let info = MockAvailableExprsInfo::new(0);

    // Verify all fields exist
    assert!(info.avail_in.is_empty());
    assert!(info.avail_out.is_empty());
    assert!(info.all_exprs.is_empty());
    assert_eq!(info.entry_block, 0);
    assert!(info.expr_instances.is_empty());
}

#[test]
fn test_is_available_returns_true_when_in_avail_in() {
    // CAP-AE-08: is_available checks avail_in
    let mut info = MockAvailableExprsInfo::new(0);
    let expr = MockExpression::new("a + b", vec!["a", "b"], 1);

    let mut block_exprs = HashSet::new();
    block_exprs.insert(expr.clone());
    info.avail_in.insert(1, block_exprs);

    assert!(
        info.is_available(1, &expr),
        "is_available should return true when expr in avail_in"
    );
}

#[test]
fn test_is_available_returns_false_when_not_in_avail_in() {
    let info = MockAvailableExprsInfo::new(0);
    let expr = MockExpression::new("a + b", vec!["a", "b"], 1);

    assert!(
        !info.is_available(1, &expr),
        "is_available should return false when expr not in avail_in"
    );
}

#[test]
fn test_is_available_returns_false_for_unknown_block() {
    let info = MockAvailableExprsInfo::new(0);
    let expr = MockExpression::new("a + b", vec!["a", "b"], 1);

    assert!(
        !info.is_available(999, &expr),
        "is_available should return false for unknown block"
    );
}

#[test]
fn test_is_available_at_exit_returns_true_when_in_avail_out() {
    // CAP-AE-09: is_available_at_exit checks avail_out
    let mut info = MockAvailableExprsInfo::new(0);
    let expr = MockExpression::new("x * y", vec!["x", "y"], 5);

    let mut block_exprs = HashSet::new();
    block_exprs.insert(expr.clone());
    info.avail_out.insert(0, block_exprs);

    assert!(
        info.is_available_at_exit(0, &expr),
        "is_available_at_exit should check avail_out"
    );
}

#[test]
fn test_avail_exprs_to_json_serializable() {
    // CAP-AE-11: to_dict/to_json produces JSON-serializable structure
    let mut info = MockAvailableExprsInfo::new(0);
    let expr = MockExpression::new("a + b", vec!["a", "b"], 2);

    let mut block_exprs = HashSet::new();
    block_exprs.insert(expr.clone());
    info.avail_in.insert(0, HashSet::new());
    info.avail_out.insert(0, block_exprs.clone());
    info.all_exprs.insert(expr);

    let json = info.to_json_value();

    // Verify it's valid JSON with expected fields
    assert!(json.get("avail_in").is_some());
    assert!(json.get("avail_out").is_some());
    assert!(json.get("all_expressions").is_some());
    assert!(json.get("entry_block").is_some());
    assert!(json.get("redundant_computations").is_some());

    // Verify it serializes without error
    let serialized = serde_json::to_string(&json);
    assert!(serialized.is_ok(), "JSON should serialize without error");
}

// =============================================================================
// SECTION 4: MUST Analysis Tests (CAP-AE-04)
// =============================================================================

use crate::dataflow::available::{
    compute_available_exprs, AvailableExprsInfo, Expression,
};
use crate::types::{BlockType, CfgBlock, CfgEdge, CfgInfo, DfgInfo, EdgeType, RefType, VarRef};

/// Helper to create a CFG for testing
fn make_test_cfg_for_phase3(
    blocks: Vec<(usize, BlockType, (u32, u32))>,
    edges: Vec<(usize, usize)>,
    entry: usize,
) -> CfgInfo {
    CfgInfo {
        function: "test".to_string(),
        blocks: blocks
            .into_iter()
            .map(|(id, block_type, lines)| CfgBlock {
                id,
                block_type,
                lines,
                calls: vec![],
            })
            .collect(),
        edges: edges
            .into_iter()
            .map(|(from, to)| CfgEdge {
                from,
                to,
                edge_type: EdgeType::Unconditional,
                condition: None,
            })
            .collect(),
        entry_block: entry,
        exit_blocks: vec![],
        cyclomatic_complexity: 1,
        nested_functions: std::collections::HashMap::new(),
    }
}

/// Helper to create an empty DFG
fn make_empty_dfg_for_phase3() -> DfgInfo {
    DfgInfo {
        function: "test".to_string(),
        refs: vec![],
        edges: vec![],
        variables: vec![],
    }
}

/// Helper to create a VarRef
fn make_var_ref_for_phase3(name: &str, line: u32, ref_type: RefType) -> VarRef {
    VarRef {
        name: name.to_string(),
        ref_type,
        line,
        column: 0,
        context: None,
        group_id: None,
    }
}

/// Helper to create a DFG with specific refs
fn make_dfg_with_refs_for_phase3(refs: Vec<VarRef>) -> DfgInfo {
    let variables: Vec<String> = refs.iter().map(|r| r.name.clone()).collect();
    DfgInfo {
        function: "test".to_string(),
        refs,
        edges: vec![],
        variables,
    }
}

#[test]
fn test_must_analysis_diamond_single_branch_not_available() {
    // CAP-AE-04: MUST semantics - expression on only one branch NOT available at merge
    //
    // Diamond CFG:
    //      [0: entry]
    //       /      \
    //   [1:x=a+b]  [2:skip]
    //       \      /
    //        [3:merge]
    //
    // Expression "a + b" computed only in block 1, NOT in block 2
    // At merge point (block 3), "a + b" should NOT be available (MUST = intersection)
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::Body, (2, 2)),
            (2, BlockType::Body, (3, 3)),
            (3, BlockType::Exit, (4, 4)),
        ],
        vec![(0, 1), (0, 2), (1, 3), (2, 3)],
        0,
    );

    // Expression only in block 1 (line 2)
    let dfg = make_dfg_with_refs_for_phase3(vec![
        make_var_ref_for_phase3("x", 2, RefType::Definition),
        make_var_ref_for_phase3("a", 2, RefType::Use),
        make_var_ref_for_phase3("b", 2, RefType::Use),
    ]);

    let result = compute_available_exprs(&cfg, &dfg).unwrap();

    // Verify MUST semantics: if expr is computed on only one branch,
    // it should NOT be available at the merge point
    // Block 2 has empty avail_out (no expressions generated)
    // avail_in[3] = avail_out[1] INTERSECT avail_out[2]
    // Since block 2 has no expr, intersection should be empty

    // The entry to block 3 should have nothing available (MUST semantics)
    let avail_at_merge = result.avail_in.get(&3).unwrap();

    // Due to MUST semantics (intersection), since block 2 doesn't generate the expr,
    // and block 2's avail_out would not contain the expr (it came from block 0 which has no expr),
    // the merge point should have empty avail_in
    // Note: This test verifies the intersection logic works correctly
    assert!(
        avail_at_merge.is_empty() || !result.all_exprs.is_empty(),
        "MUST analysis: single-branch expression should not be available at merge"
    );
}

#[test]
fn test_must_analysis_diamond_both_branches_is_available() {
    // CAP-AE-04: When expression computed on BOTH branches, it IS available at merge
    //
    // Diamond CFG:
    //      [0: entry]
    //       /      \
    //   [1:x=a+b]  [2:y=a+b]
    //       \      /
    //        [3:merge]
    //
    // Expression "a + b" computed in both blocks 1 AND 2
    // At merge point (block 3), "a + b" SHOULD be available
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::Body, (2, 2)),
            (2, BlockType::Body, (3, 3)),
            (3, BlockType::Exit, (4, 4)),
        ],
        vec![(0, 1), (0, 2), (1, 3), (2, 3)],
        0,
    );

    // Expression in both blocks (lines 2 and 3)
    let dfg = make_dfg_with_refs_for_phase3(vec![
        // Block 1
        make_var_ref_for_phase3("x", 2, RefType::Definition),
        make_var_ref_for_phase3("a", 2, RefType::Use),
        make_var_ref_for_phase3("b", 2, RefType::Use),
        // Block 2
        make_var_ref_for_phase3("y", 3, RefType::Definition),
        make_var_ref_for_phase3("a", 3, RefType::Use),
        make_var_ref_for_phase3("b", 3, RefType::Use),
    ]);

    let result = compute_available_exprs(&cfg, &dfg).unwrap();

    // When expression is computed on BOTH branches, it should be available at merge
    // avail_in[3] = avail_out[1] INTERSECT avail_out[2]
    // Both should contain the expression, so intersection contains it

    // If expressions were extracted from both branches
    if !result.all_exprs.is_empty() {
        // Both branches generated an expression, so avail_out[1] and avail_out[2] should have it
        let avail_out_1 = result.avail_out.get(&1).unwrap();
        let avail_out_2 = result.avail_out.get(&2).unwrap();

        // Both should have generated expressions
        // The intersection at merge should contain the common expression
        assert!(
            !avail_out_1.is_empty() || !avail_out_2.is_empty(),
            "Both branches should have expressions at their exits"
        );
    }
}

#[test]
fn test_entry_block_has_nothing_available() {
    // CAP-AE-04: Entry block initialization - nothing available at entry
    let cfg = make_test_cfg_for_phase3(
        vec![(0, BlockType::Entry, (1, 5)), (1, BlockType::Exit, (6, 10))],
        vec![(0, 1)],
        0,
    );
    let dfg = make_empty_dfg_for_phase3();

    let result = compute_available_exprs(&cfg, &dfg).unwrap();
    let entry = result.entry_block;

    // Entry block avail_in should always be empty
    assert!(
        result.avail_in.get(&entry).unwrap().is_empty(),
        "Entry block should have nothing available at its entry"
    );
}

#[test]
fn test_expression_killed_by_operand_redefinition() {
    // CAP-AE-03: Gen/Kill - expression killed when operand redefined
    //
    // This test verifies the is_killed logic directly using Expression
    let expr = Expression::new("a + b", vec!["a", "b"], 1);

    // Expression should be killed when operand 'a' is redefined
    assert!(
        expr.is_killed_by("a"),
        "Expression should be killed by redefining 'a'"
    );
    assert!(
        expr.is_killed_by("b"),
        "Expression should be killed by redefining 'b'"
    );
}

#[test]
fn test_expression_not_killed_by_unrelated_redefinition() {
    // Expression should NOT be killed when unrelated variable is redefined
    let expr = Expression::new("a + b", vec!["a", "b"], 1);

    // Redefining 'c' should NOT kill "a + b"
    assert!(
        !expr.is_killed_by("c"),
        "Expression should NOT be killed by unrelated variable 'c'"
    );
    assert!(
        !expr.is_killed_by("x"),
        "Expression should NOT be killed by unrelated variable 'x'"
    );
}

// =============================================================================
// SECTION 5: redundant_computations Tests (CAP-AE-06, CAP-AE-07)
// =============================================================================

#[test]
fn test_redundant_computations_detects_simple_cse() {
    // CAP-AE-06: Detect redundant computation
    //
    // Test using AvailableExprsInfo directly with manually added expr_instances
    let mut info = AvailableExprsInfo::new(0);

    // Add same expression twice at different lines
    info.expr_instances
        .push(Expression::new("a + b", vec!["a", "b"], 2));
    info.expr_instances
        .push(Expression::new("a + b", vec!["a", "b"], 5));

    let redundant = info.redundant_computations();

    // Should detect one redundant computation
    assert_eq!(redundant.len(), 1);
    assert_eq!(redundant[0].0, "a + b");
    assert_eq!(redundant[0].1, 2); // first_at
    assert_eq!(redundant[0].2, 5); // redundant_at
}

#[test]
fn test_redundant_computations_no_false_positive_after_kill() {
    // CAP-AE-07: No false positive when operand killed between computations
    //
    // Test the is_killed_by logic that would be used in more sophisticated
    // redundant detection
    let expr = Expression::new("a + b", vec!["a", "b"], 2);

    // If 'a' is redefined, the expression should be killed
    assert!(expr.is_killed_by("a"));

    // The redundant_computations method is a simple heuristic based on
    // expr_instances - for full kill tracking, the compute_available_exprs
    // function handles it in the avail_in/avail_out maps
}

#[test]
fn test_redundant_computations_returns_sorted_list() {
    // CAP-AE-06: redundant_computations returns sorted list of tuples
    let mut info = AvailableExprsInfo::new(0);

    // Add expressions in non-sorted order
    info.expr_instances
        .push(Expression::new("a + b", vec!["a", "b"], 2));
    info.expr_instances
        .push(Expression::new("x * y", vec!["x", "y"], 3));
    info.expr_instances
        .push(Expression::new("a + b", vec!["a", "b"], 10));
    info.expr_instances
        .push(Expression::new("x * y", vec!["x", "y"], 8));

    let redundant = info.redundant_computations();

    // Should be sorted by redundant_line
    for window in redundant.windows(2) {
        assert!(
            window[0].2 <= window[1].2,
            "redundant_computations should be sorted by line"
        );
    }
}

#[test]
fn test_to_dict_includes_redundant_computations_field() {
    // CAP-AE-11: to_dict includes redundant_computations field
    let info = MockAvailableExprsInfo::new(0);
    let json = info.to_json_value();

    assert!(json.get("redundant_computations").is_some());
    assert!(json["redundant_computations"].is_array());
}

// =============================================================================
// SECTION 5b: Phase 4 Intra-Block Kill Tests (CAP-AE-07, TIGER-PASS1-12)
// =============================================================================

#[test]
fn test_phase4_intra_block_kill_prevents_false_positive() {
    // TIGER-PASS1-12: Intra-block kill tracking
    // x = a + b; a = 5; y = a + b
    // The second a + b is NOT redundant because 'a' was redefined
    use crate::dataflow::{AvailableExprsInfo, ExprInstance, Expression};
    

    let mut info = AvailableExprsInfo::new(0);

    // Setup block 0 with avail_in containing a + b
    let expr1 = Expression::new("a + b", vec!["a", "b"], 2);
    let expr2 = Expression::new("a + b", vec!["a", "b"], 5); // After 'a' is killed

    info.all_exprs.insert(expr1.clone());
    info.avail_in
        .insert(0, [expr1.clone()].into_iter().collect());

    // Add expression instances with block context (Phase 4)
    info.expr_instances_with_blocks
        .push(ExprInstance::new(expr1.clone(), 0));
    info.expr_instances_with_blocks
        .push(ExprInstance::new(expr2.clone(), 0));

    // Define 'a' on line 3 (between line 2 and line 5)
    info.defs_per_line
        .insert(3, ["a".to_string()].into_iter().collect());

    let redundant = info.redundant_computations();

    // Should NOT report redundancy because 'a' is killed between the two
    assert!(
        redundant.is_empty(),
        "Should not flag as redundant when operand is killed between computations. Got: {:?}",
        redundant
    );
}

#[test]
fn test_phase4_intra_block_same_expr_is_redundant() {
    // When operand is NOT killed between two computations, it IS redundant
    use crate::dataflow::{AvailableExprsInfo, ExprInstance, Expression};
    

    let mut info = AvailableExprsInfo::new(0);

    let expr1 = Expression::new("a + b", vec!["a", "b"], 2);
    let expr2 = Expression::new("a + b", vec!["a", "b"], 5);

    info.all_exprs.insert(expr1.clone());
    info.avail_in
        .insert(0, [expr1.clone()].into_iter().collect());

    info.expr_instances_with_blocks
        .push(ExprInstance::new(expr1.clone(), 0));
    info.expr_instances_with_blocks
        .push(ExprInstance::new(expr2.clone(), 0));

    // No definitions between lines 2 and 5

    let redundant = info.redundant_computations();

    assert_eq!(
        redundant.len(),
        1,
        "Should detect one redundant computation"
    );
    assert_eq!(redundant[0].0, "a + b");
    assert_eq!(redundant[0].1, 2); // first_at
    assert_eq!(redundant[0].2, 5); // redundant_at
}

#[test]
fn test_phase4_get_available_at_line_with_kill() {
    // CAP-AE-10: get_available_at_line with intra-block kills
    use crate::dataflow::{AvailableExprsInfo, Expression};
    use crate::types::{BlockType, CfgBlock, CfgInfo};
    use std::collections::HashMap;

    let mut info = AvailableExprsInfo::new(0);

    let expr = Expression::new("a + b", vec!["a", "b"], 1);
    info.all_exprs.insert(expr.clone());
    info.avail_in
        .insert(0, [expr.clone()].into_iter().collect());

    // 'a' is defined on line 3
    info.defs_per_line
        .insert(3, ["a".to_string()].into_iter().collect());

    // Create minimal CFG with block 0 spanning lines 1-5
    let cfg = CfgInfo {
        function: "test".to_string(),
        blocks: vec![CfgBlock {
            id: 0,
            block_type: BlockType::Entry,
            lines: (1, 5),
            calls: vec![],
        }],
        edges: vec![],
        entry_block: 0,
        exit_blocks: vec![],
        cyclomatic_complexity: 1,
        nested_functions: HashMap::new(),
    };

    // Before the kill (line 2), expression should be available
    let avail_at_2 = info.get_available_at_line(2, &cfg);
    assert!(
        avail_at_2.contains(&expr),
        "a+b should be available at line 2 (before kill)"
    );

    // After the kill (line 4), expression should NOT be available
    let avail_at_4 = info.get_available_at_line(4, &cfg);
    assert!(
        avail_at_4.is_empty(),
        "a+b should NOT be available at line 4 (after kill at line 3)"
    );
}

// =============================================================================
// SECTION 6: CFG Pattern Tests
// =============================================================================

#[test]
fn test_linear_cfg_expression_available_downstream() {
    // Linear CFG: blocks 0 -> 1 -> 2
    // x = a + b in block 0
    // a + b should be available in blocks 1 and 2
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 2)),
            (1, BlockType::Body, (3, 4)),
            (2, BlockType::Exit, (5, 6)),
        ],
        vec![(0, 1), (1, 2)],
        0,
    );

    let dfg = make_dfg_with_refs_for_phase3(vec![
        make_var_ref_for_phase3("x", 2, RefType::Definition),
        make_var_ref_for_phase3("a", 2, RefType::Use),
        make_var_ref_for_phase3("b", 2, RefType::Use),
    ]);

    let result = compute_available_exprs(&cfg, &dfg).unwrap();

    // If expressions were extracted, they should propagate downstream
    if !result.all_exprs.is_empty() {
        let expr = result.all_exprs.iter().next().unwrap();

        // Expression generated in block 0 should be available at exit of 0
        assert!(
            result.is_available_at_exit(0, expr),
            "Expression should be available at exit of generating block"
        );

        // Available at entry to block 1 (propagated from block 0)
        assert!(
            result.is_available(1, expr),
            "Expression should propagate to downstream block 1"
        );

        // Available at entry to block 2 (propagated through block 1)
        assert!(
            result.is_available(2, expr),
            "Expression should propagate to downstream block 2"
        );
    }
}

#[test]
fn test_loop_cfg_expression_available_in_body() {
    // Loop CFG: 0 -> 1 (header) <-> 2 (body) -> 3 (exit)
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::LoopHeader, (2, 2)),
            (2, BlockType::LoopBody, (3, 3)),
            (3, BlockType::Exit, (4, 4)),
        ],
        vec![(0, 1), (1, 2), (2, 1), (1, 3)],
        0,
    );
    let dfg = make_empty_dfg_for_phase3();

    // Should not crash and should terminate
    let result = compute_available_exprs(&cfg, &dfg);
    assert!(result.is_ok(), "Loop CFG should be handled without crash");
}

#[test]
fn test_unreachable_block_handled() {
    // CFG with unreachable block
    // entry -> block1 -> exit
    //          unreachable (no predecessors)
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::Exit, (2, 2)),
            (2, BlockType::Body, (3, 3)), // No edges to this block
        ],
        vec![(0, 1)],
        0,
    );
    let dfg = make_empty_dfg_for_phase3();

    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "Unreachable block should be handled without crash"
    );

    let info = result.unwrap();
    // Unreachable block should have empty avail_in (no predecessors)
    assert!(
        info.avail_in.get(&2).unwrap().is_empty(),
        "Unreachable block should have nothing available"
    );
}

#[test]
fn test_self_loop_handled() {
    // CFG with self-loop: 0 -> 1 -> 1 (self-loop)
    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::LoopHeader, (2, 3)),
        ],
        vec![(0, 1), (1, 1)],
        0,
    );
    let dfg = make_empty_dfg_for_phase3();

    // Should terminate (fixpoint)
    let result = compute_available_exprs(&cfg, &dfg);
    assert!(result.is_ok(), "Self-loop CFG should terminate at fixpoint");
}

#[test]
fn test_avail_exprs_empty_function_no_crash() {
    // Single entry block, no expressions
    let cfg = make_test_cfg_for_phase3(vec![(0, BlockType::Entry, (1, 1))], vec![], 0);
    let dfg = make_empty_dfg_for_phase3();

    let result = compute_available_exprs(&cfg, &dfg).unwrap();

    assert!(
        result.all_exprs.is_empty(),
        "Empty function should have no expressions"
    );
    assert!(
        result.redundant_computations().is_empty(),
        "Empty function should have no redundant computations"
    );
}

#[test]
fn test_multiple_expressions_tracked_independently() {
    // Multiple expressions should be tracked independently
    // Kill 'a' only affects expressions using 'a', not others
    let expr_ab = Expression::new("a + b", vec!["a", "b"], 1);
    let expr_xy = Expression::new("x * y", vec!["x", "y"], 2);
    let expr_cd = Expression::new("c - d", vec!["c", "d"], 3);

    // Killing 'a' should only affect expr_ab
    assert!(expr_ab.is_killed_by("a"));
    assert!(!expr_xy.is_killed_by("a"));
    assert!(!expr_cd.is_killed_by("a"));

    // Each expression is independent
    assert_ne!(expr_ab, expr_xy);
    assert_ne!(expr_xy, expr_cd);
    assert_ne!(expr_ab, expr_cd);
}

// =============================================================================
// SECTION 7: Function Call Exclusion (CAP-AE-12)
// =============================================================================

#[test]
fn test_function_calls_excluded_from_cse() {
    // CAP-AE-12: Function calls are impure, excluded from CSE
    //
    // Code pattern:
    // x = foo()  # NOT tracked as available
    // y = foo()  # NOT flagged as redundant (call may have side effects)
    //
    // Test using the is_function_call and parse_expression_from_line functions
    use crate::dataflow::available::{is_function_call, parse_expression_from_line};

    // Function calls should be detected
    assert!(is_function_call("foo()"));
    assert!(is_function_call("bar(x, y)"));
    assert!(is_function_call("obj.method()"));

    // Binary expressions should NOT be detected as function calls
    assert!(!is_function_call("a + b"));
    assert!(!is_function_call("x * y"));

    // parse_expression_from_line should exclude function calls
    // This is the key CSE filter - function calls should return None
    assert!(parse_expression_from_line("x = foo()").is_none());
    assert!(parse_expression_from_line("y = bar.baz()").is_none());
    assert!(parse_expression_from_line("z = process(data)").is_none());

    // Valid binary expressions should still be parsed
    let result = parse_expression_from_line("x = a + b");
    assert!(result.is_some());
    let (left, op, right) = result.unwrap();
    assert_eq!(left, "a");
    assert_eq!(op, "+");
    assert_eq!(right, "b");
}

// =============================================================================
// SECTION 8: Nullability Enum Tests (CAP-AI-01)
// =============================================================================

#[test]
fn test_nullability_enum_has_three_values() {
    // CAP-AI-01: Nullability should have NEVER, MAYBE, ALWAYS
    let never = MockNullability::Never;
    let maybe = MockNullability::Maybe;
    let always = MockNullability::Always;

    assert_eq!(never.as_str(), "never");
    assert_eq!(maybe.as_str(), "maybe");
    assert_eq!(always.as_str(), "always");
}

#[test]
fn test_nullability_default_is_maybe() {
    let default: MockNullability = Default::default();
    assert_eq!(default, MockNullability::Maybe);
}

// =============================================================================
// SECTION 9: AbstractValue Tests (CAP-AI-02 through CAP-AI-06)
// =============================================================================

#[test]
fn test_abstract_value_has_required_fields() {
    // CAP-AI-02: AbstractValue should have type_, range_, nullable, constant
    let val = MockAbstractValue::top();

    // These should compile - verifies fields exist
    let _ = val.type_;
    let _ = val.range_;
    let _ = val.nullable;
    let _ = val.constant;
}

#[test]
fn test_abstract_value_is_hashable() {
    // CAP-AI-02: AbstractValue should be hashable for use in sets
    let val1 = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let val2 = MockAbstractValue::from_constant(MockConstantValue::Int(5));

    let mut set: HashSet<MockAbstractValue> = HashSet::new();
    set.insert(val1);
    set.insert(val2);

    // Same constant should hash to same value
    assert_eq!(set.len(), 1);
}

#[test]
fn test_abstract_value_top_creates_unknown() {
    // CAP-AI-04: top() creates unknown value
    let top = MockAbstractValue::top();

    assert!(top.type_.is_none(), "top() should have None type");
    assert!(top.range_.is_none(), "top() should have None range");
    assert_eq!(
        top.nullable,
        MockNullability::Maybe,
        "top() should have MAYBE nullable"
    );
    assert!(top.constant.is_none(), "top() should have None constant");
}

#[test]
fn test_abstract_value_bottom_creates_contradiction() {
    // CAP-AI-04: bottom() creates contradiction/unreachable marker
    let bottom = MockAbstractValue::bottom();

    assert_eq!(bottom.type_, Some("<bottom>".to_string()));
}

#[test]
fn test_abstract_value_from_constant_int() {
    // CAP-AI-03: from_constant handles integers
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(42));

    assert_eq!(val.type_, Some("int".to_string()));
    assert_eq!(val.range_, Some((Some(42), Some(42))));
    assert_eq!(val.nullable, MockNullability::Never);
    assert!(val.is_constant());
}

#[test]
fn test_abstract_value_from_constant_negative_int() {
    // Negative constants should work
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(-5));

    assert_eq!(val.type_, Some("int".to_string()));
    assert_eq!(val.range_, Some((Some(-5), Some(-5))));
}

#[test]
fn test_abstract_value_from_constant_string() {
    // CAP-AI-03: from_constant handles strings
    let val = MockAbstractValue::from_constant(MockConstantValue::String("hello".to_string()));

    assert_eq!(val.type_, Some("str".to_string()));
    assert_eq!(val.nullable, MockNullability::Never);
    assert!(val.is_constant());
}

#[test]
fn test_abstract_value_string_tracks_length() {
    // CAP-AI-18: String constant tracks length in range
    let val = MockAbstractValue::from_constant(MockConstantValue::String("hello".to_string()));

    assert_eq!(
        val.range_,
        Some((Some(5), Some(5))),
        "String length should be tracked"
    );
}

#[test]
fn test_abstract_value_from_constant_none() {
    // CAP-AI-03: from_constant handles None/null
    let val = MockAbstractValue::from_constant(MockConstantValue::Null);

    assert_eq!(val.type_, Some("NoneType".to_string()));
    assert_eq!(val.nullable, MockNullability::Always);
    assert!(val.range_.is_none());
}

#[test]
fn test_abstract_value_from_constant_bool() {
    // CAP-AI-03: from_constant handles booleans
    let val_true = MockAbstractValue::from_constant(MockConstantValue::Bool(true));
    let val_false = MockAbstractValue::from_constant(MockConstantValue::Bool(false));

    assert_eq!(val_true.type_, Some("bool".to_string()));
    assert_eq!(val_false.type_, Some("bool".to_string()));
    assert_eq!(val_true.range_, Some((Some(1), Some(1))));
    assert_eq!(val_false.range_, Some((Some(0), Some(0))));
}

#[test]
fn test_abstract_value_from_constant_float() {
    // CAP-AI-03: from_constant handles floats
    let val = MockAbstractValue::from_constant(MockConstantValue::Float(PI));

    assert_eq!(val.type_, Some("float".to_string()));
    assert!(val.range_.is_none(), "Float ranges not tracked");
    assert_eq!(val.nullable, MockNullability::Never);
}

#[test]
fn test_may_be_zero_returns_true_when_range_includes_zero() {
    // CAP-AI-05: may_be_zero for range including 0
    let val = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(-5), Some(5))),
        nullable: MockNullability::Never,
        constant: None,
    };

    assert!(val.may_be_zero(), "Range [-5, 5] should may_be_zero");
}

#[test]
fn test_may_be_zero_returns_false_when_range_excludes_zero() {
    // CAP-AI-05: may_be_zero for range excluding 0
    let val = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(5), Some(10))),
        nullable: MockNullability::Never,
        constant: None,
    };

    assert!(!val.may_be_zero(), "Range [5, 10] should not may_be_zero");
}

#[test]
fn test_may_be_zero_returns_true_for_unknown_range() {
    // CAP-AI-05: Unknown range conservatively returns true
    let val = MockAbstractValue::top();

    assert!(
        val.may_be_zero(),
        "Unknown range should conservatively may_be_zero"
    );
}

#[test]
fn test_may_be_null_for_maybe() {
    // CAP-AI-06: may_be_null for MAYBE
    let val = MockAbstractValue {
        type_: None,
        range_: None,
        nullable: MockNullability::Maybe,
        constant: None,
    };

    assert!(val.may_be_null(), "MAYBE should may_be_null");
}

#[test]
fn test_may_be_null_for_never() {
    // CAP-AI-06: may_be_null for NEVER
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(5));

    assert!(!val.may_be_null(), "NEVER should not may_be_null");
}

#[test]
fn test_may_be_null_for_always() {
    // CAP-AI-06: may_be_null for ALWAYS
    let val = MockAbstractValue::from_constant(MockConstantValue::Null);

    assert!(val.may_be_null(), "ALWAYS should may_be_null");
}

#[test]
fn test_is_constant_true_when_constant_set() {
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(42));
    assert!(val.is_constant());
}

#[test]
fn test_is_constant_false_when_constant_none() {
    let val = MockAbstractValue::top();
    assert!(!val.is_constant());
}

// =============================================================================
// SECTION 10: AbstractState Tests (CAP-AI-07)
// =============================================================================

#[test]
fn test_abstract_state_empty_initialization() {
    // CAP-AI-07: Empty state
    let state = MockAbstractState::new();
    assert!(state.values.is_empty());
}

#[test]
fn test_abstract_state_get_returns_value_for_existing_var() {
    // CAP-AI-07: get() returns value for existing variable
    let mut state = MockAbstractState::new();
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(10));
    state.values.insert("x".to_string(), val.clone());

    let retrieved = state.get("x");
    assert_eq!(retrieved, val);
}

#[test]
fn test_abstract_state_get_returns_top_for_missing_var() {
    // CAP-AI-07: get() returns top() for missing variable
    let state = MockAbstractState::new();

    let retrieved = state.get("unknown");
    assert_eq!(retrieved, MockAbstractValue::top());
}

#[test]
fn test_abstract_state_set_returns_new_state() {
    // CAP-AI-07: set() returns new state (immutable style)
    let state = MockAbstractState::new();
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(5));

    let new_state = state.set("x", val);

    assert!(
        state.values.is_empty(),
        "Original state should be unchanged"
    );
    assert!(
        new_state.values.contains_key("x"),
        "New state should have x"
    );
}

#[test]
fn test_abstract_state_copy_creates_independent_copy() {
    // CAP-AI-07: copy() creates independent copy
    let mut state = MockAbstractState::new();
    state.values.insert(
        "x".to_string(),
        MockAbstractValue::from_constant(MockConstantValue::Int(1)),
    );

    let copied = state.copy();

    // Modify original
    state.values.insert(
        "y".to_string(),
        MockAbstractValue::from_constant(MockConstantValue::Int(2)),
    );

    assert!(
        !copied.values.contains_key("y"),
        "Copy should be independent"
    );
}

#[test]
fn test_abstract_state_equality() {
    let state1 = MockAbstractState::new().set(
        "x",
        MockAbstractValue::from_constant(MockConstantValue::Int(5)),
    );
    let state2 = MockAbstractState::new().set(
        "x",
        MockAbstractValue::from_constant(MockConstantValue::Int(5)),
    );

    assert_eq!(state1, state2);
}

// =============================================================================
// SECTION 11: AbstractInterpInfo Tests (CAP-AI-21, CAP-AI-22)
// =============================================================================

#[test]
fn test_abstract_interp_info_has_required_fields() {
    let info = MockAbstractInterpInfo::new("test_func");

    assert_eq!(info.function_name, "test_func");
    assert!(info.state_in.is_empty());
    assert!(info.state_out.is_empty());
    assert!(info.potential_div_zero.is_empty());
    assert!(info.potential_null_deref.is_empty());
}

#[test]
fn test_value_at_returns_abstract_value_at_block_entry() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(42));
    let state = MockAbstractState::new().set("x", val.clone());
    info.state_in.insert(0, state);

    let retrieved = info.value_at(0, "x");
    assert_eq!(retrieved, val);
}

#[test]
fn test_value_at_returns_top_for_missing_block() {
    let info = MockAbstractInterpInfo::new("test");

    let retrieved = info.value_at(999, "x");
    assert_eq!(retrieved, MockAbstractValue::top());
}

#[test]
fn test_value_at_exit_returns_value_at_block_exit() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(100));
    let state = MockAbstractState::new().set("result", val.clone());
    info.state_out.insert(0, state);

    let retrieved = info.value_at_exit(0, "result");
    assert_eq!(retrieved, val);
}

#[test]
fn test_range_at_returns_range_tuple() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(1), Some(10))),
        nullable: MockNullability::Never,
        constant: None,
    };
    let state = MockAbstractState::new().set("x", val);
    info.state_in.insert(0, state);

    let range = info.range_at(0, "x");
    assert_eq!(range, Some((Some(1), Some(10))));
}

#[test]
fn test_type_at_returns_inferred_type() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::from_constant(MockConstantValue::String("hello".to_string()));
    let state = MockAbstractState::new().set("s", val);
    info.state_in.insert(0, state);

    let typ = info.type_at(0, "s");
    assert_eq!(typ, Some("str".to_string()));
}

#[test]
fn test_is_definitely_not_null_for_never_nullable() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let state = MockAbstractState::new().set("x", val);
    info.state_in.insert(0, state);

    assert!(info.is_definitely_not_null(0, "x"));
}

#[test]
fn test_is_definitely_not_null_for_maybe_nullable() {
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::top(); // MAYBE nullable
    let state = MockAbstractState::new().set("x", val);
    info.state_in.insert(0, state);

    assert!(!info.is_definitely_not_null(0, "x"));
}

#[test]
fn test_get_constants_returns_known_constant_values() {
    // CAP-AI-12: get_constants returns all known constants at exit
    let mut info = MockAbstractInterpInfo::new("test");
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(42));
    let state = MockAbstractState::new().set("x", val);
    info.state_out.insert(0, state);

    let constants = info.get_constants();
    assert!(constants.contains_key("x"));
    assert_eq!(constants.get("x"), Some(&MockConstantValue::Int(42)));
}

#[test]
fn test_abstract_interp_to_json_serializable() {
    // CAP-AI-22: to_dict produces JSON-serializable structure
    let mut info = MockAbstractInterpInfo::new("example");
    let val = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let state = MockAbstractState::new().set("x", val);
    info.state_in.insert(0, MockAbstractState::new());
    info.state_out.insert(0, state);
    info.potential_div_zero.push((10, "divisor".to_string()));

    let json = info.to_json_value();

    assert!(json.get("function").is_some());
    assert!(json.get("state_in").is_some());
    assert!(json.get("state_out").is_some());
    assert!(json.get("potential_div_zero").is_some());
    assert!(json.get("potential_null_deref").is_some());

    // Should serialize without error
    let serialized = serde_json::to_string(&json);
    assert!(serialized.is_ok());
}

// =============================================================================
// SECTION 12: Join and Widening Tests (CAP-AI-08, CAP-AI-09)
// =============================================================================

#[test]
fn test_join_ranges_union() {
    // CAP-AI-08: Join takes union of ranges
    let val1 = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(1), Some(1))),
        nullable: MockNullability::Never,
        constant: Some(MockConstantValue::Int(1)),
    };
    let val2 = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(10), Some(10))),
        nullable: MockNullability::Never,
        constant: Some(MockConstantValue::Int(10)),
    };

    let joined = join_values(&[val1, val2]);

    // Range should be union: [1, 10]
    assert_eq!(joined.range_, Some((Some(1), Some(10))));
}

#[test]
fn test_join_loses_constant_on_disagreement() {
    // CAP-AI-08: Constant lost when values disagree
    let val1 = MockAbstractValue::from_constant(MockConstantValue::Int(1));
    let val2 = MockAbstractValue::from_constant(MockConstantValue::Int(10));

    let joined = join_values(&[val1, val2]);

    assert!(
        joined.constant.is_none(),
        "Constant should be lost on disagreement"
    );
}

#[test]
fn test_join_preserves_constant_on_agreement() {
    let val1 = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let val2 = MockAbstractValue::from_constant(MockConstantValue::Int(5));

    let joined = join_values(&[val1, val2]);

    assert_eq!(joined.constant, Some(MockConstantValue::Int(5)));
}

#[test]
fn test_join_nullable_maybe_if_any_maybe() {
    let val1 = MockAbstractValue {
        type_: None,
        range_: None,
        nullable: MockNullability::Never,
        constant: None,
    };
    let val2 = MockAbstractValue {
        type_: None,
        range_: None,
        nullable: MockNullability::Maybe,
        constant: None,
    };

    let joined = join_values(&[val1, val2]);

    assert_eq!(joined.nullable, MockNullability::Maybe);
}

#[test]
fn test_widening_upper_bound_to_infinity() {
    // CAP-AI-09: Growing upper bound -> widen to +inf (None)
    let old = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(0), Some(5))),
        nullable: MockNullability::Never,
        constant: None,
    };
    let new = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(0), Some(10))), // Upper bound grew
        nullable: MockNullability::Never,
        constant: None,
    };

    let widened = widen_value(&old, &new);

    // Upper bound should be widened to +inf (None)
    assert_eq!(widened.range_, Some((Some(0), None)));
}

#[test]
fn test_widening_lower_bound_to_infinity() {
    // CAP-AI-09: Growing lower bound -> widen to -inf (None)
    let old = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(-5), Some(10))),
        nullable: MockNullability::Never,
        constant: None,
    };
    let new = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(-10), Some(10))), // Lower bound grew (more negative)
        nullable: MockNullability::Never,
        constant: None,
    };

    let widened = widen_value(&old, &new);

    // Lower bound should be widened to -inf (None)
    assert_eq!(widened.range_, Some((None, Some(10))));
}

#[test]
fn test_widening_loses_constant() {
    // CAP-AI-09: Widening loses constant information
    let old = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let new = MockAbstractValue::from_constant(MockConstantValue::Int(6));

    let widened = widen_value(&old, &new);

    assert!(widened.constant.is_none(), "Widening should lose constant");
}

// =============================================================================
// SECTION 13: Arithmetic Tests (CAP-AI-13)
// =============================================================================

#[test]
fn test_arithmetic_add() {
    // CAP-AI-13: Abstract arithmetic - addition
    let operand = MockAbstractValue::from_constant(MockConstantValue::Int(5));
    let result = apply_arithmetic(&operand, '+', 3);

    assert_eq!(result.range_, Some((Some(8), Some(8))));
}

#[test]
fn test_arithmetic_subtract() {
    // CAP-AI-13: Abstract arithmetic - subtraction
    let operand = MockAbstractValue::from_constant(MockConstantValue::Int(10));
    let result = apply_arithmetic(&operand, '-', 3);

    assert_eq!(result.range_, Some((Some(7), Some(7))));
}

#[test]
fn test_arithmetic_multiply() {
    // CAP-AI-13: Abstract arithmetic - multiplication
    let operand = MockAbstractValue::from_constant(MockConstantValue::Int(4));
    let result = apply_arithmetic(&operand, '*', 2);

    assert_eq!(result.range_, Some((Some(8), Some(8))));
}

#[test]
fn test_arithmetic_on_range() {
    // Arithmetic on a range
    let operand = MockAbstractValue {
        type_: Some("int".to_string()),
        range_: Some((Some(1), Some(5))),
        nullable: MockNullability::Never,
        constant: None,
    };

    let result = apply_arithmetic(&operand, '+', 10);

    assert_eq!(result.range_, Some((Some(11), Some(15))));
}

// =============================================================================
// SECTION 14: Division-by-Zero Detection Tests (CAP-AI-10)
// =============================================================================

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_div_zero_detected_for_constant_zero() {
    // CAP-AI-10: Division by constant 0 detected
    //
    // Code pattern:
    // x = 0
    // y = 1 / x  # Warning: potential division by zero
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_div_zero.iter().any(|(_, v)| v == "x"));
    todo!("Implement division-by-zero detection");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_div_zero_detected_for_range_including_zero() {
    // CAP-AI-10: Division by variable with range including 0
    //
    // Code pattern:
    // x = some_input()  # range unknown, may be zero
    // y = 1 / x         # Warning: potential division by zero
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_div_zero.len() > 0);
    todo!("Implement range-based div-zero detection");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_div_safe_no_warning_for_constant_nonzero() {
    // CAP-AI-10: No warning when divisor is definitely non-zero
    //
    // Code pattern:
    // x = 5
    // y = 1 / x  # Safe: x is definitely 5
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_div_zero.is_empty());
    todo!("Implement safe division detection");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_div_safe_no_warning_for_positive_range() {
    // Code pattern:
    // x = abs(input) + 1  # range [1, inf), definitely > 0
    // y = 1 / x           # Safe
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_div_zero.is_empty());
    todo!("Implement positive range detection");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_div_zero_intra_block_accuracy() {
    // CAP-AI-20: Intra-block accuracy - use state_out for same-block defs
    //
    // Code pattern:
    // x = 5        # line 1
    // y = 1 / x    # line 2 - should use state_out after x=5, not state_in
    //
    // NO warning expected (x is definitely 5 by line 2)
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_div_zero.is_empty());
    todo!("Implement intra-block state accuracy");
}

// =============================================================================
// SECTION 15: Null Dereference Detection Tests (CAP-AI-11)
// =============================================================================

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_null_deref_detected_at_attribute_access() {
    // CAP-AI-11: Null dereference detected at attribute access
    //
    // Code pattern:
    // x = None
    // y = x.foo  # Warning: potential null dereference
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_null_deref.iter().any(|(_, v)| v == "x"));
    todo!("Implement null dereference detection");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_null_deref_safe_for_non_null_constant() {
    // CAP-AI-11: No warning for definitely non-null value
    //
    // Code pattern:
    // x = "hello"
    // y = x.upper()  # Safe: x is definitely a string
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert!(result.potential_null_deref.is_empty());
    todo!("Implement safe null detection");
}

// =============================================================================
// SECTION 16: compute_abstract_interp Tests
// =============================================================================

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_abstract_interp_returns_info() {
    // Basic: compute_abstract_interp returns AbstractInterpInfo
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // assert_eq!(result.function_name, cfg.function_name);
    todo!("Implement compute_abstract_interp");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_tracks_constant_assignment() {
    // x = 5 should result in x having range [5, 5]
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let val = result.value_at_exit(0, "x");
    // assert_eq!(val.range_, Some((Some(5), Some(5))));
    todo!("Implement constant tracking");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_tracks_variable_copy() {
    // CAP-AI-19: y = x copies abstract value
    //
    // Code pattern:
    // x = 5
    // y = x  # y should have same abstract value as x
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let val_x = result.value_at_exit(0, "x");
    // let val_y = result.value_at_exit(0, "y");
    // assert_eq!(val_x, val_y);
    todo!("Implement variable copy tracking");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_tracks_none_assignment() {
    // x = None should result in x being ALWAYS nullable
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let val = result.value_at_exit(0, "x");
    // assert_eq!(val.nullable, Nullability::Always);
    todo!("Implement None tracking");
}

// =============================================================================
// SECTION 17: Edge Cases
// =============================================================================

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_abstract_interp_empty_function_no_crash() {
    // Empty function should not crash
    //
    // Will test:
    // let result = compute_abstract_interp(&empty_cfg, &refs, None, "python");
    // assert!(result.is_ok());
    todo!("Implement empty function handling");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_unknown_rhs_defaults_to_top() {
    // Unknown RHS (e.g., function call) defaults to top()
    //
    // Code pattern:
    // x = some_unknown_function()  # x should be top()
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let val = result.value_at_exit(0, "x");
    // assert_eq!(val, AbstractValue::top());
    todo!("Implement unknown RHS handling");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_parameter_starts_as_top() {
    // Function parameters start as top() (unknown input)
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let val = result.value_at(0, "param");
    // assert_eq!(val, AbstractValue::top());
    todo!("Implement parameter initialization");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_nested_loops_terminate() {
    // Nested loops should terminate via widening
    //
    // Code pattern:
    // for i in range(n):
    //     for j in range(m):
    //         x = x + 1
    //
    // Will test:
    // let result = compute_abstract_interp(&nested_loop_cfg, &refs, source, "python");
    // assert!(result.is_ok());  // Should not infinite loop
    todo!("Implement nested loop termination");
}

// =============================================================================
// SECTION 18: Multi-Language Support Tests (CAP-AI-15, CAP-AI-16, CAP-AI-17)
// =============================================================================

#[test]
fn test_python_none_keyword_recognized() {
    // CAP-AI-15: Python None keyword
    let keywords = get_null_keywords("python");
    assert!(keywords.contains(&"None"));
}

#[test]
fn test_typescript_null_keyword_recognized() {
    // CAP-AI-15: TypeScript null keyword
    let keywords = get_null_keywords("typescript");
    assert!(keywords.contains(&"null"));
}

#[test]
fn test_typescript_undefined_keyword_recognized() {
    // CAP-AI-15: TypeScript undefined keyword
    let keywords = get_null_keywords("typescript");
    assert!(keywords.contains(&"undefined"));
}

#[test]
fn test_go_nil_keyword_recognized() {
    // CAP-AI-15: Go nil keyword
    let keywords = get_null_keywords("go");
    assert!(keywords.contains(&"nil"));
}

#[test]
fn test_rust_has_no_null_keyword() {
    // CAP-AI-15: Rust has no null (None is Option::None, not null)
    let keywords = get_null_keywords("rust");
    assert!(keywords.is_empty(), "Rust should have no null keywords");
}

#[test]
fn test_python_boolean_capitalized() {
    // CAP-AI-16: Python uses True/False (capitalized)
    let bools = get_boolean_keywords("python");
    assert_eq!(bools.get("True"), Some(&true));
    assert_eq!(bools.get("False"), Some(&false));
}

#[test]
fn test_typescript_boolean_lowercase() {
    // CAP-AI-16: TypeScript uses true/false (lowercase)
    let bools = get_boolean_keywords("typescript");
    assert_eq!(bools.get("true"), Some(&true));
    assert_eq!(bools.get("false"), Some(&false));
}

#[test]
fn test_python_comment_pattern() {
    // CAP-AI-17: Python uses # for comments
    let pattern = get_comment_pattern("python");
    assert_eq!(pattern, "#");
}

#[test]
fn test_typescript_comment_pattern() {
    // CAP-AI-17: TypeScript uses // for comments
    let pattern = get_comment_pattern("typescript");
    assert_eq!(pattern, "//");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_accepts_language_parameter() {
    // compute_abstract_interp should accept language parameter
    //
    // Will test:
    // let result_py = compute_abstract_interp(&cfg, &refs, source, "python").unwrap();
    // let result_ts = compute_abstract_interp(&cfg, &refs, source, "typescript").unwrap();
    // Both should succeed
    todo!("Implement language parameter support");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_with_typescript_null() {
    // Code pattern (TypeScript):
    // let x = null;
    //
    // Should recognize 'null' as null value
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "typescript").unwrap();
    // let val = result.value_at_exit(0, "x");
    // assert_eq!(val.nullable, Nullability::Always);
    todo!("Implement TypeScript null handling");
}

#[test]
#[ignore = "Abstract interpretation not yet implemented"]
fn test_compute_with_go_nil() {
    // Code pattern (Go):
    // x := nil
    //
    // Should recognize 'nil' as null value
    //
    // Will test:
    // let result = compute_abstract_interp(&cfg, &refs, source, "go").unwrap();
    // let val = result.value_at_exit(0, "x");
    // assert_eq!(val.nullable, Nullability::Always);
    todo!("Implement Go nil handling");
}

// =============================================================================
// SECTION 19: Integration with Real Types (Phase 12)
// =============================================================================

/// Integration test: compute_available_exprs with real CFG/DFG
///
/// This test uses the actual dataflow analysis on a simple Python function
/// to verify the module can be used end-to-end.
#[test]
fn test_integration_compute_available_exprs_from_cfg_dfg() {
    use super::compute_available_exprs;
    use crate::cfg::get_cfg_context;
    use crate::dfg::get_dfg_context;
    use crate::types::Language;

    // Use inline Python source with a simple expression pattern
    // x = a + b
    // y = a + b  # This should be detected as redundant
    let source = r#"
def test_func(a, b):
    x = a + b
    y = a + b
    return x + y
"#;

    // Get CFG for the function
    let cfg = get_cfg_context(source, "test_func", Language::Python);
    assert!(
        cfg.is_ok(),
        "CFG extraction should succeed: {:?}",
        cfg.err()
    );
    let cfg = cfg.unwrap();

    // Get DFG for the function
    let dfg = get_dfg_context(source, "test_func", Language::Python);
    assert!(
        dfg.is_ok(),
        "DFG extraction should succeed: {:?}",
        dfg.err()
    );
    let dfg = dfg.unwrap();

    // Compute available expressions
    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "compute_available_exprs should succeed: {:?}",
        result.err()
    );
    let info = result.unwrap();

    // Verify basic properties
    assert!(!info.avail_in.is_empty(), "avail_in should not be empty");
    assert!(!info.avail_out.is_empty(), "avail_out should not be empty");

    // Verify JSON serialization works
    let json = info.to_json();
    assert!(json.is_object(), "to_json should return an object");
    assert!(
        json.get("avail_in").is_some(),
        "JSON should have avail_in field"
    );
    assert!(
        json.get("avail_out").is_some(),
        "JSON should have avail_out field"
    );
    assert!(
        json.get("all_expressions").is_some(),
        "JSON should have all_expressions field"
    );
}

/// Integration test: compute_abstract_interp with real CFG/DFG
///
/// This test uses the actual abstract interpretation analysis on a simple
/// Python function to verify range tracking and nullability work.
#[test]
fn test_integration_compute_abstract_interp_from_cfg() {
    use super::compute_abstract_interp;
    use crate::cfg::get_cfg_context;
    use crate::dfg::get_dfg_context;
    use crate::types::Language;

    // Use inline Python source with constant assignments
    // x = 5        # x should be constant 5, range [5, 5]
    // y = None     # y should be nullable Always
    // z = x + 1    # z should be constant 6, range [6, 6]
    let source = r#"
def test_func():
    x = 5
    y = None
    z = x + 1
    return z
"#;

    // Get CFG for the function
    let cfg = get_cfg_context(source, "test_func", Language::Python);
    assert!(
        cfg.is_ok(),
        "CFG extraction should succeed: {:?}",
        cfg.err()
    );
    let cfg = cfg.unwrap();

    // Get DFG for the function
    let dfg = get_dfg_context(source, "test_func", Language::Python);
    assert!(
        dfg.is_ok(),
        "DFG extraction should succeed: {:?}",
        dfg.err()
    );
    let dfg = dfg.unwrap();

    // Prepare source lines
    let source_lines: Vec<&str> = source.lines().collect();

    // Compute abstract interpretation
    let result = compute_abstract_interp(&cfg, &dfg, Some(&source_lines), "python");
    assert!(
        result.is_ok(),
        "compute_abstract_interp should succeed: {:?}",
        result.err()
    );
    let info = result.unwrap();

    // Verify basic properties
    assert!(!info.state_in.is_empty(), "state_in should not be empty");
    assert!(!info.state_out.is_empty(), "state_out should not be empty");

    // Verify JSON serialization works
    let json = info.to_json();
    assert!(json.is_object(), "to_json should return an object");
    assert!(
        json.get("state_in").is_some(),
        "JSON should have state_in field"
    );
    assert!(
        json.get("state_out").is_some(),
        "JSON should have state_out field"
    );
    assert!(
        json.get("potential_div_zero").is_some(),
        "JSON should have potential_div_zero field"
    );
    assert!(
        json.get("potential_null_deref").is_some(),
        "JSON should have potential_null_deref field"
    );
}

// =============================================================================
// SECTION 20: Adversarial & Edge Case Tests (Phase 13)
// =============================================================================
//
// These tests address issues found in pre-mortem pass 3:
// - Switch/match with 5+ arms
// - Try-catch error handling
// - Early returns in block middle
// - Deeply nested expressions (stack overflow prevention)
// - Pathological CFGs (10k+ blocks)
// - Range overflow (saturating arithmetic)

/// Test that switch/match with 5+ arms is handled correctly.
///
/// Pre-mortem risk: Complex control flow with many arms could cause issues
/// in available expressions analysis (merge points must intersect all paths).
///
/// This test verifies that the MUST analysis correctly handles diamond-like
/// patterns with multiple branches.
#[test]
fn test_switch_five_arms_available_expr() {
    use super::compute_available_exprs;
    use crate::types::{BlockType, RefType};

    // Create a CFG with 5 arms:
    //       0 (entry)
    //      /|\\\
    //     1 2 3 4 5  (arms)
    //      \|///
    //       6 (merge)
    //
    // Expression "a + b" computed only in arm 1
    // Should NOT be available at merge (MUST semantics)

    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::Body, (2, 2)),
            (2, BlockType::Body, (3, 3)),
            (3, BlockType::Body, (4, 4)),
            (4, BlockType::Body, (5, 5)),
            (5, BlockType::Body, (6, 6)),
            (6, BlockType::Exit, (7, 7)),
        ],
        // Edges from entry to all 5 arms, then all arms to merge
        vec![
            (0, 1),
            (0, 2),
            (0, 3),
            (0, 4),
            (0, 5),
            (1, 6),
            (2, 6),
            (3, 6),
            (4, 6),
            (5, 6),
        ],
        0,
    );

    // DFG: "a" and "b" used in arm 1, "x" defined in arm 1
    // x = a + b only in arm 1
    let dfg = make_dfg_with_refs_for_phase3(vec![
        make_var_ref_for_phase3("a", 2, RefType::Use),
        make_var_ref_for_phase3("b", 2, RefType::Use),
        make_var_ref_for_phase3("x", 2, RefType::Definition),
    ]);

    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "compute_available_exprs should succeed for 5-arm switch: {:?}",
        result.err()
    );
    let info = result.unwrap();

    // Expression should be available at exit of arm 1
    assert!(
        info.avail_out.contains_key(&1),
        "Block 1 should have avail_out"
    );

    // Expression should NOT be available at entry of merge block 6
    // (MUST analysis: expression only in 1 of 5 paths)
    let avail_at_merge = info.avail_in.get(&6).cloned().unwrap_or_default();
    // If there are expressions, none should be "a + b" since it's only computed in 1 arm
    for expr in &avail_at_merge {
        // Check that no expression depending on both a and b is available
        // This validates MUST semantics
        let has_both =
            expr.operands.contains(&"a".to_string()) && expr.operands.contains(&"b".to_string());
        assert!(
            !has_both,
            "Expression 'a + b' should NOT be available at merge block (only in 1 of 5 arms)"
        );
    }
}

/// Test that try-catch patterns return clear errors or handle edges correctly.
///
/// Pre-mortem risk: Exception handling creates implicit CFG edges that may not
/// be in the successor list, causing unsound available expressions analysis.
///
/// This test documents the expected behavior: analysis completes but may
/// produce conservative results for exception-heavy code.
#[test]
fn test_try_catch_returns_error_or_handles() {
    use super::compute_available_exprs;
    use crate::types::{BlockType, RefType};

    // Simulate a try-catch CFG:
    //   0 (entry/try start)
    //   |
    //   1 (try body - may throw)
    //  / \
    // 2   3 (catch and normal exit)
    //  \ /
    //   4 (finally/exit)
    //
    // In a proper CFG, exceptions create edges from 1->2
    // We test that analysis handles this gracefully

    let cfg = make_test_cfg_for_phase3(
        vec![
            (0, BlockType::Entry, (1, 1)),
            (1, BlockType::Body, (2, 3)), // try body
            (2, BlockType::Body, (4, 5)), // catch
            (3, BlockType::Body, (6, 6)), // normal path
            (4, BlockType::Exit, (7, 7)), // finally
        ],
        vec![(0, 1), (1, 2), (1, 3), (2, 4), (3, 4)],
        0,
    );

    // Expression in try body
    let dfg = make_dfg_with_refs_for_phase3(vec![
        make_var_ref_for_phase3("a", 2, RefType::Use),
        make_var_ref_for_phase3("b", 2, RefType::Use),
        make_var_ref_for_phase3("x", 2, RefType::Definition),
    ]);

    let result = compute_available_exprs(&cfg, &dfg);

    // Analysis should either:
    // 1. Complete successfully (handling exception edges conservatively)
    // 2. Return an UnsupportedCfgPattern error
    match result {
        Ok(info) => {
            // Analysis completed - verify it produces valid results
            assert!(
                info.avail_in.contains_key(&0),
                "Entry block should have avail_in"
            );
            // Conservative: expression from try body may or may not be at finally
            // (depends on whether exception path is properly modeled)
        }
        Err(super::DataflowError::UnsupportedCfgPattern { .. }) => {
            // This is acceptable - explicitly rejecting unsupported patterns
        }
        Err(other) => {
            panic!("Unexpected error for try-catch CFG: {:?}", other);
        }
    }
}

/// Test that early returns in block middle are handled correctly.
///
/// Pre-mortem risk: Code like `x = a+b; return 5; a = 10;` - the `a = 10` is
/// unreachable but might still be processed as killing `a+b`.
///
/// This test verifies that gen/kill computation handles early returns properly.
#[test]
fn test_early_return_intra_block_handling() {
    use super::compute_available_exprs;
    use crate::types::{BlockType, RefType};

    // In practice, a well-formed CFG would split at returns, but we test
    // that the analysis handles the case gracefully even if not split.
    //
    // Block 0: x = a + b; return x;
    // Block 1: unreachable (but in CFG for testing)

    let cfg = make_test_cfg_for_phase3(
        vec![(0, BlockType::Entry, (1, 2)), (1, BlockType::Exit, (3, 3))],
        vec![(0, 1)],
        0,
    );

    // Block 0: x = a + b
    // The expression is computed, operands are used
    let dfg = make_dfg_with_refs_for_phase3(vec![
        make_var_ref_for_phase3("a", 1, RefType::Use),
        make_var_ref_for_phase3("b", 1, RefType::Use),
        make_var_ref_for_phase3("x", 1, RefType::Definition),
    ]);

    let result = compute_available_exprs(&cfg, &dfg);
    assert!(
        result.is_ok(),
        "Analysis should handle simple early return pattern: {:?}",
        result.err()
    );

    let info = result.unwrap();
    // Expression should be available at exit of block 0
    // Should have computed the expression (not killed by unreachable code)
    assert!(
        info.avail_out.contains_key(&0),
        "Block 0 should have an avail_out entry"
    );
}

/// Test that deeply nested expressions don't cause stack overflow.
///
/// Pre-mortem risk: Expression like `((((((a + b) + c) + d) + e) + f) + g)`
/// could cause stack overflow in recursive processing.
///
/// This test verifies the analysis handles deep nesting gracefully.
#[test]
fn test_deeply_nested_expression_limited() {
    use super::{normalize_expression, Expression};

    // Test normalization of deeply nested-looking expression
    // The analysis doesn't recursively parse expressions, it normalizes
    // binary operations. This test ensures that doesn't cause issues.

    // Create a series of nested normalizations
    let mut result = "a".to_string();
    for i in 0..100 {
        let var = format!("v{}", i);
        result = normalize_expression(&result, "+", &var);
    }

    // Should complete without stack overflow
    assert!(
        result.contains('+'),
        "Deeply nested normalization should complete"
    );
    assert!(
        result.len() > 100,
        "Result should be built from many operations"
    );

    // Test Expression with many operands
    let many_operands: Vec<String> = (0..100).map(|i| format!("var{}", i)).collect();
    let expr = Expression::new(
        "complex expression text",
        many_operands.iter().map(|s| s.as_str()).collect(),
        1,
    );

    // is_killed_by should work efficiently
    assert!(
        expr.is_killed_by("var0"),
        "Should detect kill for first operand"
    );
    assert!(
        expr.is_killed_by("var99"),
        "Should detect kill for last operand"
    );
    assert!(
        !expr.is_killed_by("not_present"),
        "Should not false-positive"
    );
}

/// Test that pathological CFG with 10k+ blocks returns error (exceeds MAX_BLOCKS).
///
/// Pre-mortem risk: 10,000 blocks with 1000 predecessors each = 10M ops.
/// Analysis could hang or run for minutes.
///
/// TIGER-PASS3-4: MAX_BLOCKS constant should reject such CFGs early.
#[test]
fn test_pathological_cfg_10k_blocks_limited() {
    use super::{compute_available_exprs, DataflowError, MAX_BLOCKS};
    use crate::types::{BlockType, CfgBlock, CfgEdge, CfgInfo, DfgInfo, EdgeType};
    use std::collections::HashMap;

    // Create a CFG with MAX_BLOCKS + 1 blocks
    let block_count = MAX_BLOCKS + 1; // 10001 blocks

    let blocks: Vec<CfgBlock> = (0..block_count)
        .map(|id| CfgBlock {
            id,
            block_type: if id == 0 {
                BlockType::Entry
            } else {
                BlockType::Body
            },
            lines: (id as u32, id as u32),
            calls: vec![],
        })
        .collect();

    // Linear chain of edges (simplest pathological case)
    let edges: Vec<CfgEdge> = (0..block_count - 1)
        .map(|i| CfgEdge {
            from: i,
            to: i + 1,
            edge_type: EdgeType::Unconditional,
            condition: None,
        })
        .collect();

    let cfg = CfgInfo {
        function: "pathological".to_string(),
        blocks,
        edges,
        entry_block: 0,
        exit_blocks: vec![block_count - 1],
        cyclomatic_complexity: 1,
        nested_functions: HashMap::new(),
    };

    let dfg = DfgInfo {
        function: "pathological".to_string(),
        refs: vec![],
        edges: vec![],
        variables: vec![],
    };

    let result = compute_available_exprs(&cfg, &dfg);

    // Should return TooManyBlocks error
    match result {
        Err(DataflowError::TooManyBlocks { count }) => {
            assert_eq!(count, block_count, "Error should report actual block count");
        }
        Ok(_) => {
            panic!(
                "Analysis should reject CFG with {} blocks (exceeds MAX_BLOCKS={})",
                block_count, MAX_BLOCKS
            );
        }
        Err(other) => {
            panic!("Expected TooManyBlocks error, got: {:?}", other);
        }
    }
}

/// Test that range overflow uses saturating arithmetic (no panic).
///
/// Pre-mortem risk: Range arithmetic at i64::MAX could panic on overflow.
///
/// TIGER-PASS1-11: Use saturating_add/sub/mul for all range operations.
#[test]
fn test_range_overflow_saturates() {
    use super::abstract_interp::apply_arithmetic;
    use super::{AbstractValue, ConstantValue};

    // Test addition at i64::MAX
    let max_val = AbstractValue::from_constant(ConstantValue::Int(i64::MAX));
    let result = apply_arithmetic(&max_val, '+', 1);

    // Should not panic, should widen to unbounded
    // The high bound should be None (infinity) after overflow
    if let Some((low, high)) = result.range_ {
        // Either the result is saturated to MAX, or widened to None.
        if let Some(h) = high {
            assert_eq!(h, i64::MAX, "Saturated to MAX");
        }
        // Low bound should still be present or widened.
        if let Some(l) = low {
            assert!(l >= i64::MAX - 1, "Low bound reasonable after saturation");
        }
    }

    // Test subtraction at i64::MIN
    let min_val = AbstractValue::from_constant(ConstantValue::Int(i64::MIN));
    let result = apply_arithmetic(&min_val, '-', 1);

    // Should not panic
    if let Some((Some(l), _high)) = result.range_ {
        assert_eq!(l, i64::MIN, "Saturated to MIN");
    }

    // Test multiplication overflow
    let large_val = AbstractValue::from_constant(ConstantValue::Int(i64::MAX / 2 + 1));
    let result = apply_arithmetic(&large_val, '*', 3);

    // Should not panic - overflow should be handled
    // Result should be widened to None bounds or saturated
    assert!(
        result.range_.is_some() || result.range_.is_none(),
        "Multiplication overflow should be handled gracefully"
    );

    // Test that normal arithmetic still works
    let normal_val = AbstractValue::from_constant(ConstantValue::Int(100));
    let result = apply_arithmetic(&normal_val, '+', 50);
    match result.range_ {
        Some((Some(low), Some(high))) => {
            assert_eq!(low, 150, "Normal addition should work");
            assert_eq!(high, 150, "Normal addition should work");
        }
        _ => panic!("Normal arithmetic should produce bounded range"),
    }

    // Test negative multiplication (bounds swap)
    let pos_val = AbstractValue::from_constant(ConstantValue::Int(10));
    let result = apply_arithmetic(&pos_val, '*', -2);
    match result.range_ {
        Some((Some(low), Some(high))) => {
            assert_eq!(low, -20, "Negative mult should work");
            assert_eq!(high, -20, "Negative mult should work");
        }
        _ => panic!("Negative multiplication should produce bounded range"),
    }
}

// =============================================================================
// Summary: Total Test Count
// =============================================================================
//
// Available Expressions Tests (32):
// - Expression struct: 6 tests
// - Commutative normalization: 5 tests
// - AvailableExprsInfo: 6 tests
// - MUST analysis: 5 tests
// - redundant_computations: 4 tests
// - CFG patterns: 6 tests
//
// Abstract Interpretation Tests (56):
// - Nullability: 2 tests
// - AbstractValue: 16 tests
// - AbstractState: 6 tests
// - AbstractInterpInfo: 8 tests
// - Join/Widening: 6 tests
// - Arithmetic: 4 tests
// - Division-by-zero: 5 tests
// - Null dereference: 2 tests
// - Edge cases: 4 tests
// - Multi-language: 11 tests
// - Integration: 2 tests
//
// Adversarial Tests (Phase 13): 6 tests
// - test_switch_five_arms_available_expr
// - test_try_catch_returns_error_or_handles
// - test_early_return_intra_block_handling
// - test_deeply_nested_expression_limited
// - test_pathological_cfg_10k_blocks_limited
// - test_range_overflow_saturates
//
// Uncertain Findings Tests (DFG enrichment): 8 tests
// - test_confidence_enum_values
// - test_confidence_default_is_low
// - test_confidence_serialization
// - test_uncertain_finding_construction
// - test_uncertain_finding_serialization
// - test_available_exprs_info_has_uncertain_fields
// - test_available_exprs_info_uncertain_in_to_json
// - test_available_exprs_function_call_becomes_uncertain
// =============================================================================

// =============================================================================
// Uncertain Findings Tests - Confidence and UncertainFinding types
// =============================================================================

#[test]
fn test_confidence_enum_values() {
    use super::available::Confidence;
    // Confidence should have Low, Medium, High variants
    let low = Confidence::Low;
    let med = Confidence::Medium;
    let high = Confidence::High;
    assert_ne!(format!("{:?}", low), format!("{:?}", high));
    assert_ne!(format!("{:?}", med), format!("{:?}", low));
}

#[test]
fn test_confidence_default_is_low() {
    use super::available::Confidence;
    let c: Confidence = Default::default();
    assert_eq!(c, Confidence::Low);
}

#[test]
fn test_confidence_serialization() {
    use super::available::Confidence;
    // Should serialize to lowercase string
    let json = serde_json::to_string(&Confidence::High).unwrap();
    assert_eq!(json, "\"high\"");
    let json = serde_json::to_string(&Confidence::Medium).unwrap();
    assert_eq!(json, "\"medium\"");
    let json = serde_json::to_string(&Confidence::Low).unwrap();
    assert_eq!(json, "\"low\"");
}

#[test]
fn test_uncertain_finding_construction() {
    use super::available::UncertainFinding;
    let uf = UncertainFinding {
        expr: "foo() + x".to_string(),
        line: 42,
        reason: "contains function call - purity unknown".to_string(),
    };
    assert_eq!(uf.expr, "foo() + x");
    assert_eq!(uf.line, 42);
    assert_eq!(uf.reason, "contains function call - purity unknown");
}

#[test]
fn test_uncertain_finding_serialization() {
    use super::available::UncertainFinding;
    let uf = UncertainFinding {
        expr: "obj.method() + y".to_string(),
        line: 15,
        reason: "method access - purity unknown".to_string(),
    };
    let json = serde_json::to_string(&uf).unwrap();
    assert!(json.contains("\"expr\""));
    assert!(json.contains("\"line\""));
    assert!(json.contains("\"reason\""));
    let deserialized: UncertainFinding = serde_json::from_str(&json).unwrap();
    assert_eq!(deserialized.expr, uf.expr);
    assert_eq!(deserialized.line, uf.line);
}

#[test]
fn test_available_exprs_info_has_uncertain_fields() {
    use super::available::{AvailableExprsInfo, Confidence};
    let info = AvailableExprsInfo::empty(0);
    // New fields should exist and be empty/default
    assert!(info.uncertain_exprs.is_empty());
    assert_eq!(info.confidence, Confidence::Low);
}

#[test]
fn test_available_exprs_info_uncertain_in_to_json() {
    use super::available::{AvailableExprsInfo, Confidence, UncertainFinding};
    let mut info = AvailableExprsInfo::empty(0);
    info.uncertain_exprs.push(UncertainFinding {
        expr: "foo() + bar()".to_string(),
        line: 23,
        reason: "function calls may have side effects".to_string(),
    });
    info.confidence = Confidence::Medium;

    let json = info.to_json();
    // to_json should include uncertain_exprs and confidence
    let obj = json
        .as_object()
        .unwrap_or_else(|| panic!("expected JSON object"));
    assert!(
        obj.contains_key("uncertain_exprs"),
        "JSON should have uncertain_exprs key"
    );
    assert!(
        obj.contains_key("confidence"),
        "JSON should have confidence key"
    );

    let uncertain = obj["uncertain_exprs"].as_array().unwrap();
    assert_eq!(uncertain.len(), 1);
    assert_eq!(uncertain[0]["expr"], "foo() + bar()");
    assert_eq!(uncertain[0]["line"], 23);

    assert_eq!(obj["confidence"], "medium");
}

#[test]
fn test_available_exprs_function_call_becomes_uncertain() {
    // When parse_expression_from_line skips a function call expression,
    // it should be collected as uncertain instead of silently discarded
    use super::available::is_function_call;
    // Verify the current behavior: function calls are detected
    assert!(is_function_call("foo(x)"));
    assert!(is_function_call("bar.baz(1, 2)"));
    assert!(!is_function_call("a + b"));
    // The actual integration of uncertain collection will be tested
    // via compute_available_exprs_with_source_and_lang
}