import { beforeEach, describe, expect, it, vi } from "vitest"; import type { LaunchConfig } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { browserLaunch } from "../browser"; import { appendConsoleQueued, handleDebugEvent, resetSessions, startBrowserSession, startSession, useDebug, } from "../state"; const ipc = vi.hoisted(() => { let next = 0; return { reset: () => (next = 0), /** Addresses that answer with an HTML document. */ pages: new Set(), debugStart: vi.fn(async (_launch: unknown) => ({ status: "ok", data: ++next })), debugStop: vi.fn(async (_id: number) => ({ status: "ok", data: null })), debugEvaluateIn: vi.fn(async (..._args: unknown[]) => ({ status: "ok", data: { value: "", variablesReference: 0 }, })), webServesPage: vi.fn(async (url: string) => ({ status: "ok", data: ipc.pages.has(url) })), webBrowsers: vi.fn(async () => ({ status: "ok", data: [{ id: "chrome" }] })), }; }); vi.mock("@/platform/ipc", async (original) => { const real = await original(); const { reset: _reset, pages: _pages, ...commands } = ipc; return { ...real, isTauri: () => true, commands: { ...real.commands, ...commands } }; }); const launched = () => ipc.debugStart.mock.calls.map(([launch]) => JSON.parse((launch as LaunchConfig).extraJson!)); /** The adapter's own session for the page of browser launch `parent`. */ const pageOf = (parent: number, id: number) => handleDebugEvent(id, { kind: "started", name: "page", adapter: "js-debug", parent }); const server: LaunchConfig = { name: "dev", adapter: "js-debug", request: "launch", program: null, args: [], cwd: "/p", env: {}, extraJson: JSON.stringify({ type: "pwa-node", runtimeExecutable: "pnpm", runtimeArgs: ["dev"] }), }; const prints = async (session: number, text: string) => { appendConsoleQueued(session, "stdout", text); // Output reaches the console in batches; the address is asked after that. await new Promise((resolve) => setTimeout(resolve, 150)); }; beforeEach(() => { // Nothing live is left to stop: session ids start over, as after the backend restarts. useDebug.setState({ sessions: {}, active: null }); resetSessions(); ipc.reset(); ipc.pages.clear(); for (const mock of [ipc.debugStart, ipc.debugStop, ipc.debugEvaluateIn, ipc.webServesPage]) mock.mockClear(); useProject.setState({ project: { root: "/p" } as ReturnType["project"], settings: null, }); }); /** Where the page goes when it runs `expression`; null when it stays. Tracks the address. */ let pageAt = "http://localhost:5173/"; function run(expression: string): string | null { let went: string | null = null; const location = { get href() { return pageAt; }, assign: (url: string) => { went = url; pageAt = url; }, }; // The page's own globals may shadow the browser's (`const URL = "/api"`). new Function("location", "URL", `return ${expression}`)(location, "/api"); return went; } describe("the browser of a project under the debugger", () => { it("is started once: a second launch sends its page to the new address and selects it", async () => { const first = await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/p")); pageOf(first!, 2); expect(useDebug.getState().active).toBe(2); useDebug.setState({ active: null }); const again = await startBrowserSession(browserLaunch("b", "http://localhost:5173/cart", "chrome", "/p")); expect(again).toBe(first); expect(ipc.debugStart).toHaveBeenCalledTimes(1); const sentTo = () => ipc.debugEvaluateIn.mock.calls.map(([, , expression]) => run(String(expression))); expect(sentTo()).toEqual(["http://localhost:5173/cart"]); expect(useDebug.getState().active).toBe(2); // Back to the address it was started on: the page is at /cart now, so it goes back. ipc.debugEvaluateIn.mockClear(); await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/p")); expect(sentTo()).toEqual(["http://localhost:5173/"]); // A page already at the address stays as it is. ipc.debugEvaluateIn.mockClear(); await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/p")); expect(sentTo()).toEqual([null]); }); it("is another one for another browser, another project, or after the first has ended", async () => { const first = await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/p")); pageOf(first!, 2); await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "edge", "/p")); await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/other")); expect(launched().map((l) => l.type)).toEqual(["pwa-chrome", "pwa-msedge", "pwa-chrome"]); handleDebugEvent(first!, { kind: "terminated" }); await startBrowserSession(browserLaunch("a", "http://localhost:5173/", "chrome", "/p")); expect(ipc.debugStart).toHaveBeenCalledTimes(4); }); }); describe("a debugged program that serves a page", () => { const settings = (debugServerPage: boolean) => useProject.setState({ settings: { debugger: { debugServerPage } } as ReturnType["settings"], }); beforeEach(() => settings(true)); it("gets a browser session on the first address that answers with a page, once", async () => { ipc.pages.add("http://localhost:5173/"); const dev = await startSession(server); await prints(dev!, "api on http://localhost:3000/api\n Local: http://localhost:5173/\n"); expect(ipc.webServesPage.mock.calls.map(([url]) => url)).toEqual([ "http://localhost:3000/api", "http://localhost:5173/", ]); expect(launched().slice(1)).toMatchObject([{ type: "pwa-chrome", url: "http://localhost:5173/", webRoot: "/p" }]); await prints(dev!, "page reloaded, see http://localhost:5173/ and http://localhost:5173/admin\n"); expect(ipc.debugStart).toHaveBeenCalledTimes(2); expect(ipc.webServesPage).toHaveBeenCalledTimes(2); }); it("gets none when no address is a page, and is not asked without end", async () => { const dev = await startSession(server); const many = Array.from({ length: 8 }, (_, i) => `http://localhost:${4000 + i}/`).join(" "); await prints(dev!, `listening: ${many}\n`); expect(ipc.webServesPage).toHaveBeenCalledTimes(5); expect(ipc.debugStart).toHaveBeenCalledTimes(1); }); it.each([ ["turned off in the settings", () => settings(false)], ["left at the default", () => useProject.setState({ settings: null })], ])("gets none when that is %s", async (_, configure) => { configure(); ipc.pages.add("http://localhost:5173/"); const dev = await startSession(server); await prints(dev!, "Local: http://localhost:5173/\n"); expect(ipc.webServesPage).not.toHaveBeenCalled(); expect(ipc.debugStart).toHaveBeenCalledTimes(1); }); it("takes its page along when it stops", async () => { ipc.pages.add("http://localhost:5173/"); const dev = await startSession(server); await prints(dev!, "Local: http://localhost:5173/\n"); expect(ipc.debugStart).toHaveBeenCalledTimes(2); handleDebugEvent(dev!, { kind: "terminated" }); await vi.waitFor(() => expect(ipc.debugStop).toHaveBeenCalledWith(2)); }); it("leaves a browser running that was already being debugged", async () => { ipc.pages.add("http://localhost:5173/"); const own = await startBrowserSession(browserLaunch("mine", "http://localhost:5173/", "chrome", "/p")); pageOf(own!, 2); const dev = await startSession(server); await prints(dev!, "Local: http://localhost:5173/\n"); // The server's page is shown in that browser; no second one starts. expect(launched().map((l) => l.type)).toEqual(["pwa-chrome", "pwa-node"]); handleDebugEvent(dev!, { kind: "terminated" }); await new Promise((resolve) => setTimeout(resolve, 20)); expect(ipc.debugStop).not.toHaveBeenCalled(); }); it("is left alone when it is not a Node.js program", async () => { ipc.pages.add("http://localhost:8000/"); const python = await startSession({ ...server, adapter: "debugpy", extraJson: null }); await prints(python!, "Serving on http://localhost:8000/\n"); expect(ipc.webServesPage).not.toHaveBeenCalled(); }); });