import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("../../../utils/pid.js", () => ({
    readPidFile: vi.fn(),
    isProcessRunning: vi.fn(),
}));
import { readPidFile, isProcessRunning } from "../../../utils/pid.js";
import { statusCommand } from "../status.js";
const mockReadPidFile = vi.mocked(readPidFile);
const mockIsProcessRunning = vi.mocked(isProcessRunning);
describe("status command", () => {
    beforeEach(() => {
        vi.restoreAllMocks();
        process.exitCode = undefined;
        globalThis.fetch = vi.fn();
    });
    it("should query status endpoint", async () => {
        // isDaemonRunning: PID exists, process alive, healthz ok
        mockReadPidFile.mockResolvedValue(12345);
        mockIsProcessRunning.mockReturnValue(true);
        const statusResponse = {
            version: "1.0.0",
            machineKey: "mk_abc",
            loggedIn: true,
            gateway: {
                connected: true,
                lastConnectedAt: null,
                lastHeartbeatAt: new Date().toISOString(),
            },
            capabilities: { hasGit: true, hasOpenCode: true, warnings: [] },
            projects: [
                {
                    projectId: "p1",
                    name: "proj1",
                    path: "/home/user/proj1",
                    watcherRunning: true,
                    lastFsSeq: 5,
                },
                {
                    projectId: "p2",
                    name: "proj2",
                    path: "/home/user/proj2",
                    watcherRunning: false,
                    lastFsSeq: 0,
                },
            ],
            lastError: null,
        };
        vi.mocked(globalThis.fetch)
            // isDaemonRunning healthz call
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ status: "ok" }),
        })
            // status endpoint call
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(statusResponse),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await statusCommand();
        // Should call fetch twice: healthz + /status
        expect(vi.mocked(globalThis.fetch)).toHaveBeenCalledTimes(2);
        expect(consoleSpy).toHaveBeenCalledWith("Daemon: running (pid 12345)");
        consoleSpy.mockRestore();
    });
    it("should format output correctly", async () => {
        mockReadPidFile.mockResolvedValue(9999);
        mockIsProcessRunning.mockReturnValue(true);
        const statusResponse = {
            version: "1.0.0",
            machineKey: "mk_abc",
            loggedIn: true,
            gateway: {
                connected: true,
                lastConnectedAt: null,
                lastHeartbeatAt: new Date().toISOString(),
            },
            capabilities: { hasGit: true, hasOpenCode: true, warnings: [] },
            projects: [
                { projectId: "p1", name: "proj1", path: "/path", watcherRunning: true, lastFsSeq: 1 },
            ],
            lastError: null,
        };
        vi.mocked(globalThis.fetch)
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ status: "ok" }),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(statusResponse),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await statusCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Daemon: running (pid 9999)");
        expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Gateway: connected"));
        expect(consoleSpy).toHaveBeenCalledWith("User:   logged in");
        expect(consoleSpy).toHaveBeenCalledWith("Projects: 1 registered, 1 watching");
        consoleSpy.mockRestore();
    });
    it("should handle daemon not running", async () => {
        mockReadPidFile.mockResolvedValue(null);
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await statusCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Daemon: not running");
        expect(vi.mocked(globalThis.fetch)).not.toHaveBeenCalled();
        consoleSpy.mockRestore();
    });
    it("should show disconnected gateway", async () => {
        mockReadPidFile.mockResolvedValue(9999);
        mockIsProcessRunning.mockReturnValue(true);
        const statusResponse = {
            version: "1.0.0",
            machineKey: null,
            loggedIn: false,
            gateway: { connected: false, lastConnectedAt: null, lastHeartbeatAt: null },
            capabilities: { hasGit: true, hasOpenCode: false, warnings: [] },
            projects: [],
            lastError: null,
        };
        vi.mocked(globalThis.fetch)
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ status: "ok" }),
        })
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve(statusResponse),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await statusCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Gateway: disconnected");
        expect(consoleSpy).toHaveBeenCalledWith("User:   not logged in");
        expect(consoleSpy).toHaveBeenCalledWith("Projects: 0 registered, 0 watching");
        consoleSpy.mockRestore();
    });
    it("should handle status endpoint failure", async () => {
        mockReadPidFile.mockResolvedValue(9999);
        mockIsProcessRunning.mockReturnValue(true);
        vi.mocked(globalThis.fetch)
            .mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ status: "ok" }),
        })
            .mockRejectedValueOnce(new Error("Connection refused"));
        const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
        await statusCommand();
        expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to query status: Connection refused");
        expect(process.exitCode).toBe(1);
        consoleErrorSpy.mockRestore();
    });
});
//# sourceMappingURL=cli-status.test.js.map