Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_door.rs

77.2 KiB, 23 runs

created by r1870400018:66078, 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//! The door, driven natively: every call the wasm surface offers goes through [`door`], so what runs here is
2//! what the browser runs. External facts check each result: the sites and warnings of `typst` 0.15.1
3//! (`--diagnostic-format short`, whose line is 1-based and whose column is 0-based, in characters), the
4//! rows of `typst query`, the families `typst fonts` lists, and `pdfinfo` and `pdftotext` on the PDF.
5//!
6//! The door's columns are 1-based UTF-16 code units, so a site's column here is Typst's character column
7//! plus one, plus one for each character outside the Basic Multilingual Plane before it on its line.
8
9#[allow(dead_code)]
10#[path = "eval_oracle/mod.rs"]
11mod harness;
12
13use harness::json::{
14 self,
15 J,
16};
17use harness::pdf::{
18 self,
19 tool,
20 TYPST,
21};
22
23use oxedyne_fe2o3_austenite::caches::Budgets;
24use oxedyne_fe2o3_austenite::compile::DiagnosticKind;
25use oxedyne_fe2o3_austenite::delta::{
26 Changed,
27 PageAt,
28 PageDelta,
29 PageMiss,
30 Window,
31};
32use oxedyne_fe2o3_austenite::door::{
33 self,
34 Failure,
35 Instance,
36 Made,
37 Project,
38};
39use oxedyne_fe2o3_austenite::emit::sinks::Chunks;
40use oxedyne_fe2o3_austenite::eval::package;
41use oxedyne_fe2o3_austenite::fonts::FontBook;
42
43use oxedyne_fe2o3_core::prelude::*;
44use oxedyne_fe2o3_graphics::pdf_font::collection_of;
45
46use std::collections::BTreeSet;
47use std::path::{
48 Path,
49 PathBuf,
50};
51use std::process::Command;
52
53// The cases the node smoke prints one line for, and the lines it must equal.
54const SKELETON: &str = "#set page(width: 200pt, height: 100pt)\n= Skeleton\nA short paragraph.\n";
55
56fn work_dir(name: &str) -> Outcome<PathBuf> { pdf::work_dir(&fmt!("eval-door-{}", name)) }
57
58/// A project of `files`, `/main.typ` among them, strict.
59fn strict(files: &[(&str, &str)]) -> Project {
60 let mut p = project(files);
61 p.strict = true;
62 p
63}
64
65fn project(files: &[(&str, &str)]) -> Project {
66 let mut p = Project::default();
67 for (path, text) in files {
68 p.sources.push((path.to_string(), text.to_string()));
69 }
70 p
71}
72
73fn pdf_of(made: &Made<Chunks>) -> Vec<u8> { made.product.to_vec() }
74
75fn must_pdf(r: Result<Made<Chunks>, Failure>) -> Outcome<Made<Chunks>> {
76 match r {
77 Ok(m) => Ok(m),
78 Err(f) => Err(err!("The door refused: {} {:?}", f.head, f.head.kind; Test)),
79 }
80}
81
82fn must_fail(r: Result<Made<Chunks>, Failure>) -> Outcome<Failure> {
83 match r {
84 Ok(_) => Err(err!("The door compiled what it should have refused."; Test)),
85 Err(f) => Ok(f),
86 }
87}
88
89/// A failure's `<file>:<line>:<col>`, as the smoke prints it.
90fn site(f: &Failure) -> String { fmt!("{}:{}:{}", f.head.file, f.head.line, f.head.col) }
91
92/// Writes `files` under `dir` and returns the path of `main.typ`.
93fn write_project(dir: &Path, files: &[(&str, &str)]) -> Outcome<PathBuf> {
94 res!(std::fs::create_dir_all(dir));
95 for (path, text) in files {
96 let at = dir.join(path.trim_start_matches('/'));
97 if let Some(parent) = at.parent() {
98 res!(std::fs::create_dir_all(parent));
99 }
100 res!(std::fs::write(&at, text));
101 }
102 Ok(dir.join("main.typ"))
103}
104
105/// Typst's run with the cap every oracle run is given: its standard error, whatever the exit.
106fn typst(args: &[&str]) -> Outcome<(bool, String, String)> {
107 let out = res!(Command::new("systemd-run")
108 .args(["--user", "--scope", "--quiet", "-p", "MemoryMax=3G", "--slice=claude-rc.slice", TYPST])
109 .args(args)
110 .output());
111 Ok((
112 out.status.success(),
113 String::from_utf8_lossy(&out.stdout).to_string(),
114 String::from_utf8_lossy(&out.stderr).to_string(),
115 ))
116}
117
118/// Typst's first `severity` site in its short format, as (line, column), the column 0-based.
119fn typst_site(main: &Path, severity: &str) -> Outcome<(usize, usize)> {
120 let out = main.with_extension("pdf");
121 let (_, _, stderr) = res!(typst(&["compile", "--diagnostic-format", "short",
122 &main.display().to_string(), &out.display().to_string()]));
123 let marker = fmt!(": {}:", severity);
124 for l in stderr.lines() {
125 if let Some(at) = l.find(&marker) {
126 let mut parts = l[..at].rsplitn(3, ':');
127 let col = parts.next().and_then(|c| c.parse::<usize>().ok());
128 let line = parts.next().and_then(|c| c.parse::<usize>().ok());
129 if let (Some(line), Some(col)) = (line, col) {
130 return Ok((line, col));
131 }
132 }
133 }
134 Err(err!("Typst reported no {} site for {}: {}", severity, main.display(), stderr; Test))
135}
136
137/// The column the door gives for Typst's 0-based character column `col` on `line` of `text`: 1-based, in
138/// UTF-16 code units.
139fn door_col(text: &str, line: usize, col: usize) -> usize {
140 let row = text.split('\n').nth(line - 1).unwrap_or("");
141 let before: usize = row.chars().take(col).map(|c| c.len_utf16()).sum();
142 before + 1
143}
144
145fn pdf_pages(pdf: &[u8], dir: &Path, name: &str) -> Outcome<usize> {
146 let path = dir.join(fmt!("{}.pdf", name));
147 res!(std::fs::write(&path, pdf));
148 let info = res!(tool("pdfinfo", &[], &path, &[]));
149 let line = res!(info.lines().find(|l| l.starts_with("Pages:")).ok_or_else(|| err!("pdfinfo gave no page count"; Test)));
150 Ok(res!(line.trim_start_matches("Pages:").trim().parse::<usize>().map_err(|e| err!("{}", e; Test))))
151}
152
153// ┌───────────────────────────────────────────────────────────────────────────┐
154// │ G9: NO FALSE GREEN │
155// └───────────────────────────────────────────────────────────────────────────┘
156
157#[test]
158fn g9_a_missing_font_is_refused_under_strict_at_typsts_site() -> Outcome<()> {
159 let dir = res!(work_dir("g9-font"));
160 let text = "#set text(font: \"Nope Sans\")\nHello.\n";
161 let main = res!(write_project(&dir, &[("main.typ", text)]));
162 let (line, col) = res!(typst_site(&main, "warning"));
163 let mut inst = Instance::new();
164 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
165 assert_eq!(f.head.kind, DiagnosticKind::MissingFont, "the kind: {:?}", f.head);
166 assert_eq!((f.head.line, f.head.col), (line, door_col(text, line, col)), "at typst's site {}:{}: {:?}", line, col, f.head);
167 assert_eq!(f.head.file, "/main.typ");
168 assert!(f.head.message.starts_with("strict:"), "a strict refusal: {}", f.head.message);
169 assert_eq!(f.head.hint.as_deref(), Some("missing_font \u{d7}1"), "the hint names what refused");
170 // Not strict, the same document is a PDF with the warning beside it.
171 let made = res!(must_pdf(inst.compile_pdf(&project(&[("/main.typ", text)]))));
172 assert!(made.report.diagnostics.iter().any(|d| d.kind == DiagnosticKind::MissingFont), "{:?}", made.report.diagnostics);
173 Ok(())
174}
175
176#[test]
177fn g9_a_missing_import_is_refused_at_typsts_site() -> Outcome<()> {
178 let dir = res!(work_dir("g9-import"));
179 let text = "Intro.\n#import \"gone.typ\": thing\n";
180 let main = res!(write_project(&dir, &[("main.typ", text)]));
181 let (line, col) = res!(typst_site(&main, "error"));
182 let mut inst = Instance::new();
183 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
184 assert_eq!(f.head.kind, DiagnosticKind::MissingFile, "the kind: {:?}", f.head);
185 assert_eq!((f.head.line, f.head.col), (line, door_col(text, line, col)), "at typst's site {}:{}: {:?}", line, col, f.head);
186 assert_eq!(f.head.file, "/main.typ");
187 Ok(())
188}
189
190#[test]
191fn g9_a_missing_image_is_refused_at_typsts_site() -> Outcome<()> {
192 let dir = res!(work_dir("g9-image"));
193 let text = "Text.\n\n#image(\"gone.png\")\n";
194 let main = res!(write_project(&dir, &[("main.typ", text)]));
195 let (line, col) = res!(typst_site(&main, "error"));
196 let mut inst = Instance::new();
197 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
198 assert_eq!(f.head.kind, DiagnosticKind::MissingFile, "the kind: {:?}", f.head);
199 assert_eq!((f.head.line, f.head.col), (line, door_col(text, line, col)), "at typst's site {}:{}: {:?}", line, col, f.head);
200 assert_eq!(f.head.file, "/main.typ");
201 Ok(())
202}
203
204#[test]
205fn g9_a_main_that_sets_no_content_is_refused_at_main_one_one_as_internal_though_typst_raises_nothing() -> Outcome<()> {
206 let dir = res!(work_dir("g9-empty"));
207 let mut inst = Instance::new();
208 for (name, text) in [("empty", ""), ("spaces", " \n\n "), ("set-only", "#set text(size: 11pt)\n"), ("let-only", "#let x = 1\n")] {
209 // Typst compiles each to one blank page and raises nothing at all.
210 let case = dir.join(name);
211 let main = res!(write_project(&case, &[("main.typ", text)]));
212 let (ok, _, stderr) = res!(typst(&["compile", "--diagnostic-format", "short",
213 &main.display().to_string(), &main.with_extension("pdf").display().to_string()]));
214 assert!(ok && stderr.trim().is_empty(), "typst compiles {} clean: {}", name, stderr);
215
216 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
217 assert_eq!(site(&f), "/main.typ:1:1", "{}: {:?}", name, f.head);
218 assert_eq!(f.head.kind, DiagnosticKind::Internal, "{}: {:?}", name, f.head);
219 assert!(f.head.message.contains("sets no content"), "{}: {}", name, f.head.message);
220 // Not strict, the blank page is made.
221 let made = res!(must_pdf(inst.compile_pdf(&project(&[("/main.typ", text)]))));
222 assert_eq!(made.report.pages, 1, "{}", name);
223 }
224 // A main that sets something is not blank.
225 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", "Words.\n")]))));
226 assert_eq!(made.report.pages, 1);
227 Ok(())
228}
229
230/// Typst's own run of `text` as one project: whether it compiled clean, and its pages.
231fn typst_clean_pages(dir: &Path, name: &str, text: &str) -> Outcome<(bool, usize)> {
232 let case = dir.join(name);
233 let main = res!(write_project(&case, &[("main.typ", text)]));
234 let pdf = main.with_extension("pdf");
235 let (ok, _, stderr) = res!(typst(&["compile", "--diagnostic-format", "short",
236 &main.display().to_string(), &pdf.display().to_string()]));
237 let pages = if ok { res!(pdf_pages(&res!(std::fs::read(&pdf)), &case, "typst")) } else { 0 };
238 Ok((ok && stderr.trim().is_empty(), pages))
239}
240
241// A main sets content when its body, once its contexts are resolved and its show rules applied, holds an
242// element other than a space, a paragraph break, a page break or a column break. What it realises to is
243// judged, never what it evaluated to: a `context` that gives nothing is an element until it is resolved, and
244// the furniture of a `set page` is laid out beside the body and is no part of it.
245const BLANK: [(&str, &str); 14] = [
246 ("context-none", "#context none\n"),
247 ("context-empty", "#context []\n"),
248 ("context-of-context", "#context context none\n"),
249 ("context-space", "#context [ ]\n"),
250 ("header-only", "#set page(header: [x])\n"),
251 ("footer-and-folio", "#set page(footer: [y], numbering: \"1\")\n"),
252 ("background-and-fill", "#set page(background: [bg], fill: red)\n"),
253 ("pagebreak", "#pagebreak()\n"),
254 ("metadata", "#metadata(1) <m>\n"),
255 ("show-all", "#show: it => it\n"),
256 ("styled-nothing", "#align(center)[]\n"),
257 ("gone-by-the-last-pass", "#context if query(<a>).len() == 0 [first pass only]\n#metadata(1) <a>\n"),
258 ("set-and-let", "#set text(size: 11pt)\n#let x = 1\n"),
259 ("spaces", " \n\n "),
260];
261
262const CONTENT: [(&str, &str); 9] = [
263 ("vertical-space", "#v(1cm)\n"),
264 ("horizontal-space", "#h(1cm)\n"),
265 ("filled-box", "#box(fill: red, width: 1cm, height: 1cm)\n"),
266 ("empty-box", "#box()\n"),
267 ("context-space", "#context v(1cm)\n"),
268 ("context-text", "#context [hello]\n"),
269 ("furniture-and-body", "#set page(header: [x])\nBody.\n"),
270 ("styled-text", "#text(red)[x]\n"),
271 ("there-by-the-last-pass", "#context if query(<a>).len() > 0 [there]\n#metadata(1) <a>\n"),
272];
273
274#[test]
275fn a_main_whose_body_realises_to_nothing_is_refused_on_every_door_though_typst_raises_nothing() -> Outcome<()> {
276 let dir = res!(work_dir("blank-body"));
277 let mut inst = Instance::new();
278 for (name, text) in BLANK {
279 // Typst lays each out as one blank page, or more for a page break, and says nothing.
280 let (clean, pages) = res!(typst_clean_pages(&dir, name, text));
281 assert!(clean && pages >= 1, "typst compiles {} clean", name);
282 let p = strict(&[("/main.typ", text)]);
283 let pdf = res!(must_fail(inst.compile_pdf(&p)));
284 let svg = match inst.compile_svg(&p) {
285 Ok(_) => return Err(err!("The vector door compiled {}, which sets no content.", name; Test)),
286 Err(f) => f,
287 };
288 let delta = res!(delta_fail(&mut inst, &p));
289 for (door, f) in [("pdf", &pdf), ("svg", &svg), ("delta", &delta)] {
290 assert_eq!(site(f), "/main.typ:1:1", "{} on the {} door: {:?}", name, door, f.head);
291 assert_eq!(f.head.kind, DiagnosticKind::Internal, "{} on the {} door: {:?}", name, door, f.head);
292 assert!(f.head.message.contains("sets no content"), "{} on the {} door: {}", name, door, f.head.message);
293 }
294 // Not strict, the page is made.
295 let made = res!(must_pdf(inst.compile_pdf(&project(&[("/main.typ", text)]))));
296 assert!(made.report.pages >= 1, "{}", name);
297 }
298 Ok(())
299}
300
301#[test]
302fn a_main_with_any_element_in_its_body_passes_strict_though_it_sets_an_empty_looking_page() -> Outcome<()> {
303 let dir = res!(work_dir("content-body"));
304 let mut inst = Instance::new();
305 for (name, text) in CONTENT {
306 let (clean, pages) = res!(typst_clean_pages(&dir, name, text));
307 assert!(clean && pages == 1, "typst compiles {} clean to one page", name);
308 let p = strict(&[("/main.typ", text)]);
309 let pdf = res!(must_pdf(inst.compile_pdf(&p)));
310 assert_eq!(pdf.report.pages, 1, "{}", name);
311 match inst.compile_svg(&p) {
312 Ok(m) => assert_eq!(m.product.len(), 1, "{}", name),
313 Err(f) => return Err(err!("The vector door refused {}: {}", name, f.head; Test)),
314 }
315 let delta = res!(delta_made(&mut inst, &p));
316 assert_eq!(delta.product.order.len(), 1, "{}", name);
317 }
318 // What the body is in the pass that stood decides it: the late content is there, as typst's page says.
319 let late = CONTENT[CONTENT.len() - 1].1;
320 let case = dir.join("late-text");
321 let main = res!(write_project(&case, &[("main.typ", late)]));
322 let pdf = main.with_extension("pdf");
323 let (ok, _, stderr) = res!(typst(&["compile", &main.display().to_string(), &pdf.display().to_string()]));
324 assert!(ok, "{}", stderr);
325 assert_eq!(res!(pdf::text_of(&pdf)), "there");
326 Ok(())
327}
328
329#[test]
330fn a_main_that_is_not_utf8_is_an_encoding_error_at_the_main() -> Outcome<()> {
331 let mut inst = Instance::new();
332 let mut p = Project::default();
333 p.assets.push(("/main.typ".to_string(), vec![b'a', 0xff, 0xfe, b'b']));
334 let f = res!(must_fail(inst.compile_pdf(&p)));
335 assert_eq!(f.head.kind, DiagnosticKind::Encoding, "{:?}", f.head);
336 assert_eq!(site(&f), "/main.typ:0:0");
337 Ok(())
338}
339
340#[test]
341fn a_project_with_no_main_source_is_a_missing_file_error() -> Outcome<()> {
342 let mut inst = Instance::new();
343 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/other.typ", "x")]))));
344 assert_eq!(f.head.kind, DiagnosticKind::MissingFile, "{:?}", f.head);
345 assert_eq!(site(&f), "/main.typ:0:0");
346 Ok(())
347}
348
349#[test]
350fn the_project_is_sealed_so_nothing_resolves_from_the_hosts_filesystem() -> Outcome<()> {
351 // The host does hold the file the project names, so only the seal keeps it out.
352 assert!(Path::new("/etc/passwd").is_file(), "the host holds /etc/passwd");
353 let mut inst = Instance::new();
354 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", "#read(\"/etc/passwd\")\n")]))));
355 assert_eq!(f.head.kind, DiagnosticKind::MissingFile, "{:?}", f.head);
356 assert!(f.head.message.contains("file not found"), "{}", f.head.message);
357 Ok(())
358}
359
360// ┌───────────────────────────────────────────────────────────────────────────┐
361// │ STRICT PASS, BESIDE THE PDF, AND COLUMNS │
362// └───────────────────────────────────────────────────────────────────────────┘
363
364#[test]
365fn a_strict_skeleton_is_one_page_with_no_diagnostic_and_an_import_rename_stays_a_lint_beside_the_pdf() -> Outcome<()> {
366 let dir = res!(work_dir("strict-pass"));
367 let mut inst = Instance::new();
368 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", SKELETON)]))));
369 assert_eq!(made.report.pages, 1);
370 assert!(made.report.diagnostics.is_empty(), "{:?}", made.report.diagnostics);
371 assert!(made.needs.is_empty());
372 let bytes = pdf_of(&made);
373 assert!(bytes.starts_with(b"%PDF-"));
374 assert_eq!(res!(pdf_pages(&bytes, &dir, "skeleton")), 1, "the report's count is the PDF's");
375
376 // The evaluator's own walking skeleton, a show rule and all, passes strict as one page.
377 let fixture = res!(std::fs::read_to_string(
378 Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/skeleton/skeleton.typ")));
379 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", &fixture)]))));
380 assert!(made.report.diagnostics.is_empty() && made.report.pages == 1, "{:?}", made.report.diagnostics);
381 assert_eq!(res!(pdf_pages(&pdf_of(&made), &dir, "fixture")), 1);
382
383 // Typst's own warning stands beside the PDF; strict does not refuse it.
384 let made = res!(must_pdf(inst.compile_pdf(&strict(&[
385 ("/main.typ", "#import \"t.typ\": doc as doc\nBody text.\n"),
386 ("/t.typ", "#let doc = 1\n"),
387 ]))));
388 let lints: Vec<_> = made.report.diagnostics.iter().filter(|d| d.message.contains("unnecessary import rename")).collect();
389 assert_eq!(lints.len(), 1, "{:?}", made.report.diagnostics);
390 assert_eq!(lints[0].kind, DiagnosticKind::Lint);
391 assert_eq!(lints[0].severity.as_str(), "warning");
392 Ok(())
393}
394
395#[test]
396fn a_document_that_does_not_settle_in_five_passes_stands_beside_the_pdf_under_strict() -> Outcome<()> {
397 // A state that feeds itself never settles; Typst stops after five passes with a warning.
398 let text = "Words.\n\
399 #let s = state(\"s\", 0)\n\
400 #context s.update(s.final() + 1)\n\
401 #context [#metadata(s.final()) <probe>]\n";
402 let dir = res!(work_dir("unsettled"));
403 let main = res!(write_project(&dir, &[("main.typ", text)]));
404 let (_, _, stderr) = res!(typst(&["compile", "--diagnostic-format", "short",
405 &main.display().to_string(), &main.with_extension("pdf").display().to_string()]));
406 let typst_unsettled = stderr.contains("did not converge");
407 let mut inst = Instance::new();
408 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
409 let ours = made.report.diagnostics.iter().any(|d| d.kind == DiagnosticKind::Limit);
410 assert_eq!(ours, typst_unsettled, "typst {}, the door {}: {:?}", typst_unsettled, ours, made.report.diagnostics);
411 assert!(typst_unsettled, "the probe document must not settle in typst either: {}", stderr);
412 Ok(())
413}
414
415#[test]
416fn a_character_outside_the_bmp_before_a_site_adds_one_utf16_unit_to_typsts_character_column() -> Outcome<()> {
417 let dir = res!(work_dir("utf16"));
418 let mut inst = Instance::new();
419 // The ideographic space is one UTF-16 unit, the emoji two.
420 for (name, lead) in [("plain", ""), ("bmp", "\u{3000}"), ("astral", "\u{1F600}"), ("astral2", "\u{1F600}\u{1F600}x")] {
421 let text = fmt!("{}#include \"gone.typ\"\n", lead);
422 let main = res!(write_project(&dir.join(name), &[("main.typ", &text)]));
423 let (line, col) = res!(typst_site(&main, "error"));
424 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", &text)]))));
425 let want = door_col(&text, line, col);
426 let astral = lead.chars().filter(|c| c.len_utf16() == 2).count();
427 assert_eq!(f.head.col, col + 1 + astral, "{}: typst column {} plus one plus {} astral", name, col, astral);
428 assert_eq!((f.head.line, f.head.col), (line, want), "{}: {:?}", name, f.head);
429 }
430 Ok(())
431}
432
433#[test]
434fn a_line_break_other_than_the_line_feed_counts_a_line_as_typst_counts_it() -> Outcome<()> {
435 let dir = res!(work_dir("breaks"));
436 let mut inst = Instance::new();
437 for (name, brk) in [("cr", "\r"), ("crlf", "\r\n"), ("ls", "\u{2028}"), ("ff", "\u{c}")] {
438 let text = fmt!("a{}#import \"gone.typ\"\n", brk);
439 let main = res!(write_project(&dir.join(name), &[("main.typ", &text)]));
440 let (line, col) = res!(typst_site(&main, "error"));
441 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", &text)]))));
442 assert_eq!(line, 2, "{}: typst counts the break as one", name);
443 assert_eq!((f.head.line, f.head.col), (line, col + 1), "{}: {:?}", name, f.head);
444 }
445 Ok(())
446}
447
448// The cases of `tools/bench/door_smoke.mjs`, in its order, and the lines both print: the browser's result
449// for each must be this door's, so each is held to `tests/fixtures/door_smoke_lines.txt`.
450const SMOKE: [(&str, &str); 5] = [
451 ("skeleton", SKELETON),
452 ("missing_font", "#set text(font: \"Nope Sans\")\nHello.\n"),
453 ("missing_import", "Intro.\n#import \"gone.typ\": thing\n"),
454 ("missing_image", "Text.\n\n#image(\"gone.png\")\n"),
455 ("empty_main", ""),
456];
457
458#[test]
459fn the_smoke_cases_print_the_lines_the_node_smoke_is_held_to() -> Outcome<()> {
460 let mut inst = Instance::new();
461 let mut got: Vec<String> = Vec::new();
462 for (name, text) in SMOKE {
463 match inst.compile_pdf(&strict(&[("/main.typ", text)])) {
464 Ok(_) => got.push(fmt!("{} ok - -:0:0", name)),
465 Err(f) => got.push(fmt!("{} error {} {}", name, f.head.kind.as_str(), site(&f))),
466 }
467 }
468 let file = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/door_smoke_lines.txt");
469 let want: Vec<String> = res!(std::fs::read_to_string(&file)).lines().filter(|l| !l.is_empty()).map(|l| l.to_string()).collect();
470 assert_eq!(got, want, "the door's lines are the ones the node smoke is held to");
471 Ok(())
472}
473
474// ┌───────────────────────────────────────────────────────────────────────────┐
475// │ ISOLATION, PACKAGES AND FONTS │
476// └───────────────────────────────────────────────────────────────────────────┘
477
478fn noto() -> Outcome<Vec<u8>> {
479 Ok(res!(std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("fe2o3_font").join("fonts").join("NotoSans-Regular.ttf"))))
480}
481
482fn font_file(name: &str) -> Outcome<Vec<u8>> {
483 Ok(res!(std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("fe2o3_font").join("fonts").join(name))))
484}
485
486#[test]
487fn two_projects_in_turn_on_one_instance_share_no_file_and_no_font() -> Outcome<()> {
488 let mut inst = Instance::new();
489 // The first holds a file and a font the second does not.
490 let mut first = strict(&[
491 ("/main.typ", "#import \"/shared.typ\": greeting\n#set text(font: \"Noto Sans\")\n#greeting\n"),
492 ("/shared.typ", "#let greeting = [Hello.]\n"),
493 ]);
494 first.fonts.push(("noto.ttf".to_string(), res!(noto())));
495 let made = res!(must_pdf(inst.compile_pdf(&first)));
496 assert!(made.report.diagnostics.is_empty(), "{:?}", made.report.diagnostics);
497 assert!(inst.font_families(Some(&first)).iter().any(|f| f == "Noto Sans"));
498
499 // The second names both and holds neither.
500 let second = strict(&[("/main.typ", "#import \"/shared.typ\": greeting\n#greeting\n")]);
501 let f = res!(must_fail(inst.compile_pdf(&second)));
502 assert_eq!(f.head.kind, DiagnosticKind::MissingFile, "the first project's file does not resolve: {:?}", f.head);
503 let third = strict(&[("/main.typ", "#set text(font: \"Noto Sans\")\nHello.\n")]);
504 let f = res!(must_fail(inst.compile_pdf(&third)));
505 assert_eq!(f.head.kind, DiagnosticKind::MissingFont, "the first project's font does not resolve: {:?}", f.head);
506 assert!(!inst.font_families(None).iter().any(|f| f == "Noto Sans"), "nor is it listed");
507 assert!(!inst.font_families(Some(&second)).iter().any(|f| f == "Noto Sans"));
508 Ok(())
509}
510
511const PKG: &str = "@local/dpk:0.1.0";
512
513fn pkg_files() -> Vec<(String, Vec<u8>)> {
514 vec![
515 ("typst.toml".to_string(), b"[package]\nname = \"dpk\"\nversion = \"0.1.0\"\nentrypoint = \"lib.typ\"\n".to_vec()),
516 ("lib.typ".to_string(), b"#let greet(n) = [Hello, #n.]\n".to_vec()),
517 ]
518}
519
520#[test]
521fn a_supplied_package_is_imported_and_once_withdrawn_the_compile_fails_with_kind_package_and_names_it_in_needs() -> Outcome<()> {
522 let dir = res!(work_dir("package"));
523 let text = "#import \"@local/dpk:0.1.0\": greet\n#greet(\"door\")\n";
524 let main = res!(write_project(&dir, &[("main.typ", text)]));
525 let mut inst = Instance::new();
526
527 assert_eq!(res!(door::supply_package(PKG, pkg_files())), 2);
528 assert!(res!(door::packages()).iter().any(|s| s == PKG), "{:?}", res!(door::packages()));
529 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
530 assert!(made.needs.is_empty(), "{:?}", made.needs);
531 let path = dir.join("dpk.pdf");
532 res!(std::fs::write(&path, pdf_of(&made)));
533 assert!(res!(pdf::text_of(&path)).contains("Hello, door."), "the package's function ran");
534
535 assert!(res!(door::withdraw_package(PKG)), "it was held");
536 assert!(!res!(door::withdraw_package(PKG)), "and is not held twice");
537 assert!(!res!(door::packages()).iter().any(|s| s == PKG));
538 let (line, col) = res!(typst_site(&main, "error"));
539 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
540 assert_eq!(f.head.kind, DiagnosticKind::Package, "{:?}", f.head);
541 assert_eq!((f.head.line, f.head.col), (line, col + 1), "at typst's site: {:?}", f.head);
542 assert_eq!(f.needs, vec![PKG.to_string()], "the specification, taken at the import");
543 Ok(())
544}
545
546#[test]
547fn needs_lists_each_unsupplied_package_once_in_order_from_the_diagnostic_not_the_message() -> Outcome<()> {
548 let mut inst = Instance::new();
549 // The first unsupplied import stops the compile, so one spec is named; a repeat of it names it once.
550 let f = res!(must_fail(inst.compile_pdf(&project(&[
551 ("/main.typ", "#import \"@preview/zzz-absent:9.9.9\": x\n#import \"@preview/zzz-absent:9.9.9\": y\n"),
552 ]))));
553 assert_eq!(f.needs, vec!["@preview/zzz-absent:9.9.9".to_string()]);
554 Ok(())
555}
556
557#[test]
558fn font_families_are_what_the_font_book_resolves_and_what_typst_adds_for_the_same_files() -> Outcome<()> {
559 let dir = res!(work_dir("families"));
560 let noto = res!(font_file("NotoSans-Regular.ttf"));
561 let nmono = res!(font_file("NotoSansMono-Regular.ttf"));
562 let wide = res!(font_file("DejaVuSans.ttf"));
563 let ttc = res!(collection_of(&[&noto, &nmono]));
564 // A collection of two families, and a file whose name is another family's.
565 let fonts = dir.join("fonts");
566 res!(std::fs::create_dir_all(&fonts));
567 res!(std::fs::write(fonts.join("pair.ttc"), &ttc));
568 res!(std::fs::write(fonts.join("Otherface-Bold.ttf"), &wide));
569 let mut p = strict(&[("/main.typ", "Hello.\n")]);
570 p.fonts.push(("pair.ttc".to_string(), ttc.clone()));
571 p.fonts.push(("Otherface-Bold.ttf".to_string(), wide.clone()));
572
573 let inst = Instance::new();
574 let none = inst.font_families(None);
575 let listed = inst.font_families(Some(&p));
576 for want in ["Noto Sans", "Noto Sans Mono", "DejaVu Sans"] {
577 assert!(listed.iter().any(|f| f == want), "{} is listed: {:?}", want, listed);
578 }
579 assert!(!listed.iter().any(|f| f == "Otherface"), "never guessed from a file name: {:?}", listed);
580 let mut sorted = listed.clone();
581 sorted.sort_by_key(|f| f.to_lowercase());
582 assert_eq!(listed, sorted, "sorted");
583 for embedded in ["Libertinus Serif", "New Computer Modern Math", "DejaVu Sans Mono"] {
584 assert!(none.iter().any(|f| f == embedded), "{} is embedded: {:?}", embedded, none);
585 }
586
587 // Typst lists the same families for the same files, apart from the faces each carries of its own.
588 let (_, out, err) = res!(typst(&["fonts", "--ignore-system-fonts", "--font-path", &fonts.display().to_string()]));
589 let theirs: BTreeSet<String> = out.lines().map(|l| l.trim().to_string()).filter(|l| !l.is_empty()).collect();
590 assert!(!theirs.is_empty(), "typst lists fonts: {}", err);
591 let embedded_typst = ["DejaVu Sans Mono", "Libertinus Serif", "New Computer Modern", "New Computer Modern Math"];
592 let added_typst: BTreeSet<&String> = theirs.iter().filter(|f| !embedded_typst.contains(&f.as_str())).collect();
593 let added_ours: BTreeSet<&String> = listed.iter().filter(|f| !none.contains(f)).collect();
594 assert_eq!(added_ours, added_typst, "the families the project's files add");
595
596 // Every listed family is one a compile sets, and one that is not listed is refused.
597 for family in &listed {
598 let mut q = strict(&[("/main.typ", &fmt!("#set text(font: \"{}\")\nHello.\n", family))]);
599 q.fonts = p.fonts.clone();
600 let mut inst = Instance::new();
601 let made = res!(must_pdf(inst.compile_pdf(&q)));
602 assert!(made.report.diagnostics.is_empty(), "{} is listed, so it sets: {:?}", family, made.report.diagnostics);
603 }
604 let mut inst = Instance::new();
605 let mut q = strict(&[("/main.typ", "#set text(font: \"Otherface\")\nHello.\n")]);
606 q.fonts = p.fonts.clone();
607 let f = res!(must_fail(inst.compile_pdf(&q)));
608 assert_eq!(f.head.kind, DiagnosticKind::MissingFont);
609 Ok(())
610}
611
612#[test]
613fn the_font_book_adds_every_face_of_a_collection_and_a_fork_leaves_its_base_alone() -> Outcome<()> {
614 let noto = res!(font_file("NotoSans-Regular.ttf"));
615 let nmono = res!(font_file("NotoSansMono-Regular.ttf"));
616 let ttc = res!(collection_of(&[&noto, &nmono]));
617 let base = res!(FontBook::embedded());
618 let held = base.len();
619 let mut fork = base.fork();
620 assert_eq!(fork.len(), held, "a fork starts as its base");
621 let first = res!(fork.add(ttc));
622 assert_eq!(fork.len(), held + 2, "both faces of the collection");
623 assert_eq!(first, held, "the id of the first face is returned");
624 assert!(fork.has_family("noto sans") && fork.has_family("noto sans mono"));
625 assert_eq!(base.len(), held, "the base is unchanged");
626 assert!(!base.has_family("noto sans"), "and does not hold what its fork added");
627 Ok(())
628}
629
630// ┌───────────────────────────────────────────────────────────────────────────┐
631// │ INFO, THE VECTOR DOOR AND THE TYPST VERSION │
632// └───────────────────────────────────────────────────────────────────────────┘
633
634fn info_field(pdf: &Path, key: &str) -> Outcome<String> {
635 let info = res!(tool("pdfinfo", &[], pdf, &[]));
636 Ok(info.lines().find_map(|l| l.strip_prefix(key)).map(|v| v.trim().to_string()).unwrap_or_default())
637}
638
639#[test]
640fn a_tilde_in_the_title_and_author_gives_the_pdfinfo_text_typst_gives() -> Outcome<()> {
641 let dir = res!(work_dir("info"));
642 let text = "#set document(title: [Hello~world: it's ~ here], author: \"A~B\")\nBody.\n";
643 let main = res!(write_project(&dir, &[("main.typ", text)]));
644 let theirs = dir.join("typst.pdf");
645 res!(pdf::typst_pdf(&main, &theirs));
646 let mut inst = Instance::new();
647 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
648 let ours = dir.join("austenite.pdf");
649 res!(std::fs::write(&ours, pdf_of(&made)));
650 for key in ["Title:", "Author:"] {
651 let (a, t) = (res!(info_field(&ours, key)), res!(info_field(&theirs, key)));
652 assert!(!t.is_empty(), "typst writes {}", key);
653 assert_eq!(a, t, "{} as pdfinfo reads it", key);
654 }
655 assert!(res!(info_field(&ours, "Title:")).contains('\u{a0}'), "the tilde is a non-breaking space");
656 Ok(())
657}
658
659#[test]
660fn the_vector_door_gives_one_svg_per_page_and_refuses_as_the_pdf_door_does() -> Outcome<()> {
661 let dir = res!(work_dir("vector"));
662 // The final page count is read before it is known, so the document takes a second pass, whose pages are
663 // the ones kept: a pass that was discarded must leave none behind.
664 let text = "#set page(height: 60pt, margin: 5pt)\nOne.\n#pagebreak()\nTwo.\n#pagebreak()\n\
665 Three of #context counter(page).final().first().\n";
666 let mut inst = Instance::new();
667 let pdf = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
668 let svg = match inst.compile_svg(&strict(&[("/main.typ", text)])) {
669 Ok(m) => m,
670 Err(f) => return Err(err!("The vector door refused: {}", f.head; Test)),
671 };
672 assert_eq!(svg.product.len(), 3);
673 assert_eq!(svg.report.pages, 3);
674 assert_eq!(res!(pdf_pages(&pdf_of(&pdf), &dir, "vector")), 3, "the PDF door's count");
675 for (i, s) in svg.product.iter().enumerate() {
676 assert!(s.contains("<svg"), "page {} is an SVG document", i + 1);
677 assert!(s.contains("class=\"tsel\""), "page {} carries its selectable text layer", i + 1);
678 }
679 let refused = match inst.compile_svg(&strict(&[("/main.typ", "")])) {
680 Ok(_) => return Err(err!("The vector door compiled a main that sets no content."; Test)),
681 Err(f) => f,
682 };
683 assert_eq!(site(&refused), "/main.typ:1:1");
684 Ok(())
685}
686
687#[test]
688fn engine_info_names_the_typst_version_the_evaluator_follows() -> Outcome<()> {
689 let (ok, out, err) = res!(typst(&["--version"]));
690 assert!(ok, "{}", err);
691 let want = out.trim().trim_start_matches("typst ").split_whitespace().next().unwrap_or("").to_string();
692 assert_eq!(package::version_text(package::TYPST_VERSION), want, "the version engineInfo().typst carries");
693 Ok(())
694}
695
696// ┌───────────────────────────────────────────────────────────────────────────┐
697// │ THE DELTA DOOR │
698// └───────────────────────────────────────────────────────────────────────────┘
699
700const NAMES: [&str; 6] = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot"];
701
702/// Six pages of one short paragraph each; `edit` sets one letter of the word in that page's paragraph.
703fn six_pages(edit: Option<usize>) -> String {
704 let mut t = String::from("#set page(width: 220pt, height: 100pt, margin: 12pt)\n");
705 for (k, name) in NAMES.iter().enumerate() {
706 if k > 0 {
707 t.push_str("#pagebreak()\n");
708 }
709 let word = if edit == Some(k) { "quarts" } else { "quartz" };
710 t.push_str(&fmt!("Page {} holds the {} of paragraph {} alone.\n", name, word, k));
711 }
712 t
713}
714
715fn delta_made(inst: &mut Instance, p: &Project) -> Outcome<Made<PageDelta>> {
716 match inst.compile_delta(p) {
717 Ok(m) => Ok(m),
718 Err(f) => Err(err!("The delta door refused: {} {:?}", f.head, f.head.kind; Test)),
719 }
720}
721
722fn delta_fail(inst: &mut Instance, p: &Project) -> Outcome<Failure> {
723 match inst.compile_delta(p) {
724 Ok(_) => Err(err!("The delta door produced a delta where it should have refused."; Test)),
725 Err(f) => Ok(f),
726 }
727}
728
729/// The text a page's SVG offers for selection, with its whitespace removed: the characters of its `.tsel` layer.
730fn text_layer(svg: &str) -> String {
731 let body = match svg.find("<text class=\"tsel\">") {
732 Some(i) => &svg[i + "<text class=\"tsel\">".len()..],
733 None => return String::new(),
734 };
735 let body = body.split("</text>").next().unwrap_or("");
736 let mut out = String::new();
737 let mut tag = false;
738 for c in body.chars() {
739 match c {
740 '<' => tag = true,
741 '>' => tag = false,
742 _ if !tag => out.push(c),
743 _ => (),
744 }
745 }
746 let out = out.replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&amp;", "&");
747 out.split_whitespace().collect()
748}
749
750/// Typst's PDF of `text` and each page's text, whitespace removed: the external fact the delta is held to.
751fn typst_page_texts(dir: &Path, name: &str, text: &str) -> Outcome<Vec<String>> {
752 let case = dir.join(name);
753 let main = res!(write_project(&case, &[("main.typ", text)]));
754 let pdf = main.with_extension("pdf");
755 let (ok, _, stderr) = res!(typst(&["compile", &main.display().to_string(), &pdf.display().to_string()]));
756 assert!(ok, "typst compiles {}: {}", name, stderr);
757 let pages = res!(pdf_pages(&res!(std::fs::read(&pdf)), &case, "pages"));
758 let mut out = Vec::with_capacity(pages);
759 for n in 1..=pages {
760 let page = n.to_string();
761 let t = res!(tool("pdftotext", &["-f", &page, "-l", &page], &pdf, &["-"]));
762 out.push(t.split_whitespace().collect::<String>());
763 }
764 Ok(out)
765}
766
767#[test]
768fn the_delta_door_sends_every_page_once_then_nothing_while_the_consumer_holds_them() -> Outcome<()> {
769 let mut inst = Instance::new();
770 let mut p = strict(&[("/main.typ", &six_pages(None))]);
771 let first = res!(delta_made(&mut inst, &p));
772 let d = &first.product;
773 assert!(d.reset && d.version == 1, "a first compile is a reset at version 1");
774 assert_eq!(first.report.pages, 6);
775 assert_eq!(d.order.len(), first.report.pages, "order names every page");
776 assert_eq!(d.changed.len(), 6, "a reset carries every page");
777 let ids: BTreeSet<u64> = d.order.iter().copied().collect();
778 assert_eq!(ids.len(), 6, "six different pages, six different ids");
779 for (id, svg) in &d.changed {
780 assert!(ids.contains(id) && svg.contains("<svg") && svg.contains("class=\"tsel\""), "{}", id);
781 }
782 // The consumer holds every page, so the same document sends none, and the version moves on.
783 p.known = d.order.clone();
784 let second = res!(delta_made(&mut inst, &p));
785 assert!(!second.product.reset && second.product.changed.is_empty() && second.product.version == 2);
786 assert_eq!(second.product.order, first.product.order);
787 // A cleared cache is a reset: a resend over a blank.
788 p.known = Vec::new();
789 let third = res!(delta_made(&mut inst, &p));
790 assert!(third.product.reset && third.product.changed.len() == 6 && third.product.version == 3);
791 Ok(())
792}
793
794#[test]
795fn a_one_letter_edit_sends_exactly_the_pages_whose_text_typst_says_changed() -> Outcome<()> {
796 let dir = res!(work_dir("delta-edit"));
797 let was = res!(typst_page_texts(&dir, "was", &six_pages(None)));
798 assert_eq!(was.len(), 6);
799 let mut inst = Instance::new();
800 let mut p = strict(&[("/main.typ", &six_pages(None))]);
801 let first = res!(delta_made(&mut inst, &p));
802 for (n, k) in [0usize, 2, 5].iter().enumerate() {
803 let text = six_pages(Some(*k));
804 let now = res!(typst_page_texts(&dir, &fmt!("now{}", n), &text));
805 let differ: Vec<usize> = (0..6).filter(|i| was[*i] != now[*i]).collect();
806 assert_eq!(differ, vec![*k], "typst changes the text of page {} alone", k + 1);
807
808 p.known = first.product.order.clone();
809 p.sources = vec![("/main.typ".to_string(), text)];
810 let after = res!(delta_made(&mut inst, &p));
811 let d = &after.product;
812 assert!(!d.reset, "the consumer holds pages");
813 assert_eq!(d.order.len(), 6);
814 let want: BTreeSet<u64> = differ.iter().map(|i| d.order[*i]).collect();
815 let got: BTreeSet<u64> = d.changed.iter().map(|(id, _)| *id).collect();
816 assert_eq!(got, want, "changed holds the pages typst's text says changed (edit in page {})", k + 1);
817 for i in 0..6 {
818 if differ.contains(&i) {
819 assert_ne!(d.order[i], first.product.order[i], "page {} has a new id", i + 1);
820 } else {
821 assert_eq!(d.order[i], first.product.order[i], "page {} keeps its id", i + 1);
822 }
823 }
824 // What is sent is the edited page, not an old copy of it.
825 match d.changed.first() {
826 Some((_, svg)) => assert!(text_layer(svg).contains("quarts"), "the page sent carries the edit"),
827 None => return Err(err!("The edit sent no page."; Test)),
828 }
829 }
830 Ok(())
831}
832
833#[test]
834fn a_page_of_a_discarded_pass_never_reaches_the_delta() -> Outcome<()> {
835 // The final page count is read before it is known, so the document takes a second pass and the pages of
836 // the first are discarded: page three reads its total, and says another thing the first time.
837 let text = "#set page(width: 220pt, height: 60pt, margin: 8pt)\nOne.\n#pagebreak()\nTwo.\n#pagebreak()\n\
838 Three of #context counter(page).final().first().\n";
839 let dir = res!(work_dir("delta-passes"));
840 let theirs = res!(typst_page_texts(&dir, "typst", text));
841 assert_eq!(theirs, vec!["One.", "Two.", "Threeof3."], "typst's pages");
842 let mut inst = Instance::new();
843 let p = strict(&[("/main.typ", text)]);
844 let made = res!(delta_made(&mut inst, &p));
845 let d = &made.product;
846 assert_eq!(made.report.pages, 3);
847 assert_eq!(d.order.len(), 3, "the order is the pages of the pass that stood");
848 let ids: BTreeSet<u64> = d.order.iter().copied().collect();
849 assert_eq!(d.changed.len(), ids.len(), "each id once");
850 let vector = match inst.compile_svg(&p) {
851 Ok(m) => m,
852 Err(f) => return Err(err!("The vector door refused: {}", f.head; Test)),
853 };
854 for (i, id) in d.order.iter().enumerate() {
855 let sent = d.changed.iter().find(|(c, _)| c == id).map(|(_, svg)| svg);
856 match sent {
857 Some(svg) => {
858 assert_eq!(svg, &vector.product[i], "page {} is the page the final pass drew", i + 1);
859 assert_eq!(text_layer(svg), theirs[i], "and carries the text typst's page does");
860 },
861 None => return Err(err!("Page {} of the order was not sent on a reset.", i + 1; Test)),
862 }
863 }
864 Ok(())
865}
866
867// A document that settles in two passes: page three reads the page total, which the first pass has not seen.
868const TWO_PASSES: &str = "#set page(width: 220pt, height: 60pt, margin: 8pt)\nOne.\n#pagebreak()\nTwo.\n#pagebreak()\n\
869 Three of #context counter(page).final().first().\n";
870const ONE_PASS: &str = "#set page(width: 220pt, height: 60pt, margin: 8pt)\nOne.\n#pagebreak()\nTwo.\n#pagebreak()\n\
871 Three of 3.\n";
872
873#[test]
874fn an_open_draws_the_pages_of_the_pass_that_stands_and_no_others() -> Outcome<()> {
875 // The first pass of TWO_PASSES is discarded, and its three pages are not drawn: the sink counts each SVG it
876 // draws, and the count is the pages sent, not the pages of both passes.
877 let mut inst = Instance::new();
878 let two = res!(delta_made(&mut inst, &strict(&[("/main.typ", TWO_PASSES)])));
879 assert_eq!(two.report.pages, 3);
880 assert_eq!(two.product.changed.len(), 3);
881 assert_eq!(two.product.rendered, 3, "three pages drawn, none of the discarded pass's");
882 // A document that settles in one pass draws the same.
883 let one = res!(delta_made(&mut inst, &strict(&[("/main.typ", ONE_PASS)])));
884 assert_eq!(one.product.rendered, 3);
885 // An edit to page one, the consumer holding the rest: one page is drawn, though the edited document also
886 // takes two passes and its first pass holds a page three that the consumer does not.
887 let mut p = strict(&[("/main.typ", TWO_PASSES)]);
888 p.known = two.product.order.clone();
889 let same = res!(delta_made(&mut inst, &p));
890 assert_eq!((same.product.changed.len(), same.product.rendered), (0, 0), "an unchanged recompile draws nothing");
891 let edited = TWO_PASSES.replacen("One.", "Uno.", 1);
892 let mut q = strict(&[("/main.typ", &edited)]);
893 q.known = two.product.order.clone();
894 let made = res!(delta_made(&mut inst, &q));
895 assert_eq!(made.product.order.len(), 3);
896 assert_eq!((made.product.changed.len(), made.product.rendered), (1, 1), "only the edited page is drawn");
897 Ok(())
898}
899
900/// A consumer of changed pages that keeps the ids in the order they arrive and the bytes it was handed.
901#[derive(Default)]
902struct Tape {
903 ids: Vec<u64>,
904 bytes: usize,
905}
906
907impl Changed for Tape {
908 fn take(&mut self, id: u64, svg: String) -> Outcome<()> {
909 assert!(svg.contains("class=\"tsel\""), "page {} carries its text layer", id);
910 self.ids.push(id);
911 self.bytes += svg.len();
912 Ok(())
913 }
914}
915
916#[test]
917fn each_changed_page_goes_to_the_consumer_in_reading_order_as_it_is_drawn() -> Outcome<()> {
918 let mut inst = Instance::new();
919 let p = strict(&[("/main.typ", &six_pages(None))]);
920 let whole = res!(delta_made(&mut inst, &p));
921 let sent = match inst.compile_delta_into(&p, Tape::default()) {
922 Ok(m) => m,
923 Err(f) => return Err(err!("The delta door refused: {}", f.head; Test)),
924 };
925 let (head, tape) = sent.product;
926 assert_eq!(head.order, whole.product.order, "the order is the same through either entry");
927 assert_eq!(tape.ids, head.order, "six different pages arrive in reading order");
928 assert_eq!(head.rendered as usize, tape.ids.len());
929 let held: usize = whole.product.changed.iter().map(|(_, svg)| svg.len()).sum();
930 assert_eq!(tape.bytes, held, "the bytes handed over are the bytes the whole delta holds");
931 assert_eq!(head.version, whole.product.version + 1, "each compile steps the tick, by either entry");
932 Ok(())
933}
934
935#[test]
936fn a_refusal_leaves_the_delta_version_where_it_was() -> Outcome<()> {
937 let mut inst = Instance::new();
938 let mut p = strict(&[("/main.typ", &six_pages(None))]);
939 let one = res!(delta_made(&mut inst, &p));
940 p.known = one.product.order.clone();
941 assert_eq!(one.product.version, 1);
942 assert_eq!(res!(delta_made(&mut inst, &p)).product.version, 2);
943 let mut bad = Vec::new();
944 // A strict refusal of a warning, one of a body that sets nothing, and a hard error.
945 bad.push(strict(&[("/main.typ", "#set text(font: \"Nope Sans\")\nx\n")]));
946 bad.push(strict(&[("/main.typ", "#context none\n")]));
947 bad.push(strict(&[("/main.typ", "#nonesuch()\n")]));
948 let kinds = [DiagnosticKind::MissingFont, DiagnosticKind::Internal, DiagnosticKind::UnknownVariable];
949 for (b, kind) in bad.iter_mut().zip(kinds) {
950 b.known = p.known.clone();
951 let f = res!(delta_fail(&mut inst, b));
952 assert_eq!(f.head.kind, kind, "{:?}", f.head);
953 }
954 assert_eq!(res!(delta_made(&mut inst, &p)).product.version, 3, "the three refusals left the version where it was");
955 // Not strict, the same missing font is a delta, and it steps the tick.
956 let mut soft = project(&[("/main.typ", "#set text(font: \"Nope Sans\")\nx\n")]);
957 soft.known = Vec::new();
958 let made = res!(delta_made(&mut inst, &soft));
959 assert_eq!(made.product.version, 4);
960 assert!(made.report.diagnostics.iter().any(|d| d.kind == DiagnosticKind::MissingFont), "{:?}", made.report.diagnostics);
961 Ok(())
962}
963
964#[test]
965fn the_delta_result_carries_pages_diagnostics_skipped_and_needs_as_the_other_doors_do() -> Outcome<()> {
966 let mut inst = Instance::new();
967 let gradient = "#rect(width: 2cm, height: 1cm, fill: gradient.linear(red, blue))\nBody.\n";
968 // Not strict, a construct passed over stands beside the delta, and the line names it.
969 let made = res!(delta_made(&mut inst, &project(&[("/main.typ", gradient)])));
970 assert_eq!(made.report.pages, 1);
971 assert!(made.report.skipped.as_deref().unwrap_or("").contains("gradients"), "{:?}", made.report.skipped);
972 assert!(made.report.diagnostics.iter().any(|d| d.kind == DiagnosticKind::Unsupported), "{:?}", made.report.diagnostics);
973 assert!(made.needs.is_empty());
974 // Strict, the same document is refused as the PDF door refuses it, its sites and skip line with it.
975 let f = res!(delta_fail(&mut inst, &strict(&[("/main.typ", gradient)])));
976 let g = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", gradient)]))));
977 assert_eq!(f.head, g.head);
978 assert_eq!(f.rest, g.rest);
979 assert_eq!(f.skipped, g.skipped);
980 assert_eq!(f.pages, Some(1));
981 // An unsupplied package is named in `needs`.
982 let f = res!(delta_fail(&mut inst, &project(&[("/main.typ", "#import \"@preview/zzz-absent:9.9.9\": x\n")])));
983 assert_eq!(f.head.kind, DiagnosticKind::Package, "{:?}", f.head);
984 assert_eq!(f.needs, vec!["@preview/zzz-absent:9.9.9".to_string()]);
985 Ok(())
986}
987
988#[test]
989fn a_page_that_differs_only_in_its_face_has_another_id() -> Outcome<()> {
990 // Monospaced faces share their advances and, in a family, their glyph ids: the same word in the regular, the
991 // bold and the oblique face is placed alike, and the page is a different one. Typst says so too.
992 let dir = res!(work_dir("delta-faces"));
993 let regular = "#set page(width: 220pt, height: 60pt, margin: 8pt)\n#raw(\"hello world\")\n";
994 let bold = "#set page(width: 220pt, height: 60pt, margin: 8pt)\n#strong(raw(\"hello world\"))\n";
995 let oblique = "#set page(width: 220pt, height: 60pt, margin: 8pt)\n#emph(raw(\"hello world\"))\n";
996 let mut fonts: Vec<String> = Vec::new();
997 for (name, text) in [("regular", regular), ("bold", bold), ("oblique", oblique)] {
998 let case = dir.join(name);
999 let main = res!(write_project(&case, &[("main.typ", text)]));
1000 let pdf = main.with_extension("pdf");
1001 let (ok, _, stderr) = res!(typst(&["compile", &main.display().to_string(), &pdf.display().to_string()]));
1002 assert!(ok, "{}", stderr);
1003 fonts.push(res!(tool("pdffonts", &[], &pdf, &[])));
1004 }
1005 assert!(fonts[0].contains("DejaVuSansMono") && !fonts[0].contains("Bold") && !fonts[0].contains("Oblique"), "{}", fonts[0]);
1006 assert!(fonts[1].contains("DejaVuSansMono-Bold"), "typst sets the bold face: {}", fonts[1]);
1007 assert!(fonts[2].contains("Oblique"), "typst sets the oblique face: {}", fonts[2]);
1008
1009 let mut inst = Instance::new();
1010 let mut p = strict(&[("/main.typ", regular)]);
1011 let was = res!(delta_made(&mut inst, &p));
1012 p.known = was.product.order.clone();
1013 for (name, text) in [("bold", bold), ("oblique", oblique)] {
1014 p.sources = vec![("/main.typ".to_string(), text.to_string())];
1015 let now = res!(delta_made(&mut inst, &p));
1016 assert_ne!(now.product.order, was.product.order, "the {} page is not the regular one", name);
1017 assert_eq!(now.product.changed.len(), 1, "and is sent: {}", name);
1018 assert_ne!(now.product.changed[0].1, was.product.changed[0].1, "and is drawn otherwise: {}", name);
1019 }
1020 Ok(())
1021}
1022
1023// ┌───────────────────────────────────────────────────────────────────────────┐
1024// │ queryProject │
1025// └───────────────────────────────────────────────────────────────────────────┘
1026
1027const BOOK: &str = "#set page(height: 140pt, margin: 10pt)\n\
1028 = First <a>\n\
1029 #context [#metadata(here().page()) <pp>]\n\
1030 #metadata((k: 1, s: \"x\", l: (1, 2.5, none, true))) <m1>\n\
1031 #figure(table(columns: 2)[a][b], caption: [Tbl]) <t1>\n\
1032 #pagebreak()\n\
1033 == Second <b>\n\
1034 #context [#metadata(here().page()) <pp>]\n\
1035 #figure(table[c], caption: [Tbl2]) <t2>\n\
1036 #pagebreak()\n\
1037 = Third\n\
1038 #context [#metadata(here().page()) <pp>]\n";
1039
1040fn typst_query(main: &Path, selector: &str, field: Option<&str>) -> Outcome<J> {
1041 let mut args = vec!["query".to_string(), "--format".to_string(), "json".to_string()];
1042 if let Some(f) = field {
1043 args.push("--field".to_string());
1044 args.push(f.to_string());
1045 }
1046 args.push(main.display().to_string());
1047 args.push(selector.to_string());
1048 let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
1049 let (ok, out, err) = res!(typst(&refs));
1050 if !ok {
1051 return Err(err!("typst query {} failed: {}", selector, err; Test));
1052 }
1053 json::parse(&out)
1054}
1055
1056fn arr(j: &J) -> Outcome<&Vec<J>> {
1057 match j {
1058 J::Arr(a) => Ok(a),
1059 other => Err(err!("Expected a JSON array, found {}", other.render(); Test)),
1060 }
1061}
1062
1063#[test]
1064fn query_project_fields_equal_typst_query_and_pages_equal_a_here_page_probe() -> Outcome<()> {
1065 let dir = res!(work_dir("query"));
1066 let main = res!(write_project(&dir, &[("main.typ", BOOK)]));
1067 let mut inst = Instance::new();
1068 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", BOOK)]))));
1069
1070 // Every page a probe saw, in document order, one for each heading.
1071 let probes: Vec<i64> = res!(arr(&res!(typst_query(&main, "<pp>", Some("value"))))).iter().filter_map(|j| j.as_i64()).collect();
1072 assert_eq!(probes, [1, 2, 3], "the probe's own pages");
1073
1074 for selector in ["heading", "<a>", "figure.where(kind: table)", "metadata"] {
1075 let theirs = res!(typst_query(&main, selector, None));
1076 let theirs = res!(arr(&theirs));
1077 let ours = match inst.query(selector, "") {
1078 Some(r) => r,
1079 None => return Err(err!("The door answered null for {}.", selector; Test)),
1080 };
1081 assert_eq!(ours.len(), theirs.len(), "{}: the same count", selector);
1082 for (o, t) in ours.iter().zip(theirs.iter()) {
1083 assert_eq!(Some(o.kind.as_str()), t.get("func").and_then(|f| f.as_str()), "{}: kind", selector);
1084 let label = t.get("label").and_then(|l| l.as_str()).map(|l| l.trim_start_matches('<').trim_end_matches('>').to_string());
1085 assert_eq!(o.label, label, "{}: label", selector);
1086 if o.kind == "heading" {
1087 assert_eq!(o.level, t.get("level").and_then(|l| l.as_i64()), "{}: level", selector);
1088 let body = t.get("body").map(|b| b.render()).unwrap_or_default();
1089 let title = o.title.clone().unwrap_or_default();
1090 assert!(body.contains(&fmt!("\"{}\"", title)), "{}: title {:?} is in typst's body {}", selector, title, body);
1091 }
1092 }
1093 }
1094
1095 // `metadata` with `--field value`: the payloads.
1096 let want = res!(typst_query(&main, "<m1>", Some("value")));
1097 let want = res!(arr(&want));
1098 let got = match inst.query("<m1>", "") {
1099 Some(r) => r,
1100 None => return Err(err!("null for <m1>"; Test)),
1101 };
1102 assert_eq!(got.len(), 1);
1103 let text = got[0].value.clone().unwrap_or_default();
1104 assert_eq!(res!(json::parse(&text)).render(), want[0].render(), "the metadata value, as typst serialises it");
1105
1106 // A heading's page is the page its probe stood on.
1107 let heads = match inst.query("heading", "") {
1108 Some(r) => r,
1109 None => return Err(err!("null for heading"; Test)),
1110 };
1111 let pages: Vec<i64> = heads.iter().filter_map(|h| h.page.map(|p| p as i64)).collect();
1112 assert_eq!(pages, probes, "the introspector's page equals typst's here().page()");
1113
1114 // `field` selects one field.
1115 let levels = match inst.query("heading", "level") {
1116 Some(r) => r,
1117 None => return Err(err!("null for heading level"; Test)),
1118 };
1119 let levels: Vec<String> = levels.iter().map(|r| r.value.clone().unwrap_or_default()).collect();
1120 assert_eq!(levels, ["1", "2", "1"]);
1121 Ok(())
1122}
1123
1124#[test]
1125fn a_query_answers_null_rather_than_a_wrong_row() -> Outcome<()> {
1126 let mut inst = Instance::new();
1127 assert!(inst.query("heading", "").is_none(), "nothing has compiled");
1128 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", BOOK)]))));
1129 assert!(inst.query("nonesuch(", "").is_none(), "a selector that does not parse");
1130 assert!(inst.query("nonesuch", "").is_none(), "a selector that does not evaluate");
1131 assert!(inst.query("1 + 1", "").is_none(), "a value that is no selector");
1132 // A field typst writes as content, a length or a colour is no row at all.
1133 assert!(inst.query("figure", "caption").is_none(), "content is not written as a plain value");
1134 assert!(inst.query("heading", "body").is_none(), "nor is a heading's body");
1135 Ok(())
1136}
1137
1138#[test]
1139fn a_failed_compile_clears_the_kept_introspector() -> Outcome<()> {
1140 let mut inst = Instance::new();
1141 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", BOOK)]))));
1142 assert_eq!(inst.query("heading", "").map(|r| r.len()), Some(3));
1143 // A hard error.
1144 res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", "= Gone\n#import \"nowhere.typ\": x\n")]))));
1145 assert!(inst.query("heading", "").is_none(), "the rows of before a failed compile are gone");
1146 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", BOOK)]))));
1147 assert_eq!(inst.query("heading", "").map(|r| r.len()), Some(3));
1148 // A strict refusal.
1149 res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", "= Gone\n#set text(font: \"Nope Sans\")\nx\n")]))));
1150 assert!(inst.query("heading", "").is_none(), "a refused compile answers no query either");
1151 Ok(())
1152}
1153
1154#[test]
1155fn the_live_view_compiles_warm_and_the_pdf_export_cold_and_all_three_agree_with_a_fresh_instance() -> Outcome<()> {
1156 let edited = BOOK.replacen("First <a>", "Firsq <a>", 1);
1157 let mut inst = Instance::new();
1158 let p = project(&[("/main.typ", BOOK)]);
1159 let first = match inst.compile_delta(&p) {
1160 Ok(m) => m,
1161 Err(f) => return Err(err!("The delta refused: {}", f.head; Test)),
1162 };
1163 assert_eq!(inst.query("heading", "").map(|r| r.len()), Some(3), "the delta leaves its introspector for queries");
1164 // A failure between two good compiles answers no query, and the compile after it still answers.
1165 res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", "= Gone\n#import \"nowhere.typ\": x\n")]))));
1166 assert!(inst.query("heading", "").is_none());
1167 let p2 = project(&[("/main.typ", edited.as_str())]);
1168 let svg = match inst.compile_svg(&p2) {
1169 Ok(m) => m.product,
1170 Err(f) => return Err(err!("The vector door refused: {}", f.head; Test)),
1171 };
1172 assert_eq!(inst.query("heading", "").map(|r| r.len()), Some(3));
1173 let fresh_svg = match Instance::new().compile_svg(&p2) {
1174 Ok(m) => m.product,
1175 Err(f) => return Err(err!("The vector door refused: {}", f.head; Test)),
1176 };
1177 assert_eq!(svg, fresh_svg, "a warm vector compile equals a cold one");
1178 let warm = pdf_of(&res!(must_pdf(inst.compile_pdf(&p2))));
1179 let cold = pdf_of(&res!(must_pdf(Instance::new().compile_pdf(&p2))));
1180 assert!(warm == cold, "the PDF export is the same bytes with or without the compiles before it");
1181 assert!(first.report.pages > 0);
1182 Ok(())
1183}
1184
1185#[test]
1186fn heading_rows_keep_title_a_plain_string_and_page_a_one_based_number_across_pages() -> Outcome<()> {
1187 let text = "#set page(height: 80pt, margin: 8pt)\n\
1188 = Intro\nWords here.\n#pagebreak()\n\
1189 == Methods and *bold* parts\nMore words.\n#pagebreak()\n\
1190 === Deep\nStill more.\n#pagebreak()\n\
1191 = Close\nEnd.\n";
1192 let mut inst = Instance::new();
1193 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
1194 assert_eq!(made.report.pages, 4);
1195 let rows = match inst.query("heading", "") {
1196 Some(r) => r,
1197 None => return Err(err!("The rail's query answered null."; Test)),
1198 };
1199 let got: Vec<(String, u32, i64, String)> = rows.iter()
1200 .map(|r| (r.title.clone().unwrap_or_default(), r.page.unwrap_or(0), r.level.unwrap_or(0), r.kind.clone()))
1201 .collect();
1202 let want = [
1203 ("Intro".to_string(), 1u32, 1i64, "heading".to_string()),
1204 ("Methods and bold parts".to_string(), 2, 2, "heading".to_string()),
1205 ("Deep".to_string(), 3, 3, "heading".to_string()),
1206 ("Close".to_string(), 4, 1, "heading".to_string()),
1207 ];
1208 assert_eq!(got, want, "the rail's rows: a plain title, a 1-based page");
1209 assert!(rows.iter().all(|r| r.page.map_or(false, |p| p >= 1)), "no page is 0 or missing");
1210 Ok(())
1211}
1212
1213/// Runs `body` on a thread whose stack holds the deepest nest the door allows. A debug build's frames are many
1214/// times a release build's, and a test thread has two mebibytes.
1215fn deep<F: FnOnce() -> Outcome<()> + Send + 'static>(body: F) -> Outcome<()> {
1216 let t = res!(std::thread::Builder::new().stack_size(128 << 20).spawn(body));
1217 match t.join() {
1218 Ok(r) => r,
1219 Err(_) => Err(err!("The nest test panicked on its own stack."; Test)),
1220 }
1221}
1222
1223// A nest of containers: `kind` n deep around one word, or a list n deep.
1224fn nest(kind: &str, n: usize) -> String {
1225 let (open, close) = match kind {
1226 "box" => ("#box[", "]"),
1227 "block" => ("#block[", "]"),
1228 "stack" => ("#stack[", "]"),
1229 _ => return (0..n).map(|i| fmt!("{}- item\n", " ".repeat(i))).collect(),
1230 };
1231 fmt!("{}x{}\n", open.repeat(n), close.repeat(n))
1232}
1233
1234// As [`nest`], built by a loop at evaluation, so the parser's own depth limit does not end it first.
1235fn looped(kind: &str, n: usize) -> String {
1236 match kind {
1237 "list" => nest(kind, n),
1238 _ => fmt!("#let x = [a]\n#for i in range({}) {{ x = {}(x) }}\n#x\n", n, kind),
1239 }
1240}
1241
1242/// Typst's first error message and its short-format site for `text`, or `None` where typst compiles it.
1243fn typst_refusal(dir: &Path, name: &str, text: &str) -> Outcome<Option<(String, usize, usize)>> {
1244 let case = dir.join(name);
1245 let main = res!(write_project(&case, &[("main.typ", text)]));
1246 let pdf = main.with_extension("pdf");
1247 let (ok, _, stderr) = res!(typst(&["compile", "--diagnostic-format", "short",
1248 &main.display().to_string(), &pdf.display().to_string()]));
1249 if ok {
1250 return Ok(None);
1251 }
1252 let (line, col) = res!(typst_site(&main, "error"));
1253 let message = stderr.lines()
1254 .find_map(|l| l.split_once(": error: ").map(|(_, m)| m.trim().to_string()))
1255 .unwrap_or_default();
1256 Ok(Some((message, line, col)))
1257}
1258
1259// Typst refuses a container nested past its depth, `maximum layout depth exceeded` for boxes and blocks,
1260// `maximum show rule depth exceeded` for a stack and a list, and each kind costs its own number of levels. The
1261// door sets the nest typst sets one level under and refuses the one a level over, at typst's site, as a `limit`.
1262#[test]
1263fn a_nest_of_containers_is_set_one_level_under_typsts_depth_and_refused_one_level_over() -> Outcome<()> {
1264 deep(|| {
1265 let dir = res!(work_dir("depth-kinds"));
1266 let mut inst = Instance::new();
1267 for (kind, under) in [("box", 70usize), ("block", 35), ("stack", 32), ("list", 31)] {
1268 let set = nest(kind, under);
1269 assert!(res!(typst_refusal(&dir, &fmt!("{}-under", kind), &set)).is_none(), "typst must set {} {} deep", kind, under);
1270 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", set.as_str())]))));
1271 let over = nest(kind, under + 1);
1272 let (message, line, col) = match res!(typst_refusal(&dir, &fmt!("{}-over", kind), &over)) {
1273 Some(r) => r,
1274 None => return Err(err!("Typst must refuse {} {} deep.", kind, under + 1; Test)),
1275 };
1276 let f = res!(must_fail(inst.compile_pdf(&strict(&[("/main.typ", over.as_str())]))));
1277 assert_eq!(f.head.message, message, "{} {} deep: the message", kind, under + 1);
1278 assert_eq!(f.head.kind, DiagnosticKind::Limit, "{} {} deep: the kind", kind, under + 1);
1279 assert_eq!((f.head.line, f.head.col), (line, door_col(&over, line, col)), "{} {} deep: the site", kind, under + 1);
1280 }
1281 Ok(())
1282 })
1283}
1284
1285// Without the guard these nests overflow the stack: a trap in the browser, which ends the module for the page's
1286// life, and an abort here. With it each is a `limit` error, and the instance and a new one compile on.
1287#[test]
1288fn a_nest_two_thousand_deep_is_a_limit_error_and_the_next_compile_succeeds_on_the_same_instance_and_a_new_one() -> Outcome<()> {
1289 deep(|| {
1290 let mut inst = Instance::new();
1291 for kind in ["box", "block", "stack", "table", "list"] {
1292 let n = if kind == "list" { 100 } else { 2_000 };
1293 let text = looped(kind, n);
1294 let f = match inst.compile_pdf(&strict(&[("/main.typ", text.as_str())])) {
1295 Ok(_) => return Err(err!("The door compiled {} {} deep; it must refuse it as a limit.", kind, n; Test)),
1296 Err(f) => f,
1297 };
1298 assert_eq!(f.head.kind, DiagnosticKind::Limit, "{} {} deep: {}", kind, n, f.head);
1299 res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", SKELETON)]))));
1300 res!(must_pdf(Instance::new().compile_pdf(&strict(&[("/main.typ", SKELETON)]))));
1301 }
1302 Ok(())
1303 })
1304}
1305
1306// The contract places an error with no site at 0:0 in the main, and a warning is no different: its file is
1307// never empty.
1308#[test]
1309fn a_warning_with_no_site_is_reported_at_zero_zero_in_the_main() -> Outcome<()> {
1310 let text = "#context [#counter(\"c\").update(counter(\"c\").final().first() + 1)]\nx\n";
1311 let mut inst = Instance::new();
1312 let made = res!(must_pdf(inst.compile_pdf(&strict(&[("/main.typ", text)]))));
1313 let detached: Vec<_> = made.report.diagnostics.iter().filter(|d| d.line == 0).collect();
1314 assert!(!detached.is_empty(), "the probe must raise a warning with no site: {:?}", made.report.diagnostics);
1315 for d in &made.report.diagnostics {
1316 assert_eq!(d.file, "/main.typ", "a diagnostic's file is never empty: {:?}", d);
1317 }
1318 for d in detached {
1319 assert_eq!((d.line, d.col), (0, 0), "{:?}", d);
1320 }
1321 Ok(())
1322}
1323
1324// ┌───────────────────────────────────────────────────────────────────────────┐
1325// │ CACHE BUDGETS, COUNTERS AND A PANIC │
1326// └───────────────────────────────────────────────────────────────────────────┘
1327
1328const WORDS: &str = "Some ordinary words to set, enough to run on to a second line of the page and to be shaped.\n";
1329
1330#[test]
1331fn the_cache_budgets_are_the_hosts_and_the_result_carries_the_counters() -> Outcome<()> {
1332 let mut inst = Instance::new();
1333 let default = Budgets::default();
1334 assert_eq!(inst.cache_budget(), default);
1335 assert_eq!((default.shapes, default.pars), (32 << 20, 16 << 20), "32 MiB of shapes and 16 MiB of paragraphs");
1336 let p = project(&[("/main.typ", WORDS)]);
1337 let made = res!(must_pdf(inst.compile_pdf(&p)));
1338 assert_eq!(made.counters.shapes.budget, default.shapes, "the shaped-run cache holds the default share");
1339 assert!(made.counters.shapes.entries > 0 && made.counters.shapes.bytes > 0, "and the compile filled it");
1340 assert_eq!((made.counters.replayed, made.counters.relaid), (0, 0), "the page counts are zero until the ledger exists");
1341 assert_eq!((made.counters.par_hits, made.counters.par_misses), (0, 1), "the one paragraph was laid afresh");
1342 let by_default = pdf_of(&made);
1343
1344 // A budget too small to keep any run slows the compile and changes nothing it makes.
1345 let tiny = Budgets { shapes: 1, pars: 2, ledger: 3 };
1346 inst.set_cache_budget(tiny);
1347 assert_eq!(inst.cache_budget(), tiny);
1348 assert_eq!(inst.caches().budgets, tiny);
1349 let made = res!(must_pdf(inst.compile_pdf(&p)));
1350 assert_eq!(made.counters.shapes.budget, 1, "the new budget reaches the shaped-run cache");
1351 assert_eq!(made.counters.shapes.bytes, 0, "which keeps nothing under it");
1352 assert_eq!(pdf_of(&made), by_default, "and the document is the same bytes");
1353 assert_eq!(inst.caches().resets(), 0, "a budget is not part of the configuration");
1354
1355 // The counters ride the delta result as they ride the PDF's.
1356 let delta = res!(delta_made(&mut inst, &p));
1357 assert_eq!(delta.counters.shapes.budget, 1);
1358 Ok(())
1359}
1360
1361/// A consumer of changed pages that panics, which the fixpoint's `finish` reaches inside the compile.
1362struct Boom;
1363
1364impl Changed for Boom {
1365 fn take(&mut self, _id: u64, _svg: String) -> Outcome<()> {
1366 panic!("the consumer of a changed page panicked");
1367 }
1368}
1369
1370#[test]
1371fn a_panic_drops_the_whole_session_and_keeps_the_hosts_budgets() -> Outcome<()> {
1372 let mut inst = Instance::new();
1373 let budgets = Budgets { shapes: 1 << 20, pars: 2 << 20, ledger: 3 << 20 };
1374 inst.set_cache_budget(budgets);
1375 let mut p = project(&[("/main.typ", WORDS)]);
1376 p.fonts.push(("noto.ttf".to_string(), res!(noto())));
1377 res!(must_pdf(inst.compile_pdf(&p)));
1378 let before = match inst.caches().config() {
1379 Some(c) => c,
1380 None => return Err(err!("A compile left no configuration."; Test)),
1381 };
1382 // A delta reaches the consumer inside the compile, so the panic is the compiler's to catch.
1383 let fail = match inst.compile_delta_into(&p, Boom) {
1384 Err(f) => f,
1385 Ok(_) => return Err(err!("A compile whose consumer panicked produced a result."; Test)),
1386 };
1387 assert!(fail.head.message.contains("panicked"), "{}", fail.head.message);
1388 assert!(inst.caches().config().is_none(),
1389 "the session that panicked is gone, and with it the configuration its caches were made under");
1390 assert!(inst.query("heading", "").is_none(), "and nothing answers a query");
1391 assert_eq!(inst.cache_budget(), budgets, "the host's budgets outlast it");
1392
1393 // The next compile starts from a new session over the embedded faces, the project's font added again.
1394 let made = res!(must_pdf(inst.compile_pdf(&p)));
1395 assert_eq!(inst.caches().config(), Some(before), "the same faces, the same configuration");
1396 assert_eq!(inst.caches().resets(), 0, "and nothing was emptied for it");
1397 assert_eq!(made.counters.shapes.budget, budgets.shapes, "the budget still reaches the shaped-run cache");
1398 assert!(inst.font_families(Some(&p)).iter().any(|f| f == "Noto Sans"));
1399 Ok(())
1400}
1401
1402// ┌───────────────────────────────────────────────────────────────────────────┐
1403// │ PAGES ON REQUEST │
1404// └───────────────────────────────────────────────────────────────────────────┘
1405//
1406// The consumer holds the SVG of the pages it shows and asks for any other: the delta keeps its compile's
1407// frames, a window limits what it sends, and `page` draws one page from the frames kept.
1408
1409fn page_svg(inst: &Instance, at: PageAt) -> Outcome<(u64, usize, String)> {
1410 match inst.page(at) {
1411 Ok(Ok(r)) => Ok(r),
1412 Ok(Err(m)) => Err(err!("No page at {:?}: {:?}.", at, m; Test)),
1413 Err(f) => Err(err!("Drawing the page at {:?} failed: {}", at, f.head; Test)),
1414 }
1415}
1416
1417fn page_miss(inst: &Instance, at: PageAt) -> Outcome<PageMiss> {
1418 match inst.page(at) {
1419 Ok(Ok((id, _, _))) => Err(err!("A page {} was drawn at {:?}, where none should be.", id, at; Test)),
1420 Ok(Err(m)) => Ok(m),
1421 Err(f) => Err(err!("Drawing the page at {:?} failed: {}", at, f.head; Test)),
1422 }
1423}
1424
1425#[test]
1426fn a_page_requested_after_a_compile_is_byte_identical_to_the_one_the_delta_sent() -> Outcome<()> {
1427 let mut inst = Instance::new();
1428 assert_eq!(res!(page_miss(&inst, PageAt::Index(0))), PageMiss::Uncompiled, "nothing kept before a delta");
1429 let p = strict(&[("/main.typ", &six_pages(None))]);
1430 let d = res!(delta_made(&mut inst, &p)).product;
1431 assert_eq!(d.changed.len(), 6);
1432 for (i, (id, svg)) in d.changed.iter().enumerate() {
1433 let (got, at, by_id) = res!(page_svg(&inst, PageAt::Id(*id)));
1434 assert_eq!((got, at), (*id, i), "the id names its own page");
1435 assert!(by_id == *svg, "page {} by id is the delta's bytes", i);
1436 let (got, _, by_index) = res!(page_svg(&inst, PageAt::Index(i)));
1437 assert!(got == *id && by_index == *svg, "page {} by index is the delta's bytes", i);
1438 }
1439 match inst.kept() {
1440 Some(k) => assert!(k.len() == 6 && k.version() == d.version && k.order() == &d.order[..]),
1441 None => return Err(err!("The delta kept no pages."; Test)),
1442 }
1443 Ok(())
1444}
1445
1446#[test]
1447fn a_page_requested_after_an_edit_reflects_the_edit_and_the_old_id_is_gone() -> Outcome<()> {
1448 let mut inst = Instance::new();
1449 let mut p = strict(&[("/main.typ", &six_pages(None))]);
1450 let was = res!(delta_made(&mut inst, &p)).product;
1451 let (_, _, old) = res!(page_svg(&inst, PageAt::Index(2)));
1452 assert!(text_layer(&old).contains("quartz"));
1453 p.sources = vec![("/main.typ".to_string(), six_pages(Some(2)))];
1454 p.known = was.order.clone();
1455 let now = res!(delta_made(&mut inst, &p)).product;
1456 assert_eq!(now.changed.len(), 1, "one page changed");
1457 let (id, at, svg) = res!(page_svg(&inst, PageAt::Index(2)));
1458 assert_eq!((id, at), (now.order[2], 2));
1459 assert!(svg == now.changed[0].1 && text_layer(&svg).contains("quarts"), "the page drawn is the edited one");
1460 assert_eq!(res!(page_miss(&inst, PageAt::Id(was.order[2]))), PageMiss::Gone, "the page the edit replaced is gone");
1461 assert_eq!(res!(page_miss(&inst, PageAt::Index(6))), PageMiss::Range, "six pages, so index 6 is past the end");
1462 let (_, _, same) = res!(page_svg(&inst, PageAt::Id(was.order[3])));
1463 assert!(text_layer(&same).contains("quartz"), "an unedited page keeps its id and draws as before");
1464 Ok(())
1465}
1466
1467#[test]
1468fn a_window_limits_what_the_delta_sends_and_a_page_outside_it_is_drawn_on_request() -> Outcome<()> {
1469 let mut inst = Instance::new();
1470 let mut p = strict(&[("/main.typ", &six_pages(None))]);
1471 p.want = Some(Window { from: 2, to: 4 });
1472 let first = res!(delta_made(&mut inst, &p));
1473 let d = &first.product;
1474 assert!(d.reset && d.order.len() == 6, "the order is every page, the window or not");
1475 let sent: Vec<u64> = d.changed.iter().map(|(id, _)| *id).collect();
1476 assert_eq!(sent, d.order[2..4].to_vec(), "only the pages in the window are sent");
1477 assert_eq!(d.rendered, 2, "and only they are drawn");
1478 // The consumer holds pages 2 and 3; an edit on page 0 and on page 3 sends page 3 alone.
1479 p.known = d.order[2..4].to_vec();
1480 p.sources = vec![("/main.typ".to_string(), six_pages(Some(3)))];
1481 let second = res!(delta_made(&mut inst, &p)).product;
1482 assert_eq!(second.changed.len(), 1);
1483 assert_eq!(second.changed[0].0, second.order[3], "the edited page inside the window is sent");
1484 assert_eq!(second.order[0], d.order[0], "page 0 is unedited and keeps its id");
1485 // A page outside the window comes on request, as a window covering it would have sent it.
1486 let (_, _, far) = res!(page_svg(&inst, PageAt::Index(5)));
1487 p.known = Vec::new();
1488 p.want = Some(Window { from: 5, to: 99 });
1489 let third = res!(delta_made(&mut inst, &p)).product;
1490 assert_eq!(third.changed.len(), 1, "a window past the end stops at the last page");
1491 assert!(third.changed[0].1 == far, "the page drawn on request is the bytes a delta sends");
1492 // No window is every changed page, as before.
1493 p.want = None;
1494 assert_eq!(res!(delta_made(&mut inst, &p)).product.changed.len(), 6);
1495 Ok(())
1496}
1497
1498/// The `viewBox` width and height a page's SVG declares.
1499fn view_box(svg: &str) -> Outcome<(usize, usize)> {
1500 let rest = res!(svg.split("viewBox=\"0 0 ").nth(1).ok_or_else(|| err!("The SVG has no viewBox."; Test)));
1501 let mut nums = rest.split(|c: char| c == ' ' || c == '"');
1502 let w = res!(res!(nums.next().ok_or_else(|| err!("The viewBox has no width."; Test))).parse::<usize>());
1503 let h = res!(res!(nums.next().ok_or_else(|| err!("The viewBox has no height."; Test))).parse::<usize>());
1504 Ok((w, h))
1505}
1506
1507#[test]
1508fn the_delta_sends_every_pages_size_as_its_svg_declares_it_window_or_none() -> Outcome<()> {
1509 let src = "#set page(width: 220pt, height: 100pt, margin: 12pt)\nFirst.\n\
1510 #set page(width: 300.6pt, height: 150pt)\nSecond.\n\
1511 #set page(width: 120pt, height: 400pt)\nThird.\n\
1512 #set page(paper: \"a5\")\nFourth.\n";
1513 let mut inst = Instance::new();
1514 let mut p = strict(&[("/main.typ", src)]);
1515 let d = res!(delta_made(&mut inst, &p)).product;
1516 assert_eq!(d.order.len(), 4, "four pages");
1517 assert_eq!(d.sizes.len(), d.order.len(), "one size a page in order");
1518 assert_eq!(d.changed.len(), 4, "no window sends every page");
1519 for (id, svg) in &d.changed {
1520 let i = res!(d.order.iter().position(|o| o == id).ok_or_else(|| err!("Page {} is not in order.", id; Test)));
1521 assert_eq!(d.sizes[i], res!(view_box(svg)), "page {}'s size is its viewBox", i);
1522 }
1523 assert!(d.sizes[0] != d.sizes[1] && d.sizes[1] != d.sizes[2], "the pages differ in size: {:?}", d.sizes);
1524 // A window sends one page's SVG and still every page's size.
1525 p.known = Vec::new();
1526 p.want = Some(Window { from: 1, to: 2 });
1527 let w = res!(delta_made(&mut inst, &p)).product;
1528 assert_eq!(w.changed.len(), 1);
1529 assert_eq!(w.sizes, d.sizes, "the sizes are every page's, the window or not");
1530 for i in 0..w.order.len() {
1531 let (_, _, svg) = res!(page_svg(&inst, PageAt::Index(i)));
1532 assert_eq!(w.sizes[i], res!(view_box(&svg)), "page {} drawn on request has the size the delta sent", i);
1533 }
1534 Ok(())
1535}
1536
1537#[test]
1538fn a_failed_compile_leaves_the_last_deltas_pages_drawable() -> Outcome<()> {
1539 let mut inst = Instance::new();
1540 let p = strict(&[("/main.typ", &six_pages(None))]);
1541 let d = res!(delta_made(&mut inst, &p)).product;
1542 let _ = res!(delta_fail(&mut inst, &strict(&[("/main.typ", "#nonesuch()\n")])));
1543 let (id, _, svg) = res!(page_svg(&inst, PageAt::Index(1)));
1544 assert!(id == d.changed[1].0 && svg == d.changed[1].1, "the consumer still shows them, so they stay");
1545 Ok(())
1546}
1547
1548/// Many pages of prose, one paragraph a page, each page different.
1549fn prose_pages(n: usize) -> String {
1550 let mut t = String::from("#set page(width: 300pt, height: 400pt, margin: 20pt, numbering: \"1\")\n#set par(justify: true)\n");
1551 for k in 0..n {
1552 if k > 0 {
1553 t.push_str("#pagebreak()\n");
1554 }
1555 t.push_str(&fmt!("= Part {}\n", k));
1556 for j in 0..4 {
1557 t.push_str(&fmt!("Paragraph {} of part {} turns the batter, rests the pan and waits for the edges to lift before it is served warm. \n\n", j, k));
1558 }
1559 }
1560 t
1561}
1562
1563#[test]
1564fn the_kept_pages_stay_bounded_by_one_compile_and_well_under_their_svg() -> Outcome<()> {
1565 let mut inst = Instance::new();
1566 let mut p = strict(&[("/main.typ", &prose_pages(24))]);
1567 let d = res!(delta_made(&mut inst, &p)).product;
1568 let svg: usize = d.changed.iter().map(|(_, s)| s.len()).sum();
1569 let held = match inst.kept() { Some(k) => k.heap_bytes(), None => 0 };
1570 assert!(held > 0 && held * 3 < svg, "kept frames {} B against {} B of SVG", held, svg);
1571 // Recompiles, edited and not, hold one compile's frames: nothing accumulates.
1572 p.known = d.order.clone();
1573 for edit in 0..4 {
1574 p.sources = vec![("/main.typ".to_string(), prose_pages(24).replace("Part 3", &fmt!("Part 3{}", edit)))];
1575 let _ = res!(delta_made(&mut inst, &p));
1576 let now = match inst.kept() { Some(k) => (k.len(), k.heap_bytes()), None => (0, 0) };
1577 assert_eq!(now.0, 24);
1578 assert!(now.1 * 10 < held * 11 && now.1 * 11 > held * 10, "recompile {} holds {} B, the first {} B", edit, now.1, held);
1579 }
1580 eprintln!("[pg] 24 pages: kept {} B ({} B a page), SVG {} B ({} B a page)", held, held / 24, svg, svg / 24);
1581 Ok(())
1582}
1583
1584/// The resident cost of the kept frames on the 300-page fixtures, from `PG_FIXTURES`, a directory holding
1585/// `syn300.typ` (tools/bench/gen_synthetic.py 300) and `book300.typ` (Daimond's 300-page bench book).
1586#[test]
1587#[ignore]
1588fn measure_the_kept_frames_of_a_300_page_book() -> Outcome<()> {
1589 let dir = match std::env::var("PG_FIXTURES") {
1590 Ok(d) => PathBuf::from(d),
1591 Err(_) => return Err(err!("Set PG_FIXTURES to the fixture directory."; Missing, Input)),
1592 };
1593 for name in ["syn300.typ", "book300.typ"] {
1594 let text = res!(std::fs::read_to_string(dir.join(name)));
1595 let mut inst = Instance::new();
1596 let p = project(&[("/main.typ", &text)]);
1597 let d = res!(delta_made(&mut inst, &p)).product;
1598 let svg: usize = d.changed.iter().map(|(_, s)| s.len()).sum();
1599 let (n, held) = match inst.kept() { Some(k) => (k.len(), k.heap_bytes()), None => (0, 0) };
1600 eprintln!("[pg] {}: {} pages, kept {:.2} MB ({} B a page), SVG {:.1} MB ({} B a page), ratio {:.1}x",
1601 name, n, held as f64 / 1e6, held / n.max(1), svg as f64 / 1e6, svg / n.max(1), svg as f64 / held.max(1) as f64);
1602 }
1603 Ok(())
1604}
1605
1606#[cfg(not(feature = "embed-fonts"))]
1607#[test]
1608fn a_build_without_fonts_asks_for_the_pack_families_and_compiles_once_they_are_supplied() -> Outcome<()> {
1609 use oxedyne_fe2o3_austenite::fonts::Embedded;
1610 // The host supplies the pack: the crate's own files, which are the faces a default build embeds.
1611 let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("fonts");
1612 let mut pack: Vec<(String, Vec<u8>)> = Vec::new();
1613 for e in Embedded::BOOK {
1614 pack.push((e.file().to_string(), res!(std::fs::read(dir.join(e.file())))));
1615 }
1616 for (src, want) in [
1617 ("Hello, world.\n", "font:Libertinus Serif"),
1618 ("$x + 1$\n", "font:New Computer Modern Math"),
1619 ] {
1620 let mut inst = Instance::new();
1621 let mut p = project(&[("/main.typ", src)]);
1622 let f = match inst.compile_svg(&p) {
1623 Ok(_) => return Err(err!("A build with no fonts compiled {:?} with none supplied.", src; Test)),
1624 Err(f) => f,
1625 };
1626 assert_eq!(f.head.kind, DiagnosticKind::MissingFont, "{}", f.head);
1627 assert_eq!(f.needs, vec![want.to_string()], "{:?} asks for its family", src);
1628 p.fonts = pack.clone();
1629 let made = match inst.compile_svg(&p) {
1630 Ok(m) => m,
1631 Err(f) => return Err(err!("{:?} with the pack supplied failed: {}", src, f.head; Test)),
1632 };
1633 assert!(made.needs.is_empty(), "{:?}", made.needs);
1634 assert_eq!(made.product.len(), 1, "{:?} is set on one page", src);
1635 }
1636 let mut inst = Instance::new();
1637 let mut p = project(&[("/main.typ", "Hello, world.\n")]);
1638 p.fonts = pack;
1639 let made = match inst.compile_svg(&p) {
1640 Ok(m) => m,
1641 Err(f) => return Err(err!("The text with the pack supplied first failed: {}", f.head; Test)),
1642 };
1643 assert!(text_layer(&made.product[0]).contains("Hello"), "the page is set in the supplied face");
1644 Ok(())
1645}