Файли
openidea/src/features/python-console/store.test.ts
T
LU XIANGRUIтаwildcatDownstairs c632dfa01c feat(python): add a Python Console tool window (#5164)
Adds a Python Console tool window: an interactive interpreter of the project's Python (the one Settings and the status bar show; in a workspace, the member the active file is in), started in the project root with the source roots on `PYTHONPATH`. It uses IPython when the interpreter has it and plain Python otherwise, in its own `__main__`. Enter runs the input, Shift+Enter adds a line, Up/Down recall earlier inputs, and while code runs a typed line answers its `input()`. A Variables pane lists the console's globals after each run. Execute Selection in Python Console (`python.console.executeSelection`, Alt+Shift+E, also in the editor context menu of Python files) sends the selection or the current line, starting the console if needed; a selection inside an indented block is dedented. The console is a small driver script run on the existing Run process host, so there are no backend changes.

Closes #5148

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-22 14:34:50 +08:00

86 рядки
3.1 KiB
TypeScript

import { beforeEach, expect, it, vi } from "vitest";
import type { OutputChunk } from "@/platform/ipc";
const ok = <T>(data: T) => Promise.resolve({ status: "ok" as const, data });
const ipc = vi.hoisted(() => ({
exited: (_e: { run: number; code: number | null }) => {},
started: null as null | { resolve: (id: number) => void; channel: { onmessage?: (c: OutputChunk) => void } },
stdin: vi.fn(),
}));
vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} }));
vi.mock("@/platform/auxWindow", () => ({ windowLabel: () => "main" }));
vi.mock("@/workbench/contributions", () => ({ editorService: () => ({ activePath: () => null }) }));
vi.mock("@/platform/project", () => ({
dirname: (p: string) => p,
useProject: { getState: () => ({ project: { root: "/p" } }), subscribe: () => () => {} },
}));
vi.mock("@/platform/ipc", async (importOriginal) => {
const real = await importOriginal<typeof import("@/platform/ipc")>();
return {
...real,
onIdeEvent: (_type: string, handler: typeof ipc.exited) => {
ipc.exited = handler;
return () => {};
},
commands: {
runList: () => Promise.resolve([]),
sdkPythonEffective: () => ok("/venv/bin/python"),
runStart: (_config: unknown, channel: NonNullable<typeof ipc.started>["channel"]) =>
new Promise((resolve) => {
ipc.started = { channel, resolve: (id) => resolve({ status: "ok", data: { id } }) };
}),
runStdin: (id: number, data: string) => {
ipc.stdin(id, data);
return ok(null);
},
runStop: () => ok(null),
},
};
});
const { execute, installConsole, stopConsole, tail, useConsole } = await import("./store");
const { request } = await import("./protocol");
const settle = () => new Promise((r) => setTimeout(r, 0));
const text = () =>
useConsole
.getState()
.output.map((s) => s.text)
.join("");
installConsole();
beforeEach(() => {
stopConsole();
ipc.started = null;
ipc.stdin.mockClear();
});
it("runs code sent while starting once the driver is ready, even before the start call returns", async () => {
execute("x = 1");
await settle();
ipc.started!.channel.onmessage!({ kind: "stdout", text: '\x1e{"ready":{"version":"3.12.1","ipython":null}}\n' });
expect(ipc.stdin).not.toHaveBeenCalled();
ipc.started!.resolve(7);
await settle();
expect(ipc.stdin).toHaveBeenCalledWith(7, request("x = 1"));
expect(useConsole.getState().status).toBe("busy");
});
it("reports an interpreter that exits before the start call returns", async () => {
execute("x = 1");
await settle();
ipc.exited({ run: 8, code: 1 });
ipc.started!.resolve(8);
await settle();
expect(useConsole.getState().status).toBe("stopped");
expect(text()).toContain("exit code 1");
expect(ipc.stdin).not.toHaveBeenCalled();
});
it("keeps the newest output within the cap, cutting into the oldest kept span", () => {
const spans = [{ text: "aaaa" }, { text: "bb", fg: "red" }, { text: "cc" }];
expect(tail(spans, 10)).toBe(spans);
expect(tail(spans, 5)).toEqual([{ text: "a" }, { text: "bb", fg: "red" }, { text: "cc" }]);
expect(tail(spans, 3)).toEqual([{ text: "b", fg: "red" }, { text: "cc" }]);
});