import { describe, expect, it } from "vitest"; import type { ConnectionGrant, ToolConnection } from "@paperclipai/shared"; import { dedicatedGitHubIdentityFor } from "./dedicated-github-identity"; const connection = { id: "conn-1", name: "robot", credentialPolicy: "per_agent" } as ToolConnection; const grant = (overrides: Partial) => ({ id: "grant-1", kind: "agent", subjectAgentId: "agent-a", status: "active", ...overrides }) as ConnectionGrant; describe("dedicatedGitHubIdentityFor", () => { it("finds a shared account for an agent that the server says it serves, with or without an install", () => { const rows = [{ connection, grant: grant({}), dedicatedAgentIds: ["agent-a", "agent-b"] }]; expect(dedicatedGitHubIdentityFor(rows, "agent-b")).toEqual({ row: rows[0], shared: true }); expect(dedicatedGitHubIdentityFor(rows, "agent-c")).toEqual({ row: null, shared: false }); }); it("marks the signed-in agent's account as shared when another agent uses it", () => { const shared = [{ connection, grant: grant({}), dedicatedAgentIds: ["agent-a", "agent-b"] }]; const alone = [{ connection, grant: grant({}), dedicatedAgentIds: ["agent-a"] }]; expect(dedicatedGitHubIdentityFor(shared, "agent-a").shared).toBe(true); expect(dedicatedGitHubIdentityFor(alone, "agent-a")).toEqual({ row: alone[0], shared: false }); }); it("prefers the agent's own account and ignores revoked grants", () => { const own = { connection: { ...connection, id: "conn-2" }, grant: grant({ id: "grant-2", subjectAgentId: "agent-b" }), dedicatedAgentIds: ["agent-b"] }; const rows = [ { connection, grant: grant({ status: "revoked" }), dedicatedAgentIds: ["agent-a", "agent-b"] }, { connection, grant: grant({}), dedicatedAgentIds: ["agent-a", "agent-b"] }, own, ]; expect(dedicatedGitHubIdentityFor(rows, "agent-b").row).toBe(own); expect(dedicatedGitHubIdentityFor(rows.slice(0, 2), "agent-b").row).toBe(rows[1]); }); });