Файли
openidea/src/features/debugger/__tests__/unityAttach.test.tsx
T
itcodesтаsliencio aa8822d9a3 feat(debugger): attach to a running Unity Editor (#2919)
Unity projects get a debug-only "Unity Editor" run configuration, selected by default. Debug opens a picker of local Unity Editor processes with the current project first; Run > Attach to Unity Process is also available. The tool installer fetches the pinned Unity Debugger 3.0.1 adapter with SHA-256 verification, and the session reports ready once the Mono runtime is up. A missing Mono executable on macOS and Linux is reported in the picker.

Refs #1436

Co-authored-by: sliencio <[email protected]>
2026-11-16 10:16:50 +08:00

143 рядки
5.5 KiB
TypeScript

import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { AttachableProcess, ProjectInfo, RunConfig } from "@/platform/ipc";
const mocks = vi.hoisted(() => ({ discover: vi.fn(), generic: vi.fn(), start: vi.fn(), install: vi.fn() }));
vi.mock("@/platform/ipc", async (original) => {
const real = await original<typeof import("@/platform/ipc")>();
return {
...real,
isTauri: () => true,
commands: {
...real.commands,
debugUnityProcesses: mocks.discover,
debugAttachableProcesses: mocks.generic,
},
};
});
vi.mock("../state", async (original) => ({
...(await original<typeof import("../state")>()),
startSession: mocks.start,
}));
import { registerAction } from "@/platform/actions";
import { useProject } from "@/platform/project";
import { openAttachPopup, installAttachAdapter } from "../attachPopup";
import { DebuggerOverlays } from "../ui/Overlays";
import { useOverlays } from "../ui/popoverStore";
const editor: AttachableProcess = {
pid: 42,
name: "Unity Editor",
commandLine: "/game",
adapter: "unity",
kind: "unity",
host: null,
port: null,
};
const ok = (data: AttachableProcess[]) => ({ status: "ok" as const, data });
function deferred() {
let resolve!: (result: ReturnType<typeof ok>) => void;
const promise = new Promise<ReturnType<typeof ok>>((r) => {
resolve = r;
});
return { promise, resolve };
}
let dispose: () => void;
beforeEach(() => {
vi.clearAllMocks();
useProject.setState({ project: { root: "/game" } as ProjectInfo });
useOverlays.setState({ attach: null });
mocks.discover.mockResolvedValue(ok([editor]));
mocks.start.mockResolvedValue(1);
mocks.install.mockResolvedValue(undefined);
dispose = registerAction({ id: "tools.install", title: "Install", run: mocks.install });
});
afterEach(() => {
cleanup();
dispose();
useOverlays.setState({ attach: null });
useProject.setState({ project: null });
});
it("selects the requested Unity process and starts a non-rerunnable attach session", async () => {
const otherEditor = { ...editor, pid: 84, commandLine: "/other-game" };
mocks.discover.mockResolvedValueOnce(ok([otherEditor, editor]));
await openAttachPopup("unity");
render(<DebuggerOverlays />);
expect(screen.getByRole("dialog", { name: "Attach to Unity Process" })).toBeTruthy();
expect(screen.getByText(/switch to Debug Mode/)).toBeTruthy();
fireEvent.keyDown(screen.getByRole("textbox", { name: "Filter processes" }), { key: "ArrowDown" });
fireEvent.click(screen.getByRole("button", { name: "Attach" }));
await waitFor(() =>
expect(mocks.start).toHaveBeenCalledWith(
expect.objectContaining({
request: "attach",
adapter: "unity",
cwd: "/game",
extraJson: expect.stringContaining("Unity Editor (42)"),
}),
{ rerunnable: false },
),
);
expect(mocks.generic).not.toHaveBeenCalled();
expect(useOverlays.getState().attach).toBeNull();
});
it("associates the selected run configuration after refreshing the picker", async () => {
const runConfig = { id: "unity-editor", name: "Unity Editor", kind: "unity-editor" } as RunConfig;
await openAttachPopup("unity", runConfig);
render(<DebuggerOverlays />);
fireEvent.click(screen.getByRole("button", { name: "Refresh" }));
await waitFor(() => expect(useOverlays.getState().attach?.processes).toEqual([editor]));
fireEvent.click(screen.getByRole("button", { name: "Attach" }));
await waitFor(() => expect(mocks.start).toHaveBeenCalledWith(expect.anything(), { rerunnable: false, runConfig }));
});
it("offers installation for a missing adapter and retries discovery afterwards", async () => {
mocks.discover.mockResolvedValueOnce({
status: "error",
error: { kind: "unavailable", message: "debug adapter `unity` not found" },
});
await openAttachPopup("unity");
render(<DebuggerOverlays />);
fireEvent.click(screen.getByRole("button", { name: "Install Unity Debugger" }));
await waitFor(() => expect(useOverlays.getState().attach?.processes).toEqual([editor]));
expect(mocks.install).toHaveBeenCalledWith(expect.objectContaining({ arg: "unity-debug" }));
});
it("does not replace a refreshed picker with an older discovery response", async () => {
const old = deferred();
mocks.discover.mockReturnValueOnce(old.promise);
const first = openAttachPopup("unity");
await openAttachPopup("unity");
old.resolve(ok([]));
await first;
expect(useOverlays.getState().attach?.processes).toEqual([editor]);
});
it("does not reopen a cancelled picker when discovery finishes", async () => {
const pending = deferred();
mocks.discover.mockReturnValueOnce(pending.promise);
const request = openAttachPopup("unity");
useOverlays.setState({ attach: null });
pending.resolve(ok([editor]));
await request;
expect(useOverlays.getState().attach).toBeNull();
});
it("does not attach to an old project's process or retry installation after switching projects", async () => {
await openAttachPopup("unity");
render(<DebuggerOverlays />);
act(() => useProject.setState({ project: { root: "/other" } as ProjectInfo }));
fireEvent.click(screen.getByRole("button", { name: "Attach" }));
expect(mocks.start).not.toHaveBeenCalled();
act(() =>
useOverlays.setState({
attach: { processes: [], error: "missing", mode: "unity", projectRoot: "/game", installTool: "unity-debug" },
}),
);
await installAttachAdapter();
expect(mocks.discover).toHaveBeenCalledTimes(1);
});