// Browser-only thread layout: the daemon cannot see duplicate rows or filtered // ancestor context. Cyclic/multi-parent graphs cannot be installed via its API. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = stripTypeScriptTypes(await readFile(new URL('../src/lib/settings/extension-tree.ts', import.meta.url), 'utf8')); const { extensionTree } = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`); const item = (id, requires = []) => ({ id, requires, description: id, enabled: true, quarantined: false, diagnostic: null }); const flatten = (branches) => branches.flatMap(branch => [branch, ...flatten(branch.children)]); test('shared dependencies retain every edge but render only one toggle per extension', () => { const input = [item('a'), item('b'), item('c', ['a', 'b']), item('d', ['c'])]; const tree = extensionTree(input, ''); const rows = flatten(tree.roots); assert.equal(rows.length, input.length); assert.equal(new Set(rows.map(row => row.item.id)).size, input.length); const edges = []; for (const row of rows) { edges.push(...row.children.map(child => `${child.item.id}:${row.item.id}`)); edges.push(...row.additional.map(parent => `${row.item.id}:${parent}`)); } assert.deepEqual(edges.sort(), ['c:a', 'c:b', 'd:c']); }); test('a requirement that needs another nests a level deeper instead of becoming a note', () => { // The daemon's own shape: files needs python and run, and run needs python. const input = [item('python'), item('files', ['python', 'run']), item('run', ['python']), item('view', ['python', 'run', 'files'])]; const tree = extensionTree(input, ''); const path = (branches, prefix = []) => branches.flatMap(branch => [[...prefix, branch.item.id].join('/'), ...path(branch.children, [...prefix, branch.item.id])]); assert.deepEqual(path(tree.roots), ['python', 'python/run', 'python/run/files', 'python/run/files/view']); assert(flatten(tree.roots).every(row => row.additional.length === 0)); }); test('filtering a leaf keeps its ancestor chain and removes unrelated roots', () => { const tree = extensionTree([item('a'), item('b', ['a']), item('leaf', ['b']), item('other')], 'leaf'); assert.equal(tree.matches, 1); assert.deepEqual(flatten(tree.roots).map(row => row.item.id), ['a', 'b', 'leaf']); assert.deepEqual(flatten(tree.roots).map(row => row.context), [true, true, false]); }); test('cyclic, self-referential and missing requirements never hide or duplicate rows', () => { const input = [item('a', ['b']), item('b', ['a']), item('self', ['self']), item('missing', ['absent'])]; const rows = flatten(extensionTree(input, '').roots); assert.deepEqual(rows.map(row => row.item.id).sort(), input.map(row => row.id).sort()); assert(rows.find(row => row.item.id === 'self').additional.includes('self')); assert(rows.find(row => row.item.id === 'missing').additional.includes('absent')); });