sdsforge 0.5.0

CLI and desktop app for generating, converting, validating, and rendering chemical safety data sheets in MHLW/JIS Z 7253 structured formats.
Documentation
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
//! Subprocess-level CLI tests for `sdsforge generate`.
//!
//! Covers the offline (default) path only -- no test here performs live
//! network access. `--enrich` behavior against real PubChem is exercised
//! manually, not in automated tests, since it would make CI depend on
//! external network availability.

use std::path::Path;
use std::process::{Command, Output};

fn bin() -> &'static str {
    env!("CARGO_BIN_EXE_sdsforge")
}

fn example_yaml() -> &'static str {
    concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../examples/generate/example_cleaner.yaml"
    )
}

fn run(args: &[&str]) -> Output {
    Command::new(bin())
        .args(args)
        .output()
        .expect("failed to run sdsforge binary")
}

fn three_artifacts_exist(dir: &Path) -> bool {
    dir.join("official_sds.json").exists()
        && dir.join("generation_report.json").exists()
        && dir.join("review_report.md").exists()
}

#[test]
fn generate_help_documents_the_command() {
    let output = run(&["generate", "--help"]);
    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("--input"));
    assert!(stdout.contains("--output-dir"));
    assert!(stdout.contains("--enrich"));
    assert!(stdout.contains("--strict"));
    assert!(stdout.contains("--force"));
}

#[test]
fn json_input_generates_all_three_files() {
    let dir = tempfile::tempdir().unwrap();
    // Convert the committed YAML fixture to JSON so this test covers the
    // JSON input path with the exact same product data as the YAML test.
    let yaml_text = std::fs::read_to_string(example_yaml()).unwrap();
    let value: serde_json::Value = serde_norway::from_str(&yaml_text).unwrap();
    let json_path = dir.path().join("input.json");
    std::fs::write(&json_path, serde_json::to_string_pretty(&value).unwrap()).unwrap();

    let out_dir = dir.path().join("out");
    let output = run(&[
        "generate",
        "--input",
        json_path.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
}

#[test]
fn yaml_input_generates_all_three_files() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    let output = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
}

#[test]
fn unknown_input_extension_fails_cleanly() {
    let dir = tempfile::tempdir().unwrap();
    let bad_input = dir.path().join("input.txt");
    std::fs::copy(example_yaml(), &bad_input).unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        bad_input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("unsupported input extension"));
    assert!(!out_dir.exists());
}

#[test]
fn malformed_json_writes_no_final_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let bad_input = dir.path().join("input.json");
    std::fs::write(&bad_input, "{ not valid json").unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        bad_input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(!three_artifacts_exist(&out_dir));
}

#[test]
fn malformed_yaml_writes_no_final_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let bad_input = dir.path().join("input.yaml");
    std::fs::write(&bad_input, "trade_name: [unterminated").unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        bad_input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(!three_artifacts_exist(&out_dir));
}

#[test]
fn existing_target_files_cause_failure_without_force() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");

    let first = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(first.status.success());

    let second = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!second.status.success());
    assert!(String::from_utf8_lossy(&second.stderr).contains("already exists"));
}

#[test]
fn force_replaces_all_three_outputs() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");

    let first = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(first.status.success());

    let second = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
        "--force",
    ]);
    assert!(
        second.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&second.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
}

#[test]
fn stdout_contains_no_artifact_body_and_status_goes_to_stderr() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    let output = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(output.status.success());
    assert!(
        output.stdout.is_empty(),
        "stdout was not empty: {:?}",
        String::from_utf8_lossy(&output.stdout)
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("Generated SDS draft"));
    assert!(stderr.contains("Release status"));
}

#[test]
fn strict_mode_writes_blocked_artifacts_and_returns_nonzero() {
    let dir = tempfile::tempdir().unwrap();
    // Invalid CAS check digit -> GEN-CAS-CHECKDIGIT (HIGH) -> Blocked.
    let input = dir.path().join("input.json");
    std::fs::write(
        &input,
        r#"{
            "trade_name": "Bad CAS Test",
            "other_names": [],
            "supplier": {"company_name": "Acme", "address": null, "phone": null, "email": null},
            "components": [
                {"cas_number": "7732-18-6", "name": "Water",
                 "concentration": {"exact": 100.0, "lower": null, "upper": null, "unit": "%"}}
            ],
            "measured_properties": {"flash_point": [], "boiling_point": [], "vapor_pressure": [],
                "explosive_limits": [], "self_reactivity": [], "oxidizing_properties": [],
                "metal_corrosivity": []},
            "evidence": []
        }"#,
    )
    .unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
        "--strict",
    ]);
    assert!(!output.status.success());
    assert!(three_artifacts_exist(&out_dir));
    let report: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(out_dir.join("generation_report.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(report["release_status"], "blocked");
}

#[test]
fn normal_mode_writes_blocked_artifacts_and_exits_successfully() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.json");
    std::fs::write(
        &input,
        r#"{
            "trade_name": "Bad CAS Test",
            "other_names": [],
            "supplier": {"company_name": "Acme", "address": null, "phone": null, "email": null},
            "components": [
                {"cas_number": "7732-18-6", "name": "Water",
                 "concentration": {"exact": 100.0, "lower": null, "upper": null, "unit": "%"}}
            ],
            "measured_properties": {"flash_point": [], "boiling_point": [], "vapor_pressure": [],
                "explosive_limits": [], "self_reactivity": [], "oxidizing_properties": [],
                "metal_corrosivity": []},
            "evidence": []
        }"#,
    )
    .unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
    assert!(String::from_utf8_lossy(&output.stderr).contains("BLOCKED"));
}

/// The committed example has no invalid/duplicate CAS and one unresolved
/// (but nonblocking) property set -- ReviewRequired, not Blocked.
#[test]
fn review_required_without_blocking_fields_exits_successfully() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    let output = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(output.status.success());
    let report: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(out_dir.join("generation_report.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(report["release_status"], "review_required");
}

#[test]
fn repeated_offline_runs_produce_byte_equivalent_files() {
    let dir = tempfile::tempdir().unwrap();
    let out_a = dir.path().join("a");
    let out_b = dir.path().join("b");

    for out_dir in [&out_a, &out_b] {
        let output = run(&[
            "generate",
            "--input",
            example_yaml(),
            "--output-dir",
            out_dir.to_str().unwrap(),
        ]);
        assert!(output.status.success());
    }

    for name in [
        "official_sds.json",
        "generation_report.json",
        "review_report.md",
    ] {
        assert_eq!(
            std::fs::read(out_a.join(name)).unwrap(),
            std::fs::read(out_b.join(name)).unwrap(),
            "{name} differs between repeated runs"
        );
    }
}

#[test]
fn missing_boiling_point_remains_unresolved_with_required_evidence() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    let report: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(out_dir.join("generation_report.json")).unwrap(),
    )
    .unwrap();
    let unresolved = report["unresolved"].as_array().unwrap();
    assert!(unresolved.iter().any(
        |u| u["path"].as_str().unwrap_or("").contains("BoilingPoint")
            || u["title"]
                .as_str()
                .unwrap_or("")
                .to_lowercase()
                .contains("boiling")
    ));
}

#[test]
fn evidence_backed_flash_point_appears_in_official_json_only_with_metadata_in_report() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);

    let official = std::fs::read_to_string(out_dir.join("official_sds.json")).unwrap();
    assert!(official.contains("100"));
    // Method is a real MHLW schema field on FlashPoint, so it legitimately
    // appears here too -- but evidence-tracking metadata (sample/batch IDs,
    // which have no schema field) must never leak into the official SDS,
    // only into the generation report.
    assert!(!official.contains("LOT-2026-0341"));

    let report = std::fs::read_to_string(out_dir.join("generation_report.json")).unwrap();
    assert!(report.contains("LOT-2026-0341"));
    assert!(report.contains("ASTM D93"));
}

#[test]
fn review_report_clearly_says_the_draft_is_unapproved() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    let review = std::fs::read_to_string(out_dir.join("review_report.md")).unwrap();
    assert!(review.contains("has not been approved"));
}

#[test]
fn official_json_contains_no_report_keys() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    let official: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(out_dir.join("official_sds.json")).unwrap())
            .unwrap();
    let keys: Vec<&str> = official
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    for forbidden in [
        "release_status",
        "findings",
        "unresolved",
        "provenance",
        "evidence_summary",
    ] {
        assert!(!keys.contains(&forbidden));
    }
}

#[test]
fn invalid_profile_value_rejected_by_clap() {
    let dir = tempfile::tempdir().unwrap();
    let out_dir = dir.path().join("out");
    let output = run(&[
        "generate",
        "--input",
        example_yaml(),
        "--output-dir",
        out_dir.to_str().unwrap(),
        "--profile",
        "osha-v1",
    ]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("possible values"));
}

// -- serde(default)/deny_unknown_fields input UX (concise inputs) --

const MINIMAL_YAML: &str = r#"
trade_name: Minimal Product
supplier:
  company_name: Acme
components:
  - concentration:
      exact: 100.0
      unit: "%"
"#;

const MINIMAL_JSON: &str = r#"{
    "trade_name": "Minimal Product",
    "supplier": {"company_name": "Acme"},
    "components": [
        {"concentration": {"exact": 100.0, "unit": "%"}}
    ]
}"#;

/// Every field the concise fixtures above omit, spelled out explicitly.
/// Must produce byte-identical artifacts to the concise form.
const VERBOSE_EQUIVALENT_YAML: &str = r#"
trade_name: Minimal Product
other_names: []
supplier:
  company_name: Acme
  address: null
  phone: null
  email: null
components:
  - cas_number: null
    name: null
    concentration:
      exact: 100.0
      lower: null
      upper: null
      unit: "%"
measured_properties:
  flash_point: []
  boiling_point: []
  vapor_pressure: []
  explosive_limits: []
  self_reactivity: []
  oxidizing_properties: []
  metal_corrosivity: []
evidence: []
"#;

#[test]
fn minimal_yaml_generates_successfully() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.yaml");
    std::fs::write(&input, MINIMAL_YAML).unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
}

#[test]
fn minimal_json_generates_successfully() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.json");
    std::fs::write(&input, MINIMAL_JSON).unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        output.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
}

#[test]
fn verbose_and_concise_inputs_generate_byte_equivalent_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let concise_input = dir.path().join("concise.yaml");
    std::fs::write(&concise_input, MINIMAL_YAML).unwrap();
    let verbose_input = dir.path().join("verbose.yaml");
    std::fs::write(&verbose_input, VERBOSE_EQUIVALENT_YAML).unwrap();

    let concise_out = dir.path().join("concise_out");
    let verbose_out = dir.path().join("verbose_out");

    for (input, out_dir) in [
        (&concise_input, &concise_out),
        (&verbose_input, &verbose_out),
    ] {
        let output = run(&[
            "generate",
            "--input",
            input.to_str().unwrap(),
            "--output-dir",
            out_dir.to_str().unwrap(),
        ]);
        assert!(
            output.status.success(),
            "stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    for name in [
        "official_sds.json",
        "generation_report.json",
        "review_report.md",
    ] {
        assert_eq!(
            std::fs::read(concise_out.join(name)).unwrap(),
            std::fs::read(verbose_out.join(name)).unwrap(),
            "{name} differs between concise and verbose input"
        );
    }
}

#[test]
fn example_cleaner_concise_form_matches_fully_expanded_equivalent() {
    // The committed example was simplified to the concise form as part of
    // this change. Re-expand every now-implicit default by hand and prove
    // it still produces byte-identical artifacts to the committed file.
    const FULLY_EXPANDED: &str = r#"
trade_name: "AllClean Multi-Surface Cleaner"
other_names: []
supplier:
  company_name: "Example Chemical Co., Ltd."
  address: "1-1 Example, Chiyoda-ku, Tokyo"
  phone: "03-1234-5678"
  email: "safety@example.com"
components:
  - cas_number: "7732-18-5"
    name: "Water"
    concentration:
      exact: 85.0
      lower: null
      upper: null
      unit: "%"
  - cas_number: "151-21-3"
    name: "Sodium Lauryl Sulfate"
    concentration:
      exact: null
      lower: 5.0
      upper: 15.0
      unit: "%"
measured_properties:
  flash_point:
    - value: 100.0
      unit: "degC"
      method: "Closed Cup (ASTM D93)"
      conditions:
        temperature_c: 20.0
        pressure_kpa: null
        atmosphere: null
      sample_id: "LOT-2026-0341"
      batch_id: null
      evidence_id: "ev-flash-point-1"
  boiling_point: []
  vapor_pressure: []
  explosive_limits: []
  self_reactivity: []
  oxidizing_properties: []
  metal_corrosivity: []
evidence:
  - id: "ev-flash-point-1"
    level: "product_test_report"
    reference: "Internal Lab Report FP-2026-0341"
    issuer: "Example Chemical Co. QA Lab"
    document_date: "2026-05-12"
    applies_to: "finished_product"
"#;
    let dir = tempfile::tempdir().unwrap();
    let expanded_input = dir.path().join("expanded.yaml");
    std::fs::write(&expanded_input, FULLY_EXPANDED).unwrap();

    let concise_out = dir.path().join("concise_out");
    let expanded_out = dir.path().join("expanded_out");

    for (input, out_dir) in [
        (example_yaml().to_string(), &concise_out),
        (expanded_input.to_str().unwrap().to_string(), &expanded_out),
    ] {
        let output = run(&[
            "generate",
            "--input",
            &input,
            "--output-dir",
            out_dir.to_str().unwrap(),
        ]);
        assert!(
            output.status.success(),
            "stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    for name in [
        "official_sds.json",
        "generation_report.json",
        "review_report.md",
    ] {
        assert_eq!(
            std::fs::read(concise_out.join(name)).unwrap(),
            std::fs::read(expanded_out.join(name)).unwrap(),
            "{name} differs between the committed example and its fully-expanded equivalent"
        );
    }
}

#[test]
fn unknown_top_level_field_fails_with_actionable_error_and_writes_no_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.yaml");
    std::fs::write(
        &input,
        r#"
trade_name: Typo Product
supplier:
  company_name: Acme
components:
  - concentration:
      exact: 100.0
      unit: "%"
bogus_field: true
"#,
    )
    .unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(String::from_utf8_lossy(&output.stderr).contains("bogus_field"));
    assert!(!three_artifacts_exist(&out_dir));
}

#[test]
fn misspelled_concentration_field_fails_with_actionable_error_and_writes_no_artifacts() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.yaml");
    std::fs::write(
        &input,
        r#"
trade_name: Typo Product
supplier:
  company_name: Acme
components:
  - cas_number: "7732-18-5"
    name: Water
    concentation:
      exact: 100.0
      unit: "%"
"#,
    )
    .unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(!three_artifacts_exist(&out_dir));
    let stderr = String::from_utf8_lossy(&output.stderr);
    // The typo ("concentation") is an unknown field, and the real required
    // key ("concentration") is then missing -- either name being present in
    // the error is actionable; assert on the fields, not the exact wording.
    assert!(stderr.contains("concentation") || stderr.contains("concentration"));
}

#[test]
fn missing_trade_name_fails_via_cli() {
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("input.yaml");
    std::fs::write(
        &input,
        r#"
supplier:
  company_name: Acme
components:
  - concentration:
      exact: 100.0
      unit: "%"
"#,
    )
    .unwrap();
    let out_dir = dir.path().join("out");

    let output = run(&[
        "generate",
        "--input",
        input.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!output.status.success());
    assert!(!three_artifacts_exist(&out_dir));
    assert!(String::from_utf8_lossy(&output.stderr).contains("trade_name"));
}

// -- Cross-platform `--force` artifact replacement (CI gate) --

const CROSS_PLATFORM_INPUT_A: &str = r#"
trade_name: Cross Platform Product A
supplier:
  company_name: Acme A
components:
  - cas_number: "7732-18-5"
    name: Water
    concentration:
      exact: 100.0
      unit: "%"
"#;

const CROSS_PLATFORM_INPUT_B: &str = r#"
trade_name: Cross Platform Product B
supplier:
  company_name: Acme B
components:
  - cas_number: "64-17-5"
    name: Ethanol
    concentration:
      exact: 100.0
      unit: "%"
"#;

const ARTIFACT_NAMES: [&str; 3] = [
    "official_sds.json",
    "generation_report.json",
    "review_report.md",
];

fn exact_dir_entries(dir: &Path) -> std::collections::BTreeSet<String> {
    std::fs::read_dir(dir)
        .unwrap()
        .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
        .collect()
}

fn expected_artifact_entries() -> std::collections::BTreeSet<String> {
    ARTIFACT_NAMES.iter().map(|s| s.to_string()).collect()
}

fn read_artifacts(dir: &Path) -> [Vec<u8>; 3] {
    [
        std::fs::read(dir.join(ARTIFACT_NAMES[0])).unwrap(),
        std::fs::read(dir.join(ARTIFACT_NAMES[1])).unwrap(),
        std::fs::read(dir.join(ARTIFACT_NAMES[2])).unwrap(),
    ]
}

/// Uppercase-with-spaces equivalent of a `snake_case` `release_status`
/// value, matching `describe_release_status`'s wording in
/// `review_report.md` (e.g. `"review_required"` -> `"REVIEW REQUIRED"`).
fn shout_case(snake_case: &str) -> String {
    snake_case.replace('_', " ").to_uppercase()
}

/// Verifies, on the real compiled `sdsforge` binary (not a direct call to
/// `write_generation_artifacts`), that `generate --force` replaces all
/// three generation artifacts correctly on both Linux and Windows: a
/// preflight refusal without `--force`, a full replacement with `--force`,
/// byte-level (not just mtime) content verification, cross-artifact
/// consistency, and no leftover temp files at any step. This is the CI gate
/// referenced by `.github/workflows/ci.yml`.
#[test]
fn cross_platform_force_replaces_complete_artifact_set() {
    let dir = tempfile::tempdir().unwrap();
    let input_a = dir.path().join("input-a.yaml");
    let input_b = dir.path().join("input-b.yaml");
    std::fs::write(&input_a, CROSS_PLATFORM_INPUT_A).unwrap();
    std::fs::write(&input_b, CROSS_PLATFORM_INPUT_B).unwrap();
    let out_dir = dir.path().join("generated");

    // -- 1. Initial generation with input A --
    let first = run(&[
        "generate",
        "--input",
        input_a.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(
        first.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&first.stderr)
    );
    assert!(
        first.stdout.is_empty(),
        "stdout was not empty: {:?}",
        String::from_utf8_lossy(&first.stdout)
    );
    assert!(three_artifacts_exist(&out_dir));
    assert_eq!(exact_dir_entries(&out_dir), expected_artifact_entries());

    let [official_a, report_a, review_a] = read_artifacts(&out_dir);
    let official_a_json: serde_json::Value = serde_json::from_slice(&official_a)
        .expect("official_sds.json (input A) must parse as JSON");
    let _: serde_json::Value = serde_json::from_slice(&report_a)
        .expect("generation_report.json (input A) must parse as JSON");
    let review_a_text =
        String::from_utf8(review_a.clone()).expect("review_report.md (input A) must be UTF-8");
    assert!(!review_a_text.is_empty());
    assert_eq!(
        official_a_json["Identification"]["TradeProductIdentity"]["TradeNameJP"],
        "Cross Platform Product A"
    );

    // -- 2. Refusal without --force, using a materially different input B --
    let refused = run(&[
        "generate",
        "--input",
        input_b.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
    ]);
    assert!(!refused.status.success());
    assert!(String::from_utf8_lossy(&refused.stderr).contains("already exists"));
    // The preflight check ran before any write: all three original files
    // are byte-for-byte unchanged, and no temp/backup/fourth file appeared.
    let [official_after_refusal, report_after_refusal, review_after_refusal] =
        read_artifacts(&out_dir);
    assert_eq!(official_after_refusal, official_a);
    assert_eq!(report_after_refusal, report_a);
    assert_eq!(review_after_refusal, review_a);
    assert_eq!(exact_dir_entries(&out_dir), expected_artifact_entries());

    // -- 3. Replacement with --force, using input B --
    let forced = run(&[
        "generate",
        "--input",
        input_b.to_str().unwrap(),
        "--output-dir",
        out_dir.to_str().unwrap(),
        "--force",
    ]);
    assert!(
        forced.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&forced.stderr)
    );
    assert!(three_artifacts_exist(&out_dir));
    assert_eq!(exact_dir_entries(&out_dir), expected_artifact_entries());

    let [official_b, report_b, review_b] = read_artifacts(&out_dir);
    // Every file actually changed -- not just the one the CLI happened to
    // touch first.
    assert_ne!(official_b, official_a, "official_sds.json did not change");
    assert_ne!(report_b, report_a, "generation_report.json did not change");
    assert_ne!(review_b, review_a, "review_report.md did not change");

    // Both JSON files still parse after the CLI process has fully exited.
    let official_b_json: serde_json::Value = serde_json::from_slice(&official_b)
        .expect("official_sds.json (input B) must parse as JSON");
    let report_b_json: serde_json::Value = serde_json::from_slice(&report_b)
        .expect("generation_report.json (input B) must parse as JSON");
    let review_b_text =
        String::from_utf8(review_b.clone()).expect("review_report.md (input B) must be UTF-8");

    // -- 4. Cross-artifact consistency: every file reflects input B, and
    // none retains input A's product identity. --
    assert_eq!(
        official_b_json["Identification"]["TradeProductIdentity"]["TradeNameJP"],
        "Cross Platform Product B"
    );
    for (name, bytes) in ARTIFACT_NAMES
        .iter()
        .zip([&official_b, &report_b, &review_b])
    {
        let text = String::from_utf8_lossy(bytes);
        assert!(
            !text.contains("Cross Platform Product A"),
            "{name} still references input A's product identity"
        );
        assert!(
            !text.contains("Acme A"),
            "{name} still references input A's supplier"
        );
    }

    // release_status agrees between generation_report.json and
    // review_report.md.
    let release_status = report_b_json["release_status"].as_str().unwrap();
    assert!(
        review_b_text.contains(&shout_case(release_status)),
        "review_report.md does not mention release status {release_status:?}"
    );

    // Unresolved-field counts agree between JSON and Markdown.
    let unresolved_count = report_b_json["unresolved"].as_array().unwrap().len();
    assert!(
        review_b_text.contains(&format!("Unresolved fields: {unresolved_count}")),
        "review_report.md's unresolved count does not match generation_report.json's {unresolved_count}"
    );

    // generation_report.json embeds no full SdsRoot -- exactly its own
    // seven top-level fields, never a raw MHLW schema section key.
    let report_keys: std::collections::BTreeSet<&str> = report_b_json
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    let expected_report_keys: std::collections::BTreeSet<&str> = [
        "report_schema_version",
        "release_status",
        "findings",
        "unresolved",
        "provenance",
        "evidence_summary",
        "release_gate",
    ]
    .into_iter()
    .collect();
    assert_eq!(report_keys, expected_report_keys);

    // official_sds.json carries no report-only keys.
    let official_keys: std::collections::BTreeSet<&str> = official_b_json
        .as_object()
        .unwrap()
        .keys()
        .map(String::as_str)
        .collect();
    for forbidden in [
        "release_status",
        "findings",
        "unresolved",
        "provenance",
        "evidence_summary",
    ] {
        assert!(!official_keys.contains(forbidden));
    }
}