Files
lucisv/editors/vscode/hierarchyView_test.js
T
lucioandClaude Fable 5.1 449b874798
ci / checks (push) Successful in 1m7s
ci / corpus (push) Successful in 31s
release / build-and-publish (push) Successful in 6m20s
DUT-centred hierarchy view: star a module as DUT, its children and its parents in their own sections, everything else re-rooted
TODO §56 (docs/features/dut-hierarchy), the owner's request of 2026-09-06.

Client-only, over the flat `lucisv/moduleHierarchy` snapshot the server
already sends. `hierarchySections.js`: per starred DUT, in starred
order, the DUT's forward subtree (the DUT at the top) and an upward tree
from the DUT through every module that instantiates it (labelled `via
<instance>`, cycles cut with one `recursive` row), then one section for
every module in no DUT's subtree or ancestor closure, re-rooted where
its only parents are DUT ancestors (the review ruling — a monitor beside
the DUT under the same bench stays on screen); a module both below one
DUT and above another appears in both DUT sections; a starred name the
index lacks keeps its sections with a "not in the index" row. Star and
unstar from the webview row menu (the pane is a WebviewView, so the
menu is the renderer's, driven by a `menuTable()` per render), the
`lucisv.hierarchy.{starDut,unstarDut,clearDuts}` commands and title
actions, persistence in `workspaceState` in starred order, and the
`lucisv.hierarchy.duts` setting whose entries show locked. With nothing
starred the pane is byte-identical to before (pinned).

opentitan `hw/ip` (847 modules): `aes` → 3 sections with `tb` in its
parents and 5 modules under `aes_tb` re-rooted into others; `aes` +
`hmac` → 5 sections; every module reachable in both configurations; QA's
200-trial fuzz found no overlap and no gap. `extract_test.js` added to
`.vscodeignore`.

(Committed after the 0.48.0 version commit: the release chain broke at a
test-output grep and cut the tag one commit early; the tag is moved here.)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-06 00:04:05 -05:00

334 lines
14 KiB
JavaScript

const assert = require('assert/strict');
const Module = require('module');
const disposables = () => ({ dispose() {} });
const posted = [];
const copied = [];
const opened = [];
const shown = [];
const warned = [];
const informed = [];
const commands = new Map();
/** What `lucisv.hierarchy.duts` is set to, as a checked-in setting would be. */
let configuredDuts = [];
let picked;
let requests = 0;
let provider;
let receive;
let changed;
let configurationChanged;
class Range {
constructor(startLine, startCharacter, endLine, endCharacter) {
this.start = { line: startLine, character: startCharacter };
this.end = { line: endLine, character: endCharacter };
}
}
const vscode = {
Uri: {
parse(value) { return { value, fsPath: value.replace('file://', ''), toString: () => value }; },
joinPath(base, ...parts) {
const value = [base.value || base.toString(), ...parts].join('/');
return { value, toString: () => value };
},
},
Range,
ViewColumn: { Active: 1, Beside: 2 },
env: { clipboard: { async writeText(value) { copied.push(value); } } },
commands: {
registerCommand(id, handler) { commands.set(id, handler); return disposables(); },
},
workspace: {
asRelativePath(uri) { return uri.fsPath; },
getConfiguration(section) {
assert.equal(section, 'lucisv');
return { get(key, fallback) { return key === 'hierarchy.duts' ? configuredDuts : fallback; } };
},
onDidChangeConfiguration(handler) { configurationChanged = handler; return disposables(); },
async openTextDocument(uri) {
opened.push(uri.value);
return {
uri,
lineCount: 6,
lineAt(line) { return { text: `line ${line + 1}` }; },
};
},
},
window: {
registerWebviewViewProvider(_id, value) { provider = value; return disposables(); },
async showTextDocument(document, options) { shown.push({ document, options }); },
async showQuickPick(items) { picked = items; return items[0]; },
showWarningMessage(text) { warned.push(text); },
showInformationMessage(text) { informed.push(text); },
},
};
const originalLoad = Module._load;
Module._load = function load(request, parent, main) {
if (request === 'vscode') return vscode;
return originalLoad(request, parent, main);
};
const { HierarchyView } = require('./hierarchyView');
Module._load = originalLoad;
// The schema-2 wire: a file table and four-number ranges, no ids and no
// per-instance `definitions` — `hierarchyModel.js` rebuilds all three.
function place(file, line, start, end) {
return { file, range: [line, 0, line, end + 2], selectionRange: [line, start, line, end] };
}
const snapshot = {
schemaVersion: 2,
revision: 1,
complete: true,
files: ['file:///top.sv', 'file:///sub.sv'],
modules: [
{ moduleName: 'top', definition: place(0, 0, 7, 10), instances: [
{ instanceName: 'u', moduleName: 'sub', site: place(0, 1, 6, 9) },
] },
{ moduleName: 'sub', definition: place(1, 0, 7, 10), instances: [] },
],
};
const state = new Map();
const context = {
extensionUri: { value: 'file:///extension', toString: () => 'file:///extension' },
subscriptions: [],
workspaceState: {
get(key, fallback) { return state.has(key) ? state.get(key) : fallback; },
async update(key, value) { if (value === undefined) state.delete(key); else state.set(key, value); },
},
};
const client = {
async sendRequest(method) {
assert.equal(method, 'lucisv/moduleHierarchy');
requests += 1;
return snapshot;
},
onNotification(method, handler) {
assert.equal(method, 'lucisv/moduleHierarchyChanged');
changed = handler;
return disposables();
},
};
new HierarchyView(context, client);
assert(provider, 'the webview provider is registered');
const webview = {
cspSource: 'vscode-webview:',
options: undefined,
html: '',
asWebviewUri(uri) { return uri.toString(); },
postMessage(message) { posted.push(message); },
onDidReceiveMessage(handler) { receive = handler; return disposables(); },
};
provider.resolveWebviewView({
webview,
onDidDispose() { return disposables(); },
});
assert(webview.html.includes('Search module or instance'));
assert(webview.html.includes('Content-Security-Policy'));
async function run() {
await receive({ type: 'ready' });
const rendered = () => posted.findLast((message) => message.type === 'render');
assert(rendered(), 'ready requests and renders a snapshot');
// Owner, 2026-08-29: everything starts collapsed — one row, the root.
assert.equal(rendered().mode, 'tree');
assert.deepEqual(rendered().rows.map((row) => row.label), ['top']);
const root = rendered().rows[0];
assert.equal(root.hasChildren, true);
assert.equal(root.expanded, false);
// Opening is the user's own act, and it is the only thing that builds the
// level below.
await receive({ type: 'toggle', id: root.id });
assert.deepEqual(rendered().rows.map((row) => row.label), ['top', 'u (sub)']);
const instance = rendered().rows[1];
// A query swaps in a flat, capped result list; clearing it puts the tree
// back with the same row open and no new snapshot request.
const requestsBefore = requests;
await receive({ type: 'search', query: 'u' });
assert.equal(rendered().mode, 'search');
assert(rendered().rows.some((row) => row.label === 'u (sub)'));
assert.equal(rendered().truncated, 0);
await receive({ type: 'search', query: '' });
assert.equal(rendered().mode, 'tree');
assert.deepEqual(rendered().rows.map((row) => row.label), ['top', 'u (sub)']);
assert.equal(requests, requestsBefore, 'searching never asks the server again');
await receive({ type: 'context', action: 'copyPath', id: instance.id });
assert.deepEqual(copied, ['top.u']);
await receive({ type: 'context', action: 'instance', id: instance.id });
assert.equal(opened.at(-1), 'file:///top.sv');
assert.equal(shown.at(-1).options.viewColumn, 1);
await receive({ type: 'context', action: 'openRight', id: instance.id });
assert.equal(opened.at(-1), 'file:///sub.sv');
assert.equal(shown.at(-1).options.viewColumn, 2);
assert.equal(shown.at(-1).options.preview, false);
await receive({ type: 'context', action: 'bookmark', id: instance.id });
assert.equal(state.get('lucisv.hierarchy.bookmark'), 'top.u');
assert.equal(rendered().rows.find((row) => row.bookmarked).label, 'u (sub)');
// A search row navigates, but has no one path to copy or bookmark.
await receive({ type: 'search', query: 'u' });
const hit = rendered().rows.find((row) => row.kind === 'instance');
await receive({ type: 'context', action: 'definition', id: hit.id });
assert.equal(opened.at(-1), 'file:///sub.sv');
const copiedBefore = copied.length;
await receive({ type: 'context', action: 'copyPath', id: hit.id });
assert.equal(copied.length, copiedBefore, 'a flat hit has no full path to copy');
assert.equal(warned.at(-1), 'Clear the search to copy or bookmark an instance path.');
await receive({ type: 'search', query: '' });
// The source-line hover preview was removed at the owner's request
// (2026-08-29): a `hover` message is now one the view does not answer, and
// it must not resurrect the code that read a document behind the tree.
const before = posted.length;
await receive({ type: 'hover', id: instance.id });
assert.equal(posted.length, before, 'a hover posts nothing back');
assert(!posted.some((message) => message.type === 'preview'), 'no preview is ever posted');
assert(!webview.html.includes('id="preview"'), 'and the popover is gone from the markup');
assert.equal(typeof changed, 'function');
// ---- DUT sections (TODO §56) -------------------------------------------
// What the pane looks like with nothing starred, kept to compare against
// after an unstar: starring must be perfectly reversible.
const plainRows = rendered().rows.map((row) => ({ ...row }));
assert(plainRows.every((row) => row.contextValue === row.kind), 'nothing is a DUT yet');
assert.deepEqual(Object.keys(rendered().menus).sort(), [
'instance', 'instance.dut', 'instance.dut.locked', 'module', 'module.dut',
'module.dut.locked', 'notice', 'section',
], 'the menu table travels with every render');
// Starring from the pane's own menu, on an instance row: the module starred
// is the row's type.
await receive({ type: 'context', action: 'starDut', id: instance.id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub'], 'persisted in starred order');
assert.deepEqual(rendered().rows.map((row) => [row.label, row.depth]), [
['DUT: sub', 0],
['sub', 1],
['Parents of sub', 0],
['sub', 1],
['top', 2],
['Other modules', 0],
], 'three sections, the DUT ones opened one level, others closed');
assert.equal(rendered().rows.find((row) => row.label === 'top').detail, 'via u');
assert.equal(rendered().rows[1].contextValue, 'module.dut', 'the DUT row offers Unstar');
assert.equal(rendered().rows.at(-1).contextValue, 'section');
// A section is an ordinary collapsible row: closing it builds none of its
// tree, opening `others` shows what the exclusion left.
await receive({ type: 'toggle', id: 'sec:dut:sub' });
assert(!rendered().rows.some((row) => row.label === 'sub' && row.depth === 1
&& row.id.startsWith('dut:')), 'a closed section shows nothing under it');
await receive({ type: 'toggle', id: 'sec:dut:sub' });
await receive({ type: 'toggle', id: 'sec:others' });
assert.deepEqual(
rendered().rows.filter((row) => row.depth === 1 && !row.id.startsWith('dut:')
&& !row.id.startsWith('parents:')).map((row) => row.label),
[], 'top is above the DUT, so the others section is empty',
);
// A second DUT is two more sections, in starred order.
await receive({ type: 'context', action: 'starDut', id: rendered().rows[1].id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub']);
assert.equal(informed.at(-1), 'sub is already a DUT.');
await commands.get('lucisv.hierarchy.starDut')('top');
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub', 'top']);
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: sub', 'Parents of sub', 'DUT: top', 'Parents of top', 'Other modules'],
'two DUTs are five sections',
);
// Unstarring from the menu, and clearing the rest from the title bar, put
// the plain pane back exactly as it was.
await receive({ type: 'context', action: 'unstarDut', id: rendered().rows[1].id });
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['top']);
await commands.get('lucisv.hierarchy.clearDuts')();
assert.equal(state.get('lucisv.hierarchy.duts'), undefined);
assert.deepEqual(rendered().rows, plainRows, 'the pane is byte-identical to before the star');
// The command palette route picks from the index. Starring the same module
// again after a clear must open its sections again, not remember that it
// once seeded them.
await commands.get('lucisv.hierarchy.starDut')();
assert.deepEqual(picked, ['sub', 'top'], 'every module name, sorted');
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub']);
assert.deepEqual(rendered().rows.map((row) => row.label), [
'DUT: sub', 'sub', 'Parents of sub', 'sub', 'top', 'Other modules',
], 'the sections are open again');
await commands.get('lucisv.hierarchy.unstarDut')();
assert.deepEqual(picked, ['sub'], 'only what this workspace starred');
assert.equal(state.get('lucisv.hierarchy.duts'), undefined);
// The setting is the second way in: its DUTs come first, are locked, and the
// pane refuses to unstar them.
configuredDuts = ['top', ''];
await commands.get('lucisv.hierarchy.starDut')('sub');
configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.duts' });
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: top', 'Parents of top', 'DUT: sub', 'Parents of sub', 'Other modules'],
'the setting first, then the star; the blank entry is dropped',
);
const locked = rendered().rows.find((row) => row.label === 'top' && row.depth === 1);
assert.equal(locked.contextValue, 'module.dut.locked');
assert.match(locked.title, /lucisv\.hierarchy\.duts/);
await receive({ type: 'context', action: 'unstarDut', id: locked.id });
assert.match(warned.at(-1), /^top comes from the lucisv\.hierarchy\.duts setting/);
assert.deepEqual(state.get('lucisv.hierarchy.duts'), ['sub'], 'and nothing changed');
await commands.get('lucisv.hierarchy.clearDuts')();
assert.match(informed.at(-1), /come from the lucisv\.hierarchy\.duts setting/);
assert.deepEqual(
rendered().rows.filter((row) => row.kind === 'section').map((row) => row.label),
['DUT: top', 'Parents of top', 'Other modules'],
'clearing takes the stars, not the setting',
);
// A starred name the snapshot does not hold keeps its sections and says so.
configuredDuts = [];
await commands.get('lucisv.hierarchy.starDut')('renamed_away');
assert.deepEqual(rendered().rows.map((row) => row.label), [
'DUT: renamed_away',
'renamed_away is not in the index',
'Parents of renamed_away',
'renamed_away is not in the index',
'Other modules',
]);
await commands.get('lucisv.hierarchy.clearDuts')();
await persistenceSurvivesAReload();
console.log('hierarchy view: OK — collapsed rows, lazy opening, search list, navigation, menu, bookmark, no preview, and DUT sections (star, unstar, clear, persistence, the setting lock)');
}
/// A fresh view over the same `workspaceState`: what was starred is starred
/// again, in the order it was starred, and the sections are open on arrival.
async function persistenceSurvivesAReload() {
state.set('lucisv.hierarchy.duts', ['sub', 'nope', 7, '']);
new HierarchyView(context, client);
const reposted = [];
provider.resolveWebviewView({
webview: { ...webview, postMessage(message) { reposted.push(message); } },
onDidDispose() { return disposables(); },
});
await receive({ type: 'ready' });
const render = reposted.findLast((message) => message.type === 'render');
assert.deepEqual(render.rows.map((row) => row.label), [
'DUT: sub', 'sub', 'Parents of sub', 'sub', 'top',
'DUT: nope', 'nope is not in the index', 'Parents of nope', 'nope is not in the index',
'Other modules',
], 'both starred names come back, the number 7 and the blank do not');
}
run().catch((error) => {
console.error(error);
process.exitCode = 1;
});