Files
oh-my-pi/packages/coding-agent/test/issue-956-repro.test.ts
T
oldschoola a2854ba768 fix: migrate coding-agent tests from fs.rm to removeWithRetries
Migrate 203 test files (356 call sites) from fs.rm/fs.rmSync to
removeWithRetries/removeSyncWithRetries to reduce EBUSY test failures
on Windows. removeWithRetries is now exported from @oh-my-pi/pi-utils.

The migration uses a regex-based approach that:
- Replaces fs.rm(path, { recursive, force }) → removeWithRetries(path)
- Replaces fs.rmSync(path, { recursive, force }) → removeSyncWithRetries(path)
- Replaces fs.rm(path) → removeWithRetries(path) (no options)
- Skips fs.rm/fs.rmSync inside template literals (bun --eval scripts)
- Adds imports to existing @oh-my-pi/pi-utils import or creates new one
- Removes unused fs imports where fs.rm was the only fs usage (4 files)
2026-06-23 15:28:05 -07:00

111 lines
3.5 KiB
TypeScript

import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import * as mcpClient from "@oh-my-pi/pi-coding-agent/mcp/client";
import { MCPCommandController } from "@oh-my-pi/pi-coding-agent/modes/controllers/mcp-command-controller";
import { initTheme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
import { getConfigRootDir, getProjectDir, removeWithRetries, setAgentDir, setProjectDir } from "@oh-my-pi/pi-utils";
const originalProjectDir = getProjectDir();
const originalAgentDir = process.env.PI_CODING_AGENT_DIR;
const fallbackAgentDir = path.join(getConfigRootDir(), "agent");
describe("issue #956: interactive /mcp test", () => {
let projectDir = "";
let agentDir = "";
beforeAll(() => {
initTheme();
});
beforeEach(async () => {
projectDir = await fs.mkdtemp(path.join(os.tmpdir(), "omp-issue-956-project-"));
agentDir = await fs.mkdtemp(path.join(os.tmpdir(), "omp-issue-956-agent-"));
setProjectDir(projectDir);
setAgentDir(agentDir);
await fs.writeFile(
path.join(projectDir, ".mcp.json"),
JSON.stringify(
{
mcpServers: {
github: {
type: "stdio",
command: "github-mcp-server",
args: ["serve"],
},
},
},
null,
2,
),
);
});
afterEach(async () => {
vi.restoreAllMocks();
setProjectDir(originalProjectDir);
if (originalAgentDir) {
setAgentDir(originalAgentDir);
} else {
setAgentDir(fallbackAgentDir);
delete process.env.PI_CODING_AGENT_DIR;
}
await removeWithRetries(projectDir);
await removeWithRetries(agentDir);
});
it("tests a connected server discovered from standalone .mcp.json", async () => {
const transport = {
connected: true,
request: vi.fn(),
notify: vi.fn(),
close: vi.fn(async () => {}),
};
const connection = {
name: "github",
config: { type: "stdio" as const, command: "github-mcp-server", args: ["serve"] },
transport,
serverInfo: { name: "GitHub MCP", version: "1.0.0" },
capabilities: {},
};
const showError = vi.fn();
const showStatus = vi.fn();
const requestRender = vi.fn();
const addChild = vi.fn();
const refreshMCPTools = vi.fn();
const connectToServer = vi.spyOn(mcpClient, "connectToServer").mockResolvedValue(connection);
const listTools = vi.spyOn(mcpClient, "listTools").mockResolvedValue([{ name: "search_issues" }] as never);
const disconnectServer = vi.spyOn(mcpClient, "disconnectServer").mockResolvedValue();
const controller = new MCPCommandController({
chatContainer: { addChild },
present: (content: unknown) => {
for (const item of Array.isArray(content) ? content : [content]) addChild(item);
requestRender();
},
ui: { requestRender },
editor: {},
showError,
showStatus,
session: { refreshMCPTools },
mcpManager: {
prepareConfig: vi.fn(async config => config),
getConnectionStatus: vi.fn(() => "connected"),
},
} as never);
await controller.handle("/mcp test github");
expect(showError).not.toHaveBeenCalled();
expect(connectToServer).toHaveBeenCalledWith(
"github",
expect.objectContaining({ command: "github-mcp-server", args: ["serve"] }),
expect.objectContaining({ signal: expect.any(AbortSignal) }),
);
expect(listTools).toHaveBeenCalledWith(connection, expect.objectContaining({ signal: expect.any(AbortSignal) }));
expect(disconnectServer).toHaveBeenCalledWith(connection);
expect(requestRender).toHaveBeenCalled();
});
});