import { describe, expect, it, vi } from "vitest"; import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"; const adapterData = vi.hoisted(() => ({ /** Value of a `library` variable the adapter computes asynchronously; null leaves it out. */ library: null as string | null, scopes: vi.fn(), memoryClasses: vi.fn(), /** Value the adapter answers a watch with. */ watch: "Collecting data…", })); const debugEvaluateIn = vi.hoisted(() => vi.fn(async (..._args: unknown[]) => ({ status: "ok" as const, data: { name: "result", value: "4", typeName: "int" as string | null, variablesReference: 0, evaluateName: null }, })), ); vi.mock("@/platform/ipc", async (importOriginal) => { const real = await importOriginal(); const ok = (data: T) => Promise.resolve({ status: "ok" as const, data }); return { ...real, commands: { ...real.commands, debugJvmMemoryClasses: () => { adapterData.memoryClasses(); return ok({ status: "unavailable", reason: { kind: "programExited" } }); }, debugEvaluateIn, debugSetVariable: () => ok({ name: "count", value: "4", typeName: "int", variablesReference: 0, evaluateName: "count" }), debugScopes: () => { adapterData.scopes(); return ok([{ name: "Locals", variablesReference: 1, expensive: false }]); }, debugVariables: () => ok([ ...(adapterData.library == null ? [] : [ { name: "library", value: adapterData.library, typeName: "Library", variablesReference: 0, evaluateName: "library", }, ]), { name: "count", value: "3", typeName: "int", variablesReference: 0, evaluateName: "count" }, { name: "label", value: "'hi'", typeName: "str", variablesReference: 0, evaluateName: "label" }, { name: "bar", value: "high 7.86\nlow 7.66\nName: 772, dtype: object", typeName: "Series", variablesReference: 0, evaluateName: "bar", }, ]), }, }; }); const confirmTerminate = vi.hoisted(() => vi.fn(async () => false)); vi.mock("@/workbench/confirmTerminate", () => ({ confirmTerminate })); import type { RunConfig } from "@/platform/ipc"; import { setLocale } from "@/platform/i18n"; import { handleDebugEvent, setVariableValue, useDebug, type DebugSession } from "../state"; import { DebugTabs, DebugToolWindow } from "../ui/DebugToolWindow"; import { DebuggerOverlays } from "../ui/Overlays"; import { useOverlays } from "../ui/popoverStore"; const session: DebugSession = { id: 1, name: "main", adapter: "debugpy", parent: null, launch: null, process: null, status: "paused", stoppedThread: 1, stopReason: "breakpoint", stopDescription: null, threads: [{ id: 1, name: "MainThread" }], selectedThread: 1, frames: [{ id: 7, name: "main", path: "/p/app/main.py", line: 11, column: 0, presentationHint: null }], selectedFrame: 7, hits: 1, stops: 0, exitCode: null, caps: null, }; describe("debug tool window", () => { it.each(["gdb", "debugpy"])("evaluates expressions in the variable field and dialog under %s", async (adapter) => { debugEvaluateIn.mockClear(); useDebug.setState({ sessions: { 1: { ...session, adapter } }, active: 1 }); const { unmount } = render(); fireEvent.click(screen.getByRole("tab", { name: "Threads & Variables" })); const field = screen.getByPlaceholderText(/Evaluate expression/); fireEvent.change(field, { target: { value: "count + 1" } }); fireEvent.keyDown(field, { key: "Enter" }); const context = adapter === "gdb" ? "watch" : "repl"; await vi.waitFor(() => expect(debugEvaluateIn).toHaveBeenCalledWith(1, 7, "count + 1", context)); unmount(); debugEvaluateIn.mockClear(); useOverlays.setState({ evaluate: { expression: "count + 1" } }); const dialog = render(); fireEvent.click(screen.getByRole("button", { name: "Evaluate" })); await vi.waitFor(() => expect(debugEvaluateIn).toHaveBeenCalledWith(1, 7, "count + 1", context)); dialog.unmount(); useOverlays.setState({ evaluate: null }); }); it("offers Hot Reload and Hot Restart for Dart and Flutter sessions only", () => { const hot = () => ["debug.hotReload", "debug.hotRestart"].map((name) => !!screen.queryByRole("button", { name })); useDebug.setState({ sessions: { 1: { ...session, status: "running" } }, active: 1 }); const { unmount } = render(); expect(hot()).toEqual([false, false]); unmount(); useDebug.setState({ sessions: { 1: { ...session, adapter: "dart", status: "running" } }, active: 1 }); render(); expect(hot()).toEqual([true, true]); }); it("shows the Memory tab only for sessions with jvmMemory", async () => { const withCaps = (jvmMemory: boolean) => ({ ...session, caps: { jvmMemory } as DebugSession["caps"] }); useDebug.setState({ sessions: { 1: withCaps(false) }, active: 1 }); const { unmount } = render(); expect(screen.queryByRole("tab", { name: "Memory" })).toBeNull(); unmount(); useDebug.setState({ sessions: { 1: withCaps(true) }, active: 1 }); render(); const memory = screen.getByRole("tab", { name: "Memory" }); fireEvent.click(memory); expect(memory.getAttribute("aria-selected")).toBe("true"); expect(await screen.findByText("The program has exited.")).toBeTruthy(); // Switching to a session without the capability leaves the tab it no longer has. act(() => useDebug.setState({ sessions: { 1: withCaps(false) } })); expect(screen.queryByText("The program has exited.")).toBeNull(); expect(screen.getByRole("tab", { name: "Threads & Variables" }).getAttribute("aria-selected")).toBe("true"); }); it("shows the Memory tab for a java-debug attach session", () => { const attach = { ...session, adapter: "java-debug", launch: { request: "attach" } as DebugSession["launch"], caps: { jvmMemory: true } as DebugSession["caps"], }; useDebug.setState({ sessions: { 1: attach }, active: 1 }); render(); expect(screen.getByRole("tab", { name: "Memory" })).toBeTruthy(); }); it("keeps the Memory tab selected when the session pauses", async () => { const caps = { jvmMemory: true } as DebugSession["caps"]; useDebug.setState({ sessions: { 1: { ...session, caps, status: "running" } }, active: 1 }); render(); const memory = screen.getByRole("tab", { name: "Memory" }); fireEvent.click(memory); expect(await screen.findByText("The program has exited.")).toBeTruthy(); act(() => useDebug.setState({ sessions: { 1: { ...session, caps, status: "paused" } } })); expect(memory.getAttribute("aria-selected")).toBe("true"); expect(screen.getByText("The program has exited.")).toBeTruthy(); }); it("renames the view tabs when the locale switches to Chinese", async () => { const caps = { jvmMemory: true } as DebugSession["caps"]; useDebug.setState({ sessions: { 1: { ...session, caps } }, active: 1 }); render(); expect(screen.getByRole("tab", { name: "Memory" })).toBeTruthy(); try { await act(() => setLocale("zh")); const tabs = screen.getAllByRole("tab").map((t) => t.textContent); expect(tabs).toEqual(["线程和变量", "控制台", "内存"]); } finally { await act(() => setLocale("en")); } }); it("says a PHP Remote Debug session listens for Xdebug while nothing is suspended", () => { const runConfig = { id: "php-remote:web", name: "web", kind: "php-remote", command: "", args: [], cwd: null, env: {}, detected: false, isTest: false, debugAdapter: "php-debug", params: { port: "9010" }, } as RunConfig; const waiting = { ...session, adapter: "php-debug", status: "running" as const, threads: [], frames: [] }; useDebug.setState({ sessions: { 1: { ...waiting, runConfig } }, active: 1 }); const { unmount } = render(); fireEvent.click(screen.getByRole("tab", { name: "Threads & Variables" })); expect(screen.getByText("Listening for Xdebug on port 9010")).toBeTruthy(); expect(screen.queryByText("Running…")).toBeNull(); unmount(); useDebug.setState({ sessions: { 1: waiting }, active: 1 }); render(); fireEvent.click(screen.getByRole("tab", { name: "Threads & Variables" })); expect(screen.getByText("Running…")).toBeTruthy(); }); it("shows preparation output, preserves a chosen tab and opens variables on pause", () => { useDebug.setState({ sessions: { 1: { ...session, status: "starting" } }, active: 1 }); render(); const console = screen.getByRole("tab", { name: "Console" }); const variables = screen.getByRole("tab", { name: "Threads & Variables" }); expect(console.getAttribute("aria-selected")).toBe("true"); fireEvent.click(variables); expect(variables.getAttribute("aria-selected")).toBe("true"); fireEvent.click(console); act(() => useDebug.setState({ sessions: { 1: { ...session, status: "running" } } })); expect(console.getAttribute("aria-selected")).toBe("true"); act(() => useDebug.setState({ sessions: { 1: session } })); expect(variables.getAttribute("aria-selected")).toBe("true"); // Stepping from the Console keeps it in front. fireEvent.click(console); act(() => useDebug.setState({ sessions: { 1: { ...session, status: "running" } } })); act(() => useDebug.setState({ sessions: { 1: { ...session, stopReason: "step" } } })); expect(console.getAttribute("aria-selected")).toBe("true"); }); it("renders session tabs, views, frames and typed variables", async () => { useDebug.setState({ sessions: { 1: session }, active: 1 }); render( <> , ); expect(screen.getByRole("tab", { name: /main/ })).toBeTruthy(); expect(screen.getByRole("tab", { name: "Threads & Variables" })).toBeTruthy(); expect(screen.getByRole("tab", { name: "Console" })).toBeTruthy(); expect(screen.getByText(", main.py:12")).toBeTruthy(); expect(screen.getByPlaceholderText("Evaluate expression (↩) or add a watch (⇧⌘↩)")).toBeTruthy(); expect(await screen.findByText("{int}")).toBeTruthy(); expect(screen.getByText("'hi'")).toBeTruthy(); // A multi-line rendering stays on its row. expect(screen.getByText(/dtype: object/).textContent).toBe("high 7.86 low 7.66 Name: 772, dtype: object"); }); it("closes a session tab and moves the selection to the one left", async () => { useDebug.setState({ sessions: { 1: { ...session, status: "terminated" }, 2: { ...session, id: 2, name: "app", status: "terminated" }, }, active: 1, }); render(); await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!)); expect(screen.getAllByRole("tab").map((tab) => tab.textContent)).toEqual(["app"]); expect(useDebug.getState().active).toBe(2); expect(useDebug.getState().sessions).not.toHaveProperty("1"); }); it("passes the selection of a closed tab to the tab on its left", async () => { useDebug.setState({ sessions: { 4: { ...session, id: 4, name: "a", status: "terminated" }, 5: { ...session, id: 5, name: "b", status: "terminated" }, 6: { ...session, id: 6, name: "c", status: "terminated" }, }, active: 5, }); render(); await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[1]!)); expect(useDebug.getState().active).toBe(4); }); it("keeps a live session when the user doesn't terminate it", async () => { useDebug.setState({ sessions: { 8: { ...session, id: 8, name: "server" } }, active: 8 }); render(); await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!)); expect(confirmTerminate).toHaveBeenCalledWith("server"); expect(useDebug.getState().sessions).toHaveProperty("8"); }); it("leaves nothing selected once the last tab is closed", async () => { useDebug.setState({ sessions: { 3: { ...session, id: 3, status: "terminated" } }, active: 3 }); render(); await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!)); expect(useDebug.getState().active).toBeNull(); expect(useDebug.getState().sessions).toEqual({}); }); }); describe("variables the adapter computes after the stop", () => { const tick = () => act(() => new Promise((resolve) => setTimeout(resolve, 0))); const setup = () => { adapterData.library = "Collecting data…"; adapterData.scopes.mockClear(); useDebug.setState({ sessions: { 1: session }, active: 1, stopSeq: 1, variablesEpoch: 0 }); return render(); }; it("shows the value sent after an invalidation, unmarked as changed", async () => { setup(); expect(await screen.findByText("Collecting data…")).toBeTruthy(); adapterData.library = "com.example.library.Library@1"; act(() => handleDebugEvent(1, { kind: "invalidated", areas: ["variables"] })); const value = await screen.findByText("com.example.library.Library@1"); expect(screen.queryByText("Collecting data…")).toBeNull(); expect(value.closest("[role=treeitem]")?.querySelector("[class*=changed]")).toBeNull(); }); it("shows the watch value the adapter computes after an invalidation", async () => { adapterData.watch = "Collecting data…"; const answer = async (...args: unknown[]) => ({ status: "ok" as const, data: { name: String(args[2]), value: adapterData.watch, typeName: null as string | null, variablesReference: 0, evaluateName: null, }, }); debugEvaluateIn.mockImplementation(answer); useDebug.setState({ watches: ['library.search("java").size'] }); try { setup(); const watch = () => screen.getByText('library.search("java").size').closest("[role=treeitem]")!; await waitFor(() => expect(watch().textContent).toContain("Collecting data…")); adapterData.watch = "1"; act(() => handleDebugEvent(1, { kind: "invalidated", areas: ["variables"] })); await waitFor(() => expect(watch().textContent).not.toContain("Collecting data…")); expect(watch().textContent).toContain("1"); } finally { debugEvaluateIn.mockImplementation(async () => ({ status: "ok" as const, data: { name: "result", value: "4", typeName: "int", variablesReference: 0, evaluateName: null }, })); useDebug.setState({ watches: [] }); } }); it("reloads after Set Value", async () => { setup(); await screen.findByText("Collecting data…"); adapterData.library = "Library@2"; await act(() => setVariableValue({ container: 1, name: "count", evaluateName: "count" }, "4", session)); expect(await screen.findByText("Library@2")).toBeTruthy(); }); it("does nothing for an invalidation while the program runs", async () => { setup(); await screen.findByText("Collecting data…"); act(() => useDebug.setState({ sessions: { 1: { ...session, status: "running" } } })); adapterData.scopes.mockClear(); adapterData.library = "Library@3"; act(() => handleDebugEvent(1, { kind: "invalidated", areas: ["variables"] })); await tick(); expect(adapterData.scopes).not.toHaveBeenCalled(); expect(useDebug.getState().variablesEpoch).toBe(0); }); it("does not ask the Memory view again", async () => { const caps = { jvmMemory: true } as DebugSession["caps"]; useDebug.setState({ sessions: { 1: { ...session, caps } }, active: 1, variablesEpoch: 0 }); render(); fireEvent.click(screen.getByRole("tab", { name: "Memory" })); expect(await screen.findByText("The program has exited.")).toBeTruthy(); adapterData.memoryClasses.mockClear(); act(() => handleDebugEvent(1, { kind: "invalidated", areas: ["variables"] })); await tick(); expect(useDebug.getState().variablesEpoch).toBe(1); expect(adapterData.memoryClasses).not.toHaveBeenCalled(); }); });