import { mkdtemp, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { openDatabase, closeDatabase } from "../../database.js";
import { createProject } from "../projects.js";
import { createTerminalSession, getTerminalSession, listActiveSessions, closeTerminalSession, } from "../terminal-sessions.js";
describe("terminal-sessions repository", () => {
    let tempDir;
    let dbPath;
    let projectId;
    beforeEach(async () => {
        tempDir = await mkdtemp(join(tmpdir(), "pocketcoder-terminal-test-"));
        dbPath = join(tempDir, "daemon.db");
        closeDatabase();
        openDatabase(dbPath);
        const project = createProject("test-project", "/tmp/test-project");
        projectId = project.project_id;
    });
    afterEach(async () => {
        closeDatabase();
        await rm(tempDir, { recursive: true, force: true });
    });
    it("should create terminal session with UUID", () => {
        const session = createTerminalSession(projectId);
        expect(session.terminal_id).toBeDefined();
        expect(session.terminal_id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
        expect(session.project_id).toBe(projectId);
        expect(session.pid).toBeNull();
        expect(session.created_at).toBeDefined();
        expect(session.closed_at).toBeNull();
    });
    it("should create terminal session with pid", () => {
        const session = createTerminalSession(projectId, 12345);
        expect(session.pid).toBe(12345);
    });
    it("should get session by ID", () => {
        const created = createTerminalSession(projectId, 100);
        const fetched = getTerminalSession(created.terminal_id);
        expect(fetched).not.toBeNull();
        expect(fetched.terminal_id).toBe(created.terminal_id);
        expect(fetched.project_id).toBe(projectId);
        expect(fetched.pid).toBe(100);
    });
    it("should return null for non-existent session", () => {
        const result = getTerminalSession("non-existent-id");
        expect(result).toBeNull();
    });
    it("should list active sessions", () => {
        createTerminalSession(projectId, 1);
        createTerminalSession(projectId, 2);
        createTerminalSession(projectId, 3);
        const active = listActiveSessions();
        expect(active).toHaveLength(3);
    });
    it("should list active sessions filtered by project", () => {
        const project2 = createProject("project-2", "/tmp/project-2");
        createTerminalSession(projectId, 1);
        createTerminalSession(projectId, 2);
        createTerminalSession(project2.project_id, 3);
        const active = listActiveSessions(projectId);
        expect(active).toHaveLength(2);
        expect(active.every((s) => s.project_id === projectId)).toBe(true);
    });
    it("should not list closed sessions as active", () => {
        const session = createTerminalSession(projectId, 1);
        createTerminalSession(projectId, 2);
        closeTerminalSession(session.terminal_id);
        const active = listActiveSessions();
        expect(active).toHaveLength(1);
        expect(active[0].pid).toBe(2);
    });
    it("should mark session as closed", () => {
        const session = createTerminalSession(projectId, 1);
        expect(session.closed_at).toBeNull();
        closeTerminalSession(session.terminal_id);
        const closed = getTerminalSession(session.terminal_id);
        expect(closed).not.toBeNull();
        expect(closed.closed_at).not.toBeNull();
    });
    it("should return active sessions ordered by created_at descending", () => {
        const s1 = createTerminalSession(projectId, 1);
        const s2 = createTerminalSession(projectId, 2);
        const s3 = createTerminalSession(projectId, 3);
        const active = listActiveSessions();
        expect(active).toHaveLength(3);
        // Most recently created first
        expect(active[0].terminal_id).toBe(s3.terminal_id);
        expect(active[1].terminal_id).toBe(s2.terminal_id);
        expect(active[2].terminal_id).toBe(s1.terminal_id);
    });
});
//# sourceMappingURL=terminal-sessions.test.js.map