mirror of
https://github.com/bendtherules/opencode-multi-model.git
synced 2026-08-18 13:42:21 +00:00
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
import {
|
|
afterEach,
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
mock,
|
|
spyOn,
|
|
test,
|
|
} from "bun:test";
|
|
import * as os from "node:os";
|
|
import { closeMultiModel } from "../src/core/close";
|
|
|
|
const originalBun$: typeof Bun.$ = (globalThis as any).Bun?.$;
|
|
let mockCommandResponses: Record<
|
|
string,
|
|
{ ok: boolean; stdout: string; stderr: string }
|
|
> = {};
|
|
let executedCommands: string[] = [];
|
|
|
|
function setupMockBun$() {
|
|
const mockFn = mock((_strings: TemplateStringsArray, ...values: any[]) => {
|
|
const parts = values[0] as string[];
|
|
const cmd = parts.join(" ");
|
|
executedCommands.push(cmd);
|
|
// Simulate throwing error for a specific command
|
|
if (cmd === "git worktree list --porcelain") {
|
|
throw new Error("unexpected error");
|
|
}
|
|
const response = mockCommandResponses[cmd] ?? {
|
|
ok: true,
|
|
stdout: "",
|
|
stderr: "",
|
|
};
|
|
return {
|
|
quiet: () => ({
|
|
nothrow: async () => ({
|
|
exitCode: response.ok ? 0 : 1,
|
|
stdout: { toString: () => response.stdout },
|
|
stderr: { toString: () => response.stderr },
|
|
}),
|
|
}),
|
|
};
|
|
});
|
|
(globalThis as any).Bun.$ = mockFn;
|
|
return mockFn;
|
|
}
|
|
|
|
function restoreOriginalBun$() {
|
|
(globalThis as any).Bun.$ = originalBun$;
|
|
}
|
|
|
|
describe("closeMultiModel unexpected error handling", () => {
|
|
beforeEach(() => {
|
|
spyOn(os, "homedir").mockReturnValue("/home/user");
|
|
mockCommandResponses = {};
|
|
executedCommands = [];
|
|
setupMockBun$();
|
|
});
|
|
afterEach(() => {
|
|
restoreOriginalBun$();
|
|
});
|
|
|
|
test("catches thrown error and returns failure", async () => {
|
|
const result = await closeMultiModel({
|
|
sessionName: "any-session",
|
|
cleanupWorktrees: false,
|
|
force: false,
|
|
});
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toContain("unexpected error");
|
|
});
|
|
});
|