import { describe, expect, test } from "bun:test"; import type { LinearProjectMapping } from "../db/schema"; import { linearProjectNamesForProjectIds, resolveLinearProjectId, } from "./linear-mapping"; function mapping( partial: Partial & { linearProjectName: string; projectId: string; }, ): LinearProjectMapping { return { id: `m-${partial.linearProjectName}-${partial.projectId}`, organizationId: null, createdAt: null, ...partial, }; } describe("resolveLinearProjectId", () => { const mappings = [ mapping({ linearProjectName: "Scribe", projectId: "proj-scribe" }), mapping({ linearProjectName: "Hub", projectId: "proj-hub" }), ]; test("resolves an exact project name to its project id", () => { expect(resolveLinearProjectId("Scribe", mappings)).toBe("proj-scribe"); }); test("matches case-insensitively and ignores surrounding whitespace", () => { expect(resolveLinearProjectId(" scribe ", mappings)).toBe("proj-scribe"); expect(resolveLinearProjectId("HUB", mappings)).toBe("proj-hub"); }); test("returns null when no mapping matches", () => { expect(resolveLinearProjectId("Unmapped", mappings)).toBeNull(); }); test("returns null for missing or empty names (stays unassigned)", () => { expect(resolveLinearProjectId(null, mappings)).toBeNull(); expect(resolveLinearProjectId(undefined, mappings)).toBeNull(); expect(resolveLinearProjectId(" ", mappings)).toBeNull(); }); test("returns null when there are no mappings", () => { expect(resolveLinearProjectId("Scribe", [])).toBeNull(); }); }); describe("linearProjectNamesForProjectIds", () => { const mappings = [ mapping({ linearProjectName: "Scribe", projectId: "proj-scribe" }), mapping({ linearProjectName: "Scribe UI", projectId: "proj-scribe" }), mapping({ linearProjectName: "Hub", projectId: "proj-hub" }), ]; test("collects every Linear name linked to the requested projects", () => { expect( linearProjectNamesForProjectIds(mappings, ["proj-scribe"]).sort(), ).toEqual(["Scribe", "Scribe UI"]); }); test("spans multiple requested projects", () => { expect( linearProjectNamesForProjectIds(mappings, [ "proj-scribe", "proj-hub", ]).sort(), ).toEqual(["Hub", "Scribe", "Scribe UI"]); }); test("returns empty when no mapping targets the requested projects", () => { expect(linearProjectNamesForProjectIds(mappings, ["proj-other"])).toEqual( [], ); expect(linearProjectNamesForProjectIds([], ["proj-scribe"])).toEqual([]); }); test("de-duplicates names case-insensitively, preserving stored casing", () => { const dupes = [ mapping({ linearProjectName: "Scribe", projectId: "p1" }), mapping({ linearProjectName: "scribe", projectId: "p1" }), ]; expect(linearProjectNamesForProjectIds(dupes, ["p1"])).toEqual(["Scribe"]); }); });