wasm-rquickjs-cli 0.4.15

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
test_r::enable!();

use crate::common::js_subtest_parser::{
    BlockInfo, SubtestDiscovery, TestInfo, discover_subtests_with_options,
    rewrite_for_block_with_options, rewrite_for_node_test,
};
use crate::common::{
    CompiledTest, GolemPreparedComponent, TestInstance, load_node_compat_config,
    setup_node_compat_test_files,
};
use camino::Utf8Path;
use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::time::Duration;
use test_r::core::{DynamicTestRegistration, FlakinessControl, TestProperties};
use test_r::{test_dep, test_gen};
use tokio::time::timeout;
use wasmtime::component::Val;

#[allow(dead_code)]
#[path = "common/mod.rs"]
mod common;

struct FullPreparedComponent(Arc<GolemPreparedComponent>);

/// Parent compiles the node-compat wrapper crate exactly once and ships
/// the wasm path to every worker via test-r's `Cloneable` scope. The
/// expensive piece (cargo build of the wrapper crate) therefore runs a
/// single time per suite even with `--test-threads N` and capture on.
#[test_dep(tagged_as = "node_compat_full_compiled", scope = Cloneable)]
async fn compiled_node_compat_full() -> CompiledTest {
    let path = Utf8Path::new("examples/runtime/node-compat-runner");
    let feature_combination = if common::truthy_env("WASM_RQUICKJS_TEST_NET_WRITE_PROFILING") {
        common::FeatureCombination::NodeCompatNetWriteProfiling
    } else {
        common::FeatureCombination::FullNoLoggingWithGolemAndTypeScript
    };
    CompiledTest::new_with_features(path, true, feature_combination)
        .await
        .expect("Failed to compile node-compat-runner")
}

/// Each worker materialises its own `FullPreparedComponent` (which owns
/// the per-worker `wasmtime::Engine`, `Linker`, `Component`, and
/// epoch-ticker thread) from the parent-shipped wasm path. This is
/// `PerWorker` rather than `Shared` so it does not re-trigger the
/// single-threaded fallback that `Shared` deps cause under capture.
#[test_dep(scope = PerWorker)]
fn prepare_node_compat_full(
    #[tagged_as("node_compat_full_compiled")] compiled: &CompiledTest,
) -> Arc<FullPreparedComponent> {
    Arc::new(FullPreparedComponent(Arc::new(
        GolemPreparedComponent::new(compiled.wasm_path()).expect("Failed to prepare component"),
    )))
}

#[test_r::test]
async fn runner_waits_for_referenced_callbacks(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);
    let test_root = instance.temp_dir_path().join("home/node/test");
    fs::create_dir_all(test_root.join("parallel"))?;
    common::copy_dir_recursive(
        Utf8Path::new("tests/node_compat/common-shim").as_std_path(),
        test_root.join("common").as_std_path(),
    )?;
    fs::write(
        test_root.join("parallel/runner-delayed-callback.js"),
        "const common = require('../common');\nsetTimeout(common.mustCall(() => {}), 3100);\n",
    )?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/parallel/runner-delayed-callback.js".to_string(),
            )],
        )
        .await;
    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_unrefed_interval_preserves_referenced_server(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);
    let test_root = instance.temp_dir_path().join("home/node/test");
    fs::create_dir_all(test_root.join("parallel"))?;
    common::copy_dir_recursive(
        Utf8Path::new("tests/node_compat/common-shim").as_std_path(),
        test_root.join("common").as_std_path(),
    )?;
    fs::write(
        test_root.join("parallel/runner-unref-server.js"),
        [
            "const common = require('../common');",
            "const assert = require('node:assert');",
            "const server = require('node:net').createServer();",
            "server.on('listening', () => console.log('server listening'));",
            "server.on('close', common.mustCall(() => console.log('server closed')));",
            "server.listen(0);",
            "let count = 0;",
            "const interval = setInterval(() => {",
            "  interval.unref();",
            "  if (++count <= 5) console.log('interval tick', count);",
            "  if (count === 4) server.close();",
            "  if (count === 1000) { clearInterval(interval); assert.fail('unreferenced interval prevented completion'); }",
            "}, 1);",
        ]
        .join("\n"),
    )?;
    let (result, stdout, stderr) = timeout(
        Duration::from_secs(20),
        instance.invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/parallel/runner-unref-server.js".to_string(),
            )],
        ),
    )
    .await
    .map_err(|_| anyhow::anyhow!("timer/server completion exceeded 20 seconds"))?;
    println!("Timer/server lifecycle:\n{stdout}\n{stderr}");
    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_concurrent_idle_waiters_without_timers(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);
    let test_root = instance.temp_dir_path().join("home/node/test");
    fs::create_dir_all(test_root.join("parallel"))?;
    common::copy_dir_recursive(
        Utf8Path::new("tests/node_compat/common-shim").as_std_path(),
        test_root.join("common").as_std_path(),
    )?;
    fs::write(
        test_root.join("parallel/runner-concurrent-idle.mjs"),
        [
            "import common from '../common/index.js';",
            "import { createServer } from 'node:net';",
            "const server = createServer();",
            "server.listen(0, common.mustCall(() => server.close(common.mustCall())));",
            "await Promise.all([process._awaitRuntimeIdle(), process._awaitRuntimeIdle()]).then(common.mustCall());",
        ]
        .join("\n"),
    )?;
    let (result, stdout, stderr) = timeout(
        Duration::from_secs(20),
        instance.invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/parallel/runner-concurrent-idle.mjs".to_string(),
            )],
        ),
    )
    .await
    .map_err(|_| anyhow::anyhow!("concurrent idle completion exceeded 20 seconds"))?;
    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_import_preload_flag(prepared: &Arc<FullPreparedComponent>) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);

    let suite_dir = instance
        .temp_dir_path()
        .join("home")
        .join("node")
        .join("test")
        .join("es-module");
    fs::create_dir_all(&suite_dir)?;
    fs::write(
        suite_dir.join("preload-smoke-preload.mjs"),
        "globalThis.__nodeCompatPreloadValue = 41;\n",
    )?;
    fs::write(
        suite_dir.join("preload-smoke.mjs"),
        [
            "// Flags: --import ./test/es-module/preload-smoke-preload.mjs",
            "if (globalThis.__nodeCompatPreloadValue !== 41) {",
            "  throw new Error('preload did not run before entry');",
            "}",
        ]
        .join("\n"),
    )?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/es-module/preload-smoke.mjs".to_string(),
            )],
        )
        .await;

    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_dynamic_import_cache_survives_removed_file(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);

    let suite_dir = instance
        .temp_dir_path()
        .join("home")
        .join("node")
        .join("test")
        .join("es-module");
    fs::create_dir_all(&suite_dir)?;
    fs::write(
        suite_dir.join("dynamic-import-cache-entry.mjs"),
        [
            "import assert from 'node:assert';",
            "import fs from 'node:fs/promises';",
            "const target = new URL('./dynamic-import-cache-target.mjs', import.meta.url);",
            "await assert.rejects(import(target), { code: 'ERR_MODULE_NOT_FOUND' });",
            "await fs.writeFile(target, 'export default \"actual target\"\\n');",
            "const moduleRecord = await import(target);",
            "await fs.rm(target);",
            "assert.strictEqual(await import(target), moduleRecord);",
        ]
        .join("\n"),
    )?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/es-module/dynamic-import-cache-entry.mjs".to_string(),
            )],
        )
        .await;

    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_static_registered_loader_async_resolve(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);

    let suite_dir = instance
        .temp_dir_path()
        .join("home")
        .join("node")
        .join("test")
        .join("es-module");
    fs::create_dir_all(&suite_dir)?;
    fs::write(
        suite_dir.join("async-static-loader.mjs"),
        [
            "export async function resolve(specifier, context, nextResolve) {",
            "  if (specifier === './dep.mjs') {",
            "    if (context.parentURL !== new URL('./async-static-entry.mjs', import.meta.url).href) throw new Error('wrong logical entry parent');",
            "    return nextResolve('./real.mjs', context);",
            "  }",
            "  if (specifier === './generated.mjs') {",
            "    return { shortCircuit: true, url: 'virtual:generated', format: 'module' };",
            "  }",
            "  if (specifier === 'virtual:child') {",
            "    if (context.parentURL !== 'virtual:generated') throw new Error('wrong logical generated parent');",
            "    return { shortCircuit: true, url: new URL('./child.mjs', import.meta.url).href, format: 'module' };",
            "  }",
            "  return nextResolve(specifier, context);",
            "}",
            "export async function load(url, context, nextLoad) {",
            "  if (url === new URL('./async-static-entry.mjs', import.meta.url).href) {",
            "    return { shortCircuit: true, format: 'module', source: 'import value from \"./dep.mjs\"; import generated from \"./generated.mjs\"; if (value !== 42 || generated !== 7) throw new Error(\"wrong provided entry source\");' };",
            "  }",
            "  if (url === 'virtual:generated') {",
            "    return { shortCircuit: true, format: 'module', source: 'import value from \"virtual:child\"; export default value;' };",
            "  }",
            "  return nextLoad(url, context);",
            "}",
        ]
        .join("\n"),
    )?;
    fs::write(
        suite_dir.join("async-static-entry.mjs"),
        [
            "// Flags: --experimental-loader ./test/es-module/async-static-loader.mjs",
            "throw new Error('physical entry source must not execute');",
        ]
        .join("\n"),
    )?;
    fs::write(suite_dir.join("real.mjs"), "export default 42;\n")?;
    fs::write(suite_dir.join("child.mjs"), "export default 7;\n")?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/es-module/async-static-entry.mjs".to_string(),
            )],
        )
        .await;

    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_programmatic_registered_loader_chain(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);

    let suite_dir = instance
        .temp_dir_path()
        .join("home")
        .join("node")
        .join("test")
        .join("es-module");
    fs::create_dir_all(&suite_dir)?;
    fs::write(
        suite_dir.join("register-chain-loader-a.mjs"),
        [
            "export function resolve(specifier, context, nextResolve) {",
            "  if (!specifier.startsWith('virtual:registered-chain')) return nextResolve(specifier, context);",
            "  return nextResolve(`${specifier}:a`, context);",
            "}",
        ]
        .join("\n"),
    )?;
    fs::write(
        suite_dir.join("register-chain-loader-b.mjs"),
        [
            "let tag;",
            "export function initialize(data) { tag = data.tag; }",
            "export function resolve(specifier, context, nextResolve) {",
            "  if (!specifier.startsWith('virtual:registered-chain')) return nextResolve(specifier, context);",
            "  return nextResolve(`${specifier}:${tag}`, context);",
            "}",
        ]
        .join("\n"),
    )?;
    fs::write(
        suite_dir.join("register-chain-terminal.mjs"),
        [
            "let tag;",
            "export function initialize(data) { tag = data.tag; }",
            "export function resolve(specifier, context, nextResolve) {",
            "  if (!specifier.startsWith('virtual:registered-chain')) return nextResolve(specifier, context);",
            "  return { shortCircuit: true, url: `virtual:done:${specifier}:${tag}`, format: 'module' };",
            "}",
            "export function load(url, context, nextLoad) {",
            "  if (!url.startsWith('virtual:done:')) return nextLoad(url, context);",
            "  return { shortCircuit: true, format: 'module', source: `export default ${JSON.stringify(url)};` };",
            "}",
        ]
        .join("\n"),
    )?;
    fs::write(
        suite_dir.join("register-chain-entry.mjs"),
        [
            "import { register } from 'node:module';",
            "register('./register-chain-terminal.mjs', { parentURL: import.meta.url, data: { tag: 'terminal' } });",
            "register('./register-chain-loader-a.mjs', { parentURL: import.meta.url });",
            "register('./register-chain-loader-b.mjs', { parentURL: import.meta.url, data: { tag: 'b' } });",
            "register('./register-chain-loader-a.mjs', { parentURL: import.meta.url });",
            "const ns = await import('virtual:registered-chain');",
            "if (ns.default !== 'virtual:done:virtual:registered-chain:a:b:a:terminal') {",
            "  throw new Error('programmatic loader chain order mismatch: ' + ns.default);",
            "}",
        ]
        .join("\n"),
    )?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/es-module/register-chain-entry.mjs".to_string(),
            )],
        )
        .await;

    handle_test_result(result, &stdout, &stderr)
}

#[test_r::test]
async fn runner_module_load_uses_parent_resolution(
    prepared: &Arc<FullPreparedComponent>,
) -> anyhow::Result<()> {
    let mut instance = TestInstance::from_golem_prepared(&prepared.0).await?;
    instance.set_epoch_deadline(30);

    let suite_dir = instance
        .temp_dir_path()
        .join("home")
        .join("node")
        .join("test")
        .join("es-module");
    fs::create_dir_all(&suite_dir)?;
    fs::write(
        suite_dir.join("module-load-parent.mjs"),
        [
            "import Module from 'node:module';",
            "const parent = new Module('/home/node/test/es-module/parent.cjs');",
            "parent.filename = '/home/node/test/es-module/parent.cjs';",
            "parent.path = '/home/node/test/es-module';",
            "parent.paths = Module._nodeModulePaths('/home/node/test/es-module');",
            "parent.require = () => { throw new Error('Module._load must not call parent.require'); };",
            "const loaded = Module._load('./module-load-dep.cjs', parent);",
            "if (loaded.marker !== 42) throw new Error('Module._load did not resolve relative to parent');",
            "const resolved = Module._resolveFilename('./module-load-dep.cjs', parent);",
            "if (!resolved.endsWith('/module-load-dep.cjs')) throw new Error('Module._resolveFilename did not resolve relative to parent: ' + resolved);",
            "const viaPrototype = Module.prototype.require.call(parent, './module-load-dep.cjs');",
            "if (viaPrototype.marker !== 42) throw new Error('Module.prototype.require did not resolve relative to receiver');",
            "const customParent = new Module('synthetic-parent');",
            "if (customParent.path !== '.') throw new Error('Module constructor did not derive relative id path: ' + customParent.path);",
            "if (Object.prototype.hasOwnProperty.call(customParent, 'paths')) throw new Error('Module constructor should not define own paths');",
            "const undefinedId = new Module(undefined);",
            "if (undefinedId.id !== '' || undefinedId.path !== '.') throw new Error('Module constructor did not default undefined id');",
            "const constructorParent = new Module('/home/node/test/es-module/constructor-parent.cjs');",
            "const constructorChild = new Module('/home/node/test/es-module/subdir/constructor-child.cjs', constructorParent);",
            "if (constructorParent.path !== '/home/node/test/es-module') throw new Error('Module constructor parent path mismatch: ' + constructorParent.path);",
            "if (constructorChild.path !== '/home/node/test/es-module/subdir') throw new Error('Module constructor child path mismatch: ' + constructorChild.path);",
            "if (constructorChild.parent !== constructorParent) throw new Error('Module constructor did not expose parent');",
            "if (!constructorParent.children.includes(constructorChild)) throw new Error('Module constructor did not add child to parent.children');",
            "assertInvalidArgType(() => new Module(null));",
            "assertInvalidArgType(() => new Module(0));",
            "const arrayLikeParent = { children: { length: 0 } };",
            "const arrayLikeChild = new Module('array-like-child', arrayLikeParent);",
            "if (arrayLikeParent.children[0] !== arrayLikeChild || arrayLikeParent.children.length !== 1) throw new Error('Module constructor did not append to array-like children');",
            "customParent.path = '/not-used-for-bare-resolution';",
            "customParent.paths = ['/home/node/test/es-module/custom_lookup'];",
            "const packageLoaded = Module._load('parent-only-pkg', customParent);",
            "if (packageLoaded.marker !== 84) throw new Error('Module._load did not honor parent.paths');",
            "const packageResolved = Module._resolveFilename('parent-only-pkg', customParent);",
            "if (!packageResolved.endsWith('/custom_lookup/parent-only-pkg/index.js')) throw new Error('Module._resolveFilename did not honor parent.paths: ' + packageResolved);",
            "const pathsOptionResolved = Module._resolveFilename('paths-option-pkg', customParent, false, { paths: ['/home/node/test/es-module/paths_option'] });",
            "if (!pathsOptionResolved.endsWith('/paths_option/node_modules/paths-option-pkg/index.js')) throw new Error('Module._resolveFilename did not honor options.paths: ' + pathsOptionResolved);",
            "assertModuleNotFound(() => Module._resolveFilename('./missing-paths-option.cjs', parent, false, { paths: ['/home/node/test/es-module/paths_option'] }), '/home/node/test/es-module/parent.cjs');",
            "const packageViaPrototype = Module.prototype.require.call(customParent, 'parent-only-pkg');",
            "if (packageViaPrototype.marker !== 84) throw new Error('Module.prototype.require did not honor receiver.paths');",
            "if (Module._resolveFilename('node:module') !== 'node:module') throw new Error('Module._resolveFilename changed node: builtin specifier');",
            "if (Module._resolveFilename('module') !== 'module') throw new Error('Module._resolveFilename changed bare builtin specifier');",
            "if (!Module.isBuiltin('module')) throw new Error('Module.isBuiltin did not recognize module');",
            "if (!Module.isBuiltin('node:module')) throw new Error('Module.isBuiltin did not recognize node:module');",
            "if (!Module.builtinModules.includes('module')) throw new Error('Module.builtinModules is missing module');",
            "if (Module.builtinModules.includes('node:module')) throw new Error('Module.builtinModules should not include node:module');",
            "if (Module.prototype.require.call(parent, 'node:module').createRequire(import.meta.url).resolve.paths('module') !== null) throw new Error('require.resolve.paths should return null for module');",
            "if (Module.prototype.require.call(parent, 'node:module').createRequire(import.meta.url).resolve.paths('node:module') !== null) throw new Error('require.resolve.paths should return null for node:module');",
            "const pathOnlyParent = new Module('path-only-parent');",
            "pathOnlyParent.path = '/home/node/test/es-module/path_only_base';",
            "pathOnlyParent.paths = [];",
            "assertModuleNotFound(() => Module._load('./path-only-dep.cjs', pathOnlyParent));",
            "assertModuleNotFound(() => Module._resolveFilename('./path-only-dep.cjs', pathOnlyParent));",
            "assertModuleNotFound(() => Module.prototype.require.call(pathOnlyParent, './path-only-dep.cjs'));",
            "const compiled = new Module('/home/node/test/es-module/compiled-parent.cjs');",
            "const compiledPathBefore = compiled.path;",
            "compiled._compile('exports.filename = __filename; exports.dirname = __dirname; exports.dep = require(\"./compiled-dep.cjs\");', '/home/node/test/es-module/compiled-parent.cjs');",
            "if (compiled.filename !== null) throw new Error('Module.prototype._compile should not mutate synthetic module.filename');",
            "if (compiled.loaded !== false) throw new Error('Module.prototype._compile should not mutate synthetic module.loaded');",
            "if (compiled.path !== compiledPathBefore) throw new Error('Module.prototype._compile should not mutate synthetic module.path');",
            "if (compiled.exports.filename !== '/home/node/test/es-module/compiled-parent.cjs') throw new Error('Module.prototype._compile passed wrong __filename');",
            "if (compiled.exports.dirname !== '/home/node/test/es-module') throw new Error('Module.prototype._compile passed wrong __dirname');",
            "if (compiled.exports.dep.marker !== 252) throw new Error('Module.prototype._compile require did not resolve relative to filename');",
            "const missingCompile = new Module('/home/node/test/es-module/compiled-parent.cjs');",
            "assertModuleNotFound(() => missingCompile._compile('require(\"./missing-compiled-dep.cjs\");', '/home/node/test/es-module/compiled-parent.cjs'), '/home/node/test/es-module/compiled-parent.cjs');",
            "assertInvalidArgType(() => Module.prototype._compile.call(null, 'exports.x = 1;', '/home/node/test/es-module/null.cjs'));",
            "assertInvalidArgType(() => Module.prototype._compile.call({}, 'exports.x = 1;', '/home/node/test/es-module/plain.cjs'));",
            "const cacheRequire = Module.createRequire(import.meta.url);",
            "cacheRequire('./loaded-compile-target.cjs');",
            "const loadedModule = cacheRequire.cache[cacheRequire.resolve('./loaded-compile-target.cjs')];",
            "const unboundCompile = loadedModule._compile;",
            "assertInvalidArgType(() => unboundCompile('exports.x = 1;', '/home/node/test/es-module/unbound.cjs'));",
            "loadedModule._compile('exports.emptyFilename = __filename; exports.emptyDirname = __dirname; exports.emptyDep = require(\"./compiled-dep.cjs\");', '');",
            "if (loadedModule.exports.emptyFilename !== '') throw new Error('loaded module _compile should honor empty filename');",
            "if (loadedModule.exports.emptyDirname !== '.') throw new Error('loaded module _compile should use dot dirname for empty filename');",
            "if (loadedModule.exports.emptyDep.marker !== 252) throw new Error('loaded module _compile empty-filename require should resolve from original module');",
            "function assertModuleNotFound(fn) {",
            "  const expectedStack = arguments.length > 1 ? arguments[1] : undefined;",
            "  try { fn(); } catch (err) {",
            "    if (err && err.code === 'MODULE_NOT_FOUND') {",
            "      if (expectedStack && (!Array.isArray(err.requireStack) || !err.requireStack.includes(expectedStack))) throw err;",
            "      return;",
            "    }",
            "    throw err;",
            "  }",
            "  throw new Error('expected MODULE_NOT_FOUND');",
            "}",
            "function assertInvalidArgType(fn) {",
            "  try { fn(); } catch (err) { if (err && err.code === 'ERR_INVALID_ARG_TYPE') return; throw err; }",
            "  throw new Error('expected ERR_INVALID_ARG_TYPE');",
            "}",
        ]
        .join("\n"),
    )?;
    fs::write(
        suite_dir.join("module-load-dep.cjs"),
        "module.exports = { marker: 42 };\n",
    )?;
    let package_dir = suite_dir.join("custom_lookup").join("parent-only-pkg");
    fs::create_dir_all(&package_dir)?;
    fs::write(
        package_dir.join("index.js"),
        "module.exports = { marker: 84 };\n",
    )?;
    let paths_option_package_dir = suite_dir
        .join("paths_option")
        .join("node_modules")
        .join("paths-option-pkg");
    fs::create_dir_all(&paths_option_package_dir)?;
    fs::write(
        paths_option_package_dir.join("index.js"),
        "module.exports = { marker: 126 };\n",
    )?;
    let path_only_dir = suite_dir.join("path_only_base");
    fs::create_dir_all(&path_only_dir)?;
    fs::write(
        path_only_dir.join("path-only-dep.cjs"),
        "module.exports = { marker: 168 };\n",
    )?;
    fs::write(
        suite_dir.join("compiled-dep.cjs"),
        "module.exports = { marker: 252 };\n",
    )?;
    fs::write(
        suite_dir.join("loaded-compile-target.cjs"),
        "module.exports = { marker: 294 };\n",
    )?;

    let (result, stdout, stderr) = instance
        .invoke_and_capture_output_with_stderr(
            None,
            "run-test",
            &[Val::String(
                "/home/node/test/es-module/module-load-parent.mjs".to_string(),
            )],
        )
        .await;

    handle_test_result(result, &stdout, &stderr)
}

// --- Helper types and functions ---

/// Cloneable representation of discovery data for use in test closures.
#[derive(Clone)]
enum DiscoveryData {
    Block(Vec<BlockInfo>),
    NodeTest(Vec<TestInfo>),
}

fn timeout_error(instance: &TestInstance, timeout_secs: u64) -> anyhow::Error {
    let stdout = instance.read_stdout().unwrap_or_default();
    let stderr = instance.read_stderr().unwrap_or_default();
    anyhow::anyhow!(
        "Test timed out after {}s\n[stdout]\n{}\n[stderr]\n{}\n{}",
        timeout_secs,
        stdout.trim(),
        stderr.trim(),
        instance.failure_diagnostics()
    )
}

fn handle_test_result(
    result: anyhow::Result<Option<Val>>,
    stdout: &str,
    stderr: &str,
) -> anyhow::Result<()> {
    match result {
        Ok(Some(Val::String(ref s))) if s.starts_with("PASS") => Ok(()),
        Ok(Some(Val::String(ref s))) if s.starts_with("SKIP:") => Ok(()),
        Ok(Some(Val::String(ref s))) => {
            anyhow::bail!(
                "Test failed: {}\n[stdout]\n{}\n[stderr]\n{}",
                s,
                stdout.trim(),
                stderr.trim()
            )
        }
        Ok(other) => {
            anyhow::bail!(
                "Unexpected return: {:?}\n[stdout]\n{}\n[stderr]\n{}",
                other,
                stdout.trim(),
                stderr.trim()
            )
        }
        Err(e) => {
            anyhow::bail!(
                "Invocation error: {}\n[stdout]\n{}\n[stderr]\n{}",
                e,
                stdout.trim(),
                stderr.trim()
            )
        }
    }
}

// --- Shard tagging for CI parallelism ---

const NUM_SHARDS: u64 = 8;

fn shard_tag(name: &str) -> String {
    let mut hasher = DefaultHasher::new();
    name.hash(&mut hasher);
    format!("shard{}", hasher.finish() % NUM_SHARDS)
}

// --- Dynamic test generation ---

#[test_gen]
fn gen_node_compat_tests(r: &mut DynamicTestRegistration) {
    let entries =
        load_node_compat_config("tests/node_compat/config.jsonc").expect("Failed to load config");

    let dependency_name = "arc_fullpreparedcomponent".to_string();

    for entry in entries {
        let path = entry.path.clone();
        let file_test_name = path.replace('/', "__").replace(['.', '-'], "_");

        let test_timeout_secs = entry.timeout_secs;

        if !entry.split || entry.subtests.is_empty() {
            // Non-split: one Rust test per file (unchanged behavior)
            let props = TestProperties {
                is_ignored: entry.category.should_ignore_in_runner(),
                tags: vec![shard_tag(&file_test_name)],
                flakiness_control: if entry.flaky {
                    FlakinessControl::RetryKnownFlaky(2)
                } else {
                    FlakinessControl::None
                },
                ..TestProperties::unit_test()
            };

            r.add_async_test(
                file_test_name,
                props,
                Some(vec![dependency_name.clone()]),
                move |deps| {
                    let prepared: Arc<Arc<FullPreparedComponent>> = deps
                        .get("arc_fullpreparedcomponent")
                        .expect("FullPreparedComponent dependency not found")
                        .downcast::<Arc<FullPreparedComponent>>()
                        .expect("FullPreparedComponent type mismatch");
                    let prepared = prepared.as_ref().as_ref().0.clone();
                    let path = path.clone();
                    Box::pin(async move {
                        let mut instance = TestInstance::from_golem_prepared(&prepared).await?;
                        instance.set_epoch_deadline(test_timeout_secs);
                        setup_node_compat_test_files(instance.temp_dir_path(), &path)?;

                        let guest_path = format!("/home/node/test/{}", path);
                        let test_future = async {
                            let (result, stdout, stderr) = instance
                                .invoke_and_capture_output_with_stderr(
                                    None,
                                    "run-test",
                                    &[Val::String(guest_path)],
                                )
                                .await;

                            handle_test_result(result, &stdout, &stderr)
                        };
                        match timeout(Duration::from_secs(test_timeout_secs), test_future).await {
                            Ok(result) => result,
                            Err(_) => Err(timeout_error(&instance, test_timeout_secs)),
                        }
                    })
                },
            );
        } else {
            // Split: one Rust test per subtest
            let suite_path = format!("tests/node_compat/suite/{}", path);
            let source = match fs::read_to_string(&suite_path) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("WARNING: Cannot read split test file {}: {}", suite_path, e);
                    continue;
                }
            };

            let discovery = discover_subtests_with_options(
                &path,
                &source,
                entry.nested_node_test,
                entry.isolate_block_subtests,
            );

            // Staleness check: compare discovered subtest count vs config count
            let discovered_count = match &discovery {
                SubtestDiscovery::None => 0,
                SubtestDiscovery::Block(blocks) => blocks.len(),
                SubtestDiscovery::NodeTest(tests) => tests.len(),
            };
            assert_eq!(
                discovered_count,
                entry.subtests.len(),
                "Subtest count mismatch for {path}: config has {}, discovered {discovered_count}. Run migration tool.",
                entry.subtests.len()
            );

            for subtest in &entry.subtests {
                let test_name = format!("{}__{}", file_test_name, subtest.name);
                let is_ignored = subtest.category.should_ignore_in_runner();
                let props = TestProperties {
                    is_ignored,
                    tags: vec![shard_tag(&test_name)],
                    flakiness_control: if subtest.flaky {
                        FlakinessControl::RetryKnownFlaky(2)
                    } else {
                        FlakinessControl::None
                    },
                    ..TestProperties::unit_test()
                };

                let path = path.clone();
                let subtest_index = subtest.index;
                let source = source.clone();
                let isolate_block_subtests = entry.isolate_block_subtests;
                let discovery_clone = match &discovery {
                    SubtestDiscovery::None => None,
                    SubtestDiscovery::Block(blocks) => Some(DiscoveryData::Block(blocks.clone())),
                    SubtestDiscovery::NodeTest(tests) => {
                        Some(DiscoveryData::NodeTest(tests.clone()))
                    }
                };
                r.add_async_test(
                    test_name,
                    props,
                    Some(vec![dependency_name.clone()]),
                    move |deps| {
                        let prepared: Arc<Arc<FullPreparedComponent>> = deps
                            .get("arc_fullpreparedcomponent")
                            .expect("FullPreparedComponent dependency not found")
                            .downcast::<Arc<FullPreparedComponent>>()
                            .expect("FullPreparedComponent type mismatch");
                        let prepared = prepared.as_ref().as_ref().0.clone();
                        let path = path.clone();
                        let source = source.clone();
                        let discovery_clone = discovery_clone.clone();
                        Box::pin(async move {
                            let mut instance = TestInstance::from_golem_prepared(&prepared).await?;
                            instance.set_epoch_deadline(test_timeout_secs);
                            setup_node_compat_test_files(instance.temp_dir_path(), &path)?;

                            // Rewrite the test file to isolate the target subtest
                            let rewritten = match &discovery_clone {
                                Some(DiscoveryData::Block(blocks)) => {
                                    rewrite_for_block_with_options(
                                        &source,
                                        blocks,
                                        subtest_index,
                                        isolate_block_subtests,
                                    )
                                }
                                Some(DiscoveryData::NodeTest(tests)) => {
                                    rewrite_for_node_test(&source, tests, subtest_index)
                                }
                                None => source.clone(),
                            };

                            // Write the rewritten file to the temp dir
                            let test_filename = path.rsplit('/').next().unwrap_or(&path);
                            let suite = path.split('/').next().unwrap_or("parallel");
                            let rewritten_path = instance
                                .temp_dir_path()
                                .join("home")
                                .join("node")
                                .join("test")
                                .join(suite)
                                .join(test_filename);
                            fs::write(&rewritten_path, &rewritten)?;

                            let guest_path = format!("/home/node/test/{}", path);
                            let test_future = async {
                                let (result, stdout, stderr) = instance
                                    .invoke_and_capture_output_with_stderr(
                                        None,
                                        "run-test",
                                        &[Val::String(guest_path)],
                                    )
                                    .await;

                                handle_test_result(result, &stdout, &stderr)
                            };
                            match timeout(Duration::from_secs(test_timeout_secs), test_future).await
                            {
                                Ok(result) => result,
                                Err(_) => Err(timeout_error(&instance, test_timeout_secs)),
                            }
                        })
                    },
                );
            }
        }
    }
}