// Author: Guoliang Chen, a Senior Digital Design Engineer 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; let activeEditorChanged; /** What `lucisv.hierarchy.highlightCurrent` is set to; unset is the default. */ let highlightCurrent; /** Whether the stub's `openTextDocument` rejects, as it does for a deleted file. */ let openFails = false; 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) { if (key === 'hierarchy.duts') return configuredDuts; if (key === 'hierarchy.highlightCurrent' && highlightCurrent !== undefined) return highlightCurrent; return fallback; }, }; }, onDidChangeConfiguration(handler) { configurationChanged = handler; return disposables(); }, async openTextDocument(uri) { if (openFails) throw new Error(`cannot open ${uri.value}`); opened.push(uri.value); return { uri, lineCount: 6, lineAt(line) { return { text: `line ${line + 1}` }; }, }; }, }, window: { activeTextEditor: undefined, visibleTextEditors: [], onDidChangeActiveTextEditor(handler) { activeEditorChanged = handler; return disposables(); }, 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 theActiveFileIsLitWithoutARefetch(rendered); await persistenceSurvivesAReload(); await aFailedActionIsReportedAndADisposedViewLetsGo(); console.log('hierarchy view: OK — collapsed rows, lazy opening, search list, navigation, menu, bookmark, no preview, DUT sections (star, unstar, clear, persistence, the setting lock), the active file lit (relit in place, no refetch, the setting), a failed action reported, a disposed view releasing its listeners'); } function editorFor(value, scheme = 'file') { return { document: { uri: { scheme, toString: () => value } } }; } /// TODO §71.6: the rows leading to the active file are lit, and a new active /// file repaints only the rows whose level changed — no snapshot request, no /// render, ids untouched. async function theActiveFileIsLitWithoutARefetch(rendered) { // The bookmark from earlier paints its own background; clear it so these // rows show only the lighting. await commands.get('lucisv.hierarchy.clearBookmark')(); const plain = rendered(); assert(plain.rows.every((row) => !('lit' in row)), 'with no active file, no row carries `lit`'); const ids = plain.rows.map((row) => row.id); assert.deepEqual(plain.rows.map((row) => row.label), ['top', 'u (sub)']); const requestsBefore = requests; const rendersBefore = posted.filter((message) => message.type === 'render').length; vscode.window.visibleTextEditors = [editorFor('file:///sub.sv')]; activeEditorChanged(editorFor('file:///sub.sv')); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 1], [ids[1], 0]] }); activeEditorChanged(editorFor('file:///top.sv')); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 0], [ids[1], null]] }, 'only the two rows whose level moved'); const before = posted.length; activeEditorChanged(editorFor('file:///top.sv')); activeEditorChanged(undefined); activeEditorChanged(editorFor('output:channel', 'output')); assert.equal(posted.length, before, 'the same file, focus leaving for a panel, or the Output panel repaint nothing'); assert.equal(requests, requestsBefore, 'relighting never asks the server'); assert.equal(posted.filter((message) => message.type === 'render').length, rendersBefore, 'nor re-renders'); // A render — opening a row, a refresh — carries the level on the lit rows. await receive({ type: 'toggle', id: ids[0] }); await receive({ type: 'toggle', id: ids[0] }); assert.deepEqual(rendered().rows.map((row) => [row.label, row.lit]), [['top', 0], ['u (sub)', undefined]]); assert.deepEqual(rendered().rows.map((row) => row.id), ids, 'ids are untouched, so expansion survives'); // A file no module is declared in lights nothing. activeEditorChanged(editorFor('file:///notes.txt')); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null]] }); // The setting turns it off, and back on. activeEditorChanged(editorFor('file:///sub.sv')); highlightCurrent = false; configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.highlightCurrent' }); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null], [ids[1], null]] }); await receive({ type: 'toggle', id: ids[0] }); await receive({ type: 'toggle', id: ids[0] }); assert(rendered().rows.every((row) => !('lit' in row)), 'off means no row is lit on a render either'); highlightCurrent = undefined; configurationChanged({ affectsConfiguration: (key) => key === 'lucisv.hierarchy.highlightCurrent' }); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], 1], [ids[1], 0]] }); // Closing every editor clears it. vscode.window.visibleTextEditors = []; activeEditorChanged(undefined); assert.deepEqual(posted.at(-1), { type: 'lit', changes: [[ids[0], null], [ids[1], null]] }); } /// 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'); } /// A row action whose file cannot be opened warns instead of rejecting the /// promise VS Code drops; and a view VS Code disposes releases its own /// listeners rather than leaving them on the extension's subscriptions. async function aFailedActionIsReportedAndADisposedViewLetsGo() { state.clear(); new HierarchyView(context, client); const subscriptionsBefore = context.subscriptions.length; const disposed = []; const counted = (name) => ({ dispose() { disposed.push(name); } }); let disposeView; const posts = []; provider.resolveWebviewView({ webview: { ...webview, postMessage(message) { posts.push(message); }, onDidReceiveMessage(handler) { receive = handler; return counted('message'); }, }, onDidDispose(handler) { disposeView = handler; return counted('dispose'); }, }); assert.equal(context.subscriptions.length, subscriptionsBefore, 'the view holds its own listeners'); await receive({ type: 'ready' }); const top = posts.findLast((message) => message.type === 'render').rows[0]; assert.equal(top.label, 'top'); // Opening `top`'s definition fails, as it does when the file was deleted. openFails = true; const warnedBefore = warned.length; const outcome = await receive({ type: 'activate', id: top.id }).then(() => 'resolved', () => 'rejected'); openFails = false; assert.equal(outcome, 'resolved', 'the promise VS Code drops never rejects'); assert.equal(warned.length, warnedBefore + 1, 'one warning'); assert.equal(warned.at(-1), 'lucisv hierarchy: cannot open file:///top.sv'); disposeView(); assert.deepEqual(disposed.sort(), ['dispose', 'message'], 'both listeners disposed with the view'); } run().catch((error) => { console.error(error); process.exitCode = 1; }); // Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com