oxedyne/fe2o3/fe2o3_austenite/tools/bench/door_smoke.mjs
14.3 KiB, 1 run
created by r1870400018:66235, 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 node |
| 2 | // door_smoke.mjs -- the wasm package's doors, driven with Daimond's project shape in node. |
| 3 | // |
| 4 | // The package is a wasm-pack `--target web` output. Each case is a strict project, `{ main, sources, assets, |
| 5 | // fonts, strict: true }`, compiled through `compileProject`, and printed as one line: |
| 6 | // |
| 7 | // <case> ok|error <kind> <file>:<line>:<col> |
| 8 | // |
| 9 | // `ok` carries `-` and `-:0:0`; `error` carries the first diagnostic, which is the error's own site, its kind |
| 10 | // and its 1-based line and UTF-16 column. The native test `tests/eval_door.rs` prints the same lines through |
| 11 | // the same door, and both are held to `tests/fixtures/door_smoke_lines.txt`, so the browser's results are the |
| 12 | // native ones. After the case lines come `interop <name> ok` lines for the surface the cases do not reach: the |
| 13 | // PDF copy-out, the other doors, the package calls, `needs`, `queryProject` and `engineInfo`. |
| 14 | // |
| 15 | // node tools/bench/door_smoke.mjs --pkg DIR [--expect FILE] [--git SHA] [--delta] |
| 16 | // |
| 17 | // Run it under a memory cap, as every heavy job on this host is: |
| 18 | // systemd-run --user --scope --quiet -p MemoryMax=3G --slice=claude-rc.slice node tools/bench/door_smoke.mjs --pkg DIR |
| 19 | // |
| 20 | // Options: |
| 21 | // --pkg DIR the wasm-pack output directory (required) |
| 22 | // --expect FILE fail unless the case lines equal the lines in FILE |
| 23 | // --git SHA fail unless `engineInfo().git` is exactly SHA (a build from a committed tree) |
| 24 | // --delta also drive the live view's changed-only delta, `compileProjectDelta`, over six pages: the |
| 25 | // `order` is as long as `pages`, every changed SVG holds the `.tsel` text layer, an unchanged |
| 26 | // second call sends no SVG, a one-letter edit sends one page, a refusal leaves `version` |
| 27 | // where it was, and a page that differs only in its face has another id |
| 28 | |
| 29 | import { readFileSync, readdirSync } from 'node:fs'; |
| 30 | import { join, resolve } from 'node:path'; |
| 31 | import { pathToFileURL } from 'node:url'; |
| 32 | |
| 33 | function opt(name, dflt) { |
| 34 | const i = process.argv.indexOf(name); |
| 35 | return i >= 0 && i + 1 < process.argv.length ? process.argv[i + 1] : dflt; |
| 36 | } |
| 37 | |
| 38 | const dir = opt('--pkg', null); |
| 39 | if (!dir) { |
| 40 | console.error('usage: door_smoke.mjs --pkg DIR [--expect FILE] [--git SHA]'); |
| 41 | process.exit(2); |
| 42 | } |
| 43 | |
| 44 | // The cases, identical to the native test's. |
| 45 | const SKELETON = '#set page(width: 200pt, height: 100pt)\n= Skeleton\nA short paragraph.\n'; |
| 46 | const CASES = [ |
| 47 | ['skeleton', SKELETON], |
| 48 | ['missing_font', '#set text(font: "Nope Sans")\nHello.\n'], |
| 49 | ['missing_import', 'Intro.\n#import "gone.typ": thing\n'], |
| 50 | ['missing_image', 'Text.\n\n#image("gone.png")\n'], |
| 51 | ['empty_main', ''], |
| 52 | ]; |
| 53 | |
| 54 | const project = (text, extra = {}) => ({ |
| 55 | main: '/main.typ', sources: [['/main.typ', text]], assets: [], fonts: [], strict: true, ...extra, |
| 56 | }); |
| 57 | |
| 58 | function line(name, r) { |
| 59 | if (r && r.pdf) return `${name} ok - -:0:0`; |
| 60 | const d = r && r.diagnostics && r.diagnostics[0]; |
| 61 | if (!d) return `${name} error - ${String(r && r.error)}`; |
| 62 | return `${name} error ${d.kind} ${d.file}:${d.line}:${d.col}`; |
| 63 | } |
| 64 | |
| 65 | const failures = []; |
| 66 | function check(name, ok, why) { |
| 67 | if (ok) { |
| 68 | console.log(`interop ${name} ok`); |
| 69 | } else { |
| 70 | console.log(`interop ${name} FAILED ${why || ''}`); |
| 71 | failures.push(name); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | const js = readdirSync(dir).find((n) => /^oxedyne_fe2o3_austenite\.js$/.test(n)); |
| 76 | const wasm = readdirSync(dir).find((n) => /_bg\.wasm$/.test(n)); |
| 77 | if (!js || !wasm) { |
| 78 | console.error('no wasm-pack output in ' + dir); |
| 79 | process.exit(2); |
| 80 | } |
| 81 | const mod = await import(pathToFileURL(join(resolve(dir), js)).href); |
| 82 | await mod.default({ module_or_path: readFileSync(join(resolve(dir), wasm)) }); |
| 83 | const aus = new mod.DaimondTypst(); |
| 84 | |
| 85 | const lines = []; |
| 86 | for (const [name, text] of CASES) { |
| 87 | const l = line(name, aus.compileProject(project(text))); |
| 88 | lines.push(l); |
| 89 | console.log(l); |
| 90 | } |
| 91 | |
| 92 | // The PDF is copied out whole, at its own length. |
| 93 | const made = aus.compileProject(project(SKELETON)); |
| 94 | check('pdf copy-out', |
| 95 | made.pdf instanceof Uint8Array && made.pdf.length > 500 |
| 96 | && Buffer.from(made.pdf.subarray(0, 5)).toString('latin1') === '%PDF-' |
| 97 | && Buffer.from(made.pdf.subarray(made.pdf.length - 6)).toString('latin1').includes('%%EOF'), |
| 98 | `length ${made.pdf && made.pdf.length}`); |
| 99 | check('result shape', made.pages === 1 && Array.isArray(made.diagnostics) && made.diagnostics.length === 0 |
| 100 | && made.skipped === null && Array.isArray(made.needs) && made.needs.length === 0, |
| 101 | JSON.stringify({ pages: made.pages, skipped: made.skipped, needs: made.needs })); |
| 102 | |
| 103 | // The error shape: the head, then the sites; the head's own line is `error`. |
| 104 | const bad = aus.compileProject(project('#set text(font: "Nope Sans")\nx\n')); |
| 105 | check('error shape', typeof bad.error === 'string' && bad.error.startsWith('/main.typ:1:17: ') |
| 106 | && bad.diagnostics[0].severity === 'error' && bad.diagnostics[0].hint === 'missing_font ×1' |
| 107 | && Array.isArray(bad.needs), String(bad.error)); |
| 108 | |
| 109 | // queryProject after a good compile: the rail's rows. |
| 110 | aus.compileProject(project('#set page(height: 60pt, margin: 5pt)\n= One\nx\n#pagebreak()\n== Two\ny\n')); |
| 111 | const rows = aus.queryProject('heading', ''); |
| 112 | check('queryProject heading rows', Array.isArray(rows) && rows.length === 2 |
| 113 | && rows[0].title === 'One' && rows[0].page === 1 && rows[0].level === 1 && rows[0].kind === 'heading' |
| 114 | && rows[1].title === 'Two' && rows[1].page === 2 && rows[1].level === 2, |
| 115 | JSON.stringify(rows)); |
| 116 | check('queryProject null after a failed compile', (aus.compileProject(project('#import "gone.typ"\n')), aus.queryProject('heading', '') === null)); |
| 117 | check('queryProject null for a selector that does not evaluate', (aus.compileProject(project('= A\n')), aus.queryProject('nonesuch(', '') === null)); |
| 118 | |
| 119 | // The other doors. |
| 120 | const svg = aus.compileProjectVector(project(SKELETON)); |
| 121 | check('vector', Array.isArray(svg.svg) && svg.svg.length === 1 && svg.svg[0].includes('<svg') |
| 122 | && svg.svg[0].includes('class="tsel"') && svg.pages === 1); |
| 123 | const delta = aus.compileProjectDelta(project('= Delta\nbody\n', { known: [] })); |
| 124 | check('delta', Array.isArray(delta.order) && delta.order.length === 1 && delta.reset === true |
| 125 | && delta.changed.length === 1 && delta.version >= 1 && Array.isArray(delta.needs), JSON.stringify(Object.keys(delta))); |
| 126 | const single = aus.compile('Hello, single source.\n'); |
| 127 | check('compile(source)', single.pdf instanceof Uint8Array && single.pages === 1); |
| 128 | |
| 129 | // Fonts. |
| 130 | const fams = aus.fontFamilies(); |
| 131 | check('fontFamilies', Array.isArray(fams) && fams.includes('Libertinus Serif') && fams.includes('New Computer Modern Math') |
| 132 | && fams.join('|') === [...fams].sort((a, b) => a.toLowerCase() < b.toLowerCase() ? -1 : 1).join('|'), fams.join(',')); |
| 133 | |
| 134 | // Packages: supplied, imported, withdrawn, and `needs` names what an import still wants. |
| 135 | const spec = '@local/smoke:0.1.0'; |
| 136 | const files = [ |
| 137 | ['typst.toml', '[package]\nname = "smoke"\nversion = "0.1.0"\nentrypoint = "lib.typ"\n'], |
| 138 | ['lib.typ', new TextEncoder().encode('#let hi(n) = [Hi, #n.]\n')], |
| 139 | ]; |
| 140 | const sup = aus.supplyPackage(spec, files); |
| 141 | check('supplyPackage', sup.files === 2 && sup.spec === spec && aus.packages().includes(spec), JSON.stringify(sup)); |
| 142 | const used = aus.compileProject(project('#import "@local/smoke:0.1.0": hi\n#hi("node")\n')); |
| 143 | check('package import', !!used.pdf && used.needs.length === 0, String(used.error)); |
| 144 | check('withdrawPackage', aus.withdrawPackage(spec).withdrawn === true && !aus.packages().includes(spec)); |
| 145 | const gone = aus.compileProject(project('#import "@local/smoke:0.1.0": hi\n#hi("node")\n')); |
| 146 | check('needs', !!gone.error && gone.needs.length === 1 && gone.needs[0] === spec |
| 147 | && gone.diagnostics[0].kind === 'package', JSON.stringify(gone.needs)); |
| 148 | |
| 149 | // Identity. |
| 150 | const info = aus.engineInfo(); |
| 151 | check('engineInfo', info.engine === 'austenite' && info.typst === '0.15.1' && typeof info.version === 'string', |
| 152 | JSON.stringify(info)); |
| 153 | const want = opt('--git', null); |
| 154 | if (want !== null) { |
| 155 | check('engineInfo git', info.git === want && !info.git.endsWith('-dirty'), `${info.git} is not ${want}`); |
| 156 | } |
| 157 | |
| 158 | // A nest of containers past the layout depth is a `limit` error, never a trap: a trap ends the module for the page's |
| 159 | // life, since the wasm has one linear memory and a call that traps leaves the instance borrowed. After the refusal the |
| 160 | // same instance and a new one compile on. The nests just under the depth Typst sets must still be set, which the |
| 161 | // stack has to hold. |
| 162 | const looped = (kind, n) => `#let x = [a]\n#for i in range(${n}) { x = ${kind}(x) }\n#x\n`; |
| 163 | const nested = (open, close, n) => `${open.repeat(n)}x${close.repeat(n)}\n`; |
| 164 | const listed = (n) => Array.from({ length: n }, (_, i) => `${' '.repeat(i)}- item\n`).join(''); |
| 165 | function attempt(text) { |
| 166 | try { |
| 167 | return { r: aus.compileProject(project(text)) }; |
| 168 | } catch (e) { |
| 169 | return { threw: `${e.constructor.name}: ${String(e.message).slice(0, 80)}` }; |
| 170 | } |
| 171 | } |
| 172 | for (const [kind, text] of [ |
| 173 | ['box', looped('box', 2000)], ['block', looped('block', 2000)], ['stack', looped('stack', 2000)], |
| 174 | ['table', looped('table', 2000)], ['list', listed(100)], |
| 175 | ]) { |
| 176 | const { r, threw } = attempt(text); |
| 177 | const d = r && r.diagnostics && r.diagnostics[0]; |
| 178 | check(`depth ${kind} nested deep is a limit error`, |
| 179 | !threw && typeof r.error === 'string' && d && d.kind === 'limit' && /^maximum (layout|show rule) depth exceeded$/.test(d.message), |
| 180 | threw || String(r && r.error)); |
| 181 | const next = attempt(SKELETON); |
| 182 | check(`depth the same instance compiles after ${kind}`, !next.threw && !!next.r.pdf, next.threw || String(next.r.error)); |
| 183 | } |
| 184 | check('depth a new instance compiles after the nests', (() => { |
| 185 | try { |
| 186 | return !!new mod.DaimondTypst().compileProject(project(SKELETON)).pdf; |
| 187 | } catch (e) { |
| 188 | return false; |
| 189 | } |
| 190 | })()); |
| 191 | for (const [kind, text] of [ |
| 192 | ['box', nested('#box[', ']', 70)], ['block', nested('#block[', ']', 35)], ['stack', nested('#stack[', ']', 32)], |
| 193 | ['list', listed(31)], |
| 194 | ]) { |
| 195 | const { r, threw } = attempt(text); |
| 196 | check(`depth ${kind} just under Typst's depth is set`, !threw && !!r.pdf, threw || String(r && r.error)); |
| 197 | } |
| 198 | |
| 199 | // The case lines against the native ones. |
| 200 | const file = opt('--expect', null); |
| 201 | if (file !== null) { |
| 202 | const expected = readFileSync(file, 'utf8').split('\n').filter((l) => l.length > 0); |
| 203 | check('case lines equal the native lines', JSON.stringify(expected) === JSON.stringify(lines), |
| 204 | `expected ${JSON.stringify(expected)}`); |
| 205 | } |
| 206 | |
| 207 | // The live view's delta (G10, the engine's side). |
| 208 | if (process.argv.includes('--delta')) { |
| 209 | const NAMES = ['alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot']; |
| 210 | const six = (edit) => '#set page(width: 220pt, height: 100pt, margin: 12pt)\n' + NAMES.map((n, k) => |
| 211 | (k > 0 ? '#pagebreak()\n' : '') + `Page ${n} holds the ${edit === k ? 'quarts' : 'quartz'} of paragraph ${k} alone.\n`).join(''); |
| 212 | const hasTsel = (svg) => svg.includes('.tsel { fill: transparent; }') && svg.includes('<text class="tsel">'); |
| 213 | const key = (r) => JSON.stringify(r.order); |
| 214 | // The characters of the selectable layer, spaces dropped. |
| 215 | const layerText = (svg) => [...svg.matchAll(/<tspan[^>]*>([^<]*)<\/tspan>/g)].map((m) => m[1]).join('').replace(/\s+/g, ''); |
| 216 | |
| 217 | const first = aus.compileProjectDelta(project(six(null), { known: [] })); |
| 218 | first.order = first.order || []; |
| 219 | first.changed = first.changed || []; |
| 220 | check('delta order length equals pages', Array.isArray(first.order) && first.pages === 6 |
| 221 | && first.order.length === first.pages && first.reset === true && first.version >= 1, |
| 222 | JSON.stringify({ pages: first.pages, order: first.order && first.order.length, error: first.error })); |
| 223 | check('delta ids are opaque decimal strings', first.order.every((id) => typeof id === 'string' && /^[0-9]+$/.test(id))); |
| 224 | check('delta every changed svg holds the .tsel layer', first.changed.length === new Set(first.order).size |
| 225 | && first.changed.every((c) => first.order.includes(c.id) && hasTsel(c.svg)), |
| 226 | `${first.changed.length} changed`); |
| 227 | |
| 228 | const second = aus.compileProjectDelta(project(six(null), { known: first.order })); |
| 229 | second.order = second.order || []; |
| 230 | second.changed = second.changed || []; |
| 231 | check('delta an unchanged second call sends no svg', second.changed.length === 0 && second.reset === false |
| 232 | && key(second) === key(first) && second.version === first.version + 1, |
| 233 | JSON.stringify({ changed: second.changed.length, reset: second.reset, version: second.version })); |
| 234 | |
| 235 | const edited = aus.compileProjectDelta(project(six(2), { known: first.order })); |
| 236 | edited.order = edited.order || []; |
| 237 | edited.changed = edited.changed || []; |
| 238 | check('delta a one-letter edit sends the one page', edited.order.length === 6 && edited.changed.length === 1 |
| 239 | && edited.changed[0].id === edited.order[2] && edited.order[2] !== first.order[2] |
| 240 | && edited.order.every((id, i) => i === 2 || id === first.order[i]) |
| 241 | && hasTsel(edited.changed[0].svg) && layerText(edited.changed[0].svg).includes('quarts'), |
| 242 | JSON.stringify({ changed: edited.changed.length })); |
| 243 | |
| 244 | const refusals = [ |
| 245 | ['a missing font', '#set text(font: "Nope Sans")\nx\n', 'missing_font'], |
| 246 | ['a body that sets nothing', '#context none\n', 'internal'], |
| 247 | ['an unknown name', '#nonesuch()\n', 'unknown_variable'], |
| 248 | ]; |
| 249 | for (const [what, text, kind] of refusals) { |
| 250 | const r = aus.compileProjectDelta(project(text, { known: first.order })); |
| 251 | check(`delta ${what} is refused`, typeof r.error === 'string' && ((r.diagnostics || [])[0] || {}).kind === kind |
| 252 | && r.version === undefined && Array.isArray(r.needs), String(r.error)); |
| 253 | } |
| 254 | const blank = aus.compileProjectDelta(project('#context []\n', { known: [] })); |
| 255 | const at = (blank.diagnostics || [])[0] || {}; |
| 256 | check('delta a context that gives nothing is refused at 1:1', blank.error !== undefined && at.file === '/main.typ' |
| 257 | && at.line === 1 && at.col === 1, JSON.stringify(blank.diagnostics && blank.diagnostics[0])); |
| 258 | const after = aus.compileProjectDelta(project(six(null), { known: first.order })); |
| 259 | check('delta the refusals left the version where it was', after.version === edited.version + 1, |
| 260 | `${after.version} after ${edited.version}`); |
| 261 | |
| 262 | const raw = (wrap) => `#set page(width: 220pt, height: 60pt, margin: 8pt)\n${wrap('raw("hello world")')}\n`; |
| 263 | const regular = aus.compileProjectDelta(project(raw((r) => '#' + r), { known: [] })); |
| 264 | regular.order = regular.order || []; |
| 265 | regular.changed = regular.changed || []; |
| 266 | const bold = aus.compileProjectDelta(project(raw((r) => `#strong(${r})`), { known: regular.order })); |
| 267 | bold.order = bold.order || []; |
| 268 | bold.changed = bold.changed || []; |
| 269 | check('delta a page that differs only in its face has another id', bold.order[0] !== regular.order[0] |
| 270 | && bold.changed.length === 1 && bold.changed[0].svg !== regular.changed[0].svg); |
| 271 | } |
| 272 | |
| 273 | if (failures.length > 0) { |
| 274 | console.error(`door_smoke: ${failures.length} failed: ${failures.join(', ')}`); |
| 275 | process.exit(1); |
| 276 | } |