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.
433 lines
17 KiB
Rust
433 lines
17 KiB
Rust
//! The R5.6 switch on the switched seed (owner 2026-10-24 « corrections préalables à la
|
|
//! bascule » §1, M1 option 1): an old PDF stamped with the registered historical value opens
|
|
//! with no rebase; re-exported, its snapshot keeps the report's stamp, so the current reader,
|
|
//! the pre-switch bundle (rollback) and the frozen N-1 reader (vitest
|
|
//! `packages/core-wasm/tests/r5-6.n-1.test.ts`) all keep the snapshot; archived labels, views,
|
|
//! annotations and branding survive; forged or unknown values and a real content change keep
|
|
//! their behaviour; the sheet load refuses a contradicting reference.
|
|
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod archive_support;
|
|
mod common;
|
|
|
|
use archive_support::{draft_sheet, from, json_file, loaded};
|
|
use faunosteo_core::domain::archive::build_sheet_snapshot;
|
|
use faunosteo_core::domain::session::import_archive;
|
|
use faunosteo_core::domain::sheet::fingerprint::{TRUSTED_ALIASES, document_fingerprint};
|
|
use faunosteo_core::domain::sheet::{BundledSheetData, LoadError, LoadedSheet};
|
|
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
|
|
use faunosteo_core::pdf::write::export::export_pdf;
|
|
use faunosteo_core::pdf::write::input::{ExportInput, Rasters};
|
|
use serde_json::{Value, json};
|
|
|
|
const OLD_PDF: &str = "apps/app/tests/fixtures/pdf/r3/demo.fr.nologos.pdf";
|
|
const DRAFT: &str = "supabase/seed/anatomical-sheets/equine/draft";
|
|
const PRE_SWITCH: &str = "crates/core/tests/fixtures/r5-6/pre-switch";
|
|
|
|
fn alias() -> &'static str {
|
|
TRUSTED_ALIASES[0].alias
|
|
}
|
|
|
|
fn canonical() -> &'static str {
|
|
TRUSTED_ALIASES[0].canonical
|
|
}
|
|
|
|
fn attachment(pdf: &[u8]) -> Value {
|
|
let text = read_attached_document(pdf, ProbeLimits::UNBOUNDED)
|
|
.unwrap()
|
|
.text
|
|
.into_owned();
|
|
serde_json::from_str(&text).unwrap()
|
|
}
|
|
|
|
fn old_envelope() -> Value {
|
|
attachment(&std::fs::read(common::repo_path(OLD_PDF)).unwrap())
|
|
}
|
|
|
|
/// The bundle with its reference and migration map replaced (the document and drawings stay).
|
|
fn sheet_with_ref(reference: Value, map: Value) -> Result<LoadedSheet, String> {
|
|
let base = draft_sheet();
|
|
let assets: serde_json::Map<String, Value> = base
|
|
.assets
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let hashes: serde_json::Map<String, Value> = base
|
|
.asset_hashes
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let data: BundledSheetData = serde_json::from_value(json!({
|
|
"status": "draft",
|
|
"ref": reference,
|
|
"document": json_file(&format!("{DRAFT}/document.json")),
|
|
"migrationMap": map,
|
|
"assets": assets,
|
|
"assetHashes": hashes,
|
|
}))
|
|
.map_err(|e| format!("shape: {e}"))?;
|
|
LoadedSheet::from_bundle(data).map_err(|e: LoadError| format!("{e:?}"))
|
|
}
|
|
|
|
/// The pre-switch bundle: what a rollback build ships.
|
|
fn rollback_sheet() -> LoadedSheet {
|
|
sheet_with_ref(
|
|
json_file(&format!("{PRE_SWITCH}/sheet-ref.json")),
|
|
json_file(&format!("{PRE_SWITCH}/report-migration-map.json")),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
fn reexport(sheet: &LoadedSheet, envelope: &Value) -> Vec<u8> {
|
|
let report = import_archive(sheet, &envelope.to_string())
|
|
.unwrap()
|
|
.rebase
|
|
.report;
|
|
let input = ExportInput {
|
|
sheet_snapshot: serde_json::to_value(build_sheet_snapshot(sheet, &report)).unwrap(),
|
|
report,
|
|
branding: envelope["branding"].clone(),
|
|
labels: envelope["labels"].clone(),
|
|
locale: from(envelope["locale"].clone()),
|
|
exported_at: "2026-10-24T12:00:00.000Z".into(),
|
|
};
|
|
export_pdf(input, &Rasters::default()).unwrap().pdf
|
|
}
|
|
|
|
#[test]
|
|
fn the_switched_seed_is_v1_and_the_old_pdf_carries_the_alias() {
|
|
let sheet = draft_sheet();
|
|
assert_eq!(sheet.reference.document_sha256, canonical());
|
|
assert_eq!(document_fingerprint(&sheet.document).unwrap(), canonical());
|
|
let envelope = old_envelope();
|
|
assert_eq!(envelope["report"]["sheet"]["documentSha256"], alias());
|
|
assert_eq!(envelope["sheetSnapshot"]["ref"]["documentSha256"], alias());
|
|
}
|
|
|
|
#[test]
|
|
fn an_old_pdf_opens_with_no_rebase_and_keeps_its_archive_and_stamp() {
|
|
let import = import_archive(&draft_sheet(), &old_envelope().to_string()).unwrap();
|
|
assert!(!import.rebase.rebased);
|
|
assert_eq!(import.rebase.report.sheet.document_sha256, alias());
|
|
assert!(import.read.archive.is_some() && import.read.snapshot_rejected.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_reexport_writes_one_stamp_that_every_reader_keeps() {
|
|
let envelope = old_envelope();
|
|
let pdf = reexport(&draft_sheet(), &envelope);
|
|
let written = attachment(&pdf);
|
|
assert_eq!(written["report"]["sheet"]["documentSha256"], alias());
|
|
assert_eq!(written["sheetSnapshot"]["ref"]["documentSha256"], alias());
|
|
// The current reader, then the pre-switch bundle (rollback): snapshot kept, no rebase.
|
|
for sheet in [draft_sheet(), rollback_sheet()] {
|
|
let import = import_archive(&sheet, &written.to_string()).unwrap();
|
|
assert!(
|
|
import.read.archive.is_some(),
|
|
"{:?}",
|
|
import.read.snapshot_rejected
|
|
);
|
|
assert!(!import.rebase.rebased);
|
|
}
|
|
// Kept for the rollback check with the pre-switch build's own `report-compare`.
|
|
let out = std::path::PathBuf::from(common::repo_path("target/tmp/r5-6"));
|
|
std::fs::create_dir_all(&out).unwrap();
|
|
std::fs::write(out.join("reexport.demo.fr.nologos.pdf"), &pdf).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn a_reexport_preserves_the_archived_labels_views_annotations_and_branding() {
|
|
let envelope = old_envelope();
|
|
let written = attachment(&reexport(&draft_sheet(), &envelope));
|
|
for key in ["labels", "branding", "locale", "report"] {
|
|
assert_eq!(written[key], envelope[key], "{key}");
|
|
}
|
|
let (before, after) = (&envelope["sheetSnapshot"], &written["sheetSnapshot"]);
|
|
assert_eq!(
|
|
after["document"], before["document"],
|
|
"views, annotations, findings, labels"
|
|
);
|
|
assert_eq!(after["assets"], before["assets"]);
|
|
assert_eq!(after["ref"], before["ref"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_new_report_still_carries_the_canonical_value() {
|
|
let sheet = draft_sheet();
|
|
let mut envelope = old_envelope();
|
|
envelope["report"]["sheet"]["documentSha256"] = json!(canonical());
|
|
envelope["sheetSnapshot"]["ref"]["documentSha256"] = json!(canonical());
|
|
let pdf = reexport(&sheet, &envelope);
|
|
let written = attachment(&pdf);
|
|
assert_eq!(written["report"]["sheet"]["documentSha256"], canonical());
|
|
assert_eq!(
|
|
written["sheetSnapshot"]["ref"]["documentSha256"],
|
|
canonical()
|
|
);
|
|
// Kept for the rollback check of a NEW report with the pre-switch build (RUST.md §11.22).
|
|
let out = std::path::PathBuf::from(common::repo_path("target/tmp/r5-6"));
|
|
std::fs::create_dir_all(&out).unwrap();
|
|
std::fs::write(out.join("new-report.v1.demo.fr.nologos.pdf"), &pdf).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn an_unknown_or_forged_stamp_rebases_and_a_mixed_envelope_loses_its_snapshot() {
|
|
let sheet = draft_sheet();
|
|
let mut envelope = old_envelope();
|
|
envelope["report"]["sheet"]["documentSha256"] = json!("c".repeat(64));
|
|
envelope["sheetSnapshot"]["ref"]["documentSha256"] = json!("c".repeat(64));
|
|
assert!(
|
|
import_archive(&sheet, &envelope.to_string())
|
|
.unwrap()
|
|
.rebase
|
|
.rebased
|
|
);
|
|
// A case-changed canonical value is no 64-lowercase-hex stamp: the v3 contract refuses it.
|
|
let mut upper = old_envelope();
|
|
upper["report"]["sheet"]["documentSha256"] = json!(canonical().to_uppercase());
|
|
assert!(import_archive(&sheet, &upper.to_string()).is_err());
|
|
// An archive pairing the alias with the canonical value is not loosened into a match.
|
|
let mut mixed = old_envelope();
|
|
mixed["sheetSnapshot"]["ref"]["documentSha256"] = json!(canonical());
|
|
let import = import_archive(&sheet, &mixed.to_string()).unwrap();
|
|
assert!(import.read.archive.is_none() && import.read.snapshot_rejected.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn a_real_content_change_still_rebases() {
|
|
let mut document = json_file(&format!("{DRAFT}/document.json"));
|
|
document["name"]["fr"] = json!("Fiche équine modifiée");
|
|
let typed = from(document.clone());
|
|
let changed = document_fingerprint(&typed).unwrap();
|
|
assert_ne!(changed, canonical());
|
|
let mut reference = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
reference["document_sha256"] = json!(changed);
|
|
reference["fingerprint"]["aliases"] = json!([]);
|
|
let base = draft_sheet();
|
|
let assets: serde_json::Map<String, Value> = base
|
|
.assets
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let hashes: serde_json::Map<String, Value> = base
|
|
.asset_hashes
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let sheet = loaded(
|
|
reference,
|
|
document,
|
|
json_file(&format!("{DRAFT}/report-migration-map.json")),
|
|
Value::Object(assets),
|
|
Value::Object(hashes),
|
|
);
|
|
let import = import_archive(&sheet, &old_envelope().to_string()).unwrap();
|
|
assert!(import.rebase.rebased);
|
|
}
|
|
|
|
#[test]
|
|
fn the_sheet_load_refuses_a_reference_that_contradicts_the_registry_or_the_content() {
|
|
let map = json_file(&format!("{DRAFT}/report-migration-map.json"));
|
|
let good = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
assert!(sheet_with_ref(good.clone(), map.clone()).is_ok());
|
|
let mut cases = Vec::new();
|
|
let mut forged = good.clone();
|
|
forged["fingerprint"]["aliases"] = json!(["d".repeat(64)]);
|
|
cases.push(forged);
|
|
let mut convention = good.clone();
|
|
convention["fingerprint"]["convention"] = json!("sha256-rfc8785-validated-v2");
|
|
cases.push(convention);
|
|
let mut content = good.clone();
|
|
content["document_sha256"] = json!("e".repeat(64));
|
|
content["fingerprint"]["aliases"] = json!([]);
|
|
cases.push(content);
|
|
let mut unknown_key = good.clone();
|
|
unknown_key["fingerprint"]["trusted"] = json!(true);
|
|
cases.push(unknown_key);
|
|
for reference in cases {
|
|
assert!(
|
|
sheet_with_ref(reference.clone(), map.clone()).is_err(),
|
|
"{reference}"
|
|
);
|
|
}
|
|
// A historical v0 reference (no block) still loads, unverified, with no alias privilege,
|
|
// even when it carries the canonical value: an old stamp then rebases.
|
|
let rollback = sheet_with_ref(
|
|
json_file(&format!("{PRE_SWITCH}/sheet-ref.json")),
|
|
map.clone(),
|
|
);
|
|
assert!(!rollback.unwrap().is_fingerprint_verified());
|
|
let mut v0_with_v1_value = good.clone();
|
|
v0_with_v1_value
|
|
.as_object_mut()
|
|
.unwrap()
|
|
.remove("fingerprint");
|
|
let unverified = sheet_with_ref(v0_with_v1_value, map).unwrap();
|
|
assert!(!unverified.is_fingerprint_verified());
|
|
let import = import_archive(&unverified, &old_envelope().to_string()).unwrap();
|
|
assert!(import.rebase.rebased);
|
|
assert!(draft_sheet().is_fingerprint_verified());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// M1 correction (owner 2026-10-25): the direct APIs, not only the application's paths.
|
|
|
|
fn old_report() -> faunosteo_core::domain::report::Report {
|
|
from(old_envelope()["report"].clone())
|
|
}
|
|
|
|
/// The direct export: a snapshot built by the core for `sheet`, then `export_pdf`.
|
|
fn export_on(
|
|
sheet: &LoadedSheet,
|
|
report: &faunosteo_core::domain::report::Report,
|
|
) -> Result<Vec<u8>, String> {
|
|
let envelope = old_envelope();
|
|
let input = ExportInput {
|
|
sheet_snapshot: serde_json::to_value(build_sheet_snapshot(sheet, report)).unwrap(),
|
|
report: report.clone(),
|
|
branding: envelope["branding"].clone(),
|
|
labels: envelope["labels"].clone(),
|
|
locale: from(envelope["locale"].clone()),
|
|
exported_at: "2026-10-25T12:00:00.000Z".into(),
|
|
};
|
|
export_pdf(input, &Rasters::default())
|
|
.map(|r| r.pdf)
|
|
.map_err(|e| format!("{e:?}"))
|
|
}
|
|
|
|
#[test]
|
|
fn the_direct_export_on_an_unverified_v0_sheet_with_the_canonical_value_is_refused() {
|
|
let map = json_file(&format!("{DRAFT}/report-migration-map.json"));
|
|
let mut v0 = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
v0.as_object_mut().unwrap().remove("fingerprint");
|
|
let sheet = sheet_with_ref(v0, map).unwrap();
|
|
assert!(!sheet.is_fingerprint_verified());
|
|
let snapshot = build_sheet_snapshot(&sheet, &old_report());
|
|
assert_eq!(snapshot.reference.document_sha256, canonical());
|
|
let refused = export_on(&sheet, &old_report()).unwrap_err();
|
|
assert!(refused.contains("SheetMismatch"), "{refused}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_mutated_document_or_reference_never_keeps_the_alias() {
|
|
let mut document = draft_sheet();
|
|
document.document.name.fr = "Fiche modifiée après chargement".into();
|
|
assert!(!document.recognises_historical_stamp(&document.reference.sheet_id.clone(), alias()));
|
|
assert!(export_on(&document, &old_report()).is_err());
|
|
let mut reference = draft_sheet();
|
|
reference.reference.document_sha256 = "f".repeat(64);
|
|
assert!(!reference.recognises_historical_stamp(&reference.reference.sheet_id.clone(), alias()));
|
|
assert!(export_on(&reference, &old_report()).is_err());
|
|
// Untouched, the same sheet grants it, and the direct export writes one stamp.
|
|
let sheet = draft_sheet();
|
|
assert!(sheet.recognises_historical_stamp(&sheet.reference.sheet_id, alias()));
|
|
let written = attachment(&export_on(&sheet, &old_report()).unwrap());
|
|
assert_eq!(written["sheetSnapshot"]["ref"]["documentSha256"], alias());
|
|
}
|
|
|
|
#[test]
|
|
fn the_historical_value_on_another_sheet_id_is_not_recognised() {
|
|
let sheet = draft_sheet();
|
|
assert!(!sheet.recognises_historical_stamp("00000000-0000-0000-0000-000000000000", alias()));
|
|
let mut report = old_report();
|
|
report.sheet.sheet_id = "00000000-0000-0000-0000-000000000000".into();
|
|
assert_eq!(
|
|
build_sheet_snapshot(&sheet, &report)
|
|
.reference
|
|
.document_sha256,
|
|
canonical()
|
|
);
|
|
assert!(export_on(&sheet, &report).is_err());
|
|
let import_other = {
|
|
let mut envelope = old_envelope();
|
|
envelope["report"]["sheet"]["sheetId"] = json!("00000000-0000-0000-0000-000000000000");
|
|
import_archive(&sheet, &envelope.to_string()).unwrap()
|
|
};
|
|
assert!(import_other.rebase.rebased);
|
|
}
|
|
|
|
#[test]
|
|
fn a_v1_block_whose_value_contradicts_the_content_is_refused_at_load() {
|
|
let mut document = json_file(&format!("{DRAFT}/document.json"));
|
|
document["name"]["fr"] = json!("Contenu modifié, tampon conservé");
|
|
let base = draft_sheet();
|
|
let assets: serde_json::Map<String, Value> = base
|
|
.assets
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let data: Result<BundledSheetData, _> = serde_json::from_value(json!({
|
|
"status": "draft",
|
|
"ref": json_file(&format!("{DRAFT}/sheet-ref.json")),
|
|
"document": document,
|
|
"migrationMap": json_file(&format!("{DRAFT}/report-migration-map.json")),
|
|
"assets": assets,
|
|
"assetHashes": {},
|
|
}));
|
|
let refused = LoadedSheet::from_bundle(data.unwrap()).unwrap_err();
|
|
assert!(
|
|
format!("{refused:?}").contains("ContentMismatch"),
|
|
"{refused:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_explicit_null_block_is_refused_and_an_absent_one_is_v0() {
|
|
let map = json_file(&format!("{DRAFT}/report-migration-map.json"));
|
|
let mut null_block = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
null_block["fingerprint"] = Value::Null;
|
|
let refused = sheet_with_ref(null_block, map.clone()).unwrap_err();
|
|
assert!(refused.starts_with("shape:"), "{refused}");
|
|
let mut malformed = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
malformed["fingerprint"] = json!("sha256-rfc8785-validated-v1");
|
|
assert!(sheet_with_ref(malformed, map.clone()).is_err());
|
|
let absent = sheet_with_ref(json_file(&format!("{PRE_SWITCH}/sheet-ref.json")), map).unwrap();
|
|
assert!(!absent.is_fingerprint_verified());
|
|
}
|
|
|
|
#[test]
|
|
fn a_v1_sheet_carrying_only_draft_warnings_loads() {
|
|
use faunosteo_core::domain::sheet::validate::{SheetIssueSeverity, validate_sheet_document};
|
|
let mut document = json_file(&format!("{DRAFT}/document.json"));
|
|
document["annotations"][0]["finding_zone_id"] = Value::Null;
|
|
let typed = from(document.clone());
|
|
let issues = validate_sheet_document(&typed);
|
|
assert!(!issues.is_empty());
|
|
assert!(
|
|
issues
|
|
.iter()
|
|
.all(|i| i.severity == SheetIssueSeverity::Warning),
|
|
"{issues:?}"
|
|
);
|
|
let value = document_fingerprint(&typed).unwrap();
|
|
let mut reference = json_file(&format!("{DRAFT}/sheet-ref.json"));
|
|
reference["document_sha256"] = json!(value);
|
|
reference["fingerprint"]["aliases"] = json!([]);
|
|
let base = draft_sheet();
|
|
let assets: serde_json::Map<String, Value> = base
|
|
.assets
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let hashes: serde_json::Map<String, Value> = base
|
|
.asset_hashes
|
|
.iter()
|
|
.map(|(k, v)| (k.clone(), json!(v)))
|
|
.collect();
|
|
let sheet = loaded(
|
|
reference,
|
|
document,
|
|
json_file(&format!("{DRAFT}/report-migration-map.json")),
|
|
Value::Object(assets),
|
|
Value::Object(hashes),
|
|
);
|
|
assert!(sheet.is_fingerprint_verified());
|
|
// Another content: an old report on it is no alias, it rebases.
|
|
assert!(
|
|
import_archive(&sheet, &old_envelope().to_string())
|
|
.unwrap()
|
|
.rebase
|
|
.rebased
|
|
);
|
|
}
|