test(tests): R4 oracle plumbing for the cargo reader suites (inputs, generators, sha256)

This commit is contained in:
bchanot committed 2026-10-18 15:32:37 +02:00
1 parent 21d34fb9a7
commit 09ce18b6dc
3 files changed
+101 -1

No files matched your search

+1
View File
@@ -6,6 +6,7 @@
clippy::expect_used
)]
pub mod r4;
pub mod svg_tree;
use faunosteo_core::domain::js::{find_lone_surrogate, parse_json};
+90
View File
@@ -0,0 +1,90 @@
//! R4 oracle plumbing shared by the reader suites: the frozen inputs under
//! `apps/app/tests/fixtures/pdf/r4/<dir>` (stored bytes, or a `<slug>.gen.json` description
//! expanded and checked against its length and digest) and the sha256 digests the TypeScript
//! generators recorded.
#![allow(
dead_code,
clippy::disallowed_methods,
clippy::unwrap_used,
clippy::expect_used
)]
use std::path::PathBuf;
use serde_json::Value;
use sha2::{Digest, Sha256};
/// Repository root (the workspace lives at `crates/core/../..`).
pub fn repo_root() -> PathBuf {
PathBuf::from(format!("{}/../..", env!("CARGO_MANIFEST_DIR")))
}
/// A frozen R4 oracle (`crates/core/tests/oracles/r4/<name>`), as plain serde_json.
pub fn r4_oracle(name: &str) -> Value {
let path = repo_root().join("crates/core/tests/oracles/r4").join(name);
let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{path:?}: {e}"));
serde_json::from_str(&text).unwrap_or_else(|e| panic!("{path:?}: {e}"))
}
/// Lower-case hex sha256 of `data`, the form every oracle stores.
pub fn sha256_hex(data: &[u8]) -> String {
let digest = Sha256::digest(data);
digest.iter().map(|b| format!("{b:02x}")).collect()
}
/// The bytes of input `file` of `dir` (`read`, `inflate`, `tolerance`): stored as is, or
/// expanded from `<slug>.gen.json` (`concat/1`: `latin1` text one byte per character, `fill`
/// a byte repeated, `slice` of another file of the directory), then checked against the
/// recorded `length` and `sha256`.
pub fn r4_input(dir: &str, file: &str) -> Vec<u8> {
let base = repo_root().join("apps/app/tests/fixtures/pdf/r4").join(dir);
let stored = base.join(file);
if stored.exists() {
return std::fs::read(&stored).unwrap_or_else(|e| panic!("{stored:?}: {e}"));
}
let slug = file.rsplit_once('.').map_or(file, |(stem, _)| stem);
let description = base.join(format!("{slug}.gen.json"));
let text = std::fs::read_to_string(&description)
.unwrap_or_else(|e| panic!("{description:?}: {e} (no stored bytes for {file})"));
let spec: Value = serde_json::from_str(&text).unwrap();
assert_eq!(spec["generator"], "concat/1", "{file}: unknown generator");
let bytes = expand_parts(spec["parts"].as_array().unwrap(), &base);
assert_eq!(
bytes.len() as u64,
spec["length"].as_u64().unwrap(),
"{file}: length"
);
assert_eq!(
sha256_hex(&bytes),
spec["sha256"].as_str().unwrap(),
"{file}: digest"
);
bytes
}
fn expand_parts(parts: &[Value], dir: &std::path::Path) -> Vec<u8> {
let mut out = Vec::new();
for part in parts {
if let Some(text) = part["latin1"].as_str() {
out.extend(text.chars().map(|c| u32::from(c) as u8));
} else if let Some(byte) = part["fill"].as_u64() {
let count = part["count"].as_u64().unwrap() as usize;
out.resize(out.len() + count, byte as u8);
} else if let Some(source) = part["slice"].as_str() {
let bytes = std::fs::read(dir.join(source)).unwrap();
let (start, end) = (
part["start"].as_u64().unwrap(),
part["end"].as_u64().unwrap(),
);
out.extend_from_slice(&bytes[start as usize..end as usize]);
} else {
panic!("unknown part {part}");
}
}
out
}
/// A Latin-1 string of the oracles (one character per byte) back to its bytes.
pub fn latin1_bytes(text: &str) -> Vec<u8> {
text.chars().map(|c| u32::from(c) as u8).collect()
}
+10 -1
View File
@@ -1 +1,10 @@
//! R4.2: placeholder, written by its track.
//! R4.2a placeholder: the O7 scan suite, written by its track.
mod common;
#[test]
fn placeholder_reads_an_oracle() {
let oracle = common::r4::r4_oracle("read.json");
assert_eq!(oracle["oracle"], "O7");
let bytes = common::r4::r4_input("read", "scan.demo-export.pdf");
assert_eq!(bytes.len(), 78463);
}