Files
pplus-web/tests/load.test.mjs
T
alexm 5202c96318 The home page shows at once
It was blank until every module had loaded and the page's examples had
been run, a fuzzer's report among them (about a second of work): first
paint came 1.3 seconds into a repeat visit and about 3 into a first one.

- The assembler draws the home page once, in Node, with the app's own
  homeView, and puts it inside #app. The browser paints it with the
  HTML and the stylesheet.
- When the app has loaded it draws the page again, with its controls,
  and puts it in the prerendered one's place; the fuzzer's reports and
  the proof's counts are copied from the prerendered page, not worked
  out again.
- The prerendered page shows only on the home address: the page's first
  inline script marks it before the first paint, and without the mark it
  stays hidden, so an embed or a problem's address never shows it. That
  script changed, and its hash in docs/Caddyfile with it.

Served locally, first paint is at 0.1 to 0.2 seconds and the page with
its controls at 0.2 to 0.3; both were at 1.2 to 1.4.
2026-11-13 07:28:38 -05:00

103 lines
7.4 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, prerenderHome, HOME_MARK } 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)');
});
test('the assembled page holds the home page already drawn, shown only on the home address', async () => {
// the app's own homeView, drawn in Node: what a visitor sees before any module has loaded
const home = await prerenderHome(path.join(ROOT, 'dist'), '0123456789abcdef0123456789abcdef01234567');
assert.match(home, /^<div class="home home-page">/);
assert.match(home, /<h1[ >]/);
assert.match(home, /id="problems"/);
// no script that runs (the CSP allows inline scripts by hash only); the Quarto example's stepper carries its steps as a JSON data block
assert.doesNotMatch(home, /<script(?![^>]*\btype="application\/json")/i);
assert.match(home, /0123456789ab/, 'the footer names the commit');
// the two parts that take the time, marked so the browser copies them instead of running the fuzzer and the prover again
assert.match(home, /data-kept="tests-card"[^>]*>.*no failures.*way/s);
assert.match(home, /data-kept="proof-counts"[^>]*>.*[1-9]\d* proved.*0 broken.*0 open/s);
// it goes inside #app, where the app puts the page it draws, and changes no inline script
assert.ok(html.includes(HOME_MARK), 'index.html has the place for it');
const out = assemblePage(html, { version: '0123456789abcdef0123456789abcdef01234567', preloads: ['./dist/a.js'], home: 'a $& b <p>home</p>' });
assert.match(out, /<div id="app">[\s\S]*<div class="home-prerendered">a \$& b <p>home<\/p><\/div>\s*<\/div>\s*<!-- pplus-core's modules/);
assert.deepEqual(inlineHashes(out), inlineHashes(html));
assert.throws(() => assemblePage(html.replace(HOME_MARK, ''), { home: 'x' }), /prerendered home page goes there/);
// shown only where the page's first script marked the home address: hidden by default, so an embed, a problem's
// address, or a page whose inline script did not run never shows the home page first
const css = read('styles.css');
assert.match(css, /\n\.home-prerendered \{ display: none; \}/);
assert.match(css, /\nhtml\[data-home\] \.home-prerendered \{ display: block; \}/);
const mark = /if \((\/.*?\/)\.test\(location\.hash\)\) document\.documentElement\.setAttribute\("data-home", ""\)/.exec(html);
assert.ok(mark, 'the inline script that marks the home address is not where this test looks');
const isHome = new Function(`return ${mark[1]}`)();
const { parseRoute, isUnknownRoute } = await import('../dist/core/state.js');
for (const hash of ['', '#', '#/', '#/#problems', '#/#reference', '#/#quarto-teaser']) {
assert.ok(isHome.test(hash), `${JSON.stringify(hash)} is the home page`);
assert.equal(parseRoute(hash).kind, 'home', hash);
assert.ok(!isUnknownRoute(hash), hash);
}
for (const hash of ['#/bridge/p/count-base', '#/dev/p/demo', '#/hw/p/asm-first-instructions', '#/library', '#/quarto', '#/quarto#install', '#/embed/trace/p/count-base', '#/bridge/s/abc', '#/p/count-base']) {
assert.ok(!isHome.test(hash), `${JSON.stringify(hash)} is not the home page`);
}
// and the app takes the prerendered page's place only once the page with its controls is built
const main = read('src/main.ts');
assert.match(main, /const home = homeView\(\);\s*if \(prerendered\) clear\(app\);\s*add\(app, home\)/);
});