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

97 lines
4.5 KiB
JavaScript

// 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 '../core/dist/sync/hub.js';
import { pseudoToLingo } from '../core/dist/sync/pseudo-to-lingo.js';
import { Session } from '../dist/core/session.js';
import { PROBLEMS, stateForProblem, problemById } from '../dist/core/catalog.js';
import { encodeState, decodeState, emptyState } from '../dist/core/state.js';
import { checkMojo } from '../core/dist/mojo/check.js';
import { studyOf } from '../core/dist/services/pseudo.js';
import { EQUATIONS, FIXTURES } from '../core/tests/helpers/sync-programs.mjs';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
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('the first edit makes the pane the authority and writes the shown panes into the state', () => {
const p = problemById('count-base');
const s = new Session(stateForProblem(p));
let events = 0; s.onChange(() => events++);
const lingo = s.source('lingo').replace('Start with Count at 0.', 'Start with Count at 5.');
assert.notEqual(lingo, s.source('lingo'));
s.setSource('lingo', lingo);
assert.equal(events, 1, 'one change event for the whole cascade');
assert.equal(s.state.sync.authority, 'lingo');
assert.equal(s.state.sources.lingo, lingo);
assert.match(s.source('pseudo'), /Count <- 5/);
assert.match(s.derivedMojo().source, /count = 5/);
assert.equal(s.linkState('lingo').kind, 'authority');
assert.equal(s.linkState('pseudo').kind, 'sync');
// an unchanged text is not an edit
s.setSource('lingo', lingo);
assert.equal(events, 1);
// Mojo and x86-64 are not linked
s.setSource('mojo', 'def main():\n pass\n');
assert.equal(s.state.sync.authority, 'lingo');
});
test('a share link round-trips sync, and a stale pane stays stale', async () => {
const p = problemById('count-base');
const s = new Session(stateForProblem(p));
s.setSource('pseudo', 'FUNCTION BROKEN(\nRETURN');
assert.equal(s.linkState('lingo').kind, 'stale');
const blob = await encodeState(s.state);
const back = await decodeState(blob);
assert.deepEqual(back.sync, s.state.sync);
const s2 = new Session(back);
assert.match(s2.linkState('lingo').reason, /pseudo line/);
assert.equal(s2.source('pseudo'), 'FUNCTION BROKEN(\nRETURN', 'a state that has been edited is not filled again');
// a malformed sync field is dropped, not trusted
const { normalize } = await import('../dist/core/state.js');
assert.equal(normalize({ v: 3, sync: { authority: 'mojo' } }).sync, undefined);
});
test('derivedMojo is the literal translation, is memoized by the pseudo and the call, and passes the checker', () => {
const s = new Session(stateForProblem(problemById('count-base')));
const a = s.derivedMojo();
assert.match(a.source, /^# Generated by Programming\+ from pseudo\./);
assert.equal(s.derivedMojo(), a, 'the same object while the pseudo and the call are unchanged');
const diags = checkMojo(a.source).diagnostics.filter((d) => d.severity === 'error');
assert.deepEqual(diags, []);
s.setSource('pseudo', s.source('pseudo').replace('Total <- 0', 'Total <- 1'));
assert.notEqual(s.derivedMojo(), a);
assert.match(s.derivedMojo().source, /total = 1/);
s.setCall('COUNT_BASE("C", "CCC")');
assert.match(s.derivedMojo().source, /count_base\("C", "CCC"\)/);
// no pseudo, no Mojo
assert.equal(new Session(emptyState()).derivedMojo().source, '');
});