spdfdiff_cli 0.1.1

Command-line interface for semantic PDF diffing.
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
use serde_json::Value;
use std::{
    fs,
    path::{Path, PathBuf},
    process::{Command, Output},
    sync::atomic::{AtomicUsize, Ordering},
};

static NEXT_TEST_DIR: AtomicUsize = AtomicUsize::new(0);

#[derive(Debug, Clone, Copy)]
struct ExpectedDiffSummary {
    inserted: u64,
    deleted: u64,
    changes: usize,
}

#[derive(Debug, Clone, Copy)]
struct RealSamplePair {
    slug: &'static str,
    old_name: &'static str,
    new_name: &'static str,
    expected: Option<ExpectedDiffSummary>,
}

const REAL_SAMPLE_PDFS: &[&str] = &[
    "annotations_base_v1.pdf",
    "annotations_visual_markup_v2.pdf",
    "attachment_link_bundle_v1.pdf",
    "attachment_link_bundle_v2.pdf",
    "complex_semantic_diff_v1.pdf",
    "complex_semantic_diff_v2.pdf",
    "document_outline_v1.pdf",
    "document_outline_v2.pdf",
    "document_v1.pdf",
    "document_v2.pdf",
    "headers_footers_v1.pdf",
    "headers_footers_v2.pdf",
    "inline_formatting_v1.pdf",
    "inline_formatting_v2.pdf",
    "interactive_forms_v1.pdf",
    "interactive_forms_v2.pdf",
    "interactive_links_v1.pdf",
    "interactive_links_v2.pdf",
    "layered_redaction_v1.pdf",
    "layered_redaction_v2.pdf",
    "multicolumn_layout_v1.pdf",
    "multicolumn_layout_v2.pdf",
    "multipage_table_v1.pdf",
    "multipage_table_v2.pdf",
    "report_with_images_v1.pdf",
    "report_with_images_v2.pdf",
    "scanned_document_v1.pdf",
    "scanned_document_v2.pdf",
    "semantic_contract_v1.pdf",
    "semantic_contract_v2.pdf",
    "semantic_images_v1.pdf",
    "semantic_images_v2.pdf",
    "tagged_table_reflow_v1.pdf",
    "tagged_table_reflow_v2.pdf",
    "ultimate_semantic_diff_v1.pdf",
    "ultimate_semantic_diff_v2.pdf",
    "vector_paths_graphic_v1.pdf",
    "vector_paths_graphic_v2.pdf",
    "watermark_overlay_v1.pdf",
    "watermark_overlay_v2.pdf",
];

const REAL_SAMPLE_PAIRS: &[RealSamplePair] = &[
    RealSamplePair {
        slug: "document",
        old_name: "document_v1.pdf",
        new_name: "document_v2.pdf",
        expected: Some(ExpectedDiffSummary {
            inserted: 0,
            deleted: 0,
            changes: 1,
        }),
    },
    RealSamplePair {
        slug: "report-with-images",
        old_name: "report_with_images_v1.pdf",
        new_name: "report_with_images_v2.pdf",
        expected: Some(ExpectedDiffSummary {
            inserted: 2,
            deleted: 2,
            changes: 5,
        }),
    },
    RealSamplePair {
        slug: "semantic-contract",
        old_name: "semantic_contract_v1.pdf",
        new_name: "semantic_contract_v2.pdf",
        expected: Some(ExpectedDiffSummary {
            inserted: 1,
            deleted: 1,
            changes: 2,
        }),
    },
    RealSamplePair {
        slug: "semantic-images",
        old_name: "semantic_images_v1.pdf",
        new_name: "semantic_images_v2.pdf",
        expected: Some(ExpectedDiffSummary {
            inserted: 1,
            deleted: 2,
            changes: 8,
        }),
    },
    RealSamplePair {
        slug: "complex-semantic-diff",
        old_name: "complex_semantic_diff_v1.pdf",
        new_name: "complex_semantic_diff_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "ultimate-semantic-diff",
        old_name: "ultimate_semantic_diff_v1.pdf",
        new_name: "ultimate_semantic_diff_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "interactive-links",
        old_name: "interactive_links_v1.pdf",
        new_name: "interactive_links_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "multicolumn-layout",
        old_name: "multicolumn_layout_v1.pdf",
        new_name: "multicolumn_layout_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "headers-footers",
        old_name: "headers_footers_v1.pdf",
        new_name: "headers_footers_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "inline-formatting",
        old_name: "inline_formatting_v1.pdf",
        new_name: "inline_formatting_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "watermark-overlay",
        old_name: "watermark_overlay_v1.pdf",
        new_name: "watermark_overlay_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "multipage-table",
        old_name: "multipage_table_v1.pdf",
        new_name: "multipage_table_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "interactive-forms",
        old_name: "interactive_forms_v1.pdf",
        new_name: "interactive_forms_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "document-outline",
        old_name: "document_outline_v1.pdf",
        new_name: "document_outline_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "annotations",
        old_name: "annotations_base_v1.pdf",
        new_name: "annotations_visual_markup_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "attachment-link-bundle",
        old_name: "attachment_link_bundle_v1.pdf",
        new_name: "attachment_link_bundle_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "layered-redaction",
        old_name: "layered_redaction_v1.pdf",
        new_name: "layered_redaction_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "tagged-table-reflow",
        old_name: "tagged_table_reflow_v1.pdf",
        new_name: "tagged_table_reflow_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "vector-paths-graphic",
        old_name: "vector_paths_graphic_v1.pdf",
        new_name: "vector_paths_graphic_v2.pdf",
        expected: None,
    },
    RealSamplePair {
        slug: "scanned-document",
        old_name: "scanned_document_v1.pdf",
        new_name: "scanned_document_v2.pdf",
        expected: None,
    },
];

const SAMPLE_SCENARIO_MARKDOWN: &[&str] = &["semantic_diff_test_cases.md"];

#[test]
fn diff_command_reports_text_changes_in_stdout_and_output_file() {
    let fixture = TestFixture::new("diff_command_reports_text_changes");
    let old_pdf = fixture.write_pdf("old.pdf", "Annual revenue was 10 million.");
    let new_pdf = fixture.write_pdf("new.pdf", "Annual revenue was 12 million.");

    let json_output = run_spdfdiff([
        "diff",
        path_arg(&old_pdf).as_str(),
        path_arg(&new_pdf).as_str(),
    ]);
    assert_success(&json_output);
    let json: Value =
        serde_json::from_slice(&json_output.stdout).expect("diff stdout should be valid JSON");

    assert_eq!(json["schema_version"], "0.1.0");
    assert_eq!(json["summary"]["modified"], 1);
    assert_eq!(json["changes"][0]["id"], "change-0000");
    assert_eq!(json["changes"][0]["kind"], "Modified");
    assert_eq!(
        json["changes"][0]["old_node"]["text"],
        "Annual revenue was 10 million."
    );
    assert_eq!(
        json["changes"][0]["new_node"]["text"],
        "Annual revenue was 12 million."
    );
    assert_eq!(json["changes"][0]["text_hunks"][1]["kind"], "Replaced");
    assert_eq!(json["changes"][0]["text_hunks"][1]["old_text"], "10");
    assert_eq!(json["changes"][0]["text_hunks"][1]["new_text"], "12");

    let markdown_path = fixture.path("diff.md");
    let markdown_output = run_spdfdiff([
        "diff",
        path_arg(&old_pdf).as_str(),
        path_arg(&new_pdf).as_str(),
        "--format",
        "md",
        "--output",
        path_arg(&markdown_path).as_str(),
    ]);
    assert_success(&markdown_output);
    assert!(
        markdown_output.stdout.is_empty(),
        "--output should not duplicate the report on stdout"
    );

    let markdown = fs::read_to_string(markdown_path).expect("Markdown report should be written");
    assert!(markdown.contains("# Semantic PDF Diff"));
    assert!(markdown.contains("| Modified | 1 |"));
    assert!(markdown.contains("`change-0000` Modified"));
}

#[test]
fn diff_fail_on_changes_exits_one_only_when_changes_exist() {
    let fixture = TestFixture::new("diff_fail_on_changes");
    let old_pdf = fixture.write_pdf("old.pdf", "Annual revenue was 10 million.");
    let new_pdf = fixture.write_pdf("new.pdf", "Annual revenue was 12 million.");

    let changed = run_spdfdiff([
        "diff",
        path_arg(&old_pdf).as_str(),
        path_arg(&new_pdf).as_str(),
        "--fail-on-changes",
    ]);
    assert_eq!(changed.status.code(), Some(1));
    assert!(changed.stderr.is_empty());

    let unchanged = run_spdfdiff([
        "diff",
        path_arg(&old_pdf).as_str(),
        path_arg(&old_pdf).as_str(),
        "--fail-on-changes",
    ]);
    assert_success(&unchanged);
}

#[test]
fn inspect_command_reports_object_graph_for_supported_formats() {
    let fixture = TestFixture::new("inspect_command_reports_object_graph");
    let pdf = fixture.write_pdf("inspect.pdf", "Inspection target");

    let json_output = run_spdfdiff(["inspect", path_arg(&pdf).as_str()]);
    assert_success(&json_output);
    let json: Value =
        serde_json::from_slice(&json_output.stdout).expect("inspect stdout should be valid JSON");

    assert!(json["object_count"].as_u64().unwrap_or_default() >= 4);
    assert_eq!(json["first_page_streams"], 1);

    let html_output = run_spdfdiff(["inspect", path_arg(&pdf).as_str(), "--format", "html"]);
    assert_success(&html_output);
    let html = String::from_utf8(html_output.stdout).expect("inspect HTML should be UTF-8");

    assert!(html.starts_with("<!doctype html>"));
    assert!(html.contains("# PDF Inspect"));
    assert!(html.contains("- First-page streams: 1"));
}

#[test]
fn extract_command_reports_positioned_text_and_writes_json() {
    let fixture = TestFixture::new("extract_command_reports_positioned_text");
    let pdf = fixture.write_pdf("extract.pdf", "First paragraph.");

    let markdown_output = run_spdfdiff(["extract", path_arg(&pdf).as_str(), "--format", "md"]);
    assert_success(&markdown_output);
    let markdown =
        String::from_utf8(markdown_output.stdout).expect("extract Markdown should be UTF-8");

    assert!(markdown.contains("# Extracted Text"));
    assert!(markdown.contains("- First paragraph."));

    let json_path = fixture.path("extract.json");
    let json_output = run_spdfdiff([
        "extract",
        path_arg(&pdf).as_str(),
        "--output",
        path_arg(&json_path).as_str(),
    ]);
    assert_success(&json_output);
    assert!(
        json_output.stdout.is_empty(),
        "--output should not duplicate the report on stdout"
    );

    let json: Value = serde_json::from_str(
        &fs::read_to_string(json_path).expect("extract JSON report should be written"),
    )
    .expect("extract output file should be valid JSON");
    assert_eq!(json["paragraphs"], 1);
    assert!(
        json["diagnostic_count"].as_u64().unwrap_or_default() >= 1,
        "literal-string fallback extraction should remain visibly diagnostic"
    );
}

#[test]
fn corpus_command_sorts_files_and_summarizes_partial_and_failed_inputs() {
    let fixture = TestFixture::new("corpus_command_sorts_files");
    let corpus = fixture.path("corpus");
    fs::create_dir_all(&corpus).expect("corpus directory should be created");
    fs::write(
        corpus.join("b.pdf"),
        minimal_pdf("Corpus parseable document."),
    )
    .expect("valid PDF should be written");
    fs::write(corpus.join("a.pdf"), b"this is not a pdf").expect("invalid PDF should be written");
    fs::write(corpus.join("ignored.txt"), b"not part of the corpus")
        .expect("ignored file should be written");

    let report_path = fixture.path("corpus_report.json");
    let output = run_spdfdiff([
        "corpus",
        path_arg(&corpus).as_str(),
        "--output",
        path_arg(&report_path).as_str(),
    ]);
    assert_success(&output);
    assert!(
        output.stdout.is_empty(),
        "corpus writes only to the requested output file"
    );

    let report: Value = serde_json::from_str(
        &fs::read_to_string(report_path).expect("corpus report should be written"),
    )
    .expect("corpus report should be valid JSON");

    assert_eq!(report["folder"], "corpus");
    assert_eq!(report["total"], 2);
    assert_eq!(report["parsed"], 1);
    assert_eq!(report["partial"], 1);
    assert_eq!(report["failed"], 1);
    assert_eq!(report["files"][0]["file"], "a.pdf");
    assert_eq!(report["files"][0]["status"], "failed");
    assert_eq!(report["files"][1]["file"], "b.pdf");
    assert_eq!(report["files"][1]["status"], "partial");
    assert_eq!(report["diagnostic_counts"]["MISSING_TOUNICODE"], 1);
}

#[test]
fn benchmark_command_reports_m8_t5_phase_metrics() {
    let fixture = TestFixture::new("benchmark_command_phase_metrics");
    let output_path = fixture.path("benchmark.json");

    let output = run_spdfdiff([
        "benchmark",
        "--pages",
        "50",
        "--output",
        path_arg(&output_path).as_str(),
    ]);
    assert_success(&output);

    let json: Value = serde_json::from_slice(
        &fs::read(&output_path).expect("benchmark report should be written"),
    )
    .expect("benchmark output should be valid JSON");

    assert_eq!(json["pages"], 50);
    assert_eq!(json["target_total_ms"], 5000);
    assert_eq!(json["under_target"], true);
    for phase in ["parse", "extract", "semantic", "diff", "report", "total"] {
        assert!(json["timings_ms"][phase].is_number());
    }
    assert!(json["summary"]["modified"].as_u64().unwrap_or_default() >= 1);
}

#[test]
fn diff_command_completes_against_real_sample_pdfs() {
    let fixture = TestFixture::new("diff_command_real_samples");
    for pair in REAL_SAMPLE_PAIRS {
        assert_real_sample_diff(&fixture, *pair);
    }
}

fn assert_real_sample_diff(fixture: &TestFixture, pair: RealSamplePair) {
    let old_pdf = real_sample_pdf(pair.old_name);
    let new_pdf = real_sample_pdf(pair.new_name);
    let output_path = fixture.path(&format!("{}-diff.json", pair.slug));

    let output = run_spdfdiff([
        "diff",
        path_arg(&old_pdf).as_str(),
        path_arg(&new_pdf).as_str(),
        "--format",
        "json",
        "--output",
        path_arg(&output_path).as_str(),
    ]);
    assert_success(&output);
    assert!(
        output.stdout.is_empty(),
        "--output should not duplicate the report on stdout"
    );

    let report = read_json(&output_path);
    assert_eq!(report["schema_version"], "0.1.0");
    assert_eq!(report["old_fingerprint"], pair.old_name);
    assert_eq!(report["new_fingerprint"], pair.new_name);
    assert!(report["summary"].is_object());
    assert!(report["changes"].is_array());
    if let Some(expected) = pair.expected {
        assert_eq!(report["summary"]["inserted"], expected.inserted);
        assert_eq!(report["summary"]["deleted"], expected.deleted);
        assert!(
            report["summary"]["modified"].as_u64().unwrap_or_default()
                + report["summary"]["layout_changed"]
                    .as_u64()
                    .unwrap_or_default()
                <= expected.changes as u64
        );
        assert!(
            report["changes"]
                .as_array()
                .expect("changes should be an array")
                .len()
                >= expected.changes,
            "expected at least the documented semantic changes; object-level surfaces may add evidence changes"
        );
    }
    if !matches!(
        pair.slug,
        "attachment-link-bundle" | "layered-redaction" | "tagged-table-reflow"
    ) {
        assert_diagnostic_code_absent(&report, "MISSING_TOUNICODE");
    }
    assert_diagnostic_code_absent(&report, "UNSUPPORTED_STREAM_FILTER");
    assert_diagnostic_code_absent(&report, "UNSUPPORTED_OBJECT_STREAM");
    assert_diagnostic_code_absent(&report, "MISSING_PAGE_CONTENT");
    assert!(
        !fs::read_to_string(output_path)
            .expect("diff JSON should be readable")
            .contains("\\u0000")
    );
}

#[test]
fn inspect_command_completes_against_real_sample_pdf() {
    for sample in real_sample_pdf_names().iter().copied() {
        let pdf = real_sample_pdf(sample);

        let output = run_spdfdiff(["inspect", path_arg(&pdf).as_str(), "--format", "json"]);
        assert_success(&output);
        let report: Value =
            serde_json::from_slice(&output.stdout).expect("inspect stdout should be valid JSON");

        assert_eq!(report["file"], sample);
        assert!(
            report["object_count"].as_u64().unwrap_or_default() >= 1,
            "inspect should parse a non-empty object graph for {sample}"
        );
        assert!(report["diagnostic_count"].as_u64().unwrap_or_default() <= 8);
        assert!(report["first_page_streams"].as_u64().unwrap_or_default() >= 1);
    }
}

#[test]
fn extract_command_completes_against_real_sample_pdf_with_readable_content() {
    for sample in real_sample_pdf_names().iter().copied() {
        let pdf = real_sample_pdf(sample);

        let output = run_spdfdiff(["extract", path_arg(&pdf).as_str(), "--format", "json"]);
        assert_success(&output);
        let report: Value =
            serde_json::from_slice(&output.stdout).expect("extract stdout should be valid JSON");

        assert_eq!(report["file"], sample);
        let paragraphs = report["paragraphs"].as_u64().unwrap_or_default();
        if sample.starts_with("scanned_document_") {
            assert_eq!(
                paragraphs, 0,
                "image-only scanned samples should not invent text"
            );
        } else {
            assert!(paragraphs >= 1, "expected extractable text in {sample}");
        }
        assert!(report["diagnostic_count"].as_u64().unwrap_or_default() <= 4);
    }
}

#[test]
fn html_outputs_complete_against_real_sample_pdfs() {
    let fixture = TestFixture::new("real_sample_html_outputs");

    for pair in real_sample_pdf_pairs().iter().copied() {
        let output_path = fixture.path(&format!("{}-diff.html", pair.slug));
        assert_success(&run_spdfdiff([
            "diff",
            path_arg(&real_sample_pdf(pair.old_name)).as_str(),
            path_arg(&real_sample_pdf(pair.new_name)).as_str(),
            "--format",
            "html",
            "--output",
            path_arg(&output_path).as_str(),
        ]));
        let html = fs::read_to_string(output_path).expect("diff HTML should be written");
        assert_self_contained_html(&html);
        assert!(html.contains("<h1>Semantic PDF Diff</h1>"));
        assert!(html.contains("<th>Old</th><th>New</th>"));
        if pair.slug == "semantic-contract" {
            assert_readable_output_contains_all(
                &html,
                &["TechCorp LLC", "$6,000.00", "Annual Maintenance"],
            );
        }
        if pair.slug == "semantic-images" {
            assert_readable_output_contains_all(&html, &["upgraded, reinforced", "24V"]);
        }
    }

    for sample in real_sample_pdf_names().iter().copied() {
        let pdf = real_sample_pdf(sample);
        let inspect_path = fixture.path(&format!("inspect-{sample}.html"));
        assert_success(&run_spdfdiff([
            "inspect",
            path_arg(&pdf).as_str(),
            "--format",
            "html",
            "--output",
            path_arg(&inspect_path).as_str(),
        ]));
        let inspect_html =
            fs::read_to_string(inspect_path).expect("inspect HTML should be written");
        assert_self_contained_html(&inspect_html);
        assert!(inspect_html.contains("# PDF Inspect"));

        let extract_path = fixture.path(&format!("extract-{sample}.html"));
        assert_success(&run_spdfdiff([
            "extract",
            path_arg(&pdf).as_str(),
            "--format",
            "html",
            "--output",
            path_arg(&extract_path).as_str(),
        ]));
        let extract_html =
            fs::read_to_string(extract_path).expect("extract HTML should be written");
        assert_self_contained_html(&extract_html);
        assert!(extract_html.contains("# Extracted Text"));
        if sample == "semantic_contract_v2.pdf" {
            assert_readable_output_contains_all(
                &extract_html,
                &["TechCorp LLC", "Annual Maintenance", "50% of the total"],
            );
        }
        if sample == "semantic_images_v2.pdf" {
            assert_readable_output_contains_all(
                &extract_html,
                &[
                    "Product Specification: The Widget",
                    "upgraded, reinforced",
                    "24V",
                ],
            );
        }
    }
}

#[test]
fn generated_output_files_include_expected_semantic_sample_content() {
    let fixture = TestFixture::new("semantic_sample_output_content");
    let contract_v2 = real_sample_pdf("semantic_contract_v2.pdf");
    let images_v2 = real_sample_pdf("semantic_images_v2.pdf");
    let contract_extract = fixture.path("contract-v2.md");
    let images_extract = fixture.path("images-v2.md");
    let contract_diff = fixture.path("contract-diff.json");
    let images_diff = fixture.path("images-diff.json");

    assert_success(&run_spdfdiff([
        "extract",
        path_arg(&contract_v2).as_str(),
        "--format",
        "md",
        "--output",
        path_arg(&contract_extract).as_str(),
    ]));
    assert_success(&run_spdfdiff([
        "extract",
        path_arg(&images_v2).as_str(),
        "--format",
        "md",
        "--output",
        path_arg(&images_extract).as_str(),
    ]));
    assert_success(&run_spdfdiff([
        "diff",
        path_arg(&real_sample_pdf("semantic_contract_v1.pdf")).as_str(),
        path_arg(&contract_v2).as_str(),
        "--format",
        "json",
        "--output",
        path_arg(&contract_diff).as_str(),
    ]));
    assert_success(&run_spdfdiff([
        "diff",
        path_arg(&real_sample_pdf("semantic_images_v1.pdf")).as_str(),
        path_arg(&images_v2).as_str(),
        "--format",
        "json",
        "--output",
        path_arg(&images_diff).as_str(),
    ]));

    let contract_text =
        fs::read_to_string(contract_extract).expect("contract extract should be written");
    assert_readable_output_contains_all(
        &contract_text,
        &[
            "TechCorp LLC",
            "30 days written notice",
            "15 days of invoice receipt",
            "Annual Maintenance",
            "50% of the total",
        ],
    );

    let images_text = fs::read_to_string(images_extract).expect("image extract should be written");
    assert_readable_output_contains_all(
        &images_text,
        &[
            "Product Specification: The Widget",
            "upgraded, reinforced",
            "60Hz",
            "24V",
            "Internal Wiring",
        ],
    );

    let contract_diff_text =
        fs::read_to_string(contract_diff).expect("contract diff should be written");
    assert_readable_output_contains_all(
        &contract_diff_text,
        &["TechCorp LLC", "$6,000.00", "Annual Maintenance"],
    );

    let images_diff_text = fs::read_to_string(images_diff).expect("image diff should be written");
    assert_readable_output_contains_all(&images_diff_text, &["upgraded, reinforced", "24V"]);
}

#[test]
fn generated_reports_reflect_documented_scenario_expectations() {
    let fixture = TestFixture::new("documented_scenario_expectations");

    assert_diff_contains_all(
        &fixture,
        "document",
        "document_v1.pdf",
        "document_v2.pdf",
        &["Redis", "150ms", "Version 1.1", "scalable backend"],
    );
    assert_diff_contains_all(
        &fixture,
        "complex",
        "complex_semantic_diff_v1.pdf",
        "complex_semantic_diff_v2.pdf",
        &["γ", "Σ", "Δ"],
    );
    assert_diff_contains_all(
        &fixture,
        "headers-footers",
        "headers_footers_v1.pdf",
        "headers_footers_v2.pdf",
        &["994-B", "2026", "firewall logs"],
    );
    assert_diff_contains_all(
        &fixture,
        "multipage-table",
        "multipage_table_v1.pdf",
        "multipage_table_v2.pdf",
        &["User_15@example.com"],
    );
    assert_diff_contains_all(
        &fixture,
        "interactive-forms",
        "interactive_forms_v1.pdf",
        "interactive_forms_v2.pdf",
        &["Jane Doe", "Engineering", "Laptop"],
    );
    assert_diff_contains_all(
        &fixture,
        "document-outline",
        "document_outline_v1.pdf",
        "document_outline_v2.pdf",
        &["Caching Layer", "API Specifications"],
    );
    assert_diff_contains_all(
        &fixture,
        "layered-redaction",
        "layered_redaction_v1.pdf",
        "layered_redaction_v2.pdf",
        &["REDACTED", "hidden legacy text", "Privacy review"],
    );
    assert_diff_contains_all(
        &fixture,
        "tagged-table-reflow",
        "tagged_table_reflow_v1.pdf",
        "tagged_table_reflow_v2.pdf",
        &[
            "Tagged Control Matrix Q2",
            "MFA Required",
            "Evidence Export",
        ],
    );
    assert_diff_contains_all(
        &fixture,
        "attachment-link-bundle",
        "attachment_link_bundle_v1.pdf",
        "attachment_link_bundle_v2.pdf",
        &["control-evidence-v2.zip", "sha256: BBB222", "production"],
    );

    assert_diff_contains_all(
        &fixture,
        "image-report",
        "report_with_images_v1.pdf",
        "report_with_images_v2.pdf",
        &["ObjectChanged", "image payload differs"],
    );
    assert_diff_contains_all(
        &fixture,
        "semantic-images",
        "semantic_images_v1.pdf",
        "semantic_images_v2.pdf",
        &["ObjectChanged", "image payload"],
    );
    assert_diff_has_diagnostic(
        &fixture,
        "interactive-links",
        "interactive_links_v1.pdf",
        "interactive_links_v2.pdf",
        "UNSUPPORTED_ANNOTATION_DIFF",
    );
    assert_diff_has_diagnostic(
        &fixture,
        "attachment-link-bundle-diagnostic",
        "attachment_link_bundle_v1.pdf",
        "attachment_link_bundle_v2.pdf",
        "UNSUPPORTED_ANNOTATION_DIFF",
    );
    assert_diff_has_diagnostic(
        &fixture,
        "scanned-document",
        "scanned_document_v1.pdf",
        "scanned_document_v2.pdf",
        "MISSING_TEXT_LAYER",
    );
    assert_diff_has_diagnostic(
        &fixture,
        "vector-paths",
        "vector_paths_graphic_v1.pdf",
        "vector_paths_graphic_v2.pdf",
        "UNSUPPORTED_VECTOR_GRAPHIC_DIFF",
    );
    assert_diff_has_diagnostic(
        &fixture,
        "tagged-table-reflow-diagnostic",
        "tagged_table_reflow_v1.pdf",
        "tagged_table_reflow_v2.pdf",
        "TAGGED_MCID_DETECTED",
    );
}

#[test]
fn scenario_markdown_files_document_all_real_sample_pairs() {
    let mut scenarios = String::new();
    for markdown in SAMPLE_SCENARIO_MARKDOWN {
        let path = sample_file(markdown);
        assert!(
            path.is_file(),
            "expected scenario markdown at {}",
            path.display()
        );
        scenarios.push_str(
            &fs::read_to_string(path).expect("scenario markdown should be readable as UTF-8"),
        );
        scenarios.push('\n');
    }

    for pair in real_sample_pdf_pairs() {
        assert!(
            scenarios.contains(pair.old_name),
            "scenario markdown should document {}",
            pair.old_name
        );
        assert!(
            scenarios.contains(pair.new_name),
            "scenario markdown should document {}",
            pair.new_name
        );
    }
}

#[test]
fn corpus_command_completes_against_real_sample_pdfs() {
    let fixture = TestFixture::new("corpus_command_real_samples");
    let corpus = fixture.path("real_corpus");
    fs::create_dir_all(&corpus).expect("real-sample corpus directory should be created");
    for sample in real_sample_pdf_names().iter().copied() {
        fs::copy(real_sample_pdf(sample), corpus.join(sample))
            .expect("real sample should be copied");
    }
    let output_path = fixture.path("real-corpus.json");

    let output = run_spdfdiff([
        "corpus",
        path_arg(&corpus).as_str(),
        "--output",
        path_arg(&output_path).as_str(),
    ]);
    assert_success(&output);
    assert!(
        output.stdout.is_empty(),
        "corpus writes only to the requested output file"
    );

    let report = read_json(&output_path);
    assert_eq!(report["folder"], "real_corpus");
    assert_eq!(report["total"], 40);
    assert_eq!(report["parsed"], 40);
    assert_eq!(report["partial"], 40);
    assert_eq!(report["failed"], 0);
    for (index, sample) in real_sample_pdf_names().iter().copied().enumerate() {
        assert_eq!(report["files"][index]["file"], sample);
    }
    assert!(report["diagnostic_counts"]["CONTENT_OPERATOR_UNKNOWN"].is_null());
    assert_eq!(report["diagnostic_counts"]["STREAM_LENGTH_MISMATCH"], 7);
    assert_eq!(report["diagnostic_counts"]["MISSING_TEXT_LAYER"], 2);
    assert_eq!(
        report["diagnostic_counts"]["UNSUPPORTED_ANNOTATION_DIFF"],
        4
    );
    assert_eq!(
        report["diagnostic_counts"]["UNSUPPORTED_VECTOR_GRAPHIC_DIFF"],
        30
    );
    assert_eq!(
        report["diagnostic_counts"]["MISSING_TOUNICODE_CID_FONT"],
        32
    );
    assert_eq!(report["diagnostic_counts"]["MISSING_TOUNICODE"], 6);
    assert_eq!(report["diagnostic_counts"]["TAGGED_MCID_DETECTED"], 2);
    assert_eq!(
        report["diagnostic_counts"]["TAGGED_PDF_STRUCTURE_DETECTED"],
        2
    );
    assert!(report["diagnostic_counts"]["UNSUPPORTED_IMAGE_DIFF"].is_null());
    assert!(report["diagnostic_counts"]["UNSUPPORTED_STREAM_FILTER"].is_null());
    assert!(report["diagnostic_counts"]["UNSUPPORTED_OBJECT_STREAM"].is_null());
    assert!(report["diagnostic_counts"]["MISSING_PAGE_CONTENT"].is_null());
}

fn run_spdfdiff<const N: usize>(args: [&str; N]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_spdfdiff"))
        .args(args)
        .output()
        .expect("spdfdiff process should start")
}

fn assert_success(output: &Output) {
    assert!(
        output.status.success(),
        "expected spdfdiff to exit successfully\nstatus: {}\nstdout:\n{}\nstderr:\n{}",
        output.status,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

fn path_arg(path: &Path) -> String {
    path.to_string_lossy().into_owned()
}

fn sample_file(name: &str) -> PathBuf {
    let path = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .join("samples")
        .join(name);
    assert!(path.is_file(), "expected sample file at {}", path.display());
    path
}

fn real_sample_pdf(name: &str) -> PathBuf {
    let path = sample_file(name);
    assert!(
        path.extension().and_then(|extension| extension.to_str()) == Some("pdf"),
        "expected real PDF sample at {}",
        path.display()
    );
    path
}

fn real_sample_pdf_names() -> &'static [&'static str] {
    REAL_SAMPLE_PDFS
}

fn real_sample_pdf_pairs() -> &'static [RealSamplePair] {
    REAL_SAMPLE_PAIRS
}

fn read_json(path: &Path) -> Value {
    serde_json::from_str(&fs::read_to_string(path).expect("JSON report should be written"))
        .expect("report should be valid JSON")
}

fn assert_diff_contains_all(
    fixture: &TestFixture,
    slug: &str,
    old_name: &str,
    new_name: &str,
    expected_terms: &[&str],
) {
    let output_path = fixture.path(&format!("{slug}.json"));
    assert_success(&run_spdfdiff([
        "diff",
        path_arg(&real_sample_pdf(old_name)).as_str(),
        path_arg(&real_sample_pdf(new_name)).as_str(),
        "--format",
        "json",
        "--output",
        path_arg(&output_path).as_str(),
    ]));
    let output = fs::read_to_string(output_path).expect("diff JSON should be written");
    assert_readable_output_contains_all(&output, expected_terms);
}

fn assert_diff_has_diagnostic(
    fixture: &TestFixture,
    slug: &str,
    old_name: &str,
    new_name: &str,
    expected_code: &str,
) {
    let output_path = fixture.path(&format!("{slug}.json"));
    assert_success(&run_spdfdiff([
        "diff",
        path_arg(&real_sample_pdf(old_name)).as_str(),
        path_arg(&real_sample_pdf(new_name)).as_str(),
        "--format",
        "json",
        "--output",
        path_arg(&output_path).as_str(),
    ]));
    let report = read_json(&output_path);
    let diagnostics = report["diagnostics"]
        .as_array()
        .expect("diagnostics should be an array");
    assert!(
        diagnostics
            .iter()
            .any(|diagnostic| diagnostic["code"] == expected_code),
        "expected diagnostic code {expected_code} in {diagnostics:?}"
    );
}

fn assert_diagnostic_code_absent(report: &Value, code: &str) {
    let diagnostics = report["diagnostics"]
        .as_array()
        .expect("diagnostics should be an array");
    assert!(
        diagnostics
            .iter()
            .all(|diagnostic| diagnostic["code"] != code),
        "did not expect diagnostic code {code} in {diagnostics:?}"
    );
}

fn assert_readable_output_contains_all(output: &str, expected_terms: &[&str]) {
    assert!(
        !output.contains('\0') && !output.contains("\\u0000"),
        "output should not contain embedded NUL text: {output}"
    );
    for expected in expected_terms {
        assert!(
            output.contains(expected),
            "expected generated output to contain `{expected}` in:\n{output}"
        );
    }
}

fn assert_self_contained_html(output: &str) {
    assert!(output.starts_with("<!doctype html>"));
    assert!(
        !output.contains("http://") && !output.contains("https://"),
        "HTML output should not depend on external network resources: {output}"
    );
}

struct TestFixture {
    root: PathBuf,
}

impl TestFixture {
    fn new(name: &str) -> Self {
        let index = NEXT_TEST_DIR.fetch_add(1, Ordering::Relaxed);
        let root = std::env::temp_dir()
            .join("spdfdiff_cli_integration")
            .join(format!("{}-{}-{index}", std::process::id(), name));
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).expect("test fixture directory should be created");
        Self { root }
    }

    fn path(&self, name: &str) -> PathBuf {
        self.root.join(name)
    }

    fn write_pdf(&self, name: &str, text: &str) -> PathBuf {
        let path = self.path(name);
        fs::write(&path, minimal_pdf(text)).expect("PDF fixture should be written");
        path
    }
}

impl Drop for TestFixture {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.root);
    }
}

fn minimal_pdf(text: &str) -> Vec<u8> {
    let content = format!("BT /F1 12 Tf 72 720 Td ({text}) Tj ET\n");
    format!(
        "%PDF-1.7\n\
         1 0 obj\n\
         << /Type /Catalog /Pages 2 0 R >>\n\
         endobj\n\
         2 0 obj\n\
         << /Type /Pages /Kids [3 0 R] /Count 1 >>\n\
         endobj\n\
         3 0 obj\n\
         << /Type /Page /Parent 2 0 R /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>\n\
         endobj\n\
         4 0 obj\n\
         << /Length {} >>\n\
         stream\n\
         {content}\
         endstream\n\
         endobj\n\
         5 0 obj\n\
         << /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\n\
         endobj\n",
        content.len()
    )
    .into_bytes()
}