commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,95 @@
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Programming+ is a new product: a state from any other version normalizes to empty, not migrated.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { normalize, emptyState, DEFAULT_PANES } from '../dist/core/state.js';
|
||||
|
||||
test('the state format is 3: no code pane, and mojo is the pane of Mojo the student writes', () => {
|
||||
assert.equal(emptyState().v, 3);
|
||||
assert.ok(!DEFAULT_PANES.includes('code'));
|
||||
assert.ok(DEFAULT_PANES.includes('mojo'));
|
||||
});
|
||||
|
||||
test('format-3 Mojo state round-trips, with its build level', () => {
|
||||
const s = normalize({ v: 3, lang: 'mojo', panes: ['pseudo', 'mojo'], sources: { mojo: 'def main():\n print(1)' }, view: 'compiler', build: 'default' });
|
||||
assert.equal(s.lang, 'mojo'); assert.equal(s.view, 'compiler'); assert.equal(s.build, 'default');
|
||||
assert.equal(s.sources.mojo, 'def main():\n print(1)');
|
||||
assert.equal(normalize({ v: 3, build: 'O3' }).build, undefined);
|
||||
});
|
||||
|
||||
test('a state from any other version normalizes to empty, not migrated', () => {
|
||||
assert.deepEqual(normalize({ v: 2, lang: 'mojo', panes: ['pseudo', 'mojo'], sources: { pseudo: 'OUTPUT 1' } }), emptyState());
|
||||
assert.deepEqual(normalize({ v: 1, lang: 'code', panes: ['pseudo', 'code', 'asm'], sources: { pseudo: 'OUTPUT 1', code: 'print(1)' }, target: 'rust', codeView: 'manual' }), emptyState());
|
||||
// the oldest links, with no v at all, and whose generated pane was called mojo: also empty, not migrated
|
||||
assert.deepEqual(normalize({ lang: 'mojo', panes: ['lingo', 'mojo'] }), emptyState());
|
||||
});
|
||||
|
||||
test('format-3 state round-trips', () => {
|
||||
const s = normalize({ v: 3, lang: 'asm', sources: { asm: 'ret' }, view: 'asm' });
|
||||
assert.equal(s.lang, 'asm'); assert.equal(s.panes, undefined, 'the area, not the state, decides which panes show'); assert.equal(s.sources.asm, 'ret');
|
||||
});
|
||||
|
||||
test('an embed keeps its own save, never the app\'s save for the problem', async () => {
|
||||
const { embedStorageId } = await import('../dist/core/state.js');
|
||||
const { Session } = await import('../dist/core/session.js');
|
||||
assert.equal(embedStorageId('mojo', 'count-base'), 'embed.mojo.p.count-base');
|
||||
assert.equal(embedStorageId('trace', undefined, 'abc'), embedStorageId('trace', undefined, 'abc'));
|
||||
assert.notEqual(embedStorageId('trace', undefined, 'abc'), embedStorageId('trace', undefined, 'abd'));
|
||||
assert.match(embedStorageId('trace', undefined, 'x'.repeat(5000)), /^embed\.trace\.b\.[0-9a-f]{8}$/);
|
||||
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) };
|
||||
try {
|
||||
const embed = new Session(normalize({ v: 3, problem: 'count-base', sources: { pseudo: 'OUTPUT 1' } }), { storageId: 'embed.pseudo.p.count-base' });
|
||||
embed.setSource('pseudo', 'OUTPUT 2');
|
||||
assert.deepEqual([...store.keys()], ['pplus.embed.pseudo.p.count-base']);
|
||||
const app = new Session(normalize({ v: 3, problem: 'count-base', sources: { pseudo: 'OUTPUT 1' } }));
|
||||
app.setSource('pseudo', 'OUTPUT 3');
|
||||
assert.ok(store.has('pplus.count-base'));
|
||||
assert.match(store.get('pplus.embed.pseudo.p.count-base'), /OUTPUT 2/);
|
||||
// an embed keeps a save only while it holds edits: editing back to the original removes it
|
||||
const { editedFrom } = await import('../dist/core/state.js');
|
||||
const original = normalize({ v: 3, problem: 'count-base', sources: { pseudo: 'OUTPUT 1' } });
|
||||
const kept = new Session(structuredClone(original), { storageId: 'embed.x', keep: (st) => editedFrom(st, original) });
|
||||
kept.setSource('pseudo', 'OUTPUT 9'); assert.ok(store.has('pplus.embed.x'));
|
||||
kept.setSource('pseudo', 'OUTPUT 1'); assert.ok(!store.has('pplus.embed.x'), 'back to the original: no save');
|
||||
kept.setView('proof'); assert.ok(!store.has('pplus.embed.x'), 'where the reader looks is not an edit');
|
||||
const readOnly = new Session(normalize({ v: 3, problem: 'count-base' }), { storageId: null });
|
||||
store.clear(); readOnly.setSource('pseudo', 'OUTPUT 4');
|
||||
assert.equal(store.size, 0, 'a read-only embed saves nothing');
|
||||
} finally { delete globalThis.localStorage; }
|
||||
});
|
||||
|
||||
test('a save made against an older version of a problem is dropped unless it holds edits', async () => {
|
||||
const { normalize, editsFingerprint, resolveSaved } = await import('../dist/core/state.js');
|
||||
const original = normalize({ v: 3, problem: 'p', sources: { pseudo: 'OUTPUT 2' } });
|
||||
const older = normalize({ v: 3, problem: 'p', sources: { pseudo: 'OUTPUT 1' } });
|
||||
assert.deepEqual(resolveSaved(original, null), { state: original, stale: false });
|
||||
// made against the current version: kept as is
|
||||
const current = { ...structuredClone(original), base: editsFingerprint(original), edited: false, t: 3 };
|
||||
assert.equal(resolveSaved(original, current).state.t, 3);
|
||||
// made against an older version, no edits: the author's current version wins
|
||||
const untouched = { ...structuredClone(older), base: editsFingerprint(older), edited: false };
|
||||
assert.deepEqual(resolveSaved(original, untouched), { state: original, stale: false });
|
||||
// made against an older version, with edits: kept, and marked stale
|
||||
const edited = { ...structuredClone(older), sources: { pseudo: 'OUTPUT 9' }, base: editsFingerprint(older), edited: true };
|
||||
const r = resolveSaved(original, edited);
|
||||
assert.equal(r.stale, true); assert.equal(r.state.sources.pseudo, 'OUTPUT 9');
|
||||
// a save from before fingerprints existed is kept
|
||||
assert.equal(resolveSaved(original, normalize({ v: 3, sources: { pseudo: 'OUTPUT 7' } })).state.sources.pseudo, 'OUTPUT 7');
|
||||
// fingerprints ignore where the reader is looking
|
||||
assert.equal(editsFingerprint(original), editsFingerprint({ ...original, t: 9, view: 'proof' }));
|
||||
});
|
||||
|
||||
test('a save from another state format is discarded, so the problem opens as written', async () => {
|
||||
const { loadLocal, resolveSaved } = await import('../dist/core/state.js');
|
||||
const { problemById, stateForProblem } = await import('../dist/core/catalog.js');
|
||||
const store = new Map([['pplus.count-base', JSON.stringify({ v: 2, problem: 'count-base', sources: { pseudo: 'OUTPUT 9' } })]]);
|
||||
globalThis.localStorage = { getItem: (k) => store.get(k) ?? null, setItem: (k, v) => store.set(k, String(v)), removeItem: (k) => store.delete(k) };
|
||||
try {
|
||||
assert.equal(loadLocal('count-base'), null);
|
||||
assert.ok(!store.has('pplus.count-base'), 'the old save is removed');
|
||||
const original = stateForProblem(problemById('count-base'));
|
||||
assert.deepEqual(resolveSaved(original, loadLocal('count-base')).state, original);
|
||||
} finally { delete globalThis.localStorage; }
|
||||
});
|
||||
Reference in new issue
Block a user