Files
pplus-web/tests/load.test.mjs
T
alexm e092cb029c
Build, test, deploy / site (push) Failing after 15m13s
Initial commit
2026-11-12 22:24:02 -05:00

63 lines
4.2 KiB
JavaScript

// 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(/<script(?![^>]*\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, /<meta name="pplus-version" content="0123456789abcdef0123456789abcdef01234567">\n<\/head>/);
const map = out.indexOf('<script type="importmap">');
const first = out.indexOf('rel="modulepreload"');
assert.ok(map > 0 && first > map, 'a module preload before the import map would make the browser ignore the map');
assert.ok(first < out.indexOf('<script type="module"'));
assert.deepEqual(inlineHashes(out), inlineHashes(html), 'the CSP allows the inline scripts by their hashes');
assert.doesNotMatch(assemblePage(html, { version: 'dev' }), /pplus-version/, 'a checkout without a commit names none');
assert.match(read('scripts/assemble.sh'), /node scripts\/assemble-page\.mjs site/);
});
test('the page asks for its two everyday faces with the stylesheet, and every face swaps in when it arrives', () => {
const pre = [...html.matchAll(/<link rel="preload" href="\.\/fonts\/([^"]+)" as="font" type="font\/woff2" crossorigin>/g)].map((m) => m[1]);
assert.deepEqual(pre, ['IBMPlexSans-Regular.woff2', 'IBMPlexMono-Regular.woff2']);
const css = read('fonts/plex.css');
const faces = css.match(/@font-face/g).length;
assert.equal(css.match(/font-display: swap/g).length, faces);
});
test('the Caddy block caches nothing as immutable, revalidates the page and the code, and keeps the fonts', () => {
const caddy = read('docs/Caddyfile');
assert.doesNotMatch(caddy, /Cache-Control "[^"]*immutable/, 'no file name here changes with its content');
assert.match(caddy, /@page path \/ \/index\.html\n\s*header @page Cache-Control "no-cache"/);
assert.match(caddy, /@code path \*\.js \*\.css\n\s*header @code Cache-Control "no-cache"/);
assert.match(caddy, /@fonts path \/fonts\/\*\.woff2\n\s*header @fonts Cache-Control "public, max-age=\d+"/);
assert.match(caddy, /header \/VERSION Cache-Control "no-store"/);
assert.doesNotMatch(read('src/main.ts') + html, /serviceWorker/, 'no service worker (offline reload is a later decision)');
});