tishlang 2.0.3

Tish CLI - run, REPL, compile to native
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
//! Full-stack integration tests: run .tish files with interpreter or each backend and compare
//! stdout to static expected files (e.g. `fn_any.tish.expected`).
//!
//! - Run: `cargo test -p tishlang` (or `cargo nextest run -p tishlang`).
//! - Generate/update expected files: `REGENERATE_EXPECTED=1 cargo test -p tishlangtest_mvp_programs_interpreter`
//!   then commit the new/updated `tests/core/*.tish.expected` files.
//! - Compiled outputs are cached under `target/integration_compile_cache/` per backend.
//!   MVP native tests use `native_many/<hash>/` plus one batched nested Cargo build.

use std::collections::hash_map::DefaultHasher;
use std::ffi::OsString;
use std::hash::{Hash, Hasher};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::Command;

use rayon::prelude::*;
use tishlang_native::compile_many_to_native;

fn workspace_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
}

fn core_dir() -> PathBuf {
    workspace_root().join("tests").join("core")
}

/// Path to the static expected stdout for a .tish file (e.g. fn_any.tish -> fn_any.tish.expected).
fn expected_path(path: &Path) -> PathBuf {
    path.with_file_name(format!(
        "{}.expected",
        path.file_name().unwrap().to_string_lossy()
    ))
}

/// Read static expected stdout for a test file. Returns None if the file does not exist.
fn get_expected(path: &Path) -> Option<String> {
    let p = expected_path(path);
    std::fs::read_to_string(&p).ok()
}

fn target_dir() -> PathBuf {
    std::env::var("CARGO_TARGET_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|_| workspace_root().join("target"))
}

/// Cache dir for tish build outputs (under target/ so CI rust-cache restores it).
fn integration_compile_cache_dir() -> PathBuf {
    target_dir().join("integration_compile_cache")
}

/// Match `tish build` with no `--feature`: link every capability compiled into this `tish` binary.
fn native_build_features_for_integration_test() -> Vec<String> {
    let mut v: Vec<String> = tishlang_vm::all_compiled_capabilities()
        .into_iter()
        .collect();
    v.sort();
    v
}

fn combined_mvp_native_inputs_hash(paths: &[PathBuf]) -> u64 {
    let mut h = DefaultHasher::new();
    let feats = native_build_features_for_integration_test();
    feats.len().hash(&mut h);
    for f in &feats {
        f.hash(&mut h);
    }
    paths.len().hash(&mut h);
    for p in paths {
        p.file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .hash(&mut h);
        file_content_hash(p).hash(&mut h);
    }
    // Native batch cache must invalidate when the emitter or `value_call` changes — not only
    // when `.tish` sources change; otherwise CI/rust-cache can keep stale nested binaries.
    let codegen_rs = workspace_root().join("crates/tish_compile/src/codegen.rs");
    if codegen_rs.is_file() {
        file_content_hash(&codegen_rs).hash(&mut h);
    }
    let value_rs = workspace_root().join("crates/tish_core/src/value.rs");
    if value_rs.is_file() {
        file_content_hash(&value_rs).hash(&mut h);
    }
    // Inference (struct/param/return typing — M1/M4/M5) also drives native emission, so the
    // native batch cache must invalidate when it changes too.
    let infer_rs = workspace_root().join("crates/tish_compile/src/infer.rs");
    if infer_rs.is_file() {
        file_content_hash(&infer_rs).hash(&mut h);
    }
    h.finish()
}

fn mvp_native_batch_cache_dir(combined: u64) -> PathBuf {
    integration_compile_cache_dir()
        .join("native_many")
        .join(format!("{:016x}", combined))
}

/// Restores the previous process env when dropped (for `TISH_FAST_NATIVE_BUILD` in batch tests).
struct EnvVarGuard {
    key: &'static str,
    previous: Option<std::ffi::OsString>,
}

impl EnvVarGuard {
    fn set(key: &'static str, value: &str) -> Self {
        let previous = std::env::var_os(key);
        std::env::set_var(key, value);
        Self { key, previous }
    }
}

impl Drop for EnvVarGuard {
    fn drop(&mut self) {
        match &self.previous {
            None => std::env::remove_var(self.key),
            Some(v) => std::env::set_var(self.key, v),
        }
    }
}

fn file_content_hash(path: &Path) -> u64 {
    let mut f = std::fs::File::open(path).expect("open file for hash");
    let mut content = Vec::new();
    f.read_to_end(&mut content).expect("read file for hash");
    let mut h = DefaultHasher::new();
    path.to_string_lossy().hash(&mut h);
    content.hash(&mut h);
    h.finish()
}

/// Compile a .tish file with the given backend, using a persistent cache so we only run
/// `tish build` when the file or backend changed. Returns path to the compiled artifact
/// (binary, .js, or .wasm) in a temp dir; caller may run it and then delete it.
///
/// Cache is keyed by backend (native, cranelift, js, wasi) so e.g. cranelift and wasi
/// compiles of the same file do not overwrite each other: .../cranelift/<stem>_<hash> vs .../wasi/<stem>_<hash>.wasm.
///
/// The artifact **basename** must be unique per `(stem, hash, backend)`: nested `tish build`
/// uses it as the Cargo binary name under the workspace `target/release/`. If native and
/// cranelift both used `strict_equality_<hash>`, parallel `cargo nextest` could run those
/// tests concurrently and corrupt the same `target/release/...` output (Linux: ETXTBSY when
/// executing a binary still being written).
fn compile_cached(bin: &Path, path: &Path, backend: &str) -> PathBuf {
    let stem = path.file_stem().unwrap().to_string_lossy();
    // The cache must invalidate when the COMPILER changes, not only the `.tish` source: cranelift/wasi
    // (and native) bake the VM/codegen/runtime into the artifact, so a stale cached binary built before
    // a VM fix would silently use old behaviour. The `tish` binary's mtime is the precise signal — any
    // VM/compiler/runtime/codegen source edit rebuilds it. Mix it into the key alongside the file hash.
    let bin_stamp = std::fs::metadata(bin)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0);
    let hash = {
        let mut h = DefaultHasher::new();
        file_content_hash(path).hash(&mut h);
        bin_stamp.hash(&mut h);
        h.finish()
    };
    let hash8 = &format!("{:016x}", hash)[..8];
    let cache_base = integration_compile_cache_dir().join(backend);
    let _ = std::fs::create_dir_all(&cache_base);
    // Include `backend` in the leaf name so nested cargo bin names never collide across backends.
    let leaf = format!("{}__{}__{}", stem, backend, hash8);

    let (artifact_path, compile_args): (PathBuf, Vec<OsString>) = match backend {
        "native" => {
            let ext = if cfg!(target_os = "windows") {
                ".exe"
            } else {
                ""
            };
            let cached = cache_base.join(format!("{}{}", leaf, ext));
            let args = vec![
                OsString::from("build"),
                OsString::from(path),
                OsString::from("-o"),
                OsString::from(&cached),
            ];
            (cached, args)
        }
        "cranelift" => {
            let ext = if cfg!(target_os = "windows") {
                ".exe"
            } else {
                ""
            };
            let cached = cache_base.join(format!("{}{}", leaf, ext));
            let args = vec![
                OsString::from("build"),
                OsString::from(path),
                OsString::from("-o"),
                OsString::from(&cached),
                OsString::from("--native-backend"),
                OsString::from("cranelift"),
            ];
            (cached, args)
        }
        "js" => {
            let cached = cache_base.join(format!("{}.js", leaf));
            let args = vec![
                OsString::from("build"),
                OsString::from(path),
                OsString::from("--target"),
                OsString::from("js"),
                OsString::from("-o"),
                OsString::from(&cached),
            ];
            (cached, args)
        }
        "wasi" => {
            let out_base = cache_base.join(&leaf);
            let artifact = out_base.with_extension("wasm");
            let args = vec![
                OsString::from("build"),
                OsString::from(path),
                OsString::from("-o"),
                OsString::from(&out_base),
                OsString::from("--target"),
                OsString::from("wasi"),
            ];
            (artifact, args)
        }
        _ => panic!("unknown backend {}", backend),
    };

    if !artifact_path.exists() {
        let out = Command::new(bin)
            .args(compile_args)
            .current_dir(workspace_root())
            .output()
            .expect("run tish build");
        assert!(
            out.status.success(),
            "Compile failed for {} ({}): {}",
            path.display(),
            backend,
            String::from_utf8_lossy(&out.stderr)
        );
    }

    // Copy to temp so caller can run and delete without touching cache.
    let ext = artifact_path
        .extension()
        .map(|e| e.to_string_lossy().to_string())
        .unwrap_or_default();
    let temp_dest =
        std::env::temp_dir().join(format!("tish_cached_{}_{}_{}", backend, stem, hash8));
    let temp_dest = if ext.is_empty() {
        temp_dest
    } else {
        temp_dest.with_extension(ext)
    };
    std::fs::copy(&artifact_path, &temp_dest).expect("copy cached artifact to temp");
    temp_dest
}

/// Path to the tish CLI binary. When running under cargo-llvm-cov, the build goes to
/// target/llvm-cov-target and CARGO_TARGET_DIR may not be set for the test process.
fn tish_bin() -> PathBuf {
    let bin_name = if cfg!(target_os = "windows") {
        "tish.exe"
    } else {
        "tish"
    };
    let default = target_dir().join("debug").join(bin_name);
    if default.exists() {
        return default;
    }
    let llvm_cov = workspace_root()
        .join("target")
        .join("llvm-cov-target")
        .join("debug")
        .join(bin_name);
    if llvm_cov.exists() {
        return llvm_cov;
    }
    default
}

/// tish -V and --version print the version.
#[test]
fn test_tish_version_flag() {
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found. Run `cargo build -p tishlang` first."
    );
    let out = Command::new(&bin).arg("-V").output().expect("run tish -V");
    assert!(
        out.status.success(),
        "tish -V failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains(env!("CARGO_PKG_VERSION")),
        "tish -V should print version {}; got: {}",
        env!("CARGO_PKG_VERSION"),
        stdout
    );
    let out2 = Command::new(&bin)
        .arg("--version")
        .output()
        .expect("run tish --version");
    assert!(out2.status.success());
    let stdout2 = String::from_utf8_lossy(&out2.stdout);
    assert!(
        stdout2.contains(env!("CARGO_PKG_VERSION")),
        "tish --version should print version"
    );
}

/// Parse async-await example (validates async fn parsing).
#[test]
fn test_async_await_parse() {
    let path = workspace_root()
        .join("examples")
        .join("async-await")
        .join("src")
        .join("main.tish");
    if path.exists() {
        let source = std::fs::read_to_string(&path).unwrap();
        let result = tishlang_parser::parse(&source);
        assert!(
            result.is_ok(),
            "Parse failed for {}: {:?}",
            path.display(),
            result.err()
        );
    }
}

/// Invoke tish binary to compile async-await and run compiled output (validates non-blocking pipeline).
#[test]
#[cfg(feature = "http")]
fn test_async_await_compile_via_binary() {
    let bin = tish_bin();
    let path = workspace_root()
        .join("examples")
        .join("async-await")
        .join("src")
        .join("main.tish");
    if path.exists() && bin.exists() {
        let out = std::env::temp_dir().join("tish_async_test_out");
        let compile_result = Command::new(&bin)
            .args([
                "build",
                path.to_string_lossy().as_ref(),
                "-o",
                out.to_string_lossy().as_ref(),
            ])
            .current_dir(workspace_root())
            .output();
        let compile_out = compile_result.expect("run tish build");
        assert!(
            compile_out.status.success(),
            "tish build failed: {}",
            String::from_utf8_lossy(&compile_out.stderr)
        );
        // Run compiled binary to validate non-blocking fetchAll executes correctly
        let run_result = Command::new(&out).current_dir(workspace_root()).output();
        let run_out = run_result.expect("run compiled async binary");
        assert!(
            run_out.status.success(),
            "compiled async binary failed: {}",
            String::from_utf8_lossy(&run_out.stderr)
        );
        let stdout = String::from_utf8_lossy(&run_out.stdout);
        assert!(
            stdout.contains("Fetching"),
            "expected output to mention fetching"
        );
        assert!(stdout.contains("Done"), "expected output to contain Done");
    }
}

/// DEFINITIVE VALIDATION: Parallel fetches must be faster than sequential.
/// Uses httpbin.org/delay/1 (1s each). 3 parallel ≈ 1s, 3 sequential ≈ 3s.
#[test]
#[cfg(feature = "http")]
#[ignore = "timing and network sensitive; run manually: cargo test test_async_parallel_vs_sequential_timing -p tishlang--features http -- --ignored"]
fn test_async_parallel_vs_sequential_timing() {
    let bin = tish_bin();
    let parallel_src = workspace_root()
        .join("examples")
        .join("async-await")
        .join("src")
        .join("parallel.tish");
    let sequential_src = workspace_root()
        .join("examples")
        .join("async-await")
        .join("src")
        .join("sequential.tish");
    if !parallel_src.exists() || !sequential_src.exists() || !bin.exists() {
        return;
    }
    let out_parallel = std::env::temp_dir().join("tish_parallel_timing");
    let out_sequential = std::env::temp_dir().join("tish_sequential_timing");

    // Compile both
    let compile_par = Command::new(&bin)
        .args([
            "build",
            parallel_src.to_string_lossy().as_ref(),
            "-o",
            out_parallel.to_string_lossy().as_ref(),
        ])
        .current_dir(workspace_root())
        .output();
    assert!(
        compile_par.as_ref().unwrap().status.success(),
        "compile parallel: {}",
        String::from_utf8_lossy(&compile_par.as_ref().unwrap().stderr)
    );

    let compile_seq = Command::new(&bin)
        .args([
            "build",
            sequential_src.to_string_lossy().as_ref(),
            "-o",
            out_sequential.to_string_lossy().as_ref(),
        ])
        .current_dir(workspace_root())
        .output();
    assert!(
        compile_seq.as_ref().unwrap().status.success(),
        "compile sequential: {}",
        String::from_utf8_lossy(&compile_seq.as_ref().unwrap().stderr)
    );

    // Run parallel and time
    let t_parallel = std::time::Instant::now();
    let run_par = Command::new(&out_parallel)
        .current_dir(workspace_root())
        .output();
    let elapsed_parallel = t_parallel.elapsed();
    assert!(
        run_par.as_ref().unwrap().status.success(),
        "run parallel: {}",
        String::from_utf8_lossy(&run_par.as_ref().unwrap().stderr)
    );

    // Run sequential and time
    let t_sequential = std::time::Instant::now();
    let run_seq = Command::new(&out_sequential)
        .current_dir(workspace_root())
        .output();
    let elapsed_sequential = t_sequential.elapsed();
    assert!(
        run_seq.as_ref().unwrap().status.success(),
        "run sequential: {}",
        String::from_utf8_lossy(&run_seq.as_ref().unwrap().stderr)
    );

    // PARALLEL MUST BE FASTER: parallel < sequential * 0.6 (parallel ~1s, sequential ~3s)
    let parallel_secs = elapsed_parallel.as_secs_f64();
    let sequential_secs = elapsed_sequential.as_secs_f64();
    assert!(
        parallel_secs < sequential_secs * 0.6,
        "Async NOT validated: parallel took {:.2}s but sequential took {:.2}s. Parallel must be < 60% of sequential to prove non-blocking.",
        parallel_secs,
        sequential_secs
    );
}

/// Run async-await example via tishlang_eval (same path as `tish run`).
/// Ignored: tishlang_eval::run() is synchronous and does not run the event loop.
#[test]
#[cfg(feature = "http")]
#[ignore = "requires async runtime; use test_async_await_compile_via_binary for CI"]
fn test_async_await_run() {
    let path = workspace_root()
        .join("examples")
        .join("async-await")
        .join("src")
        .join("main.tish");
    if path.exists() {
        let source = std::fs::read_to_string(&path).unwrap();
        let result = tishlang_eval::run(&source);
        assert!(
            result.is_ok(),
            "Run failed for {}: {:?}",
            path.display(),
            result.err()
        );
    }
}

/// `promise.tish` — full Promise API including `new Promise(executor)`, `.then`, `.catch`,
/// `all`, `race`, `any`, `allSettled`. Runs via the binary (vm + interp), asserts exact output.
/// This was previously `#[ignore]`'d and only checked "doesn't error" — the old fixture also
/// avoided `new Promise` entirely, hiding a bug where the executor never ran on the VM. Now
/// CI-gated and output-asserting. vm ≡ interp.
#[test]
fn test_promise_core() {
    let bin = tish_bin();
    if !bin.exists() {
        return;
    }
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("promise.tish");
    if !path.exists() {
        return;
    }
    let expected = "\
new Promise: new-ctor
Promise sync resolve: 42
Promise.resolve: 100
Promise.reject caught: true
.then chain: 4
.catch: handled: fail
Promise.all: 1 2 3
Promise.race: fast
Promise.any: any-win
Promise.allSettled: fulfilled rejected reason
Promise tests completed
";
    for backend_args in [vec!["run"], vec!["run", "--backend", "interp"]] {
        let mut args = backend_args.clone();
        args.push(path.to_string_lossy().to_string().leak());
        let out = Command::new(&bin)
            .args(&args)
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "promise.tish ({:?}) failed: stderr={}",
            backend_args,
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "Promise output mismatch on backend {:?} — check new Promise/any/allSettled regressions",
            backend_args
        );
    }
}

/// Import aliasing — `{ x as y }` rename and `* as M` namespace. `as` is a dedicated keyword
/// token (shared with type casts), so the import-specifier parser must accept it in that position.
/// Regression: previously `import { a as b }` failed with "Expected Comma, got As". vm ≡ interp.
#[test]
fn test_import_alias() {
    let bin = tish_bin();
    if !bin.exists() {
        return;
    }
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("import_alias.tish");
    if !path.exists() {
        return;
    }
    let expected = "42\nhi there\n42\n1.0\n";
    for backend_args in [vec!["run"], vec!["run", "--backend", "vm"]] {
        let mut args = backend_args.clone();
        args.push(path.to_string_lossy().to_string().leak());
        let out = Command::new(&bin)
            .args(&args)
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "import_alias.tish ({:?}) failed: stderr={}",
            backend_args,
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "import alias output mismatch on backend {:?}",
            backend_args
        );
    }
}

/// Combined validation: async/await + Promise + setTimeout + multiple HTTP requests.
/// #97: a module's non-exported top-level bindings stay private — a same-named binding in
/// another module must not overwrite them (runtime), and the `--target js` bundle must not
/// emit duplicate `let` declarations (SyntaxError). Verified identical across interp / VM /
/// native / node, including parameter and inner-`let` shadowing.
#[test]
fn test_module_private_binding_isolation() {
    let bin = tish_bin();
    if !bin.exists() {
        return;
    }
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("private_isolation.tish");
    if !path.exists() {
        return;
    }
    let expected = "from-a:helper-a from-b:helper-b\narg inner\nhelper-b\n";

    // Interpreter + VM via `tish run`.
    for backend in ["interp", "vm"] {
        let out = Command::new(&bin)
            .args(["run", "--backend", backend])
            .arg(&path)
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "private_isolation.tish ({backend}) failed: stderr={}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "module private-binding isolation mismatch on backend {backend}"
        );
    }

    // Native: compile + run.
    let native_bin = compile_cached(&bin, &path, "native");
    let out = Command::new(&native_bin)
        .current_dir(workspace_root())
        .output()
        .expect("run native binary");
    let _ = std::fs::remove_file(&native_bin);
    assert!(
        out.status.success(),
        "private_isolation native run failed: stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert_eq!(
        String::from_utf8_lossy(&out.stdout),
        expected,
        "module private-binding isolation mismatch on native backend"
    );

    // JS target: compile + run through Node (also asserts no duplicate-`let` SyntaxError).
    let node_available = Command::new("node")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if node_available {
        let out_js = compile_cached(&bin, &path, "js");
        let out = Command::new("node")
            .arg(&out_js)
            .current_dir(workspace_root())
            .output()
            .expect("run node");
        let _ = std::fs::remove_file(&out_js);
        assert!(
            out.status.success(),
            "private_isolation JS run failed: stderr={}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "module private-binding isolation mismatch on JS target"
        );
    }
}

/// Ignored: tishlang_eval::run() does not run the event loop.
#[test]
#[cfg(feature = "http")]
#[ignore = "requires async runtime"]
fn test_async_promise_settimeout_combined() {
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("async_promise_settimeout.tish");
    if path.exists() {
        let source = std::fs::read_to_string(&path).unwrap();
        let result = tishlang_eval::run(&source);
        assert!(
            result.is_ok(),
            "Failed to run async_promise_settimeout: {:?}",
            result.err()
        );
    }
}

/// VM run with Date global (resolve+merge+bytecode+run pipeline).
#[test]
fn test_vm_date_now() {
    let path = workspace_root()
        .join("tests")
        .join("core")
        .join("date.tish");
    if !path.exists() {
        return;
    }
    // Library path
    let modules = tishlang_compile::resolve_project(&path, path.parent()).expect("resolve");
    tishlang_compile::detect_cycles(&modules).expect("cycles");
    let program = tishlang_compile::merge_modules(modules)
        .expect("merge")
        .program;
    let chunk = tishlang_bytecode::compile(&program).expect("compile");
    let result = tishlang_vm::run(&chunk);
    assert!(
        result.is_ok(),
        "VM run (library) failed: {:?}",
        result.err()
    );
    // Binary path - same flow as `tish run <file>`
    let bin = tish_bin();
    if bin.exists() {
        let out = Command::new(&bin)
            .args(["run", path.to_string_lossy().as_ref()])
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "tish run failed: stdout={} stderr={}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr)
        );
    }
}

/// `Promise.any`, `Promise.allSettled`, fixed `Promise.race` — cross-backend, network-free, CI-gated.
/// vm ≡ interp exact match.
#[test]
fn test_promise_combinators() {
    let bin = tish_bin();
    if !bin.exists() {
        return;
    }
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("promise_combinators.tish");
    if !path.exists() {
        return;
    }
    let expected = "\
any first-fulfilled: win
any all-rejected: [\"e1\",\"e2\"]
allSettled[0] ok: 10
allSettled[1] rejected: boom
allSettled[2] ok: 30
race winner: A
any passthrough: 42
";
    for backend_args in [vec!["run"], vec!["run", "--backend", "interp"]] {
        let mut args = backend_args.clone();
        args.push(path.to_string_lossy().to_string().leak());
        let out = Command::new(&bin)
            .args(&args)
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "promise_combinators ({:?}) failed: stderr={}",
            backend_args,
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "Promise.any/allSettled/race divergence on backend {:?}",
            backend_args
        );
    }
}

/// Pins tish's DOCUMENTED async/Promise ordering (docs/concurrency-model.md) — network-free, so it
/// runs in CI (unlike the `#[ignore]`'d network async tests). Asserts Promise.all order + non-promise
/// passthrough, `.then` chaining, await-reject catch, Promise.all reject short-circuit, AND the
/// deliberate blocking signature: a 0ms `setTimeout` queued before an `await` fires LAST (a JS event
/// loop would interleave it earlier — tish's `await` blocks instead of yielding). NOT compared to node
/// on purpose; this is tish's own contract. vm ≡ interp guards cross-backend agreement.
#[test]
fn test_async_ordering_documented() {
    let bin = tish_bin();
    if !bin.exists() {
        return;
    }
    let path = workspace_root()
        .join("tests")
        .join("modules")
        .join("async_ordering.tish");
    if !path.exists() {
        return;
    }
    // tish's documented, deliberately-non-JS ordering (timer drains last because await blocks).
    let expected = "\
1: sync-start
2: await = 42
2b: new Promise = ctor-ran
3: all = a b c
4: chain = 13
5: caught = boom
6: all-reject = rej
7: post-await = after-timer-was-queued
9: sync-end
8: timer-fires-LAST (await did not yield)
";
    // Both the bytecode VM and the tree-walking interpreter must agree on this contract.
    for backend_args in [vec!["run"], vec!["run", "--backend", "interp"]] {
        let mut args = backend_args.clone();
        args.push(path.to_string_lossy().to_string().leak());
        let out = Command::new(&bin)
            .args(&args)
            .current_dir(workspace_root())
            .output()
            .expect("run tish binary");
        assert!(
            out.status.success(),
            "async_ordering ({:?}) failed: stderr={}",
            backend_args,
            String::from_utf8_lossy(&out.stderr)
        );
        assert_eq!(
            String::from_utf8_lossy(&out.stdout),
            expected,
            "async ordering divergence on backend {:?} — the documented blocking-await/timer contract changed",
            backend_args
        );
    }
}

/// VM run with parse+compile only (no resolve/merge) - isolates bytecode IndexAssign.
#[test]
fn test_vm_index_assign_direct() {
    let source = r#"let arr = [1, 2, 3]; arr[1] = 99; console.log(arr[1]);"#;
    let program = tishlang_parser::parse(source).expect("parse");
    let chunk = tishlang_bytecode::compile(&program).expect("compile");
    let result = tishlang_vm::run(&chunk);
    assert!(result.is_ok(), "VM IndexAssign failed: {:?}", result.err());
}

/// VM run via resolve+merge (same as tish run) - must also pass.
#[test]
fn test_vm_index_assign_via_resolve() {
    let path = workspace_root()
        .join("tests")
        .join("core")
        .join("array_sort_minimal.tish");
    let modules = tishlang_compile::resolve_project(&path, path.parent()).expect("resolve");
    tishlang_compile::detect_cycles(&modules).expect("cycles");
    let program = tishlang_compile::merge_modules(modules)
        .expect("merge")
        .program;
    let chunk = tishlang_bytecode::compile(&program).expect("compile");
    let result = tishlang_vm::run(&chunk);
    assert!(
        result.is_ok(),
        "VM IndexAssign via resolve failed: {:?}",
        result.err()
    );
}

/// tish run binary must pass array_sort_minimal (ensures CLI works).
#[test]
fn test_tish_run_index_assign() {
    let bin = tish_bin();
    let path = workspace_root()
        .join("tests")
        .join("core")
        .join("array_sort_minimal.tish");
    if !bin.exists() {
        eprintln!("Skipping: tish binary not built");
        return;
    }
    let out = Command::new(&bin)
        .args(["run", path.to_string_lossy().as_ref()])
        .current_dir(workspace_root())
        .output()
        .expect("run tish");
    assert!(
        out.status.success(),
        "tish run failed: stdout={} stderr={}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        String::from_utf8_lossy(&out.stdout).contains("pass"),
        "Expected 'pass' in output"
    );
}

/// Full stack: lex + parse each .tish file and assert no parse error.
#[test]
fn test_full_stack_parse() {
    let core_dir = core_dir();
    for entry in std::fs::read_dir(&core_dir).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().map(|e| e == "tish").unwrap_or(false) {
            let source = std::fs::read_to_string(&path).unwrap();
            let result = tishlang_parser::parse(&source);
            assert!(
                result.is_ok(),
                "Parse failed for {}: {:?}",
                path.display(),
                result.err()
            );
        }
    }
}

// (The hand-maintained `MVP_TEST_FILES` allowlist was removed in favor of `discover_core_tests()`
// below — every `tests/core/*.tish` with a `.expected` now gets cross-backend coverage automatically.)

/// Tests whose `.expected` embeds elapsed-ms timings (perf/stress/probe) — nondeterministic, so
/// excluded from exact-output comparison. Run them via `just perf-*` instead.
const TIMING_NONDETERMINISTIC: &[&str] = &[
    "array_stress.tish",
    "array_stress_01_large_array_creation.tish",
    "array_stress_02_iteration.tish",
    "array_stress_03_map_filter_reduce.tish",
    "array_stress_04_chained.tish",
    "array_stress_05_sorting.tish",
    "array_stress_06_search.tish",
    "array_stress_07_splice_slice.tish",
    "array_stress_08_concat_spread.tish",
    "array_stress_09_flat.tish",
    "array_stress_10_objects.tish",
    "basic_types.tish",
    "benchmark_granular.tish",
    "new_features_perf.tish",
    "object_stress.tish",
    "objects_perf.tish",
    "string_methods_perf.tish",
    "recursion_stress.tish",
    "jit_probe.tish",
];

/// Known cross-backend gaps skipped on the interp↔vm parity check, with reason + a tracking note.
/// Each is a real divergence to fix, not a permanent exclusion.
///
/// Empty: the former `nested_complex.tish` gap (the VM's fixed 2-level `enclosing` lost captures
/// >2 levels deep — `level4` couldn't see `level1`'s `a`) is fixed. The VM now captures the full
/// lexical chain (`Vm.enclosing: Vec<ScopeMap>`), so closures nested arbitrarily deep resolve
/// every ancestor's locals. interp↔vm parity holds for all discovered tests.
const VM_PARITY_SKIP: &[&str] = &[];

/// Discover every `tests/core/*.tish` that has a `.expected` sibling, minus timing-nondeterministic
/// ones. Replaces the hand-maintained `MVP_TEST_FILES` allowlist so a new `*.tish` + `*.expected`
/// gets cross-backend coverage automatically (the allowlist silently left ~38 tests running nowhere).
fn discover_core_tests() -> Vec<String> {
    let mut v: Vec<String> = std::fs::read_dir(core_dir())
        .expect("read tests/core")
        .filter_map(|e| {
            let p = e.ok()?.path();
            if p.extension().map(|x| x == "tish").unwrap_or(false) && expected_path(&p).exists() {
                Some(p.file_name()?.to_string_lossy().into_owned())
            } else {
                None
            }
        })
        .filter(|n| !TIMING_NONDETERMINISTIC.contains(&n.as_str()))
        .collect();
    v.sort();
    v
}

/// True if `name` has a sibling `.js` (so it can run through the Node oracle).
fn has_js_sibling(name: &str) -> bool {
    core_dir()
        .join(name)
        .with_extension("js")
        .exists()
}

/// The gradual type checker must produce ZERO diagnostics on the (valid) corpus — any diagnostic is
/// a false positive. Lists every offender so they can be inspected/fixed.
#[test]
fn checker_no_false_positives_on_corpus() {
    let mut flagged: Vec<String> = Vec::new();
    for name in discover_core_tests() {
        let src = std::fs::read_to_string(core_dir().join(&name)).unwrap();
        if let Ok(prog) = tishlang_parser::parse(&src) {
            let diags = tishlang_compile::check_program(&prog);
            if !diags.is_empty() {
                let msgs: Vec<String> = diags.iter().map(|d| d.message.clone()).collect();
                flagged.push(format!("{name}: {}", msgs.join(" | ")));
            }
        }
    }
    assert!(
        flagged.is_empty(),
        "type checker flagged valid corpus programs (false positives):\n{}",
        flagged.join("\n")
    );
}

/// Run each .tish file with interpreter and compare stdout to static expected.
/// Set REGENERATE_EXPECTED=1 to write .expected files from interpreter output (run once, then commit).
#[test]
fn test_mvp_programs_interpreter() {
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );
    let regenerate = std::env::var("REGENERATE_EXPECTED").as_deref() == Ok("1");
    for name in &discover_core_tests() {
        let path = core_dir.join(name);
        if !path.exists() {
            continue;
        }
        let path_str = path.to_string_lossy();
        let out = Command::new(&bin)
            .args(["run", path_str.as_ref(), "--backend", "interp"])
            .current_dir(workspace_root())
            .output()
            .expect("run tish interpreter");
        assert!(
            out.status.success(),
            "Interpreter failed for {}: {}",
            path.display(),
            String::from_utf8_lossy(&out.stderr)
        );
        let stdout = String::from_utf8_lossy(&out.stdout).to_string();
        if regenerate {
            std::fs::write(expected_path(&path), &stdout).expect("write expected");
        } else {
            let expected = get_expected(&path).unwrap_or_else(|| {
                panic!(
                    "missing expected file for {}; run with REGENERATE_EXPECTED=1 to generate",
                    path.display()
                )
            });
            assert_eq!(
                stdout,
                expected,
                "Interpreter output mismatch for {}",
                path.display()
            );
        }
    }
}

/// Default bytecode VM must match the tree-walking interpreter for every MVP program.
#[test]
fn test_mvp_programs_interp_vm_stdout_parity() {
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );
    for name in &discover_core_tests() {
        if VM_PARITY_SKIP.contains(&name.as_str()) {
            continue;
        }
        let path = core_dir.join(name);
        if !path.exists() {
            continue;
        }
        let path_str = path.to_string_lossy();
        let out_interp = Command::new(&bin)
            .args(["run", path_str.as_ref(), "--backend", "interp"])
            .current_dir(workspace_root())
            .output()
            .expect("run tish interpreter");
        assert!(
            out_interp.status.success(),
            "Interpreter failed for {}: {}",
            path.display(),
            String::from_utf8_lossy(&out_interp.stderr)
        );
        let out_vm = Command::new(&bin)
            .args(["run", path_str.as_ref()])
            .current_dir(workspace_root())
            .output()
            .expect("run tish VM");
        assert!(
            out_vm.status.success(),
            "VM failed for {}: {}",
            path.display(),
            String::from_utf8_lossy(&out_vm.stderr)
        );
        let s_interp = String::from_utf8_lossy(&out_interp.stdout);
        let s_vm = String::from_utf8_lossy(&out_vm.stdout);
        assert_eq!(
            s_interp,
            s_vm,
            "interp vs VM stdout mismatch for {}",
            path.display()
        );
    }
}

/// Compile each .tish file to native, run, and compare stdout to static expected (parallelized).
#[test]
fn test_mvp_programs_native() {
    let _fast_native = EnvVarGuard::set("TISH_FAST_NATIVE_BUILD", "1");
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );

    let mut paths: Vec<PathBuf> = discover_core_tests()
        .iter()
        .filter_map(|name| {
            let p = core_dir.join(name);
            if p.exists() {
                Some(p)
            } else {
                None
            }
        })
        .collect();
    paths.sort();

    if paths.is_empty() {
        return;
    }

    let combined = combined_mvp_native_inputs_hash(&paths);
    let cache_dir = mvp_native_batch_cache_dir(combined);
    let _ = std::fs::create_dir_all(&cache_dir);

    let ext = if cfg!(target_os = "windows") {
        ".exe"
    } else {
        ""
    };

    let entries_owned: Vec<(PathBuf, PathBuf)> = paths
        .iter()
        .map(|p| {
            let stem = p.file_stem().unwrap().to_string_lossy();
            let cached = cache_dir.join(format!("{}{}", stem, ext));
            (p.clone(), cached)
        })
        .collect();

    let need_build = entries_owned.iter().any(|(_, o)| !o.exists());
    if need_build {
        let refs: Vec<(&Path, &Path)> = entries_owned
            .iter()
            .map(|(a, b)| (a.as_path(), b.as_path()))
            .collect();
        let feats = native_build_features_for_integration_test();
        compile_many_to_native(&refs, Some(workspace_root().as_path()), &feats, true)
            .unwrap_or_else(|e| panic!("compile_many_to_native: {}", e.message));
    }

    // Run each binary sequentially. Parallel `fs::copy` + `exec` caused Linux ETXTBSY (errno 26)
    // in CI when several threads replaced/ran temp executables under load.
    let errors: Vec<String> = entries_owned
        .iter()
        .enumerate()
        .filter_map(|(run_index, (path, cached_bin))| {
            let expected = match get_expected(path) {
                Some(e) => e,
                None => return Some(format!("missing expected: {}", path.display())),
            };
            if !cached_bin.exists() {
                return Some(format!("missing cached binary: {}", cached_bin.display()));
            }
            let stem = path.file_stem().unwrap().to_string_lossy();
            let ext_bin = cached_bin
                .extension()
                .map(|e| e.to_string_lossy().to_string())
                .unwrap_or_default();
            let temp_dest = std::env::temp_dir().join(format!(
                "tish_mvp_native_{}_{:x}_{}_{}",
                stem,
                file_content_hash(path),
                std::process::id(),
                run_index
            ));
            let temp_dest = if ext_bin.is_empty() {
                temp_dest
            } else {
                temp_dest.with_extension(&ext_bin)
            };
            std::fs::copy(cached_bin, &temp_dest).expect("copy cached native bin to temp");
            let out_bin = temp_dest;
            let out = match Command::new(&out_bin)
                .current_dir(workspace_root())
                .output()
            {
                Ok(o) => o,
                Err(e) => {
                    let _ = std::fs::remove_file(&out_bin);
                    return Some(format!("{}: run failed: {}", path.display(), e));
                }
            };
            let _ = std::fs::remove_file(&out_bin);
            if !out.status.success() {
                return Some(format!(
                    "{}: {}",
                    path.display(),
                    String::from_utf8_lossy(&out.stderr)
                ));
            }
            let stdout = String::from_utf8_lossy(&out.stdout);
            if stdout != expected {
                return Some(format!("{}: output mismatch", path.display()));
            }
            None
        })
        .collect();
    assert!(errors.is_empty(), "native failures:\n{}", errors.join("\n"));
}

// cranelift + wasi now use `discover_core_tests()` (full file discovery), like interp/vm/native —
// the former curated `CRANELIFT_TEST_FILES` allowlist is gone. They embed the bytecode VM, which
// has full interp↔vm parity (`VM_PARITY_SKIP` empty), so they inherit it: a disk-safe sweep confirmed
// cranelift 66/66 and wasi 66/66. The old blocker was build COST — each backend build used to emit a
// per-program `target/` (~2-5 GB) that accumulated to ~130 GB; now `tish_cranelift`/`tish_wasm` build
// into a SHARED target dir so the deps compile once (610 MB / 85 MB total for the whole sweep, and a
// repeat build is ~1 s). If a construct ever regresses on a backend, add it to a documented skip set
// rather than re-introducing an allowlist.

/// Compile each .tish file with Cranelift backend, run, and compare stdout to static expected (parallelized).
#[test]
fn test_mvp_programs_cranelift() {
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );
    let errors: Vec<String> = discover_core_tests()
        .par_iter()
        .filter_map(|name| {
            let path = core_dir.join(name);
            if !path.exists() {
                return None;
            }
            let expected = match get_expected(&path) {
                Some(e) => e,
                None => return Some(format!("missing expected: {}", path.display())),
            };
            let out_bin = compile_cached(&bin, &path, "cranelift");
            let out = match Command::new(&out_bin)
                .current_dir(workspace_root())
                .output()
            {
                Ok(o) => o,
                Err(e) => {
                    let _ = std::fs::remove_file(&out_bin);
                    return Some(format!("{}: run failed: {}", path.display(), e));
                }
            };
            let _ = std::fs::remove_file(&out_bin);
            if !out.status.success() {
                return Some(format!(
                    "{}: {}",
                    path.display(),
                    String::from_utf8_lossy(&out.stderr)
                ));
            }
            let stdout = String::from_utf8_lossy(&out.stdout);
            if stdout != expected {
                return Some(format!("{}: output mismatch", path.display()));
            }
            None
        })
        .collect();
    assert!(
        errors.is_empty(),
        "cranelift failures:\n{}",
        errors.join("\n")
    );
}

/// Compile each .tish file to WASI, run with wasmtime, and compare stdout to static expected (parallelized).
/// Skips if wasmtime is not available.
#[test]
fn test_mvp_programs_wasi() {
    let wasmtime_available = Command::new("wasmtime")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if !wasmtime_available {
        eprintln!("Skipping test_mvp_programs_wasi: wasmtime not found");
        return;
    }
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );
    let errors: Vec<String> = discover_core_tests()
        .par_iter()
        .filter_map(|name| {
            let path = core_dir.join(name);
            if !path.exists() {
                return None;
            }
            let expected = match get_expected(&path) {
                Some(e) => e,
                None => return Some(format!("missing expected: {}", path.display())),
            };
            let out_wasm = compile_cached(&bin, &path, "wasi");
            let out = match Command::new("wasmtime")
                .arg(out_wasm.as_os_str())
                .current_dir(workspace_root())
                .output()
            {
                Ok(o) => o,
                Err(e) => {
                    let _ = std::fs::remove_file(&out_wasm);
                    return Some(format!("{}: wasmtime failed: {}", path.display(), e));
                }
            };
            let _ = std::fs::remove_file(&out_wasm);
            if !out.status.success() {
                return Some(format!(
                    "{}: {}",
                    path.display(),
                    String::from_utf8_lossy(&out.stderr)
                ));
            }
            let stdout = String::from_utf8_lossy(&out.stdout);
            if stdout != expected {
                return Some(format!("{}: output mismatch", path.display()));
            }
            None
        })
        .collect();
    assert!(errors.is_empty(), "wasi failures:\n{}", errors.join("\n"));
}

/// Files where Tish intentionally differs from JavaScript (typeof, void); skip in JS test since we compare to Tish expected.
const JS_SKIP_FILES: &[&str] = &["typeof.tish", "void.tish"];

/// Compile each .tish file to JS, run with Node, and compare stdout to static expected.
#[test]
fn test_mvp_programs_js() {
    let node_available = Command::new("node")
        .args(["--version"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if !node_available {
        eprintln!("Skipping test_mvp_programs_js: Node.js not found");
        return;
    }
    let core_dir = core_dir();
    let bin = tish_bin();
    assert!(
        bin.exists(),
        "tish binary not found at {}. Run `cargo build -p tishlang` first.",
        bin.display()
    );
    for name in &discover_core_tests() {
        // intentional JS divergences + tests without a `.js` sibling can't use the Node oracle
        if JS_SKIP_FILES.contains(&name.as_str()) || !has_js_sibling(name) {
            continue;
        }
        let path = core_dir.join(name);
        if !path.exists() {
            continue;
        }
        let expected = get_expected(&path).unwrap_or_else(|| {
            panic!(
                "missing expected file for {}; run with REGENERATE_EXPECTED=1 to generate",
                path.display()
            )
        });
        let out_js = compile_cached(&bin, &path, "js");
        let out = Command::new("node")
            .arg(&out_js)
            .current_dir(workspace_root())
            .output()
            .expect("run node");
        let _ = std::fs::remove_file(&out_js);
        assert!(
            out.status.success(),
            "Node failed for {}: {}",
            path.display(),
            String::from_utf8_lossy(&out.stderr)
        );
        let stdout = String::from_utf8_lossy(&out.stdout);
        assert_eq!(
            stdout,
            expected,
            "JS output mismatch for {}",
            path.display()
        );
    }
}