Files
bchanot 6ee04c96c5 feat(domain): R4.2e OD-3 value cap (400,000), rescue-copy guard, 1 GiB linear ceiling, budget measurements
json_js::MAX_JSON_VALUES from the R4.2e measurements (CAP envelope 39,652 values, 612-leaf
export 23,803; ~100 B of tree per value, a 16 MiB dense document 784 MiB with the cap off,
16 MiB with it); draft.restore refuses an over-budget copy as invalid; --max-memory 1 GiB
proven to trap on the proofs build; cargo pdf_budget prints the value table, the Node
budget suite prints the per-class cost (fresh instance, W4 asserted).
2026-10-18 16:06:36 +02:00

128 lines
5.0 KiB
Rust

//! R4.2e, OD-3: the value count (`json_js::prescan`, the parser's allocation proxy) of the
//! legitimate archives against the hostile references, printed for RUST.md §10.3 and pinned
//! loosely so a drift of an order of magnitude is seen.
//!
//! Legitimate: the R3 export of the demo with logos (its attachment located by the reader),
//! the v2 full and 612-leaves exports, and envelopes built like the export does for the
//! synthetic L (60 observations) and CAP (500, the collection cap) reports: the demo's
//! branding with logos and labels, a snapshot of the draft sheet for that report, the
//! `JSON.stringify(_, null, 2)` form. Hostile: the token-dense `values-1m.pdf` of O8.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::r4::{r4_input, repo_root};
use faunosteo_core::domain::archive::{ReportArchiveEnvelope, build_sheet_snapshot};
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::report::Report;
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
use faunosteo_core::json::to_js_pretty;
use faunosteo_core::pdf::import::import_pdf;
use faunosteo_core::pdf::json_js::{JsonPrescan, prescan};
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use serde_json::{Value, json};
const SEED: &str = "supabase/seed/anatomical-sheets/equine/draft";
const FIXTURES: &str = "apps/app/tests/fixtures";
fn draft_sheet() -> LoadedSheet {
let seed = repo_root().join(SEED);
let read = |name: &str| std::fs::read_to_string(seed.join(name)).unwrap();
let manifest: Value = serde_json::from_str(&read("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(&file)));
hashes.insert(id, asset["sha256"].clone());
}
let payload = json!({
"status": "draft",
"ref": parse_json(&read("sheet-ref.json")).unwrap(),
"document": parse_json(&read("document.json")).unwrap(),
"migrationMap": parse_json(&read("report-migration-map.json")).unwrap(),
"assets": assets,
"assetHashes": hashes,
});
let data: BundledSheetData = serde_json::from_value(payload).unwrap();
LoadedSheet::from_bundle(data).unwrap()
}
fn fixture(path: &str) -> Vec<u8> {
std::fs::read(repo_root().join(FIXTURES).join(path)).unwrap()
}
/// The attachment text of a PDF, as the reader hands it to the session.
fn attachment_text(pdf: &[u8]) -> String {
read_attached_document(pdf, ProbeLimits::UNBOUNDED)
.unwrap()
.text
.into_owned()
}
/// An envelope like the export writes for `report`: the demo's branding (logos) and labels,
/// a snapshot of the draft for that report.
fn envelope_for(sheet: &LoadedSheet, demo: &ReportArchiveEnvelope, report: Report) -> String {
let envelope = ReportArchiveEnvelope {
sheet_snapshot: build_sheet_snapshot(sheet, &report),
report,
..demo.clone()
};
to_js_pretty(&envelope).unwrap()
}
fn report(path: &str) -> Report {
let text = String::from_utf8(fixture(path)).unwrap();
serde_json::from_value(parse_json(&text).unwrap()).unwrap()
}
fn row(name: &str, text: &str) -> (String, usize, JsonPrescan) {
(name.to_owned(), text.len(), prescan(text.as_bytes()))
}
#[test]
fn legitimate_archives_stay_far_below_the_hostile_references() {
let sheet = draft_sheet();
let demo_pdf = fixture("pdf/r3/demo.fr.logos.pdf");
let demo = import_pdf(&sheet, &demo_pdf, ProbeLimits::UNBOUNDED)
.unwrap()
.read
.archive
.unwrap();
let rows = [
row("demo fr logos (R3 export)", &attachment_text(&demo_pdf)),
row(
"v2full fr logos (R3 export)",
&attachment_text(&fixture("pdf/r3/v2full.fr.logos.pdf")),
),
row(
"612 leaves (O7 read.612-leaves-exported)",
&attachment_text(&r4_input("read", "read.612-leaves-exported.pdf")),
),
row(
"L envelope (60 observations, 120 views, logos)",
&envelope_for(&sheet, &demo, report("r4/reports/L.json")),
),
row(
"CAP envelope (500 observations, 1,000 views, logos)",
&envelope_for(&sheet, &demo, report("r4/reports/CAP.json")),
),
row(
"hostile: tolerance/values-1m.pdf",
&attachment_text(&fixture("pdf/r4/tolerance/values-1m.pdf")),
),
];
println!("| Document | Bytes | Values | Within depth |");
println!("|---|---|---|---|");
for (name, bytes, scan) in &rows {
println!(
"| {name} | {bytes} | {} | {} |",
scan.values, scan.within_depth
);
}
let cap = rows[4].2.values;
assert!(cap < 200_000, "CAP envelope values: {cap}");
assert!(rows[5].2.values > 5 * cap, "values-1m is not far above CAP");
}