diff --git a/frontend/shell-consistency.test.js b/frontend/shell-consistency.test.js new file mode 100644 index 0000000..752875f --- /dev/null +++ b/frontend/shell-consistency.test.js @@ -0,0 +1,29 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { readFileSync, existsSync } = require('node:fs'); +const { join } = require('node:path'); + +const root = join(__dirname, '..'); +const html = readFileSync(join(__dirname, 'index.html'), 'utf8'); +const sw = readFileSync(join(__dirname, 'sw.js'), 'utf8'); +const shellMatch = sw.match(/const SHELL = \[([\s\S]*?)\n\];/); +assert.ok(shellMatch, 'service worker SHELL list is present'); +const shell = [...shellMatch[1].matchAll(/['"](\/[^'"]+)['"]/g)].map((match) => match[1]); +const listed = new Set(shell); + +test('every local script and stylesheet in index.html is precached by the service worker', () => { + const localAssets = []; + for (const match of html.matchAll(/<(?:script\b[^>]*\bsrc|link\b[^>]*\bhref)="([^"]+)"/gi)) { + const url = match[1]; + if (/^(?:[a-z]+:|\/|#)/i.test(url)) continue; + if (!/\.(?:js|css)(?:[?#]|$)/i.test(url)) continue; + localAssets.push(`/${url.split(/[?#]/, 1)[0]}`); + } + assert.ok(localAssets.length > 0, 'found local script/style assets'); + for (const asset of localAssets) assert.ok(listed.has(asset), `${asset} appears in SHELL`); +}); + +test('every SHELL asset exists on disk and there are no duplicate entries', () => { + assert.equal(new Set(shell).size, shell.length, 'SHELL has no duplicate entries'); + for (const asset of shell) assert.ok(existsSync(join(__dirname, asset.slice(1))), `${asset} exists`); +});