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 { machineLabelCommand } from "../machine.js";
const mockReadPidFile = vi.mocked(readPidFile);
const mockIsProcessRunning = vi.mocked(isProcessRunning);
function mockDaemonRunning() {
    mockReadPidFile.mockResolvedValue(12345);
    mockIsProcessRunning.mockReturnValue(true);
    vi.mocked(globalThis.fetch).mockResolvedValueOnce({
        ok: true,
        json: () => Promise.resolve({ status: "ok" }),
    });
}
describe("machine label command", () => {
    beforeEach(() => {
        vi.restoreAllMocks();
        process.exitCode = undefined;
        globalThis.fetch = vi.fn();
    });
    it("should set machine label", async () => {
        mockDaemonRunning();
        vi.mocked(globalThis.fetch).mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ label: "Work Laptop" }),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("Work Laptop", {});
        // First call is healthz, second is POST /machine/label
        expect(vi.mocked(globalThis.fetch)).toHaveBeenCalledTimes(2);
        const postCall = vi.mocked(globalThis.fetch).mock.calls[1];
        expect(postCall[0]).toContain("/machine/label");
        expect(postCall[1]).toMatchObject({
            method: "POST",
            headers: { "Content-Type": "application/json" },
        });
        expect(consoleSpy).toHaveBeenCalledWith("Machine label updated: Work Laptop");
        consoleSpy.mockRestore();
    });
    it("should show current label", async () => {
        mockDaemonRunning();
        vi.mocked(globalThis.fetch).mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ label: "Work Laptop" }),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("", { show: true });
        // First call is healthz, second is GET /machine/label
        expect(vi.mocked(globalThis.fetch)).toHaveBeenCalledTimes(2);
        const getCall = vi.mocked(globalThis.fetch).mock.calls[1];
        expect(getCall[0]).toContain("/machine/label");
        // GET request - no method specified (defaults to GET)
        expect(getCall[1]).not.toHaveProperty("method");
        expect(consoleSpy).toHaveBeenCalledWith("Current label: Work Laptop");
        consoleSpy.mockRestore();
    });
    it("should display success message", async () => {
        mockDaemonRunning();
        vi.mocked(globalThis.fetch).mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ label: "My Server" }),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("My Server", {});
        expect(consoleSpy).toHaveBeenCalledWith("Machine label updated: My Server");
        consoleSpy.mockRestore();
    });
    it("should handle daemon not running when setting label", async () => {
        mockReadPidFile.mockResolvedValue(null);
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("Test", {});
        expect(consoleSpy).toHaveBeenCalledWith("Daemon is not running. Start the daemon first.");
        expect(process.exitCode).toBe(1);
        consoleSpy.mockRestore();
    });
    it("should handle daemon not running when showing label", async () => {
        mockReadPidFile.mockResolvedValue(null);
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("", { show: true });
        expect(consoleSpy).toHaveBeenCalledWith("Daemon is not running. Start the daemon first.");
        expect(process.exitCode).toBe(1);
        consoleSpy.mockRestore();
    });
    it("should show no label message when none is set", async () => {
        mockDaemonRunning();
        vi.mocked(globalThis.fetch).mockResolvedValueOnce({
            ok: true,
            json: () => Promise.resolve({ label: null }),
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await machineLabelCommand("", { show: true });
        expect(consoleSpy).toHaveBeenCalledWith("No custom label set (using default)");
        consoleSpy.mockRestore();
    });
    it("should handle set label failure", async () => {
        mockDaemonRunning();
        vi.mocked(globalThis.fetch).mockResolvedValueOnce({
            ok: false,
            status: 500,
            json: () => Promise.resolve({ error: "Internal error" }),
        });
        const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
        await machineLabelCommand("Test", {});
        expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to set machine label: Internal error");
        expect(process.exitCode).toBe(1);
        consoleErrorSpy.mockRestore();
    });
    it("should handle empty label text", async () => {
        const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => { });
        await machineLabelCommand("", {});
        expect(consoleErrorSpy).toHaveBeenCalledWith("Label text is required");
        expect(process.exitCode).toBe(1);
        consoleErrorSpy.mockRestore();
    });
});
//# sourceMappingURL=cli-machine.test.js.map