import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("../../../auth/login.js", () => ({
    login: vi.fn(),
}));
vi.mock("../../../auth/token-storage.js", () => ({
    clearTokens: vi.fn(),
}));
vi.mock("../../../utils/machine-key.js", () => ({
    deleteMachineKey: vi.fn(),
    getMachineKey: vi.fn(),
}));
vi.mock("../start.js", () => ({
    isDaemonRunning: vi.fn(),
}));
import { login } from "../../../auth/login.js";
import { clearTokens } from "../../../auth/token-storage.js";
import { deleteMachineKey, getMachineKey } from "../../../utils/machine-key.js";
import { isDaemonRunning } from "../start.js";
import { loginCommand } from "../login.js";
import { logoutCommand } from "../logout.js";
const mockLogin = vi.mocked(login);
const mockClearTokens = vi.mocked(clearTokens);
const mockDeleteMachineKey = vi.mocked(deleteMachineKey);
const mockGetMachineKey = vi.mocked(getMachineKey);
const mockIsDaemonRunning = vi.mocked(isDaemonRunning);
describe("login command", () => {
    beforeEach(() => {
        vi.restoreAllMocks();
        process.exitCode = undefined;
    });
    it("should trigger OAuth on login", async () => {
        mockLogin.mockResolvedValue({
            user_id: "user-123",
            email: "user@example.com",
            machine_key: "mk-abc",
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await loginCommand();
        expect(mockLogin).toHaveBeenCalledOnce();
        consoleSpy.mockRestore();
    });
    it("should show username after login", async () => {
        mockLogin.mockResolvedValue({
            user_id: "user-123",
            email: "testuser@example.com",
            machine_key: "mk-abc",
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await loginCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Logged in as testuser@example.com");
        consoleSpy.mockRestore();
    });
    it("should show user_id when email not available", async () => {
        mockLogin.mockResolvedValue({
            user_id: "user-456",
            email: undefined,
            machine_key: "mk-abc",
        });
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await loginCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Logged in as user-456");
        consoleSpy.mockRestore();
    });
    it("should handle login failure", async () => {
        mockLogin.mockRejectedValue(new Error("OAuth timeout"));
        const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
        await loginCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Login failed: OAuth timeout");
        expect(process.exitCode).toBe(1);
        consoleSpy.mockRestore();
    });
});
describe("logout command", () => {
    beforeEach(() => {
        vi.restoreAllMocks();
        vi.clearAllMocks();
        process.exitCode = undefined;
        mockIsDaemonRunning.mockResolvedValue(false);
        mockClearTokens.mockResolvedValue(undefined);
        mockDeleteMachineKey.mockResolvedValue(undefined);
        mockGetMachineKey.mockResolvedValue("new-machine-key-hex");
    });
    it("should stop Host if running", async () => {
        mockIsDaemonRunning.mockResolvedValue(true);
        const mockFetch = vi.fn().mockResolvedValue({ ok: true });
        vi.stubGlobal("fetch", mockFetch);
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logoutCommand();
        expect(mockIsDaemonRunning).toHaveBeenCalledOnce();
        expect(mockFetch).toHaveBeenCalledWith(expect.stringContaining("/stop"), expect.objectContaining({ method: "POST" }));
        expect(consoleSpy).toHaveBeenCalledWith("Daemon stopped");
        consoleSpy.mockRestore();
        vi.unstubAllGlobals();
    });
    it("should delete auth.json", async () => {
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logoutCommand();
        expect(mockClearTokens).toHaveBeenCalledOnce();
        consoleSpy.mockRestore();
    });
    it("should regenerate machineKey", async () => {
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logoutCommand();
        expect(mockDeleteMachineKey).toHaveBeenCalledOnce();
        expect(mockGetMachineKey).toHaveBeenCalledOnce();
        consoleSpy.mockRestore();
    });
    it("should preserve projects (no project deletion)", async () => {
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logoutCommand();
        // Verify only clearTokens and machine key ops are called — no project deletion
        expect(mockClearTokens).toHaveBeenCalledOnce();
        expect(mockDeleteMachineKey).toHaveBeenCalledOnce();
        expect(consoleSpy).toHaveBeenCalledWith("Logged out");
        consoleSpy.mockRestore();
    });
    it("should skip daemon stop when not running", async () => {
        mockIsDaemonRunning.mockResolvedValue(false);
        const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => { });
        await logoutCommand();
        // Should not print "Daemon stopped"
        expect(consoleSpy).not.toHaveBeenCalledWith("Daemon stopped");
        expect(consoleSpy).toHaveBeenCalledWith("Logged out");
        consoleSpy.mockRestore();
    });
    it("should handle logout failure", async () => {
        mockClearTokens.mockRejectedValue(new Error("Permission denied"));
        const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => { });
        await logoutCommand();
        expect(consoleSpy).toHaveBeenCalledWith("Logout failed: Permission denied");
        expect(process.exitCode).toBe(1);
        consoleSpy.mockRestore();
    });
});
//# sourceMappingURL=cli-auth.test.js.map