383 lines
15 KiB
Rust
383 lines
15 KiB
Rust
//! 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::{PngKeeper, 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::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, pngs: &[Vec<u8>]) -> 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(&pngs[i.embed]) },
|
||
"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 mut keeper = PngKeeper::default();
|
||
let drawn = layout(&checked, &rasters, &mut keeper).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, &keeper.pngs);
|
||
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> = keeper
|
||
.pngs
|
||
.iter()
|
||
.enumerate()
|
||
.map(|(i, png)| json!({ "embedIndex": i, "pngSha256": sha256_hex(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,
|
||
&mut PngKeeper::default(),
|
||
)
|
||
.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, &mut PngKeeper::default()).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);
|
||
}
|
||
|
||
/// Layout cost on the synthetic L (60 observations) and CAP (500) reports, printed for
|
||
/// RUST.md §10.6; a loose bound catches an order-of-magnitude regression only.
|
||
#[test]
|
||
fn layout_of_the_large_reports_stays_fast() {
|
||
for (name, cap_ms) in [("L", 500), ("CAP", 4000)] {
|
||
let mut input = export_input("demo", "fr", "logos");
|
||
let text = std::fs::read_to_string(
|
||
repo_root().join(format!("apps/app/tests/fixtures/r4/reports/{name}.json")),
|
||
)
|
||
.unwrap();
|
||
let mut report: faunosteo_core::domain::report::Report =
|
||
serde_json::from_value(parse_json(&text).unwrap()).unwrap();
|
||
report.sheet = input.report.sheet.clone();
|
||
input.report = report;
|
||
let rasters = stub_rasters(&input, "logos");
|
||
let started = std::time::Instant::now();
|
||
let checked = check_input(input).unwrap();
|
||
let checks_ms = started.elapsed().as_millis();
|
||
let started = std::time::Instant::now();
|
||
let mut keeper = PngKeeper::default();
|
||
let drawn = layout(&checked, &rasters, &mut keeper).unwrap();
|
||
let layout_ms = started.elapsed().as_millis();
|
||
let ops: usize = drawn.log.pages.iter().map(|p| p.ops.len()).sum();
|
||
println!(
|
||
"{name}: {} pages, {ops} ops, {} embeds; checks {checks_ms} ms, layout {layout_ms} ms",
|
||
drawn.log.pages.len(),
|
||
keeper.pngs.len()
|
||
);
|
||
assert!(layout_ms < cap_ms, "{name}: layout {layout_ms} ms");
|
||
}
|
||
}
|