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. |
| 18 | set -u |
| 19 | |
| 20 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 21 | CRATE="$(cd "$HERE/../.." && pwd)" |
| 22 | # shellcheck source=gate_lib.sh |
| 23 | . "$HERE/gate_lib.sh" |
| 24 | |
| 25 | BIN="${1:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}" |
| 26 | PKG="${2:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}" |
| 27 | PARENT="${3:?usage: selftest_gate.sh <austenite-bin> <pkg> <scratch>}" |
| 28 | TYPST="${GATE_TYPST:-/home/jason/bin/typst}" |
| 29 | mkdir -p "$PARENT" || exit 2 |
| 30 | S="$(mktemp -d "$PARENT/o2.XXXXXX")" || exit 2 |
| 31 | trap 'rm -rf "${S:?}"' EXIT |
| 32 | export GATE_SCRATCH="$S" |
| 33 | |
| 34 | bad=0 |
| 35 | ok() { echo "selftest: ok $1"; } |
| 36 | fail() { echo "selftest: FAIL $1"; bad=$((bad + 1)); } |
| 37 | expect() { # 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 | } |
| 40 | lack() { # 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 | } |
| 43 | lackany() { # 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 | |
| 47 | G="$HERE/g_checks.py" |
| 48 | |
| 49 | # ── typst documents ───────────────────────────────────────────────────────── |
| 50 | mkpdf() { # 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 | } |
| 54 | npages() { 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. |
| 57 | subst() { |
| 58 | python3 - "$@" <<'PY' |
| 59 | import sys |
| 60 | src, dst, old, new = sys.argv[1:] |
| 61 | text = open(src).read() |
| 62 | if text.count(old) != 1: |
| 63 | sys.exit(1) |
| 64 | open(dst, 'w').write(text.replace(old, new)) |
| 65 | PY |
| 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. |
| 71 | plant() { |
| 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 ────────────────────────────────────────────────────────────────── |
| 87 | mkpdf p3 $'Alpha one\n#pagebreak()\nBeta two\n#pagebreak()\nGamma three' |
| 88 | mkpdf p3tall $'Alpha one\n#pagebreak()\n#set page(height: 100pt)\nBeta two\n#pagebreak()\n#set page(height: 80pt)\nGamma three' |
| 89 | mkpdf p2 $'Alpha one\n#pagebreak()\nBeta two' |
| 90 | r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p3.pdf")" |
| 91 | expect "g1: a PDF agrees with itself" "$r" "g1 verdict same" |
| 92 | r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p3tall.pdf")" |
| 93 | expect "g1: a page of another size goes red, on that page" "$r" "g1 mediabox differ 1 pages 2" |
| 94 | expect "g1: and the deviation is its 20 pt" "$r" "g1 mediabox max-deviation 20.0000" |
| 95 | expect "g1: and the verdict is differs" "$r" "g1 verdict differs" |
| 96 | r="$(python3 "$G" g1 "$S/p3.pdf" "$S/p2.pdf")" |
| 97 | expect "g1: a page that is missing goes red" "$r" "g1 pages a 3 b 2" |
| 98 | expect "g1: and the verdict is differs" "$r" "g1 verdict differs" |
| 99 | plant "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" |
| 100 | plant "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 ────────────────────────────────────────────────────────────────── |
| 103 | mkpdf f_plain 'Alpha one two' |
| 104 | mkpdf f_other 'Beta three four five' |
| 105 | mkpdf f_mono $'Alpha one two\n#text(font: "DejaVu Sans Mono")[mono]' |
| 106 | r="$(python3 "$G" g2 "$S/f_plain.pdf" "$S/f_other.pdf")" |
| 107 | expect "g2: the same fonts with other glyphs agree (the subset tag is stripped)" "$r" "g2 verdict same" |
| 108 | r="$(python3 "$G" g2 "$S/f_plain.pdf" "$S/f_mono.pdf")" |
| 109 | expect "g2: a font that only b embeds is named" "$r" "g2 only-b DejaVuSansMono" |
| 110 | expect "g2: and the verdict is differs" "$r" "g2 verdict differs" |
| 111 | plant "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" |
| 112 | plant "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. |
| 114 | python3 - "$S/noemb.pdf" <<'PY' |
| 115 | import sys |
| 116 | import pikepdf |
| 117 | pdf = pikepdf.Pdf.new() |
| 118 | font = pdf.make_indirect(pikepdf.Dictionary(Type=pikepdf.Name.Font, Subtype=pikepdf.Name.Type1, BaseFont=pikepdf.Name.Helvetica)) |
| 119 | page = pdf.add_blank_page(page_size=(200, 80)) |
| 120 | page.Resources = pikepdf.Dictionary(Font=pikepdf.Dictionary(F1=font)) |
| 121 | page.Contents = pdf.make_stream(b'BT /F1 12 Tf 10 40 Td (Hello) Tj ET') |
| 122 | pdf.save(sys.argv[1]) |
| 123 | PY |
| 124 | r="$(python3 "$G" g2 "$S/noemb.pdf" "$S/noemb.pdf")" |
| 125 | expect "g2: a font that is not embedded and has no Unicode map is flagged on both sides" "$r" "g2 flags a 1 b 1" |
| 126 | expect "g2: and the verdict is differs although the sets are equal" "$r" "g2 verdict differs" |
| 127 | plant "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 ────────────────────────────────────────────────────────────────── |
| 130 | mkpdf h_num $'#set heading(numbering: "1.1")\n= One\n== Under\n= Two' |
| 131 | mkpdf h_plain $'= One\n== Under\n= Two' |
| 132 | mkpdf h_flat $'= One\n= Under\n= Two' |
| 133 | mkpdf h_short $'= One\n== Under' |
| 134 | mkpdf h_ab $'= Alpha\n= Beta' |
| 135 | mkpdf h_ba $'= Beta\n= Alpha' |
| 136 | r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_plain.pdf")" |
| 137 | expect "g4: an outline agrees with itself" "$r" "g4 verdict same" |
| 138 | r="$(python3 "$G" g4 "$S/h_num.pdf" "$S/h_plain.pdf")" |
| 139 | expect "g4: a numbered title and a plain one differ, with the levels equal" "$r" "g4 levels equal first-diff none" |
| 140 | expect "g4: all 3 titles are unmatched" "$r" "g4 titles differs unmatched a 3 b 3" |
| 141 | expect "g4: and all 3 differ only by a leading number" "$r" "g4 titles prefix-only 3" |
| 142 | expect "g4: and the verdict is differs" "$r" "g4 verdict differs" |
| 143 | r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_flat.pdf")" |
| 144 | expect "g4: a title at another level goes red, at its place" "$r" "g4 levels differs first-diff 2" |
| 145 | r="$(python3 "$G" g4 "$S/h_plain.pdf" "$S/h_short.pdf")" |
| 146 | expect "g4: a missing entry is counted" "$r" "g4 outline a 3 b 2" |
| 147 | expect "g4: and the levels differ where it is missing" "$r" "g4 levels differs first-diff 3" |
| 148 | r="$(python3 "$G" g4 "$S/h_ab.pdf" "$S/h_ba.pdf")" |
| 149 | expect "g4: the same titles in another order are one multiset" "$r" "g4 titles equal unmatched a 0 b 0" |
| 150 | expect "g4: but two are out of place" "$r" "g4 titles position differ 2 of 2" |
| 151 | expect "g4: and the verdict is differs" "$r" "g4 verdict differs" |
| 152 | plant "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" |
| 153 | plant "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" |
| 154 | plant "g4 order ignored" 'pos += 1' 'pos += 0' "g4 verdict differs" g4 "$S/h_ab.pdf" "$S/h_ba.pdf" |
| 155 | |
| 156 | # ── 1d. G5 ────────────────────────────────────────────────────────────────── |
| 157 | mkpdf i_full $'#set document(title: "T", author: "Au", description: "Sub")\nbody' |
| 158 | mkpdf i_part $'#set document(title: "T", author: "Other")\nbody' |
| 159 | r="$(python3 "$G" g5 "$S/i_full.pdf" "$S/i_full.pdf")" |
| 160 | expect "g5: Info agrees with itself" "$r" "g5 verdict same" |
| 161 | expect "g5: an entry that neither carries is absent-both" "$r" "g5 keywords absent-both" |
| 162 | r="$(python3 "$G" g5 "$S/i_full.pdf" "$S/i_part.pdf")" |
| 163 | expect "g5: an equal title" "$r" "g5 title equal" |
| 164 | expect "g5: a different author" "$r" "g5 author differs" |
| 165 | expect "g5: an entry only a carries differs" "$r" "g5 subject differs" |
| 166 | expect "g5: and the verdict is differs" "$r" "g5 verdict differs" |
| 167 | plant "g5 values ignored" 'elif x == y:' 'elif True:' "g5 author differs" g5 "$S/i_full.pdf" "$S/i_part.pdf" |
| 168 | plant "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 ────────────────────────────────────────────────────────────────── |
| 171 | mkpdf r_plain $'Alpha one\n#pagebreak()\nBeta two' |
| 172 | mkpdf r_red $'Alpha one\n#pagebreak()\nBeta two\n#place(top + left, dx: -10pt, dy: -10pt, rect(width: 200pt, height: 40pt, fill: red))' |
| 173 | r="$(python3 "$G" g7 "$S/r_plain.pdf" "$S/r_red.pdf")" |
| 174 | expect "g7: the page that is the same reads 0" "$r" "g7 page 1 0.0000" |
| 175 | p2="$(printf '%s\n' "$r" | awk '$1 == "g7" && $2 == "page" && $3 == 2 { print $4 }')" |
| 176 | if [ -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)" |
| 178 | else |
| 179 | fail "g7: the red half page read '$p2', wanted 0.4 to 0.6" |
| 180 | fi |
| 181 | expect "g7: both pages were compared" "$r" "g7 compared 2 of a 2 b 2" |
| 182 | r="$(env -u GATE_SCRATCH python3 "$G" g7 "$S/r_plain.pdf" "$S/r_red.pdf")" |
| 183 | expect "g7: with no scratch directory it refuses, and writes no raster" "$r" "g7 error scratch" |
| 184 | plant "g7 threshold" 'diff > 16' 'diff > 255' "g7 page 2 $p2" g7 "$S/r_plain.pdf" "$S/r_red.pdf" |
| 185 | plant "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 ────────────────────────────────────────────────────────────────── |
| 189 | words() { echo "#range($1).map(_ => \"abcde\").join(\" \")"; } |
| 190 | mkpdf c_a $'#set page(height: 300pt)\n'"$(words 60)"$'\n#pagebreak()\n'"$(words 60)" |
| 191 | mkpdf c_b $'#set page(height: 300pt)\n'"$(words 60) f"$'\n#pagebreak()\n'"$(words 66)" |
| 192 | mkpdf 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" |
| 194 | r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_a.pdf")" |
| 195 | expect "g8: a PDF agrees with itself" "$r" "g8 verdict same" |
| 196 | r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_b.pdf")" |
| 197 | expect "g8: a page 0.3% over is within the 1%, one 10% over is not" "$r" "g8 out-of-tolerance 1 pages 2" |
| 198 | expect "g8: its two counts are named" "$r" "g8 page 2 a 300 b 330" |
| 199 | expect "g8: and the verdict is differs" "$r" "g8 verdict differs" |
| 200 | r="$(python3 "$G" g8 "$S/c_a.pdf" "$S/c_blank.pdf")" |
| 201 | expect "g8: a page with no text where a has some is named" "$r" "g8 empty 1 pages 2" |
| 202 | plant "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" |
| 203 | plant "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 ───────────────────────────────────────────────────────────────── |
| 206 | python3 - "$S/mark.png" <<'PY' |
| 207 | import sys |
| 208 | from PIL import Image |
| 209 | Image.new('RGB', (8, 8), (200, 40, 40)).save(sys.argv[1]) |
| 210 | PY |
| 211 | TXT="#range(40).map(i => \"word\" + str(i)).join(\" \")" |
| 212 | mkpdf y_a $'#set page(height: 200pt)\n'"$TXT"$'\n\n'"$TXT" |
| 213 | mkpdf y_same $'#set page(height: 200pt)\n'"$TXT"$'\n\n'"$TXT" |
| 214 | mkpdf y_lead $'#set page(height: 200pt)\n#set par(leading: 1.2em)\n'"$TXT"$'\n\n'"$TXT" |
| 215 | mkpdf y_cols $'#set page(height: 200pt, columns: 2)\n'"$TXT"$'\n\n'"$TXT" |
| 216 | mkpdf y_img $'#set page(height: 200pt)\n#image("mark.png", width: 20pt)\n'"$TXT"$'\n\n'"$TXT" |
| 217 | mkpdf y_link $'#set page(height: 200pt)\n#link("https://example.org")[link] '"$TXT"$'\n\n'"$TXT" |
| 218 | r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_same.pdf")" |
| 219 | expect "g9: the same layout differs on no page" "$r" "g9 pages differ 0 pages none" |
| 220 | r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_lead.pdf")" |
| 221 | echo "$r" | grep -q '^g9 pages differ [1-9]' && ok "g9: a looser leading differs" || fail "g9: a looser leading read as no difference" |
| 222 | pitch="$(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'" |
| 224 | want_pitch="$(printf '%s\n' "$r" | grep '^g9 page 1 pitch ')" |
| 225 | want_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" |
| 227 | r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_cols.pdf")" |
| 228 | narrow="$(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'" |
| 230 | r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_img.pdf")" |
| 231 | expect "g9: an image that only b places is counted" "$r" "g9 page 1 images a 0 b 1" |
| 232 | r="$(python3 "$G" g9 "$S/y_a.pdf" "$S/y_link.pdf")" |
| 233 | expect "g9: a link that only b carries is counted" "$r" "g9 page 1 links a 0 b 1" |
| 234 | plant "g9 pitch ignored" "if abs(a['pitch'] - b['pitch']) > 0.1:" 'if False:' "$want_pitch" g9 "$S/y_a.pdf" "$S/y_lead.pdf" |
| 235 | plant "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" |
| 236 | plant "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" |
| 237 | plant "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 ───────────────────────────────────────────────────────────────── |
| 240 | touch_byte() { # touch_byte <in> <out>: the same size, one byte changed |
| 241 | python3 - "$1" "$2" <<'PY' |
| 242 | import sys |
| 243 | d = bytearray(open(sys.argv[1], 'rb').read()) |
| 244 | d[100] ^= 0x55 |
| 245 | open(sys.argv[2], 'wb').write(bytes(d)) |
| 246 | PY |
| 247 | } |
| 248 | cp "$S/p3.pdf" "$S/x1.pdf" |
| 249 | cp "$S/p3.pdf" "$S/x2.pdf" |
| 250 | cp "$S/p3.pdf" "$S/x3.pdf" |
| 251 | touch_byte "$S/p3.pdf" "$S/y.pdf" |
| 252 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/x3.pdf")" |
| 253 | expect "g12: five equal PDFs agree" "$r" "g12 verdict same" |
| 254 | expect "g12: and the CLI's is the door's" "$r" "g12 cli-door equal" |
| 255 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/y.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/x3.pdf")" |
| 256 | expect "g12: two CLI runs that differ go red" "$r" "g12 cli-twice differs" |
| 257 | expect "g12: and the verdict is differs" "$r" "g12 verdict differs" |
| 258 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/y.pdf" "$S/x3.pdf")" |
| 259 | expect "g12: two compiles on one door instance that differ go red" "$r" "g12 door-one-instance differs" |
| 260 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/y.pdf")" |
| 261 | expect "g12: a second instance that differs goes red" "$r" "g12 door-two-instances differs" |
| 262 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x2.pdf" "$S/x1.pdf" "$S/x2.pdf" "$S/none.pdf")" |
| 263 | expect "g12: a PDF that is absent is named" "$r" "g12 door-two-instances absent" |
| 264 | expect "g12: and the verdict is differs" "$r" "g12 verdict differs" |
| 265 | r="$(python3 "$G" g12 "$S/x1.pdf" "$S/x1.pdf" "$S/y.pdf" "$S/y.pdf" "$S/y.pdf")" |
| 266 | expect "g12: a CLI PDF that is not the door's is reported" "$r" "g12 cli-door differs" |
| 267 | expect "g12: and does not turn the verdict red" "$r" "g12 verdict same" |
| 268 | plant "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" |
| 269 | plant "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 ───────────────────────────────────────────────────────── |
| 273 | mkproj() { # 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 | } |
| 277 | HEAD='#set page(width: 200pt, height: 80pt, margin: 10pt) |
| 278 | #set document(title: "Synthetic", author: "A. Writer", description: "Sub")' |
| 279 | mkproj "$S/pj_ok" "$HEAD"$'\n= One\nAlpha one two three.\n#pagebreak()\n= Two\nBeta four five six.\n#pagebreak()\nGamma seven.' |
| 280 | mkproj "$S/pj_font" "$HEAD"$'\n#set text(font: "ZZLeakMarkerZZ")\nHello.' |
| 281 | mkproj "$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. |
| 284 | mkdir -p "$S/pk/preview/zzpkg/0.1.0" |
| 285 | printf '[package]\nname = "zzpkg"\nversion = "0.1.0"\nentrypoint = "lib.typ"\n' > "$S/pk/preview/zzpkg/0.1.0/typst.toml" |
| 286 | printf '#let zz(x) = [zz(#x)]\n' > "$S/pk/preview/zzpkg/0.1.0/lib.typ" |
| 287 | mkproj "$S/pj_pkg" "$HEAD"$'\n#import "@preview/zzpkg:0.1.0": zz\n#zz([a])' |
| 288 | |
| 289 | door() { # 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 | |
| 295 | r="$(door pj_ok "$S/d_ok.pdf" --again "$S/d_ok2.pdf")" |
| 296 | expect "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" |
| 297 | expect "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" |
| 299 | cmp -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 | |
| 301 | r="$(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. |
| 303 | expect "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" |
| 305 | lackany "door: and prints nothing of the project's text" "$r" "ZZLeakMarker" |
| 306 | |
| 307 | r="$(door pj_syn "$S/d_syn.pdf")" |
| 308 | expect "door: a hard error is named by its kind" "$r" "door error pages none kinds unknown_variable:1 needs 0" |
| 309 | lackany "door: and prints nothing of the project's text" "$r" "ZZLeakMarker" |
| 310 | |
| 311 | r="$(door pj_pkg "$S/d_pkg0.pdf")" |
| 312 | expect "door: a package that is not supplied is an error, and a need" "$r" "door error pages none kinds package:1 needs 1" |
| 313 | r="$(door pj_pkg "$S/d_pkg1.pdf" --packages "$S/pk")" |
| 314 | expect "door: the same project with the package directory is ok" "$r" "door ok pages 1 kinds none needs 0" |
| 315 | r="$(door pj_pkg "$S/d_pkg2.pdf" --packages "$S/pk" --exclude assets)" |
| 316 | expect "door: an --exclude of a directory without sources changes nothing" "$r" "door ok pages 1 kinds none needs 0" |
| 317 | |
| 318 | PACKS="$HOME/usr/code/web/apps/oxedyne/daimond/www/assets/typst/packs" |
| 319 | CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/typst/packages/preview" |
| 320 | if [ -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 |
| 334 | else |
| 335 | echo "selftest: skip real packs (Daimond's packs or Typst's cache of cetz 0.3.4 are absent)" |
| 336 | fi |
| 337 | |
| 338 | r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$PKG" 2>&1)" |
| 339 | expect "door: too few arguments is a closed word" "$r" "door fail bad-args" |
| 340 | r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$S/nowhere" "$S/pj_ok" main.typ assets/fonts "$S/x.pdf" 2>&1)" |
| 341 | expect "door: a package directory that is not there is a closed word" "$r" "door fail bad-pkg" |
| 342 | r="$(gate_cap node "$CRATE/tools/gate/door_pdf.mjs" "$PKG" "$S/pj_ok" nothing.typ assets/fonts "$S/x.pdf" 2>&1)" |
| 343 | expect "door: a main that is not there is a closed word" "$r" "door fail no-main" |
| 344 | r="$(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)" |
| 345 | expect "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. |
| 348 | cp -r "$CRATE/tools" "$S/tools_p" |
| 349 | D0="$CRATE/tools/gate/door_pdf.mjs" |
| 350 | DP="$S/tools_p/gate/door_pdf.mjs" |
| 351 | door_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 | } |
| 366 | door_plant "door not strict" 'strict: true }' 'strict: false }' pj_font "door error pages 1 kinds missing_font:2 needs 0" |
| 367 | door_plant "door packages not supplied" 'of g.packages) {' 'of []) {' pj_pkg "door ok pages 1 kinds none needs 0" --packages "$S/pk" |
| 368 | door_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" |
| 369 | door_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" |
| 370 | door_plant "door second compile not run" 'if (a.again) {' 'if (false) {' pj_ok "door again ok" --again "$S/d_ok3.pdf" |
| 371 | |
| 372 | cp "$D0" "$DP" |
| 373 | |
| 374 | # ── 3. gate.sh end to end ─────────────────────────────────────────────────── |
| 375 | gate() { # 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 | } |
| 380 | r="$(gate pj_ok)" |
| 381 | echo "$r" | sed 's/^/ /' |
| 382 | n="$(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)" |
| 384 | expect "gate: typst compiled" "$r" "gate: typst exit 0" |
| 385 | expect "gate: the CLI compiled strict" "$r" "gate: cli exit 0" |
| 386 | expect "gate: the door compiled strict, with the CLI's pages" "$r" "gate: door ok pages 3 kinds none needs 0" |
| 387 | expect "gate: the door's PDF is the CLI's, byte for byte" "$r" "gate: door same-as-cli" |
| 388 | expect "gate: G12 is green" "$r" "gate: table g12 green" |
| 389 | for 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 |
| 396 | done |
| 397 | expect "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" |
| 399 | GATE_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 | |
| 402 | r="$(gate pj_font)" |
| 403 | echo "$r" | sed 's/^/ /' |
| 404 | expect "gate: a project that strict refuses has no CLI checks" "$r" "gate: cli checks skipped" |
| 405 | expect "gate: and the door refuses it by kind" "$r" "gate: door error pages 1 kinds missing_font:2 needs 0" |
| 406 | expect "gate: and the verdict is red" "$r" "gate: verdict red" |
| 407 | lackany "gate: and nothing of the project's text is printed" "$r" "ZZLeakMarker" |
| 408 | GATE_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" |
| 410 | r="$(gate pj_syn)" |
| 411 | expect "gate: a project that typst cannot compile is a closed word" "$r" "gate: error typst-failed" |
| 412 | lackany "gate: and nothing of the project's text is printed" "$r" "ZZLeakMarker" |
| 413 | r="$(GATE_OUT="$S/gout" "$CRATE/tools/gate/gate.sh" 2>&1)" |
| 414 | expect "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. |
| 417 | raw="$(cd "$S/pj_font" && gate_hidden "$BIN" --eval --strict --diag-summary --root . --font-path assets/fonts main.typ "$S/rawout" 2>&1)" |
| 418 | printf '%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. |
| 422 | cp "$CRATE/tools/gate/gate.sh" "$S/gate0.sh" |
| 423 | GP="$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. |
| 426 | if 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 |
| 441 | else |
| 442 | fail "plant: g_checks.py's G1 verdict line has moved" |
| 443 | fi |
| 444 | cp "$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. |
| 447 | if 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' |
| 451 | import sys |
| 452 | p = sys.argv[1] |
| 453 | s = open(p).read() |
| 454 | i = s.index('# The final whitelist') |
| 455 | open(p, 'w').write(s[:i] + 'main "$@" 2>&1\nexit "$?"\n') |
| 456 | PY |
| 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 |
| 463 | else |
| 464 | fail "plant: the CLI filter's first line has moved in gate.sh" |
| 465 | fi |
| 466 | rm -rf "${S:?}/gout" |
| 467 | mkdir -p "$S/gout" |
| 468 | if 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" |
| 472 | else |
| 473 | fail "plant: gate.sh's trap has moved" |
| 474 | fi |
| 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. |
| 482 | layout_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 | } |
| 491 | LAYOUT_HEAD=$'#set page(width: 612pt, height: 300pt, margin: 20pt)\n#set text(size: 10pt)' |
| 492 | mkproj "$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)])' |
| 493 | layout_same "a justified terms list" pj_terms |
| 494 | mkproj "$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)' |
| 495 | layout_same "footnotes under an every-paragraph first-line indent" pj_notes |
| 496 | mkproj "$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' |
| 497 | layout_same "a paragraph between lists" pj_lists |
| 498 | |
| 499 | echo "selftest: $bad failure(s)" |
| 500 | [ "$bad" -eq 0 ] |