import { beforeEach, describe, expect, it, vi } from "vitest"; import { createChromeMock, resetChromeMock } from "../../mocks/chrome"; vi.mock("../../../src/native/port-manager", () => ({ initNativeMessaging: vi.fn(), postToNativeHost: vi.fn(), })); async function loadHandleMessage() { vi.resetModules(); (globalThis as any).chrome = createChromeMock(); const mod = await import("../../../src/service-worker/index"); return mod.handleMessage; } function exceptionDetails(error: unknown) { const text = error instanceof Error ? error.message : String(error); const description = error instanceof Error ? error.toString() : String(error); return { exceptionDetails: { text, exception: { description } } }; } /** Page-side parser check (Runtime.compileScript): compile without running. */ function compileWith(FunctionImpl: FunctionConstructor, expression: string) { try { new FunctionImpl(expression); return {}; } catch (error) { return exceptionDetails(error); } } async function evaluateWith(FunctionImpl: FunctionConstructor, expression: string) { if (expression.includes("if(!window.piHelpers)")) { return { result: { value: undefined, type: "undefined" } }; } try { const value = await new FunctionImpl(`return (${expression});`)(); return { result: { value, type: typeof value } }; } catch (error) { return exceptionDetails(error); } } /** * Emulate the page runtime. `FunctionImpl` is the constructor used for the * page side, so a test can block the service worker's global `Function` * (as the extension CSP does) while the page keeps parsing and evaluating. */ function mockRuntimeEvaluate( chrome: ReturnType, FunctionImpl: FunctionConstructor = Function, ) { chrome.debugger.sendCommand.mockImplementation( async (_target: any, method: string, params?: any) => { if (method === "Runtime.compileScript") { return compileWith(FunctionImpl, params.expression); } if (method === "Runtime.evaluate") { return evaluateWith(FunctionImpl, params.expression); } return {}; }, ); } describe("JavaScript command handlers", () => { beforeEach(() => { resetChromeMock(); }); it("falls back to statement mode when the extension CSP blocks new Function", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; // The page parser is available even when the service worker cannot eval. mockRuntimeEvaluate(chrome, Function); // Emulate `script-src 'self'`: any attempt to compile a string throws an EvalError. vi.stubGlobal("Function", function BlockedFunction() { throw new EvalError( "Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source", ); }); try { const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "const a = 40;\nconst b = 2;\nreturn a + b;", }, {}, ); expect(result).toEqual({ output: "42" }); } finally { vi.unstubAllGlobals(); } }); it("returns final expression values", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage({ type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "1 + 2" }, {}); expect(result).toEqual({ output: "3" }); }); it("refuses selected-frame execution instead of evaluating in the main frame", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, frameId: 7, code: "location.href" }, {}, ); expect(result).toEqual({ error: "JavaScript cannot run in a selected frame. Run frame.main first.", errorCode: "UNSUPPORTED_FRAME_EXECUTION", }); expect(chrome.debugger.sendCommand).not.toHaveBeenCalled(); }); it("returns multiline member chain expressions", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "({ nested: { value: 42 } })\n .nested\n .value", }, {}, ); expect(result).toEqual({ output: "42" }); }); it("returns expressions with trailing line comments", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "1 + 2 // trailing comment" }, {}, ); expect(result).toEqual({ output: "3" }); }); it("returns regex literal expressions", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "/;/.test(';')" }, {}, ); expect(result).toEqual({ output: "true" }); }); it("preserves explicit returns through SyntaxError fallback", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "return Promise.resolve('done')" }, {}, ); expect(result).toEqual({ output: '"done"' }); }); it("awaits final expression promises", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "await Promise.resolve({ ok: true })" }, {}, ); expect(result).toEqual({ output: '{\n "ok": true\n}' }); }); it("preserves runtime errors without falling back", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "missingValue.property" }, {}, ); const evaluations = chrome.debugger.sendCommand.mock.calls.filter( ([, method]: [unknown, string]) => method === "Runtime.evaluate", ); expect(result.error).toContain("ReferenceError: missingValue is not defined"); expect(evaluations).toHaveLength(2); }); it("does not fall back for runtime errors whose message mentions SyntaxError", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "(() => { throw new Error('SyntaxError') })()", }, {}, ); const evaluations = chrome.debugger.sendCommand.mock.calls.filter( ([, method]: [unknown, string]) => method === "Runtime.evaluate", ); expect(result.error).toContain("Error: SyntaxError"); expect(evaluations).toHaveLength(2); }); it("does not fall back for runtime SyntaxError objects", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "(() => { throw new SyntaxError('boom') })()" }, {}, ); const evaluations = chrome.debugger.sendCommand.mock.calls.filter( ([, method]: [unknown, string]) => method === "Runtime.evaluate", ); expect(result.error).toContain("SyntaxError: boom"); expect(evaluations).toHaveLength(2); }); it("falls back to original script on expression SyntaxError", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "const value = 42;" }, {}, ); const evaluations = chrome.debugger.sendCommand.mock.calls.filter( ([, method]: [unknown, string]) => method === "Runtime.evaluate", ); expect(result).toEqual({ output: "undefined" }); expect(evaluations).toHaveLength(3); }); it("preserves multi-statement scripts as-is through SyntaxError fallback", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "const value = 40;\nvalue + 2" }, {}, ); expect(result).toEqual({ output: "undefined" }); }); it("preserves thrown errors", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; mockRuntimeEvaluate(chrome); const result = await handleMessage( { type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "throw new Error('boom')" }, {}, ); expect(result.error).toContain("Error: boom"); }); it("preserves restricted-page errors", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.debugger.attach.mockRejectedValue(new Error("Cannot access a chrome:// URL")); const result = await handleMessage({ type: "EXECUTE_JAVASCRIPT", tabId: 1, code: "1 + 2" }, {}); expect(result).toEqual({ error: "Cannot control this page. Chrome restricts automation on chrome://, extensions, and web store pages.", }); }); }); describe("Page state handler", () => { beforeEach(() => { resetChromeMock(); }); it("reads state from the selected frame, or the main frame without one", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.scripting.executeScript.mockResolvedValue([{ result: { hasModal: true } }]); expect(await handleMessage({ type: "PAGE_STATE", tabId: 1, frameId: 7 }, {})).toEqual({ hasModal: true, }); await handleMessage({ type: "PAGE_STATE", tabId: 1 }, {}); const targets = chrome.scripting.executeScript.mock.calls.map(([call]: any[]) => call.target); expect(targets).toEqual([ { tabId: 1, frameIds: [7] }, { tabId: 1, frameIds: [0] }, ]); }); }); describe("Page HTML handler", () => { beforeEach(() => { resetChromeMock(); }); it("serializes the selected frame document with its doctype", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document"); Object.defineProperty(globalThis, "document", { configurable: true, value: { doctype: { name: "html" }, documentElement: { outerHTML: "Rendered" }, }, }); chrome.webNavigation.getAllFrames.mockResolvedValue([ { frameId: 0, url: "https://example.com" }, { frameId: 7, url: "https://frame.example.com" }, ]); chrome.scripting.executeScript.mockImplementation(async (injection: any) => [ { result: injection.func(...injection.args) }, ]); try { const switched = await handleMessage({ type: "FRAME_SWITCH", tabId: 1, index: 0 }, {}); const result = await handleMessage( { type: "GET_PAGE_HTML", tabId: 1, frameId: switched.frameId }, {}, ); expect(result).toEqual({ html: "\nRendered" }); expect(chrome.scripting.executeScript).toHaveBeenCalledWith( expect.objectContaining({ target: { tabId: 1, frameIds: [7] } }), ); } finally { if (originalDocument) { Object.defineProperty(globalThis, "document", originalDocument); } else { Reflect.deleteProperty(globalThis, "document"); } } }); it("rejects invalid export selector values", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.scripting.executeScript.mockImplementation(async (injection: any) => [ { result: injection.func(...injection.args) }, ]); await expect( handleMessage({ type: "GET_PAGE_HTML", tabId: 1, selector: false }, {}), ).rejects.toThrow("selector must be a non-empty string"); }); it("fails loudly when the requested export selector does not match", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document"); Object.defineProperty(globalThis, "document", { configurable: true, value: { querySelector: () => null }, }); chrome.scripting.executeScript.mockImplementation(async (injection: any) => [ { result: injection.func(...injection.args) }, ]); try { await expect( handleMessage({ type: "GET_PAGE_HTML", tabId: 1, selector: "#missing" }, {}), ).rejects.toThrow('No element matches selector "#missing"'); } finally { if (originalDocument) { Object.defineProperty(globalThis, "document", originalDocument); } else { Reflect.deleteProperty(globalThis, "document"); } } }); it("strips a selected script root", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document"); const script = { cloneNode: () => script, matches: (selector: string) => selector === "script", outerHTML: "", querySelectorAll: () => [], }; Object.defineProperty(globalThis, "document", { configurable: true, value: { querySelector: () => script }, }); chrome.scripting.executeScript.mockImplementation(async (injection: any) => [ { result: injection.func(...injection.args) }, ]); try { await expect( handleMessage( { type: "GET_PAGE_HTML", tabId: 1, selector: "script", stripScripts: true }, {}, ), ).resolves.toEqual({ html: "" }); } finally { if (originalDocument) { Object.defineProperty(globalThis, "document", originalDocument); } else { Reflect.deleteProperty(globalThis, "document"); } } }); it("rejects malformed page HTML extraction results", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.scripting.executeScript.mockResolvedValue([{ result: undefined }]); await expect(handleMessage({ type: "GET_PAGE_HTML", tabId: 1 }, {})).rejects.toThrow( "Page HTML extraction returned an invalid result", ); }); it("omits the doctype prefix when the document has no doctype", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; const originalDocument = Object.getOwnPropertyDescriptor(globalThis, "document"); Object.defineProperty(globalThis, "document", { configurable: true, value: { doctype: null, documentElement: { outerHTML: "" } }, }); chrome.scripting.executeScript.mockImplementation(async (injection: any) => [ { result: injection.func(...injection.args) }, ]); try { await expect(handleMessage({ type: "GET_PAGE_HTML", tabId: 1 }, {})).resolves.toEqual({ html: "", }); } finally { if (originalDocument) { Object.defineProperty(globalThis, "document", originalDocument); } else { Reflect.deleteProperty(globalThis, "document"); } } }); }); describe("Animation audit handler", () => { beforeEach(() => { resetChromeMock(); }); it("runs a bounded in-page sampler and returns the timeline", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.debugger.sendCommand.mockImplementation( async (_target: any, method: string, params?: any) => { if (method !== "Runtime.evaluate") { return {}; } expect(params.returnByValue).toBe(true); expect(params.awaitPromise).toBe(true); expect(params.expression).toContain('const selector = ".thing"'); expect(params.expression).toContain("const durationMs = 500"); expect(params.expression).toContain("const fps = 5"); expect(params.expression).toContain("const maxElements = 25"); return { result: { value: { selector: ".thing", durationMs: 500, fps: 5, sampleCount: 1, samples: [ { t: 0, timestamp: 123, elements: [ { selector: ".thing", index: 0, rect: { x: 1, y: 2, width: 3, height: 4 }, opacity: "1", transform: "none", visibility: "visible", display: "block", text: "Hello", }, ], }, ], }, type: "object", }, }; }, ); const result = await handleMessage( { type: "ANIMATE_AUDIT", tabId: 1, selector: ".thing", durationMs: 500, fps: 5 }, {}, ); expect(result).toMatchObject({ selector: ".thing", durationMs: 500, fps: 5, sampleCount: 1, }); expect(result.samples[0].elements[0]).toMatchObject({ rect: { x: 1, y: 2, width: 3, height: 4 }, opacity: "1", transform: "none", visibility: "visible", display: "block", text: "Hello", }); }); it("validates animate-audit numeric inputs in the service worker", async () => { const handleMessage = await loadHandleMessage(); await expect( handleMessage({ type: "ANIMATE_AUDIT", tabId: 1, selector: ".thing", durationMs: true }, {}), ).rejects.toThrow("duration must be a number"); await expect( handleMessage({ type: "ANIMATE_AUDIT", tabId: 1, selector: ".thing", fps: true }, {}), ).rejects.toThrow("fps must be a number"); await expect( handleMessage({ type: "ANIMATE_AUDIT", tabId: 1, selector: ".thing", durationMs: 10001 }, {}), ).rejects.toThrow("duration must be between 100 and 10000 ms"); await expect( handleMessage({ type: "ANIMATE_AUDIT", tabId: 1, selector: ".thing", fps: 31 }, {}), ).rejects.toThrow("fps must be between 1 and 30"); }); it("captures perf-audit snapshots", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.debugger.sendCommand.mockResolvedValue({ result: { value: { durationMs: 500, observedEntryTypes: ["layout-shift", "event"], trigger: { action: "click", selector: ".cta", fired: true }, summary: { cumulativeLayoutShift: 0.12, maxEventDuration: 40, counts: { layoutShifts: 1, events: 1 }, }, entries: { layoutShifts: [{ value: 0.12, hadRecentInput: false }], events: [{ name: "click", duration: 40 }], }, }, type: "object", }, }); const result = await handleMessage( { type: "PERF_AUDIT", tabId: 1, durationMs: 500, trigger: "click:.cta" }, {}, ); expect(result).toMatchObject({ durationMs: 500, observedEntryTypes: ["layout-shift", "event"], trigger: { action: "click", selector: ".cta", fired: true }, summary: { cumulativeLayoutShift: 0.12, maxEventDuration: 40 }, }); }); it("filters buffered perf-audit entries outside the audit window", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; const originalPerformanceObserver = (globalThis as any).PerformanceObserver; class FakePerformanceObserver { static supportedEntryTypes = ["layout-shift"]; callback: (list: { getEntries(): any[] }) => void; constructor(callback: (list: { getEntries(): any[] }) => void) { this.callback = callback; } observe() { const now = performance.now(); this.callback({ getEntries: () => [ { entryType: "layout-shift", name: "", startTime: now - 1000, duration: 0, value: 1, hadRecentInput: false, sources: [], }, { entryType: "layout-shift", name: "", startTime: now + 1, duration: 0, value: 0.2, hadRecentInput: false, sources: [], }, ], }); } disconnect() { return undefined; } } (globalThis as any).PerformanceObserver = FakePerformanceObserver; mockRuntimeEvaluate(chrome); try { const result = await handleMessage({ type: "PERF_AUDIT", tabId: 1, durationMs: 100 }, {}); expect(result.summary.cumulativeLayoutShift).toBe(0.2); expect(result.summary.counts.layoutShifts).toBe(1); expect(result.entries.layoutShifts).toHaveLength(1); } finally { (globalThis as any).PerformanceObserver = originalPerformanceObserver; } }); it("validates perf-audit numeric and trigger inputs in the service worker", async () => { const handleMessage = await loadHandleMessage(); await expect( handleMessage({ type: "PERF_AUDIT", tabId: 1, durationMs: true }, {}), ).rejects.toThrow("duration must be a number"); await expect( handleMessage({ type: "PERF_AUDIT", tabId: 1, durationMs: 10001 }, {}), ).rejects.toThrow("duration must be between 100 and 10000 ms"); await expect( handleMessage({ type: "PERF_AUDIT", tabId: 1, trigger: true }, {}), ).rejects.toThrow("trigger must be action:target"); }); it("returns perf-audit runtime evaluation errors", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.debugger.sendCommand.mockResolvedValue({ exceptionDetails: { text: "PerformanceObserver failed", exception: { description: "Error: PerformanceObserver failed" }, }, }); const result = await handleMessage({ type: "PERF_AUDIT", tabId: 1, durationMs: 500 }, {}); expect(result).toEqual({ error: "Error: PerformanceObserver failed" }); }); it("returns runtime evaluation errors", async () => { const handleMessage = await loadHandleMessage(); const chrome = (globalThis as any).chrome; chrome.debugger.sendCommand.mockResolvedValue({ exceptionDetails: { text: "SyntaxError", exception: { description: "DOMException: invalid selector" }, }, }); const result = await handleMessage( { type: "ANIMATE_AUDIT", tabId: 1, selector: "[", durationMs: 500, fps: 5 }, {}, ); expect(result).toEqual({ error: "DOMException: invalid selector" }); }); });