feat(domain): R4.4 layout to a draw log: text rules, AFM metrics, dates (O4, O5, O6) and the page layouts (O1)

pdf::write: text (safe_text with the frozen V8 classes, js_upper from the O4 tables,
js_trim, measure = Σ(WX || 250 + KPX) × size / 1000 over WinAnsi bytes, wrap_lines on JS
\s), afm_data generated by scripts/r4-afm-data.mjs from O5 and O4, dates (Date.UTC years
0-99 → 19xx, fr-FR / en-US long dates), input checks (branding and labels schemas,
snapshot gate with the R3 SVG reader, sheet mismatch, page caps), the header, footer,
observation and conclusion layouts into a DrawLog (per page, footers last, links by page).
cargo: O4 55,992 widths bit-exact, 1,168 wraps, safeText over every code point, upper
1,580, O6 6,468 cells + 22; O1 12 runs 2,124 ops equal at full precision; the Step 6 text
and image goldens (24) derive from the log. ts-rs: ExportInput, ExportErrorKind,
ExportWarning, LogoSlot.
This commit is contained in:
bchanot committed 2026-10-18 21:47:40 +02:00
1 parent 71bcb742c9
commit 70007e0c93
19 files changed
+4094 -29

No files matched your search

+345 -1
View File
@@ -1 +1,345 @@
//! R4.4b placeholder.
//! R4.4b: the layout against the O1 draw logs (`apps/app/tests/fixtures/goldens/r4/
//! draw.<report>.<locale>.<logos>.json`, `apps/app/tests/helpers/r4-draw.ts`): every call
//! of the TypeScript engine, at full precision, in call order, for the 12 Step 6 runs. The
//! export input of each run is the envelope its own R3 PDF carries (`pdf/r3/*.pdf`, read by
//! the R4 reader), the rasters the stubs the goldens used (1×1 PNG without logos, 2×1 PNG
//! with them: the SVG partner logo and every view; the PNG logos decode their own base64).
//! Then the Step 6 text and image goldens (`goldens/step6/{text,images}.*.json`, two
//! decimals) as the derived check the plan lists.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::r4::{repo_root, sha256_hex};
use faunosteo_core::domain::archive::Locale;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use faunosteo_core::pdf::write::draw_log::{DrawLog, DrawOp};
use faunosteo_core::pdf::write::input::{ExportInput, Rasters, check_input};
use faunosteo_core::pdf::write::layout::layout;
use serde_json::{Value, json};
const REPORTS: [&str; 3] = ["demo", "v2full", "v2unresolved"];
const LOCALES: [&str; 2] = ["fr", "en"];
const MODES: [&str; 2] = ["nologos", "logos"];
const ONE_PX_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
const TWO_BY_ONE_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC";
fn base64(text: &str) -> Vec<u8> {
let table: Vec<u8> = (b'A'..=b'Z')
.chain(b'a'..=b'z')
.chain(b'0'..=b'9')
.chain(*b"+/")
.collect();
let mut out = Vec::new();
let (mut acc, mut bits) = (0u32, 0u32);
for c in text.bytes().filter(|c| *c != b'=') {
acc = (acc << 6) | table.iter().position(|t| *t == c).unwrap() as u32;
bits += 6;
if bits >= 8 {
bits -= 8;
out.push((acc >> bits) as u8);
acc &= (1 << bits) - 1;
}
}
out
}
fn fixture(path: &str) -> Vec<u8> {
std::fs::read(repo_root().join("apps/app/tests/fixtures").join(path)).unwrap()
}
/// The envelope the R3 PDF of a run carries, as the export input of that run.
fn export_input(report: &str, locale: &str, mode: &str) -> ExportInput {
let pdf = fixture(&format!("pdf/r3/{report}.{locale}.{mode}.pdf"));
let located = read_attached_document(&pdf, ProbeLimits::UNBOUNDED).unwrap();
let envelope = parse_json(&located.text).unwrap();
let input = json!({
"report": envelope["report"],
"sheetSnapshot": envelope["sheetSnapshot"],
"branding": envelope["branding"],
"labels": envelope["labels"],
"locale": envelope["locale"],
"exportedAt": envelope["exportedAt"],
});
serde_json::from_value(input).unwrap()
}
/// The stub rasters of the goldens: one PNG for every view and every SVG logo slot.
fn stub_rasters(input: &ExportInput, mode: &str) -> Rasters {
let png = base64(if mode == "logos" {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
});
let mut rasters = Rasters {
header_logo: Some(png.clone()),
footer_logo: Some(png.clone()),
powered_by: Some(png.clone()),
partner: Some(png.clone()),
views: Default::default(),
};
for observation in &input.report.observations {
for zone in &observation.zones {
rasters
.views
.entry((observation.id.clone(), zone.view_id.clone()))
.or_insert_with(|| png.clone());
}
}
rasters
}
fn color(c: &faunosteo_core::pdf::write::draw_log::Rgb) -> Value {
json!({ "type": "RGB", "red": c.red, "green": c.green, "blue": c.blue })
}
/// One op in the oracle's shape (without `seq`).
fn op_json(op: &DrawOp, log: &DrawLog) -> Value {
match op {
DrawOp::Text(t) => json!({ "op": "drawText", "text": t.text, "options": {
"x": t.x, "y": t.y, "size": t.size, "font": t.font.as_str(), "color": color(&t.color) } }),
DrawOp::Image(i) => json!({ "op": "drawImage",
"image": { "embedIndex": i.embed, "pngSha256": sha256_hex(&log.embeds[i.embed].png) },
"options": { "x": i.x, "y": i.y, "width": i.width, "height": i.height } }),
DrawOp::Line(l) => json!({ "op": "drawLine", "options": {
"start": { "x": l.start.x, "y": l.start.y }, "end": { "x": l.end.x, "y": l.end.y },
"thickness": l.thickness, "color": color(&l.color) } }),
DrawOp::Rectangle(r) => json!({ "op": "drawRectangle", "options": {
"x": r.x, "y": r.y, "width": r.width, "height": r.height, "color": color(&r.color) } }),
}
}
fn without_seq(op: &Value) -> Value {
let mut copy = op.clone();
copy.as_object_mut().unwrap().remove("seq");
copy
}
/// Objects with their keys sorted, recursively: the oracle's key order is the recorder's.
fn canon(value: &Value) -> Value {
match value {
Value::Object(map) => {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
Value::Object(
keys.into_iter()
.map(|k| (k.clone(), canon(&map[k])))
.collect(),
)
}
Value::Array(items) => Value::Array(items.iter().map(canon).collect()),
other => other.clone(),
}
}
/// Compares two JSON values in their JavaScript spelling (`4` and `4.0` are one number),
/// key order aside.
fn same(a: &Value, b: &Value) -> bool {
let js = |v: &Value| faunosteo_core::json::to_js_compact(&canon(v)).unwrap();
js(a) == js(b)
}
fn compare_run(report: &str, locale: &str, mode: &str, mismatches: &mut Vec<String>) -> usize {
let name = format!("{report}.{locale}.{mode}");
let oracle: Value = serde_json::from_str(
&std::fs::read_to_string(repo_root().join(format!(
"apps/app/tests/fixtures/goldens/r4/draw.{name}.json"
)))
.unwrap(),
)
.unwrap();
let input = export_input(report, locale, mode);
let rasters = stub_rasters(&input, mode);
let checked = check_input(input).unwrap();
let drawn = layout(&checked, &rasters).unwrap();
assert!(drawn.warnings.is_empty(), "{name}: {:?}", drawn.warnings);
let pages = oracle["pages"].as_array().unwrap();
if pages.len() != drawn.log.pages.len() {
mismatches.push(format!(
"{name}: {} pages, expected {}",
drawn.log.pages.len(),
pages.len()
));
return 0;
}
let mut ops = 0;
for (p, page) in pages.iter().enumerate() {
let expected = page["ops"].as_array().unwrap();
let actual = &drawn.log.pages[p].ops;
if expected.len() != actual.len() {
mismatches.push(format!(
"{name} page {p}: {} ops, expected {}",
actual.len(),
expected.len()
));
}
for (i, (e, a)) in expected.iter().zip(actual).enumerate() {
ops += 1;
let a = op_json(a, &drawn.log);
if !same(&without_seq(e), &a) {
mismatches.push(format!(
"{name} page {p} op {i}\n expected {}\n actual {a}",
without_seq(e)
));
}
}
}
let embeds: Vec<Value> = drawn
.log
.embeds
.iter()
.enumerate()
.map(|(i, e)| json!({ "embedIndex": i, "pngSha256": sha256_hex(&e.png) }))
.collect();
if !same(&oracle["embeds"], &Value::Array(embeds.clone())) {
mismatches.push(format!(
"{name} embeds\n expected {}\n actual {}",
oracle["embeds"],
Value::Array(embeds)
));
}
let links: Vec<Value> = drawn
.log
.links
.iter()
.map(|l| json!({ "page": l.page, "rect": l.rect, "uri": l.uri }))
.collect();
if !same(&oracle["links"], &Value::Array(links.clone())) {
mismatches.push(format!(
"{name} links\n expected {}\n actual {}",
oracle["links"],
Value::Array(links)
));
}
ops
}
#[test]
fn o1_draw_logs_match_the_typescript_at_full_precision() {
let mut mismatches = Vec::new();
let mut ops = 0;
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
ops += compare_run(report, locale, mode, &mut mismatches);
}
}
}
assert!(
mismatches.is_empty(),
"{} mismatches:\n{}",
mismatches.len(),
mismatches
.iter()
.take(12)
.cloned()
.collect::<Vec<_>>()
.join("\n")
);
println!("O1: 12 runs, {ops} ops equal");
}
/// Two decimals as the Step 6 recorder rounded (`Math.round(n * 100) / 100`).
fn round2(n: f64) -> f64 {
(n * 100.0).round() / 100.0
}
#[test]
fn step6_text_and_image_goldens_derive_from_the_log() {
let goldens = repo_root().join("apps/app/tests/fixtures/goldens/step6");
let mut checked = 0;
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
let name = format!("{report}.{locale}.{mode}");
let input = export_input(report, locale, mode);
let rasters = stub_rasters(&input, mode);
let drawn = layout(&check_input(input).unwrap(), &rasters).unwrap();
let text: Value = serde_json::from_str(
&std::fs::read_to_string(goldens.join(format!("text.{name}.json"))).unwrap(),
)
.unwrap();
let images: Value = serde_json::from_str(
&std::fs::read_to_string(goldens.join(format!("images.{name}.json"))).unwrap(),
)
.unwrap();
let (mut texts, mut imgs) = (Vec::new(), Vec::new());
for (p, op) in drawn.log.calls_in_order() {
{
match op {
DrawOp::Text(t) => texts.push(json!({ "page": p, "text": t.text, "x": round2(t.x), "y": round2(t.y), "size": t.size, "font": t.font.as_str() })),
DrawOp::Image(i) => imgs.push(json!({ "page": p, "x": round2(i.x), "y": round2(i.y), "width": round2(i.width), "height": round2(i.height) })),
_ => {}
}
}
}
assert!(same(&text, &Value::Array(texts)), "{name}: text golden");
assert!(same(&images, &Value::Array(imgs)), "{name}: images golden");
checked += 2;
}
}
}
assert_eq!(checked, 24);
}
#[test]
fn the_checks_refuse_what_export_ts_refused() {
let mut input = export_input("demo", "fr", "nologos");
input.report.sheet.document_sha256 = "0".repeat(64);
let err = check_input(input).err().unwrap();
assert_eq!(err.kind.as_str(), "sheet_mismatch");
let mut input = export_input("demo", "fr", "nologos");
input.branding = json!({ "siteUrl": "not a url" });
assert_eq!(check_input(input).err().unwrap().detail, "branding");
let mut input = export_input("demo", "fr", "nologos");
input.labels = json!({ "fr": {} });
assert_eq!(check_input(input).err().unwrap().detail, "labels");
let mut input = export_input("demo", "fr", "nologos");
input.sheet_snapshot = json!({ "status": "draft" });
assert_eq!(
check_input(input).err().unwrap().kind.as_str(),
"snapshot_invalid"
);
let input = export_input("demo", "fr", "nologos");
let _ = Locale::Fr;
assert!(check_input(input).is_ok());
}
#[test]
fn page_caps_and_missing_rasters_behave_as_export_ts() {
use faunosteo_core::pdf::write::input::{ExportWarning, LogoSlot, MAX_OBSERVATIONS};
// 501 observations: too_many_pages / observations.
let mut input = export_input("demo", "fr", "nologos");
let first = input.report.observations[0].clone();
while input.report.observations.len() <= MAX_OBSERVATIONS {
input.report.observations.push(first.clone());
}
let err = check_input(input).err().unwrap();
assert_eq!(
(err.kind.as_str(), err.detail.as_str()),
("too_many_pages", "observations")
);
// A missing view raster and an unreadable logo: one warning each, the page still drawn.
let input = export_input("demo", "fr", "logos");
let mut rasters = stub_rasters(&input, "logos");
rasters.partner = Some(b"not a png".to_vec());
let first_key = rasters.views.keys().next().cloned().unwrap();
rasters.views.remove(&first_key);
let checked = check_input(input).unwrap();
let drawn = layout(&checked, &rasters).unwrap();
assert_eq!(
drawn.warnings,
vec![
ExportWarning::LogoFailed {
slot: LogoSlot::Partner
},
ExportWarning::ViewRasterFailed {
observation_id: first_key.0,
view_id: first_key.1,
},
]
);
assert_eq!(drawn.log.pages.len(), checked.report.observations.len() + 1);
}