106 lines
5.5 KiB
JavaScript
106 lines
5.5 KiB
JavaScript
// Pure logic of the window layout (layout.js). Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/layout.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { serialize, migrate, legacy, prune, restore, createSaver, layoutKey } from './layout.js';
|
|
|
|
const snap = () => ({
|
|
tools: { open: { left: 'project', bottom: 'terminal', right: 'claude' }, sizes: { '--left-width': '300px', '--evil': 'x' }, sides: { game: 'bottom', junk: 'middle' } },
|
|
editors: { activeGroup: 1, groups: [
|
|
{ active: 'a.php', tabs: [{ path: 'a.php', view: { line: 12, col: 3, top: 240, sel: [1, 1, 2, 2] } }, { path: 'gone.php' }] },
|
|
{ active: 'b.php', tabs: [{ path: 'b.php', view: { line: -4, col: 'x' } }] }] },
|
|
tree: { expanded: ['src', 'old', 'lib:'], selected: 'src/a.php' },
|
|
claude: { active: 'c-1', convs: { 'c-1': { draft: 'hello', scroll: 120, stick: false, log: true } }, detached: ['c-2'] },
|
|
terminals: { boot: 'abc', active: 1, tabs: [{ id: 1, label: 'bash' }, { id: 2, label: 'Claude Remote Control', rc: { conv: 'c-1', mark: { lines: 4 } } }] },
|
|
share: { active: true },
|
|
});
|
|
|
|
test('serialize keeps the layout and drops what is odd', () => {
|
|
const r = serialize(snap(), 5);
|
|
assert.equal(r.v, 1);
|
|
assert.equal(r.savedAt, 5);
|
|
assert.deepEqual(r.tools.sizes, { '--left-width': '300px' });
|
|
assert.deepEqual(r.tools.sides, { game: 'bottom' });
|
|
assert.deepEqual(r.editors.groups[1].tabs[0].view, null);
|
|
assert.deepEqual(r.editors.groups[0].tabs[0].view, { line: 12, col: 3, top: 240, sel: [1, 1, 2, 2] });
|
|
assert.equal(r.claude.convs['c-1'].draft, 'hello');
|
|
assert.equal(r.terminals.tabs[1].rc.conv, 'c-1');
|
|
assert.equal(r.share.active, true);
|
|
});
|
|
|
|
test('serialize of nothing is a valid empty record', () => {
|
|
const r = serialize({});
|
|
assert.deepEqual(r.editors.groups, []);
|
|
assert.equal(r.tools.open.left, null);
|
|
assert.equal(r.claude.active, null);
|
|
});
|
|
|
|
test('migrate: round trip, other versions and junk', () => {
|
|
const r = serialize(snap(), 1);
|
|
assert.deepEqual(migrate(JSON.parse(JSON.stringify(r))), r);
|
|
assert.equal(migrate({ v: 2 }), null);
|
|
assert.equal(migrate(null), null);
|
|
assert.equal(migrate('x'), null);
|
|
assert.equal(migrate([]), null);
|
|
});
|
|
|
|
test('legacy reads the older prefs', () => {
|
|
const store = { 'tool:left': 'structure', 'tool:bottom': 'run', 'size--left-width': '250px', 'tabs:p': [{ tabs: ['a.php'], active: 'a.php' }], 'tree:p': ['src'] };
|
|
const r = legacy((k, d) => (k in store ? store[k] : d), 'p');
|
|
assert.equal(r.tools.open.left, 'structure');
|
|
assert.equal(r.editors.groups[0].tabs[0].path, 'a.php');
|
|
assert.deepEqual(r.tree.expanded, ['src']);
|
|
assert.equal(r.tools.sizes['--left-width'], '250px');
|
|
});
|
|
|
|
test('prune skips renamed tools and missing files silently', () => {
|
|
const l = prune(serialize(snap()), { tools: ['project', 'claude'], files: new Set(['a.php', 'b.php', 'src/a.php']) });
|
|
assert.equal(l.tools.open.bottom, null);
|
|
assert.equal(l.tools.open.right, 'claude');
|
|
assert.deepEqual(l.tools.sides, {});
|
|
assert.deepEqual(l.editors.groups[0].tabs.map((x) => x.path), ['a.php']);
|
|
assert.deepEqual(l.tree.expanded, ['src', 'lib:']);
|
|
assert.equal(l.tree.selected, 'src/a.php');
|
|
});
|
|
|
|
test('prune: a group whose active file is gone shows the last one; empty groups go', () => {
|
|
const rec = serialize({ editors: { activeGroup: 5, groups: [{ active: 'x', tabs: [{ path: 'x' }, { path: 'y' }] }, { active: 'z', tabs: [{ path: 'z' }] }] } });
|
|
const l = prune(rec, { tools: [], files: new Set(['y']) });
|
|
assert.equal(l.editors.groups.length, 1);
|
|
assert.equal(l.editors.groups[0].active, 'y');
|
|
assert.equal(l.editors.activeGroup, 0);
|
|
});
|
|
|
|
test('restore: order tools, tree, editors, terminals, claude; a failing step does not stop the others', async () => {
|
|
const calls = [];
|
|
const host = {
|
|
toolIds: () => ['project', 'terminal', 'claude', 'game'], files: () => new Set(['a.php', 'b.php', 'src/a.php']),
|
|
setSizes: (s) => calls.push(['sizes', s]), moveTool: (id, side) => calls.push(['move', id, side]),
|
|
showTool: (id) => calls.push(['show', id]), closeSide: (s) => calls.push(['close', s]),
|
|
tree: () => calls.push(['tree']), editors: () => { calls.push(['editors']); throw new Error('boom'); },
|
|
terminals: () => calls.push(['terminals']), claude: (c) => calls.push(['claude', c.active]),
|
|
};
|
|
const { failed } = await restore(serialize(snap()), host);
|
|
assert.deepEqual(calls.map((c) => c[0]), ['sizes', 'move', 'show', 'show', 'show', 'tree', 'editors', 'terminals', 'claude']);
|
|
assert.deepEqual(calls.filter((c) => c[0] === 'show').map((c) => c[1]), ['project', 'terminal', 'claude']);
|
|
assert.equal(failed.length, 1);
|
|
assert.match(failed[0], /^editors: boom/);
|
|
});
|
|
|
|
test('restore closes a side that was closed', async () => {
|
|
const closed = [];
|
|
await restore(serialize({ tools: { open: { left: 'project' } } }), { toolIds: () => ['project'], setSizes() {}, showTool() {}, closeSide: (s) => closed.push(s) });
|
|
assert.deepEqual(closed, ['bottom', 'right']);
|
|
});
|
|
|
|
test('the saver writes only a changed snapshot, and flush writes at once', () => {
|
|
let n = 0; const writes = [];
|
|
const saver = createSaver(() => serialize({ tools: { open: { left: 'p' + (n > 1 ? 1 : 0) } } }, ++n), (r) => writes.push(r));
|
|
assert.equal(saver.flush(), true);
|
|
assert.equal(saver.flush(), false); // same layout (only savedAt differs)
|
|
assert.equal(saver.flush(), true); // changed
|
|
assert.equal(writes.length, 2);
|
|
saver.forget();
|
|
assert.equal(saver.flush(), true);
|
|
});
|
|
|
|
test('layoutKey is per project', () => { assert.notEqual(layoutKey('a'), layoutKey('b')); });
|