// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // What a first visit asks for, and what a browser may keep (docs/deploy.md): the assembled page preloads every module // its imports reach and names its commit (scripts/assemble-page.mjs), and the Caddy block caches only what may be cached. import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { createHash } from 'node:crypto'; import { fileURLToPath } from 'node:url'; import { modulePreloads, assemblePage } from '../scripts/assemble-page.mjs'; const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url))); const read = (p) => fs.readFileSync(path.join(ROOT, p), 'utf8'); const html = read('index.html'); const inlineHashes = (h) => [...h.matchAll(/]*\bsrc=)[^>]*>([\s\S]*?)<\/script>/g)].map((m) => createHash('sha256').update(m[1]).digest('base64')); test('the preload list is every module the page imports, each once, and only files that exist', () => { const list = modulePreloads(ROOT); assert.ok(list.length > 100, `${list.length} modules`); assert.equal(new Set(list).size, list.length); assert.ok(!list.includes('./dist/main.js'), 'the entry loads itself'); for (const href of ['./dist/ui/shell.js', './dist/vendor/codemirror.js', './core/dist/mojo/interp.js', './dist/ui/footer.js']) assert.ok(list.includes(href), href); for (const href of list) assert.ok(fs.existsSync(path.join(ROOT, href)), href); // every import in every listed module is listed too (or is the entry) const listed = new Set(list.map((h) => path.join(ROOT, h))); listed.add(path.join(ROOT, 'dist/main.js')); for (const f of listed) for (const m of fs.readFileSync(f, 'utf8').matchAll(/\bfrom\s*['"](\.[^'"]+|#core\/[^'"]+)['"]/g)) { const t = m[1].startsWith('#core/') ? path.join(ROOT, 'core/dist', m[1].slice(6)) : path.resolve(path.dirname(f), m[1]); assert.ok(listed.has(t), `${path.relative(ROOT, f)} imports ${m[1]}, which the list misses`); } }); test('the assembled page adds the version and the preloads after the import map, and changes no inline script', () => { const out = assemblePage(html, { version: '0123456789abcdef0123456789abcdef01234567', preloads: ['./dist/a.js', './core/dist/b.js'] }); assert.match(out, /\n<\/head>/); const map = out.indexOf('