111 lines
6 KiB
JavaScript
111 lines
6 KiB
JavaScript
// The variable tree must never lose its rows: nested expansions, answers arriving out of order, a failing child, a redraw of the panel.
|
|
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/var-tree.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
// a minimal DOM: just what el() and the tree use
|
|
class FakeNode {
|
|
constructor(tag) {
|
|
this.tag = tag; this.children = []; this.parent = null; this.attrs = {}; this.style = {}; this.dataset = {}; this.listeners = {};
|
|
this.className = ''; this.textContent = ''; this.tabIndex = -1;
|
|
const self = this;
|
|
this.classList = {
|
|
add: (c) => { if (!self.classList.contains(c)) self.className = `${self.className} ${c}`.trim(); },
|
|
remove: (c) => { self.className = self.className.split(/\s+/).filter((x) => x && x !== c).join(' '); },
|
|
contains: (c) => self.className.split(/\s+/).includes(c),
|
|
toggle: (c, on) => (on ? self.classList.add(c) : self.classList.remove(c)),
|
|
};
|
|
}
|
|
append(...nodes) { for (const n of nodes) { n.parent?.children.splice(n.parent.children.indexOf(n), 1); n.parent = this; this.children.push(n); } }
|
|
remove() { if (this.parent) { this.parent.children.splice(this.parent.children.indexOf(this), 1); this.parent = null; } }
|
|
replaceWith(n) { const p = this.parent; if (!p) return; p.children.splice(p.children.indexOf(this), 1, n); n.parent = p; this.parent = null; }
|
|
get lastChild() { return this.children[this.children.length - 1]; }
|
|
setAttribute(k, v) { this.attrs[k] = v; }
|
|
addEventListener(type, fn) { (this.listeners[type] ||= []).push(fn); }
|
|
focus() {}
|
|
click() { const e = { stopPropagation() {}, preventDefault() {} }; this.onclick?.(e); for (const f of this.listeners.click || []) f(e); }
|
|
text() { return (this.textContent || '') + this.children.map((c) => (c instanceof FakeNode ? c.text() : '')).join(''); }
|
|
rows() { const out = []; const walk = (n) => { if (n.classList.contains('row')) out.push(n); n.children.forEach(walk); }; walk(this); return out; }
|
|
}
|
|
globalThis.Node = FakeNode;
|
|
globalThis.document = { createElement: (t) => new FakeNode(t), createTextNode: (s) => { const n = new FakeNode('#text'); n.textContent = s; return n; } };
|
|
Object.defineProperty(globalThis, 'navigator', { value: { clipboard: {} }, configurable: true });
|
|
|
|
const { VarTree } = await import('./var-tree.js');
|
|
const tick = () => new Promise((r) => setTimeout(r, 5));
|
|
|
|
/** A fake debugger with nested arrays; `delays` per fullname, `fail` names that throw. */
|
|
function ctxOf({ data, delays = {}, fail = new Set() }) {
|
|
return {
|
|
canSet: () => false,
|
|
fetchChildren: async (v, page) => {
|
|
await new Promise((r) => setTimeout(r, delays[v.fullname] || 0));
|
|
if (fail.has(v.fullname)) throw new Error('read failed: ' + v.fullname);
|
|
const items = data[v.fullname] || [];
|
|
return { items, size: items.length, pagesize: 100 };
|
|
},
|
|
};
|
|
}
|
|
const node = (name, fullname, kids = true) => ({ name, fullname, type: 'array', children: kids, size: 1 });
|
|
|
|
test('nested expansion: every level keeps its rows', async () => {
|
|
const data = { $a: [node('b', '$a[b]'), { name: 'n', fullname: '$a[n]', type: 'int', value: '1' }], '$a[b]': [node('c', '$a[b][c]')], '$a[b][c]': [{ name: 'deep', fullname: 'x', type: 'string', value: 'ok' }] };
|
|
const tree = new VarTree(ctxOf({ data }));
|
|
const box = tree.row(node('a', '$a'), 0);
|
|
const click = async (i) => { box.rows()[i].click(); await tick(); };
|
|
await click(0); await click(1); await click(2);
|
|
assert.match(box.text(), /deep/);
|
|
assert.equal(box.rows().filter((r) => r.className.includes('open')).length, 3);
|
|
});
|
|
|
|
test('answers out of order: the slow answer of a closed node does not erase or fill another one', async () => {
|
|
const data = { $a: [node('slow', '$s'), node('fast', '$f')], $s: [{ name: 'S', type: 'int', value: '1' }], $f: [{ name: 'F', type: 'int', value: '2' }] };
|
|
const tree = new VarTree(ctxOf({ data, delays: { $s: 40 } }));
|
|
const box = tree.row(node('a', '$a'), 0);
|
|
box.rows()[0].click(); await tick();
|
|
box.rows()[1].click(); // slow: pending
|
|
box.rows()[1].click(); // closed again before the answer
|
|
box.rows()[2].click(); await tick(); // fast (row 2 now)
|
|
await new Promise((r) => setTimeout(r, 60));
|
|
const t = box.text();
|
|
assert.match(t, /F/);
|
|
assert.doesNotMatch(t, /\bS\b/);
|
|
assert.match(t, /slow/, 'the parent still has its rows');
|
|
});
|
|
|
|
test('a failing child stays inside its node as an error row; siblings and parent remain', async () => {
|
|
const data = { $a: [node('bad', '$bad'), node('good', '$good'), null], $good: [{ name: 'G', type: 'int', value: '3' }] };
|
|
const tree = new VarTree(ctxOf({ data, fail: new Set(['$bad']) }));
|
|
const box = tree.row(node('a', '$a'), 0);
|
|
box.rows()[0].click(); await tick();
|
|
const rows = box.rows();
|
|
assert.equal(rows.length, 4, 'a, bad, good and the empty item are all drawn');
|
|
rows[1].click(); await tick();
|
|
assert.match(box.text(), /read failed: \$bad/);
|
|
box.rows().find((r) => r.text().startsWith('good')).click(); await tick();
|
|
assert.match(box.text(), /G/);
|
|
});
|
|
|
|
test('a row that cannot be drawn becomes an error row (no exception, no empty panel)', () => {
|
|
const tree = new VarTree(ctxOf({ data: {} }));
|
|
const row = tree.row(null, 0);
|
|
assert.match(row.text(), /error/);
|
|
const odd = tree.row({ name: 'x', type: 'object', children: true, get value() { throw new Error('boom'); } }, 0);
|
|
assert.match(odd.text(), /error: boom/);
|
|
});
|
|
|
|
test('what was opened stays open when the panel is drawn again', async () => {
|
|
const data = { $a: [node('b', '$a[b]')], '$a[b]': [{ name: 'leaf', type: 'int', value: '7' }] };
|
|
const tree = new VarTree(ctxOf({ data }));
|
|
let box = tree.row(node('a', '$a'), 0);
|
|
box.rows()[0].click(); await tick();
|
|
box.rows()[1].click(); await tick();
|
|
assert.match(box.text(), /leaf/);
|
|
box = tree.row(node('a', '$a'), 0); // a redraw
|
|
await tick(); await tick();
|
|
assert.match(box.text(), /leaf/, 'opened again by itself');
|
|
tree.resetOpen();
|
|
box = tree.row(node('a', '$a'), 0);
|
|
await tick();
|
|
assert.equal(box.rows().length, 1, 'a new stop starts closed');
|
|
});
|