import { describe, it, expect, beforeEach, vi } from "vitest";
import { ErrorCode } from "@pocketcoder/shared/constants";
import { handleProjectsList } from "../projects-list.js";
// Suppress logger output in tests
vi.mock("../../utils/logger.js", () => ({
    logger: {
        debug: vi.fn(),
        info: vi.fn(),
        warn: vi.fn(),
        error: vi.fn(),
    },
}));
// Mock project repository
vi.mock("../../db/repositories/projects.js", () => ({
    listProjects: vi.fn(),
}));
import { listProjects } from "../../db/repositories/projects.js";
const mockListProjects = vi.mocked(listProjects);
describe("projects_list handler", () => {
    beforeEach(() => {
        vi.clearAllMocks();
    });
    function createRequest(requestId = "req-1", machineKey = "machine-key-1") {
        return {
            type: "projects_list",
            requestId,
            payload: { machineKey },
        };
    }
    function createSendCollector() {
        const responses = [];
        const send = (msg) => {
            responses.push(msg);
        };
        return { responses, send };
    }
    it("should return all registered projects", async () => {
        mockListProjects.mockReturnValue([
            {
                project_id: "proj-1",
                name: "my-app",
                path: "/home/dev/my-app",
                created_at: "2026-02-12T10:00:00Z",
                last_opened_at: "2026-02-14T10:00:00Z",
            },
            {
                project_id: "proj-2",
                name: "api-server",
                path: "/home/dev/api-server",
                created_at: "2026-02-11T15:30:00Z",
                last_opened_at: null,
            },
        ]);
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        expect(responses).toHaveLength(1);
        const resp = responses[0];
        expect(resp.type).toBe("projects_list_response");
        expect(resp.requestId).toBe("req-1");
        expect(resp.error).toBeUndefined();
        expect(resp.payload).toBeDefined();
        expect(resp.payload.projects).toHaveLength(2);
    });
    it("should include project metadata", async () => {
        mockListProjects.mockReturnValue([
            {
                project_id: "proj-1",
                name: "my-app",
                path: "/home/dev/my-app",
                created_at: "2026-02-12T10:00:00Z",
                last_opened_at: "2026-02-14T10:00:00Z",
            },
        ]);
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        const project = responses[0].payload.projects[0];
        expect(project.projectId).toBe("proj-1");
        expect(project.name).toBe("my-app");
        expect(project.path).toBe("/home/dev/my-app");
        expect(project.lastAccessed).toBe("2026-02-14T10:00:00Z");
    });
    it("should set hasActiveSession to false (status comes via SSE)", async () => {
        mockListProjects.mockReturnValue([
            {
                project_id: "proj-1",
                name: "my-app",
                path: "/home/dev/my-app",
                created_at: "2026-02-12T10:00:00Z",
                last_opened_at: "2026-02-14T10:00:00Z",
            },
            {
                project_id: "proj-2",
                name: "api-server",
                path: "/home/dev/api-server",
                created_at: "2026-02-11T15:30:00Z",
                last_opened_at: null,
            },
        ]);
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        const projects = responses[0].payload.projects;
        // Active session status comes via SSE events, not from this handler
        expect(projects[0].hasActiveSession).toBe(false);
        expect(projects[1].hasActiveSession).toBe(false);
    });
    it("should use created_at as lastAccessed when last_opened_at is null", async () => {
        mockListProjects.mockReturnValue([
            {
                project_id: "proj-2",
                name: "api-server",
                path: "/home/dev/api-server",
                created_at: "2026-02-11T15:30:00Z",
                last_opened_at: null,
            },
        ]);
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        const project = responses[0].payload.projects[0];
        expect(project.lastAccessed).toBe("2026-02-11T15:30:00Z");
    });
    it("should return empty list when no projects registered", async () => {
        mockListProjects.mockReturnValue([]);
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        expect(responses).toHaveLength(1);
        const resp = responses[0];
        expect(resp.type).toBe("projects_list_response");
        expect(resp.error).toBeUndefined();
        expect(resp.payload.projects).toHaveLength(0);
    });
    it("should handle database errors gracefully", async () => {
        mockListProjects.mockImplementation(() => {
            throw new Error("Database is locked");
        });
        const { responses, send } = createSendCollector();
        await handleProjectsList(createRequest(), send);
        expect(responses).toHaveLength(1);
        const resp = responses[0];
        expect(resp.type).toBe("projects_list_response");
        expect(resp.error).toBeDefined();
        expect(resp.error.code).toBe(ErrorCode.UNKNOWN_ERROR);
        expect(resp.error.message).toBe("Database is locked");
    });
});
//# sourceMappingURL=projects-list.test.js.map