Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/gate/selftest_gate.sh

32.4 KiB, 1 run, executable

created by r1870400018:66271, 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#!/usr/bin/env bash
2# Proves the O2 tools on synthetic projects, and proves each proof can fail.
3#
4# selftest_gate.sh <austenite-bin> <pkg> <scratch>
5#
6# <austenite-bin> is the `austenite` binary and <pkg> the wasm-pack output directory, both built from the tree
7# under test; <scratch> is a directory this script makes a child of and removes on exit. Everything here is
8# synthetic: no path to a real project is taken or needed.
9#
10# It checks, in order:
11# 1. g_checks.py, G1 G2 G4 G5 G7 G8 and G12: for each, a pair that agrees reads `same` and a pair that
12# differs the way the check is about reads red on the line that names the difference; then a copy of
13# g_checks.py with one fault planted is shown to miss it, so the expectation on that copy is red;
14# 2. door_pdf.mjs on synthetic projects: a pass, a strict refusal, a package supplied and a package missing,
15# the same plants on a copy of the tools, and no word of the project's own text on the stream;
16# 3. gate.sh end to end: a project that agrees is green, one that strict refuses is red, a fault in a required
17# check turns the verdict red, and nothing of the project's text is printed, with its two plants.
18set -u
19
20HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
21CRATE="$(cd "$HERE/../.." && pwd)"
22# shellcheck source=gate_lib.sh
23. "$HERE/gate_lib.sh"
24
25BIN="${1:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}"
26PKG="${2:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}"
27PARENT="${3:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}"
28TYPST="${GATE_TYPST:-/home/jason/bin/typst}"
29mkdir -p "$PARENT" || exit 2
30S="$(mktemp -d "$PARENT/o2.XXXXXX")" || exit 2
31trap 'rm -rf "${S:?}"' EXIT
32export GATE_SCRATCH="$S"
33
34bad=0
35ok() { echo "selftest: ok $1"; }
36fail() { echo "selftest: FAIL $1"; bad=$((bad + 1)); }
37expect() { # expect <label> <text> <fixed line the text must contain>
38 if printf '%s\n' "$2" | grep -qxF -- "$3"; then ok "$1"; else fail "$1 (wanted: $3)"; fi
39}
40lack() { # lack <label> <text> <fixed line the text must not contain>
41 if printf '%s\n' "$2" | grep -qxF -- "$3"; then fail "$1 (found: $3)"; else ok "$1"; fi
42}
43lackany() { # lackany <label> <text> <fixed string the text must not contain anywhere>
44 if printf '%s\n' "$2" | grep -qiF -- "$3"; then fail "$1 (found: $3)"; else ok "$1"; fi
45}
46
47G="$HERE/g_checks.py"
48
49# ── typst documents ─────────────────────────────────────────────────────────
50mkpdf() { # mkpdf <name> <typst source>: a PDF of a 200x80 page document
51 printf '#set page(width: 200pt, height: 80pt, margin: 10pt)\n%s\n' "$2" > "$S/$1.typ"
52 gate_typst "$TYPST" compile "$S/$1.typ" "$S/$1.pdf" > /dev/null 2>&1 || fail "fixture $1 did not compile"
53}
54npages() { pdfinfo "$1" 2>/dev/null | awk '/^Pages:/ { print $2 }'; }
55
56# subst <src> <dst> <old> <new>: `dst` is `src` with `old` replaced, which must occur exactly once.
57subst() {
58 python3 - "$@" <<'PY'
59import sys
60src, dst, old, new = sys.argv[1:]
61text = open(src).read()
62if text.count(old) != 1:
63 sys.exit(1)
64open(dst, 'w').write(text.replace(old, new))
65PY
66}
67
68# plant <label> <old text> <new text> <line> <check> <pdf>...
69# The copy is g_checks.py with `old` replaced once by `new`; <line> is one the real file prints for this pair, so a
70# copy that still prints it did not lose the check, and the proof is red.
71plant() {
72 local label="$1" old="$2" new="$3" want="$4" r
73 shift 4
74 if ! subst "$G" "$S/plant.py" "$old" "$new"; then
75 fail "plant $label: the text it replaces has moved or repeats in g_checks.py"
76 return
77 fi
78 r="$(python3 "$S/plant.py" "$@" 2>&1)"
79 if printf '%s\n' "$r" | grep -qxF -- "$want"; then
80 fail "plant $label: the planted copy still prints: $want"
81 else
82 ok "plant $label: the planted copy no longer prints the line, so the check is red"
83 fi
84}
85
86# ── 1a. G1 ──────────────────────────────────────────────────────────────────
87mkpdf p3 $'Alpha one\n#pagebreak()\nBeta two\n#pagebreak()\nGamma three'
88mkpdf p3tall $'Alpha one\n#pagebreak()\n#set page(height: 100pt)\nBeta two\n#pagebreak()\n#set page(height: 80pt)\nGamma three'
89mkpdf p2 $'Alpha one\n#pagebreak()\nBeta two'
90r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p3.pdf")"
91expect "g1: a PDF agrees with itself" "$r" "g1 verdict same"
92r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p3tall.pdf")"
93expect "g1: a page of another size goes red, on that page" "$r" "g1 mediabox differ 1 pages 2"
94expect "g1: and the deviation is its 20 pt" "$r" "g1 mediabox max-deviation 20.0000"
95expect "g1: and the verdict is differs" "$r" "g1 verdict differs"
96r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p2.pdf")"
97expect "g1: a page that is missing goes red" "$r" "g1 pages a 3 b 2"
98expect "g1: and the verdict is differs" "$r" "g1 verdict differs"
99plant "g1 sizes ignored" 'd = max(abs(x - y) for x, y in zip(ma[i], mb[i]))' 'd = 0.0' "g1 mediabox differ 1 pages 2" g1 "$S/p3.pdf" "$S/p3tall.pdf"
100plant "g1 count ignored" 'same = len(ma) == len(mb) and not differ' 'same = not differ' "g1 verdict differs" g1 "$S/p3.pdf" "$S/p2.pdf"
101
102# ── 1b. G2 ──────────────────────────────────────────────────────────────────
103mkpdf f_plain 'Alpha one two'
104mkpdf f_other 'Beta three four five'
105mkpdf f_mono $'Alpha one two\n#text(font: "DejaVu Sans Mono")[mono]'
106r="$(python3 "$G" g2 "$S/f_plain.pdf" "$S/f_other.pdf")"
107expect "g2: the same fonts with other glyphs agree (the subset tag is stripped)" "$r" "g2 verdict same"
108r="$(python3 "$G" g2 "$S/f_plain.pdf" "$S/f_mono.pdf")"
109expect "g2: a font that only b embeds is named" "$r" "g2 only-b DejaVuSansMono"
110expect "g2: and the verdict is differs" "$r" "g2 verdict differs"
111plant "g2 subset tag kept" "FONT_TAG = re.compile(r'^[A-Z]{6}\\+')" "FONT_TAG = re.compile(r'^(?!)')" "g2 verdict same" g2 "$S/f_plain.pdf" "$S/f_other.pdf"
112plant "g2 font sets not compared" 'same = sa == sb and flags_a == 0 and flags_b == 0' 'same = flags_a == 0 and flags_b == 0' "g2 verdict differs" g2 "$S/f_plain.pdf" "$S/f_mono.pdf"
113# A font that is not embedded and has no Unicode map: one is written by hand.
114python3 - "$S/noemb.pdf" <<'PY'
115import sys
116import pikepdf
117pdf = pikepdf.Pdf.new()
118font = pdf.make_indirect(pikepdf.Dictionary(Type=pikepdf.Name.Font, Subtype=pikepdf.Name.Type1, BaseFont=pikepdf.Name.Helvetica))
119page = pdf.add_blank_page(page_size=(200, 80))
120page.Resources = pikepdf.Dictionary(Font=pikepdf.Dictionary(F1=font))
121page.Contents = pdf.make_stream(b'BT /F1 12 Tf 10 40 Td (Hello) Tj ET')
122pdf.save(sys.argv[1])
123PY
124r="$(python3 "$G" g2 "$S/noemb.pdf" "$S/noemb.pdf")"
125expect "g2: a font that is not embedded and has no Unicode map is flagged on both sides" "$r" "g2 flags a 1 b 1"
126expect "g2: and the verdict is differs although the sets are equal" "$r" "g2 verdict differs"
127plant "g2 flags ignored" 'flags_a = sum(1 for _, ok in fa if not ok)' 'flags_a = 0' "g2 flags a 1 b 1" g2 "$S/noemb.pdf" "$S/noemb.pdf"
128
129# ── 1c. G4 ──────────────────────────────────────────────────────────────────
130mkpdf h_num $'#set heading(numbering: "1.1")\n= One\n== Under\n= Two'
131mkpdf h_plain $'= One\n== Under\n= Two'
132mkpdf h_flat $'= One\n= Under\n= Two'
133mkpdf h_short $'= One\n== Under'
134mkpdf h_ab $'= Alpha\n= Beta'
135mkpdf h_ba $'= Beta\n= Alpha'
136r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_plain.pdf")"
137expect "g4: an outline agrees with itself" "$r" "g4 verdict same"
138r="$(python3 "$G" g4 "$S/h_num.pdf" "$S/h_plain.pdf")"
139expect "g4: a numbered title and a plain one differ, with the levels equal" "$r" "g4 levels equal first-diff none"
140expect "g4: all 3 titles are unmatched" "$r" "g4 titles differs unmatched a 3 b 3"
141expect "g4: and all 3 differ only by a leading number" "$r" "g4 titles prefix-only 3"
142expect "g4: and the verdict is differs" "$r" "g4 verdict differs"
143r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_flat.pdf")"
144expect "g4: a title at another level goes red, at its place" "$r" "g4 levels differs first-diff 2"
145r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_short.pdf")"
146expect "g4: a missing entry is counted" "$r" "g4 outline a 3 b 2"
147expect "g4: and the levels differ where it is missing" "$r" "g4 levels differs first-diff 3"
148r="$(python3 "$G" g4 "$S/h_ab.pdf" "$S/h_ba.pdf")"
149expect "g4: the same titles in another order are one multiset" "$r" "g4 titles equal unmatched a 0 b 0"
150expect "g4: but two are out of place" "$r" "g4 titles position differ 2 of 2"
151expect "g4: and the verdict is differs" "$r" "g4 verdict differs"
152plant "g4 titles ignored" 'titles_equal = miss_a == 0 and miss_b == 0' 'titles_equal = True' "g4 titles differs unmatched a 3 b 3" g4 "$S/h_num.pdf" "$S/h_plain.pdf"
153plant "g4 levels ignored" 'if la[i] != lb[i]:' 'if False:' "g4 levels differs first-diff 2" g4 "$S/h_plain.pdf" "$S/h_flat.pdf"
154plant "g4 order ignored" 'pos += 1' 'pos += 0' "g4 verdict differs" g4 "$S/h_ab.pdf" "$S/h_ba.pdf"
155
156# ── 1d. G5 ──────────────────────────────────────────────────────────────────
157mkpdf i_full $'#set document(title: "T", author: "Au", description: "Sub")\nbody'
158mkpdf i_part $'#set document(title: "T", author: "Other")\nbody'
159r="$(python3 "$G" g5 "$S/i_full.pdf" "$S/i_full.pdf")"
160expect "g5: Info agrees with itself" "$r" "g5 verdict same"
161expect "g5: an entry that neither carries is absent-both" "$r" "g5 keywords absent-both"
162r="$(python3 "$G" g5 "$S/i_full.pdf" "$S/i_part.pdf")"
163expect "g5: an equal title" "$r" "g5 title equal"
164expect "g5: a different author" "$r" "g5 author differs"
165expect "g5: an entry only a carries differs" "$r" "g5 subject differs"
166expect "g5: and the verdict is differs" "$r" "g5 verdict differs"
167plant "g5 values ignored" 'elif x == y:' 'elif True:' "g5 author differs" g5 "$S/i_full.pdf" "$S/i_part.pdf"
168plant "g5 absence ignored" 'if x is None and y is None:' 'if x is None or y is None:' "g5 subject differs" g5 "$S/i_full.pdf" "$S/i_part.pdf"
169
170# ── 1e. G7 ──────────────────────────────────────────────────────────────────
171mkpdf r_plain $'Alpha one\n#pagebreak()\nBeta two'
172mkpdf r_red $'Alpha one\n#pagebreak()\nBeta two\n#place(top + left, dx: -10pt, dy: -10pt, rect(width: 200pt, height: 40pt, fill: red))'
173r="$(python3 "$G" g7 "$S/r_plain.pdf" "$S/r_red.pdf")"
174expect "g7: the page that is the same reads 0" "$r" "g7 page 1 0.0000"
175p2="$(printf '%s\n' "$r" | awk '$1 == "g7" && $2 == "page" && $3 == 2 { print $4 }')"
176if [ -n "$p2" ] && awk -v f="$p2" 'BEGIN { exit !(f >= 0.4 && f <= 0.6) }'; then
177 ok "g7: the page with half its area red reads about a half ($p2)"
178else
179 fail "g7: the red half page read '$p2', wanted 0.4 to 0.6"
180fi
181expect "g7: both pages were compared" "$r" "g7 compared 2 of a 2 b 2"
182r="$(env -u GATE_SCRATCH python3 "$G" g7 "$S/r_plain.pdf" "$S/r_red.pdf")"
183expect "g7: with no scratch directory it refuses, and writes no raster" "$r" "g7 error scratch"
184plant "g7 threshold" 'diff > 16' 'diff > 255' "g7 page 2 $p2" g7 "$S/r_plain.pdf" "$S/r_red.pdf"
185plant "g7 one channel only" '.max(axis=2)' '.min(axis=2)' "g7 page 2 $p2" g7 "$S/r_plain.pdf" "$S/r_red.pdf"
186[ -z "$(ls -A "$S" | grep -v '\.\(typ\|pdf\|py\)$')" ] && ok "g7: its rasters are gone" || fail "g7 left rasters behind: $(ls -A "$S" | grep -v '\.\(typ\|pdf\|py\)$' | head -3)"
187
188# ── 1f. G8 ──────────────────────────────────────────────────────────────────
189words() { echo "#range($1).map(_ => \"abcde\").join(\" \")"; }
190mkpdf c_a $'#set page(height: 300pt)\n'"$(words 60)"$'\n#pagebreak()\n'"$(words 60)"
191mkpdf c_b $'#set page(height: 300pt)\n'"$(words 60) f"$'\n#pagebreak()\n'"$(words 66)"
192mkpdf c_blank $'#set page(height: 300pt)\n'"$(words 60)"$'\n#pagebreak()\n#h(1pt)'
193[ "$(npages "$S/c_b.pdf")" = 2 ] && [ "$(npages "$S/c_blank.pdf")" = 2 ] || fail "fixture: the character-count documents are not 2 pages"
194r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_a.pdf")"
195expect "g8: a PDF agrees with itself" "$r" "g8 verdict same"
196r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_b.pdf")"
197expect "g8: a page 0.3% over is within the 1%, one 10% over is not" "$r" "g8 out-of-tolerance 1 pages 2"
198expect "g8: its two counts are named" "$r" "g8 page 2 a 300 b 330"
199expect "g8: and the verdict is differs" "$r" "g8 verdict differs"
200r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_blank.pdf")"
201expect "g8: a page with no text where a has some is named" "$r" "g8 empty 1 pages 2"
202plant "g8 tolerance" 'if dev > 0.01:' 'if dev > 0.5:' "g8 out-of-tolerance 1 pages 2" g8 "$S/c_a.pdf" "$S/c_b.pdf"
203plant "g8 empty ignored" 'if a > 0 and b == 0:' 'if False:' "g8 empty 1 pages 2" g8 "$S/c_a.pdf" "$S/c_blank.pdf"
204
205# ── 1f2. G9 ─────────────────────────────────────────────────────────────────
206python3 - "$S/mark.png" <<'PY'
207import sys
208from PIL import Image
209Image.new('RGB', (8, 8), (200, 40, 40)).save(sys.argv[1])
210PY
211TXT="#range(40).map(i => \"word\" + str(i)).join(\" \")"
212mkpdf y_a $'#set page(height: 200pt)\n'"$TXT"$'\n\n'"$TXT"
213mkpdf y_same $'#set page(height: 200pt)\n'"$TXT"$'\n\n'"$TXT"
214mkpdf y_lead $'#set page(height: 200pt)\n#set par(leading: 1.2em)\n'"$TXT"$'\n\n'"$TXT"
215mkpdf y_cols $'#set page(height: 200pt, columns: 2)\n'"$TXT"$'\n\n'"$TXT"
216mkpdf y_img $'#set page(height: 200pt)\n#image("mark.png", width: 20pt)\n'"$TXT"$'\n\n'"$TXT"
217mkpdf y_link $'#set page(height: 200pt)\n#link("https://example.org")[link] '"$TXT"$'\n\n'"$TXT"
218r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_same.pdf")"
219expect "g9: the same layout differs on no page" "$r" "g9 pages differ 0 pages none"
220r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_lead.pdf")"
221echo "$r" | grep -q '^g9 pages differ [1-9]' && ok "g9: a looser leading differs" || fail "g9: a looser leading read as no difference"
222pitch="$(printf '%s\n' "$r" | awk '$1 == "g9" && $2 == "page" && $3 == 1 && $4 == "pitch" { print ($8 > $6) ? "wider" : "not" }')"
223[ "$pitch" = wider ] && ok "g9: and its line pitch is the larger one" || fail "g9: the pitch line of the looser leading is '$pitch'"
224want_pitch="$(printf '%s\n' "$r" | grep '^g9 page 1 pitch ')"
225want_line="$(printf '%s\n' "$r" | grep '^g9 line 1 2 ' | head -1)"
226[ -n "$want_line" ] && ok "g9: and the second line of page 1 has moved" || fail "g9: no line moved on the looser leading"
227r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_cols.pdf")"
228narrow="$(printf '%s\n' "$r" | awk '$1 == "g9" && $2 == "line" && $3 == 1 && $4 == 1 { print ($19 < $17 / 1.5) ? "narrower" : "not" }')"
229[ "$narrow" = narrower ] && ok "g9: a first line set in a column is narrower than set across the page" || fail "g9: the first line's width reads '$narrow'"
230r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_img.pdf")"
231expect "g9: an image that only b places is counted" "$r" "g9 page 1 images a 0 b 1"
232r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_link.pdf")"
233expect "g9: a link that only b carries is counted" "$r" "g9 page 1 links a 0 b 1"
234plant "g9 pitch ignored" "if abs(a['pitch'] - b['pitch']) > 0.1:" 'if False:' "$want_pitch" g9 "$S/y_a.pdf" "$S/y_lead.pdf"
235plant "g9 lines not compared" 'if max(abs(u[1] - v[1]), abs(u[0] - v[0]), abs((u[2] - u[0]) - (v[2] - v[0]))) > 0.5:' 'if False:' "$want_line" g9 "$S/y_a.pdf" "$S/y_lead.pdf"
236plant "g9 images and links not counted" "for m in COUNTS + ('images', 'links'):" 'for m in COUNTS:' "g9 page 1 images a 0 b 1" g9 "$S/y_a.pdf" "$S/y_img.pdf"
237plant "g9 links not counted" "ann is None else sum(1 for x in ann if str(x.get('/Subtype')) == '/Link')" "ann is None else 0" "g9 page 1 links a 0 b 1" g9 "$S/y_a.pdf" "$S/y_link.pdf"
238
239# ── 1g. G12 ─────────────────────────────────────────────────────────────────
240touch_byte() { # touch_byte <in> <out>: the same size, one byte changed
241 python3 - "$1" "$2" <<'PY'
242import sys
243d = bytearray(open(sys.argv[1], 'rb').read())
244d[100] ^= 0x55
245open(sys.argv[2], 'wb').write(bytes(d))
246PY
247}
248cp "$S/p3.pdf" "$S/x1.pdf"
249cp "$S/p3.pdf" "$S/x2.pdf"
250cp "$S/p3.pdf" "$S/x3.pdf"
251touch_byte "$S/p3.pdf" "$S/y.pdf"
252r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/x3.pdf")"
253expect "g12: five equal PDFs agree" "$r" "g12 verdict same"
254expect "g12: and the CLI's is the door's" "$r" "g12 cli-door equal"
255r="$(python3 "$G" g12 "$S/x1.pdf" "$S/y.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/x3.pdf")"
256expect "g12: two CLI runs that differ go red" "$r" "g12 cli-twice differs"
257expect "g12: and the verdict is differs" "$r" "g12 verdict differs"
258r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/y.pdf" "$S/x3.pdf")"
259expect "g12: two compiles on one door instance that differ go red" "$r" "g12 door-one-instance differs"
260r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/y.pdf")"
261expect "g12: a second instance that differs goes red" "$r" "g12 door-two-instances differs"
262r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/none.pdf")"
263expect "g12: a PDF that is absent is named" "$r" "g12 door-two-instances absent"
264expect "g12: and the verdict is differs" "$r" "g12 verdict differs"
265r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x1.pdf" "$S/y.pdf" "$S/y.pdf" "$S/y.pdf")"
266expect "g12: a CLI PDF that is not the door's is reported" "$r" "g12 cli-door differs"
267expect "g12: and does not turn the verdict red" "$r" "g12 verdict same"
268plant "g12 bytes not compared" "return 'equal' if x[0] == y[0] else 'differs'" "return 'equal' if x[1] == y[1] else 'differs'" "g12 door-one-instance differs" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/y.pdf" "$S/x3.pdf"
269plant "g12 absent passes" $'\tif x is None or y is None:\n\t\treturn \'absent\'' $'\tif x is None or y is None:\n\t\treturn \'equal\'' "g12 door-two-instances absent" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/none.pdf"
270
271
272# ── 2. door_pdf.mjs ─────────────────────────────────────────────────────────
273mkproj() { # mkproj <dir> <typst source>: a project of one file and an empty font directory
274 mkdir -p "$1/assets/fonts"
275 printf '%s\n' "$2" > "$1/main.typ"
276}
277HEAD='#set page(width: 200pt, height: 80pt, margin: 10pt)
278#set document(title: "Synthetic", author: "A. Writer", description: "Sub")'
279mkproj "$S/pj_ok" "$HEAD"$'\n= One\nAlpha one two three.\n#pagebreak()\n= Two\nBeta four five six.\n#pagebreak()\nGamma seven.'
280mkproj "$S/pj_font" "$HEAD"$'\n#set text(font: "ZZLeakMarkerZZ")\nHello.'
281mkproj "$S/pj_syn" "$HEAD"$'\nText #ZZLeakMarkerZZ more.'
282
283# One package in Typst's layout, which typst and the CLI read as their cache and the gather as a directory.
284mkdir -p "$S/pk/preview/zzpkg/0.1.0"
285printf '[package]\nname = "zzpkg"\nversion = "0.1.0"\nentrypoint = "lib.typ"\n' > "$S/pk/preview/zzpkg/0.1.0/typst.toml"
286printf '#let zz(x) = [zz(#x)]\n' > "$S/pk/preview/zzpkg/0.1.0/lib.typ"
287mkproj "$S/pj_pkg" "$HEAD"$'\n#import "@preview/zzpkg:0.1.0": zz\n#zz([a])'
288
289door() { # door <project> <out> [options...]: door_pdf.mjs from `tools` (or $TOOLS)
290 local p="$1" out="$2"
291 shift 2
292 gate_cap node "${TOOLS:-$CRATE/tools}/gate/door_pdf.mjs" "$PKG" "$S/$p" main.typ assets/fonts "$out" "$@" 2>&1
293}
294
295r="$(door pj_ok "$S/d_ok.pdf" --again "$S/d_ok2.pdf")"
296expect "door: a project that compiles strict is ok, with its pages, no diagnostic and no need" "$r" "door ok pages 3 kinds none needs 0"
297expect "door: a second compile on the instance is ok" "$r" "door again ok"
298[ "$(npages "$S/d_ok.pdf")" = 3 ] && ok "door: the PDF has the 3 pages" || fail "door: the PDF has $(npages "$S/d_ok.pdf") pages"
299cmp -s "$S/d_ok.pdf" "$S/d_ok2.pdf" && ok "door: the two compiles on one instance are the same bytes" || fail "door: the two compiles differ"
300
301r="$(door pj_font "$S/d_font.pdf")"
302# A refusal's first diagnostic restates its first site, so a refusal for one font counts that kind twice.
303expect "door: a font that is not there is a strict refusal, named by its kind" "$r" "door error pages 1 kinds missing_font:2 needs 0"
304[ ! -e "$S/d_font.pdf" ] && ok "door: and writes no PDF" || fail "door: wrote a PDF for a refusal"
305lackany "door: and prints nothing of the project's text" "$r" "ZZLeakMarker"
306
307r="$(door pj_syn "$S/d_syn.pdf")"
308expect "door: a hard error is named by its kind" "$r" "door error pages none kinds unknown_variable:1 needs 0"
309lackany "door: and prints nothing of the project's text" "$r" "ZZLeakMarker"
310
311r="$(door pj_pkg "$S/d_pkg0.pdf")"
312expect "door: a package that is not supplied is an error, and a need" "$r" "door error pages none kinds package:1 needs 1"
313r="$(door pj_pkg "$S/d_pkg1.pdf" --packages "$S/pk")"
314expect "door: the same project with the package directory is ok" "$r" "door ok pages 1 kinds none needs 0"
315r="$(door pj_pkg "$S/d_pkg2.pdf" --packages "$S/pk" --exclude assets)"
316expect "door: an --exclude of a directory without sources changes nothing" "$r" "door ok pages 1 kinds none needs 0"
317
318PACKS="$HOME/usr/code/web/apps/oxedyne/daimond/www/assets/typst/packs"
319CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/typst/packages/preview"
320if [ -f "$PACKS/preview/cetz/0.3.4.pack" ] && [ -d "$CACHE/cetz/0.3.4" ]; then
321 # A cetz canvas that draws paths, through Daimond's own pack and the door, against Typst's own result. The
322 # paths are drawn by a `for ((kind, ..rest)) in` pattern in the package, which the evaluator once did not bind.
323 mkproj "$S/pj_cetz" "$HEAD"$'\n#set page(height: 200pt)\n#import "@preview/oxifmt:0.2.1": strfmt\n#import "@preview/cetz:0.3.4": canvas, draw\n#strfmt("{} and {}", 1, 2)\n#canvas({\n import draw: *\n line((0, 0), (2, 1))\n circle((1, 1), radius: 0.5)\n})'
324 r="$(door pj_cetz "$S/d_cetz.pdf" --packages "$PACKS")"
325 expect "door: a cetz canvas that draws a line and a circle, from Daimond's pack, is ok" "$r" "door ok pages 1 kinds none needs 0"
326 gate_typst "$TYPST" compile --root "$S/pj_cetz" "$S/pj_cetz/main.typ" "$S/t_cetz.pdf" > /dev/null 2>&1
327 r="$(python3 "$G" g7 "$S/t_cetz.pdf" "$S/d_cetz.pdf" 2>&1)"
328 p1="$(printf '%s\n' "$r" | awk '$1 == "g7" && $2 == "page" && $3 == 1 { print $4 }')"
329 if [ -n "$p1" ] && awk -v f="$p1" 'BEGIN { exit !(f < 0.05) }'; then
330 ok "door: and its ink is Typst's (G7 $p1 of the page differs)"
331 else
332 fail "door: the cetz drawing's ink differs from Typst's (G7 '$p1')"
333 fi
334else
335 echo "selftest: skip real packs (Daimond's packs or Typst's cache of cetz 0.3.4 are absent)"
336fi
337
338r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$PKG" 2>&1)"
339expect "door: too few arguments is a closed word" "$r" "door fail bad-args"
340r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$S/nowhere" "$S/pj_ok" main.typ assets/fonts "$S/x.pdf" 2>&1)"
341expect "door: a package directory that is not there is a closed word" "$r" "door fail bad-pkg"
342r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$PKG" "$S/pj_ok" nothing.typ assets/fonts "$S/x.pdf" 2>&1)"
343expect "door: a main that is not there is a closed word" "$r" "door fail no-main"
344r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$PKG" "$S/pj_ok" main.typ assets/fonts "$S/x.pdf" --packages "$S/nowhere" 2>&1)"
345expect "door: a package directory that is not there is refused" "$r" "door fail no-packages"
346
347# The plants, on a copy of the tools.
348cp -r "$CRATE/tools" "$S/tools_p"
349D0="$CRATE/tools/gate/door_pdf.mjs"
350DP="$S/tools_p/gate/door_pdf.mjs"
351door_plant() { # door_plant <label> <old> <new> <project> <line the planted copy must not print> [options...]
352 local label="$1" old="$2" new="$3" p="$4" want="$5" r
353 shift 5
354 if ! subst "$D0" "$DP" "$old" "$new"; then
355 fail "plant $label: the text it replaces has moved or repeats in door_pdf.mjs"
356 return
357 fi
358 r="$(TOOLS="$S/tools_p" door "$p" "$S/dp.pdf" "$@")"
359 if printf '%s\n' "$r" | grep -qxF -- "$want"; then
360 fail "plant $label: the planted copy still prints: $want"
361 else
362 ok "plant $label: the planted copy no longer prints the line, so the check is red"
363 fi
364 rm -f "${S:?}/dp.pdf"
365}
366door_plant "door not strict" 'strict: true }' 'strict: false }' pj_font "door error pages 1 kinds missing_font:2 needs 0"
367door_plant "door packages not supplied" 'of g.packages) {' 'of []) {' pj_pkg "door ok pages 1 kinds none needs 0" --packages "$S/pk"
368door_plant "door kinds not counted" "return keys.length ? keys.map((k) => \`\${k}:\${n[k]}\`).join(',') : 'none';" "return 'none';" pj_font "door error pages 1 kinds missing_font:2 needs 0"
369door_plant "door needs not counted" 'const needs = first.r && Array.isArray(first.r.needs) ? first.r.needs.length : 0;' 'const needs = 0;' pj_pkg "door error pages none kinds package:1 needs 1"
370door_plant "door second compile not run" 'if (a.again) {' 'if (false) {' pj_ok "door again ok" --again "$S/d_ok3.pdf"
371
372cp "$D0" "$DP"
373
374# ── 3. gate.sh end to end ───────────────────────────────────────────────────
375gate() { # gate <project> [tools dir]: gate.sh, with this project's packages and the CLI's cache beside them
376 local p="$1" t="${2:-$CRATE/tools}"
377 TYPST_PACKAGE_CACHE_PATH="$S/pk" GATE_PACKAGES="$S/pk" GATE_OUT="$S/gout" \
378 "$t/gate/gate.sh" "$S/$p" main.typ assets/fonts "$BIN" "$PKG" 2>&1
379}
380r="$(gate pj_ok)"
381echo "$r" | sed 's/^/ /'
382n="$(printf '%s\n' "$r" | grep -vc '^gate: ')"
383[ "$n" -eq 0 ] && ok "gate: every line is a gate: line" || fail "gate: $n stray line(s)"
384expect "gate: typst compiled" "$r" "gate: typst exit 0"
385expect "gate: the CLI compiled strict" "$r" "gate: cli exit 0"
386expect "gate: the door compiled strict, with the CLI's pages" "$r" "gate: door ok pages 3 kinds none needs 0"
387expect "gate: the door's PDF is the CLI's, byte for byte" "$r" "gate: door same-as-cli"
388expect "gate: G12 is green" "$r" "gate: table g12 green"
389for row in cli door; do
390 for g in g1 g2 g4 g5 g8; do
391 expect "gate: $row $g is green on a project that agrees" "$r" "gate: table $row $g green"
392 done
393 for g in g3 g7 g9; do
394 expect "gate: $row $g is measured" "$r" "gate: table $row $g measured"
395 done
396done
397expect "gate: the verdict is green" "$r" "gate: verdict green"
398[ -z "$(ls -A "$S/gout")" ] && ok "gate: its scratch is gone on exit" || fail "gate: left $(ls -A "$S/gout" | wc -l) entr(ies) behind"
399GATE_OUT="$S/gout" "$CRATE/tools/gate/gate.sh" "$S/pj_ok" main.typ assets/fonts "$BIN" "$PKG" > /dev/null 2>&1
400[ $? -eq 0 ] && ok "gate: the exit status is 0 on a green verdict" || fail "gate: a green verdict did not exit 0"
401
402r="$(gate pj_font)"
403echo "$r" | sed 's/^/ /'
404expect "gate: a project that strict refuses has no CLI checks" "$r" "gate: cli checks skipped"
405expect "gate: and the door refuses it by kind" "$r" "gate: door error pages 1 kinds missing_font:2 needs 0"
406expect "gate: and the verdict is red" "$r" "gate: verdict red"
407lackany "gate: and nothing of the project's text is printed" "$r" "ZZLeakMarker"
408GATE_OUT="$S/gout" "$CRATE/tools/gate/gate.sh" "$S/pj_font" main.typ assets/fonts "$BIN" "$PKG" > /dev/null 2>&1
409[ $? -eq 1 ] && ok "gate: the exit status is 1 on a red verdict" || fail "gate: a red verdict did not exit 1"
410r="$(gate pj_syn)"
411expect "gate: a project that typst cannot compile is a closed word" "$r" "gate: error typst-failed"
412lackany "gate: and nothing of the project's text is printed" "$r" "ZZLeakMarker"
413r="$(GATE_OUT="$S/gout" "$CRATE/tools/gate/gate.sh" 2>&1)"
414expect "gate: no arguments is a usage error" "$r" "gate: error usage"
415
416# The premise of the leak plants: the CLI's own stderr quotes the project's text.
417raw="$(cd "$S/pj_font" && gate_hidden "$BIN" --eval --strict --diag-summary --root . --font-path assets/fonts main.typ "$S/rawout" 2>&1)"
418printf '%s\n' "$raw" | grep -qiF ZZLeakMarker && ok "leak premise: the CLI's own stderr quotes the project's text" \
419 || fail "leak premise: the CLI did not quote the marker, so the next checks prove nothing"
420
421# The plants, on a copy of the tools.
422cp "$CRATE/tools/gate/gate.sh" "$S/gate0.sh"
423GP="$S/tools_p/gate/gate.sh"
424# A fault in a required check, planted into g_checks.py, must turn the verdict red; the table logic that
425# ignores it is the plant that proves that test.
426if subst "$G" "$S/tools_p/gate/g_checks.py" 'same = len(ma) == len(mb) and not differ' 'same = False'; then
427 r="$(gate pj_ok "$S/tools_p")"
428 expect "gate: a required check that is red is red in the table" "$r" "gate: table cli g1 red"
429 expect "gate: and turns the verdict red" "$r" "gate: verdict red"
430 if subst "$S/gate0.sh" "$GP" 'if [ "$v" = same ]; then' 'if true; then'; then
431 r="$(gate pj_ok "$S/tools_p")"
432 if printf '%s\n' "$r" | grep -qxF "gate: verdict red"; then
433 fail "plant gate red ignored: the planted copy is still red"
434 printf '%s\n' "$r" | grep -v 'g[1-8] \|g12 ' | sed 's/^/ /'
435 else
436 ok "plant gate red ignored: the planted copy reads green with a red check, so the test is red"
437 fi
438 else
439 fail "plant gate red ignored: the text it replaces has moved in gate.sh"
440 fi
441else
442 fail "plant: g_checks.py's G1 verdict line has moved"
443fi
444cp "$G" "$S/tools_p/gate/g_checks.py"
445# Defence in depth: the stage filter alone does not let the text out, the stage filter and the final whitelist
446# together do.
447if subst "$S/gate0.sh" "$GP" '/: warning: / { w++; next }' '{ print } /: warning: / { w++; next }'; then
448 r="$(gate pj_font "$S/tools_p")"
449 lackany "gate: with the CLI filter broken the final whitelist still holds" "$r" "ZZLeakMarker"
450 python3 - "$GP" <<'PY'
451import sys
452p = sys.argv[1]
453s = open(p).read()
454i = s.index('# The final whitelist')
455open(p, 'w').write(s[:i] + 'main "$@" 2>&1\nexit "$?"\n')
456PY
457 r="$(gate pj_font "$S/tools_p")"
458 if printf '%s\n' "$r" | grep -qiF ZZLeakMarker; then
459 ok "plant gate whitelist: with the CLI filter and the final whitelist both gone the text is printed, so the filters are what holds it"
460 else
461 fail "plant gate whitelist: the text was not printed with both filters gone, so the premise is wrong"
462 fi
463else
464 fail "plant: the CLI filter's first line has moved in gate.sh"
465fi
466rm -rf "${S:?}/gout"
467mkdir -p "$S/gout"
468if subst "$S/gate0.sh" "$GP" "trap 'rm -rf \"\${W:?}\"' EXIT" "trap '' EXIT"; then
469 gate pj_ok "$S/tools_p" > /dev/null
470 [ -n "$(ls -A "$S/gout")" ] && ok "plant gate scratch: with the trap gone the scratch is left behind, so the test is red" \
471 || fail "plant gate scratch: the planted copy left nothing behind"
472else
473 fail "plant: gate.sh's trap has moved"
474fi
475
476# ── 4. layout against Typst, line by line (G9) ──────────────────────────────
477# Projects that Austenite once set differently from Typst, each held to G9 with the link counts left out (Austenite
478# writes no link annotation for a footnote's marks). A page that differs in a count, an extent, a pitch or a line's
479# place is red. The faults are in the engine, so each project was shown red against the engine before its fix:
480# a terms item shrunk to its longest line (a justified first line left short), a footnote entry given the first-line
481# indent of every paragraph, a paragraph lost between two lists.
482layout_same() { # layout_same <label> <project>: G9 finds no difference but in the links
483 local label="$1" p="$2"
484 r="$(gate "$p")"
485 expect "layout: $label compiles with Typst and with the CLI" "$r" "gate: cli exit 0"
486 local diff
487 diff="$(printf '%s\n' "$r" | grep -E '^gate: cli g9 (page [0-9]+ (lines|blocks|words|sizes|images|left|right|top|bottom|pitch|height) |line )' || true)"
488 if [ -z "$diff" ]; then ok "layout: $label has no line, extent or count that G9 reads differently"; else fail "layout: $label differs from Typst: $diff"; fi
489 expect "layout: $label has the same pages" "$r" "gate: cli g8 verdict same"
490}
491LAYOUT_HEAD=$'#set page(width: 612pt, height: 300pt, margin: 20pt)\n#set text(size: 10pt)'
492mkproj "$S/pj_terms" "$LAYOUT_HEAD"$'\n#set par(justify: true, leading: 0.55em, spacing: 1.2em)\n#terms(terms.item[Term 1][#lorem(25)], terms.item[T][#lorem(25)])\n#terms(tight: false, indent: 1em, hanging-indent: 3em, terms.item[Term 1][#lorem(25)])'
493layout_same "a justified terms list" pj_terms
494mkproj "$S/pj_notes" "$LAYOUT_HEAD"$'\n#set par(first-line-indent: (amount: 1.5em, all: true), justify: true)\nFirst.#footnote[#lorem(30)] #lorem(20)\n\nSecond.#footnote[#lorem(12)] #lorem(15)'
495layout_same "footnotes under an every-paragraph first-line indent" pj_notes
496mkproj "$S/pj_lists" "$LAYOUT_HEAD"$'\n- one\n- two\n\nBetween the lists.\n\n- three\n\n+ four\nText with no blank line.\n+ five'
497layout_same "a paragraph between lists" pj_lists
498
499echo "selftest: $bad failure(s)"
500[ "$bad" -eq 0 ]