Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_flow.rs

12.9 KiB, 1 run

created by r1870400018:63580, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Block flow, floats, fractional spacing and pages against the `typst` 0.15.1 oracle (U6b).
2//!
3//! Each fixture in `tests/fixtures/eval/{flow,float,fr}` labels the elements it probes `<probe>`. Typst
4//! reports each probe's page and position through `typst eval --in`, and its page count and page sizes
5//! through an SVG compile; Austenite evaluates the fixture, lays its pages out through
6//! `flow::paginate` and `Paginator::next_placed` a page at a time, and reads the same from the located elements
7//! the pages report and the pages. Positions must agree within 0.5pt, page counts exactly, page sizes within 0.5pt.
8//!
9//! A fixture whose first lines say `// needs: <unit>` depends on a unit not yet merged. While Austenite
10//! cannot evaluate it, it is reported as pending; the moment it evaluates, it is held to the oracle like
11//! any other. The suite fails unless at least `MIN_COMPARED` fixtures were compared in full.
12
13use oxedyne_fe2o3_austenite::driver::Recorder;
14use oxedyne_fe2o3_austenite::eval::content::Content;
15use oxedyne_fe2o3_austenite::eval::styles::StyleChain;
16use oxedyne_fe2o3_austenite::eval::{
17 eval_source,
18 Engine,
19 World,
20};
21use oxedyne_fe2o3_austenite::flow;
22use oxedyne_fe2o3_austenite::ledger::Position;
23
24use oxedyne_fe2o3_core::prelude::*;
25
26use std::collections::HashMap;
27use std::path::{
28 Path,
29 PathBuf,
30};
31use std::process::Command;
32
33const TOL: f64 = 0.5; // points
34const MIN_COMPARED: usize = 10; // fixtures that must be compared in full
35const AREAS: &[&str] = &["flow", "float", "fr", "flow_errors"];
36
37fn root() -> PathBuf {
38 Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval")
39}
40
41fn typst_bin() -> String {
42 std::env::var("TYPST").unwrap_or_else(|_| "typst".to_string())
43}
44
45/// Runs the host `typst` under a 3G memory cap.
46fn typst(args: &[&str]) -> std::result::Result<String, String> {
47 let mut cmd = Command::new("systemd-run");
48 cmd.args(["--user", "--scope", "--quiet", "-p", "MemoryMax=3G", "--slice=claude-rc.slice"]);
49 cmd.arg(typst_bin());
50 cmd.args(args);
51 cmd.current_dir(root());
52 let out = match cmd.output() {
53 Ok(o) => o,
54 Err(e) => return Err(fmt!("could not run typst: {}", e)),
55 };
56 if !out.status.success() {
57 return Err(String::from_utf8_lossy(&out.stderr).to_string());
58 }
59 Ok(String::from_utf8_lossy(&out.stdout).to_string())
60}
61
62#[derive(Clone, Copy, Debug)]
63struct Pos {
64 page: u32,
65 x: f64,
66 y: f64,
67}
68
69#[derive(Debug)]
70struct Laid {
71 probes: Vec<Pos>,
72 pages: Vec<(f64, f64)>,
73}
74
75/// Typst's side: probe positions from `typst eval`, page sizes from the SVG pages of a compile.
76fn oracle(rel: &str) -> std::result::Result<Laid, String> {
77 let expr = "query(<probe>).map(e => { let p = e.location().position(); (p.page, p.x.pt(), p.y.pt()) })";
78 let json = match typst(&["eval", "--root", ".", "--in", rel, expr]) {
79 Ok(j) => j,
80 Err(e) => return Err(e),
81 };
82 let nums = numbers(&json);
83 let mut probes = Vec::new();
84 for c in nums.chunks(3) {
85 if c.len() == 3 {
86 probes.push(Pos { page: c[0] as u32, x: c[1], y: c[2] });
87 }
88 }
89 let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("eval_flow").join(rel.replace('/', "_"));
90 let _ = std::fs::remove_dir_all(&dir);
91 if let Err(e) = std::fs::create_dir_all(&dir) {
92 return Err(fmt!("could not make {}: {}", dir.display(), e));
93 }
94 let pattern = dir.join("p{0p}.svg");
95 let pattern = pattern.to_string_lossy().to_string();
96 if let Err(e) = typst(&["compile", "--root", ".", rel, &pattern]) {
97 return Err(e);
98 }
99 let mut files: Vec<PathBuf> = match std::fs::read_dir(&dir) {
100 Ok(rd) => rd.filter_map(|e| e.ok().map(|e| e.path())).collect(),
101 Err(e) => return Err(fmt!("could not read {}: {}", dir.display(), e)),
102 };
103 files.sort();
104 let mut pages = Vec::new();
105 for f in files {
106 let svg = std::fs::read_to_string(&f).unwrap_or_default();
107 let w = attr(&svg, "width=\"").unwrap_or(0.0);
108 let h = attr(&svg, "height=\"").unwrap_or(0.0);
109 pages.push((w, h));
110 }
111 Ok(Laid { probes, pages })
112}
113
114/// Every number in a JSON text, in order.
115fn numbers(s: &str) -> Vec<f64> {
116 let mut out = Vec::new();
117 let mut cur = String::new();
118 for ch in s.chars() {
119 if ch.is_ascii_digit() || ch == '.' || ch == '-' || ch == 'e' || ch == '+' {
120 cur.push(ch);
121 } else {
122 if let Ok(v) = cur.parse::<f64>() {
123 out.push(v);
124 }
125 cur.clear();
126 }
127 }
128 if let Ok(v) = cur.parse::<f64>() {
129 out.push(v);
130 }
131 out
132}
133
134/// The number after the first occurrence of `key` in an SVG's root element, in points.
135fn attr(svg: &str, key: &str) -> Option<f64> {
136 let i = match svg.find(key) {
137 Some(i) => i + key.len(),
138 None => return None,
139 };
140 let rest = &svg[i..];
141 match rest.find(|c: char| !(c.is_ascii_digit() || c == '.')) {
142 Some(end) => rest[..end].parse::<f64>().ok(),
143 None => None,
144 }
145}
146
147/// Typst's first error for a fixture it must reject.
148fn oracle_error(rel: &str) -> std::result::Result<String, String> {
149 let out = Path::new(env!("CARGO_TARGET_TMPDIR")).join("eval_flow_reject.pdf");
150 let out = out.to_string_lossy().to_string();
151 match typst(&["compile", "--root", ".", rel, &out]) {
152 Ok(_) => Err("typst accepted a fixture marked `rejects`".to_string()),
153 Err(e) => match e.lines().find_map(|l| l.strip_prefix("error: ")) {
154 Some(m) => Ok(m.trim().to_string()),
155 None => Err(fmt!("typst failed without an error line: {}", e)),
156 },
157 }
158}
159
160/// Austenite's first error for a fixture it must reject, as the innermost message.
161fn austenite_error(path: &Path) -> std::result::Result<String, String> {
162 match austenite(path) {
163 Ok(_) => Err("austenite accepted it".to_string()),
164 Err(e) => Ok(e.splitn(2, ": ").nth(1).unwrap_or("").trim().to_string()),
165 }
166}
167
168/// Austenite's side: evaluate, lay the pages out, read the probes' located positions.
169fn austenite(path: &Path) -> std::result::Result<Laid, String> {
170 let mut world = World::new(root());
171 let id = match world.load(path) {
172 Ok(id) => id,
173 Err(e) => return Err(fmt!("load: {}", e)),
174 };
175 let mut engine = Engine::new(world);
176 let module = match eval_source(&mut engine, id) {
177 Ok(m) => m,
178 Err(e) => return Err(fmt!("eval: {}", last(&e))),
179 };
180 let mut paginator = flow::paginate(&module.content, &StyleChain::root());
181 let mut order = Order::default();
182 let mut pages = Vec::new();
183 loop {
184 let page = match paginator.next_placed(&mut engine, &mut order) {
185 Ok(Some((p, _))) => p,
186 Ok(None) => break,
187 Err(e) => return Err(fmt!("flow: {}", last(&e))),
188 };
189 pages.push((page.geom.width.to_pt(), page.geom.height.to_pt()));
190 }
191 let probes = order.finish().iter()
192 .filter(|(elem, _)| elem.label().map(|x| x.as_str() == "probe").unwrap_or(false))
193 .map(|(_, pos)| Pos { page: pos.page, x: pos.x.to_pt(), y: pos.y.to_pt() })
194 .collect();
195 Ok(Laid { probes, pages })
196}
197
198/// Where a located element stands in document order: in the order the pages placed it, or, inside a frame
199/// with a logical parent, just after that parent's mark with the others under it.
200#[derive(Clone, Copy, Debug)]
201enum Slot {
202 Main(u64),
203 Under(u64, u64), // the parent's id, then the order among its children
204}
205
206/// The located elements of a document in Typst's document order: page order, except that a frame with a
207/// parent (a float's material, a footnote's entry) is read where its parent stands, as Typst's introspector
208/// reads a frame's parent. The test's stand-in for the introspector's builder.
209#[derive(Default)]
210struct Order {
211 seq: u64,
212 marks: HashMap<u64, Slot>,
213 entries: Vec<(Slot, Content, Position)>,
214}
215
216impl Order {
217 fn slot(&mut self, parent: Option<u64>) -> Slot {
218 self.seq += 1;
219 match parent {
220 Some(p) => Slot::Under(p, self.seq),
221 None => Slot::Main(self.seq),
222 }
223 }
224
225 fn key(&self, slot: Slot, depth: usize) -> Vec<u64> {
226 match slot {
227 Slot::Main(s) => vec![2 * s],
228 Slot::Under(p, s) => match self.marks.get(&p) {
229 // A parent chain deeper than any real nesting is cut off rather than followed round a cycle.
230 Some(m) if depth < 64 => {
231 let mut k = self.key(*m, depth + 1);
232 k.push(1);
233 k.push(s);
234 k
235 },
236 _ => vec![2 * s],
237 },
238 }
239 }
240
241 fn finish(self) -> Vec<(Content, Position)> {
242 let mut keyed: Vec<(Vec<u64>, Content, Position)> = Vec::with_capacity(self.entries.len());
243 for (slot, elem, pos) in &self.entries {
244 keyed.push((self.key(*slot, 0), elem.clone(), *pos));
245 }
246 keyed.sort_by(|a, b| a.0.cmp(&b.0));
247 keyed.into_iter().map(|(_, e, p)| (e, p)).collect()
248 }
249}
250
251impl Recorder for Order {
252 fn record(&mut self, elem: &Content, pos: Position, parent: Option<u64>) {
253 let slot = self.slot(parent);
254 if let Some(loc) = elem.location() {
255 // A footnote's entry is read after the footnote itself: the element's location is a parent id.
256 self.marks.insert(loc.0, slot);
257 }
258 self.entries.push((slot, elem.clone(), pos));
259 }
260
261 fn mark(&mut self, id: u64) {
262 let slot = self.slot(None);
263 self.marks.insert(id, slot);
264 }
265}
266
267fn last(e: &Error<ErrTag>) -> String {
268 match e.msgs().into_iter().last() {
269 Some(m) => m,
270 None => fmt!("{}", e),
271 }
272}
273
274/// The differences between the two sides, one line each; empty when they agree.
275fn compare(want: &Laid, got: &Laid) -> Vec<String> {
276 let mut out = Vec::new();
277 if want.pages.len() != got.pages.len() {
278 out.push(fmt!("pages: typst {} austenite {}", want.pages.len(), got.pages.len()));
279 }
280 for (i, (w, g)) in want.pages.iter().zip(got.pages.iter()).enumerate() {
281 // Typst's SVG gives whole-point viewports when the size is whole; compare within the tolerance.
282 if (w.0 - g.0).abs() > TOL.max(1.0) || (w.1 - g.1).abs() > TOL.max(1.0) {
283 out.push(fmt!("page {} size: typst {:.2}x{:.2} austenite {:.2}x{:.2}", i + 1, w.0, w.1, g.0, g.1));
284 }
285 }
286 if want.probes.len() != got.probes.len() {
287 out.push(fmt!("probes: typst {} austenite {}", want.probes.len(), got.probes.len()));
288 }
289 for (i, (w, g)) in want.probes.iter().zip(got.probes.iter()).enumerate() {
290 if w.page != g.page || (w.x - g.x).abs() > TOL || (w.y - g.y).abs() > TOL {
291 out.push(fmt!("probe {}: typst p{} ({:.2}, {:.2}) austenite p{} ({:.2}, {:.2})",
292 i + 1, w.page, w.x, w.y, g.page, g.x, g.y));
293 }
294 }
295 out
296}
297
298struct Fixture {
299 rel: String,
300 path: PathBuf,
301 needs: Vec<String>,
302 rejects: bool,
303}
304
305fn fixtures() -> Vec<Fixture> {
306 let mut out = Vec::new();
307 for area in AREAS {
308 let dir = root().join(area);
309 let mut files: Vec<PathBuf> = match std::fs::read_dir(&dir) {
310 Ok(rd) => rd.filter_map(|e| e.ok().map(|e| e.path())).collect(),
311 Err(_) => continue,
312 };
313 files.sort();
314 for f in files {
315 let name = f.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
316 if !name.ends_with(".typ") || name.starts_with('_') {
317 continue;
318 }
319 let text = std::fs::read_to_string(&f).unwrap_or_default();
320 let needs = text.lines()
321 .take_while(|l| l.starts_with("//"))
322 .filter_map(|l| l.strip_prefix("// needs:"))
323 .flat_map(|l| l.split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>())
324 .collect();
325 let rejects = text.lines().take_while(|l| l.starts_with("//")).any(|l| l.trim() == "// oracle: rejects");
326 out.push(Fixture { rel: fmt!("{}/{}", area, name), path: f, needs, rejects });
327 }
328 }
329 out
330}
331
332#[test]
333fn flow_matches_the_typst_oracle() {
334 let only = std::env::var("EVAL_FLOW_FIXTURE").ok();
335 let mut compared = 0usize;
336 let mut probes = 0usize;
337 let mut failures = Vec::new();
338 let mut pending = Vec::new();
339 let mut refusals = 0usize;
340 for fx in fixtures() {
341 let (rel, path, needs) = (fx.rel, fx.path, fx.needs);
342 if let Some(o) = &only {
343 if !rel.contains(o.as_str()) {
344 continue;
345 }
346 }
347 if fx.rejects {
348 let want = match oracle_error(&rel) {
349 Ok(m) => m,
350 Err(e) => {
351 failures.push(fmt!("{}: {}", rel, e));
352 continue;
353 },
354 };
355 match austenite_error(&path) {
356 Ok(got) if got == want => {
357 refusals += 1;
358 println!("ok {} (refused: {})", rel, want);
359 },
360 Ok(got) => failures.push(fmt!("{}:\n typst: {}\n austenite: {}", rel, want, got)),
361 Err(e) => failures.push(fmt!("{}: {}", rel, e)),
362 }
363 continue;
364 }
365 let want = match oracle(&rel) {
366 Ok(w) => w,
367 Err(e) => {
368 failures.push(fmt!("{}: typst rejected the fixture: {}", rel, e.lines().next().unwrap_or("")));
369 continue;
370 },
371 };
372 let got = match austenite(&path) {
373 Ok(g) => g,
374 Err(e) => {
375 if needs.is_empty() {
376 failures.push(fmt!("{}: {}", rel, e));
377 } else {
378 pending.push(fmt!("{} (needs {}): {}", rel, needs.join(", "), e));
379 }
380 continue;
381 },
382 };
383 let diffs = compare(&want, &got);
384 if diffs.is_empty() {
385 compared += 1;
386 probes += want.probes.len();
387 println!("ok {} ({} pages, {} probes)", rel, want.pages.len(), want.probes.len());
388 } else {
389 failures.push(fmt!("{}:\n {}", rel, diffs.join("\n ")));
390 }
391 }
392 for p in &pending {
393 println!("pending {}", p);
394 }
395 for f in &failures {
396 println!("FAIL {}", f);
397 }
398 println!("{} fixtures compared in full, {} probes; {} refusals matched; {} pending; {} failing",
399 compared, probes, refusals, pending.len(), failures.len());
400 assert!(failures.is_empty(), "{} fixture(s) differ from typst", failures.len());
401 if only.is_none() {
402 assert!(compared >= MIN_COMPARED, "only {} fixtures were compared in full, fewer than {}", compared, MIN_COMPARED);
403 }
404}