import { describe, expect, it, vi, beforeEach } from "vitest";
// Mock dependencies
vi.mock("../../../auth/token-storage.js");
vi.mock("../../../db/repositories/projects.js");
vi.mock("../../../utils/machine-info.js");
const { loadTokens } = await import("../../../auth/token-storage.js");
const { listProjects } = await import("../../../db/repositories/projects.js");
const { getMachineMetadata } = await import("../../../utils/machine-info.js");
const { buildDaemonRegisterMessage, sendDaemonRegister } = await import("../daemon-register.js");
const mockLoadTokens = vi.mocked(loadTokens);
const mockListProjects = vi.mocked(listProjects);
const mockGetMachineMetadata = vi.mocked(getMachineMetadata);
const sampleTokens = {
    machine_key: "550e8400-e29b-41d4-a716-446655440000",
    access_token: "eyJhbGciOiJIUzI1NiJ9.test",
    refresh_token: "refresh-token-123",
    expires_at: Date.now() / 1000 + 3600,
    user_id: "user-uuid-123",
};
const sampleMachine = {
    os: "linux",
    osVersion: "Linux 6.1.0",
    hostname: "dev-machine",
    username: "testuser",
    label: null,
};
const sampleProjects = [
    {
        project_id: "proj-1",
        name: "my-app",
        path: "/home/user/my-app",
        created_at: "2026-01-01T00:00:00.000Z",
        last_opened_at: null,
    },
    {
        project_id: "proj-2",
        name: "api-server",
        path: "/home/user/api-server",
        created_at: "2026-01-02T00:00:00.000Z",
        last_opened_at: "2026-01-10T12:00:00.000Z",
    },
];
function createMockClient() {
    return { send: vi.fn() };
}
describe("daemon-register", () => {
    beforeEach(() => {
        vi.clearAllMocks();
        mockGetMachineMetadata.mockReturnValue(sampleMachine);
        mockListProjects.mockReturnValue(sampleProjects);
        mockLoadTokens.mockResolvedValue(sampleTokens);
    });
    describe("buildDaemonRegisterMessage", () => {
        it("should build correct registration message", async () => {
            const message = await buildDaemonRegisterMessage();
            expect(message).not.toBeNull();
            expect(message.type).toBe("daemon_register");
            expect(message.payload.machineKey).toBe(sampleTokens.machine_key);
            expect(message.payload.token).toBe(sampleTokens.access_token);
        });
        it("should include machine metadata", async () => {
            const message = await buildDaemonRegisterMessage();
            expect(message.payload.machine).toEqual(sampleMachine);
            expect(mockGetMachineMetadata).toHaveBeenCalledWith(null);
        });
        it("should include project list", async () => {
            const message = await buildDaemonRegisterMessage();
            expect(message.payload.projects).toEqual([
                { projectId: "proj-1", name: "my-app" },
                { projectId: "proj-2", name: "api-server" },
            ]);
        });
        it("should return null when not authenticated", async () => {
            mockLoadTokens.mockResolvedValue(null);
            const message = await buildDaemonRegisterMessage();
            expect(message).toBeNull();
        });
        it("should pass label to getMachineMetadata", async () => {
            await buildDaemonRegisterMessage("Work Laptop");
            expect(mockGetMachineMetadata).toHaveBeenCalledWith("Work Laptop");
        });
        it("should handle empty project list", async () => {
            mockListProjects.mockReturnValue([]);
            const message = await buildDaemonRegisterMessage();
            expect(message.payload.projects).toEqual([]);
        });
    });
    describe("sendDaemonRegister", () => {
        it("should send registration message via client", async () => {
            const mockClient = createMockClient();
            const result = await sendDaemonRegister(mockClient);
            expect(result).toBe(true);
            expect(vi.mocked(mockClient.send)).toHaveBeenCalledOnce();
            const sent = vi.mocked(mockClient.send).mock.calls[0][0];
            expect(sent.type).toBe("daemon_register");
            expect(sent.payload.machineKey).toBe(sampleTokens.machine_key);
        });
        it("should return false when not authenticated", async () => {
            mockLoadTokens.mockResolvedValue(null);
            const mockClient = createMockClient();
            const result = await sendDaemonRegister(mockClient);
            expect(result).toBe(false);
            expect(vi.mocked(mockClient.send)).not.toHaveBeenCalled();
        });
        it("should re-register on reconnect (callable multiple times)", async () => {
            const mockClient = createMockClient();
            await sendDaemonRegister(mockClient);
            await sendDaemonRegister(mockClient);
            expect(vi.mocked(mockClient.send)).toHaveBeenCalledTimes(2);
        });
    });
});
//# sourceMappingURL=daemon-register.test.js.map