import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../../utils/logger.js", () => ({
    logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
const mockExecFile = vi.fn();
vi.mock("node:child_process", () => ({
    execFile: mockExecFile,
}));
// promisify(execFile) returns a function that calls execFile with a callback
// We need to simulate that behavior
vi.mock("node:util", () => ({
    promisify: () => mockExecFile,
}));
// Import after mocks
const { checkOpenCodeInstalled } = await import("../check-installed.js");
describe("checkOpenCodeInstalled", () => {
    beforeEach(() => {
        vi.clearAllMocks();
    });
    afterEach(() => {
        vi.restoreAllMocks();
    });
    it("should detect opencode binary and return version", async () => {
        mockExecFile.mockResolvedValue({ stdout: "opencode v1.2.0\n", stderr: "" });
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(true);
        expect(result.version).toBe("opencode v1.2.0");
    });
    it("should get version string from stdout", async () => {
        mockExecFile.mockResolvedValue({ stdout: "  v2.0.0-beta  \n", stderr: "" });
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(true);
        expect(result.version).toBe("v2.0.0-beta");
    });
    it("should set hasOpenCode flag to true when available", async () => {
        mockExecFile.mockResolvedValue({ stdout: "opencode v1.0.0", stderr: "" });
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(true);
    });
    it("should handle missing binary", async () => {
        mockExecFile.mockRejectedValue(new Error("ENOENT: opencode not found"));
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(false);
        expect(result.version).toBeUndefined();
    });
    it("should handle command timeout", async () => {
        mockExecFile.mockRejectedValue(new Error("ETIMEDOUT"));
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(false);
        expect(result.version).toBeUndefined();
    });
    it("should handle non-zero exit code", async () => {
        const error = new Error("Command failed");
        error.code = 1;
        mockExecFile.mockRejectedValue(error);
        const result = await checkOpenCodeInstalled();
        expect(result.available).toBe(false);
        expect(result.version).toBeUndefined();
    });
    it("should call opencode with --version flag", async () => {
        mockExecFile.mockResolvedValue({ stdout: "opencode v1.0.0", stderr: "" });
        await checkOpenCodeInstalled();
        expect(mockExecFile).toHaveBeenCalledWith("opencode", ["--version"], {
            timeout: 10_000,
        });
    });
});
//# sourceMappingURL=check-installed.test.js.map