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

125 lines
8.0 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// "What this site stores and sends" (SITE_DATA in src/core/site.ts, in the footer) is held to the code here: what the
// shipped files ask for, which other sites they name, what they keep in the browser, and the footer's legal lines.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { GODBOLT_BASE } from '../core/dist/mojo/generated/pin.js';
import { SITE_DATA, SOURCE_URL, LICENSE_FILES, DISCLAIMER, DESCRIPTION, siteVersion } from '../dist/core/site.js';
const ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const read = (p) => fs.readFileSync(path.join(ROOT, p), 'utf8');
const walk = (dir, keep) => fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true }).flatMap((e) => {
const p = path.join(dir, e.name);
return e.isDirectory() ? walk(p, keep) : keep(p) ? [p] : [];
});
/** What scripts/assemble.sh ships: the page, the styles and fonts, dist/, core's dist/ (without the lesson corpus), and the Quarto extension. */
const shipped = () => {
const code = (p) => /\.(m?js|css|html|lua)$/.test(p) && !p.endsWith('.d.ts') && !p.endsWith('.map');
return [
'index.html', 'styles.css',
...walk('styles', code), ...walk('fonts', code), ...walk('dist', code),
...walk('core/dist', code).filter((p) => !p.startsWith('core/dist/curriculum/')),
...walk('quarto/_extensions/pplus', code).filter((p) => !p.includes('/engine/')),
];
};
test('the only request the shipped code makes to another site is Compile & run\'s, to godbolt.org', () => {
const files = shipped();
assert.ok(files.length > 100, `${files.length} files`);
const api = /\bfetch\s*\(|XMLHttpRequest|sendBeacon|new\s+WebSocket|new\s+EventSource|\.importScripts\s*\(/;
const callers = files.filter((f) => api.test(read(f)));
assert.deepEqual(callers, ['core/dist/services/godbolt.js'], 'a new network call must be added to the note on what the site sends');
assert.equal(new URL(GODBOLT_BASE).hostname, 'godbolt.org');
});
test('every other site the shipped files name is a link a reader follows, or not a place at all', () => {
// a new origin fails here until it is listed with what it is; none of them is asked for anything (the CSP's
// connect-src, font-src and script-src allow this site and godbolt.org only: tests/csp.test.mjs)
const allowed = new Map([
['https://godbolt.org', 'Compile & run (the one request, after asking)'],
['https://mojolang.org', 'links to the Mojo manual and release notes'],
['https://quest.mojolang.org', 'a link to Mojo Quest'],
['https://doi.org', 'links to where a library routine\'s method comes from'],
['https://dlmf.nist.gov', 'the same: the NIST Digital Library of Mathematical Functions'],
['https://www.itl.nist.gov', 'the same: the NIST/SEMATECH e-Handbook of Statistical Methods'],
['https://archive.org', 'the same: a scanned handbook'],
['https://biopython.org', 'the same: the documentation of the function a routine is held against'],
['https://biosc1540.scient.ing', 'the "guide" link beside a diagnostic'],
['https://pplus.scient.ing', 'the Quarto extension\'s default address of the app, for its live views'],
['http://www.w3.org', 'the SVG namespace, not a request'],
['https://pplus.invalid', 'the address the small document of src/quarto/dom.ts pretends to have'],
]);
if (SOURCE_URL) allowed.set(new URL(SOURCE_URL).origin, 'the footer\'s link to the source');
const seen = new Map();
for (const f of shipped()) for (const m of read(f).matchAll(/\bhttps?:\/\/[a-z0-9.-]+\.[a-z]{2,}(?::\d+)?/gi)) {
const o = new URL(m[0]).origin;
if (!seen.has(o)) seen.set(o, f);
}
const unknown = [...seen].filter(([o]) => !allowed.has(o)).map(([o, f]) => `${o} (${f})`);
assert.deepEqual(unknown, [], 'say what each is in this test, and in SITE_DATA if the site asks it for anything');
assert.ok(seen.has(new URL(GODBOLT_BASE).origin));
});
test('the site keeps nothing but localStorage and one sessionStorage mark, under names that start with pplus', () => {
const src = walk('src', (p) => p.endsWith('.ts') && !p.includes('/vendor/')).map(read).join('\n');
for (const api of ['document.cookie', 'indexedDB', 'caches.open', 'serviceWorker', 'openDatabase']) assert.ok(!src.includes(api), `src uses ${api}`);
const shippedCode = shipped().filter((f) => f.endsWith('.js')).map(read).join('\n');
assert.ok(!/document\.cookie|indexedDB\.open|navigator\.serviceWorker/.test(shippedCode), 'nor do the shipped modules');
// every key a storage call names starts with pplus. (keys built by a function are checked where they are built)
const literal = [...src.matchAll(/(?:local|session)Storage\.(?:getItem|setItem|removeItem)\(\s*(['"`])([^'"`$]*)/g)].map((m) => m[2]);
assert.ok(literal.length > 3);
for (const k of literal) assert.ok(/^pplus[.:]/.test(k) || k.startsWith('progo.'), `storage key ${k}`);
// the keys built from an id (a problem's save, its versions, a preference per area) are template strings
const built = [...src.matchAll(/`(pplus[.:][^`$]*)\$\{/g)].map((m) => m[1]);
assert.ok(built.length >= 4, built.join(' '));
for (const k of built) assert.ok(/^pplus[.:]/.test(k), `storage key ${k}…`);
assert.match(read('src/core/guard.ts'), /sessionStorage/, 'the one sessionStorage mark (SITE_DATA says so)');
assert.match(SITE_DATA.join(' '), /localStorage/);
assert.match(SITE_DATA.join(' '), /sessionStorage/);
assert.match(SITE_DATA.join(' '), /under names that start with "pplus"/);
});
test('the note says what Compile & run sends, that it asks first, and that a shared link never saves over your work', () => {
const text = SITE_DATA.join(' ');
assert.match(text, /No accounts, no cookies, no analytics/);
assert.match(text, /Compile & run sends the Mojo pane's text, the input box's lines and the build flags to godbolt\.org/);
assert.match(text, /The first time, it says so and asks; nothing is sent until you agree/);
assert.match(text, /never saves over your own work/);
// what the request carries is mojoRequestBody's: the source, the input and the flags (core's src/services/godbolt.ts)
const body = read('core/src/services/godbolt.ts');
assert.match(body, /export function mojoRequestBody\(source: string, stdin = '', args = ''\)/);
// held elsewhere: tests/hardening.test.mjs "the first Compile & run asks before anything is sent" and
// "a shared link never saves over the reader's own work on the problem it names"
const hardening = read('tests/hardening.test.mjs');
assert.match(hardening, /the first Compile & run asks before anything is sent/);
assert.match(hardening, /a shared link never saves over the reader\\'s own work/);
});
test('the footer links the license and notices the site ships, and repeats NOTICE.md\'s disclaimer', () => {
const assemble = read('scripts/assemble.sh');
for (const f of LICENSE_FILES) {
const file = f.href.replace('./', '');
assert.ok(fs.existsSync(path.join(ROOT, file)), file);
assert.match(assemble, new RegExp(`\\b${file.replace('.', '\\.')}\\b`), `assemble.sh copies ${file}`);
}
assert.match(read('LICENSE.md'), /Apache License/);
const notice = read('NOTICE.md').replace(/\s+/g, ' ');
assert.ok(notice.includes('Programming+ is not affiliated with, sponsored by, or endorsed by Modular Inc.'));
assert.match(DISCLAIMER, /^Programming\+ is not affiliated with, sponsored by, or endorsed by Modular Inc\./);
assert.equal(typeof SOURCE_URL, 'string', 'the source address: one constant, empty until the owner sets it');
assert.ok(DESCRIPTION.length < 120);
});
test('the build\'s version is shown only when the assembled page names a commit', () => {
assert.equal(siteVersion('0123456789abcdef0123456789abcdef01234567'), '0123456789ab');
assert.equal(siteVersion('dev'), null);
assert.equal(siteVersion(''), null);
assert.equal(siteVersion(null), null);
assert.equal(siteVersion('<script>'), null);
});