fix: harden hierarchy keys and virtual navigation

This commit is contained in:
2026-08-20 01:26:00 +00:00
parent 8d256cd4ab
commit 3b5e2acb75
4 changed files with 255 additions and 56 deletions
+76 -16
View File
@@ -33,18 +33,19 @@ function baseFixture() {
test('builds deterministic hierarchy with explicit exceptional buckets and diagnostics', () => {
const model = buildDeviceTree(baseFixture());
const camera = (id) => [...model.cameraByKey.values()].find((entry) => entry.canonicalId === id);
assert.deepEqual(model.sites.map((site) => site.name), ['Alpha', 'Orphaned cameras', 'Unknown site: missing-s', 'Zulu']);
assert.equal(model.sites[0].pendingDeletion, true);
assert.deepEqual(model.sites[0].groups.map((group) => group.name), ['Doors', 'Lobby', 'Ungrouped', 'Unknown group: missing-g']);
assert.equal(model.sites[0].groups.find((group) => group.id === 'g1').pendingDeletion, true);
assert.deepEqual(model.sites[3].groups[0].cameras.map((camera) => camera.name), ['Inferred']);
const conflict = model.cameraByKey.get('camera:c3');
const conflict = camera('c3');
assert.equal(conflict.site.id, 's1');
assert.equal(conflict.group.name, 'Doors');
assert.equal(conflict.hierarchyStatus, 'conflict');
assert.equal(model.cameraByKey.get('camera:c2').hierarchyStatus, 'inferred-site');
assert.ok(model.diagnostics.some((entry) => entry.code === 'site-group-conflict' && entry.key === 'camera:c3'));
assert.equal(camera('c2').hierarchyStatus, 'inferred-site');
assert.ok(model.diagnostics.some((entry) => entry.code === 'site-group-conflict' && entry.key === conflict.key));
});
test('duplicate and malformed IDs get stable unique keys and diagnostics', () => {
@@ -67,44 +68,49 @@ test('duplicate and malformed IDs get stable unique keys and diagnostics', () =>
test('sites start collapsed; expansion produces levels and complete ARIA metadata', () => {
const model = buildDeviceTree(baseFixture());
let rows = flattenVisibleRows(model, { expandedKeys: new Set(), selectedKey: 'camera:c1' });
const site = model.sites.find((entry) => entry.canonicalId === 's1' && !entry.synthetic);
const group = site.groups.find((entry) => entry.canonicalId === 'g1' && !entry.synthetic);
const selected = [...model.cameraByKey.values()].find((entry) => entry.canonicalId === 'c1');
let rows = flattenVisibleRows(model, { expandedKeys: new Set(), selectedKey: selected.key });
assert.equal(rows.length, 4);
assert.ok(rows.every((row) => row.kind === 'site' && row.aria.level === 1 && row.aria.expanded === false));
assert.ok(rows.every((row) => row.aria.setsize === 4));
assert.equal(rows.hiddenSelected, true);
rows = flattenVisibleRows(model, { expandedKeys: new Set(['site:s1']) });
rows = flattenVisibleRows(model, { expandedKeys: new Set([site.key]) });
assert.deepEqual(rows.slice(0, 5).map((row) => row.kind), ['site', 'group', 'group', 'group', 'group']);
assert.ok(rows.slice(1, 5).every((row) => row.aria.level === 2 && row.aria.expanded === false));
rows = flattenVisibleRows(model, {
expandedKeys: new Set(['site:s1', 'group:s1:g1']),
selectedKey: 'camera:c1',
expandedKeys: new Set([site.key, group.key]),
selectedKey: selected.key,
});
const camera = rows.find((row) => row.key === 'camera:c1');
assert.equal(camera.parentKey, 'group:s1:g1');
const camera = rows.find((row) => row.key === selected.key);
assert.equal(camera.parentKey, group.key);
assert.deepEqual(camera.aria, { level: 3, expanded: undefined, selected: true, posinset: 1, setsize: 1 });
assert.equal(rows.hiddenSelected, false);
});
test('search covers camera fields and ancestors without mutating saved expansion', async () => {
const model = buildDeviceTree(baseFixture());
const site = (id) => model.sites.find((entry) => entry.canonicalId === id && !entry.synthetic);
const camera = (id) => [...model.cameraByKey.values()].find((entry) => entry.canonicalId === id);
const scheduled = [];
const runner = createSearchRunner({ scheduler: (work) => scheduled.push(work), chunkSize: 2 });
const saved = new Set(['site:s2']);
const saved = new Set([site('s2').key]);
const promise = runner.search(model, 'alpha lobby 10.0.0.1', { expandedKeys: saved });
while (scheduled.length) scheduled.shift()();
const result = await promise;
assert.equal(result.count, 1);
assert.deepEqual([...result.cameraKeys], ['camera:c1']);
assert.ok(result.expandedKeys.has('site:s1'));
assert.ok(result.expandedKeys.has('group:s1:g1'));
assert.deepEqual([...saved], ['site:s2']);
assert.deepEqual([...result.cameraKeys], [camera('c1').key]);
assert.ok(result.expandedKeys.has(site('s1').key));
assert.ok(result.expandedKeys.has(camera('c1').group.key));
assert.deepEqual([...saved], [site('s2').key]);
const ancestorPromise = runner.search(model, 'zulu', { expandedKeys: new Set() });
while (scheduled.length) scheduled.shift()();
const ancestor = await ancestorPromise;
assert.deepEqual([...ancestor.cameraKeys], ['camera:c2']);
assert.deepEqual([...ancestor.cameraKeys], [camera('c2').key]);
});
test('new search cancels stale chunked generation', async () => {
@@ -132,7 +138,9 @@ test('virtual window remains structurally bounded for 5,000 expanded shuffled ca
[devices[i], devices[j]] = [devices[j], devices[i]];
}
const model = buildDeviceTree({ sites: [{ id: 's', name: 'Large' }], groups: [], devices });
const rows = flattenVisibleRows(model, { expandedKeys: new Set(['site:s', 'group:s:__ungrouped__']) });
const rows = flattenVisibleRows(model, {
expandedKeys: new Set([model.sites[0].key, model.sites[0].groups[0].key]),
});
const window = calculateVirtualWindow(rows, { scrollTop: 50000, viewportHeight: 320, rowHeight: 28 });
assert.ok(window.rows.length >= 40 && window.rows.length <= 50, `mounted ${window.rows.length}`);
assert.equal(window.totalHeight, rows.length * 28);
@@ -146,3 +154,55 @@ test('1,500-site/group fixture has deterministic stable ordering', () => {
const namesAndIds = model.sites.map((site) => `${site.normalizedName}|${site.id}`);
assert.deepEqual(namesAndIds, [...namesAndIds].sort((a, b) => a.localeCompare(b)));
});
test('tenant and synthetic namespaces stay distinct for reserved and ambiguous IDs', () => {
const model = buildDeviceTree({
sites: [
{ id: '__orphaned__', name: 'Tenant Reserved Site' },
{ id: '__unknown_site__:missing', name: 'Tenant Unknown-shaped Site' },
{ id: 'a', name: 'A' },
{ id: 'a:b', name: 'A colon B' },
{ id: 'percent%:site', name: 'Percent Site' },
],
groups: [
{ id: '__ungrouped__', name: 'Tenant Reserved Group', parentId: '__orphaned__' },
{ id: 'b:c', name: 'Tuple One', parentId: 'a' },
{ id: 'c', name: 'Tuple Two', parentId: 'a:b' },
{ id: 'g:%', name: 'Escaped Group', parentId: 'percent%:site' },
],
devices: [
{ id: 'tenant-group', siteId: '__orphaned__', deviceGroupId: '__ungrouped__' },
{ id: 'synthetic-group', siteId: '__orphaned__' },
{ id: 'orphan' },
{ id: 'unknown-site', siteId: 'missing' },
{ id: 'tuple-one', siteId: 'a', deviceGroupId: 'b:c' },
{ id: 'tuple-two', siteId: 'a:b', deviceGroupId: 'c' },
{ id: 'escaped', siteId: 'percent%:site', deviceGroupId: 'g:%' },
],
});
const tenantReserved = model.sites.find((site) => !site.synthetic && site.id === '__orphaned__');
const orphanBucket = model.sites.find((site) => site.synthetic && site.hierarchyStatus === 'orphan');
const unknownBucket = model.sites.find((site) => site.synthetic && site.hierarchyStatus === 'unknown-site');
assert.ok(tenantReserved && orphanBucket && unknownBucket);
assert.notEqual(tenantReserved.key, orphanBucket.key);
const camera = (id) => [...model.cameraByKey.values()].find((entry) => entry.canonicalId === id);
assert.notEqual(camera('tenant-group').group.key, camera('synthetic-group').group.key);
assert.notEqual(camera('tuple-one').group.key, camera('tuple-two').group.key);
assert.equal(model.rowByKey.size, model.sites.length +
model.sites.reduce((count, site) => count + site.groups.length, 0) + model.cameraCount);
assert.ok([...model.rowByKey.keys()].every((key) => !/\s/.test(key)));
});
test('virtual window clamps an obsolete scroll offset after rows shrink', () => {
const rows = [{ key: 'only-row' }];
const window = calculateVirtualWindow(rows, {
scrollTop: 10000,
viewportHeight: 320,
rowHeight: 28,
overscan: 0,
});
assert.deepEqual(window.rows, rows);
assert.equal(window.startIndex, 0);
assert.equal(window.endIndex, 1);
});