rudof_cli 0.3.17

RDF and Knowledge Graphs processing tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
//! Integration tests for the `rudof shell` interactive REPL.
//!
//! These spawn the actual `rudof` binary with `shell` as an argument and
//! drive it over stdin/stdout, the same way a user would, since the
//! interesting behavior here (the readline loop, command dispatch, and
//! session state persisting across commands) only shows up end-to-end.

#[cfg(not(target_family = "wasm"))]
use std::io::Write as _;
#[cfg(not(target_family = "wasm"))]
use std::path::Path;
#[cfg(not(target_family = "wasm"))]
use std::process::{Command, Stdio};

#[cfg(not(target_family = "wasm"))]
struct ShellOutput {
    stdout: String,
    stderr: String,
    code: i32,
}

/// The last line of `shell::repl::BANNER`, byte-for-byte — that banner is
/// built from ANSI-colored fragments (unconditionally, not gated on TTY/
/// `NO_COLOR` detection), so the plain ASCII-art substring a colorless
/// banner would have never appears in the shell's actual output.
#[cfg(not(target_family = "wasm"))]
const BANNER_LAST_LINE: &str = concat!(
    "\x1b[34m|_|    \x1b[0m",
    "\x1b[90m\\____|\x1b[0m",
    "\x1b[34m\\____|\x1b[0m",
    "\x1b[90m\\___/\x1b[0m",
    "\x1b[34m|_|\x1b[0m",
);

/// Runs `rudof shell`, feeding it `input` on stdin, and returns its
/// captured stdout/stderr and exit code.
///
/// Each call gets its own `HOME` (a fresh temp dir) so the shell's
/// `~/.rudof_history` file never touches the real home directory or
/// collides with other tests running in parallel.
#[cfg(not(target_family = "wasm"))]
fn run_shell(input: &str) -> ShellOutput {
    let home = tempfile::tempdir().expect("failed to create temp HOME for shell test");

    let mut child = Command::new(env!("CARGO_BIN_EXE_rudof"))
        .arg("shell")
        .env("HOME", home.path())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("failed to spawn `rudof shell`");

    child
        .stdin
        .take()
        .expect("child stdin was not piped")
        .write_all(input.as_bytes())
        .expect("failed to write to `rudof shell` stdin");

    let output = child.wait_with_output().expect("failed to wait for `rudof shell`");

    ShellOutput {
        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
        stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
        code: output.status.code().unwrap_or(-1),
    }
}

#[cfg(not(target_family = "wasm"))]
impl ShellOutput {
    fn transcript(&self) -> String {
        format!("--- stdout ---\n{}\n--- stderr ---\n{}", self.stdout, self.stderr)
    }
}

#[cfg(not(target_family = "wasm"))]
fn fixture(name: &str) -> String {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures")
        .join(name)
        .to_string_lossy()
        .into_owned()
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prints_banner_and_exits_cleanly_on_exit() {
    let out = run_shell("exit\n");
    // The ASCII art banner is printed on startup, before the tip line.
    assert!(out.stdout.contains(BANNER_LAST_LINE));
    assert!(
        out.stdout
            .contains("Type 'help' for available commands, 'exit' to quit.")
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_quit_is_an_alias_for_exit() {
    let out = run_shell("quit\n");
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_ends_cleanly_on_eof_without_exit() {
    // Closing stdin without ever typing `exit` should still terminate the loop.
    let out = run_shell("");
    assert!(out.stdout.contains(BANNER_LAST_LINE));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_help_lists_top_level_commands_and_shell_builtins() {
    let out = run_shell("help\nexit\n");
    assert!(out.stdout.contains("Usage: rudof <COMMAND>"));
    assert!(out.stdout.contains("Shell-only commands:"));
    assert!(out.stdout.contains("exit, quit"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_unknown_subcommand_reports_error_and_keeps_session_alive() {
    let out = run_shell("nonexistent-command\nhelp\nexit\n");
    assert!(out.stdout.contains("unrecognized subcommand"));
    // The session kept going after the bad command: `help` still ran.
    assert!(out.stdout.contains("Shell-only commands:"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_treats_unterminated_quote_as_multiline_continuation() {
    // An open quote (e.g. a SPARQL query passed inline to `-q`) makes the
    // shell keep reading further lines as part of the same command instead
    // of dispatching or erroring immediately, so a query can be typed
    // across several lines at the prompt.
    let data = fixture("shell_data.ttl");
    let script = format!("query -q 'SELECT ?s ?p ?o WHERE {{\n?s ?p ?o .\n}}' {data}\nexit\n");
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("Alice"),
        "expected query results, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_discards_unterminated_quote_left_open_at_eof() {
    // If stdin closes while still inside an open quote, the pending
    // command is discarded with a warning instead of being misparsed —
    // and the lines absorbed as continuation input (`help`, `exit`) never
    // ran as commands in their own right.
    let out = run_shell("data \"unterminated\nhelp\nexit\n");
    assert!(out.stderr.contains("unterminated quote"));
    assert!(!out.stdout.contains("Shell-only commands:"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_typing_shell_again_does_not_recurse() {
    let out = run_shell("shell\nexit\n");
    assert!(out.stdout.contains("Already inside the shell."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_bare_data_without_prior_load_reports_no_data_loaded() {
    let out = run_shell("data\nexit\n");
    assert!(out.stderr.contains("No data loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_persists_loaded_data_across_commands() {
    let data = fixture("shell_data.ttl");
    let script = format!("data {data}\ndata\nexit\n");
    let out = run_shell(&script);

    // The first call prints a stats line for the data it just loaded; the
    // second, bare call has nothing new to load, so it re-shows the full
    // loaded data instead (not another stats line).
    assert_eq!(out.stdout.matches("2 triple(s) loaded").count(), 1);
    assert_eq!(
        out.stdout.matches("Alice").count(),
        1,
        "expected the second, bare 'data' call to re-show the full loaded data, got:\n{}",
        out.stdout
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_data_replaces_by_default_instead_of_merging() {
    let data1 = fixture("shell_data.ttl");
    let data2 = fixture("shell_data2.ttl");
    let script = format!("data {data1}\ndata {data2}\ndata\nexit\n");
    let out = run_shell(&script);

    // The second `data FILE` call, with no `--merge`, replaces the first
    // file's data rather than merging into it: its own stats line reports
    // only its own triple, and the final bare `data` shows only that data.
    assert!(
        out.stdout.contains("1 triple(s) loaded"),
        "expected the second load to report only its own triple, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains("bob") && !out.stdout.contains("Alice"),
        "expected the final bare 'data' to show only the second file's data, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_data_merge_flag_merges_instead_of_replacing() {
    let data1 = fixture("shell_data.ttl");
    let data2 = fixture("shell_data2.ttl");
    let script = format!("data {data1}\ndata {data2} --merge\ndata\nexit\n");
    let out = run_shell(&script);

    // With `--merge`, the second load's stats line counts both files'
    // triples, and the final bare `data` shows the union of both.
    assert!(
        out.stdout.contains("3 triple(s) loaded"),
        "expected the merged load to report triples from both files, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains("bob") && out.stdout.contains("Alice"),
        "expected the final bare 'data' to show both files' data, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_bare_dctap_without_prior_load_reports_no_dctap_loaded() {
    let out = run_shell("dctap\nexit\n");
    assert!(out.stderr.contains("No DCTap data loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_persists_loaded_dctap_across_commands() {
    let dctap = fixture("shell_dctap.csv");
    let script = format!("dctap -s {dctap}\ndctap\nexit\n");
    let out = run_shell(&script);

    // The first call prints a stats line for the DCTap it just loaded; the
    // second, bare call has nothing new to load, so it re-shows the full
    // loaded DCTap instead (not another stats line).
    assert_eq!(out.stdout.matches("1 shape(s) loaded").count(), 1);
    assert_eq!(
        out.stdout.matches("xsd:string").count(),
        1,
        "expected the second, bare 'dctap' call to re-show the full loaded DCTap, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_bare_service_without_prior_load_reports_no_service_description() {
    let out = run_shell("service\nexit\n");
    assert!(out.stderr.contains("No Service Description loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_persists_loaded_service_description_across_commands() {
    let service = fixture("shell_service.ttl");
    let script = format!("service -s {service}\nservice\nexit\n");
    let out = run_shell(&script);

    // The first call prints a stats line for the service description it just
    // loaded; the second, bare call has nothing new to load, so it re-shows
    // the full loaded service description instead (not another stats line).
    assert_eq!(out.stdout.matches("feature(s) loaded").count(), 1);
    assert_eq!(
        out.stdout.matches("SPARQL11Query").count(),
        1,
        "expected the second, bare 'service' call to re-show the full loaded service description, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_output_flag_redirects_to_file_instead_of_printing() {
    let out_dir = tempfile::tempdir().expect("failed to create temp output dir");
    let out_file = out_dir.path().join("data.ttl");
    let data = fixture("shell_data.ttl");

    let script = format!("data {data} -o {}\nexit\n", out_file.display());
    let out = run_shell(&script);

    assert!(out_file.exists(), "expected {} to be created", out_file.display());
    let written = std::fs::read_to_string(&out_file).expect("failed to read redirected output file");
    assert!(
        written.contains("Alice"),
        "expected the data in the output file, got:\n{written}"
    );

    // The command's actual output went to the file, not the terminal.
    assert!(
        !out.stdout.contains("Alice"),
        "did not expect the data to be printed to stdout, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains(&format!("Output saved in {}", out_file.display())),
        "expected a confirmation message naming the output file, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_output_flag_dash_still_prints_to_terminal() {
    // `-o -` is the standard Unix convention for "write to stdout"; it must
    // not be treated as a file redirection (no file created, no confirmation
    // message — the data prints normally, same as with no `-o` at all).
    let data = fixture("shell_data.ttl");
    let script = format!("data {data} -o -\nexit\n");
    let out = run_shell(&script);

    assert!(out.stdout.contains("2 triple(s) loaded"));
    assert!(!out.stdout.contains("Output saved in"));
    assert_eq!(out.code, 0);
}

// ============================================================================
// Cross-command state reuse: commands that consume multiple resources
// (data, a ShEx/SHACL/PGSchema schema, a shapemap/typemap) should be able to
// omit args for pieces that were already loaded by an earlier command in the
// same shell session, instead of requiring everything to be re-specified.
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_validate_without_any_state_reports_clear_error() {
    let out = run_shell("shex-validate\nexit\n");
    assert!(out.stderr.contains("No data loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_validate_reuses_data_schema_and_shapemap_loaded_separately() {
    let data = fixture("shell_data.ttl");
    let schema = fixture("shell_schema.shex");
    let shapemap = fixture("shell_shapemap.sm");

    let script = format!("data {data}\nshex -s {schema}\nshapemap -m {shapemap}\nshex-validate\nexit\n");
    let out = run_shell(&script);

    // "Shape passed" only appears in shex-validate's own validation report,
    // not in the `data`/`shex`/`shapemap` commands' echoes of what they
    // each loaded — so this can't pass just because an earlier command
    // happened to print matching text.
    assert!(
        out.stdout.contains("Shape passed"),
        "expected a validation report reusing the separately-loaded data/schema/shapemap, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shacl_validate_without_any_state_reports_clear_error() {
    let out = run_shell("shacl-validate\nexit\n");
    assert!(out.stderr.contains("No data loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shacl_validate_reuses_data_and_shapes_loaded_separately() {
    let data = fixture("shell_data.ttl");
    let shapes = fixture("shell_shapes.ttl");

    let script = format!("data {data}\nshacl -s {shapes}\nshacl-validate\nexit\n");
    let out = run_shell(&script);

    assert!(
        out.stdout.contains("No Errors found") || out.stdout.contains("Errors"),
        "expected a SHACL validation report reusing the separately-loaded data/shapes, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_node_reuses_data_loaded_separately() {
    let data = fixture("shell_data.ttl");
    let script = format!("data {data}\nnode -n :alice\nexit\n");
    let out = run_shell(&script);

    // "Outgoing arcs" only appears in `node`'s own report, not in `data`'s
    // echo of what it loaded.
    assert!(
        out.stdout.contains("Outgoing arcs"),
        "expected `node` to reuse the separately-loaded data, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_materialize_reuses_schema_loaded_separately() {
    let schema = fixture("shell_schema.shex");
    let script = format!("shex -s {schema}\nmaterialize\nexit\n");
    let out = run_shell(&script);

    // The schema is reused (no "required arguments" clap error); the only
    // thing still missing is the MapState, which nothing else loaded here.
    assert!(
        out.stderr.contains("No MapState available"),
        "expected materialize to reuse the separately-loaded schema and fail only on missing MapState, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_pgschema_validate_reuses_schema_typemap_and_data_across_calls() {
    let workspace_root = Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .expect("rudof_cli has a parent directory");
    let pgs = workspace_root.join("examples/property_graphs/demo.pgs");
    let map = workspace_root.join("examples/property_graphs/demo.map");
    let pg = workspace_root.join("examples/property_graphs/demo.pg");

    let script = format!(
        "pgschema-validate -s {} -m {} {}\npgschema-validate\nexit\n",
        pgs.display(),
        map.display(),
        pg.display()
    );
    let out = run_shell(&script);

    // The second, bare call re-validates by reusing the schema, typemap and
    // data loaded by the first call, so the same report appears twice.
    let occurrences = out.stdout.matches("PersonType").count();
    assert_eq!(
        occurrences,
        2,
        "expected the validation report to appear twice, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

// ============================================================================
// Shell-only convenience: `<command> <file>` (a single bare positional, with
// nothing else on the line) is accepted as an alternative to `<command> -s
// <file>` for commands whose primary resource flag is otherwise required.
// This only applies inside the shell, and only when the line is unambiguous
// (exactly one token besides the command name) — the top-level CLI and any
// line combining the bare file with other flags are unaffected.
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_accepts_bare_file_as_schema() {
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!("shex {schema}\nexit\n"));

    assert!(
        out.stdout.contains("1 shape(s) loaded"),
        "expected the bare file to be loaded as the schema, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_unsupported_result_format_reports_an_error_instead_of_crashing() {
    // Regression test: `shex -r <format>` used to hit an unconditional
    // `todo!()` for any RDF format (turtle, rdfxml, ...) and panic, killing
    // the whole session. Those formats are implemented now (see
    // `shell_shex_r_turtle_serializes_the_schema_as_rdf` below); `simple` is
    // the one ShEx format left with no writer, so it's what now exercises
    // this "a normal error is reported, and the session survives it" path.
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!("shex -s {schema}\nshex -r simple\nshex\nexit\n"));

    assert!(
        out.stderr.contains("Failed to serialize ShEx schema to simple"),
        "expected a clean error for the unsupported serialization format, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    // Regression test: the error message used to be printed twice (the
    // outer `RudofError`'s `Display` already embeds the full inner error
    // text, and anyhow's `{:#}` used to additionally walk the `source()`
    // chain and repeat it).
    assert_eq!(
        out.stderr.matches("Failed to serialize ShEx schema to simple").count(),
        1,
        "expected the error message to appear exactly once, got stderr:\n{}",
        out.transcript()
    );
    // The final bare `shex` still ran and showed the schema, proving the
    // session survived the previous command's error.
    assert!(
        out.stdout.contains("PersonShape"),
        "expected the session to keep working after the error, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_r_turtle_serializes_the_schema_as_rdf() {
    // `shex FILE` loads a schema, and a later bare `shex -r turtle` shows it
    // serialized as RDF (using the ShExR vocabulary) instead of erroring —
    // the scenario `shell_shex_unsupported_result_format_reports_an_error_instead_of_crashing`
    // used to cover before RDF output formats were implemented.
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!("shex -s {schema}\nshex -r turtle\nexit\n"));

    assert!(out.stderr.is_empty(), "expected no error, got stderr:\n{}", out.stderr);
    assert!(
        out.stdout.contains("@prefix sx: <http://www.w3.org/ns/shex#> ."),
        "expected the ShExR vocabulary's own prefix to be declared, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains("sx:Schema"),
        "expected ShExR RDF output, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains("sx:ShapeDecl"),
        "expected ShExR RDF output, got:\n{}",
        out.transcript()
    );
    // The schema's own prefixes (`PREFIX : <http://example.org/>` in
    // shell_schema.shex) are carried over into the generated RDF too.
    assert!(
        out.stdout.contains("@prefix : <http://example.org/> ."),
        "expected the schema's own prefix to be declared, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains(":PersonShape"),
        "expected the schema's prefix to be used in the output, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_data_parse_error_is_not_duplicated() {
    // Regression test: a `Data` parse error used to be printed twice, the
    // same way the `shex -r turtle` case above was.
    let not_turtle = fixture("shell_schema.shex"); // ShExC, not valid Turtle
    let out = run_shell(&format!("data {not_turtle}\nexit\n"));

    assert!(
        out.stderr.contains("Failed to parse RDF data from"),
        "expected a parse error, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(
        out.stderr.matches("Failed to parse RDF data from").count(),
        1,
        "expected the error message to appear exactly once, got stderr:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_bare_file_convenience_does_not_apply_alongside_other_flags() {
    // Combining the bare file with another flag is ambiguous, so it's left
    // to clap's normal parsing (and normal error) rather than guessed at.
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!("shex {schema} -f shexc\nexit\n"));

    assert!(
        out.stdout.contains("unrecognized") || out.stdout.contains("unexpected argument"),
        "expected a clap parse error, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

// ============================================================================
// `!` escapes to the system shell, e.g. `! ls` or `!code file.shex`.
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[cfg(unix)]
#[test]
fn shell_bang_with_space_runs_external_command() {
    let out = run_shell("! echo hello-from-outside\nexit\n");
    assert!(
        out.stdout.contains("hello-from-outside"),
        "expected the external command's output in stdout, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[cfg(unix)]
#[test]
fn shell_bang_without_space_runs_external_command() {
    let out = run_shell("!echo hello-from-outside\nexit\n");
    assert!(
        out.stdout.contains("hello-from-outside"),
        "expected the external command's output in stdout, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[cfg(unix)]
#[test]
fn shell_bang_uses_a_real_shell_so_pipes_work() {
    let out = run_shell("!echo hello | tr a-z A-Z\nexit\n");
    assert!(
        out.stdout.contains("HELLO"),
        "expected shell-interpreted output (pipe), got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_bang_alone_reports_no_command_given() {
    let out = run_shell("!\nexit\n");
    assert!(
        out.stdout.contains("No command given after '!'"),
        "expected a hint about the missing command, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[cfg(unix)]
#[test]
fn shell_bang_failing_command_does_not_end_the_session() {
    let out = run_shell("!false\nhelp\nexit\n");
    assert!(
        out.stdout.contains("Shell-only commands:"),
        "expected the session to keep going after a failing external command, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_node_accepts_bare_identifier_as_node_flag() {
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!("data {data}\nnode :alice\nexit\n"));

    assert!(
        out.stdout.contains("Outgoing arcs"),
        "expected `node :alice` to behave like `node -n :alice`, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

// ============================================================================
// `endpoint [NAME]` — shell-only command to show or activate a registered
// SPARQL endpoint for the rest of the session.
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_with_no_active_endpoint_points_at_the_command() {
    let out = run_shell("endpoint\nexit\n");
    assert!(out.stdout.contains("No endpoint is currently active."));
    assert!(out.stdout.contains("Use 'endpoint NAME' to activate one."));
    // The default config registers these endpoints out of the box, shown
    // capitalized (the registered key is now each endpoint's own `name`).
    assert!(out.stdout.contains("Registered endpoints:"));
    assert!(out.stdout.contains("Wikidata"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_activates_a_registered_endpoint_and_reports_it_back() {
    // Lower-case input still resolves the endpoint (matched
    // case-insensitively); the report uses its canonical, capitalized name.
    let out = run_shell("endpoint wikidata\nexit\n");
    assert!(
        out.stdout
            .contains("Active endpoint: Wikidata (https://query.wikidata.org/sparql)")
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_activation_is_case_insensitive() {
    // "wikidata", "Wikidata" and "WikiData" all resolve the endpoint
    // registered as "Wikidata", regardless of casing typed.
    for spelling in ["wikidata", "Wikidata", "WikiData", "WIKIDATA"] {
        let out = run_shell(&format!("endpoint {spelling}\nexit\n"));
        assert!(
            out.stdout
                .contains("Active endpoint: Wikidata (https://query.wikidata.org/sparql)"),
            "expected '{spelling}' to resolve the Wikidata endpoint, got:\n{}",
            out.transcript()
        );
        assert_eq!(out.code, 0);
    }
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_bare_reports_the_previously_activated_endpoint() {
    let out = run_shell("endpoint wikidata\nendpoint\nexit\n");
    let occurrences = out
        .stdout
        .matches("Active endpoint: Wikidata (https://query.wikidata.org/sparql)")
        .count();
    assert_eq!(
        occurrences,
        2,
        "expected the activation and the bare follow-up to both report it, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_rejects_unknown_name() {
    let out = run_shell("endpoint not-a-real-endpoint\nexit\n");
    assert!(out.stdout.contains("Unknown endpoint 'not-a-real-endpoint'."));
    assert!(out.stdout.contains("Registered endpoints:"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_rejects_too_many_arguments() {
    let out = run_shell("endpoint wikidata dbpedia\nexit\n");
    assert!(out.stdout.contains("Usage: endpoint [NAME|FILE.toml]"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_registers_and_activates_a_new_endpoint_from_a_toml_file() {
    let path = fixture("shell_endpoint.toml");
    let script = format!("endpoint {path}\nexit\n");
    let out = run_shell(&script);

    assert!(
        out.stdout
            .contains(&format!("Registered endpoint 'ShellEndpoint' from {path}"))
    );
    assert!(
        out.stdout
            .contains("Active endpoint: ShellEndpoint (https://example.org/sparql)")
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_registered_from_file_persists_for_the_rest_of_the_session() {
    let path = fixture("shell_endpoint.toml");
    let script = format!("endpoint {path}\nconfig get rdf.endpoints.ShellEndpoint.query_url\nexit\n");
    let out = run_shell(&script);

    assert!(
        out.stdout.contains("https://example.org/sparql"),
        "expected the newly-registered endpoint to be visible via 'config get', got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_endpoint_reports_a_missing_toml_file() {
    let out = run_shell("endpoint /no/such/endpoint.toml\nexit\n");
    assert!(out.stderr.contains("Failed to load endpoint description"));
    assert_eq!(out.code, 0);
}

// ============================================================================
// `reset [TARGET...]` — shell-only command to clear session state, either
// everything (bare `reset`/`reset all`) or one or more named pieces.
// ============================================================================

// ============================================================================
// `shex-check` — checks a ShEx schema's well-formedness without touching
// session state (no schema/data/ShapeMap loaded, unaffected by `reset`).
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn shex_check_reports_a_well_formed_schema_as_valid() {
    let out = run_shell("shex-check -s \"PREFIX : <http://example.org/> :S { :p . }\"\nexit\n");
    assert!(out.stdout.contains("Schema is valid"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shex_check_reports_a_negative_cycle_without_loading_the_schema() {
    let out =
        run_shell("shex-check -s \"PREFIX : <http://example.org/> :S { :p @:T } :T { :q NOT @:S }\"\nshex\nexit\n");
    assert!(out.stdout.contains("negative cycles"));
    // `shex-check` never touches the session's loaded schema.
    assert!(out.stderr.contains("No ShEx schema loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_target_clears_only_that_state() {
    let data = fixture("shell_data.ttl");
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!("data {data}\nshex {schema}\nreset shex\nshex\ndata\nexit\n"));

    // `shex` errors after the reset (schema gone)...
    assert!(
        out.stderr.contains("No ShEx schema loaded"),
        "expected the schema to be gone after 'reset shex', got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    // ...but `data`, never targeted, still shows what was loaded.
    assert!(
        out.stdout.contains("2 triple(s) loaded"),
        "expected 'reset shex' to leave data untouched, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_shex_also_drops_the_compiled_validator() {
    // Regression test: loading a shapemap only used to check `shex_validator`,
    // not the schema itself, so a stale validator left behind by an incomplete
    // reset let this silently succeed against a schema that was supposedly gone.
    let data = fixture("shell_data.ttl");
    let schema = fixture("shell_schema.shex");
    let shapemap = fixture("shell_shapemap.sm");
    let out = run_shell(&format!(
        "data {data}\nshex {schema}\nreset shex\nshapemap -m {shapemap}\nexit\n"
    ));

    assert!(
        out.stderr.contains("No ShEx schema loaded"),
        "expected loading a shapemap to fail once the schema (and its compiled validator) were reset, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_shex_validation_also_clears_the_shapemap() {
    // Unlike the narrow `reset shex` (schema only), `reset shex-validation`
    // clears the schema, the ShapeMap, and any validation results together.
    let data = fixture("shell_data.ttl");
    let schema = fixture("shell_schema.shex");
    let shapemap = fixture("shell_shapemap.sm");
    let out = run_shell(&format!(
        "data {data}\nshex {schema}\nshapemap -m {shapemap}\nreset shex-validation\nshex\nshapemap\ndata\nexit\n"
    ));

    assert!(
        out.stderr.contains("No ShEx schema loaded"),
        "expected the schema to be gone after 'reset shex-validation', got stderr:\n{}",
        out.stderr
    );
    assert!(
        out.stderr.contains("No shapemap loaded"),
        "expected the ShapeMap to be gone after 'reset shex-validation', got stderr:\n{}",
        out.stderr
    );
    // ...but `data`, never targeted, still shows what was loaded.
    assert!(out.stdout.contains("2 triple(s) loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_shacl_target_clears_only_the_shapes() {
    let shapes = fixture("shell_shapes.ttl");
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!(
        "data {data}\nshacl {shapes}\nreset shacl\nshacl\ndata\nexit\n"
    ));

    assert!(
        out.stderr.contains("No SHACL shapes loaded"),
        "expected the shapes to be gone after 'reset shacl', got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert!(out.stdout.contains("2 triple(s) loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_shacl_validation_also_clears_the_shapes() {
    let shapes = fixture("shell_shapes.ttl");
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!(
        "data {data}\nshacl {shapes}\nreset shacl-validation\nshacl\ndata\nexit\n"
    ));

    assert!(
        out.stderr.contains("No SHACL shapes loaded"),
        "expected the shapes to be gone after 'reset shacl-validation', got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert!(out.stdout.contains("2 triple(s) loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_bare_clears_everything() {
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!(
        "data {data}\nendpoint wikidata\nreset\ndata\nendpoint\nexit\n"
    ));

    assert!(out.stderr.contains("No data loaded"));
    assert!(out.stdout.contains("No endpoint is currently active."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_all_is_an_alias_for_bare_reset() {
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!("data {data}\nreset all\ndata\nexit\n"));

    assert!(out.stdout.contains("Reset all session state."));
    assert!(out.stderr.contains("No data loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_accepts_multiple_targets() {
    let data = fixture("shell_data.ttl");
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!(
        "data {data}\nshex {schema}\nreset data shex\ndata\nshex\nexit\n"
    ));

    assert!(out.stdout.contains("Reset: data, shex."));
    assert!(out.stderr.contains("No data loaded"));
    assert!(out.stderr.contains("No ShEx schema loaded"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_rejects_unknown_target() {
    let out = run_shell("reset bogus\nexit\n");
    assert!(out.stdout.contains("Unknown reset target(s): bogus."));
    assert!(out.stdout.contains("Valid targets:"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_reset_endpoint_clears_the_active_endpoint() {
    let schema = fixture("shell_schema.shex");
    let out = run_shell(&format!(
        "endpoint wikidata\nreset endpoint\nendpoint\nshex {schema}\nshex\nexit\n"
    ));

    assert!(out.stdout.contains("No endpoint is currently active."));
    // A target unrelated to `endpoint` is untouched by `reset endpoint`.
    assert!(out.stdout.contains("1 shape(s) loaded"));
    assert_eq!(out.code, 0);
}

// ============================================================================
// `resolvers [clear]` — shell-only command to list the built-in external
// ShEx-shape resolver kinds, or clear the session's resolver chain back to
// its default (`reject-all` only).
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn resolvers_bare_lists_the_built_in_kinds() {
    let out = run_shell("resolvers\nexit\n");
    assert!(out.stdout.contains("reject-all"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn resolvers_clear_resets_the_chain_and_reports_success() {
    let out = run_shell("resolvers clear\nexit\n");
    assert!(out.stdout.contains("Cleared external-shape resolvers"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_node_bare_identifier_convenience_does_not_apply_alongside_other_flags() {
    // With another flag present, `:alice` is left to normal parsing, where
    // it falls to the (real) `data` positional and fails as a bad file
    // path — not silently reinterpreted as the node to inspect.
    let data = fixture("shell_data.ttl");
    let out = run_shell(&format!("data {data}\nnode :alice -d 2\nexit\n"));

    assert!(
        out.stdout.contains("does not exist"),
        "expected a clap parse error when combined with another flag, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_bare_dumps_the_whole_config_as_toml() {
    let out = run_shell("config\nexit\n");
    assert!(out.stdout.contains("auto_base = false"));
    assert!(out.stdout.contains("[shex]"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_get_reports_a_single_key() {
    let out = run_shell("config get auto_base\nexit\n");
    assert!(out.stdout.contains("false"));
    assert!(!out.stdout.contains("Unknown config key"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_get_rejects_unknown_key() {
    let out = run_shell("config get not.a.real.key\nexit\n");
    assert!(out.stdout.contains("Unknown config key 'not.a.real.key'."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_set_changes_a_value_for_the_rest_of_the_session() {
    let out = run_shell("config set auto_base true\nconfig get auto_base\nexit\n");
    assert!(out.stdout.contains("auto_base = true"));
    let occurrences = out.stdout.matches("true").count();
    assert_eq!(
        occurrences,
        2,
        "expected both the confirmation and the follow-up get to report true, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_set_can_populate_an_unset_optional_field() {
    // `base_iri` is `None` by default, so it's omitted from the TOML dump
    // entirely -- setting it still has to work.
    let out = run_shell("config set base_iri http://example.org/\nconfig get base_iri\nexit\n");
    assert!(out.stdout.contains("base_iri = http://example.org/"));
    assert!(out.stdout.contains("http://example.org/"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_set_rejects_a_typo_key_without_applying_it() {
    let out = run_shell("config set shex.sho_imports true\nexit\n");
    assert!(out.stdout.contains("Unknown config key 'shex.sho_imports'."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_config_set_rejects_a_value_of_the_wrong_type() {
    let out = run_shell("config set auto_base not-a-bool\nexit\n");
    assert!(out.stderr.contains("invalid value for 'auto_base'"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_bare_reports_empty_by_default() {
    let out = run_shell("prefixes\nexit\n");
    assert!(out.stdout.contains("No default prefixes are defined."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_add_then_shows_it() {
    let out = run_shell("prefixes add rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#\nprefixes\nexit\n");
    assert!(
        out.stdout
            .contains("Added prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
    );
    assert!(
        out.stdout
            .contains("prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_rm_removes_it() {
    let out =
        run_shell("prefixes add rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#\nprefixes rm rdf\nprefixes\nexit\n");
    assert!(out.stdout.contains("Removed prefix rdf"));
    assert!(out.stdout.contains("No default prefixes are defined."));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_rm_unknown_alias_reports_error() {
    let out = run_shell("prefixes rm rdf\nexit\n");
    assert!(out.stderr.contains("Unknown prefix alias 'rdf'"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_rename_changes_the_alias_keeping_the_iri() {
    let out = run_shell(
        "prefixes add rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#\nprefixes rename rdf rdf1\nprefixes\nexit\n",
    );
    assert!(out.stdout.contains("Renamed prefix rdf to rdf1"));
    assert!(
        out.stdout
            .contains("prefix rdf1: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
    );
    // Only the "Added prefix rdf: ..." confirmation from the setup line
    // mentions `rdf` at all -- the final listing (its own line, starting
    // with "prefix ") should show only `rdf1` now.
    assert!(!out.stdout.contains("\nprefix rdf:"));
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_prefixes_copy_adds_new_alias_keeping_the_old_one() {
    let out = run_shell(
        "prefixes add rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#\nprefixes copy rdf rdf1\nprefixes\nexit\n",
    );
    assert!(out.stdout.contains("Copied prefix rdf to rdf1"));
    assert!(
        out.stdout
            .contains("prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
    );
    assert!(
        out.stdout
            .contains("prefix rdf1: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>")
    );
    assert_eq!(out.code, 0);
}

// The following tests cover the default prefixes being applied when
// loading `data`, `shex`, `shacl`, `query` and `shapemap` -- each of those
// fixtures uses a `p:` alias it never declares itself, so they only parse
// once a `prefixes add p ...` default is in scope.

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_data_without_a_default_prefix_reports_a_parse_error() {
    let data = fixture("shell_data_no_prefix.ttl");
    let out = run_shell(&format!("data {data}\nexit\n"));
    assert!(
        out.stderr.contains("has not been declared") || out.stderr.contains("not declared"),
        "expected an undeclared-prefix parse error, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_data_resolves_prefixed_names_using_default_prefixes() {
    let data = fixture("shell_data_no_prefix.ttl");
    let script = format!("prefixes add p http://example.org/\ndata {data}\ndata\nexit\n");
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("2 triple(s) loaded"),
        "expected the data to load using the default prefix, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_node_resolves_a_default_prefixed_selector() {
    let data = fixture("shell_data_no_prefix.ttl");
    let script = format!("prefixes add p http://example.org/\ndata {data}\nnode p:alice\nexit\n");
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("p:alice"),
        "expected `node` to resolve the default-prefixed selector, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_resolves_prefixed_names_using_default_prefixes() {
    let schema = fixture("shell_schema_no_prefix.shex");
    let script = format!(
        "prefixes add p http://example.org/\nprefixes add xsd http://www.w3.org/2001/XMLSchema#\nshex {schema}\nexit\n"
    );
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("1 shape(s) loaded"),
        "expected the schema to load using the default prefixes, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

// ============================================================================
// A resource argument (`shex -s`/bare, `shacl -s`, `sparql -q`, `shapemap
// -m`, ...) that looks like `alias:local` is expanded into a full URL using
// a known prefix — the session's default prefixes, or the active endpoint's
// own prefixes — before being treated as a file/URL to load. These tests
// point the expanded URL at a closed local port so resolution can be
// verified (via the resulting error message naming the *expanded* URL)
// without depending on network access or a real remote resource.
// ============================================================================

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_resolves_a_prefixed_resource_argument_using_default_prefixes() {
    // Bare-shorthand (`shex ex:foo.shex`, no explicit `-s`) is covered too,
    // since prefix expansion runs after that shorthand is already expanded.
    let script = "prefixes add ex http://localhost:1/\nshex ex:foo.shex\nexit\n";
    let out = run_shell(script);

    assert!(
        out.stderr.contains("http://localhost:1/foo.shex"),
        "expected 'ex:foo.shex' to be expanded to the full URL before being loaded, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_resolves_a_prefixed_resource_argument_using_the_active_endpoint_prefixes() {
    let path = fixture("shell_endpoint.toml");
    let script = format!("endpoint {path}\nshex -s local:foo.shex\nexit\n");
    let out = run_shell(&script);

    assert!(
        out.stderr.contains("http://localhost:1/foo.shex"),
        "expected 'local:foo.shex' to be expanded using the active endpoint's own prefixes, got stdout:\n{}\nstderr:\n{}",
        out.stdout,
        out.stderr
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shex_leaves_an_unregistered_prefixed_name_unresolved() {
    // No default prefix and no active endpoint define "nosuchalias", so this
    // is left alone: not a known prefix to expand, and (being a CURIE-like
    // token with no path separator) never treated as a candidate file path
    // either. It falls through to `InputSpec::Str`, i.e. literal schema
    // content, and fails as a ShEx syntax error instead.
    let out = run_shell("shex nosuchalias:E10\nexit\n");
    assert!(
        out.stderr.contains("nosuchalias:E10"),
        "expected the literal token to surface in the parse error, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_node_identifier_is_not_expanded_as_a_resource() {
    // `node`'s bare identifier is an RDF term reference, not a resource URL
    // — it must NOT go through prefixed-resource expansion (unlike `-s`).
    let data = fixture("shell_data.ttl");
    let script = format!("data {data}\nprefixes add \"\" http://example.org/\nnode :alice\nexit\n");
    let out = run_shell(&script);

    assert!(
        out.stdout.contains(":alice"),
        "expected the node identifier to remain ':alice', not be expanded into a URL, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_query_resolves_prefixed_names_using_default_prefixes() {
    let data = fixture("shell_data_no_prefix.ttl");
    let query = fixture("shell_query_no_prefix.rq");
    let script = format!("prefixes add p http://example.org/\ndata {data}\nquery -q {query}\nexit\n");
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("Alice"),
        "expected the query to resolve using the default prefix, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_shapemap_resolves_prefixed_names_using_default_prefixes() {
    let data = fixture("shell_data_no_prefix.ttl");
    let schema = fixture("shell_schema_no_prefix.shex");
    let shapemap = fixture("shell_shapemap_no_prefix.sm");
    let script = format!(
        "prefixes add p http://example.org/\nprefixes add xsd http://www.w3.org/2001/XMLSchema#\ndata {data}\nshex {schema}\nshapemap {shapemap}\nexit\n"
    );
    let out = run_shell(&script);
    assert!(
        out.stdout.contains("PersonShape"),
        "expected the shapemap to resolve using the default prefixes, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

// `connect`/`ddl`/`load`/`query --dialect cypher` (see tests/db.rs for their
// CLI-level coverage) are ordinary subcommands like any other, dispatched
// through the same `CliCommand` enum as the rest of the shell — so they need
// no shell-specific wiring. These tests just confirm that dispatch path
// covers them too, including the connection-details file (rather than
// session state) bridging `connect` to `load`/`query --dialect cypher`
// across shell lines the same way it bridges separate `rudof` process
// invocations.

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_ddl_derives_schema_from_data() {
    let data = fixture("shell_data.ttl");
    let script = format!("ddl {data} --dialect cypher\nexit\n");
    let out = run_shell(&script);
    assert!(
        out.stdout
            .contains("CREATE NODE TABLE Person (id STRING, name STRING, PRIMARY KEY(id));"),
        "expected ddl to derive a node table from the fixture, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn shell_connect_load_and_query_cypher_round_trip_through_connection_file() {
    let dir = tempfile::tempdir().expect("failed to create temp dir for db test");
    let db_path = dir.path().join("mydb");
    let connection_file = dir.path().join(".rudof-connection.toml");
    let data = fixture("shell_data.ttl");

    let script = format!(
        "connect {} --connection {}\n\
         load {data} --connection {} --skip-validation\n\
         query --dialect cypher -q \"MATCH (n) RETURN n\" --connection {}\n\
         exit\n",
        db_path.display(),
        connection_file.display(),
        connection_file.display(),
        connection_file.display(),
    );
    let out = run_shell(&script);

    assert!(
        out.stdout.contains("LadybugDB database opened successfully"),
        "expected connect to succeed, got:\n{}",
        out.transcript()
    );
    assert!(
        connection_file.exists(),
        "expected connect to persist connection details to a file, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains("Inserted 1 node(s)"),
        "expected load, reading the connection file connect just wrote, to insert the fixture's node, got:\n{}",
        out.transcript()
    );
    assert!(
        out.stdout.contains(r#""name":"Alice""#),
        "expected the cypher query, reading the same connection file, to return the loaded node, got:\n{}",
        out.transcript()
    );
    assert_eq!(out.code, 0);
}