commit
e092cb029c
252 files changed
+35037
No files matched your search
@@ -0,0 +1,231 @@
|
||||
// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Stage 14: saved versions. A version holds what the student edits (sources, link bookkeeping, where the Mojo came
|
||||
// from, call, input, setup, machine, build) and not where they are looking (layout, step, open tab). Switching and
|
||||
// Reset save unsaved work first, so neither loses anything; the cap holds; a link carries no versions, a file may.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Session } from '../dist/core/session.js';
|
||||
import { stateForProblem, problemById } from '../dist/core/catalog.js';
|
||||
import { resolveSaved, editsFingerprint, normalize, encodeState, decodeState, saveLocal } from '../dist/core/state.js';
|
||||
import {
|
||||
listVersions, saveVersion, renameVersion, deleteVersion, restoreVersion, editsOf, hasUnsavedEdits, versionKey, currentVersion,
|
||||
exportVersions, importVersions, cleanVersion, storagePressure, MAX_VERSIONS, PRESSURE_CHARS, newWorkspaceId,
|
||||
} from '../dist/core/versions.js';
|
||||
|
||||
function fakeStorage() {
|
||||
const m = new Map();
|
||||
return {
|
||||
getItem: (k) => m.get(k) ?? null, setItem: (k, v) => m.set(k, String(v)), removeItem: (k) => m.delete(k),
|
||||
key: (i) => [...m.keys()][i] ?? null, get length() { return m.size; }, _m: m,
|
||||
};
|
||||
}
|
||||
let store;
|
||||
test.beforeEach(() => { store = fakeStorage(); globalThis.localStorage = store; });
|
||||
|
||||
const problem = (id = 'count-base') => stateForProblem(problemById(id));
|
||||
const open = (state, area = 'bridge', opts = {}) => new Session(state, { area, storageId: null, original: opts.original, stale: opts.stale });
|
||||
const withLingo = (st, text) => { const s = structuredClone(st); s.sources.lingo = text; return s; };
|
||||
|
||||
test('save, list, rename, delete and restore round-trip', () => {
|
||||
const st = problem();
|
||||
const key = versionKey(st);
|
||||
assert.equal(key, 'pplus.versions.count-base');
|
||||
assert.deepEqual(listVersions(key), []);
|
||||
const a = saveVersion(key, 'first', st, 'bridge', { now: 1000 });
|
||||
const edited = withLingo(st, 'Start with Total at 0.');
|
||||
const b = saveVersion(key, 'second', edited, 'dev', { now: 2000 });
|
||||
assert.ok(a.ok && b.ok);
|
||||
assert.deepEqual(listVersions(key).map((v) => [v.name, v.area]), [['second', 'dev'], ['first', 'bridge']], 'newest first, with the area it was saved in');
|
||||
assert.equal(renameVersion(key, a.version.id, 'original'), true);
|
||||
assert.equal(listVersions(key)[1].name, 'original');
|
||||
const back = restoreVersion(key, b.version.id, st);
|
||||
assert.equal(back.sources.lingo, 'Start with Total at 0.');
|
||||
assert.equal(restoreVersion(key, 'nope', st), null);
|
||||
assert.equal(deleteVersion(key, b.version.id), true);
|
||||
assert.equal(listVersions(key).length, 1);
|
||||
assert.equal(deleteVersion(key, 'nope'), false);
|
||||
deleteVersion(key, a.version.id);
|
||||
assert.equal(store.getItem(key), null, 'an empty list leaves nothing behind');
|
||||
});
|
||||
|
||||
test('a version holds the edited fields, sync, mojoOrigin and machine, and not the layout, step or tab', () => {
|
||||
const st = problem();
|
||||
Object.assign(st, { input: '3\n', call: 'COUNT_BASE("A", "AA")', setup: 'x', machine: 'm', build: 'default', mojoOrigin: 'generated', sync: { authority: 'lingo', stale: {}, names: {} }, layout: { bridge: { split: 0.3 } }, t: 7, lang: 'matho', view: 'flow', title: 'T' });
|
||||
const e = editsOf(st);
|
||||
assert.deepEqual(Object.keys(e).sort(), ['build', 'call', 'input', 'machine', 'mojoOrigin', 'setup', 'sources', 'sync']);
|
||||
for (const k of ['layout', 't', 'lang', 'view', 'title', 'problem', 'base', 'edited']) assert.ok(!(k in e), k);
|
||||
});
|
||||
|
||||
test('restoring changes the edits but not the layout, the step or the open tab; a field the version lacks does not linger', () => {
|
||||
const key = 'pplus.versions.count-base';
|
||||
const plain = problem();
|
||||
const v = saveVersion(key, 'plain', plain, 'bridge').version;
|
||||
const now = withLingo(plain, 'something else');
|
||||
Object.assign(now, { call: 'X()', mojoOrigin: 'generated-edited', sync: { authority: 'lingo', stale: {}, names: {} }, layout: { bridge: { split: 0.7 } }, t: 9, lang: 'lingo', view: 'flow' });
|
||||
const r = restoreVersion(key, v.id, now);
|
||||
assert.equal(r.sources.lingo, plain.sources.lingo);
|
||||
assert.equal(r.call, plain.call);
|
||||
assert.ok(!('mojoOrigin' in r) && !('sync' in r));
|
||||
assert.deepEqual([r.layout, r.t, r.lang, r.view], [now.layout, 9, 'lingo', 'flow']);
|
||||
});
|
||||
|
||||
test('switching with unsaved edits saves "Before switching" first; switching without edits does not', () => {
|
||||
const st = problem();
|
||||
const s = open(structuredClone(st), 'bridge', { original: st });
|
||||
const keep = s.saveVersion('plain'); assert.ok(keep.ok);
|
||||
assert.equal(s.hasUnsavedEdits(), false, 'the problem as written has nothing to lose');
|
||||
s.setSource('lingo', 'Start with Total at 5.');
|
||||
assert.equal(s.hasUnsavedEdits(), true);
|
||||
const was = s.state.sources.lingo;
|
||||
assert.ok(s.restoreVersion(keep.version.id));
|
||||
const names = s.versions().map((v) => v.name);
|
||||
assert.equal(names.length, 2);
|
||||
assert.ok(names.some((n) => /^Before switching \(/.test(n)), names.join('|'));
|
||||
assert.ok(s.versions().find((v) => v.auto).edits.sources.lingo === was, 'the automatic version holds the work that was replaced');
|
||||
assert.equal(s.state.sources.lingo, st.sources.lingo);
|
||||
// now the work is exactly a saved version: switching again adds nothing
|
||||
assert.equal(s.hasUnsavedEdits(), false);
|
||||
const before = s.versions().length;
|
||||
const other = s.versions().find((v) => v.auto);
|
||||
assert.ok(s.restoreVersion(other.id));
|
||||
assert.equal(s.versions().length, before);
|
||||
});
|
||||
|
||||
test('Reset saves the work first as "Before reset", and does nothing extra when there is nothing to lose', () => {
|
||||
const st = problem();
|
||||
const s = open(structuredClone(st), 'dev', { original: st });
|
||||
assert.ok(s.reset(problem()));
|
||||
assert.deepEqual(s.versions(), []);
|
||||
s.setSource('mojo', s.source('mojo') + '\n# mine');
|
||||
assert.ok(s.reset(problem()));
|
||||
const [v] = s.versions();
|
||||
assert.match(v.name, /^Before reset \(/);
|
||||
assert.ok(v.auto && v.edits.sources.mojo.endsWith('# mine'));
|
||||
assert.ok(!s.source('mojo').includes('# mine'));
|
||||
});
|
||||
|
||||
test('the cap holds: automatic versions make room, and named ones are never dropped to do it', () => {
|
||||
const key = 'pplus.versions.count-base', st = problem();
|
||||
for (let i = 0; i < MAX_VERSIONS; i++) assert.ok(saveVersion(key, `v${i}`, st, 'bridge', { now: 1000 + i, auto: i < 3 }).ok);
|
||||
assert.equal(listVersions(key).length, MAX_VERSIONS);
|
||||
const r = saveVersion(key, 'more', st, 'bridge', { now: 9999 });
|
||||
assert.ok(r.ok);
|
||||
assert.equal(r.dropped.length, 1);
|
||||
assert.equal(r.dropped[0].name, 'v0', 'the oldest automatic one went');
|
||||
assert.equal(listVersions(key).length, MAX_VERSIONS);
|
||||
for (let i = 0; i < 2; i++) saveVersion(key, `x${i}`, st, 'bridge', { now: 10000 + i });
|
||||
assert.equal(listVersions(key).length, MAX_VERSIONS);
|
||||
const full = saveVersion(key, 'no room', st, 'bridge', { now: 20000 });
|
||||
assert.deepEqual(full, { ok: false, reason: 'cap' });
|
||||
assert.equal(listVersions(key).length, MAX_VERSIONS);
|
||||
});
|
||||
|
||||
test('a refused save stops the switch: nothing changes and nothing is lost', () => {
|
||||
const st = problem();
|
||||
const s = open(structuredClone(st), 'bridge', { original: st });
|
||||
const keep = s.saveVersion('plain');
|
||||
for (let i = 0; i < MAX_VERSIONS - 1; i++) saveVersion(s.versionKey(), `n${i}`, st, 'bridge', { now: 5 + i });
|
||||
s.setSource('lingo', 'Start with Total at 6.');
|
||||
assert.equal(s.restoreVersion(keep.version.id), false);
|
||||
assert.equal(s.state.sources.lingo, 'Start with Total at 6.');
|
||||
});
|
||||
|
||||
test('a stale problem: "See the new version" keeps the edits as a version and opens the problem as it is now', () => {
|
||||
const original = problem();
|
||||
const saved = withLingo(original, 'My own start.');
|
||||
saved.base = 'deadbeef'; saved.edited = true; // made against another version of the problem
|
||||
const { state, stale } = resolveSaved(original, saved);
|
||||
assert.equal(stale, true);
|
||||
const s = open(state, 'bridge', { original, stale });
|
||||
assert.equal(s.stale, true);
|
||||
assert.ok(s.seeNewVersion());
|
||||
assert.equal(s.stale, false);
|
||||
assert.equal(s.state.sources.lingo, original.sources.lingo, 'the problem as it is now');
|
||||
const [v] = s.versions();
|
||||
assert.equal(v.name, 'My edits, before the problem changed');
|
||||
assert.ok(!v.auto);
|
||||
assert.equal(v.edits.sources.lingo, 'My own start.');
|
||||
assert.equal(editsFingerprint(s.state), editsFingerprint(original));
|
||||
});
|
||||
|
||||
test('work from a share link has no problem: it gets a workspace id when first saved, and the list is found again by it', async () => {
|
||||
const shared = normalize({ ...problem(), v: 3 }); shared.problem = undefined; shared.title = 'From a link';
|
||||
const blob = await encodeState(shared);
|
||||
const s = open(await decodeState(blob), 'bridge');
|
||||
assert.equal(s.versionKey(), null);
|
||||
assert.deepEqual(s.versions(), []);
|
||||
assert.equal(s.hasUnsavedEdits(), true, 'work with no original has something to save once it has text');
|
||||
const r = s.saveVersion('mine');
|
||||
assert.ok(r.ok);
|
||||
assert.match(s.state.workspace, /^w[0-9a-f]{16}$/);
|
||||
assert.equal(s.versionKey(), `pplus.versions.ws.${s.state.workspace}`);
|
||||
const again = open(await decodeState(await encodeState(s.state)), 'dev');
|
||||
assert.deepEqual(again.versions().map((v) => v.name), ['mine'], 'the same work in another area finds the same versions');
|
||||
});
|
||||
|
||||
test('the same work in each area shares one list', () => {
|
||||
const st = problem();
|
||||
const a = open(structuredClone(st), 'bridge', { original: st }), b = open(structuredClone(st), 'dev', { original: st }), c = open(structuredClone(st), 'hw', { original: st });
|
||||
a.saveVersion('from the Bridge');
|
||||
assert.deepEqual(b.versions().map((v) => [v.name, v.area]), [['from the Bridge', 'bridge']]);
|
||||
assert.equal(c.versions().length, 1);
|
||||
});
|
||||
|
||||
test('a file with versions restores them in a fresh storage, never over a version with the same id', () => {
|
||||
const key = 'pplus.versions.ws.wabc12345678', st = problem();
|
||||
saveVersion(key, 'one', st, 'bridge', { now: 1 }); saveVersion(key, 'two', withLingo(st, 'x'), 'dev', { now: 2 });
|
||||
const file = JSON.parse(JSON.stringify(exportVersions(key)));
|
||||
const fresh = fakeStorage();
|
||||
assert.deepEqual(importVersions(key, file, fresh), { added: 2, skipped: 0 });
|
||||
assert.deepEqual(listVersions(key, fresh).map((v) => v.name), ['two', 'one']);
|
||||
// renamed in the fresh browser; importing the file again must not undo that
|
||||
renameVersion(key, file[0].id, 'renamed here', fresh);
|
||||
assert.deepEqual(importVersions(key, file, fresh), { added: 0, skipped: 2 });
|
||||
assert.equal(listVersions(key, fresh).find((v) => v.id === file[0].id).name, 'renamed here');
|
||||
});
|
||||
|
||||
test('a damaged or hostile version in a file is dropped or cleaned, never trusted', () => {
|
||||
assert.equal(cleanVersion(null), null);
|
||||
assert.equal(cleanVersion({ id: '../x', name: 'n', at: 1, area: 'bridge', edits: {} }), null);
|
||||
assert.equal(cleanVersion({ id: 'ok', name: 'n', at: 1, area: 'nowhere', edits: {} }), null);
|
||||
const v = cleanVersion({ id: 'ok', name: 'n'.repeat(500), at: 1, area: 'bridge', edits: { sources: { pseudo: 'x', evil: 'y' }, layout: { bridge: { split: 0.1 } }, t: 4, mojoOrigin: 'hand-written' } });
|
||||
assert.equal(v.name.length, 120);
|
||||
assert.deepEqual(Object.keys(v.edits), ['sources']);
|
||||
assert.deepEqual(v.edits.sources, { pseudo: 'x' });
|
||||
const key = 'pplus.versions.count-base';
|
||||
assert.deepEqual(importVersions(key, 'nonsense'), { added: 0, skipped: 0 });
|
||||
assert.deepEqual(importVersions(key, [{ id: 1 }]), { added: 0, skipped: 1 });
|
||||
});
|
||||
|
||||
test('the drop-down warns when storage is nearly full', () => {
|
||||
assert.equal(storagePressure(store).nearlyFull, false);
|
||||
store.setItem('big', 'x'.repeat(PRESSURE_CHARS));
|
||||
assert.equal(storagePressure(store).nearlyFull, true);
|
||||
});
|
||||
|
||||
test('a browser that refuses storage still keeps versions for the tab, and a full one says so instead of losing them', () => {
|
||||
const key = 'pplus.versions.count-base', st = problem();
|
||||
assert.ok(saveVersion(key, 'tab only', st, 'bridge', { store: null }).ok);
|
||||
assert.deepEqual(listVersions(key, null).map((v) => v.name), ['tab only']);
|
||||
const full = { getItem: () => null, setItem() { throw new Error('quota'); }, removeItem() { } };
|
||||
assert.deepEqual(saveVersion(key, 'x', st, 'bridge', { store: full }), { ok: false, reason: 'storage' });
|
||||
});
|
||||
|
||||
test('a version is not in the state, a link or the problem\'s own save', async () => {
|
||||
const s = open(problem(), 'bridge');
|
||||
s.saveVersion('v');
|
||||
assert.ok(!/versions?/i.test(JSON.stringify(s.state).replace(/"version"/g, '')));
|
||||
assert.ok(!/"edits"/.test(await encodeState(s.state)));
|
||||
assert.equal(newWorkspaceId().length, 17);
|
||||
assert.equal(currentVersion(s.versionKey(), s.state)?.name, 'v', 'the work is exactly the version just saved');
|
||||
});
|
||||
|
||||
test('a session keeps its own copy of the problem: editing the work never edits "the problem as written"', () => {
|
||||
const st = problem();
|
||||
const s = open(st, 'bridge', { original: st }); // the very same object, as when a problem opens with no save
|
||||
s.setSource('lingo', 'Start with Total at 5.');
|
||||
assert.notEqual(st.sources.lingo, 'Start with Total at 5.' === st.sources.lingo ? '' : 'x');
|
||||
assert.equal(s.hasUnsavedEdits(), true, 'the edit shows against the problem as written');
|
||||
assert.equal(s.state.sources.lingo, 'Start with Total at 5.');
|
||||
});
|
||||
Reference in new issue
Block a user