edab058993
- Remove pi-ref.ts deferred barrel import (no longer needed after circular dep fix) - Update extension/hook/custom-tool/custom-command loaders to use direct imports - Fix warmPythonEnvironment mock return type in python tool tests (add docs field)
88 lines
2.9 KiB
TypeScript
88 lines
2.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { type SettingPath, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import * as pythonExecutor from "@oh-my-pi/pi-coding-agent/ipy/executor";
|
|
import * as pythonKernel from "@oh-my-pi/pi-coding-agent/ipy/kernel";
|
|
import { createTools, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
|
|
import { PythonTool } from "@oh-my-pi/pi-coding-agent/tools/python";
|
|
import { Snowflake } from "@oh-my-pi/pi-utils";
|
|
|
|
function createSession(
|
|
cwd: string,
|
|
sessionFile: string,
|
|
overrides?: Partial<Record<SettingPath, unknown>>,
|
|
): ToolSession {
|
|
return {
|
|
cwd,
|
|
hasUI: false,
|
|
getSessionFile: () => sessionFile,
|
|
getSessionSpawns: () => null,
|
|
settings: Settings.isolated({ "python.toolMode": "ipy-only", ...overrides }),
|
|
};
|
|
}
|
|
|
|
describe("python tool settings", () => {
|
|
let testDir: string;
|
|
|
|
beforeEach(() => {
|
|
testDir = path.join(os.tmpdir(), `python-tool-settings-${Snowflake.next()}`);
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("exposes python tool when kernel is available", async () => {
|
|
vi.spyOn(pythonKernel, "checkPythonKernelAvailability").mockResolvedValue({ ok: true });
|
|
const sessionFile = path.join(testDir, "session.jsonl");
|
|
const tools = await createTools(createSession(testDir, sessionFile), ["python"]);
|
|
|
|
expect(tools.map(tool => tool.name).sort()).toEqual(["exit_plan_mode", "python"]);
|
|
});
|
|
|
|
it("falls back to bash when python is unavailable", async () => {
|
|
vi.spyOn(pythonKernel, "checkPythonKernelAvailability").mockResolvedValue({
|
|
ok: false,
|
|
reason: "missing",
|
|
});
|
|
const sessionFile = path.join(testDir, "session.jsonl");
|
|
const tools = await createTools(createSession(testDir, sessionFile), ["python"]);
|
|
|
|
expect(tools.map(tool => tool.name).sort()).toEqual(["bash", "exit_plan_mode"]);
|
|
});
|
|
|
|
it("passes kernel mode from settings to executor", async () => {
|
|
vi.spyOn(pythonExecutor, "warmPythonEnvironment").mockResolvedValue({ ok: true, docs: [] });
|
|
const executeSpy = vi.spyOn(pythonExecutor, "executePython").mockResolvedValue({
|
|
output: "ok",
|
|
exitCode: 0,
|
|
cancelled: false,
|
|
truncated: false,
|
|
totalLines: 1,
|
|
totalBytes: 2,
|
|
outputLines: 1,
|
|
outputBytes: 2,
|
|
displayOutputs: [],
|
|
stdinRequested: false,
|
|
});
|
|
|
|
const sessionFile = path.join(testDir, "session.jsonl");
|
|
const session = createSession(testDir, sessionFile, { "python.kernelMode": "per-call" });
|
|
const pythonTool = new PythonTool(session);
|
|
|
|
await pythonTool.execute("tool-call", { cells: [{ code: "print(1)" }] });
|
|
|
|
expect(executeSpy).toHaveBeenCalledWith(
|
|
"print(1)",
|
|
expect.objectContaining({
|
|
kernelMode: "per-call",
|
|
sessionId: `session:${sessionFile}:cwd:${testDir}`,
|
|
}),
|
|
);
|
|
});
|
|
});
|