The Rust pipeline is the official seed writer (pnpm import:equine); sheet-ref.json and report-migration-map.json carry 12d78872...74e2 and the v1 block, the only two seed files changed. 27c7383c...2660 stays a registered alias, honoured only through LoadedSheet::recognises_historical_stamp (verified v1, recomputed document). M1: the snapshot of an old report keeps its stamp; export, archive reader strict. Historical oracles run in their pre-switch context; equivalence is crate-private.
520 lines
19 KiB
Rust
520 lines
19 KiB
Rust
//! R4.7c: the archive check tool against the frozen O10 oracle
|
|
//! (`oracles/r4/compare/compare.json`, `apps/app/tests/unit/r4.compare-oracles.test.ts`): the
|
|
//! 230 invocations of the TypeScript CLI (every fixture PDF, a folder, the argument errors and
|
|
//! the 11 cases of `apps/app/tests/unit/scripts.compare.test.ts`, their documents attached to
|
|
//! the scenario PDFs) replayed in-process through `pdf::compare::run` on the seed draft sheet.
|
|
//! A row without `r4Declared` must match exactly (stdout, stderr, exit code); a declared row
|
|
//! (plan §A.3: a ZodError dump, a version read as `Infinity`, a lone surrogate kept) must
|
|
//! print the closed wording of `CompareFailure::reason`. The real binary is spawned once per
|
|
//! argument shape to prove it prints what the pure run prints.
|
|
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod common;
|
|
|
|
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
use std::sync::OnceLock;
|
|
|
|
use common::r4::{r4_oracle, repo_root, sha256_hex};
|
|
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
|
|
use faunosteo_core::pdf::compare::lines::esc;
|
|
use faunosteo_core::pdf::compare::run::diff_lines;
|
|
use faunosteo_core::pdf::compare::{CliArgs, FileFailure, Files, Locale, Run, parse_args, run};
|
|
use faunosteo_core::pdf::read::MAX_PDF_BYTES;
|
|
use serde_json::{Value, json};
|
|
|
|
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
|
|
|
|
/// The seed sheet as the binary loads it: document and map, no drawing.
|
|
/// The sheet O10 was frozen against (before R5.6): the seed's document with the pre-switch
|
|
/// `sheet-ref.json` and migration map (`tests/fixtures/r5-6/pre-switch`). O10 replays in that
|
|
/// context; the switched seed has its own test under the declared rule below.
|
|
fn draft_sheet() -> &'static LoadedSheet {
|
|
static SHEET: OnceLock<LoadedSheet> = OnceLock::new();
|
|
SHEET
|
|
.get_or_init(|| sheet_with(&repo_root().join("crates/core/tests/fixtures/r5-6/pre-switch")))
|
|
}
|
|
|
|
/// The committed (switched) seed, as the `report-compare` binary reads it.
|
|
fn switched_sheet() -> &'static LoadedSheet {
|
|
static SHEET: OnceLock<LoadedSheet> = OnceLock::new();
|
|
SHEET.get_or_init(|| sheet_with(&repo_root().join(SEED)))
|
|
}
|
|
|
|
fn sheet_with(refs: &std::path::Path) -> LoadedSheet {
|
|
let seed = repo_root().join(SEED);
|
|
let read = |dir: &std::path::Path, name: &str| -> Value {
|
|
serde_json::from_str(&std::fs::read_to_string(dir.join(name)).unwrap()).unwrap()
|
|
};
|
|
let payload = json!({
|
|
"status": "draft",
|
|
"ref": read(refs, "sheet-ref.json"),
|
|
"document": read(&seed, "document.json"),
|
|
"migrationMap": read(refs, "report-migration-map.json"),
|
|
"assets": {},
|
|
"assetHashes": {},
|
|
});
|
|
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
|
|
LoadedSheet::from_bundle(data).unwrap()
|
|
}
|
|
|
|
/// The declared difference of the R5.6 switch for `report-compare` (`R5.6-fingerprint-v1`), per
|
|
/// file block: a report read as is keeps its historical stamp and is the bundled sheet through
|
|
/// the trusted alias, both raw values printed; a v2 report migrated on the switched map carries
|
|
/// the canonical value and keeps the plain line. Only that one line changes; a report on
|
|
/// another document still prints `!!`, so no content difference can hide behind the rule.
|
|
fn r5_6_rule(text: &str) -> String {
|
|
let plain = "(draft, matches the bundled sheet)";
|
|
let alias = "(draft, matches the bundled sheet through the trusted alias: report sha 27c7383c445c…, sheet sha 12d788727c1e…)";
|
|
let mut out = String::new();
|
|
for (index, block) in text.split("== ").enumerate() {
|
|
if index > 0 {
|
|
out.push_str("== ");
|
|
}
|
|
if block.contains("(migrated)") {
|
|
out.push_str(block);
|
|
} else {
|
|
out.push_str(&block.replace(plain, alias));
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The repository as the TypeScript CLI saw it: arguments typed from the root.
|
|
struct RepoFiles;
|
|
|
|
impl Files for RepoFiles {
|
|
fn expand(&self, arg: &str) -> Vec<String> {
|
|
let path = repo_root().join(arg);
|
|
let Ok(entries) = std::fs::read_dir(&path) else {
|
|
return vec![path.to_string_lossy().into_owned()];
|
|
};
|
|
let mut names: Vec<String> = entries
|
|
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
|
.filter(|n| n.to_lowercase().ends_with(".pdf"))
|
|
.collect();
|
|
names.sort();
|
|
names
|
|
.iter()
|
|
.map(|n| path.join(n).to_string_lossy().into_owned())
|
|
.collect()
|
|
}
|
|
|
|
fn read(&self, path: &str) -> Result<Vec<u8>, FileFailure> {
|
|
let meta = std::fs::metadata(path).map_err(|_| FileFailure::NotFound)?;
|
|
if !meta.is_file() {
|
|
return Err(FileFailure::NotRegular);
|
|
}
|
|
if meta.len() > MAX_PDF_BYTES as u64 {
|
|
return Err(FileFailure::TooLarge);
|
|
}
|
|
std::fs::read(path).map_err(|_| FileFailure::NotReadable)
|
|
}
|
|
}
|
|
|
|
/// One recorded invocation of the TypeScript CLI.
|
|
struct Row {
|
|
group: String,
|
|
scenario: Option<String>,
|
|
args: Vec<String>,
|
|
declared: Option<String>,
|
|
exit_code: i32,
|
|
stdout: String,
|
|
stderr: String,
|
|
}
|
|
|
|
fn lines_text(value: &Value) -> String {
|
|
let lines: Vec<&str> = value
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|l| l.as_str().unwrap())
|
|
.collect();
|
|
lines.join("\n")
|
|
}
|
|
|
|
fn oracle() -> &'static Value {
|
|
static ORACLE: OnceLock<Value> = OnceLock::new();
|
|
ORACLE.get_or_init(|| r4_oracle("compare/compare.json"))
|
|
}
|
|
|
|
fn rows() -> Vec<Row> {
|
|
oracle()["invocations"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|row| Row {
|
|
group: row["group"].as_str().unwrap().to_owned(),
|
|
scenario: row["scenario"].as_str().map(str::to_owned),
|
|
args: row["args"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|a| a.as_str().unwrap().to_owned())
|
|
.collect(),
|
|
declared: row["r4Declared"].as_str().map(str::to_owned),
|
|
exit_code: i32::try_from(row["exitCode"].as_i64().unwrap()).unwrap(),
|
|
stdout: lines_text(&row["stdout"]),
|
|
stderr: lines_text(&row["stderr"]),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn replay(row: &Row) -> Run {
|
|
run(draft_sheet(), &row.args, &RepoFiles)
|
|
}
|
|
|
|
fn replay_switched(row: &Row) -> Run {
|
|
run(switched_sheet(), &row.args, &RepoFiles)
|
|
}
|
|
|
|
fn assert_exact(row: &Row) {
|
|
let got = replay(row);
|
|
let args = row.args.join(" ");
|
|
assert_eq!(got.stdout, row.stdout, "stdout of `{args}`");
|
|
assert_eq!(got.stderr, row.stderr, "stderr of `{args}`");
|
|
assert_eq!(got.exit_code, row.exit_code, "exit code of `{args}`");
|
|
}
|
|
|
|
/// The file named by a failure line (`!! <name>: …`), or the one argument's last segment.
|
|
fn entry_name(row: &Row) -> String {
|
|
let path = row.args.iter().find(|a| !a.starts_with("--")).unwrap();
|
|
Path::new(path)
|
|
.file_name()
|
|
.unwrap()
|
|
.to_string_lossy()
|
|
.into_owned()
|
|
}
|
|
|
|
/// The Rust wording a declared row printed: which §A.3 line it is.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
enum Printed {
|
|
/// `report document invalid: <detail>` where the TypeScript dumped the ZodError issues.
|
|
Invalid,
|
|
/// `attachment refused: lone UTF-16 surrogate escape at byte N` where the TypeScript
|
|
/// kept the surrogate (or, for one decoy-first file, dumped a ZodError on the decoy).
|
|
Surrogate,
|
|
/// `report version null cannot be read` where the TypeScript read `Infinity`.
|
|
Null,
|
|
}
|
|
|
|
/// A declared row: the Rust closed wording of its §A.3 class, one line, exit 2.
|
|
fn assert_declared(row: &Row, declared: &str) -> Printed {
|
|
let got = replay(row);
|
|
let name = entry_name(row);
|
|
let args = row.args.join(" ");
|
|
assert_eq!(got.stderr, "", "stderr of `{args}`");
|
|
assert_eq!(got.exit_code, 2, "`{args}`");
|
|
assert_eq!(got.stdout.matches('\n').count(), 1, "`{args}`: one line");
|
|
let invalid = format!("!! {name}: report document invalid: ");
|
|
let surrogate = format!("!! {name}: attachment refused: lone UTF-16 surrogate escape at byte ");
|
|
let printed = if got.stdout.starts_with(&invalid) {
|
|
Printed::Invalid
|
|
} else if got.stdout.starts_with(&surrogate) {
|
|
Printed::Surrogate
|
|
} else {
|
|
assert_infinity_row(row, &got, &name);
|
|
Printed::Null
|
|
};
|
|
let allowed: &[Printed] = if declared.contains("ZodError") {
|
|
&[Printed::Invalid, Printed::Surrogate]
|
|
} else if declared.contains("lone surrogate") {
|
|
&[Printed::Surrogate]
|
|
} else if declared.contains("Infinity") {
|
|
&[Printed::Null]
|
|
} else {
|
|
panic!("`{args}`: unknown declaration {declared}");
|
|
};
|
|
assert!(allowed.contains(&printed), "`{args}`: {}", got.stdout);
|
|
printed
|
|
}
|
|
|
|
/// `Infinity` read as `null` (the core's reading, the one the application's import gave on
|
|
/// the R3 path too: O8 `no_attachment`): a report version overflow is `report version null
|
|
/// cannot be read`; an envelope version overflow leaves a bare object without a version,
|
|
/// the same line, where the TypeScript CLI printed the report under a rejected envelope.
|
|
fn assert_infinity_row(row: &Row, got: &Run, name: &str) {
|
|
let args = row.args.join(" ");
|
|
let infinity = format!("!! {name}: report version Infinity cannot be read\n");
|
|
let envelope_line = " archive v1 (snapshot rejected: unsupported_envelope)\n";
|
|
assert!(
|
|
row.stdout == infinity || row.stdout.contains(envelope_line),
|
|
"`{args}`: the TypeScript line"
|
|
);
|
|
let expected = format!("!! {name}: report version null cannot be read\n");
|
|
assert_eq!(got.stdout, expected, "`{args}`");
|
|
assert_eq!(got.exit_code, 2, "`{args}`");
|
|
}
|
|
|
|
/// Every invocation of one `scripts.compare.test.ts` case, exact.
|
|
fn assert_scenario(title: &str) {
|
|
let rows: Vec<Row> = rows()
|
|
.into_iter()
|
|
.filter(|r| r.scenario.as_deref() == Some(title))
|
|
.collect();
|
|
assert!(!rows.is_empty(), "no invocation recorded for `{title}`");
|
|
for row in &rows {
|
|
assert!(
|
|
row.declared.is_none(),
|
|
"`{title}`: declared rows are not scenarios"
|
|
);
|
|
assert_exact(row);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// O10: every invocation
|
|
|
|
#[test]
|
|
fn o10_every_invocation_matches_or_prints_the_declared_wording() {
|
|
let rows = rows();
|
|
assert_eq!(rows.len(), 230);
|
|
let mut printed: HashMap<Printed, usize> = HashMap::new();
|
|
for row in &rows {
|
|
match &row.declared {
|
|
None => assert_exact(row),
|
|
Some(reason) => *printed.entry(assert_declared(row, reason)).or_default() += 1,
|
|
}
|
|
}
|
|
// 63 ZodError dumps: 62 invalid documents, one decoy-first file whose decoy holds a lone
|
|
// surrogate (refused before any schema); 6 surrogates kept or overwritten; 3 overflows.
|
|
assert_eq!(
|
|
printed[&Printed::Invalid],
|
|
62,
|
|
"the rows plan §A.3 declares"
|
|
);
|
|
assert_eq!(printed[&Printed::Surrogate], 7);
|
|
assert_eq!(printed[&Printed::Null], 3);
|
|
let groups: Vec<&str> = rows.iter().map(|r| r.group.as_str()).collect();
|
|
assert_eq!(groups.iter().filter(|g| **g == "fixture").count(), 202);
|
|
assert_eq!(groups.iter().filter(|g| **g == "folder").count(), 2);
|
|
assert_eq!(groups.iter().filter(|g| **g == "cli").count(), 4);
|
|
assert_eq!(groups.iter().filter(|g| **g == "scenario").count(), 22);
|
|
}
|
|
|
|
#[test]
|
|
fn o10_inputs_are_the_recorded_bytes() {
|
|
let inputs = oracle()["inputs"].as_object().unwrap();
|
|
assert!(inputs.len() >= 202);
|
|
for (file, digest) in inputs {
|
|
let bytes = std::fs::read(repo_root().join(file)).unwrap();
|
|
assert_eq!(sha256_hex(&bytes), digest.as_str().unwrap(), "{file}");
|
|
}
|
|
let sheet = draft_sheet();
|
|
assert_eq!(oracle()["sheet"]["sheetId"], sheet.reference.sheet_id);
|
|
assert_eq!(
|
|
oracle()["sheet"]["documentSha256"],
|
|
sheet.reference.document_sha256
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn o10_declared_rows_name_each_a3_class_at_least_once() {
|
|
let mut classes: HashMap<&str, usize> = HashMap::new();
|
|
for row in rows() {
|
|
if let Some(reason) = &row.declared {
|
|
let class = [
|
|
"ZodError",
|
|
"Infinity",
|
|
"overwritten duplicate",
|
|
"keeps a lone surrogate",
|
|
]
|
|
.into_iter()
|
|
.find(|c| reason.contains(c))
|
|
.unwrap_or_else(|| panic!("unknown declaration {reason}"));
|
|
*classes.entry(class).or_default() += 1;
|
|
}
|
|
}
|
|
assert_eq!(classes["ZodError"], 63);
|
|
assert_eq!(classes["Infinity"], 3);
|
|
assert_eq!(classes["keeps a lone surrogate"], 3);
|
|
assert_eq!(classes["overwritten duplicate"], 3);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The 11 cases of scripts.compare.test.ts (ledger targets)
|
|
|
|
#[test]
|
|
fn frozen_v2_archive_pdf_shows_every_legacy_item_next_to_its_target() {
|
|
assert_scenario(
|
|
"compareReport on the frozen v2 archive PDF > shows every legacy zone and finding next to its canonical target",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unresolved_items_name_a_removed_an_unknown_finding_and_an_unknown_zone() {
|
|
assert_scenario(
|
|
"compareReport on unresolved items > names a removed finding, a finding unknown to the catalog and an unknown zone",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn v3_report_read_as_is_flags_what_the_sheet_cannot_place() {
|
|
assert_scenario(
|
|
"compareReport on unresolved items > reads a v3 report as is and flags what the sheet cannot place",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn envelope_archive_line_says_snapshot_kept_or_rejected_with_its_reason() {
|
|
assert_scenario(
|
|
"compareReport on unresolved items > prints the archive line of an envelope: snapshot kept, or rejected with its reason",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn v3_report_written_on_another_sheet_document_warns() {
|
|
assert_scenario(
|
|
"compareReport on unresolved items > warns when a v3 report was written on another sheet document",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn diff_of_a_migrated_v2_report_and_its_v3_re_reading() {
|
|
assert_scenario(
|
|
"diff and arguments > diffs the v3 content of a migrated v2 report and its v3 re-reading",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn diff_compares_placeholders_by_default_and_real_texts_with_full() {
|
|
assert_scenario(
|
|
"diff and arguments > compares placeholders by default and real texts with --full",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn arguments_parsed_and_unknown_options_refused() {
|
|
assert_scenario("diff and arguments > parses files and options, refuses unknown ones");
|
|
let argv = |list: &[&str]| -> Vec<String> { list.iter().map(|s| (*s).to_owned()).collect() };
|
|
assert_eq!(
|
|
parse_args(&argv(&["a.pdf", "--summary", "dir", "--locale", "en"])),
|
|
Ok(CliArgs {
|
|
files: vec!["a.pdf".to_owned(), "dir".to_owned()],
|
|
locale: Locale::En,
|
|
full: false,
|
|
summary: true,
|
|
diff: false,
|
|
})
|
|
);
|
|
assert!(
|
|
parse_args(&argv(&["--full", "--diff", "a", "b"]))
|
|
.unwrap()
|
|
.full
|
|
);
|
|
assert_eq!(
|
|
parse_args(&argv(&["--identity"])),
|
|
Err("unknown option --identity".to_owned())
|
|
);
|
|
assert_eq!(
|
|
parse_args(&argv(&["--locale", "de"])),
|
|
Err("--locale takes fr or en".to_owned())
|
|
);
|
|
assert_eq!(
|
|
parse_args(&argv(&["--json"])),
|
|
Err("unknown option --json".to_owned())
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn control_characters_escaped_in_every_untrusted_string() {
|
|
assert_scenario(
|
|
"terminal safety and privacy > escapes control characters in every untrusted string",
|
|
);
|
|
assert_eq!(esc("a\u{1b}[2K\nb"), "a\\u001b[2K\\u000ab");
|
|
assert_eq!(
|
|
esc("\u{9b}\u{202e}\u{2028}\u{2029}"),
|
|
"\\u009b\\u202e\\u2028\\u2029"
|
|
);
|
|
assert_eq!(esc("\u{e0001}\u{feff}"), "\\ue0001\\ufeff");
|
|
assert_eq!(esc("plain é 漢 😀"), "plain é 漢 😀");
|
|
}
|
|
|
|
#[test]
|
|
fn prototype_chain_names_as_legacy_ids_do_not_throw() {
|
|
assert_scenario(
|
|
"terminal safety and privacy > does not throw on prototype-chain names used as legacy ids",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn names_and_free_texts_hidden_by_default_shown_with_full() {
|
|
assert_scenario(
|
|
"terminal safety and privacy > hides names and free texts by default, shows them with --full",
|
|
);
|
|
let lines = |list: &[&str]| -> Vec<String> { list.iter().map(|s| (*s).to_owned()).collect() };
|
|
assert_eq!(
|
|
diff_lines(&lines(&["a", "b", "b"]), &lines(&["b", "c"])),
|
|
lines(&["- a", "+ c"])
|
|
);
|
|
assert!(diff_lines(&lines(&["a"]), &lines(&["a"])).is_empty());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// The binary
|
|
|
|
fn spawn(args: &[String]) -> Run {
|
|
let output = Command::new(env!("CARGO_BIN_EXE_report-compare"))
|
|
.args(args)
|
|
.env("INIT_CWD", repo_root())
|
|
.current_dir(PathBuf::from(env!("CARGO_MANIFEST_DIR")))
|
|
.output()
|
|
.unwrap();
|
|
Run {
|
|
stdout: String::from_utf8(output.stdout).unwrap(),
|
|
stderr: String::from_utf8(output.stderr).unwrap(),
|
|
exit_code: output.status.code().unwrap(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn binary_prints_what_the_pure_run_prints_from_init_cwd() {
|
|
let rows = rows();
|
|
let shapes = ["cli", "folder"];
|
|
let mut spawned = 0;
|
|
for row in rows.iter().filter(|r| shapes.contains(&r.group.as_str())) {
|
|
// The binary reads the committed (switched) seed: the oracle under the declared rule.
|
|
let got = spawn(&row.args);
|
|
let args = row.args.join(" ");
|
|
assert_eq!(
|
|
got.stdout,
|
|
r5_6_rule(&row.stdout),
|
|
"binary stdout of `{args}`"
|
|
);
|
|
assert_eq!(got.stderr, row.stderr, "binary stderr of `{args}`");
|
|
assert_eq!(got.exit_code, row.exit_code, "binary exit code of `{args}`");
|
|
spawned += 1;
|
|
}
|
|
assert_eq!(spawned, 6);
|
|
// A fixture typed from the root, a folder with mixed options, and a missing entry.
|
|
let scenario = rows
|
|
.iter()
|
|
.find(|r| r.args.first().map(String::as_str) == Some("a.pdf"))
|
|
.unwrap();
|
|
assert_eq!(spawn(&scenario.args), replay_switched(scenario));
|
|
let full = rows
|
|
.iter()
|
|
.find(|r| r.args.first().map(String::as_str) == Some("--full") && r.args.len() == 2)
|
|
.unwrap();
|
|
assert_eq!(spawn(&full.args), replay_switched(full));
|
|
}
|
|
|
|
/// After R5.6, on the switched seed: every exact O10 row prints the oracle under the declared
|
|
/// rule (the one sheet line), stderr and exit codes unchanged.
|
|
#[test]
|
|
fn o10_on_the_switched_seed_differs_only_by_the_declared_alias_line() {
|
|
let mut aliased = 0;
|
|
for row in rows().iter().filter(|r| r.declared.is_none()) {
|
|
let got = replay_switched(row);
|
|
let args = row.args.join(" ");
|
|
let expected = r5_6_rule(&row.stdout);
|
|
aliased += usize::from(expected != row.stdout);
|
|
assert_eq!(got.stdout, expected, "stdout of `{args}`");
|
|
assert_eq!(got.stderr, row.stderr, "stderr of `{args}`");
|
|
assert_eq!(got.exit_code, row.exit_code, "exit code of `{args}`");
|
|
}
|
|
assert_eq!(aliased, 77, "rows through the alias");
|
|
assert!(switched_sheet().is_fingerprint_verified() && !draft_sheet().is_fingerprint_verified());
|
|
}
|