// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // Stage 8: the sync hub (restructure plan, section 2.3), without any UI. Pseudo is the hub; the pane that is // edited is the authority and is never rewritten; an authority with errors changes nothing; a language that // cannot say the program goes stale rather than blank; contracts ride along; opening rewrites nothing; and the // whole cascade is fast enough that typing stays instant. import test from 'node:test'; import assert from 'node:assert/strict'; import { SyncHub, LINKED, namesFromMatho } from '../dist/sync/hub.js'; import { pseudoToLingo } from '../dist/sync/pseudo-to-lingo.js'; import { checkMojo } from '../dist/mojo/check.js'; import { studyOf } from '../dist/services/pseudo.js'; import { EQUATIONS, FIXTURES } from './helpers/sync-programs.mjs'; import { Workspace } from '../dist/common/workspace.js'; import { PROBLEMS, problemById, workForProblem } from '../dist/common/problems.js'; const sync = (r) => ({ authority: r.authority, stale: r.stale, names: r.names }); const next = (r, lang, text) => new SyncHub(r.sources, sync(r)).edit(lang, text); const COUNT = `FUNCTION COUNT_BASE(B : CHAR, T : STRING) RETURNS INTEGER DECLARE Total : INTEGER Total <- 0 FOR EACH C IN T IF C == B THEN Total <- Total + 1 ENDIF NEXT C RETURN Total ENDFUNCTION`; const SIMPLE = `FUNCTION SUMMED(N : INTEGER, X : LIST OF FLOAT) RETURNS FLOAT DECLARE S : FLOAT S <- 0.0 FOR I <- 0 TO N S <- S + X[I] NEXT I RETURN S ENDFUNCTION`; const WHILE_PSEUDO = FIXTURES['fixture:div-mod'][0]; test('a lingo edit updates pseudo, matho and Mojo, and does not rewrite the lingo', () => { const mean = new SyncHub({}).edit('matho', EQUATIONS['mean'][0]); assert.ok(mean.sources.lingo.includes('Start with Total'), 'the equations also became English'); const lingo = mean.sources.lingo.replace('The answer is 1 / N * Total.', 'The answer is 2 / N * Total.'); const r = next(mean, 'lingo', lingo); assert.equal(r.authority, 'lingo'); assert.equal(r.sources.lingo, lingo, 'the edited pane is exactly what was typed'); assert.match(r.sources.pseudo, /RETURN 2 \/ N \* Total/); assert.match(r.sources.matho, /\\frac\{2\}\{n\}|2/, 'matho follows'); assert.match(r.sources.matho, /\\sum/); assert.match(r.mojo.source, /Float64\(2\) \/ Float64\(n\) \* total/); assert.deepEqual(r.stale, {}); assert.ok(r.changed.includes('pseudo') && r.changed.includes('matho') && !r.changed.includes('lingo')); }); test('a pseudo edit updates lingo', () => { const r0 = new SyncHub({}).edit('pseudo', SIMPLE); assert.match(r0.sources.lingo, /Start with S at 0\.0/); const r = next(r0, 'pseudo', SIMPLE.replace('S <- 0.0', 'S <- 2.0')); assert.match(r.sources.lingo, /Start with S at 2\.0/); assert.equal(r.sources.pseudo, SIMPLE.replace('S <- 0.0', 'S <- 2.0')); assert.equal(r.authority, 'pseudo'); }); test('an edit in X never rewrites X, for every linked language', () => { const base = new SyncHub({}).edit('pseudo', SIMPLE); for (const lang of LINKED) { const text = base.sources[lang]; if (!text) continue; const r = new SyncHub(base.sources, sync(base)).edit(lang, text + '\n'); assert.equal(r.sources[lang], text + '\n', `${lang} is kept as typed, trailing blank line included`); } }); test('an error in the authority changes nothing: the others keep their last good text and say why', () => { const ok = new SyncHub({}).edit('pseudo', SIMPLE); const before = { ...ok.sources }; const r = next(ok, 'lingo', 'Gibberish that is no sentence of lingo at all.'); assert.equal(r.sources.lingo, 'Gibberish that is no sentence of lingo at all.'); for (const l of ['pseudo', 'matho', 'speco']) assert.equal(r.sources[l] ?? '', before[l] ?? '', `${l} kept its text`); for (const l of ['pseudo', 'matho', 'speco']) assert.match(r.stale[l], /lingo line 1 does not read yet/); assert.equal(r.stale.lingo, undefined, 'the pane being typed in is not stale'); // a pseudo with errors does the same const bad = next(ok, 'pseudo', 'FUNCTION BROKEN(\nRETURN'); assert.match(bad.stale.lingo, /pseudo line \d+ does not read yet/); assert.equal(bad.sources.lingo, before.lingo); // and a good edit afterwards clears every mark const fixed = next(bad, 'pseudo', SIMPLE); assert.deepEqual(fixed.stale, {}); }); test('matho goes stale, not blank, on a WHILE; a later expressible program brings it back', () => { const r0 = new SyncHub({}).edit('matho', EQUATIONS['mean'][0]); const matho = r0.sources.matho; const r = next(r0, 'pseudo', WHILE_PSEUDO); assert.equal(r.sources.matho, matho, 'matho keeps its last good text'); assert.match(r.stale.matho, /matho has no form for a WHILE loop \(line \d+\)/); assert.equal(r.stale.lingo, undefined, 'lingo can say a WHILE'); assert.match(r.sources.lingo, /\S/); const back = next(r, 'pseudo', SIMPLE); assert.equal(back.stale.matho, undefined); assert.notEqual(back.sources.matho, matho); }); test('contracts survive an edit in English or in equations', () => { const p = problemById('count-base-proved'); const st = workForProblem(p); const filled = new SyncHub(st.sources).fill(); const spec = filled.sources.speco; assert.match(spec, /ENSURE/); const r = next({ sources: filled.sources, authority: 'speco', stale: {}, names: {} }, 'lingo', filled.sources.lingo.replace('Add 1 to Total.', 'Add 2 to Total.')); assert.equal(r.sources.speco, spec, 'the speco is what it was'); assert.match(r.sources.pseudo, /Total <- Total \+ 2/, 'the edit took'); assert.match(r.sources.pseudo, /ENSURE RESULT == LENGTH/, 'the contract is woven into the pseudo the English lowered to'); assert.match(r.sources.pseudo, /INVARIANT/); assert.match(r.mojo.source, /# ENSURE/, 'the translator carries the contract as a comment'); // a claim about a routine the English no longer has is kept in the speco, not dropped const renamed = filled.sources.lingo.replace('To count base for B and T:', 'To tally base for B and T:'); const r2 = next({ sources: filled.sources, authority: 'speco', stale: {}, names: {} }, 'lingo', renamed); assert.match(r2.sources.speco, /kept:/); assert.match(r2.sources.speco, /ENSURE/); }); test('an edit in speco puts its contracts into the pseudo, then lingo and matho follow', () => { const r0 = new SyncHub({}).edit('pseudo', SIMPLE); const spec = 'SPEC SUMMED(N, X)\n REQUIRE N >= 0\n'; const r = next(r0, 'speco', spec); assert.equal(r.sources.speco, spec); assert.match(r.sources.pseudo, /REQUIRE N >= 0/); assert.equal(r.authority, 'speco'); // replacing it replaces: the old REQUIRE is gone const r2 = next(r, 'speco', 'SPEC SUMMED(N, X)\n REQUIRE N >= 1\n'); assert.match(r2.sources.pseudo, /REQUIRE N >= 1/); assert.doesNotMatch(r2.sources.pseudo, /REQUIRE N >= 0/); // a speco with a syntax error changes nothing const bad = next(r, 'speco', 'SPEC SUMMED(N, X\n REQUIRE ??\n'); assert.equal(bad.sources.pseudo, r.sources.pseudo); assert.ok(bad.stale.pseudo); }); test('the student\'s spelling of equation names survives a trip through pseudo', () => { const r0 = new SyncHub({}).edit('matho', '\\overline{x} = \\frac{1}{n} \\sum_{i=0}^{n-1} x_i'); assert.deepEqual(r0.names, { X_BAR: '\\overline{x}' }); const r = next(r0, 'pseudo', r0.sources.pseudo.replace('RETURN 1 / N * Total', 'RETURN 2 / N * Total')); assert.match(r.sources.matho, /^\\overline\{x\} = /m); assert.deepEqual(namesFromMatho('f(x) = x + 1'), {}, 'a function written with its inputs is spelled by the generator'); }); test('loading an authored exercise changes nothing until the first edit', () => { for (const p of PROBLEMS) { const st = workForProblem(p); const before = JSON.stringify(st.sources); const filled = new SyncHub(st.sources).fill(); for (const l of [...LINKED, 'mojo', 'asm']) if ((st.sources[l] ?? '').trim()) assert.equal(filled.sources[l], st.sources[l], `${p.id}: authored ${l} is shown as written`); assert.equal(JSON.stringify(st.sources), before, `${p.id}: the state's own sources are untouched`); } // through the session: reading never writes, and nothing is saved or marked as edited const p = problemById('count-base'); const s = new Workspace(workForProblem(p)); const raw = JSON.stringify(s.state.sources); assert.match(s.source('lingo'), /\S/, 'a missing pane is shown, generated from the pseudo'); s.parsedLingo(); s.derivedMojo(); s.lineMaps(); assert.equal(JSON.stringify(s.state.sources), raw); assert.equal(s.state.sync, undefined); assert.equal(s.linkState('lingo').kind, 'authored'); }); test('takeAuthority makes a stale pane the authority and regenerates the others from it', () => { const s = new Workspace(workForProblem(problemById('count-base'))); s.edit('matho', 'x = '); // does not read: everything else is stale assert.equal(s.linkState('pseudo').kind, 'stale'); s.edit('pseudo', COUNT); assert.equal(s.state.sync.authority, 'pseudo'); assert.ok(s.linkState('matho').kind === 'stale' || s.linkState('matho').kind === 'sync'); s.takeAuthority('lingo'); assert.equal(s.state.sync.authority, 'lingo'); }); test('line maps compose through the hub', () => { const r = new SyncHub({}).edit('pseudo', SIMPLE); // pseudo line 3 is `S <- 0.0`; its lingo and Mojo lines come back through the maps const lingoLines = SyncHub.relate(r.maps, 'pseudo', 3, 'lingo'); assert.ok(lingoLines.length, 'a lingo line matches'); assert.match(r.sources.lingo.split('\n')[lingoLines[0] - 1], /Start with S at 0\.0/); const mojoLines = SyncHub.relate(r.maps, 'pseudo', 3, 'mojo'); assert.match(r.mojo.source.split('\n')[mojoLines[0] - 1], /s = 0\.0/); // from Mojo back to lingo, through the pseudo const back = SyncHub.relate(r.maps, 'mojo', mojoLines[0], 'lingo'); assert.deepEqual(back, lingoLines); // an authority's own lowering is used the other way: lingo line -> pseudo line const l = new SyncHub({}).edit('lingo', pseudoToLingo(SIMPLE).source); const lingoLine = l.sources.lingo.split('\n').findIndex((t) => /Start with S at/.test(t)) + 1; const ps = SyncHub.relate(l.maps, 'lingo', lingoLine, 'pseudo'); assert.ok(ps.some((n) => /S <- 0\.0/.test(l.sources.pseudo.split('\n')[n - 1])), 'the lingo line maps to the assignment'); assert.ok(SyncHub.relate(l.maps, 'lingo', lingoLine, 'mojo').length); }); test('line maps stay right when contracts are woven into the pseudo an English edit produced', () => { const filled = new SyncHub(workForProblem(problemById('count-base-proved')).sources).fill(); const r = new SyncHub(filled.sources).edit('lingo', filled.sources.lingo); const lingoLine = r.sources.lingo.split('\n').findIndex((t) => /Add 1 to Total/.test(t)) + 1; const ps = SyncHub.relate(r.maps, 'lingo', lingoLine, 'pseudo'); assert.ok(ps.length); assert.match(r.sources.pseudo.split('\n')[ps[0] - 1], /Total <- Total \+ 1/); }); test('every exercise: an edit in any linked pane leaves the Mojo in step with the pseudo and checking clean', () => { let n = 0; for (const p of PROBLEMS) { const st = workForProblem(p); if (!(st.sources.pseudo ?? '').trim() || !st.call) continue; if (!studyOf(st.sources.pseudo).ok) continue; const r = new SyncHub(st.sources, undefined, { call: st.call }).edit('pseudo', st.sources.pseudo); assert.equal(r.sources.pseudo, st.sources.pseudo); assert.match(r.mojo.source, /Generated by Programming\+/, p.id); n++; } assert.ok(n >= 12, `${n} exercises went through the hub`); }); test('timing: a full cascade (lingo edit -> pseudo, matho, speco, Mojo) on the largest exercise stays under 50 ms', () => { let big = null; for (const p of PROBLEMS) { const st = workForProblem(p); const t = st.sources.pseudo ?? ''; if (t.trim() && studyOf(t).ok && (!big || t.length > big.pseudo.length)) big = { id: p.id, pseudo: t, st }; } assert.ok(big); const filled = new SyncHub(big.st.sources, undefined, { call: big.st.call }).fill(); // a lingo that reads: whatever the English generator wrote, or, when it has no English, the smallest program const lingo = filled.sources.lingo || pseudoToLingo(SIMPLE).source; const start = filled.sources.lingo ? filled.sources : { lingo }; const hub = () => new SyncHub(start, undefined, { call: big.st.call }); hub().edit('lingo', lingo); // warm up the engines const times = []; for (let i = 0; i < 7; i++) { const t0 = performance.now(); hub().edit('lingo', lingo + (i % 2 ? '' : '\n')); times.push(performance.now() - t0); } times.sort((a, b) => a - b); const median = times[3]; assert.ok(median < 50, `${big.id}: the median cascade took ${median.toFixed(1)} ms (limit 50)`); });