import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import vm from "node:vm"; const panel = readFileSync(new URL("./VaultPanel.qml", import.meta.url), "utf8"); const start = panel.indexOf(" function reconcileVisibleSelection()"); const end = panel.indexOf(" onSetupModeChanged:", start); assert.ok(start >= 0 && end > start, "selection reconciliation is present"); const vaultService = { visibleItems: [], selectedId: "stale", detail: { id: "stale", title: "stale detail" }, resultScrollPosition: 91, detailScrollPosition: 47, select(id) { this.selectedId = id; }, }; const context = { vaultService }; vm.createContext(context); vm.runInContext(panel.slice(start, end), context); context.reconcileVisibleSelection(); assert.equal(vaultService.selectedId, "", "zero matches clear stale selection"); assert.equal(Object.keys(vaultService.detail).length, 0, "zero matches clear stale details"); assert.equal(vaultService.resultScrollPosition, 0, "zero matches reset the empty result viewport"); assert.equal(vaultService.detailScrollPosition, 0, "zero matches reset the empty detail viewport"); vaultService.visibleItems = [{ id: "first" }, { id: "second" }]; vaultService.selectedId = "missing"; context.reconcileVisibleSelection(); assert.equal(vaultService.selectedId, "first", "filtering selects the first visible result"); vaultService.selectedId = "second"; context.reconcileVisibleSelection(); assert.equal(vaultService.selectedId, "second", "a still-visible selection is retained"); assert.match(panel, /property bool restoringScroll: true[\s\S]*onContentYChanged: if \(!restoringScroll\)/, "restoration does not overwrite retained scroll before layout"); assert.match(panel, /function onFrameSwapped\(\)[\s\S]*results\.restoreScroll\(\);[\s\S]*detailScroll\.restoreScroll\(\);/, "both result and detail scroll restore after a rendered layout"); assert.match(panel, /function revealItem\(item\)[\s\S]*item\.mapToItem\(resultColumn[\s\S]*onActiveChanged: if \(active\)\s*Qt\.callLater\(results\.revealItem, itemRow\)/, "keyboard selection reveals its result row"); console.log("vault-panel-repair.test.mjs: ALL PASS");