decy-core 2.2.0

Core transpilation pipeline for C-to-Rust conversion
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
//! Deep coverage tests for decy-core lib.rs.
//!
//! Targets uncovered branches in:
//! - `process_ast_to_rust` (line 1355, 34.7% coverage)
//! - `transpile_with_includes` (line 845, 79.4% coverage)
//!
//! Exercises diverse C language features through the full pipeline:
//! structs, enums, typedefs, global variables, multiple functions,
//! pointer operations, arrays, type casting, error paths, and
//! deduplication logic.

use decy_core::{
    transpile, transpile_from_file_path, transpile_with_box_transform, transpile_with_includes,
    transpile_with_trace, transpile_with_verification, DependencyGraph, ProjectContext,
    TranspilationCache, TranspiledFile,
};
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper: Create temporary C file with content.
fn create_temp_c_file(dir: &TempDir, name: &str, content: &str) -> PathBuf {
    let path = dir.path().join(name);
    std::fs::write(&path, content).expect("Failed to write temp file");
    path
}

// ============================================================================
// STRUCT DEFINITION AND ACCESS
// ============================================================================

#[test]
fn deep_transpile_struct_definition() {
    let c_code = r#"
        struct Point {
            int x;
            int y;
        };

        int main() {
            struct Point p;
            p.x = 10;
            p.y = 20;
            return p.x + p.y;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Struct transpilation should succeed: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Point"), "Should contain struct name Point");
    assert!(rust.contains("fn main"), "Should contain main function");
}

#[test]
fn deep_transpile_struct_with_multiple_types() {
    let c_code = r#"
        struct Record {
            int id;
            float value;
            double precision;
            char label;
        };

        int main() {
            struct Record r;
            r.id = 1;
            r.value = 3.14;
            return r.id;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Struct with mixed types should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Record"), "Should contain struct Record");
    assert!(rust.contains("i32") || rust.contains("id"), "Should map int field");
}

#[test]
fn deep_transpile_nested_struct_access() {
    let c_code = r#"
        struct Inner {
            int value;
        };

        struct Outer {
            struct Inner inner;
            int count;
        };

        int main() {
            struct Outer o;
            o.count = 5;
            return o.count;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Nested struct should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Inner"), "Should contain Inner struct");
    assert!(rust.contains("Outer"), "Should contain Outer struct");
}

// ============================================================================
// ENUM DEFINITIONS
// ============================================================================

#[test]
fn deep_transpile_enum_definition() {
    let c_code = r#"
        enum Color {
            RED,
            GREEN,
            BLUE
        };

        int main() {
            enum Color c = RED;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Enum transpilation should succeed: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("RED") || rust.contains("Color"), "Should contain enum definition");
}

#[test]
fn deep_transpile_enum_with_values() {
    let c_code = r#"
        enum Status {
            OK = 0,
            ERROR = 1,
            PENDING = 2
        };

        int main() {
            return OK;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Enum with values should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("OK") || rust.contains("Status"), "Should contain enum");
}

// ============================================================================
// TYPEDEF DEFINITIONS
// ============================================================================

#[test]
fn deep_transpile_typedef_int() {
    let c_code = r#"
        typedef int myint;

        myint add(myint a, myint b) {
            return a + b;
        }

        int main() {
            myint x = add(2, 3);
            return x;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Typedef should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn add") || rust.contains("fn main"), "Should have functions");
}

#[test]
fn deep_transpile_typedef_struct() {
    let c_code = r#"
        typedef struct {
            int x;
            int y;
        } Point;

        int main() {
            Point p;
            p.x = 1;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Typedef struct should transpile: {:?}", result.err());
}

// ============================================================================
// GLOBAL VARIABLES (all type defaults)
// ============================================================================

#[test]
fn deep_transpile_global_int() {
    let c_code = r#"
        int counter = 0;

        int main() {
            counter = 42;
            return counter;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global int should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(
        rust.contains("static mut") || rust.contains("counter"),
        "Should generate global variable"
    );
}

#[test]
fn deep_transpile_global_float_uninit() {
    let c_code = r#"
        float temperature;

        int main() {
            temperature = 98.6;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global float should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("0.0") || rust.contains("temperature"), "Should default-init float");
}

#[test]
fn deep_transpile_global_double() {
    let c_code = r#"
        double precision;

        int main() {
            precision = 3.14159;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global double should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_char_uninit() {
    let c_code = r#"
        char marker;

        int main() {
            marker = 'X';
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global char should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_unsigned_int() {
    let c_code = r#"
        unsigned int flags = 0;

        int main() {
            flags = 255;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global unsigned int should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_pointer_uninit() {
    let c_code = r#"
        int* global_ptr;

        int main() {
            int x = 5;
            return x;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global pointer should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(
        rust.contains("null_mut") || rust.contains("global_ptr"),
        "Should default-init pointer to null"
    );
}

#[test]
fn deep_transpile_global_array_int_uninit() {
    let c_code = r#"
        int buffer[100];

        int main() {
            buffer[0] = 42;
            return buffer[0];
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global int array should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("100") || rust.contains("buffer"), "Should preserve array size");
}

#[test]
fn deep_transpile_global_array_char_uninit() {
    let c_code = r#"
        char name[50];

        int main() {
            name[0] = 'H';
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global char array should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_array_float_uninit() {
    let c_code = r#"
        float values[10];

        int main() {
            values[0] = 1.0;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global float array should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_array_double_uninit() {
    let c_code = r#"
        double measurements[5];

        int main() {
            measurements[0] = 1.5;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global double array should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_array_unsigned_int_uninit() {
    let c_code = r#"
        unsigned int masks[8];

        int main() {
            masks[0] = 0xFF;
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global unsigned int array should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_with_initializer() {
    let c_code = r#"
        int max_count = 100;
        float pi = 3.14;

        int main() {
            return max_count;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Globals with initializers should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(
        rust.contains("100") || rust.contains("max_count"),
        "Should preserve initializer value"
    );
}

// ============================================================================
// MULTIPLE FUNCTION DEFINITIONS
// ============================================================================

#[test]
fn deep_transpile_multiple_functions() {
    let c_code = r#"
        int square(int x) {
            return x * x;
        }

        int cube(int x) {
            return x * x * x;
        }

        int main() {
            int a = square(3);
            int b = cube(2);
            return a + b;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Multiple functions should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn square"), "Should contain square");
    assert!(rust.contains("fn cube"), "Should contain cube");
    assert!(rust.contains("fn main"), "Should contain main");
}

#[test]
fn deep_transpile_void_function() {
    let c_code = r#"
        void do_nothing() {
        }

        int main() {
            do_nothing();
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Void function should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn do_nothing"), "Should contain void function");
}

#[test]
fn deep_transpile_function_with_many_params() {
    let c_code = r#"
        int add_four(int a, int b, int c, int d) {
            return a + b + c + d;
        }

        int main() {
            return add_four(1, 2, 3, 4);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Function with many params should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn add_four"), "Should contain function");
}

// ============================================================================
// FUNCTION DEDUPLICATION (DECY-190)
// ============================================================================

#[test]
fn deep_transpile_function_deduplication() {
    // Declaration followed by definition - should deduplicate
    let c_code = r#"
        int add(int a, int b);

        int add(int a, int b) {
            return a + b;
        }

        int main() {
            return add(1, 2);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Function dedup should work: {:?}", result.err());
    let rust = result.unwrap();
    // Should only have one fn add definition, not two
    let add_count = rust.matches("fn add").count();
    assert!(add_count <= 2, "Should deduplicate: found {} occurrences of fn add", add_count);
}

// ============================================================================
// POINTER OPERATIONS
// ============================================================================

#[test]
fn deep_transpile_pointer_param() {
    let c_code = r#"
        void increment(int* ptr) {
            (*ptr) = (*ptr) + 1;
        }

        int main() {
            int x = 5;
            increment(&x);
            return x;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Pointer param should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn increment"), "Should contain increment function");
}

#[test]
fn deep_transpile_double_pointer() {
    let c_code = r#"
        int main() {
            int x = 42;
            int* p = &x;
            return *p;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Pointer variable should transpile: {:?}", result.err());
}

// ============================================================================
// ARRAY OPERATIONS
// ============================================================================

#[test]
fn deep_transpile_array_declaration() {
    let c_code = r#"
        int main() {
            int arr[5];
            arr[0] = 10;
            arr[1] = 20;
            return arr[0] + arr[1];
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Array declaration should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn main"), "Should contain main");
}

#[test]
fn deep_transpile_array_parameter() {
    let c_code = r#"
        int sum(int* arr, int len) {
            int total = 0;
            int i;
            for (i = 0; i < len; i++) {
                total = total + arr[i];
            }
            return total;
        }

        int main() {
            int data[3];
            data[0] = 1;
            data[1] = 2;
            data[2] = 3;
            return sum(data, 3);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Array parameter should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn sum"), "Should contain sum function");
}

// ============================================================================
// CONTROL FLOW
// ============================================================================

#[test]
fn deep_transpile_if_else() {
    let c_code = r#"
        int abs_val(int x) {
            if (x < 0) {
                return -x;
            } else {
                return x;
            }
        }

        int main() {
            return abs_val(-5);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "If/else should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn abs_val"), "Should contain abs_val function");
}

#[test]
fn deep_transpile_while_loop() {
    let c_code = r#"
        int count_down(int n) {
            int count = 0;
            while (n > 0) {
                count = count + 1;
                n = n - 1;
            }
            return count;
        }

        int main() {
            return count_down(10);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "While loop should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("while") || rust.contains("loop"), "Should contain loop");
}

#[test]
fn deep_transpile_for_loop() {
    let c_code = r#"
        int sum_to(int n) {
            int sum = 0;
            int i;
            for (i = 0; i <= n; i++) {
                sum = sum + i;
            }
            return sum;
        }

        int main() {
            return sum_to(10);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "For loop should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_switch_statement() {
    let c_code = r#"
        int classify(int x) {
            switch (x) {
                case 0: return 0;
                case 1: return 1;
                default: return -1;
            }
        }

        int main() {
            return classify(1);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Switch should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("match") || rust.contains("classify"), "Should convert switch to match");
}

// ============================================================================
// TYPE CASTING AND EXPRESSIONS
// ============================================================================

#[test]
fn deep_transpile_type_cast() {
    let c_code = r#"
        int main() {
            float f = 3.14;
            int i = (int)f;
            return i;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Type cast should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_complex_expressions() {
    let c_code = r#"
        int compute(int a, int b) {
            return (a + b) * (a - b) / 2;
        }

        int main() {
            return compute(10, 3);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Complex expressions should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn compute"), "Should contain compute");
}

#[test]
fn deep_transpile_ternary_expression() {
    let c_code = r#"
        int max(int a, int b) {
            return (a > b) ? a : b;
        }

        int main() {
            return max(5, 10);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Ternary should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_bitwise_operations() {
    let c_code = r#"
        int bitops(int a, int b) {
            int x = a & b;
            int y = a | b;
            int z = a ^ b;
            return x + y + z;
        }

        int main() {
            return bitops(0xFF, 0x0F);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Bitwise ops should transpile: {:?}", result.err());
}

// ============================================================================
// ERROR PATHS (Invalid C)
// ============================================================================

#[test]
fn deep_transpile_error_missing_semicolon() {
    let c_code = "int main() { return 0 }";
    let result = transpile(c_code);
    // This may or may not error depending on parser tolerance
    // Either result is acceptable, we just want no panic
    let _ = result;
}

#[test]
fn deep_transpile_error_empty_input() {
    let c_code = "";
    let result = transpile(c_code);
    // Empty input should either produce empty output or error gracefully
    let _ = result;
}

#[test]
fn deep_transpile_error_only_comments() {
    let c_code = "/* this is a comment */";
    let result = transpile(c_code);
    // Comment-only input should be handled gracefully
    let _ = result;
}

#[test]
fn deep_transpile_error_garbage() {
    let c_code = "@#$%^&*!!!";
    let result = transpile(c_code);
    // Garbage input should error, not panic
    let _ = result;
}

#[test]
fn deep_transpile_error_incomplete_function() {
    let c_code = "int main(";
    let result = transpile(c_code);
    assert!(result.is_err(), "Incomplete function should fail");
}

// ============================================================================
// transpile_with_includes PATHS
// ============================================================================

#[test]
fn deep_transpile_with_includes_no_base_dir() {
    let c_code = "int main() { return 0; }";
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Should work with no base dir: {:?}", result.err());
}

#[test]
fn deep_transpile_with_includes_with_base_dir() {
    let temp = TempDir::new().unwrap();
    let c_code = "int main() { return 0; }";
    let result = transpile_with_includes(c_code, Some(temp.path()));
    assert!(result.is_ok(), "Should work with base dir: {:?}", result.err());
}

#[test]
fn deep_transpile_with_includes_struct_and_func() {
    let c_code = r#"
        struct Pair {
            int first;
            int second;
        };

        int sum_pair(int a, int b) {
            return a + b;
        }

        int main() {
            return sum_pair(1, 2);
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Struct+func should work via includes path: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Pair"), "Should contain Pair struct");
    assert!(rust.contains("fn sum_pair"), "Should contain function");
}

#[test]
fn deep_transpile_with_includes_global_vars() {
    let c_code = r#"
        int global_x = 10;
        float global_y;

        int main() {
            return global_x;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global vars should work: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("global_x"), "Should contain global variable");
}

#[test]
fn deep_transpile_with_includes_enum_and_typedef() {
    let c_code = r#"
        enum Direction { NORTH, SOUTH, EAST, WEST };
        typedef int score_t;

        int main() {
            score_t s = 100;
            return s;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Enum+typedef should transpile: {:?}", result.err());
}

// ============================================================================
// transpile_from_file_path (exercises process_ast_to_rust)
// ============================================================================

#[test]
fn deep_transpile_from_file_path_simple() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(&temp, "simple.c", "int main() { return 0; }");
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "File path transpilation should succeed: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn main"), "Should contain main");
}

#[test]
fn deep_transpile_from_file_path_with_structs() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "structs.c",
        r#"
        struct Vec3 {
            float x;
            float y;
            float z;
        };

        float dot(float ax, float ay, float az, float bx, float by, float bz) {
            return ax * bx + ay * by + az * bz;
        }

        int main() {
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "File with structs should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Vec3"), "Should contain Vec3 struct");
}

#[test]
fn deep_transpile_from_file_path_with_enums() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "enums.c",
        r#"
        enum Level { LOW = 0, MEDIUM = 1, HIGH = 2 };

        int main() {
            return LOW;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "File with enums should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_from_file_path_with_globals() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "globals.c",
        r#"
        int counter = 0;
        float ratio = 0.5;
        char flag = 'N';

        int main() {
            counter = 42;
            return counter;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "File with globals should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("counter"), "Should contain global counter");
}

#[test]
fn deep_transpile_from_file_path_with_typedefs() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "typedefs.c",
        r#"
        typedef int int32;
        typedef unsigned int uint32;

        int32 add(int32 a, int32 b) {
            return a + b;
        }

        int main() {
            int32 result = add(1, 2);
            return result;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "File with typedefs should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_from_file_path_nonexistent() {
    let result = transpile_from_file_path(std::path::Path::new("/tmp/nonexistent_decy_test.c"));
    assert!(result.is_err(), "Nonexistent file should error");
}

#[test]
fn deep_transpile_from_file_path_all_constructs() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "all.c",
        r#"
        struct Data {
            int id;
            float value;
        };

        enum Type { INT_TYPE, FLOAT_TYPE };

        typedef int myint;

        int global_counter = 0;

        myint process(myint x) {
            return x + 1;
        }

        int main() {
            struct Data d;
            d.id = 1;
            d.value = 2.0;
            global_counter = process(d.id);
            return global_counter;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "All constructs together should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Data"), "Should contain Data struct");
    assert!(rust.contains("fn process"), "Should contain process function");
    assert!(rust.contains("fn main"), "Should contain main function");
}

// ============================================================================
// transpile_with_verification PATHS
// ============================================================================

#[test]
fn deep_transpile_with_verification_success() {
    let c_code = "int main() { return 0; }";
    let result = transpile_with_verification(c_code);
    assert!(result.is_ok(), "Verification should succeed: {:?}", result.err());
    let transpilation = result.unwrap();
    assert!(!transpilation.rust_code.is_empty(), "Should produce code");
}

#[test]
fn deep_transpile_with_verification_failure() {
    let c_code = "int main( {";
    let result = transpile_with_verification(c_code);
    // transpile_with_verification wraps errors into TranspilationResult, should not error
    assert!(result.is_ok(), "Verification wrapper should not propagate error");
    let transpilation = result.unwrap();
    assert!(
        !transpilation.errors.is_empty() || transpilation.rust_code.is_empty(),
        "Should record errors for invalid code"
    );
}

// ============================================================================
// transpile_with_trace PATHS (additional coverage)
// ============================================================================

#[test]
fn deep_transpile_with_trace_struct() {
    let c_code = r#"
        struct Item { int count; };
        int main() { return 0; }
    "#;
    let result = transpile_with_trace(c_code);
    assert!(result.is_ok(), "Trace with struct should work: {:?}", result.err());
    let (code, trace) = result.unwrap();
    assert!(code.contains("Item"), "Should contain struct");
    assert!(!trace.entries().is_empty(), "Should have trace entries");
}

#[test]
fn deep_transpile_with_trace_global_var() {
    let c_code = r#"
        int g = 42;
        int main() { return g; }
    "#;
    let result = transpile_with_trace(c_code);
    assert!(result.is_ok(), "Trace with global should work: {:?}", result.err());
}

// ============================================================================
// transpile_with_box_transform
// ============================================================================

#[test]
fn deep_transpile_with_box_transform_simple() {
    let c_code = r#"
        int main() {
            int x = 42;
            return x;
        }
    "#;
    let result = transpile_with_box_transform(c_code);
    assert!(result.is_ok(), "Box transform should work for simple code: {:?}", result.err());
}

// ============================================================================
// STRUCT DEDUPLICATION
// ============================================================================

#[test]
fn deep_transpile_struct_deduplication() {
    // When struct appears multiple times (e.g., forward decl + definition)
    let c_code = r#"
        struct Node {
            int value;
        };

        int main() {
            struct Node n;
            n.value = 5;
            return n.value;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Struct dedup should work: {:?}", result.err());
    let rust = result.unwrap();
    // Verify struct is emitted
    assert!(rust.contains("Node"), "Should contain Node");
}

// ============================================================================
// GLOBAL VARIABLE DEDUPLICATION
// ============================================================================

#[test]
fn deep_transpile_global_deduplication() {
    // Duplicate globals should be deduplicated
    let c_code = r#"
        int shared = 0;

        int get_shared() {
            return shared;
        }

        int main() {
            shared = 100;
            return get_shared();
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Global dedup should work: {:?}", result.err());
}

// ============================================================================
// ARRAY INIT PATHS IN GLOBAL SCOPE
// ============================================================================

#[test]
fn deep_transpile_global_array_with_initializer() {
    let c_code = r#"
        int data[3] = {1, 2, 3};

        int main() {
            return data[0];
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Initialized global array should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_global_array_zero_init() {
    let c_code = r#"
        int zeros[10] = {0};

        int main() {
            return zeros[0];
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Zero-init global array should transpile: {:?}", result.err());
}

// ============================================================================
// TRANSPILED FILE AND PROJECT CONTEXT
// ============================================================================

#[test]
fn deep_transpile_file_with_context() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "module.c",
        r#"
        int helper(int x) {
            return x * 2;
        }

        int main() {
            return helper(5);
        }
        "#,
    );
    let context = ProjectContext::default();
    let result = decy_core::transpile_file(&file, &context);
    assert!(result.is_ok(), "transpile_file should succeed: {:?}", result.err());
    let transpiled = result.unwrap();
    assert!(!transpiled.rust_code.is_empty(), "Should produce Rust code");
    assert_eq!(transpiled.source_path, file, "Should preserve source path");
}

#[test]
fn deep_transpile_file_nonexistent() {
    let context = ProjectContext::default();
    let result = decy_core::transpile_file(
        std::path::Path::new("/tmp/nonexistent_decy_test_file.c"),
        &context,
    );
    assert!(result.is_err(), "Nonexistent file should error");
}

// ============================================================================
// DEPENDENCY GRAPH
// ============================================================================

#[test]
fn deep_dependency_graph_single_file() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(&temp, "single.c", "int main() { return 0; }");

    let mut graph = DependencyGraph::new();
    graph.add_file(&file);
    let order = graph.topological_sort();
    assert!(order.is_ok(), "Single file should sort: {:?}", order.err());
    let order = order.unwrap();
    assert_eq!(order.len(), 1, "Should have one file");
}

#[test]
fn deep_dependency_graph_multiple_files() {
    let temp = TempDir::new().unwrap();
    let f1 = create_temp_c_file(&temp, "a.c", "int a() { return 1; }");
    let f2 = create_temp_c_file(&temp, "b.c", "int b() { return 2; }");
    let f3 = create_temp_c_file(&temp, "main.c", "int main() { return 0; }");

    let mut graph = DependencyGraph::new();
    graph.add_file(&f1);
    graph.add_file(&f2);
    graph.add_file(&f3);
    let order = graph.topological_sort();
    assert!(order.is_ok(), "Multiple files should sort: {:?}", order.err());
    assert_eq!(order.unwrap().len(), 3, "Should have three files");
}

// ============================================================================
// TRANSPILATION CACHE
// ============================================================================

#[test]
fn deep_transpilation_cache_basic() {
    let cache = TranspilationCache::new();
    let stats = cache.statistics();
    assert_eq!(stats.total_files, 0, "New cache should be empty");
    assert_eq!(stats.hits, 0, "New cache should have no hits");
    assert_eq!(stats.misses, 0, "New cache should have no misses");
}

#[test]
fn deep_transpilation_cache_insert_and_get() {
    let temp = TempDir::new().unwrap();
    let path = create_temp_c_file(&temp, "cached.c", "int main() { return 0; }");

    let mut cache = TranspilationCache::new();
    let transpiled = TranspiledFile {
        source_path: path.clone(),
        rust_code: "fn main() {}".to_string(),
        dependencies: vec![],
        functions_exported: vec!["main".to_string()],
        ffi_declarations: String::new(),
    };

    cache.insert(&path, &transpiled);
    let cached = cache.get(&path);
    assert!(cached.is_some(), "Should find cached entry");
}

// ============================================================================
// COMPLEX PIPELINE: STRUCT + ENUM + GLOBAL + FUNCTIONS
// ============================================================================

#[test]
fn deep_transpile_comprehensive_c_program() {
    let c_code = r#"
        struct Config {
            int width;
            int height;
            float scale;
        };

        enum Mode { NORMAL = 0, DEBUG = 1 };

        int default_width = 800;
        int default_height = 600;

        int area(int w, int h) {
            return w * h;
        }

        int main() {
            struct Config cfg;
            cfg.width = default_width;
            cfg.height = default_height;
            cfg.scale = 1.0;
            int a = area(cfg.width, cfg.height);
            return a;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Comprehensive C program should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("Config"), "Should contain Config struct");
    assert!(rust.contains("fn area"), "Should contain area function");
    assert!(rust.contains("fn main"), "Should contain main");
    assert!(
        rust.contains("ERRNO") || rust.contains("static mut"),
        "Should contain errno or static mut globals"
    );
}

#[test]
fn deep_transpile_string_literal() {
    let c_code = r#"
        int main() {
            char* msg = "Hello World";
            return 0;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "String literal should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_signed_char_global() {
    let c_code = r#"
        signed char offset;

        int main() {
            offset = -1;
            return offset;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Signed char global should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_do_while_loop() {
    let c_code = r#"
        int main() {
            int x = 10;
            do {
                x = x - 1;
            } while (x > 0);
            return x;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Do-while loop should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_logical_operators() {
    let c_code = r#"
        int check(int a, int b) {
            if (a > 0 && b > 0) {
                return 1;
            }
            if (a < 0 || b < 0) {
                return -1;
            }
            return 0;
        }

        int main() {
            return check(1, 2);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Logical operators should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_compound_assignment() {
    let c_code = r#"
        int main() {
            int x = 10;
            x += 5;
            x -= 3;
            x *= 2;
            x /= 4;
            return x;
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Compound assignment should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_nested_if() {
    let c_code = r#"
        int classify(int x) {
            if (x > 0) {
                if (x > 100) {
                    return 2;
                }
                return 1;
            }
            return 0;
        }

        int main() {
            return classify(50);
        }
    "#;
    let result = transpile(c_code);
    assert!(result.is_ok(), "Nested if should transpile: {:?}", result.err());
}

// ============================================================================
// EDGE CASES: ERRNO AND STATIC GENERATION
// ============================================================================

#[test]
fn deep_transpile_errno_generation() {
    // Every transpilation should generate the ERRNO global
    let c_code = "int main() { return 0; }";
    let result = transpile(c_code).unwrap();
    assert!(result.contains("ERRNO"), "Should generate ERRNO static variable");
}

// ============================================================================
// CACHE PERSISTENCE
// ============================================================================

#[test]
fn deep_transpilation_cache_save_load() {
    let temp = TempDir::new().unwrap();
    let cache_dir = temp.path().join("cache");
    std::fs::create_dir_all(&cache_dir).unwrap();

    // Create and save cache
    {
        let c_file = create_temp_c_file(&temp, "persist.c", "int main() { return 0; }");
        let mut cache = TranspilationCache::new();
        let transpiled = TranspiledFile {
            source_path: c_file.clone(),
            rust_code: "fn test() {}".to_string(),
            dependencies: vec![],
            functions_exported: vec!["test".to_string()],
            ffi_declarations: String::new(),
        };
        cache.insert(&c_file, &transpiled);
        // Save is no-op for in-memory cache, but exercise the code path
        let _ = cache.save();
    }

    // Load cache
    let loaded = TranspilationCache::load(&cache_dir);
    assert!(loaded.is_ok(), "Cache load should succeed: {:?}", loaded.err());
}

// ============================================================================
// process_ast_to_rust: UNINITIALIZED GLOBALS (default value generation)
// ============================================================================

#[test]
fn deep_transpile_uninitialized_int_global() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "uninit_int.c",
        r#"
        int counter;
        int main() {
            counter = 42;
            return counter;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Uninitialized int global should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("counter"), "Should contain global counter");
}

#[test]
fn deep_transpile_uninitialized_float_global() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "uninit_float.c",
        r#"
        float ratio;
        double precise;
        int main() {
            ratio = 0.5;
            precise = 3.14;
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Uninitialized float globals should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("ratio"), "Should contain ratio global");
    assert!(rust.contains("precise"), "Should contain precise global");
}

#[test]
fn deep_transpile_uninitialized_char_global() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "uninit_char.c",
        r#"
        char flag;
        int main() {
            flag = 'Y';
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Uninitialized char global should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_uninitialized_pointer_global() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "uninit_ptr.c",
        r#"
        int *buffer;
        int main() {
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Uninitialized pointer global should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_uninitialized_array_global() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "uninit_arr.c",
        r#"
        int arr[10];
        char buf[256];
        float floats[5];
        int main() {
            arr[0] = 1;
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Uninitialized array globals should transpile: {:?}", result.err());
}

// ============================================================================
// process_ast_to_rust: ENUM VARIANTS WITHOUT EXPLICIT VALUES
// ============================================================================

#[test]
fn deep_transpile_enum_implicit_values() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "enum_implicit.c",
        r#"
        enum Status {
            OK,
            ERROR,
            PENDING
        };

        int main() {
            return OK;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Enum with implicit values should transpile: {:?}", result.err());
}

#[test]
fn deep_transpile_enum_mixed_values() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "enum_mixed.c",
        r#"
        enum Priority {
            LOW,
            MEDIUM = 5,
            HIGH,
            CRITICAL = 100
        };

        int main() {
            return LOW;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Enum with mixed values should transpile: {:?}", result.err());
}

// ============================================================================
// process_ast_to_rust: FUNCTION DECLARATION + DEFINITION DEDUPLICATION
// ============================================================================

#[test]
fn deep_transpile_function_decl_then_def() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "decl_def.c",
        r#"
        int add(int a, int b);

        int main() {
            return add(1, 2);
        }

        int add(int a, int b) {
            return a + b;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Function decl then def should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("fn add"), "Should contain add function");
}

// ============================================================================
// process_ast_to_rust: MULTIPLE CONSTRUCTS COMBINED
// ============================================================================

#[test]
fn deep_transpile_many_globals_many_types() {
    let temp = TempDir::new().unwrap();
    let file = create_temp_c_file(
        &temp,
        "many_globals.c",
        r#"
        int g_int;
        float g_float;
        double g_double;
        char g_char;
        unsigned int g_uint = 0;

        struct Config {
            int width;
            int height;
        };

        enum Mode { FAST, SLOW, AUTO };

        typedef unsigned int uint32;

        int main() {
            g_int = 42;
            return 0;
        }
        "#,
    );
    let result = transpile_from_file_path(&file);
    assert!(result.is_ok(), "Many globals with many types should transpile: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("g_int"), "Should contain g_int");
    assert!(rust.contains("Config"), "Should contain Config struct");
}

// ============================================================================
// Global array variable defaults (lines 1200-1217 in lib.rs)
// ============================================================================

#[test]
fn deep_transpile_with_includes_global_char_array() {
    let c_code = r#"
        char g_buffer[256];

        int main() {
            g_buffer[0] = 'A';
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global char array: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("g_buffer"), "Should contain g_buffer, Got: {}", rust);
}

#[test]
fn deep_transpile_with_includes_global_int_array() {
    let c_code = r#"
        int g_scores[10];

        int main() {
            g_scores[0] = 100;
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global int array: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("g_scores"), "Should contain g_scores, Got: {}", rust);
}

#[test]
fn deep_transpile_with_includes_global_float_array() {
    let c_code = r#"
        float g_weights[5];
        double g_values[3];

        int main() {
            g_weights[0] = 1.0f;
            g_values[0] = 2.0;
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global float/double arrays: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("g_weights"), "Should contain g_weights, Got: {}", rust);
    assert!(rust.contains("g_values"), "Should contain g_values, Got: {}", rust);
}

#[test]
fn deep_transpile_with_includes_global_pointer_array() {
    let c_code = r#"
        int *g_ptrs[4];
        unsigned int g_flags[8];

        int main() {
            g_flags[0] = 1;
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global pointer/uint arrays: {:?}", result.err());
    let rust = result.unwrap();
    assert!(
        rust.contains("g_ptrs") || rust.contains("g_flags"),
        "Should contain globals, Got: {}",
        rust
    );
}

#[test]
fn deep_transpile_with_includes_global_struct_array() {
    let c_code = r#"
        struct Point {
            int x;
            int y;
        };

        struct Point g_points[3];

        int main() {
            g_points[0].x = 1;
            g_points[0].y = 2;
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Global struct array: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("g_points"), "Should contain g_points, Got: {}", rust);
}

// ============================================================================
// Extern variable filter (line 953) and struct dedup (line 1063)
// ============================================================================

#[test]
fn deep_transpile_with_includes_extern_global() {
    let c_code = r#"
        extern int max_value;
        int local_value = 5;

        int main() {
            return local_value;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Extern global: {:?}", result.err());
    let rust = result.unwrap();
    assert!(rust.contains("local_value"), "Should contain local_value, Got: {}", rust);
}

#[test]
fn deep_transpile_with_includes_duplicate_struct() {
    let c_code = r#"
        struct Config {
            int value;
        };

        struct Config g_cfg;

        struct Config create_config() {
            struct Config c;
            c.value = 0;
            return c;
        }

        int main() {
            struct Config c = create_config();
            return c.value;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Duplicate struct usage: {:?}", result.err());
}

// ============================================================================
// const_struct_literal closure paths (lines 1109, 1119, 1121, 1133)
// ============================================================================

#[test]
fn deep_transpile_with_includes_struct_with_unsigned_field() {
    let c_code = r#"
        struct Counter {
            unsigned int count;
            signed char flags;
            int data[4];
        };

        struct Counter g_counter;

        int main() {
            g_counter.count = 0;
            return 0;
        }
    "#;
    let result = transpile_with_includes(c_code, None);
    assert!(result.is_ok(), "Struct with unsigned/signed fields: {:?}", result.err());
}