Files
bchanot 9e87016399 feat(domain): R5.6 fingerprint switch to sha256-rfc8785-validated-v1
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.
2026-10-25 02:02:42 +01:00

364 lines
15 KiB
Rust

//! R4.2d: the attachment probe and the PDF import against the frozen TypeScript outcomes.
//!
//! O7 (`oracles/r4/read.json`, `packages/pdf/tests/r4.reader-oracles.test.ts`): per input,
//! `locateAttachment` / `readAttachedDocument` (candidate index, name, data offset, bytes
//! digest, digest of `JSON.stringify` of the parsed value) and `readReportArchive` on the
//! draft migration map (origin, counters, snapshot rejection, report and archive digests).
//! O8 (`oracles/r4/tolerance.json`, `apps/app/tests/unit/r4.tolerance-oracles.test.ts`): the
//! full R3 import path (scan → core `archive.import` on the bundled draft sheet) on 51 valid
//! and hostile PDFs, compared with `import_pdf` and with the facade envelope.
//!
//! Differences the plan declares (§A.3, OD-4) are listed in `declared()` with the Rust
//! outcome; any other difference fails. The cap is off (`ProbeLimits::UNBOUNDED`) so that the
//! comparison measures the TypeScript behaviour; the OD-3 cap is measured in R4.2e.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::r4::{r4_input, r4_oracle, repo_root, sha256_hex};
use faunosteo_core::domain::archive::read_archive_document;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
use faunosteo_core::facade;
use faunosteo_core::json::to_js_compact;
use faunosteo_core::pdf::import::{PdfImportFailure, PdfImportKind, import_pdf};
use faunosteo_core::pdf::read::probe::{
ProbeLimits, candidate_bytes, locate_attachment, read_attached_document,
};
use faunosteo_core::pdf::read::scan::find_embedded_files;
use serde_json::{Value, json};
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
/// The bundled draft sheet exactly as `apps/app/vite/sheet-bundle.ts` embeds it.
fn draft_sheet() -> LoadedSheet {
sheet_with(&repo_root().join(SEED))
}
/// The sheet the R4 oracles were frozen against (before R5.6): the same document and drawing,
/// with the pre-switch `sheet-ref.json` and migration map (`tests/fixtures/r5-6/pre-switch`,
/// proven to be the TypeScript's files by `curation_draft`).
fn historical_sheet() -> LoadedSheet {
sheet_with(&repo_root().join("crates/core/tests/fixtures/r5-6/pre-switch"))
}
/// The seed's document and drawings, with `sheet-ref.json` and the migration map from `refs`.
fn sheet_with(refs: &std::path::Path) -> LoadedSheet {
let seed = repo_root().join(SEED);
let read = |dir: &std::path::Path, name: &str| std::fs::read_to_string(dir.join(name)).unwrap();
let manifest: Value = serde_json::from_str(&read(&seed, "assets.json")).unwrap();
let mut assets = serde_json::Map::new();
let mut hashes = serde_json::Map::new();
for asset in manifest.as_array().unwrap() {
let id = asset["svg_asset_id"].as_str().unwrap().to_owned();
let file = format!("assets/{}", asset["file"].as_str().unwrap());
assets.insert(id.clone(), Value::String(read(&seed, &file)));
hashes.insert(id, asset["sha256"].clone());
}
let payload = json!({
"status": "draft",
"ref": parse_json(&read(refs, "sheet-ref.json")).unwrap(),
"document": parse_json(&read(&seed, "document.json")).unwrap(),
"migrationMap": parse_json(&read(refs, "report-migration-map.json")).unwrap(),
"assets": assets,
"assetHashes": hashes,
});
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
LoadedSheet::from_bundle(data).unwrap()
}
fn digest_js(value: &impl serde::Serialize) -> String {
sha256_hex(to_js_compact(value).unwrap().as_bytes())
}
/// The `file` an O7 row loads (a generator row names its description).
fn input_bytes(row: &Value) -> Vec<u8> {
r4_input("read", row["file"].as_str().unwrap())
}
// ---------------------------------------------------------------------------
// O7: locate and read
/// `{ ok: true, candidate, name, dataOffset, bytesLength, bytesSha256, objectSha256 }` or
/// `{ ok: false, kind }`, as the TypeScript recorded `locateAttachment`.
fn located_row(
bytes: &[u8],
located: Result<
&faunosteo_core::pdf::read::probe::LocatedAttachment<'_>,
&faunosteo_core::pdf::read::ReadKind,
>,
) -> Value {
match located {
Err(kind) => json!({ "ok": false, "kind": kind.as_str() }),
Ok(found) => {
let candidates = find_embedded_files(bytes);
let data = candidate_bytes(bytes, &candidates[found.index], usize::MAX).unwrap();
// The TypeScript digested `JSON.stringify(JSON.parse(text))`; the core reads the
// same text (lone surrogate inputs are the declared OD-4 rows, listed below).
let object = parse_json(&found.text).map(|value| digest_js(&value)).ok();
json!({
"ok": true,
"candidate": found.index,
"name": found.candidate.name,
"dataOffset": found.candidate.offset,
"bytesLength": found.data_len,
"bytesSha256": sha256_hex(&data),
"objectSha256": object,
})
}
}
}
/// `readReportArchive` on the draft map: the layered reading before any rebase.
fn archive_row(text: &str, sheet: &LoadedSheet) -> Value {
let Ok(data) = parse_json(text) else {
return json!({ "ok": false, "kind": "lone_surrogate_or_syntax" });
};
match read_archive_document(&data, &sheet.migration_map) {
Ok(read) => json!({
"ok": true,
"origin": read.origin,
"migrated": read.migrated,
"unresolvedZones": read.unresolved_zones,
"unresolvedFindings": read.unresolved_findings,
"snapshotRejected": read.snapshot_rejected,
"reportSha256": digest_js(&read.report),
// The frozen TS domain stringified its Zod envelope output, keys in schema
// order; the core keeps the document's key order (R2, what the R3 application
// stored and what O8 pins). Presence is compared, not the digest.
"archiveSha256": read.archive.as_ref().map(|_| "present"),
}),
Err(faunosteo_core::domain::report::ReadFailure::UnsupportedVersion(Some(found))) => {
json!({ "ok": false, "kind": "incompatible_version", "foundVersion": found })
}
Err(_) => json!({ "ok": false, "kind": "no_attachment" }),
}
}
#[test]
fn o7_locate_and_read_match_the_typescript() {
let oracle = r4_oracle("read.json");
// In its original context: the oracle froze the reports a v2 migration stamps with the
// pre-switch map (R5.6 leaves it untouched; the post-switch stamp has its own test below).
let sheet = historical_sheet();
let (mut inputs, mut located, mut archives, mut mismatches) = (0, 0, 0, Vec::new());
for row in oracle["inputs"].as_array().unwrap() {
if row["kind"] != "pdf" {
continue;
}
inputs += 1;
let file = row["file"].as_str().unwrap();
let bytes = input_bytes(row);
let outcomes = &row["outcomes"];
if let Some(expected) = outcomes.get("locateAttachment") {
located += 1;
let found = locate_attachment(&bytes, ProbeLimits::UNBOUNDED);
let actual = located_row(&bytes, found.as_ref());
compare(&mut mismatches, file, "locateAttachment", expected, &actual);
}
let read = read_attached_document(&bytes, ProbeLimits::UNBOUNDED);
if let Some(expected) = outcomes.get("readAttachedDocument") {
let actual = located_row(&bytes, read.as_ref());
compare(
&mut mismatches,
file,
"readAttachedDocument",
expected,
&actual,
);
}
if let Some(expected) = outcomes.get("readReportArchive") {
archives += 1;
let actual = match &read {
Err(kind) => json!({ "ok": false, "kind": kind.as_str() }),
Ok(found) => archive_row(&found.text, &sheet),
};
let mut expected = expected.clone();
if let Some(digest) = expected.get_mut("archiveSha256")
&& !digest.is_null()
{
*digest = Value::from("present");
}
compare(
&mut mismatches,
file,
"readReportArchive",
&expected,
&actual,
);
}
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
assert_eq!(inputs, 144);
println!("O7: {inputs} inputs, {located} locate rows, {archives} archive rows");
}
/// After R5.6 a v2 migration stamps the canonical value; everything else is what O7 froze.
#[test]
fn o7_after_r5_6_migrated_v2_reports_carry_the_canonical_stamp_and_nothing_else_moves() {
let (now, then) = (draft_sheet(), historical_sheet());
for file in ["scan.report-v2-full.pdf", "scan.report-v2-minimal.pdf"] {
let bytes = r4_input("read", file);
let text = read_attached_document(&bytes, ProbeLimits::UNBOUNDED)
.unwrap()
.text
.into_owned();
let data = parse_json(&text).unwrap();
let after = read_archive_document(&data, &now.migration_map).unwrap();
let before = read_archive_document(&data, &then.migration_map).unwrap();
assert_eq!(
after.report.sheet.document_sha256, now.reference.document_sha256,
"{file}"
);
assert_eq!(
before.report.sheet.document_sha256, then.reference.document_sha256,
"{file}"
);
let mut aligned = after.report.clone();
aligned.sheet.document_sha256 = before.report.sheet.document_sha256.clone();
assert_eq!(aligned, before.report, "{file}");
assert_eq!(
(
after.migrated,
after.unresolved_zones,
after.unresolved_findings
),
(
before.migrated,
before.unresolved_zones,
before.unresolved_findings
),
"{file}"
);
}
}
fn compare(
mismatches: &mut Vec<String>,
file: &str,
field: &str,
expected: &Value,
actual: &Value,
) {
// Compared in the JavaScript spelling: `4` and `4.0` are one number there.
if to_js_compact(expected).unwrap() != to_js_compact(actual).unwrap() {
mismatches.push(format!(
"{file} {field}\n expected {expected}\n actual {actual}"
));
}
}
// ---------------------------------------------------------------------------
// O8: the full import path
/// The O8 outcome of one PDF through `import_pdf`, in the oracle's shape.
fn o8_outcome(sheet: &LoadedSheet, pdf: &[u8]) -> Value {
match import_pdf(sheet, pdf, ProbeLimits::UNBOUNDED) {
Ok(import) => json!({
"ok": true,
"reportSha256": digest_js(&import.rebase.report),
"archiveSha256": import.read.archive.as_ref().map(digest_js),
"conclusion": import.rebase.report.conclusion,
"read": {
"origin": import.read.origin,
"migrated": import.read.migrated,
"unresolvedZones": import.read.unresolved_zones,
"unresolvedFindings": import.read.unresolved_findings,
"snapshotRejected": import.read.snapshot_rejected,
},
"rebase": {
"rebased": import.rebase.rebased,
"unresolvedZones": import.rebase.unresolved_zones,
"unresolvedFindings": import.rebase.unresolved_findings,
},
}),
Err(PdfImportFailure::Import(
faunosteo_core::domain::session::ImportFailure::IncompatibleVersion(found),
)) => json!({ "ok": false, "kind": "incompatible_version", "foundVersion": found }),
Err(failure) => json!({ "ok": false, "kind": PdfImportKind::from(&failure).as_str() }),
}
}
/// O8 cases whose Rust outcome differs on purpose (plan §A.3, OD-4: a lone surrogate escape
/// in a value a duplicate key overwrote imports in JavaScript, which dropped it when it
/// stringified the parsed object; the core refuses the text whole, nothing altered).
fn declared_o8(id: &str) -> Option<Value> {
match id {
"surrogate-content-overwritten"
| "surrogate-version-overwritten"
| "two-files-decoy-first" => Some(json!({ "ok": false, "kind": "lone_surrogate" })),
_ => None,
}
}
#[test]
fn o8_import_matches_the_r3_path_outside_the_declared_differences() {
let oracle = r4_oracle("tolerance.json");
let sheet = draft_sheet();
let mut mismatches = Vec::new();
let (mut cases, mut declared) = (0, 0);
for case in oracle["cases"].as_array().unwrap() {
cases += 1;
let id = case["id"].as_str().unwrap();
let path = repo_root().join(case["pdf"].as_str().unwrap());
let pdf = std::fs::read(&path).unwrap();
assert_eq!(sha256_hex(&pdf), case["pdfSha256"], "{id}: fixture bytes");
let actual = o8_outcome(&sheet, &pdf);
let expected = match declared_o8(id) {
Some(rust) => {
declared += 1;
assert_ne!(
rust, case["outcome"],
"{id}: declared difference is not one"
);
rust
}
None => case["outcome"].clone(),
};
compare(&mut mismatches, id, "outcome", &expected, &actual);
}
assert!(mismatches.is_empty(), "{}", mismatches.join("\n"));
assert_eq!(cases, 51);
assert_eq!(declared, 3);
}
/// O8 cases the application's caps refuse (OD-3, `MAX_JSON_VALUES`): a token-dense
/// document is skipped like one nested too deep, so the kind follows the stamp, as the
/// depth cap's does today (`depth-65` is `attachment_stripped` in O8).
fn declared_od3(id: &str) -> Option<Value> {
match id {
"values-1m" => Some(json!({ "ok": false, "kind": "attachment_stripped" })),
_ => None,
}
}
#[test]
fn the_facade_envelope_carries_the_closed_kinds() {
let sheet = draft_sheet();
let oracle = r4_oracle("tolerance.json");
for case in oracle["cases"].as_array().unwrap() {
let id = case["id"].as_str().unwrap();
let pdf = std::fs::read(repo_root().join(case["pdf"].as_str().unwrap())).unwrap();
let envelope: Value = serde_json::from_str(&facade::import_pdf(&sheet, &pdf)).unwrap();
let expected = declared_od3(id)
.or_else(|| declared_o8(id))
.unwrap_or_else(|| case["outcome"].clone());
assert_eq!(envelope["ok"], expected["ok"], "{id}");
if expected["ok"] == false {
assert_eq!(envelope["kind"], expected["kind"], "{id}");
assert_eq!(
envelope.get("found").cloned(),
expected.get("foundVersion").cloned(),
"{id}"
);
} else {
assert_eq!(
envelope["value"]["rebase"]["report"]["conclusion"], expected["conclusion"],
"{id}"
);
}
}
let not_a_pdf = facade::import_pdf(&sheet, b"hello");
assert!(not_a_pdf.contains(r#""kind":"invalid_pdf""#), "{not_a_pdf}");
}