wasm-rquickjs-cli 0.4.0

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
pub mod js_subtest_parser;
pub mod test_server;

use crate::common::WasmSource::Precompiled;
use anyhow::anyhow;
use camino::{Utf8Path, Utf8PathBuf};
use camino_tempfile::{NamedUtf8TempFile, Utf8TempDir};
use futures::FutureExt;
use heck::ToSnakeCase;
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::io::Write;
use std::process::Command;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
use tokio::time::timeout;
use wac_graph::types::{Package, SubtypeChecker};
use wac_graph::{CompositionGraph, EncodeOptions, PackageId, PlugError};
use wasm_rquickjs::{
    EmbeddingMode, GenerationTarget, JsModuleSpec, generate_wrapper_crate_with_target,
};
use wasmtime::component::{
    Component, Func, Instance, Linker, ResourceAny, ResourceTable, ResourceType, Val,
};
use wasmtime::{Engine, Store, StoreContextMut, UpdateDeadline};
use wasmtime_wasi::cli::OutputFile;
use wasmtime_wasi::p2::bindings;
use wasmtime_wasi::{DirPerms, FilePerms, WasiCtx, WasiCtxView, WasiView};
use wasmtime_wasi_http::WasiHttpCtx;
use wasmtime_wasi_http::p2::{WasiHttpCtxView, WasiHttpView, default_hooks};

/// Default timeout for node_compat tests (in seconds).
pub const DEFAULT_NODE_COMPAT_TEST_TIMEOUT_SECS: u64 = 120;

/// In-memory buffer holding host-side tracing output so it can be attached to test failure
/// messages. On CI only the failure message itself is visible (in the ctrf report and the
/// GitHub annotations); anything the test runner captures — including output written via
/// `with_test_writer` — never appears in the logs. So the tracing output must travel inside
/// the error itself, like the guest stdout/stderr already does.
///
/// The buffer is shared by all tests in the process and capped, keeping the most recent output.
static HOST_TRACE: Mutex<Vec<u8>> = Mutex::new(Vec::new());
const HOST_TRACE_CAP: usize = 256 * 1024;

#[derive(Clone, Copy)]
struct HostTraceWriter;

impl std::io::Write for HostTraceWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut trace = HOST_TRACE.lock().unwrap();
        trace.extend_from_slice(buf);
        let len = trace.len();
        if len > HOST_TRACE_CAP {
            trace.drain(..len - HOST_TRACE_CAP);
        }
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl tracing_subscriber::fmt::MakeWriter<'_> for HostTraceWriter {
    type Writer = HostTraceWriter;

    fn make_writer(&self) -> Self::Writer {
        *self
    }
}

/// Returns the host-side tracing output captured so far (see [`init_tracing`]).
pub fn host_trace() -> String {
    String::from_utf8_lossy(&HOST_TRACE.lock().unwrap()).into_owned()
}

/// Installs a global tracing subscriber (once per process) so host-side `tracing` diagnostics
/// are visible in test output. Most importantly, `wasmtime-wasi-http` flattens the underlying
/// hyper error of a failed outgoing request into `ErrorCode::HttpProtocolError` and only reports
/// the real error via `tracing::warn!` — without a subscriber that information is lost, which
/// makes intermittent CI-only fetch failures undiagnosable.
///
/// The output is collected into [`HOST_TRACE`] (not the test runner's capture buffer) so that
/// failing tests can attach it to their error message, which is the only output channel visible
/// in the CI failure reports.
///
/// The filter can be overridden with `RUST_LOG`; by default only `wasmtime-wasi-http` warnings
/// are shown to keep output noise low.
pub fn init_tracing() {
    use std::sync::Once;
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        let filter = tracing_subscriber::EnvFilter::try_from_default_env()
            .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("wasmtime_wasi_http=warn"));
        let _ = tracing_subscriber::fmt()
            .with_env_filter(filter)
            .with_writer(HostTraceWriter)
            .with_ansi(false)
            .try_init();
    });
}

/// Strip JSONC comments (// and /* */) while respecting string literals.
pub fn strip_jsonc_comments(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let chars: Vec<char> = input.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        if chars[i] == '"' {
            result.push(chars[i]);
            i += 1;
            while i < len && chars[i] != '"' {
                if chars[i] == '\\' && i + 1 < len {
                    result.push(chars[i]);
                    result.push(chars[i + 1]);
                    i += 2;
                } else {
                    result.push(chars[i]);
                    i += 1;
                }
            }
            if i < len {
                result.push(chars[i]);
                i += 1;
            }
        } else if chars[i] == '/' && i + 1 < len && chars[i + 1] == '/' {
            i += 2;
            while i < len && chars[i] != '\n' {
                i += 1;
            }
        } else if chars[i] == '/' && i + 1 < len && chars[i + 1] == '*' {
            i += 2;
            while i + 1 < len && !(chars[i] == '*' && chars[i + 1] == '/') {
                i += 1;
            }
            if i + 1 < len {
                i += 2;
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }

    result
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum NodeCompatCategory {
    /// The test exercises supported public API and should pass. Failures count against primary compatibility.
    Runnable,
    /// The tested public API is not implemented yet, but is in scope for this runtime.
    KnownGap,
    /// The test requires capabilities that WASI Preview 2 cannot provide.
    WasmImpossible,
    /// The test depends on V8-specific behavior that QuickJS cannot reasonably mirror.
    EngineDifference,
    /// The test checks Node.js internal implementation details rather than public API.
    NodeInternals,
    /// The test has not been triaged yet and should not affect compatibility percentages.
    Unevaluated,
}

impl NodeCompatCategory {
    pub fn from_config_value(value: &str) -> anyhow::Result<Self> {
        match value {
            "runnable" | "expected-pass" => Ok(Self::Runnable),
            "gap" | "known-gap" | "not-implemented" => Ok(Self::KnownGap),
            "wasi-impossible" | "wasm-impossible" | "impossible" | "unsupported-by-wasi" => {
                Ok(Self::WasmImpossible)
            }
            "engine-difference" | "quickjs-difference" | "v8-specific" => {
                Ok(Self::EngineDifference)
            }
            "node-internals" | "internals" | "implementation-detail" => Ok(Self::NodeInternals),
            "unevaluated" | "untriaged" => Ok(Self::Unevaluated),
            other => anyhow::bail!("unknown node_compat category '{other}'"),
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Runnable => "runnable",
            Self::KnownGap => "known gap",
            Self::WasmImpossible => "WASI-impossible",
            Self::EngineDifference => "engine difference",
            Self::NodeInternals => "Node.js internals",
            Self::Unevaluated => "unevaluated",
        }
    }

    pub fn should_ignore_in_runner(self) -> bool {
        !matches!(self, Self::Runnable)
    }

    pub fn is_primary_surface(self) -> bool {
        matches!(self, Self::Runnable | Self::KnownGap)
    }
}

#[derive(Debug, Clone)]
pub struct NodeCompatSubtestEntry {
    pub name: String,
    pub index: usize,
    pub category: NodeCompatCategory,
    pub reason: Option<String>,
    pub flaky: bool,
}

#[derive(Debug, Clone)]
pub struct NodeCompatTestEntry {
    pub path: String,
    pub category: NodeCompatCategory,
    pub reason: Option<String>,
    pub split: bool,
    pub timeout_secs: u64,
    pub flaky: bool,
    pub subtests: Vec<NodeCompatSubtestEntry>,
}

/// Extract the numeric index from a subtest name like "block_00_foo" or "test_03_bar".
/// Panics if the name doesn't match the expected format (config is authoritative).
pub fn extract_node_compat_subtest_index(name: &str) -> usize {
    let after_prefix = if let Some(rest) = name.strip_prefix("block_") {
        rest
    } else if let Some(rest) = name.strip_prefix("test_") {
        rest
    } else {
        panic!("Subtest name '{name}' must start with 'block_' or 'test_'");
    };
    let digits: String = after_prefix
        .chars()
        .take_while(|c| c.is_ascii_digit())
        .collect();
    digits
        .parse()
        .unwrap_or_else(|_| panic!("Subtest name '{name}' has no valid numeric index after prefix"))
}

fn is_unevaluated_node_compat_reason(reason: &str) -> bool {
    let r = reason.trim();
    r == "newly discovered, not yet evaluated" || r.starts_with("inherited: newly discovered")
}

fn node_compat_category_from_entry(
    path: &str,
    entry: &serde_json::Value,
    inherited: Option<NodeCompatCategory>,
) -> anyhow::Result<NodeCompatCategory> {
    if let Some(category) = entry.get("category").and_then(|v| v.as_str()) {
        return NodeCompatCategory::from_config_value(category);
    }

    if entry
        .get("impossible")
        .and_then(|v| v.as_bool())
        .unwrap_or(false)
    {
        return Ok(NodeCompatCategory::WasmImpossible);
    }

    if entry.get("skip").and_then(|v| v.as_bool()).unwrap_or(false) {
        let reason = entry.get("reason").and_then(|v| v.as_str()).unwrap_or("");
        return Ok(if is_unevaluated_node_compat_reason(reason) {
            NodeCompatCategory::Unevaluated
        } else if uses_node_internals(path) {
            NodeCompatCategory::NodeInternals
        } else {
            NodeCompatCategory::KnownGap
        });
    }

    if let Some(category) = inherited
        && category.should_ignore_in_runner()
    {
        return Ok(category);
    }

    if uses_node_internals(path) {
        Ok(NodeCompatCategory::NodeInternals)
    } else {
        Ok(NodeCompatCategory::Runnable)
    }
}

pub fn load_node_compat_config(path: &str) -> anyhow::Result<Vec<NodeCompatTestEntry>> {
    let content = fs::read_to_string(path)?;
    let json_str = strip_jsonc_comments(&content);
    let value: serde_json::Value = serde_json::from_str(&json_str)?;

    let tests_obj = value
        .get("tests")
        .and_then(|v| v.as_object())
        .ok_or_else(|| anyhow::anyhow!("config.jsonc missing 'tests' object"))?;

    let mut tests = Vec::new();
    for (path, opts) in tests_obj {
        let category = node_compat_category_from_entry(path, opts, None)?;
        let reason = opts
            .get("reason")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let split = opts.get("split").and_then(|v| v.as_bool()).unwrap_or(false);
        let timeout_secs = opts
            .get("timeout")
            .and_then(|v| v.as_u64())
            .unwrap_or(DEFAULT_NODE_COMPAT_TEST_TIMEOUT_SECS);
        let flaky = opts.get("flaky").and_then(|v| v.as_bool()).unwrap_or(false);

        let mut subtests = Vec::new();
        if let Some(subtests_obj) = opts.get("subtests").and_then(|v| v.as_object()) {
            for (subtest_name, subtest_opts) in subtests_obj {
                let sub_category =
                    node_compat_category_from_entry(path, subtest_opts, Some(category))?;
                let sub_reason = subtest_opts
                    .get("reason")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .or_else(|| reason.clone());
                let sub_flaky = subtest_opts
                    .get("flaky")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(flaky);
                let index = extract_node_compat_subtest_index(subtest_name);
                subtests.push(NodeCompatSubtestEntry {
                    name: subtest_name.clone(),
                    index,
                    category: sub_category,
                    reason: sub_reason,
                    flaky: sub_flaky,
                });
            }
        }

        tests.push(NodeCompatTestEntry {
            path: path.clone(),
            category,
            reason,
            split,
            timeout_secs,
            flaky,
            subtests,
        });
    }

    Ok(tests)
}

/// Recursively copy a directory and all its contents to a destination.
pub fn copy_dir_recursive(src: &std::path::Path, dst: &std::path::Path) -> anyhow::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if src_path.is_dir() {
            copy_dir_recursive(&src_path, &dst_path)?;
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

/// Copy a vendored Node.js test file and common shims into a temp directory.
///
/// Sets up the directory layout expected by the node-compat-runner:
/// - `/home/node/test/<suite>/<test_file>` — the test itself
/// - `/home/node/test/common/` — common shims
/// - `/tmp/` — for tmpdir shim
/// - `/home/node/test/fixtures/` — fixture data files (recursively copied)
pub fn setup_node_compat_test_files(temp: &Utf8Path, test_rel_path: &str) -> anyhow::Result<()> {
    // Parse the suite name from the relative path (e.g., "parallel/test-foo.js" → "parallel")
    let suite = test_rel_path.split('/').next().unwrap_or("parallel");

    // Create directory structure: /home/node/test/<suite>/ and /home/node/test/common/
    // The /home/node prefix ensures import.meta.url matches patterns like /.*\/test\//.
    let test_root = temp.join("home").join("node").join("test");
    let suite_dir = test_root.join(suite);
    let common_dir = test_root.join("common");
    fs::create_dir_all(&suite_dir)?;
    fs::create_dir_all(&common_dir)?;

    // Copy the test file
    let test_filename = test_rel_path.rsplit('/').next().unwrap_or(test_rel_path);
    let src_test = format!("tests/node_compat/suite/{test_rel_path}");
    let dst_test = suite_dir.join(test_filename);
    fs::copy(&src_test, &dst_test)?;

    // Copy the common shim
    let src_shim = "tests/node_compat/common-shim/index.js";
    let dst_shim = common_dir.join("index.js");
    fs::copy(src_shim, &dst_shim)?;

    // Copy the common ESM shim if it exists
    let src_shim_mjs = "tests/node_compat/common-shim/index.mjs";
    if std::path::Path::new(src_shim_mjs).exists() {
        fs::copy(src_shim_mjs, common_dir.join("index.mjs"))?;
    }

    // Copy all additional common shims from common-shim directory
    let shim_dir = std::path::Path::new("tests/node_compat/common-shim");
    if shim_dir.exists() {
        for entry in fs::read_dir(shim_dir)? {
            let entry = entry?;
            let file_name = entry.file_name();
            let file_name_str = file_name.to_string_lossy();
            // Skip index.js and index.mjs (already copied above)
            if file_name_str == "index.js" || file_name_str == "index.mjs" {
                continue;
            }
            if entry.file_type()?.is_file() {
                fs::copy(entry.path(), common_dir.join(file_name_str.as_ref()))?;
            }
        }
    }

    // Create /tmp directory for tmpdir shim
    let tmp_dir = temp.join("tmp");
    fs::create_dir_all(&tmp_dir)?;

    // Copy fixture data files for tests that use require('../common/fixtures')
    let fixtures_dst = test_root.join("fixtures");

    // First copy vendored suite fixtures
    let vendored_fixtures_src = std::path::Path::new("tests/node_compat/suite/fixtures");
    if vendored_fixtures_src.exists() {
        copy_dir_recursive(vendored_fixtures_src, fixtures_dst.as_std_path())?;
    }

    // Then overlay with our custom fixtures (take priority over vendored ones)
    let fixtures_src = std::path::Path::new("tests/node_compat/fixtures");
    if fixtures_src.exists() {
        copy_dir_recursive(fixtures_src, fixtures_dst.as_std_path())?;
    }

    Ok(())
}

pub fn collect_example_paths(dirs: &[&str]) -> anyhow::Result<Vec<Utf8PathBuf>> {
    let mut result = Vec::new();
    for dir in dirs {
        let paths = fs::read_dir(dir)?;
        for example_path in paths {
            let example_path = example_path?;
            let metadata = example_path.metadata()?;
            if metadata.is_dir() {
                let path = Utf8PathBuf::from_path_buf(example_path.path())
                    .map_err(|_| anyhow!("Non UTF-8 example path"))?;
                result.push(path);
            }
        }
    }
    Ok(result)
}

/// The WASI generation target a runtime/node_compat test is exercised against.
///
/// Selected once per process via the `WASM_RQUICKJS_TEST_TARGET` environment variable
/// (`p2` — the default — or `p3`). Preview 2 reproduces the historical behavior; Preview 3
/// generates async component exports and runs them on a Component Model async host.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TestTarget {
    P2,
    P3,
}

impl TestTarget {
    /// Suffix appended to generated crate / shared-target directories so that P2 and P3 builds of
    /// the same example never share an output tree (the P3 generator writes a different Cargo.toml
    /// and skeleton set).
    pub fn dir_suffix(self) -> &'static str {
        match self {
            TestTarget::P2 => "",
            TestTarget::P3 => "-p3",
        }
    }

    pub fn generation_target(self) -> GenerationTarget {
        match self {
            TestTarget::P2 => GenerationTarget::WasiP2,
            TestTarget::P3 => GenerationTarget::WasiP3,
        }
    }
}

/// Reads the active test target once from `WASM_RQUICKJS_TEST_TARGET` (default: `p2`).
pub fn test_target() -> TestTarget {
    static TARGET: OnceLock<TestTarget> = OnceLock::new();
    *TARGET.get_or_init(
        || match std::env::var("WASM_RQUICKJS_TEST_TARGET").ok().as_deref() {
            Some("p3") | Some("P3") => TestTarget::P3,
            Some("p2") | Some("P2") | None => TestTarget::P2,
            Some(other) => {
                panic!("Unknown WASM_RQUICKJS_TEST_TARGET '{other}'; expected 'p2' or 'p3'")
            }
        },
    )
}

/// Copies a WIT directory to `dst`, turning every synchronous freestanding exported function into
/// an `async func`.
///
/// The Preview 3 generation path rejects *synchronous freestanding exports* — both world-level
/// `export …: func(…)` and plain `name: func(…)` declarations inside an exported `interface`. A
/// synchronous *resource instance method* additionally traps at runtime if its JS implementation
/// returns a Promise. Because the JS in these examples freely uses `async` methods, the rewrite
/// async-ifies every `name: func(` declaration — freestanding functions and resource instance
/// methods alike (see [`rewrite_wit_source_exports_async`]). Resource `constructor`s and `static
/// func`s are left synchronous: WIT has no async spelling for them, and their JS returns values
/// directly.
///
/// Only the package's own `.wit` files (those directly in `src_wit_dir`) are rewritten; the
/// `deps/` subtree is copied verbatim. Dependency interfaces are *imported* (e.g. `wasi:random`),
/// and their function signatures must keep matching the host imports, so they must never be
/// async-ified. Examples that *export* an interface defined in a dependency package may therefore
/// still fail to build or run under the P3 lane; every test is run in P3 mode on CI so such gaps
/// surface directly.
pub fn rewrite_wit_exports_async(
    src_wit_dir: &Utf8Path,
    dst_wit_dir: &Utf8Path,
) -> anyhow::Result<()> {
    if dst_wit_dir.exists() {
        fs::remove_dir_all(dst_wit_dir)?;
    }
    fs::create_dir_all(dst_wit_dir)?;

    for entry in fs::read_dir(src_wit_dir)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let src_path =
            Utf8PathBuf::from_path_buf(entry.path()).map_err(|_| anyhow!("Non UTF-8 WIT path"))?;
        let file_name = src_path
            .file_name()
            .ok_or_else(|| anyhow!("WIT entry without file name"))?;
        let dst_path = dst_wit_dir.join(file_name);

        if file_type.is_dir() {
            // A `deps/` subtree holds *imported* interfaces. Imports satisfied by the *host*
            // (`wasi:*`, `golem:*` packages — the host registers synchronous implementations)
            // must keep their sync signatures, so those files are copied verbatim. Imports
            // satisfied by *another example component* via composition (`plug_into`) must be
            // rewritten to async, because the providing component is itself built in P3 mode
            // with its exports rewritten to `async func` — otherwise the plug's async exports
            // would not type-match the socket's sync imports.
            copy_deps_rewriting_non_host_packages(src_path.as_std_path(), dst_path.as_std_path())?;
        } else if src_path.extension() == Some("wit") {
            let rewritten = rewrite_wit_source_exports_async(&fs::read_to_string(&src_path)?);
            fs::write(&dst_path, rewritten)?;
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

/// Recursively copies a `deps/` subtree, rewriting `: func(` to `: async func(` in every WIT
/// file whose package is *not* host-provided. Host-provided packages (`wasi:*`, `golem:*`)
/// are copied verbatim because the test host registers synchronous implementations for them;
/// everything else (e.g. `quickjs:*` interfaces exported by sibling example components) is
/// rewritten so composed components type-match. See [`rewrite_wit_exports_async`].
fn copy_deps_rewriting_non_host_packages(
    src: &std::path::Path,
    dst: &std::path::Path,
) -> anyhow::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if src_path.is_dir() {
            copy_deps_rewriting_non_host_packages(&src_path, &dst_path)?;
        } else if src_path.extension().and_then(|e| e.to_str()) == Some("wit") {
            let source = fs::read_to_string(&src_path)?;
            let is_host_package = source.lines().any(|line| {
                let line = line.trim_start();
                line.starts_with("package wasi:") || line.starts_with("package golem:")
            });
            if is_host_package {
                fs::write(&dst_path, source)?;
            } else {
                fs::write(&dst_path, rewrite_wit_source_exports_async(&source))?;
            }
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

/// Line-oriented rewrite backing [`rewrite_wit_exports_async`]. Kept separate so it is trivially
/// unit-testable and free of any filesystem access.
///
/// Every line declaring a function type as `name: func(` is turned into `name: async func(`. This
/// covers world-level `export foo: func(…)`, freestanding `foo: func(…)` inside an exported
/// interface, and resource *instance* methods `bar: func(…)`. It deliberately does **not** match
/// `constructor(…)` (no `: func(`) or `baz: static func(…)` (spelled `: static func(`, not
/// `: func(`): WIT has no async spelling for those, and their JS returns values directly.
fn rewrite_wit_source_exports_async(source: &str) -> String {
    source
        .lines()
        .map(|line| {
            if line.contains(": func(") && !line.contains(": async func(") {
                line.replacen(": func(", ": async func(", 1)
            } else {
                line.to_string()
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
        + if source.ends_with('\n') { "\n" } else { "" }
}

#[derive(Copy, Clone)]
pub enum FeatureCombination {
    None,
    Lite,
    Normal,
    Full,
    FullNoLogging,
    Golem,
    FullWithGolem,
    FullNoLoggingWithGolem,
}

impl FeatureCombination {
    pub fn all() -> Vec<FeatureCombination> {
        vec![Self::Lite, Self::Normal, Self::Full, Self::FullWithGolem]
    }

    pub fn label(&self) -> &str {
        match self {
            Self::None => "none",
            Self::Lite => "lite",
            Self::Normal => "normal",
            Self::Full => "full",
            Self::FullNoLogging => "full-no-logging",
            Self::Golem => "golem",
            Self::FullWithGolem => "full-golem",
            Self::FullNoLoggingWithGolem => "full-no-logging-golem",
        }
    }

    pub fn cargo_args(&self) -> Vec<&'static str> {
        match self {
            // The skeleton now requires exactly one WASI target feature (`p2` or `p3`), so the
            // minimal Preview 2 build must still enable `p2` even with no other features.
            FeatureCombination::None => vec!["--no-default-features", "--features", "p2"],
            FeatureCombination::Lite => {
                vec!["--no-default-features", "--features", "lite"]
            }
            FeatureCombination::Normal => vec![],
            FeatureCombination::Full => {
                vec!["--no-default-features", "--features", "full"]
            }
            FeatureCombination::FullNoLogging => {
                vec!["--no-default-features", "--features", "full-no-logging"]
            }
            FeatureCombination::Golem => vec!["--features", "golem"],
            FeatureCombination::FullWithGolem => {
                vec!["--no-default-features", "--features", "full,golem"]
            }
            FeatureCombination::FullNoLoggingWithGolem => {
                vec![
                    "--no-default-features",
                    "--features",
                    "full-no-logging,golem",
                ]
            }
        }
    }

    /// Cargo `--features` args for a given [`TestTarget`].
    ///
    /// For Preview 2 this is the historical [`cargo_args`](Self::cargo_args). For Preview 3 each
    /// combination enables exactly the same capabilities as its Preview 2 counterpart: the P3
    /// tiers (`normal-p3`, `full-p3`, `full-no-logging-p3`) mirror the P2 tiers, and `golem` /
    /// `websocket` / `logging` are target-agnostic. The only difference is `fetch`/`node-http`,
    /// which are the Preview 2 HTTP implementations — the `p3` path ships its own `wasi:http@0.3`
    /// based fetch and node:http unconditionally, so `None`/`Lite` collapse onto bare `p3`. The
    /// features are always spelled out explicitly so the P3 build never silently falls back to
    /// the P2 default feature set.
    pub fn cargo_args_for_target(&self, target: TestTarget) -> Vec<&'static str> {
        match target {
            TestTarget::P2 => self.cargo_args(),
            TestTarget::P3 => {
                let features = match self {
                    FeatureCombination::None | FeatureCombination::Lite => "p3",
                    FeatureCombination::Normal => "normal-p3",
                    FeatureCombination::Full => "full-p3",
                    FeatureCombination::FullNoLogging => "full-no-logging-p3",
                    FeatureCombination::Golem => "normal-p3,golem",
                    FeatureCombination::FullWithGolem => "full-p3,golem",
                    FeatureCombination::FullNoLoggingWithGolem => "full-no-logging-p3,golem",
                };
                vec!["--no-default-features", "--features", features]
            }
        }
    }
}

pub struct PreparedComponent {
    engine: Engine,
    linker: Linker<Host>,
    component: Component,
}

impl PreparedComponent {
    pub fn new(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        init_tracing();
        match test_target() {
            TestTarget::P2 => Self::new_p2(wasm_path),
            TestTarget::P3 => Self::new_p3(wasm_path),
        }
    }

    /// Preview 3 host: a Component Model async engine, a P2+P3 WASI/HTTP linker, and the same
    /// component. Works on both stock wasmtime and the Golem fork — see [`p3_engine`].
    fn new_p3(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        let engine = p3_engine()?;
        let linker = p3_linker(&engine)?;
        let component = Component::from_file(&engine, wasm_path)?;
        Ok(Self {
            engine,
            linker,
            component,
        })
    }

    fn new_p2(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        let mut config = wasmtime::Config::default();
        config.wasm_component_model(true);
        config.epoch_interruption(true);
        config.async_stack_size(32 * 1024 * 1024); // 32MB async stack (must be >= max_wasm_stack)
        config.max_wasm_stack(16 * 1024 * 1024); // 16MB WASM stack (default is 512KB, QuickJS in WASM needs more for deep recursion)
        let engine = Engine::new(&config)?;

        // Start a background thread that increments the epoch every 10ms,
        // enabling epoch-based interruption to enforce timeouts on spinning WASM.
        let epoch_engine = engine.clone();
        std::thread::spawn(move || {
            loop {
                std::thread::sleep(std::time::Duration::from_millis(10));
                epoch_engine.increment_epoch();
            }
        });
        let mut linker: Linker<Host> = Linker::new(&engine);

        wasmtime_wasi::p2::add_to_linker_with_options_async(
            &mut linker,
            &bindings::LinkOptions::default(),
        )?;
        wasmtime_wasi_http::p2::add_only_http_to_linker_async(&mut linker)?;

        // Mock wasi:logging/logging (required by the full feature)
        add_wasi_logging_mock(&mut linker)?;

        // Mock golem:websocket/client@1.5.0 (required when websocket module is included)
        add_websocket_client_mock(&mut linker, TestTarget::P2)?;

        let component = Component::from_file(&engine, wasm_path)?;

        Ok(Self {
            engine,
            linker,
            component,
        })
    }
}

/// Mock logging level for wasi:logging/logging
#[derive(Debug, Clone, wasmtime::component::ComponentType, wasmtime::component::Lift)]
#[component(enum)]
#[repr(u8)]
#[allow(dead_code)]
pub enum LogLevel {
    #[component(name = "trace")]
    Trace,
    #[component(name = "debug")]
    Debug,
    #[component(name = "info")]
    Info,
    #[component(name = "warn")]
    Warn,
    #[component(name = "error")]
    Error,
    #[component(name = "critical")]
    Critical,
}

/// Mock attribute-value variant for golem:api/context
#[derive(wasmtime::component::ComponentType, wasmtime::component::Lift)]
#[component(variant)]
pub enum AttributeValue {
    #[component(name = "string")]
    String(String),
}

/// Mock span for golem:api/context testing
pub struct GolemSpan {
    pub name: String,
    pub attributes: Vec<(String, String)>,
    pub finished: bool,
}

/// A PreparedComponent that includes a mock golem:api/context host implementation.
pub struct GolemPreparedComponent {
    engine: Engine,
    linker: Linker<Host>,
    component: Component,
    pub spans: Arc<Mutex<Vec<GolemSpan>>>,
}

impl GolemPreparedComponent {
    pub fn new(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        init_tracing();
        match test_target() {
            TestTarget::P2 => Self::new_p2(wasm_path),
            TestTarget::P3 => Self::new_p3(wasm_path),
        }
    }

    /// Preview 3 host: the P2+P3 WASI/HTTP surface plus the `wasi:logging` and `golem:websocket`
    /// mocks (see [`p3_linker`]) and the same `golem:api/context` span-recording mock as the
    /// Preview 2 host, so Golem-flavored feature combinations behave identically on both targets.
    /// Works on both stock wasmtime and the Golem fork.
    fn new_p3(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        let engine = p3_engine()?;
        let mut linker = p3_linker(&engine)?;
        let spans = add_golem_context_mock(&mut linker)?;
        let component = Component::from_file(&engine, wasm_path)?;
        Ok(Self {
            engine,
            linker,
            component,
            spans,
        })
    }

    fn new_p2(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        let mut config = wasmtime::Config::default();
        config.wasm_component_model(true);
        config.epoch_interruption(true);
        config.async_stack_size(32 * 1024 * 1024);
        config.max_wasm_stack(16 * 1024 * 1024);
        let engine = Engine::new(&config)?;

        // Start a background thread that increments the epoch every 10ms,
        // enabling epoch-based interruption to enforce timeouts on spinning WASM.
        let epoch_engine = engine.clone();
        std::thread::spawn(move || {
            loop {
                std::thread::sleep(std::time::Duration::from_millis(10));
                epoch_engine.increment_epoch();
            }
        });
        let mut linker: Linker<Host> = Linker::new(&engine);

        wasmtime_wasi::p2::add_to_linker_with_options_async(
            &mut linker,
            &bindings::LinkOptions::default(),
        )?;
        wasmtime_wasi_http::p2::add_only_http_to_linker_async(&mut linker)?;

        // Mock wasi:logging/logging (required by the golem feature)
        add_wasi_logging_mock(&mut linker)?;

        // Mock golem:websocket/client@1.5.0 (required when websocket module is included)
        add_websocket_client_mock(&mut linker, TestTarget::P2)?;

        // Mock golem:api/context@1.5.0
        let spans = add_golem_context_mock(&mut linker)?;

        let component = Component::from_file(&engine, wasm_path)?;

        Ok(Self {
            engine,
            linker,
            component,
            spans,
        })
    }
}

#[allow(dead_code)]
pub struct TestInstance {
    engine: Engine,
    linker: Linker<Host>,
    component: Component,
    store: Store<Host>,
    instance: Instance,
    stdout_file: NamedUtf8TempFile,
    stderr_file: NamedUtf8TempFile,
    temp_dir: Utf8TempDir,
}

impl TestInstance {
    pub async fn new(wasm_path: &Utf8Path) -> anyhow::Result<Self> {
        let prepared = PreparedComponent::new(wasm_path)?;
        Self::from_prepared(&prepared).await
    }

    pub async fn from_prepared(prepared: &PreparedComponent) -> anyhow::Result<Self> {
        Self::from_parts(&prepared.engine, &prepared.linker, &prepared.component).await
    }

    pub async fn from_golem_prepared(prepared: &GolemPreparedComponent) -> anyhow::Result<Self> {
        Self::from_parts(&prepared.engine, &prepared.linker, &prepared.component).await
    }

    async fn from_parts(
        engine: &Engine,
        linker: &Linker<Host>,
        component: &Component,
    ) -> anyhow::Result<Self> {
        let stdout_file = NamedUtf8TempFile::new()?;
        let stderr_file = NamedUtf8TempFile::new()?;

        let temp_dir = Utf8TempDir::new()?;
        fs::write(temp_dir.path().join("input.txt"), "test file contents")?;
        fs::create_dir(temp_dir.path().join("test"))?;

        let mut ctx_builder = WasiCtx::builder();
        ctx_builder
            .stdout(OutputFile::new(stdout_file.reopen()?))
            .stderr(OutputFile::new(stderr_file.reopen()?))
            .arg("first-arg")
            .arg("second-arg")
            .env("TEST_KEY", "TEST_VALUE")
            .env("TEST_KEY_2", "TEST_VALUE_2")
            .preopened_dir(&temp_dir, "/", DirPerms::all(), FilePerms::all())?
            .inherit_network()
            .allow_ip_name_lookup(true);
        #[cfg(feature = "use-golem-wasmtime")]
        let (ctx, io_ctx) = ctx_builder.build();
        #[cfg(not(feature = "use-golem-wasmtime"))]
        let ctx = ctx_builder.build();
        let http_ctx = WasiHttpCtx::new();
        let host = Host {
            table: Arc::new(Mutex::new(ResourceTable::new())),
            wasi: Arc::new(Mutex::new(ctx)),
            wasi_http: Arc::new(Mutex::new(http_ctx)),
            started_at: Instant::now(),
            timeout: Duration::from_secs(120),
            log_messages: Arc::new(Mutex::new(Vec::new())),
            #[cfg(feature = "use-golem-wasmtime")]
            io_ctx: Arc::new(Mutex::new(io_ctx)),
        };

        let mut store = Store::new(engine, host);
        store.set_epoch_deadline(0);
        store.epoch_deadline_callback(|cx| {
            let data = cx.data();
            if data.started_at.elapsed() >= data.timeout {
                Ok(UpdateDeadline::Interrupt)
            } else {
                Ok(UpdateDeadline::YieldCustom(
                    1,
                    tokio::task::yield_now().boxed(),
                ))
            }
        });

        let instance = linker.instantiate_async(&mut store, component).await?;

        Ok(Self {
            engine: engine.clone(),
            linker: linker.clone(),
            component: component.clone(),
            store,
            instance,
            stdout_file,
            stderr_file,
            temp_dir,
        })
    }

    pub async fn invoke_and_capture_output(
        &mut self,
        interface_name: Option<&str>,
        function_name: &str,
        args: &[Val],
    ) -> (anyhow::Result<Option<Val>>, String) {
        let (results, stdout, _stderr) = self
            .invoke_and_capture_output_with_stderr(interface_name, function_name, args)
            .await;
        (results, stdout)
    }

    pub async fn invoke(
        &mut self,
        interface_name: Option<&str>,
        function_name: &str,
        args: &[Val],
    ) -> anyhow::Result<Option<Val>> {
        self.invoke_and_capture_output_inner(interface_name, function_name, args)
            .await
            .map(|results| results.first().cloned())
    }

    pub async fn invoke_and_capture_output_with_stderr(
        &mut self,
        interface_name: Option<&str>,
        function_name: &str,
        args: &[Val],
    ) -> (anyhow::Result<Option<Val>>, String, String) {
        let results = self
            .invoke_and_capture_output_inner(interface_name, function_name, args)
            .await;

        let stdout = fs::read_to_string(&self.stdout_file).expect("failed to read stdout");
        let stderr = fs::read_to_string(&self.stderr_file).expect("failed to read stderr");

        if results.is_err() {
            for line in stdout.lines() {
                println!("[stdout] {line}");
            }
        }

        for line in stderr.lines() {
            println!("[stderr] {line}");
        }

        // Attach the captured guest output and the host-side tracing output to the error
        // itself so they show up in the test failure report (the `println!`s above are
        // captured by the test runner and are not part of the reported failure message
        // on CI).
        let results = results.map_err(|err| {
            let host_trace = host_trace();
            err.context(format!(
                "guest stdout:\n{stdout}\nguest stderr:\n{stderr}\nhost trace:\n{host_trace}"
            ))
        });

        (
            results.map(|results| results.first().cloned()),
            stdout,
            stderr,
        )
    }

    pub fn set_epoch_deadline(&mut self, timeout_secs: u64) {
        self.store.data_mut().timeout = Duration::from_secs(timeout_secs);
        self.store.data_mut().started_at = Instant::now();
    }

    pub fn temp_dir_path(&self) -> &Utf8Path {
        self.temp_dir.path()
    }

    pub fn read_stdout(&self) -> anyhow::Result<String> {
        Ok(fs::read_to_string(&self.stdout_file)?)
    }

    pub fn read_stderr(&self) -> anyhow::Result<String> {
        Ok(fs::read_to_string(&self.stderr_file)?)
    }

    pub fn read_log_messages(&self) -> Vec<(LogLevel, String, String)> {
        self.store.data().log_messages.lock().unwrap().clone()
    }

    async fn invoke_and_capture_output_inner(
        &mut self,
        interface_name: Option<&str>,
        function_name: &str,
        args: &[Val],
    ) -> anyhow::Result<Vec<Val>> {
        let func = match interface_name {
            Some(interface_name) => {
                let (_, exported_instance_id) = self
                    .instance
                    .get_export(&mut self.store, None, interface_name)
                    .ok_or_else(|| anyhow!("Interface {interface_name} not found"))?;
                let (_, func_id) = self
                    .instance
                    .get_export(&mut self.store, Some(&exported_instance_id), function_name)
                    .ok_or_else(|| {
                        anyhow!("Function {function_name} not found in interface {interface_name}")
                    })?;
                self.instance
                    .get_func(&mut self.store, func_id)
                    .ok_or_else(|| anyhow!("Function {function_name} not found"))?
            }
            None => self
                .instance
                .get_func(&mut self.store, function_name)
                .ok_or_else(|| anyhow!("Function {function_name} not found"))?,
        };

        match timeout(Duration::from_secs(300), self.perform_invoke(func, args)).await {
            Ok(result) => result,
            Err(_) => Err(anyhow!("Function {function_name} timed out")),
        }
    }

    async fn perform_invoke(&mut self, func: Func, args: &[Val]) -> anyhow::Result<Vec<Val>> {
        let mut results = (0..func.ty(&self.store).results().len())
            .map(|_| Val::Bool(false))
            .collect::<Vec<_>>();
        func.call_async(&mut self.store, args, &mut results).await?;
        Ok(results)
    }

    pub async fn drop_resource(&mut self, resource: ResourceAny) -> anyhow::Result<()> {
        resource.resource_drop_async(&mut self.store).await?;
        Ok(())
    }
}

/// Maximum attempts for guest invocations that fail with the intermittent
/// wasmtime-wasi-http p3 scheduling race (see [`is_p3_http_flake`]).
const P3_HTTP_FLAKE_MAX_ATTEMPTS: u32 = 3;

/// wasmtime-wasi-http 46's p3 outgoing request path has a scheduling-sensitive
/// race in its manual connection-driving loop: under load, hyper intermittently
/// fails with `IncompleteMessage` or `Canceled(UnexpectedMessage)`, both of
/// which are flattened into `ErrorCode::HttpProtocolError` before reaching the
/// guest. The p3 rework in wasmtime 47 replaces this code, but until that is
/// released (and supported by the Golem fork) we retry invocations that fail
/// with this signature. Each retry uses a fresh `TestInstance`.
fn is_p3_http_flake(err: &anyhow::Error) -> bool {
    test_target() == TestTarget::P3 && format!("{err:#}").contains("ErrorCode::HttpProtocolError")
}

pub async fn invoke_and_capture_output(
    wasm_path: &Utf8Path,
    interface_name: Option<&str>,
    function_name: &str,
    args: &[Val],
) -> (anyhow::Result<Option<Val>>, String) {
    let (results, stdout, _stderr) =
        invoke_and_capture_output_with_stderr(wasm_path, interface_name, function_name, args).await;
    (results, stdout)
}

pub async fn invoke_and_capture_output_with_stderr(
    wasm_path: &Utf8Path,
    interface_name: Option<&str>,
    function_name: &str,
    args: &[Val],
) -> (anyhow::Result<Option<Val>>, String, String) {
    let mut last = None;
    for attempt in 1..=P3_HTTP_FLAKE_MAX_ATTEMPTS {
        let result = match TestInstance::new(wasm_path).await {
            Ok(mut test_instance) => {
                test_instance
                    .invoke_and_capture_output_with_stderr(interface_name, function_name, args)
                    .await
            }
            Err(e) => (Err(e), String::new(), String::new()),
        };
        match &result.0 {
            Err(e) if attempt < P3_HTTP_FLAKE_MAX_ATTEMPTS && is_p3_http_flake(e) => {
                eprintln!(
                    "Invocation of {function_name} failed with the intermittent p3 \
                     HttpProtocolError (attempt {attempt}/{P3_HTTP_FLAKE_MAX_ATTEMPTS}), retrying"
                );
                last = Some(result);
            }
            _ => return result,
        }
    }
    last.expect("at least one invocation attempt must have run")
}

enum WasmSource {
    Precompiled(Utf8PathBuf),
    OwnedTemporary(NamedUtf8TempFile),
}

pub struct CompiledTest {
    wasm: WasmSource,
}

impl CompiledTest {
    pub async fn new(path: &Utf8Path, use_shared_target: bool) -> anyhow::Result<CompiledTest> {
        Self::new_with_features(path, use_shared_target, FeatureCombination::Normal).await
    }

    pub async fn new_unoptimized_with_features(
        path: &Utf8Path,
        use_shared_target: bool,
        feature_combination: FeatureCombination,
    ) -> anyhow::Result<CompiledTest> {
        Self::compile_with_features(path, use_shared_target, feature_combination).await
    }

    pub async fn new_with_features(
        path: &Utf8Path,
        use_shared_target: bool,
        feature_combination: FeatureCombination,
    ) -> anyhow::Result<CompiledTest> {
        let compiled =
            Self::compile_with_features(path, use_shared_target, feature_combination).await?;
        compiled.optimize().await
    }

    async fn compile_with_features(
        path: &Utf8Path,
        use_shared_target: bool,
        feature_combination: FeatureCombination,
    ) -> anyhow::Result<CompiledTest> {
        let target = test_target();
        let name = path.file_name().unwrap();
        // P2 and P3 builds of the same example never share an output tree.
        let feature_label = format!("{}{}", feature_combination.label(), target.dir_suffix());
        let wrapper_crate_root = Utf8Path::new("tmp").join(name).join(&feature_label);

        // shared_target is relative to wrapper_crate_root.
        // this is a _different_ shared target than the one used in the compilation tests to make
        // sure different feature combinations do not interfere with these tests. P3 uses its own
        // shared target so P2 and P3 artifacts never collide.
        let shared_target_name = format!("rt-target{}", target.dir_suffix());
        let shared_target = Utf8Path::new("..").join("..").join(&shared_target_name);

        // The Preview 3 generation path rejects synchronous freestanding exports, so for P3 we
        // rewrite the example's WIT so its world-level exported functions become `async func`
        // before generation. The rewritten WIT lives inside the wrapper crate dir so it never
        // touches the committed example sources.
        let wit_dir = match target {
            TestTarget::P2 => path.join("wit"),
            TestTarget::P3 => {
                let rewritten = wrapper_crate_root.join("wit-async");
                rewrite_wit_exports_async(&path.join("wit"), &rewritten)?;
                rewritten
            }
        };

        println!(
            "Generating wrapper create for example '{name}' ({:?}) to {wrapper_crate_root}",
            target
        );
        generate_wrapper_crate_with_target(
            &wit_dir,
            &[JsModuleSpec {
                name: name.to_string(),
                mode: EmbeddingMode::EmbedFile(path.join("src").join(format!("{name}.js"))),
            }],
            &wrapper_crate_root,
            None,
            target.generation_target(),
        )?;

        println!("Compiling wrapper crate in {wrapper_crate_root}");
        let mut command = Command::new("cargo");
        command.arg("build").arg("--target").arg("wasm32-wasip2");
        if use_shared_target {
            command.arg("--target-dir");
            command.arg(shared_target);
        }
        command
            .args(feature_combination.cargo_args_for_target(target))
            .current_dir(&wrapper_crate_root)
            .status()
            .and_then(|status| {
                if status.success() {
                    Ok(status)
                } else {
                    Err(std::io::Error::other(format!(
                        "cargo build failed for {wrapper_crate_root}"
                    )))
                }
            })?;

        let compiled = if use_shared_target {
            CompiledTest {
                wasm: Precompiled(
                    Utf8Path::new("tmp")
                        .join(&shared_target_name)
                        .join("wasm32-wasip2")
                        .join("debug")
                        .join(format!("{}.wasm", name.to_snake_case())),
                ),
            }
        } else {
            CompiledTest {
                wasm: Precompiled(
                    wrapper_crate_root
                        .join("target")
                        .join("wasm32-wasip2")
                        .join("debug")
                        .join(format!("{}.wasm", name.to_snake_case())),
                ),
            }
        };

        Ok(compiled)
    }

    /// Run Wizer pre-initialization on the compiled component.
    /// Returns a new `CompiledTest` pointing to the optimized wasm file.
    pub async fn optimize(&self) -> anyhow::Result<CompiledTest> {
        let input = self.wasm_path();
        let optimized = input.with_extension("optimized.wasm");
        println!("Optimizing component {input} -> {optimized}");
        wasm_rquickjs::optimize_component(input, &optimized, "wizer-initialize").await?;
        Ok(CompiledTest {
            wasm: Precompiled(optimized),
        })
    }

    pub fn wasm_path(&self) -> &Utf8Path {
        match &self.wasm {
            WasmSource::Precompiled(path) => path,
            WasmSource::OwnedTemporary(temp_file) => temp_file.path(),
        }
    }
}

/// Opt `CompiledTest` into test-r's `Cloneable` sharing strategy so that
/// worker subprocesses can share the parent's compilation result instead
/// of forcing the suite into single-threaded mode under output capturing.
///
/// The wire format is just the **absolute** wasm path. The parent compiles
/// the wrapper crate once (via the existing `CompiledTest::new*` ctors) into
/// a stable on-disk location under `tmp/<example>/<features>/...` (or the
/// shared `tmp/rt-target/...` tree when `use_shared_target = true`) — these
/// paths outlive both the dep value and the suite. Each worker simply receives
/// the path and reconstructs a `Precompiled(...)` `CompiledTest` that points
/// at the same on-disk artifact.
///
/// `OwnedTemporary` is only ever produced by `plug_into`, which is called
/// inside test bodies (never inside a `#[test_dep]` ctor). Shipping an
/// `OwnedTemporary` over wire would silently delete the temp file as soon as
/// the parent dropped the value after `to_wire`, leaving workers reading a
/// dangling path. We refuse loudly instead.
impl test_r::core::CloneableDep for CompiledTest {
    fn to_wire(&self) -> Vec<u8> {
        match &self.wasm {
            Precompiled(path) => {
                let abs = path.canonicalize_utf8().unwrap_or_else(|e| {
                    panic!(
                        "CompiledTest path '{path}' must exist before \
                         being shipped via Cloneable scope: {e}"
                    )
                });
                abs.as_str().as_bytes().to_vec()
            }
            WasmSource::OwnedTemporary(_) => panic!(
                "OwnedTemporary CompiledTest cannot be shared via Cloneable \
                 scope; plug_into() output must stay inside a single test body"
            ),
        }
    }

    fn from_wire(bytes: &[u8]) -> Self {
        let path_str = std::str::from_utf8(bytes)
            .expect("Cloneable CompiledTest wire bytes must be valid UTF-8 path");
        let path = Utf8PathBuf::from(path_str);
        assert!(
            path.exists(),
            "Cloneable CompiledTest received path that does not exist: {path}. \
             The parent must keep the compiled wasm artifact alive for the suite duration."
        );
        CompiledTest {
            wasm: Precompiled(path),
        }
    }
}

impl CompiledTest {
    pub fn plug_into(&self, other: &CompiledTest) -> anyhow::Result<CompiledTest> {
        let mut graph = CompositionGraph::new();
        let socket_package =
            Package::from_file("socket", None, other.wasm_path(), graph.types_mut())?;
        let socket_id = graph.register_package(socket_package)?;

        let plug_package = Package::from_file("plug", None, self.wasm_path(), graph.types_mut())?;
        let plug_id = graph.register_package(plug_package)?;

        plug(
            &mut graph,
            vec![(self.wasm_path().to_string(), plug_id)],
            socket_id,
        )?;

        let bytes = graph.encode(EncodeOptions::default())?;
        let mut wasm_path = NamedUtf8TempFile::new()?;
        wasm_path.write_all(bytes.as_slice())?;
        wasm_path.flush()?;
        Ok(CompiledTest {
            wasm: WasmSource::OwnedTemporary(wasm_path),
        })
    }
}

#[derive(Clone)]
pub struct Host {
    pub table: Arc<Mutex<ResourceTable>>,
    pub wasi: Arc<Mutex<WasiCtx>>,
    pub wasi_http: Arc<Mutex<WasiHttpCtx>>,
    pub started_at: Instant,
    pub timeout: Duration,
    pub log_messages: Arc<Mutex<Vec<(LogLevel, String, String)>>>,
    #[cfg(feature = "use-golem-wasmtime")]
    pub io_ctx: Arc<Mutex<wasmtime_wasi::IoCtx>>,
}

impl WasiView for Host {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView {
            ctx: Arc::get_mut(&mut self.wasi)
                .expect("WasiCtx is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("WasiCtx mutex must never fail"),
            table: Arc::get_mut(&mut self.table)
                .expect("ResourceTable is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("ResourceTable mutex must never fail"),
            #[cfg(feature = "use-golem-wasmtime")]
            io_ctx: Arc::get_mut(&mut self.io_ctx)
                .expect("IoCtx is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("IoCtx mutex must never fail"),
        }
    }
}

impl WasiHttpView for Host {
    fn http(&mut self) -> WasiHttpCtxView<'_> {
        WasiHttpCtxView {
            ctx: Arc::get_mut(&mut self.wasi_http)
                .expect("WasiHttpCtx is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("WasiHttpCtx mutex must never fail"),
            table: Arc::get_mut(&mut self.table)
                .expect("ResourceTable is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("ResourceTable mutex must never fail"),
            hooks: default_hooks(),
        }
    }
}

// ---------------------------------------------------------------------------------------------
// WASI Preview 3 (Component Model async) host support.
//
// Works on both stock wasmtime and the Golem wasmtime fork (`use-golem-wasmtime`): the fork
// supports Preview 3 just like upstream, and the only fork-specific difference (the extra `IoCtx`
// returned by `WasiCtxBuilder::build()` / required by `WasiCtxView`) is handled on the shared
// `Host` type above.
// ---------------------------------------------------------------------------------------------

/// Preview 3 engine: same stack/epoch configuration as the P2 host, plus Component Model async
/// support so that async-lifted exports can be driven by the concurrent executor that
/// `Func::call_async` uses internally.
fn p3_engine() -> anyhow::Result<Engine> {
    let mut config = wasmtime::Config::default();
    config.wasm_component_model(true);
    config.wasm_component_model_async(true);
    config.epoch_interruption(true);
    config.async_stack_size(32 * 1024 * 1024);
    config.max_wasm_stack(16 * 1024 * 1024);
    let engine = Engine::new(&config)?;

    let epoch_engine = engine.clone();
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(std::time::Duration::from_millis(10));
            epoch_engine.increment_epoch();
        }
    });
    Ok(engine)
}

/// Preview 3 linker: the P2 WASI surface (P3 components still import residual `wasi:io`/0.2 std
/// interfaces), the P3 WASI surface, and the P3 async HTTP surface used by `fetch`. Also mocks
/// `wasi:logging/logging` and `golem:websocket/client@1.5.0` so P3 builds with the (target-
/// agnostic) `logging` / `websocket` features can instantiate; the definitions are ignored by
/// components that don't import them.
fn p3_linker(engine: &Engine) -> anyhow::Result<Linker<Host>> {
    let mut linker: Linker<Host> = Linker::new(engine);
    wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
    wasmtime_wasi::p3::add_to_linker(&mut linker)?;
    wasmtime_wasi_http::p3::add_to_linker(&mut linker)?;
    add_wasi_logging_mock(&mut linker)?;
    add_websocket_client_mock(&mut linker, TestTarget::P3)?;
    Ok(linker)
}

/// Mock `wasi:logging/logging`: records every `log` call in the Host's `log_messages` list.
fn add_wasi_logging_mock(linker: &mut Linker<Host>) -> anyhow::Result<()> {
    let mut logging = linker.instance("wasi:logging/logging")?;
    logging.func_wrap(
        "log",
        |mut ctx: StoreContextMut<'_, Host>,
         (level, context, message): (LogLevel, String, String)|
         -> Result<(), wasmtime::Error> {
            ctx.data_mut()
                .log_messages
                .lock()
                .unwrap()
                .push((level, context, message));
            Ok(())
        },
    )?;
    Ok(())
}

/// Mock `golem:api/context@1.5.0`: implements `start-span`, `span.set-attribute`, and
/// `span.finish`, recording every span in the returned shared list so tests can assert on the
/// emitted tracing spans.
fn add_golem_context_mock(linker: &mut Linker<Host>) -> anyhow::Result<Arc<Mutex<Vec<GolemSpan>>>> {
    let spans: Arc<Mutex<Vec<GolemSpan>>> = Arc::new(Mutex::new(Vec::new()));
    let spans_clone = spans.clone();

    let mut golem_ctx = linker.instance("golem:api/context@1.5.0")?;

    // Register the span resource type
    let span_resource_type = ResourceType::host::<GolemSpan>();
    golem_ctx.resource("span", span_resource_type, {
        let spans = spans_clone.clone();
        move |mut ctx: StoreContextMut<'_, Host>, rep: u32| {
            // Destructor: mark span as finished if not already
            let table = ctx.data_mut().table.lock().unwrap();
            // Resource already dropped by wasmtime
            let _ = (spans.as_ref(), rep, table);
            Ok(())
        }
    })?;

    // start-span: func(name: string) -> span
    golem_ctx.func_wrap("start-span", {
        let spans = spans_clone.clone();
        move |mut ctx: StoreContextMut<'_, Host>,
              (name,): (String,)|
              -> Result<(wasmtime::component::Resource<GolemSpan>,), wasmtime::Error> {
            let span = GolemSpan {
                name,
                attributes: Vec::new(),
                finished: false,
            };
            let mut table = ctx.data_mut().table.lock().unwrap();
            let resource = table.push(span)?;
            spans.lock().unwrap().push(GolemSpan {
                name: String::new(), // placeholder, real data is in table
                attributes: Vec::new(),
                finished: false,
            });
            Ok((resource,))
        }
    })?;

    // [method]span.set-attribute: func(name: string, value: attribute-value)
    // attribute-value is a variant with one case: string(string)
    golem_ctx.func_wrap("[method]span.set-attribute", {
        let spans = spans_clone.clone();
        move |mut ctx: StoreContextMut<'_, Host>,
              (span_res, attr_name, attr_value): (
            wasmtime::component::Resource<GolemSpan>,
            String,
            AttributeValue,
        )|
              -> Result<(), wasmtime::Error> {
            let value_str = match &attr_value {
                AttributeValue::String(s) => s.clone(),
            };
            let mut table = ctx.data_mut().table.lock().unwrap();
            if let Ok(span) = table.get_mut(&span_res) {
                span.attributes.push((attr_name.clone(), value_str.clone()));
            }
            // Also record in the shared spans list
            let mut shared = spans.lock().unwrap();
            if let Some(last) = shared.last_mut() {
                last.attributes.push((attr_name, value_str));
            }
            Ok(())
        }
    })?;

    // [method]span.finish: func()
    golem_ctx.func_wrap("[method]span.finish", {
        let spans = spans_clone.clone();
        move |mut ctx: StoreContextMut<'_, Host>,
              (span_res,): (wasmtime::component::Resource<GolemSpan>,)|
              -> Result<(), wasmtime::Error> {
            let mut table = ctx.data_mut().table.lock().unwrap();
            if let Ok(span) = table.get_mut(&span_res) {
                span.finished = true;
                // Copy final state to shared spans
                let name = span.name.clone();
                let attributes = span.attributes.clone();
                let mut shared = spans.lock().unwrap();
                if let Some(last) = shared.last_mut() {
                    last.name = name;
                    last.finished = true;
                    last.attributes = attributes;
                }
            }
            Ok(())
        }
    })?;

    Ok(spans)
}

/// Mock `golem:websocket/client@1.5.0`: registers the `websocket-connection` resource and stubs
/// every method to fail. This satisfies the host import required when the `websocket` module is
/// compiled in; tests only exercise the JS-side API surface (globals, brand checks), not live
/// connections.
fn add_websocket_client_mock(linker: &mut Linker<Host>, target: TestTarget) -> anyhow::Result<()> {
    struct WsConn;
    let mut ws = linker.instance("golem:websocket/client@1.5.0")?;
    ws.resource("websocket-connection", ResourceType::host::<WsConn>(), {
        move |_ctx: StoreContextMut<'_, Host>, _rep: u32| Ok(())
    })?;

    ws.func_new(
        "[static]websocket-connection.connect",
        |_store, _ty, _params, _results| {
            Err(wasmtime::Error::msg(
                "WebSocket connect not available in tests",
            ))
        },
    )?;

    ws.func_new(
        "[method]websocket-connection.send",
        |_store, _ty, _params, _results| {
            Err(wasmtime::Error::msg(
                "WebSocket send not available in tests",
            ))
        },
    )?;

    match target {
        TestTarget::P2 => {
            ws.func_new(
                "[method]websocket-connection.receive",
                |_store, _ty, _params, _results| {
                    Err(wasmtime::Error::msg(
                        "WebSocket receive not available in tests",
                    ))
                },
            )?;
            ws.func_new(
                "[method]websocket-connection.receive-with-timeout",
                |_store, _ty, _params, _results| {
                    Err(wasmtime::Error::msg(
                        "WebSocket receive-with-timeout not available in tests",
                    ))
                },
            )?;
        }
        TestTarget::P3 => {
            ws.func_new_concurrent(
                "[method]websocket-connection.receive",
                |_accessor, _ty, _params, _results| {
                    Box::pin(async {
                        Err(wasmtime::Error::msg(
                            "WebSocket receive not available in tests",
                        ))
                    })
                },
            )?;
            ws.func_new_concurrent(
                "[method]websocket-connection.receive-with-timeout",
                |_accessor, _ty, _params, _results| {
                    Box::pin(async {
                        Err(wasmtime::Error::msg(
                            "WebSocket receive-with-timeout not available in tests",
                        ))
                    })
                },
            )?;
        }
    }

    ws.func_new(
        "[method]websocket-connection.close",
        |_store, _ty, _params, _results| {
            Err(wasmtime::Error::msg(
                "WebSocket close not available in tests",
            ))
        },
    )?;

    ws.func_new(
        "[method]websocket-connection.subscribe",
        |_store, _ty, _params, _results| {
            Err(wasmtime::Error::msg(
                "WebSocket subscribe not available in tests",
            ))
        },
    )?;

    Ok(())
}

impl wasmtime_wasi_http::p3::WasiHttpView for Host {
    fn http(&mut self) -> wasmtime_wasi_http::p3::WasiHttpCtxView<'_> {
        wasmtime_wasi_http::p3::WasiHttpCtxView {
            hooks: wasmtime_wasi_http::p3::default_hooks(),
            table: Arc::get_mut(&mut self.table)
                .expect("ResourceTable is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("ResourceTable mutex must never fail"),
            ctx: Arc::get_mut(&mut self.wasi_http)
                .expect("WasiHttpCtx is shared and cannot be borrowed mutably")
                .get_mut()
                .expect("WasiHttpCtx mutex must never fail"),
        }
    }
}

// Based on https://github.com/bytecodealliance/wac/blob/release-0.6.0/crates/wac-graph/src/plug.rs#L23
// but instead of returning NoPlugError, it logs skipped instantiations
fn plug(
    graph: &mut CompositionGraph,
    plugs: Vec<(String, PackageId)>,
    socket: PackageId,
) -> Result<(), PlugError> {
    let socket_instantiation = graph.instantiate(socket);

    let mut requested_plugs = BTreeSet::<String>::new();
    let mut plug_exports_to_plug = BTreeMap::<String, String>::new();

    for (plug_name, plug) in plugs {
        requested_plugs.insert(plug_name.clone());

        let mut plug_exports = Vec::new();
        let mut cache = Default::default();
        let mut checker = SubtypeChecker::new(&mut cache);
        for (name, plug_ty) in &graph.types()[graph[plug].ty()].exports {
            if let Some(socket_ty) = graph.types()[graph[socket].ty()].imports.get(name)
                && checker
                    .is_subtype(*plug_ty, graph.types(), *socket_ty, graph.types())
                    .is_ok()
            {
                plug_exports.push(name.clone());
            }
        }

        // Instantiate the plug component
        let mut plug_instantiation = None;
        for plug_export_name in plug_exports {
            plug_exports_to_plug.insert(plug_export_name.clone(), plug_name.clone());

            let plug_instantiation =
                *plug_instantiation.get_or_insert_with(|| graph.instantiate(plug));
            let export = graph
                .alias_instance_export(plug_instantiation, &plug_export_name)
                .map_err(|err| PlugError::GraphError { source: err.into() })?;
            graph
                .set_instantiation_argument(socket_instantiation, &plug_export_name, export)
                .map_err(|err| PlugError::GraphError { source: err.into() })?;
        }
    }

    // Export all exports from the socket component.
    for name in graph.types()[graph[socket].ty()]
        .exports
        .keys()
        .cloned()
        .collect::<Vec<_>>()
    {
        let export = graph
            .alias_instance_export(socket_instantiation, &name)
            .map_err(|err| PlugError::GraphError { source: err.into() })?;

        graph
            .export(export, &name)
            .map_err(|err| PlugError::GraphError { source: err.into() })?;
    }

    Ok(())
}

/// Classify a test filename into a module category based on its name prefix.
pub fn classify_test(filename: &str) -> &str {
    // Strip "test-" prefix
    let name = filename
        .strip_prefix("test-")
        .unwrap_or(filename)
        .strip_suffix(".js")
        .unwrap_or(filename);

    if name.starts_with("path") {
        "path"
    } else if name.starts_with("assert") {
        "assert"
    } else if name.starts_with("buffer") {
        "buffer"
    } else if name.starts_with("stream") {
        "stream"
    } else if name.starts_with("string-decoder") || name.starts_with("stringdecoder") {
        "string_decoder"
    } else if name.starts_with("url") {
        "url"
    } else if name.starts_with("util") {
        "util"
    } else if name.starts_with("querystring") {
        "querystring"
    } else if name.starts_with("events") || name.starts_with("event-emitter") {
        "events"
    } else if name.starts_with("fs") || name.starts_with("file") {
        "fs"
    } else if name.starts_with("crypto") {
        "crypto"
    } else if name.starts_with("http") || name.starts_with("http2") || name.starts_with("https") {
        "http"
    } else if name.starts_with("net") {
        "net"
    } else if name.starts_with("dns") {
        "dns"
    } else if name.starts_with("os") {
        "os"
    } else if name.starts_with("process") {
        "process"
    } else if name.starts_with("child-process") || name.starts_with("child_process") {
        "child_process"
    } else if name.starts_with("tls") || name.starts_with("ssl") {
        "tls"
    } else if name.starts_with("zlib") {
        "zlib"
    } else if name.starts_with("console") {
        "console"
    } else if name.starts_with("timers")
        || name.starts_with("settimeout")
        || name.starts_with("setinterval")
        || name.starts_with("setimmediate")
    {
        "timers"
    } else if name.starts_with("worker") || name.starts_with("worker-threads") {
        "worker_threads"
    } else if name.starts_with("cluster") {
        "cluster"
    } else if name.starts_with("readline") {
        "readline"
    } else if name.starts_with("repl") {
        "repl"
    } else if name.starts_with("vm") {
        "vm"
    } else if name.starts_with("dgram") {
        "dgram"
    } else if name.starts_with("tty") {
        "tty"
    } else if name.starts_with("async-hooks")
        || name.starts_with("async-context")
        || name.starts_with("async-local-storage")
    {
        "async_hooks"
    } else if name.starts_with("inspector") || name.starts_with("debugger") {
        "inspector"
    } else if name.starts_with("module")
        || name.starts_with("require")
        || name.starts_with("esm")
        || name.starts_with("cjs")
        || name.starts_with("loaders")
    {
        "module"
    } else if name.starts_with("perf") || name.starts_with("performance") {
        "perf_hooks"
    } else if name.starts_with("diagnostics") {
        "diagnostics_channel"
    } else if name.starts_with("domain") {
        "domain"
    } else if name.starts_with("v8") {
        "v8"
    } else if name.starts_with("trace") {
        "trace_events"
    } else if name.starts_with("runner") || name.starts_with("test-runner") {
        "test_runner"
    } else if name.starts_with("abortcontroller")
        || name.starts_with("abortsignal")
        || name.starts_with("aborted")
    {
        "abort"
    } else if name.starts_with("encoding")
        || name.starts_with("textdecoder")
        || name.starts_with("textencoder")
    {
        "encoding"
    } else if name.starts_with("blob") {
        "blob"
    } else if name.starts_with("fetch")
        || name.starts_with("response")
        || name.starts_with("request")
        || name.starts_with("headers")
    {
        "fetch"
    } else if name.starts_with("readable")
        || name.starts_with("writable")
        || name.starts_with("transform")
        || name.starts_with("duplex")
    {
        "stream"
    } else if name.starts_with("sqlite") {
        "sqlite"
    } else if name.starts_with("whatwg") {
        "whatwg"
    } else if name.starts_with("webcrypto") {
        "webcrypto"
    } else if name.starts_with("permission") {
        "permission"
    } else if name.starts_with("promise") || name.starts_with("promises") {
        "promises"
    } else if name.starts_with("global") {
        "global"
    } else if name.starts_with("compile") {
        "compile"
    } else if name.starts_with("cli") {
        "cli"
    } else if name.starts_with("stdin") || name.starts_with("stdout") || name.starts_with("stdio") {
        "stdio"
    } else if name.starts_with("signal") {
        "signal"
    } else if name.starts_with("errors") || name.starts_with("error") {
        "errors"
    } else if name.starts_with("pipe")
        || name.starts_with("socket")
        || name.starts_with("listen")
        || name.starts_with("tcp")
    {
        "net"
    } else if name.starts_with("webstream") || name.starts_with("webstreams") {
        "webstreams"
    } else if name.starts_with("snapshot") {
        "snapshot"
    } else if name.starts_with("eslint") {
        "eslint"
    } else if name.starts_with("internal") {
        "internal"
    } else if name.starts_with("heap") {
        "heap"
    } else if name.starts_with("node") {
        "node"
    } else if name.starts_with("inspect") {
        "inspector"
    } else if name.starts_with("shadow-realm") {
        "shadow_realm"
    } else if name.starts_with("btoa") || name.starts_with("atob") {
        "encoding"
    } else if name.starts_with("common") {
        "common"
    } else {
        "other"
    }
}

/// Check if a test file relies on Node.js internals (not public API).
///
/// Detects patterns like `// Flags: --expose-internals`, `require('internal/...')`,
/// and `internalBinding(...)` in the test source code.
pub fn uses_node_internals(test_path: &str) -> bool {
    let file_path = format!("tests/node_compat/suite/{test_path}");
    let content = match fs::read_to_string(&file_path) {
        Ok(c) => c,
        Err(_) => return false,
    };
    // Only check the first 50 lines for the Flags comment (it's always near the top)
    let header: String = content.lines().take(50).collect::<Vec<_>>().join("\n");
    if header.contains("--expose-internals") {
        return true;
    }
    // Check the full file for internal requires/bindings
    content.contains("require('internal/")
        || content.contains("require(\"internal/")
        || content.contains("internalBinding(")
}