import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import vm from "node:vm"; function loadAppContext(overrides = {}) { const selectors = new Map(); const body = { children: [], appendChild(node) { node.parentElement = this; this.children.push(node); return node; }, removeChild(node) { this.children = this.children.filter((child) => child !== node); node.parentElement = null; return node; } }; const document = { body, userActivation: true, execCommand(command) { if (command !== "copy") return false; if (!this.userActivation) return false; this.copiedText = this.activeElement?.value || ""; return true; }, querySelector(selector) { if (!selectors.has(selector)) { selectors.set(selector, makeElement()); } return selectors.get(selector); }, querySelectorAll() { return []; }, createElement(tagName) { const element = makeElement(tagName); element.ownerDocument = document; return element; } }; const context = { document, localStorage: { getItem() { return ""; }, setItem() {} }, navigator: {}, isSecureContext: false, location: { protocol: "http:", host: "192.168.1.10:10051" }, setTimeout() {}, clearTimeout() {}, WebSocket: { OPEN: 1 } }; Object.assign(context, overrides); const source = readFileSync(new URL("./app.js", import.meta.url), "utf8") .replace("const state = {", "globalThis.state = {") .replace(/boot\(\)\.catch\([\s\S]*?\n\}\);?\s*$/, ""); vm.runInNewContext(source, context); return context; } function makeElement(tagName = "div") { return { tagName, children: [], className: "", style: {}, value: "", parentElement: null, append(...nodes) { for (const node of nodes) { node.parentElement = this; this.children.push(node); } }, focus() { if (this.ownerDocument) this.ownerDocument.activeElement = this; }, select() { this.selected = true; }, setSelectionRange(start, end) { this.selectionStart = start; this.selectionEnd = end; }, setAttribute() {}, addEventListener() {}, querySelector() { return makeElement(); } }; } test("copyTextToClipboard falls back when navigator.clipboard is unavailable", async () => { const context = loadAppContext(); const text = '{\n "copy": "json text"\n}'; const copied = await context.copyTextToClipboard(text); assert.equal(copied, true); assert.equal(context.document.copiedText, text); }); test("copyTextToClipboard uses the synchronous fallback first on insecure remote pages", async () => { const context = loadAppContext(); context.navigator.clipboard = { async writeText() { context.document.userActivation = false; throw new Error("Clipboard API blocked on insecure context"); } }; const text = `[ { "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImFlNjc5NWRjLTRiYWUtNDE4YS04NjVkLTA4YTY0MzllZWRhNiIsInR5cCI6IkpXVCJ9..UVl-YCSs3lfN6QLMGH03qfE3pBH7YYuEV2be8hV6VQDFRX0-bMMlWvB9sE0DCnfnDSRCOw4_fh7V72fg", "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImFlNjc5NWRjLTRiYWUtNDE4YS04NjVkLTA4YTY0MzllZWRhNiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiaHR0cHM6LykJzi2DPDA1mEUCMRCyg_3tGuskm0bQlbo0_ttDv53sF-fO-7nEPHqBDO7KpKPQPvG9w3HdTXtxPktHNkcmO5453BHc61ONukP2QJUp-OBv6T2mvC4ZKq6TlgOaXxrH32sMV4VtixWDbmmlrZ0zbXD72_WMS9mWR7wQLDN21-fCyaLma0cbLuWyxSqyWvkMIeuBEnFcTZVAcrgwd1lTw0e-pdsfVGyDiqDg2ZNs_Bk7vdk8c3VVcpZA5o7SEZUEaJRN-9ncI11INQpf5BubH8zCMnFa5u2fLfvtThZ_JON75mGKPBdwJYpY5FMjscBipu95RXf4ch0PQieCSoROx21YI6z7gSvvMHL-rCc", "refresh_token": "rt.1.AACMpNpZuZ1c2Q0I9Bdcv7uo-5HKMEM3oz3XDEe_94-NGxq9TPQODzZ6SzYo66U6Ef4Efyub2ocxy4NrJRqtzz-ZthgvY6SG4PvQJYdUH9MZEwFCRsq22FTb__oE_t_QGVqGSbNIhZ2XhT8Lzvb7gI9utaKG9uH3P1zofogkRXlC3Xb8zd8okhjRl8QDFz8", "account_id": "c1f", "last_refresh": "", "email": "l, "type": "", "expired": "2Z" } ]`; const copied = await context.copyTextToClipboard(text); assert.equal(copied, true); assert.equal(context.document.copiedText, text); }); test("renderServerInfo displays configured public URLs without private LAN filtering", () => { const context = loadAppContext(); const serverAddress = context.document.querySelector("#serverAddress"); context.state.serverInfo = { urls: ["https://drop.company.lan", "http://10.20.8.138:10051"] }; context.renderServerInfo(); assert.equal(serverAddress.textContent, "https://drop.company.lan"); assert.equal(serverAddress.title, "https://drop.company.lan\nhttp://10.20.8.138:10051"); }); test("copyServerAddress copies the visible server URL", async () => { const context = loadAppContext(); const serverAddress = context.document.querySelector("#serverAddress"); serverAddress.textContent = "http://10.20.8.138:10051"; const copied = await context.copyServerAddress(); assert.equal(copied, true); assert.equal(context.document.copiedText, "http://10.20.8.138:10051"); });