Build, test, deploy / site (push) Successful in 7m44s
The Prove view gives each routine 3 seconds on the clock. On a slow or busy device that runs out before the proof does, and claims stay open with nothing to say why. - The view says when the time, not the proof, stopped the checker, that what was not finished is open and not disproved, and offers to prove again with the next longer time. The setting is at the foot of the view: 3, 10 or 30 seconds or 2 minutes for each routine, kept in the browser (pplus.proveclock). - A proof rendered with Quarto takes prove-seconds on the shortcode or pplus-prove-seconds for the site, and the render warns when the time ran out. - The home page's own example is proved with no time on the clock, and the tests that hold a proof's outcome give it time to spare, so neither depends on the machine. - core moves to 38e6e2c, whose report says which routines the clock stopped.
155 lines
8.7 KiB
JavaScript
155 lines
8.7 KiB
JavaScript
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// Stage 10 (and the part of stage 11 it needs): the Bridge's session behavior, without a DOM. In the Bridge the
|
|
// Mojo pane is the literal translation of the pseudo, computed and never stored; the views that read a program
|
|
// (trace, checker, tests, proof) work on the hub's pseudo; a pane that is not in step is not stepped; "Edit in
|
|
// Developer" copies the generated Mojo out and records that it was generated.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { Session } from '../dist/core/session.js';
|
|
import { PROBLEMS, stateForProblem, problemById } from '../dist/core/catalog.js';
|
|
import { SyncHub } from '../core/dist/sync/hub.js';
|
|
import { normalize, editedFrom } from '../dist/core/state.js';
|
|
import { GENERATED_HEADER } from '../core/dist/sync/header.js';
|
|
|
|
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
|
|
const open = (id, area = 'bridge') => new Session(stateForProblem(problemById(id)), { area, storageId: null });
|
|
|
|
test('in the Bridge the Mojo pane is the generated Mojo, not the stored Mojo; elsewhere it is the state\'s own', () => {
|
|
const own = stateForProblem(problemById('count-base')).sources.mojo;
|
|
assert.ok(own && !own.startsWith('# Generated'), 'the exercise has its own Mojo');
|
|
const bridge = open('count-base', 'bridge');
|
|
assert.ok(bridge.source('mojo').startsWith(GENERATED_HEADER), 'the Bridge shows the literal translation, header first');
|
|
assert.equal(bridge.source('mojo'), bridge.derivedMojo().source);
|
|
assert.equal(bridge.state.sources.mojo, own, 'the stored Mojo is untouched');
|
|
assert.equal(open('count-base', 'dev').source('mojo'), own);
|
|
assert.equal(open('count-base', 'hw').source('mojo'), own);
|
|
assert.equal(new Session(stateForProblem(problemById('count-base')), { storageId: null }).source('mojo'), own, 'a session with no area (an embed of one pane) shows the state\'s own Mojo');
|
|
});
|
|
|
|
test('nothing given to the Bridge\'s Mojo pane is stored: opening a problem there leaves the exercise\'s own Mojo', () => {
|
|
// the workspace fills every pane's editor as it opens; the generated Mojo, loaded into the Bridge's read-only
|
|
// pane, had been reported as typing and saved over the exercise's program, which Developer then called "your edits"
|
|
const own = stateForProblem(problemById('count-base')).sources.mojo;
|
|
const s = open('count-base', 'bridge');
|
|
let told = 0;
|
|
s.onChange(() => told++);
|
|
s.setSource('mojo', s.source('mojo'));
|
|
s.setSource('mojo', 'def main():\n pass\n');
|
|
assert.equal(s.state.sources.mojo, own, 'the stored Mojo is still the exercise\'s');
|
|
assert.equal(s.state.mojoOrigin, undefined);
|
|
assert.equal(told, 0, 'and nothing was announced as a change');
|
|
assert.equal(editedFrom(s.state, stateForProblem(problemById('count-base'))), false, 'so the problem does not count as edited');
|
|
// elsewhere the Mojo pane is the student's
|
|
const d = open('count-base', 'dev');
|
|
d.setSource('mojo', 'def main():\n pass\n');
|
|
assert.equal(d.state.sources.mojo, 'def main():\n pass\n');
|
|
});
|
|
|
|
test('the Bridge\'s Mojo follows an edit in any linked pane, and every view of it reads the generated source', () => {
|
|
const s = open('count-base');
|
|
const before = s.source('mojo');
|
|
s.setSource('pseudo', s.source('pseudo').replace('Total <- 0', 'Total <- 3'));
|
|
assert.notEqual(s.source('mojo'), before);
|
|
assert.match(s.source('mojo'), /total = 3/);
|
|
// the checker, the stepper and the tests read it
|
|
s.setLang('mojo');
|
|
assert.equal(s.parsedMojo().program.source ?? s.source('mojo'), s.source('mojo'));
|
|
assert.deepEqual(s.parsedMojo().diagnostics.filter((d) => d.severity === 'error'), []);
|
|
const tr = s.trace();
|
|
assert.ok(tr && !tr.error, 'the generated Mojo steps in the interpreter');
|
|
assert.ok(tr.output.some((l) => /3|4|5|6|7|8|9/.test(l)), 'and prints a count that includes the changed start');
|
|
assert.ok(s.tests().length >= 0);
|
|
// an edit in lingo reaches it too
|
|
const lingo = s.source('lingo');
|
|
assert.match(lingo, /Start with Total at 3\./, 'the English was regenerated from the pseudo');
|
|
s.setSource('lingo', lingo.replace('Start with Total at 3.', 'Start with Total at 10.'));
|
|
assert.match(s.source('mojo'), /total = 10/);
|
|
});
|
|
|
|
test('trace steps the pseudo, not a stale pane', () => {
|
|
const s = open('count-base');
|
|
s.setLang('lingo');
|
|
assert.equal(s.traceLang(), 'lingo', 'an in-step lingo pane is stepped as English');
|
|
s.setSource('pseudo', 'FUNCTION BROKEN(\nRETURN'); // pseudo has errors: lingo is now stale
|
|
assert.equal(s.linkState('lingo').kind, 'stale');
|
|
assert.equal(s.traceLang(), 'pseudo', 'lingo holds text the pseudo has moved on from');
|
|
});
|
|
|
|
test('prove and tests work on the hub\'s pseudo, with the speco woven', () => {
|
|
const s = open('count-base-proved');
|
|
s.setLang('lingo');
|
|
s.setSource('lingo', s.source('lingo').replace('Add 1 to Total.', 'Add 1 to Total.')); // no change: nothing to do
|
|
const lingo = s.source('lingo');
|
|
s.setSource('lingo', lingo + '\n'); // an edit in English
|
|
assert.equal(s.state.sync.authority, 'lingo');
|
|
assert.match(s.source('pseudo'), /ENSURE/, 'the contracts are in the pseudo the English lowered to');
|
|
s.proofBudgetMs = Infinity; // no time on the clock: the same answer on a slow machine
|
|
const proof = s.proof();
|
|
assert.ok(proof && proof.report.counts.total > 0, 'there is something to prove');
|
|
assert.equal(proof.report.counts.broken, 0);
|
|
assert.ok(s.tests().length > 0, 'tests are generated from the pseudo');
|
|
});
|
|
|
|
test('Edit in Developer copies the generated Mojo into the state and records that it was generated', () => {
|
|
const s = open('count-base');
|
|
const generated = s.derivedMojo().source;
|
|
assert.ok(s.wouldReplaceMojo(), 'the exercise\'s own Mojo would be replaced: the shell asks first');
|
|
assert.equal(s.state.mojoOrigin, undefined);
|
|
assert.ok(s.adoptGeneratedMojo());
|
|
assert.equal(s.state.sources.mojo, generated);
|
|
assert.equal(s.state.mojoOrigin, 'generated');
|
|
assert.equal(s.state.lang, 'mojo');
|
|
assert.equal(s.wouldReplaceMojo(), false, 'it already holds this program');
|
|
// the Developer area shows it, and the first edit makes it theirs
|
|
const dev = new Session(s.state, { area: 'dev', storageId: null });
|
|
assert.equal(dev.source('mojo'), generated);
|
|
dev.setSource('mojo', generated + '\n# mine\n');
|
|
assert.equal(dev.state.mojoOrigin, 'generated-edited');
|
|
dev.setSource('mojo', generated + '\n# mine again\n');
|
|
assert.equal(dev.state.mojoOrigin, 'generated-edited');
|
|
// a state with no pseudo has nothing to adopt
|
|
const empty = new Session(normalize({ v: 3 }), { area: 'bridge', storageId: null });
|
|
assert.equal(empty.adoptGeneratedMojo(), false);
|
|
assert.equal(empty.state.mojoOrigin, undefined);
|
|
});
|
|
|
|
test('mojoOrigin is validated, and is only ever set on Mojo the student did not write', () => {
|
|
assert.equal(normalize({ v: 3, mojoOrigin: 'generated' }).mojoOrigin, 'generated');
|
|
assert.equal(normalize({ v: 3, mojoOrigin: 'generated-edited' }).mojoOrigin, 'generated-edited');
|
|
assert.equal(normalize({ v: 3, mojoOrigin: 'hand-written' }).mojoOrigin, undefined);
|
|
const s = open('count-base', 'dev');
|
|
s.setSource('mojo', s.source('mojo') + '\n# note\n');
|
|
assert.equal(s.state.mojoOrigin, undefined, 'editing the exercise\'s own Mojo does not make it generated');
|
|
});
|
|
|
|
test('line maps reach the Mojo: a pseudo line finds its Mojo lines and back', () => {
|
|
const s = open('count-base');
|
|
const maps = s.lineMaps();
|
|
const pseudo = s.source('pseudo').split('\n');
|
|
const at = pseudo.findIndex((l) => /Total <- Total \+ 1/.test(l)) + 1;
|
|
const mojoLines = SyncHub.relate(maps, 'pseudo', at, 'mojo');
|
|
assert.ok(mojoLines.length, 'the line has a Mojo line');
|
|
assert.match(s.source('mojo').split('\n')[mojoLines[0] - 1], /total = total \+ 1/);
|
|
assert.ok(SyncHub.relate(maps, 'mojo', mojoLines[0], 'pseudo').includes(at));
|
|
// lingo line -> pseudo line, for a problem whose lingo was authored
|
|
const lingo = s.source('lingo').split('\n');
|
|
const l = lingo.findIndex((t) => /add 1 to Count/.test(t)) + 1;
|
|
const p = SyncHub.relate(maps, 'lingo', l, 'pseudo');
|
|
assert.ok(Array.isArray(p));
|
|
});
|
|
|
|
test('every Bridge problem opens with generated Mojo that names itself and checks clean', () => {
|
|
let n = 0;
|
|
for (const p of PROBLEMS) {
|
|
if (p.area !== 'bridge') continue;
|
|
const s = new Session(stateForProblem(p), { area: 'bridge', storageId: null });
|
|
if (!s.source('pseudo').trim()) continue;
|
|
const m = s.source('mojo');
|
|
if (!m) continue; // pseudo that does not read (five-bugs, by design)
|
|
assert.ok(m.startsWith(GENERATED_HEADER), p.id);
|
|
n++;
|
|
}
|
|
assert.ok(n >= 12, `${n} problems`);
|
|
});
|