Файли
openidea/src/features/debugger/__tests__/toolWindow.test.tsx
T
ntbang0901 a3d1e57f3e feat(debugger): memory view for a paused JVM (#2648)
Adds a Memory tab to the Debug tool window for JVM sessions: loaded classes with instance counts and shallow sizes (with deltas between refreshes), read over the HotSpot attach listener (`GC.class_histogram`), falling back to counting reachable objects through the debugger on JDKs that cannot answer while suspended. While paused, a class's instances, an instance's fields and its referrers are listed through a JDWP link (the Kotlin adapter's path, and a jdtls bundle `java/jvm-inspect` for java-debug). Debug launches started by the IDE get `-XX:+StartAttachListener` unless the user's VM options decide otherwise (not on Windows).

New crates: ide-jdwp, ide-jvm-attach, ide-jvm-inspect. The bundled jar rebuilds byte-for-byte from build.py against the pinned JDT LS plugins.

Co-authored-by: ntbang0901 <[email protected]>
2026-11-11 17:10:21 +08:00

267 рядки
12 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
const debugEvaluateIn = vi.hoisted(() =>
vi.fn(async () => ({
status: "ok" as const,
data: { name: "result", value: "4", typeName: "int", variablesReference: 0, evaluateName: null },
})),
);
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
const ok = <T,>(data: T) => Promise.resolve({ status: "ok" as const, data });
return {
...real,
commands: {
...real.commands,
debugJvmMemoryClasses: () => ok({ status: "unavailable", reason: { kind: "programExited" } }),
debugEvaluateIn,
debugScopes: () => ok([{ name: "Locals", variablesReference: 1, expensive: false }]),
debugVariables: () =>
ok([
{ 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 { 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(<DebugToolWindow />);
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(<DebuggerOverlays />);
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(<DebugToolWindow />);
expect(hot()).toEqual([false, false]);
unmount();
useDebug.setState({ sessions: { 1: { ...session, adapter: "dart", status: "running" } }, active: 1 });
render(<DebugToolWindow />);
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(<DebugToolWindow />);
expect(screen.queryByRole("tab", { name: "Memory" })).toBeNull();
unmount();
useDebug.setState({ sessions: { 1: withCaps(true) }, active: 1 });
render(<DebugToolWindow />);
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(<DebugToolWindow />);
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(<DebugToolWindow />);
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(<DebugToolWindow />);
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(<DebugToolWindow />);
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(<DebugToolWindow />);
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(<DebugToolWindow />);
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(
<>
<DebugTabs />
<DebugToolWindow />
</>,
);
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(<DebugTabs />);
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(<DebugTabs />);
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(<DebugTabs />);
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(<DebugTabs />);
await act(async () => fireEvent.click(screen.getAllByRole("button", { name: "Close Tab" })[0]!));
expect(useDebug.getState().active).toBeNull();
expect(useDebug.getState().sessions).toEqual({});
});
});