import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import vm from "node:vm"; import { createRequire } from "node:module"; const { readSource } = createRequire(import.meta.url)("./test-fixtures/sources.js"); // Execute the panels' actual JS methods and binding expressions against synthetic // services. This checks state/action logic, not Qt focus, rendering or input delivery. const read = name => readFileSync(new URL(name, import.meta.url), "utf8"); const util = vm.createContext({}); vm.runInContext(read("Util.js").replace(/^\.pragma library\s*/, ""), util); function panel(name, service, sessionState = { query: "", selectedId: "", scrollY: 0 }) { const source = readSource(`${name}Panel.qml`); const launcher = name === "Launcher"; const rowsName = launcher ? "results" : "rows"; const calls = []; const context = vm.createContext({ sessionState, stateReady: false, Util: util, launcherService: launcher ? service : null, clipboardService: launcher ? null : service, rowHeight: launcher ? 52 : 56, rowStride: launcher ? 56 : 62, Qt: { ControlModifier: 1 }, closeRequested: reason => calls.push(["close", reason]), pasteRequested: id => calls.push(["paste", id]), sessionStateChanged() {}, detailContent: {}, detailFlick: { contentY: 0, height: 120 }, }); const start = source.indexOf(" function updateState("); const end = source.indexOf(" width: parent ?", start); assert(start >= 0 && end > start, "method block exists"); vm.runInContext(source.slice(start, end), context); // Each service-state binding is re-evaluated on read, like a QML binding. const binding = (declaration, end) => { const start = source.indexOf(declaration) + declaration.length; assert(start >= declaration.length, `${declaration} exists`); const expression = source.slice(start, source.indexOf(end, start)).trim(); return { get: () => vm.runInContext(`(${expression})`, context) }; }; const serviceState = launcher ? { results: binding("readonly property var results:", "\n"), } : { rows: binding("readonly property var rows:", "\n"), privateMode: binding("readonly property bool privateMode:", "\n"), lastAction: binding("readonly property var lastAction:", "\n"), }; Object.defineProperties(context, { query: { get: () => String(sessionState.query || "") }, ...serviceState, selectedIndex: { get: () => context.selectionIndex() }, selectedRow: { get: () => context[rowsName][context.selectedIndex] || {} }, selectedActions: { get: () => context.selectedRow.actions || [] }, }); const viewport = { height: 120 }; Object.defineProperty(viewport, "contentY", { get: () => Math.max(0, Math.min(sessionState.scrollY, context[rowsName].length * context.rowStride - 4 - viewport.height)), }); context[launcher ? "resultsFlick" : "list"] = viewport; return { context, calls, source, viewport }; } const entries = Array.from({ length: 80 }, (_, i) => ({ id: `item-${i}`, title: `match ${i}`, pinned: false, actions: Array.from({ length: 8 }, (_, n) => ({ id: `action-${n}` })), })); for (const name of ["Launcher", "Clipboard"]) { const service = { entries: entries.slice(), viewRows(query) { return this.entries.filter(row => row.title.includes(query)); }, }; const state = { query: "match", selectedId: "item-20", scrollY: 1000 }; const { context: first } = panel(name, service, state); first.reconcileSelection(); assert.equal(first.selectedRow.id, "item-20"); assert.equal(state.scrollY, 1000, "initial reconciliation preserves saved scroll"); const viewportName = name === "Launcher" ? "resultsFlick" : "list"; const viewport = first[viewportName]; first[viewportName] = { contentY: 37, height: 120 }; first.rememberScroll(); assert.equal(state.scrollY, 1000, "initial layout cannot overwrite saved scroll"); first.stateReady = true; first.rememberScroll(); assert.equal(state.scrollY, 37, "user scrolling updates the retained owner"); first[viewportName] = viewport; first.select(79); assert.equal(state.selectedId, "item-79", "all rows remain selectable, beyond the old 60-row cap"); assert.equal(state.scrollY, 79 * first.rowStride + first.rowHeight - 120); first.select(-20); assert.equal(state.selectedId, "item-0"); assert.equal(state.scrollY, 0); first.select(20); const saved = { ...state }; const { context: reopened } = panel(name, service, state); reopened.reconcileSelection(); assert.equal(reopened.query, saved.query); assert.equal(reopened.selectedRow.id, saved.selectedId); assert.equal(state.scrollY, saved.scrollY, "recreated panel uses owner scroll"); service.entries = service.entries.toReversed(); reopened.reconcileSelection(); assert.equal(reopened.selectedRow.id, "item-20", "service reorder preserves selected ID"); service.entries = service.entries.filter(row => row.id !== "item-20"); reopened.reconcileSelection(); assert.equal(reopened.selectedRow.id, "item-79", "removed selection falls back to first match"); reopened[name === "Launcher" ? "setQuery" : "search"]("absent"); reopened.reconcileSelection(); assert.equal(reopened.selectedIndex, -1); assert.equal(state.selectedId, ""); assert.equal(state.query, "absent", "zero matches do not clear the query"); reopened[name === "Launcher" ? "setQuery" : "search"](""); reopened.reconcileSelection(); assert.equal(state.query, "", "explicit clear remains available"); } const writes = []; const clipboard = { entries: entries.slice(0, 3), privateMode: false, lastAction: {}, viewRows(query) { return this.entries.filter(row => row.title.includes(query)); }, stateJson() { return JSON.stringify({ rows: this.viewRows(""), privateMode: this.privateMode, lastAction: this.lastAction }); }, copy(id) { writes.push(["copy", id]); this.lastAction = { kind: "copy", id }; return this.stateJson(); }, togglePin(id) { writes.push(["pin", id]); this.entries = this.entries.map(row => row.id === id ? { ...row, pinned: !row.pinned } : row); return this.stateJson(); }, deleteRow(id) { writes.push(["delete", id]); this.entries = this.entries.filter(row => row.id !== id); return this.stateJson(); }, setPrivateMode(value) { writes.push(["private", value]); this.privateMode = value; return this.stateJson(); }, paste() { assert.fail("panel must not dispatch service paste"); }, }; const { context: clip, calls: clipCalls } = panel("Clipboard", clipboard, { query: "match 1", selectedId: "item-1", scrollY: 0 }); clip.copy("item-1"); clip.togglePin("item-1"); clip.setPrivateMode(true); assert.equal(clip.rows.length, 1, "unfiltered command return values never replace the search binding"); assert.equal(clip.rows[0].pinned, true); assert.equal(clip.privateMode, true); assert.equal(clip.lastAction.kind, "copy"); clip.paste("item-1"); clip.paste("absent"); assert.deepEqual(clipCalls, [["paste", "item-1"]], "paste is intent only and rejects missing rows"); clipboard.entries.push({ id: "new", title: "match 1 new" }); assert.equal(clip.rows.length, 2, "later captures remain reactive after actions"); clip.deleteRow("item-1"); clip.reconcileSelection(); assert.equal(clip.query, "match 1"); assert.equal(clip.selectedRow.id, "new"); assert.deepEqual(writes, [["copy", "item-1"], ["pin", "item-1"], ["private", true], ["delete", "item-1"]]); const launchWrites = []; const launcher = { viewRows: () => entries, activate: row => launchWrites.push(["activate", row.id]), activateAction: (row, index) => launchWrites.push(["action", row.id, index]), }; const launchState = { query: "match", selectedId: "", scrollY: 0, detailsExpanded: true }; const { context: launch, calls: launchCalls } = panel("Launcher", launcher, launchState); const cleared = { query: "", selectedId: "", scrollY: 0, detailsExpanded: true }; launch.select(4); launch.activateSelected(); assert.deepEqual(launchState, cleared, "a launch clears query, selection and scroll, not the disclosure"); launch.setQuery("match"); launch.select(4); launch.activateAction(7); assert.deepEqual(launchState, cleared, "a desktop action clears like a launch"); launch.setQuery("match"); launch.select(4); launch.activateAction(8); launch.keyAction({ modifiers: 1, text: "2", isAutoRepeat: true }); assert.equal(launchState.query, "match", "no dispatch keeps the query"); assert.equal(launchState.selectedId, "item-4", "no dispatch keeps the selection"); launch.keyAction({ modifiers: 1, text: "2", isAutoRepeat: false }); assert.deepEqual(launchState, cleared, "Ctrl+number clears like a launch"); assert.deepEqual(launchWrites, [["activate", "item-4"], ["action", "item-4", 7], ["action", "item-4", 1]]); assert.deepEqual(launchCalls, Array(3).fill(["close", "launcher-activated"]), "successful dispatch requests one close each"); launch.revealAction({ mapToItem: () => ({ y: 400 }), height: 42 }); assert.equal(launch.detailFlick.contentY, 322, "lower action is revealed completely"); launch.revealAction({ mapToItem: () => ({ y: 10 }), height: 42 }); assert.equal(launch.detailFlick.contentY, 10, "reverse traversal reveals upper action"); for (const name of ["Launcher", "Clipboard"]) { const { context } = panel(name, null); context.reconcileSelection(); assert.equal(context.selectedIndex, -1); context.select(0); if (name === "Launcher") { context.activateSelected(); context.activateAction(0); } else { context.copy("missing"); context.paste("missing"); context.togglePin("missing"); context.deleteRow("missing"); context.setPrivateMode(true); } } console.log("search-panels.test.mjs: state and command logic pass; Qt interaction and visual validation not run");