import { describe, expect, it } from "vitest";
import { filterAndSortSessions } from "../session-filter.js";
function makeSession(overrides) {
    return {
        title: "Test session",
        time: { created: 1000, updated: 1000 },
        ...overrides,
    };
}
describe("filterAndSortSessions", () => {
    it("should filter by project directory", () => {
        const sessions = [
            makeSession({ id: "s1", directory: "/projects/foo" }),
            makeSession({ id: "s2", directory: "/projects/bar" }),
            makeSession({ id: "s3", directory: "/other/baz" }),
        ];
        const projectPaths = new Set(["/projects/foo", "/projects/bar"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result).toHaveLength(2);
        expect(result.map((s) => s.id)).toEqual(["s1", "s2"]);
    });
    it("should sort by most recent activity (updated time)", () => {
        const sessions = [
            makeSession({
                id: "s1",
                directory: "/projects/a",
                time: { created: 100, updated: 200 },
            }),
            makeSession({
                id: "s2",
                directory: "/projects/b",
                time: { created: 100, updated: 500 },
            }),
            makeSession({
                id: "s3",
                directory: "/projects/c",
                time: { created: 100, updated: 300 },
            }),
        ];
        const projectPaths = new Set(["/projects/a", "/projects/b", "/projects/c"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result.map((s) => s.id)).toEqual(["s2", "s3", "s1"]);
    });
    it("should fall back to created time when updated is 0", () => {
        const sessions = [
            makeSession({
                id: "s1",
                directory: "/projects/a",
                time: { created: 300, updated: 0 },
            }),
            makeSession({
                id: "s2",
                directory: "/projects/b",
                time: { created: 100, updated: 0 },
            }),
        ];
        const projectPaths = new Set(["/projects/a", "/projects/b"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result.map((s) => s.id)).toEqual(["s1", "s2"]);
    });
    it("should exclude unregistered projects", () => {
        const sessions = [
            makeSession({ id: "s1", directory: "/projects/registered" }),
            makeSession({ id: "s2", directory: "/projects/unregistered" }),
            makeSession({ id: "s3", directory: "/tmp/random" }),
        ];
        const projectPaths = new Set(["/projects/registered"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result).toHaveLength(1);
        expect(result[0].id).toBe("s1");
    });
    it("should return empty array when no sessions match", () => {
        const sessions = [makeSession({ id: "s1", directory: "/projects/unknown" })];
        const projectPaths = new Set(["/projects/registered"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result).toEqual([]);
    });
    it("should return empty array for empty sessions list", () => {
        const projectPaths = new Set(["/projects/a"]);
        const result = filterAndSortSessions([], projectPaths);
        expect(result).toEqual([]);
    });
    it("should return empty array for empty project paths", () => {
        const sessions = [makeSession({ id: "s1", directory: "/projects/a" })];
        const result = filterAndSortSessions(sessions, new Set());
        expect(result).toEqual([]);
    });
    it("should handle sessions with no timestamps gracefully", () => {
        const sessions = [
            makeSession({
                id: "s1",
                directory: "/projects/a",
                time: { created: 0, updated: 0 },
            }),
            makeSession({
                id: "s2",
                directory: "/projects/b",
                time: { created: 0, updated: 0 },
            }),
        ];
        const projectPaths = new Set(["/projects/a", "/projects/b"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result).toHaveLength(2);
    });
    it("should use exact path matching (not substring)", () => {
        const sessions = [
            makeSession({ id: "s1", directory: "/projects/foo" }),
            makeSession({ id: "s2", directory: "/projects/foobar" }),
            makeSession({ id: "s3", directory: "/projects/foo/bar" }),
        ];
        const projectPaths = new Set(["/projects/foo"]);
        const result = filterAndSortSessions(sessions, projectPaths);
        expect(result).toHaveLength(1);
        expect(result[0].id).toBe("s1");
    });
});
//# sourceMappingURL=session-filter.test.js.map