import { expect, test, mock, beforeEach, afterEach, describe, spyOn } from "bun:test"; import * as os from "node:os"; import { closeMultiModel } from "../src/core/close"; let originalBun$: typeof Bun.$ = (globalThis as any).Bun?.$; let mockCommandResponses: Record = {}; 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"); }); });