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

168 lines
8.3 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// Stage 12: the Mojo badges. The generated label (where the Mojo came from) and the godbolt badge (whether the real
// compiler was asked about exactly this source) are independent, both are data a view reads from the session, and
// neither can come from a link or a file.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Session } from '../dist/core/session.js';
import { stateForProblem, problemById, PROBLEMS } from '../dist/core/catalog.js';
import { mojoProvenance } from '../dist/core/provenance.js';
import { compiledStatus, compileHash, forgetCompiles } from '../dist/core/compiles.js';
import { normalize } from '../dist/core/state.js';
import { GENERATED_HEADER } from '../core/dist/sync/header.js';
import { mojoFromPseudo } from '../core/dist/sync/hub.js';
const store = new Map();
globalThis.localStorage = { getItem: (k) => store.get(k) ?? null, setItem: (k, v) => store.set(k, String(v)), removeItem: (k) => store.delete(k) };
const open = (id, area = 'bridge') => new Session(stateForProblem(problemById(id)), { area, storageId: null });
const answer = (over = {}) => async () => ({ error: null, seconds: 1, answer: { compiled: true, buildStderr: '', executed: true, exitCode: 0, stdout: '1\n', stderr: '', timedOut: false, asm: [], ...over } });
test('the Bridge\'s Mojo is always generated; elsewhere the state says', () => {
const b = open('count-base', 'bridge');
assert.equal(b.provenance(), 'generated');
assert.equal(mojoProvenance(b, 'bridge'), 'generated');
assert.equal(open('count-base', 'dev').provenance(), null, 'an exercise\'s own Mojo is nobody\'s translation');
assert.equal(open('count-base', 'hw').provenance(), null);
assert.equal(mojoProvenance({ area: 'dev', state: { mojoOrigin: 'generated-edited' } }), 'generated-edited');
});
test('copying sets generated, editing sets generated-edited, and only the button clears it', async () => {
const s = open('count-base');
assert.ok(s.adoptGeneratedMojo());
assert.equal(s.state.mojoOrigin, 'generated');
const dev = new Session(s.state, { area: 'dev', storageId: null });
assert.equal(dev.provenance(), 'generated');
dev.setSource('mojo', dev.source('mojo') + '\n# my note');
assert.equal(dev.provenance(), 'generated-edited');
forgetCompiles();
await dev.compile(answer());
assert.equal(dev.provenance(), 'generated-edited', 'a successful compile does not clear it');
assert.ok(dev.clearMojoOrigin());
assert.equal(dev.provenance(), null);
assert.equal(dev.state.mojoOrigin, undefined);
assert.ok(!dev.source('mojo').includes(GENERATED_HEADER), 'the header comment goes with it');
assert.ok(dev.source('mojo').includes('# my note'), 'the student\'s own lines stay');
assert.equal(dev.clearMojoOrigin(), false, 'nothing left to clear');
});
test('the Bridge cannot clear provenance, and a translation always starts with the header', () => {
const s = open('count-base');
assert.equal(s.clearMojoOrigin(), false);
let n = 0;
for (const p of PROBLEMS) {
const pseudo = stateForProblem(p).sources.pseudo;
if (!pseudo) continue;
const m = mojoFromPseudo(pseudo, null).source;
if (m) { n++; assert.ok(m.startsWith(GENERATED_HEADER), p.id); }
}
assert.ok(n > 5);
});
test('the godbolt badge: shown for the compiled source, not for one more character, level or input', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
assert.equal(s.compileBadge(), null, 'never compiled: no badge');
await s.compile(answer());
const b = s.compileBadge();
assert.equal(b.kind, 'compiled');
assert.equal(b.status.outcome, 'ran');
assert.equal(b.status.exit, 0);
const original = s.source('mojo');
s.setSource('mojo', original + ' ');
assert.equal(s.compileBadge().kind, 'edited', 'a one-character edit removes the badge');
s.setSource('mojo', original);
assert.equal(s.compileBadge().kind, 'compiled', 'the same text again is the same compile');
const back = open('count-base', 'dev');
assert.equal(back.compileBadge().kind, 'compiled', 'the same text again is the same compile');
back.setBuildLevel('default');
assert.equal(back.compileBadge(), null, 'another build level was never asked');
back.setBuildLevel('O0');
back.setInput('3\n');
assert.equal(back.compileBadge(), null, 'another input was never asked');
});
test('outcomes: did not compile, exit N, timed out', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(answer({ compiled: false, executed: false, exitCode: null }));
assert.equal(s.compileBadge().status.outcome, 'did-not-compile');
forgetCompiles();
await s.compile(answer({ exitCode: 3 }));
assert.deepEqual([s.compileBadge().status.outcome, s.compileBadge().status.exit], ['ran', 3]);
forgetCompiles();
await s.compile(answer({ timedOut: true }));
assert.equal(s.compileBadge().status.outcome, 'timed-out');
});
test('a failed request (no answer) leaves no badge', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(async () => ({ answer: null, error: 'offline', seconds: 1 }));
assert.equal(s.compileBadge(), null);
});
test('the badge survives a new Session and the full answers being dropped, but is not in the state, a link or a file', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(answer());
const src = s.source('mojo');
assert.equal(compiledStatus(src, '', 'O0').outcome, 'ran');
assert.equal(compiledStatus(src, '', 'O0').level, 'O0');
assert.ok(store.has('pplus.compiled'));
store.delete('pplus.compiles'); // the full answers are gone; the summary index stays
assert.equal(open('count-base', 'dev').compileBadge().kind, 'compiled');
const text = JSON.stringify(s.state);
assert.ok(!/compiled|godbolt|pplus\./i.test(text.replace(/"compiler"/g, '')), 'nothing about a compile in the state');
forgetCompiles(); // someone else's browser: the same link, no compile
const theirs = new Session(normalize(JSON.parse(text)), { area: 'dev', storageId: null });
assert.equal(theirs.compileBadge(), null);
});
test('both badges can show together, and the hash covers source, input and level', async () => {
forgetCompiles();
const s = open('count-base', 'bridge');
await s.compile(answer());
assert.equal(s.provenance(), 'generated');
assert.equal(s.compileBadge().kind, 'compiled');
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a', '', 'default'));
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a', 'x', 'O0'));
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a ', '', 'O0'));
});
test('Compile & run on unchanged work shows the kept answer and sends nothing; asking again is a choice', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
assert.equal(s.keptCompile(), null, 'nothing kept before the first answer');
let sent = 0;
const fake = async () => { sent++; return { error: null, seconds: 1, answer: { compiled: true, buildStderr: '', executed: true, exitCode: 0, stdout: `${sent}\n`, stderr: '', timedOut: false, asm: [] } }; };
const first = await s.compile(fake);
assert.equal(sent, 1);
assert.equal(s.keptCompile(), first, 'the same source, input and level: the kept answer');
assert.equal(s.reusedCompile(), null);
s.reuseCompile(s.keptCompile());
assert.equal(sent, 1, 'nothing sent');
assert.equal(s.reusedCompile(), first, 'the view can say the answer was kept, not asked');
assert.equal(s.lastCompile().record, first);
// another level, another input, one more character: nothing kept for them
assert.equal(s.keptCompile('default'), null);
s.setInput('7');
assert.equal(s.keptCompile(), null);
s.setInput('');
assert.equal(s.keptCompile(), first);
const src = s.source('mojo');
s.setSource('mojo', src + ' ');
assert.equal(s.keptCompile(), null);
s.setSource('mojo', src);
// asking again, on purpose, sends and is no longer "kept"
const again = await s.compile(fake);
assert.equal(sent, 2);
assert.equal(s.reusedCompile(), null);
assert.equal(s.keptCompile(), again);
// a request that failed is not an answer to show again
forgetCompiles();
const t = open('match', 'dev');
await t.compile(async () => ({ answer: null, error: 'offline', seconds: 0 }));
assert.equal(t.keptCompile(), null);
});