shuflr-cli 0.1.1

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

#![allow(clippy::unwrap_used, clippy::expect_used)]

use assert_cmd::Command;
use predicates::prelude::*;

fn shuflr() -> Command {
    Command::cargo_bin("shuflr").expect("binary must build")
}

#[test]
fn bare_invocation_prints_help_and_exits_nonzero() {
    shuflr()
        .assert()
        .failure()
        .stderr(predicate::str::contains("Stream large JSONL"));
}

#[test]
fn help_flag_lists_all_subcommands() {
    let assert = shuflr().arg("--help").assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    for sub in ["stream", "analyze", "index", "verify", "completions"] {
        assert!(
            out.contains(sub),
            "--help output missing subcommand '{sub}':\n{out}"
        );
    }
}

#[test]
fn version_flag_emits_a_version() {
    shuflr()
        .arg("--version")
        .assert()
        .success()
        .stdout(predicate::str::contains("shuflr"));
}

#[test]
fn implicit_stream_dispatch_on_bare_path() {
    // Implicit stream + default chunk-shuffled mode. On a nonexistent path
    // the reader reports EX_NOINPUT via the typed NotFound variant.
    shuflr()
        .arg("nonexistent-file.jsonl")
        .assert()
        .code(66) // EX_NOINPUT
        .stderr(predicate::str::contains("no such file"));
}

#[test]
fn explicit_stream_with_stdin_rejects_chunk_shuffled() {
    // Default mode is chunk-shuffled. stdin isn't seekable, so the chunk
    // handler refuses with a clear EX_USAGE, nudging toward --shuffle=buffer.
    shuflr()
        .args(["stream", "-"])
        .write_stdin("one\ntwo\n")
        .assert()
        .code(64) // EX_USAGE
        .stderr(predicate::str::contains("--shuffle=buffer"));
}

#[test]
fn completions_subcommand_emits_a_script() {
    let assert = shuflr().args(["completions", "bash"]).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("_shuflr"),
        "bash completion missing function marker:\n{out}"
    );
    assert!(
        out.contains("stream"),
        "bash completion missing subcommand:\n{out}"
    );
}

#[test]
fn man_emits_roff_for_top_level() {
    let assert = shuflr().args(["man"]).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    // clap_mangen emits an .TH header and .SH sections in roff.
    assert!(
        out.contains(".TH"),
        "missing roff .TH header:\n{}",
        &out[..200.min(out.len())]
    );
    assert!(out.contains("shuflr"), "missing command name");
    assert!(
        out.contains(".SH NAME") || out.contains(r"\fBNAME\fR"),
        "missing NAME section"
    );
}

#[test]
fn man_per_subcommand() {
    let assert = shuflr().args(["man", "convert"]).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains(".TH"), "missing roff header");
    assert!(
        out.contains("zstd-seekable") || out.contains("frame"),
        "convert man page doesn't mention its core concepts:\n{out}"
    );
}

#[test]
fn man_unknown_subcommand_errors_cleanly() {
    shuflr()
        .args(["man", "nonexistent"])
        .assert()
        .code(64) // EX_USAGE
        .stderr(predicate::str::contains("no subcommand named"));
}

#[test]
fn completions_supports_zsh_and_fish() {
    for shell in ["zsh", "fish"] {
        shuflr()
            .args(["completions", shell])
            .assert()
            .success()
            .stdout(predicate::str::is_empty().not());
    }
}

#[test]
fn unknown_subcommand_fails_cleanly() {
    shuflr()
        .arg("--not-a-real-flag")
        .assert()
        .failure()
        .stderr(predicate::str::contains("error:"));
}

#[test]
fn byte_suffix_parsing_in_convert_help() {
    // Convert is feature-gated on `zstd`, enabled by default.
    let assert = shuflr().args(["convert", "--help"]).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("frame-size"),
        "convert --help missing --frame-size:\n{out}"
    );
    assert!(
        out.contains("2MiB"),
        "convert --help missing default 2MiB:\n{out}"
    );
}

#[test]
fn seed_env_var_is_documented() {
    let assert = shuflr().args(["stream", "--help"]).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("SHUFLR_SEED"),
        "stream --help missing SHUFLR_SEED env doc:\n{out}"
    );
}

#[test]
fn rank_requires_world_size() {
    shuflr()
        .args(["stream", "--rank", "0", "data.jsonl"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("world-size"));
}

#[test]
fn rank_world_size_produces_disjoint_partitions() {
    use std::collections::HashSet;

    // Run W separate shuflr invocations, each at a different rank, concat
    // their outputs, and assert it's a permutation of the input.
    let input: String = (0..400).map(|i| format!("rec_{i:03}\n")).collect();
    let w = 4u32;
    let mut union: HashSet<String> = HashSet::new();
    let mut total_out = 0usize;
    for rank in 0..w {
        let assert = shuflr()
            .args([
                "stream",
                "--shuffle",
                "none",
                "--rank",
                &rank.to_string(),
                "--world-size",
                &w.to_string(),
                "--log-level",
                "warn",
            ])
            .write_stdin(input.clone())
            .assert()
            .success();
        let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
        for line in out.lines() {
            assert!(
                union.insert(line.to_string()),
                "record {line} appeared in two ranks"
            );
            total_out += 1;
        }
    }
    assert_eq!(total_out, 400, "all records accounted for across ranks");
    let in_set: HashSet<&str> = input.lines().collect();
    let out_set: HashSet<&str> = union.iter().map(|s| s.as_str()).collect();
    assert_eq!(in_set, out_set);
}

fn tiny_corpus() -> std::path::PathBuf {
    let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
    let workspace = manifest.ancestors().nth(2).unwrap();
    workspace.join("tests/corpora/tiny.jsonl")
}

#[test]
fn stream_none_roundtrips_tiny_fixture() {
    let path = tiny_corpus();
    let assert = shuflr()
        .args(["stream", "--shuffle", "none"])
        .arg(&path)
        .assert()
        .success();
    let expected = std::fs::read(&path).unwrap();
    assert.stdout(expected);
}

#[test]
fn implicit_stream_on_plain_file_with_default_mode_points_user_to_convert() {
    // Default mode is chunk-shuffled which (in PR-6) works only on seekable
    // zstd files. A plain-JSONL file gets an EX_USAGE with a `shuflr convert`
    // suggestion — the canonical "convert once, shuffle forever" onramp.
    shuflr()
        .arg(tiny_corpus())
        .assert()
        .code(64)
        .stderr(predicate::str::contains("shuflr convert"));
}

#[test]
fn chunk_shuffled_on_seekable_zstd_works_end_to_end() {
    use std::collections::BTreeSet;

    let tmp = tempfile::tempdir().unwrap();
    let in_path = tmp.path().join("in.jsonl");
    let seekable_path = tmp.path().join("in.jsonl.zst");

    let records: Vec<String> = (0..500).map(|i| format!("{{\"i\":{i}}}\n")).collect();
    std::fs::write(&in_path, records.concat()).unwrap();

    // convert → seekable zstd
    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable_path)
        .arg(&in_path)
        .assert()
        .success();

    // chunk-shuffled stream of the seekable file
    let assert_a = shuflr()
        .args([
            "stream",
            "--shuffle",
            "chunk-shuffled",
            "--seed",
            "7",
            "--log-level",
            "warn",
        ])
        .arg(&seekable_path)
        .assert()
        .success();
    let out_a = String::from_utf8(assert_a.get_output().stdout.clone()).unwrap();

    // Same seed: byte-identical.
    let assert_b = shuflr()
        .args([
            "stream",
            "--shuffle",
            "chunk-shuffled",
            "--seed",
            "7",
            "--log-level",
            "warn",
        ])
        .arg(&seekable_path)
        .assert()
        .success();
    let out_b = String::from_utf8(assert_b.get_output().stdout.clone()).unwrap();
    assert_eq!(out_a, out_b, "same seed must give byte-identical output");

    // Different seed: different order; same multiset.
    let assert_c = shuflr()
        .args([
            "stream",
            "--shuffle",
            "chunk-shuffled",
            "--seed",
            "8",
            "--log-level",
            "warn",
        ])
        .arg(&seekable_path)
        .assert()
        .success();
    let out_c = String::from_utf8(assert_c.get_output().stdout.clone()).unwrap();
    assert_ne!(out_a, out_c);

    let in_set: BTreeSet<&str> = records.iter().map(|s| s.trim_end()).collect();
    let out_set: BTreeSet<&str> = out_a.lines().collect();
    assert_eq!(in_set, out_set, "multiset preserved under shuffle");

    // And the order actually changed vs. the original file.
    assert_ne!(out_a, records.concat());
}

#[test]
fn index_perm_on_seekable_zstd_builds_sidecar_and_is_deterministic() {
    use std::collections::BTreeSet;

    let tmp = tempfile::tempdir().unwrap();
    let in_path = tmp.path().join("in.jsonl");
    let seekable_path = tmp.path().join("in.jsonl.zst");
    let sidecar_path = tmp.path().join("in.jsonl.zst.shuflr-idx-zst");

    let records: Vec<String> = (0..400).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&in_path, records.concat()).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable_path)
        .arg(&in_path)
        .assert()
        .success();

    assert!(
        !sidecar_path.exists(),
        "sidecar must not exist before first index-perm run"
    );

    let run = |seed: &str| {
        let out = shuflr()
            .args([
                "stream",
                "--shuffle",
                "index-perm",
                "--seed",
                seed,
                "--log-level",
                "warn",
            ])
            .arg(&seekable_path)
            .assert()
            .success();
        String::from_utf8(out.get_output().stdout.clone()).unwrap()
    };

    let out_a = run("7");
    assert!(
        sidecar_path.exists(),
        "sidecar must be created on first run: {}",
        sidecar_path.display()
    );
    let sidecar_mtime_a = std::fs::metadata(&sidecar_path)
        .unwrap()
        .modified()
        .unwrap();

    // Same seed → byte-identical output on second run (sidecar-loaded path).
    let out_b = run("7");
    assert_eq!(
        out_a, out_b,
        "same seed must give byte-identical output across runs"
    );

    // Sidecar must not have been rewritten on the second run.
    let sidecar_mtime_b = std::fs::metadata(&sidecar_path)
        .unwrap()
        .modified()
        .unwrap();
    assert_eq!(
        sidecar_mtime_a, sidecar_mtime_b,
        "fresh sidecar must not be overwritten on a cache-hit run"
    );

    // Different seed → different order, same multiset.
    let out_c = run("8");
    assert_ne!(out_a, out_c);

    let in_set: BTreeSet<&str> = records.iter().map(|s| s.trim_end()).collect();
    let out_set: BTreeSet<&str> = out_a.lines().collect();
    assert_eq!(in_set, out_set, "multiset preserved under index-perm");
    assert_ne!(out_a, records.concat(), "index-perm must actually reorder");
}

#[test]
fn stream_none_honors_sample() {
    let path = tiny_corpus();
    let assert = shuflr()
        .args(["stream", "--shuffle", "none", "--sample", "2"])
        .arg(&path)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert_eq!(out.lines().count(), 2, "expected exactly 2 records:\n{out}");
}

#[test]
fn stream_none_decodes_gzip_transparently() {
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write as _;

    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("data.jsonl.gz");
    let mut enc = GzEncoder::new(
        std::fs::File::create(&path).unwrap(),
        Compression::default(),
    );
    enc.write_all(b"{\"a\":1}\n{\"a\":2}\n{\"a\":3}\n").unwrap();
    enc.finish().unwrap();

    shuflr()
        .args(["stream", "--shuffle", "none", "--log-level", "warn"])
        .arg(&path)
        .assert()
        .success()
        .stdout("{\"a\":1}\n{\"a\":2}\n{\"a\":3}\n");
}

#[test]
fn stream_none_decodes_zstd_transparently() {
    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("data.jsonl.zst");
    let bytes = b"{\"a\":1}\n{\"a\":2}\n{\"a\":3}\n";
    let compressed = zstd::stream::encode_all(&bytes[..], 3).unwrap();
    std::fs::write(&path, compressed).unwrap();

    shuflr()
        .args(["stream", "--shuffle", "none", "--log-level", "warn"])
        .arg(&path)
        .assert()
        .success()
        .stdout("{\"a\":1}\n{\"a\":2}\n{\"a\":3}\n");
}

#[test]
fn stream_none_decodes_gzip_via_stdin() {
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write as _;

    let mut enc = GzEncoder::new(Vec::new(), Compression::default());
    enc.write_all(b"one\ntwo\n").unwrap();
    let compressed = enc.finish().unwrap();

    shuflr()
        .args(["stream", "--shuffle", "none", "--log-level", "warn"])
        .write_stdin(compressed)
        .assert()
        .success()
        .stdout("one\ntwo\n");
}

#[test]
fn stream_none_stdin_works() {
    shuflr()
        .args(["stream", "--shuffle", "none"])
        .write_stdin("one\ntwo\nthree\n")
        .assert()
        .success()
        .stdout("one\ntwo\nthree\n");
}

#[test]
fn stream_none_patches_missing_trailing_newline() {
    shuflr()
        .args(["stream", "--shuffle", "none"])
        .write_stdin("a\nb")
        .assert()
        .success()
        .stdout("a\nb\n");
}

#[test]
fn stream_none_exit_65_on_fail_policy() {
    shuflr()
        .args([
            "stream",
            "--shuffle",
            "none",
            "--max-line",
            "5",
            "--on-error",
            "fail",
        ])
        .write_stdin("ok\nWAY_TOO_LONG\n")
        .assert()
        .code(65) // EX_DATAERR
        .stderr(predicate::str::contains("oversized"));
}

#[test]
fn convert_plain_jsonl_roundtrips_via_zstdcat() {
    let tmp = tempfile::tempdir().unwrap();
    let input_path = tmp.path().join("in.jsonl");
    let output_path = tmp.path().join("out.jsonl.zst");

    let content = (0..500)
        .map(|i| format!("{{\"i\":{i},\"t\":\"record number {i}\"}}\n"))
        .collect::<String>();
    std::fs::write(&input_path, &content).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output_path)
        .arg(&input_path)
        .assert()
        .success();

    // Output must be decodable by any standard zstd reader and reproduce the input.
    let compressed = std::fs::read(&output_path).unwrap();
    let decoded = zstd::stream::decode_all(&compressed[..]).unwrap();
    assert_eq!(decoded, content.as_bytes());
}

#[test]
fn info_reports_seekable_table_of_converted_file() {
    let tmp = tempfile::tempdir().unwrap();
    let input_path = tmp.path().join("in.jsonl");
    let output_path = tmp.path().join("out.jsonl.zst");

    // Larger than default 2 MiB frame size so the output has multiple frames.
    let mut content = String::new();
    for i in 0..100_000 {
        content.push_str(&format!(
            "{{\"i\":{i},\"pad\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"}}\n"
        ));
    }
    std::fs::write(&input_path, &content).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output_path)
        .arg(&input_path)
        .assert()
        .success();

    let assert = shuflr().args(["info"]).arg(&output_path).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("format:"), "info missing 'format:':\n{out}");
    assert!(
        out.contains("zstd-seekable"),
        "info missing codec name:\n{out}"
    );
    assert!(out.contains("frames:"), "info missing frames count:\n{out}");
    assert!(
        out.contains("XXH64"),
        "checksums should be on by default:\n{out}"
    );
}

#[test]
fn info_json_mode_parses_cleanly() {
    let tmp = tempfile::tempdir().unwrap();
    let input_path = tmp.path().join("in.jsonl");
    let output_path = tmp.path().join("out.jsonl.zst");
    std::fs::write(&input_path, "a\nb\nc\n").unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output_path)
        .arg(&input_path)
        .assert()
        .success();

    let assert = shuflr()
        .args(["info", "--json"])
        .arg(&output_path)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.trim().starts_with('{') && out.trim().ends_with('}'));
    assert!(out.contains("\"format\":\"zstd-seekable\""));
    assert!(out.contains("\"frames\":"));
}

#[test]
fn chunk_shuffled_parallel_emit_matches_sequential() {
    // PR-28: chunk-shuffled gains --emit-threads / --emit-prefetch.
    // Output must be byte-identical across any thread count for the
    // same seed.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("in.jsonl");
    let seekable = tmp.path().join("in.jsonl.zst");
    let records: Vec<String> = (0..800).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&plain, records.concat()).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&plain)
        .assert()
        .success();

    let run = |threads: &str, prefetch: &str| {
        let out = shuflr()
            .args([
                "stream",
                "--shuffle",
                "chunk-shuffled",
                "--seed",
                "19",
                "--emit-threads",
                threads,
                "--emit-prefetch",
                prefetch,
                "--log-level",
                "warn",
            ])
            .arg(&seekable)
            .assert()
            .success();
        String::from_utf8(out.get_output().stdout.clone()).unwrap()
    };

    let seq = run("1", "8");
    let par2 = run("2", "4");
    let par4 = run("4", "16");
    assert_eq!(seq, par2);
    assert_eq!(seq, par4);
}

#[test]
fn index_perm_zstd_parallel_emit_matches_sequential() {
    // PR-27: --emit-threads parallelizes the emit phase. Output must
    // remain byte-identical across any thread count for the same seed.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("in.jsonl");
    let seekable = tmp.path().join("in.jsonl.zst");
    let records: Vec<String> = (0..800).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&plain, records.concat()).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&plain)
        .assert()
        .success();

    let run = |emit_threads: &str, prefetch: &str| {
        let out = shuflr()
            .args([
                "stream",
                "--shuffle",
                "index-perm",
                "--seed",
                "42",
                "--emit-threads",
                emit_threads,
                "--emit-prefetch",
                prefetch,
                "--log-level",
                "warn",
            ])
            .arg(&seekable)
            .assert()
            .success();
        String::from_utf8(out.get_output().stdout.clone()).unwrap()
    };

    let seq = run("1", "32");
    let par2 = run("2", "4");
    let par8 = run("4", "64");
    assert_eq!(seq, par2, "2-thread emit must match sequential");
    assert_eq!(
        seq, par8,
        "4-thread + larger prefetch must match sequential"
    );
}

#[test]
fn index_perm_zstd_parallel_build_matches_sequential() {
    // Same seed, same input, different --build-threads => byte-identical
    // output. Pins the PR-26 invariant that parallelism is a pure
    // performance optimization and never changes semantics.
    use std::collections::BTreeSet;

    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("in.jsonl");
    let seekable = tmp.path().join("in.jsonl.zst");
    let records: Vec<String> = (0..600).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&plain, records.concat()).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&plain)
        .assert()
        .success();

    let run = |threads: &str| {
        std::fs::remove_file(tmp.path().join("in.jsonl.zst.shuflr-idx-zst")).ok();
        let out = shuflr()
            .args([
                "stream",
                "--shuffle",
                "index-perm",
                "--seed",
                "42",
                "--build-threads",
                threads,
                "--log-level",
                "warn",
            ])
            .arg(&seekable)
            .assert()
            .success();
        String::from_utf8(out.get_output().stdout.clone()).unwrap()
    };

    let seq = run("1");
    let par = run("4");
    assert_eq!(
        seq, par,
        "parallel build must produce byte-identical output to sequential"
    );

    // And both cover the full multiset.
    let expected: BTreeSet<&str> = records.iter().map(|s| s.trim_end()).collect();
    let got: BTreeSet<&str> = par.lines().collect();
    assert_eq!(got, expected);
}

#[test]
fn info_json_escapes_quotes_and_backslashes_in_path() {
    // Unix-only test: put a " and a \ in the filename and verify that
    // --json output is still syntactically valid JSON (no stray quote,
    // no stray backslash in the output key string).
    #[cfg(unix)]
    {
        let tmp = tempfile::tempdir().unwrap();
        let bad_name = r#"weird" \name.jsonl"#;
        let input = tmp.path().join(bad_name);
        let seekable = tmp.path().join(format!("{bad_name}.zst"));
        std::fs::write(&input, b"a\nb\n").unwrap();

        shuflr()
            .args(["convert", "--log-level", "warn", "-o"])
            .arg(&seekable)
            .arg(&input)
            .assert()
            .success();

        let assert = shuflr()
            .args(["info", "--json"])
            .arg(&seekable)
            .assert()
            .success();
        let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();

        // Trailing newline from writeln!; strip for the parse.
        let trimmed = out.trim();

        // Hand-rolled JSON; do a minimal well-formed check that the
        // escape sequences landed:
        //   - there's exactly one closing `"` for the file value
        //   - the raw " in the filename is escaped to \"
        //   - the raw \ is escaped to \\
        assert!(
            trimmed.contains(r#"\""#),
            "expected escaped quote in output:\n{out}"
        );
        assert!(
            trimmed.contains(r"\\"),
            "expected escaped backslash in output:\n{out}"
        );
    }
}

#[test]
fn analyze_strict_does_not_change_report() {
    // --strict must only affect exit code, never which frames get
    // sampled. Regression pin for the old bug where strict-as-u64 fed
    // the RNG.
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("a.jsonl");
    let seekable = tmp.path().join("a.jsonl.zst");
    let body: String = (0..400).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&input, body).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&input)
        .assert()
        .success();

    let run = |strict: bool| {
        let mut cmd = shuflr();
        cmd.args(["analyze", "--json", "--sample-chunks", "8"]);
        if strict {
            cmd.arg("--strict");
        }
        let assert = cmd.arg(&seekable).assert();
        String::from_utf8(assert.get_output().stdout.clone()).unwrap()
    };

    assert_eq!(
        run(false),
        run(true),
        "--strict must not change the JSON report"
    );
}

#[test]
fn analyze_json_mode_emits_parseable_report() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let seekable = tmp.path().join("in.jsonl.zst");
    // A small but non-trivial corpus so analyze has something to sample.
    let body: String = (0..200).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&input, body).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&input)
        .assert()
        .success();

    let assert = shuflr()
        .args(["analyze", "--json"])
        .arg(&seekable)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    let trimmed = out.trim();
    assert!(
        trimmed.starts_with('{') && trimmed.ends_with('}'),
        "not a JSON object:\n{out}"
    );
    // Every field the report exposes should be present so scripts can
    // pin to specific keys without fear of silent rename.
    for key in [
        "\"file\":",
        "\"total_frames\":",
        "\"sampled_frames\":",
        "\"total_records_sampled\":",
        "\"mean_record_len_bytes\":",
        "\"byte_kl_max\":",
        "\"byte_kl_mean\":",
        "\"byte_js_max\":",
        "\"byte_js_mean\":",
        "\"frame_entropy_mean\":",
        "\"reclen_cv\":",
        "\"thresholds\":{",
        "\"byte_kl_unsafe\":",
        "\"byte_js_unsafe\":",
        "\"reclen_cv_unsafe\":",
        "\"verdict\":",
    ] {
        assert!(out.contains(key), "missing JSON key {key}:\n{out}");
    }
    // Verdict must be one of the two documented values.
    assert!(
        out.contains("\"verdict\":\"safe\"") || out.contains("\"verdict\":\"unsafe\""),
        "verdict not a documented value:\n{out}"
    );
    // Human output must not leak into JSON mode.
    assert!(
        !out.contains("recommendation:"),
        "human text leaked into JSON output:\n{out}"
    );
}

#[test]
fn convert_respects_input_format_override() {
    // Take a plain JSONL file whose first bytes happen to be magic for
    // *something other than plain*, then force --input-format=plain.
    // Without the override, auto-detect would try to wrap it in a
    // decoder and produce garbage or an error.
    //
    // We don't have a plausible non-plain false-positive sitting around,
    // so instead we give convert a plain file and force
    // --input-format=zstd: it should reject because the file isn't
    // actually zstd, proving the flag takes effect.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("pretend.jsonl");
    std::fs::write(&plain, b"{\"a\":1}\n{\"a\":2}\n").unwrap();

    let out = tmp.path().join("out.jsonl.zst");
    shuflr()
        .args([
            "convert",
            "--log-level",
            "warn",
            "--input-format",
            "zstd",
            "-o",
        ])
        .arg(&out)
        .arg(&plain)
        .assert()
        .failure();

    // And the no-op case (auto) still works end-to-end.
    let out2 = tmp.path().join("out-auto.jsonl.zst");
    shuflr()
        .args([
            "convert",
            "--log-level",
            "warn",
            "--input-format",
            "auto",
            "-o",
        ])
        .arg(&out2)
        .arg(&plain)
        .assert()
        .success();
}

#[test]
fn convert_rejects_multi_input_in_pr4() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.jsonl");
    let b = tmp.path().join("b.jsonl");
    let o = tmp.path().join("o.zst");
    std::fs::write(&a, "x\n").unwrap();
    std::fs::write(&b, "y\n").unwrap();

    shuflr()
        .args(["convert", "-o"])
        .arg(&o)
        .arg(&a)
        .arg(&b)
        .assert()
        .failure()
        .stderr(predicate::str::contains("PR-4"));
}

#[test]
fn stream_buffer_preserves_record_multiset_and_is_deterministic() {
    let input_content: String = (0..200).map(|i| format!("record_{i:03}\n")).collect();

    let run_once = |seed: &str| {
        let assert = shuflr()
            .args([
                "stream",
                "--shuffle",
                "buffer",
                "--buffer-size",
                "32",
                "--seed",
                seed,
                "--log-level",
                "warn",
            ])
            .write_stdin(input_content.clone())
            .assert()
            .success();
        String::from_utf8(assert.get_output().stdout.clone()).unwrap()
    };

    let out_a = run_once("42");
    let out_b = run_once("42");
    assert_eq!(out_a, out_b, "same seed must produce byte-identical output");

    let out_c = run_once("43");
    assert_ne!(
        out_a, out_c,
        "different seeds must produce different orderings"
    );

    // Multisets must match the input.
    let mut input_lines: Vec<&str> = input_content.lines().collect();
    let mut out_lines: Vec<&str> = out_a.lines().collect();
    input_lines.sort_unstable();
    out_lines.sort_unstable();
    assert_eq!(input_lines, out_lines);
    // And some actual reordering happened — not identity.
    assert_ne!(out_a, input_content);
}

#[test]
fn stream_buffer_on_gzip_input_works() {
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write as _;

    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("data.jsonl.gz");
    let records: String = (0..50).map(|i| format!("rec_{i:02}\n")).collect();
    let mut enc = GzEncoder::new(
        std::fs::File::create(&path).unwrap(),
        Compression::default(),
    );
    enc.write_all(records.as_bytes()).unwrap();
    enc.finish().unwrap();

    let assert = shuflr()
        .args([
            "stream",
            "--shuffle",
            "buffer",
            "--buffer-size",
            "16",
            "--seed",
            "7",
            "--log-level",
            "warn",
        ])
        .arg(&path)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    let mut in_sorted: Vec<&str> = records.lines().collect();
    let mut out_sorted: Vec<&str> = out.lines().collect();
    in_sorted.sort_unstable();
    out_sorted.sort_unstable();
    assert_eq!(in_sorted, out_sorted);
}

#[test]
fn convert_with_verify_passes_on_clean_output() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let output = tmp.path().join("out.jsonl.zst");
    let content: String = (0..300)
        .map(|i| format!("{{\"i\":{i},\"pad\":\"xxxxxxxxxxxx\"}}\n"))
        .collect();
    std::fs::write(&input, &content).unwrap();

    shuflr()
        .args(["convert", "--verify", "--log-level", "warn", "-o"])
        .arg(&output)
        .arg(&input)
        .assert()
        .success();
}

#[test]
fn convert_with_verify_fails_when_output_is_corrupted() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let output = tmp.path().join("out.jsonl.zst");
    std::fs::write(&input, "a\nb\nc\nd\ne\n").unwrap();

    // First write without verify, then corrupt, then verify via a second run.
    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output)
        .arg(&input)
        .assert()
        .success();

    // Corrupt the trailing seek-table magic.
    let mut bytes = std::fs::read(&output).unwrap();
    let len = bytes.len();
    bytes[len - 3] ^= 0xff;
    std::fs::write(&output, &bytes).unwrap();

    // Re-running convert with --verify would overwrite; instead use `info`
    // which also reads the seek table, so we assert rejection via info.
    shuflr()
        .args(["info"])
        .arg(&output)
        .assert()
        .failure()
        .stderr(predicate::str::contains("not a zstd-seekable"));
}

#[test]
fn index_subcommand_builds_sidecar() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("data.jsonl");
    std::fs::write(&input, "a\nb\nc\nd\n").unwrap();

    shuflr().args(["index"]).arg(&input).assert().success();

    let sidecar = tmp.path().join("data.jsonl.shuflr-idx");
    assert!(sidecar.exists(), "sidecar must be written");
    let bytes = std::fs::read(&sidecar).unwrap();
    // 8 magic + 1 version + 7 reserved + 32 fingerprint + 8 count + 8*(N+1) offsets
    // For N=4: 56 + 8*5 = 96
    assert_eq!(
        bytes.len(),
        96,
        "unexpected sidecar layout: {} bytes",
        bytes.len()
    );
    assert_eq!(&bytes[..8], b"SHUFLIDX");
}

#[test]
fn index_subcommand_builds_seekable_zstd_sidecar() {
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("data.jsonl");
    let seekable = tmp.path().join("data.jsonl.zst");
    let records: Vec<String> = (0..50).map(|i| format!("{{\"i\":{i:03}}}\n")).collect();
    std::fs::write(&plain, records.concat()).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&plain)
        .assert()
        .success();

    // `shuflr index` on a seekable-zstd file should dispatch to the
    // record-index builder and drop a .shuflr-idx-zst sidecar.
    shuflr().args(["index"]).arg(&seekable).assert().success();

    let sidecar = tmp.path().join("data.jsonl.zst.shuflr-idx-zst");
    assert!(
        sidecar.exists(),
        "seekable-zstd sidecar must exist: {}",
        sidecar.display()
    );
    let bytes = std::fs::read(&sidecar).unwrap();
    // 8 magic + 1 version + 7 reserved + 32 fingerprint + 8 count + 12·N entries
    // For N=50: 56 + 600 = 656.
    assert_eq!(
        bytes.len(),
        656,
        "unexpected seekable-zstd sidecar size: {} bytes (expected 656)",
        bytes.len(),
    );
    assert_eq!(&bytes[..8], b"SHUFLRZI");
}

#[test]
fn stream_index_perm_builds_index_on_demand() {
    use std::collections::BTreeSet;

    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("c.jsonl");
    let records: Vec<String> = (0..200).map(|i| format!("{{\"i\":{i}}}\n")).collect();
    std::fs::write(&input, records.concat()).unwrap();

    // First run with no sidecar: shuflr builds, saves, then emits.
    let assert_a = shuflr()
        .args([
            "stream",
            "--shuffle",
            "index-perm",
            "--seed",
            "17",
            "--log-level",
            "warn",
        ])
        .arg(&input)
        .assert()
        .success();
    let out_a = String::from_utf8(assert_a.get_output().stdout.clone()).unwrap();
    let sidecar = tmp.path().join("c.jsonl.shuflr-idx");
    assert!(sidecar.exists());

    // Second run should hit the sidecar path and produce byte-identical output.
    let assert_b = shuflr()
        .args([
            "stream",
            "--shuffle",
            "index-perm",
            "--seed",
            "17",
            "--log-level",
            "warn",
        ])
        .arg(&input)
        .assert()
        .success();
    let out_b = String::from_utf8(assert_b.get_output().stdout.clone()).unwrap();
    assert_eq!(out_a, out_b, "same seed must give byte-identical output");

    // Different seed = different order (same multiset).
    let assert_c = shuflr()
        .args([
            "stream",
            "--shuffle",
            "index-perm",
            "--seed",
            "18",
            "--log-level",
            "warn",
        ])
        .arg(&input)
        .assert()
        .success();
    let out_c = String::from_utf8(assert_c.get_output().stdout.clone()).unwrap();
    assert_ne!(out_a, out_c);

    let in_set: BTreeSet<&str> = records.iter().map(|s| s.trim_end()).collect();
    let out_set: BTreeSet<&str> = out_a.lines().collect();
    assert_eq!(in_set, out_set, "multiset preserved");
    // And the order actually changed.
    let original: String = records.concat();
    assert_ne!(out_a, original);
}

#[test]
fn stream_index_perm_rejects_compressed_input_with_hint() {
    use flate2::Compression;
    use flate2::write::GzEncoder;
    use std::io::Write as _;

    let tmp = tempfile::tempdir().unwrap();
    let path = tmp.path().join("data.jsonl.gz");
    let mut enc = GzEncoder::new(
        std::fs::File::create(&path).unwrap(),
        Compression::default(),
    );
    enc.write_all(b"a\nb\nc\n").unwrap();
    enc.finish().unwrap();

    shuflr()
        .args(["stream", "--shuffle", "index-perm"])
        .arg(&path)
        .assert()
        .code(64) // EX_USAGE
        .stderr(predicate::str::contains("shuflr convert"));
}

#[test]
fn stream_index_perm_rebuilds_when_fingerprint_mismatches() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("c.jsonl");
    std::fs::write(&input, "one\ntwo\nthree\n").unwrap();

    // Build once.
    shuflr().args(["index"]).arg(&input).assert().success();
    let sidecar = tmp.path().join("c.jsonl.shuflr-idx");
    assert!(sidecar.exists());

    // Mutate the input: now fingerprint (size/mtime) won't match.
    std::thread::sleep(std::time::Duration::from_millis(1100)); // mtime tick
    std::fs::write(&input, "one\ntwo\nthree\nFOUR\nFIVE\n").unwrap();

    // stream --shuffle=index-perm should detect, rebuild, and still work.
    let assert = shuflr()
        .args(["stream", "--shuffle", "index-perm", "--seed", "1"])
        .arg(&input)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert_eq!(
        out.lines().count(),
        5,
        "should now see all 5 records:\n{out}"
    );
}

#[test]
fn verify_plain_jsonl_reports_ok() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("clean.jsonl");
    std::fs::write(&input, "a\nb\nc\nd\n").unwrap();
    let assert = shuflr().args(["verify"]).arg(&input).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("verdict:       OK"),
        "missing OK verdict:\n{out}"
    );
    assert!(
        out.contains("records:       4"),
        "missing record count:\n{out}"
    );
}

#[test]
fn verify_deep_plain_catches_invalid_json() {
    // Without --deep, the plain path happily accepts "not json".
    // With --deep, it must flip to FAILED.
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("bad.jsonl");
    std::fs::write(
        &input,
        r#"{"ok":1}
not json here
{"ok":2}
"#,
    )
    .unwrap();

    shuflr().args(["verify"]).arg(&input).assert().success();

    let assert = shuflr()
        .args(["verify", "--deep"])
        .arg(&input)
        .assert()
        .code(65); // EX_DATAERR
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("deep-json:"), "missing deep-json line:\n{out}");
    assert!(
        out.contains("verdict:       ISSUES"),
        "expected ISSUES verdict:\n{out}"
    );
}

#[test]
fn verify_deep_plain_passes_on_valid_jsonl() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("good.jsonl");
    std::fs::write(
        &input,
        r#"{"i":0}
{"nested":{"a":[1,2,3],"b":null}}
42
true
"yep"
"#,
    )
    .unwrap();

    let assert = shuflr()
        .args(["verify", "--deep"])
        .arg(&input)
        .assert()
        .success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("deep-json:     0 invalid"), "{out}");
    assert!(out.contains("verdict:       OK"), "{out}");
}

#[test]
fn verify_deep_seekable_catches_invalid_json() {
    // Build a seekable zstd whose records are a mix of valid + invalid JSON.
    // Convert doesn't enforce JSON syntax, so this round-trips without error;
    // it's verify --deep's job to catch it.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("mix.jsonl");
    let seekable = tmp.path().join("mix.jsonl.zst");
    std::fs::write(
        &plain,
        r#"{"ok":1}
<<<not json>>>
{"ok":2}
"#,
    )
    .unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&seekable)
        .arg(&plain)
        .assert()
        .success();

    // Without --deep, framing is fine → OK.
    let assert = shuflr().args(["verify"]).arg(&seekable).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("verdict:       OK"), "{out}");

    // With --deep, JSON parse fails → FAILED.
    let assert = shuflr()
        .args(["verify", "--deep"])
        .arg(&seekable)
        .assert()
        .code(65);
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("deep-json:"), "{out}");
    assert!(
        out.contains("verdict:       FAILED"),
        "expected FAILED verdict:\n{out}"
    );
}

#[test]
fn verify_seekable_ok_after_convert() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let output = tmp.path().join("out.jsonl.zst");
    let body: String = (0..300).map(|i| format!("{{\"i\":{i}}}\n")).collect();
    std::fs::write(&input, &body).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output)
        .arg(&input)
        .assert()
        .success();

    let assert = shuflr().args(["verify"]).arg(&output).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("format:        zstd-seekable"), "\n{out}");
    assert!(out.contains("verdict:       OK"), "\n{out}");
}

#[test]
fn verify_seekable_falls_back_to_streaming_when_trailer_broken() {
    // Trailer corruption only breaks seekability; the zstd frames still
    // decode cleanly as a streaming blob. Verify should say so — and
    // still pass, because the data is intact.
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let output = tmp.path().join("out.jsonl.zst");
    std::fs::write(&input, "a\nb\nc\nd\ne\n").unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output)
        .arg(&input)
        .assert()
        .success();

    // Stomp the seek-table trailer magic.
    let mut bytes = std::fs::read(&output).unwrap();
    let n = bytes.len();
    bytes[n - 3] ^= 0xff;
    std::fs::write(&output, &bytes).unwrap();

    let assert = shuflr().args(["verify"]).arg(&output).assert().success();
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("zstd (streaming)"),
        "expected streaming fallback when trailer is broken:\n{out}"
    );
}

#[test]
fn verify_seekable_fails_when_frame_body_corrupted() {
    // Stomping bytes deep inside a zstd frame makes the decoder error;
    // verify should exit 65 and say FAILED.
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("in.jsonl");
    let output = tmp.path().join("out.jsonl.zst");
    // Enough records to force more than one frame so the body corruption
    // is on a frame that the seek-table parse reaches.
    let body: String = (0..3000)
        .map(|i| format!("padding_padding_padding_padding_padding_{i:05}\n"))
        .collect();
    std::fs::write(&input, &body).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output)
        .arg(&input)
        .assert()
        .success();

    // Stomp 64 bytes deep inside frame 0's compressed body.
    let mut bytes = std::fs::read(&output).unwrap();
    for b in bytes.iter_mut().take(80).skip(16) {
        *b = 0x55;
    }
    std::fs::write(&output, &bytes).unwrap();

    let assert = shuflr().args(["verify"]).arg(&output).assert().code(65); // EX_DATAERR
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(
        out.contains("verdict:       FAILED"),
        "expected FAILED verdict:\n{out}"
    );
}

#[test]
fn verify_flags_trailing_partial_record() {
    let tmp = tempfile::tempdir().unwrap();
    let input = tmp.path().join("partial.jsonl");
    // Missing trailing newline on the last record.
    std::fs::write(&input, "one\ntwo\nthree").unwrap();
    let assert = shuflr()
        .args(["verify"])
        .arg(&input)
        .assert()
        .code(65) // EX_DATAERR
        ;
    let out = String::from_utf8(assert.get_output().stdout.clone()).unwrap();
    assert!(out.contains("trailing-partial: true"), "\n{out}");
    assert!(out.contains("verdict:       ISSUES"), "\n{out}");
}

#[test]
fn convert_preserves_crlf_and_nul() {
    let tmp = tempfile::tempdir().unwrap();
    let input_path = tmp.path().join("in.jsonl");
    let output_path = tmp.path().join("out.jsonl.zst");
    // Nasty bytes: CRLF, embedded NUL, multi-byte UTF-8.
    let original: &[u8] = b"one\r\nt\0wo\n\xe2\x98\x83snowman\n";
    std::fs::write(&input_path, original).unwrap();

    shuflr()
        .args(["convert", "--log-level", "warn", "-o"])
        .arg(&output_path)
        .arg(&input_path)
        .assert()
        .success();

    let compressed = std::fs::read(&output_path).unwrap();
    let decoded = zstd::stream::decode_all(&compressed[..]).unwrap();
    assert_eq!(decoded, original);
}

#[test]
fn stream_none_exit_66_on_missing_input() {
    shuflr()
        .args(["stream", "--shuffle", "none", "/does/not/exist.jsonl"])
        .assert()
        .code(66) // EX_NOINPUT
        .stderr(predicate::str::contains("no such file"));
}