import { describe, it, expect, beforeEach, vi } from "vitest"; // --------------------------------------------------------------------------- // Mock cross-keychain with an in-memory store // --------------------------------------------------------------------------- const keychainStore = new Map(); vi.mock("cross-keychain", () => ({ getPassword: vi.fn( async (_service: string, account: string): Promise => { return keychainStore.get(account) ?? null; }, ), setPassword: vi.fn( async ( _service: string, account: string, password: string, ): Promise => { keychainStore.set(account, password); }, ), deletePassword: vi.fn( async (_service: string, account: string): Promise => { keychainStore.delete(account); }, ), })); // Import after mocking const { KeychainBackend } = await import("../backends/keychain-backend.js"); describe("KeychainBackend", () => { let backend: InstanceType; beforeEach(() => { keychainStore.clear(); backend = new KeychainBackend({ service: "test-vault" }); }); describe("check", () => { it("reports available when keychain is accessible", async () => { const status = await backend.check(); expect(status.available).toBe(true); }); }); describe("CRUD operations", () => { it("returns undefined for missing provider", async () => { expect(await backend.get("nonexistent")).toBeUndefined(); }); it("stores and retrieves an api_key entry", async () => { await backend.set("test", { type: "api_key", key: "sk-secret" }); const entry = await backend.get("test"); expect(entry).toEqual({ type: "api_key", key: "sk-secret" }); }); it("stores and retrieves an oauth entry", async () => { const oauth = { type: "oauth" as const, access: "acc-123", refresh: "ref-456", expires: 1700000000000, accountId: "acct-1", }; await backend.set("oauth-test", oauth); const entry = await backend.get("oauth-test"); expect(entry).toEqual(oauth); }); it("overwrites existing entries", async () => { await backend.set("test", { type: "api_key", key: "old" }); await backend.set("test", { type: "api_key", key: "new" }); const entry = await backend.get("test"); expect(entry).toEqual({ type: "api_key", key: "new" }); }); it("removes entries", async () => { await backend.set("test", { type: "api_key", key: "val" }); await backend.remove("test"); expect(await backend.get("test")).toBeUndefined(); }); }); describe("index management", () => { it("lists stored provider IDs", async () => { await backend.set("a", { type: "api_key", key: "1" }); await backend.set("b", { type: "api_key", key: "2" }); const list = await backend.list(); expect(list).toContain("a"); expect(list).toContain("b"); expect(list).toHaveLength(2); }); it("returns empty list when no providers stored", async () => { const list = await backend.list(); expect(list).toEqual([]); }); it("removes provider from index on remove", async () => { await backend.set("a", { type: "api_key", key: "1" }); await backend.set("b", { type: "api_key", key: "2" }); await backend.remove("a"); const list = await backend.list(); expect(list).toEqual(["b"]); }); it("does not duplicate index entries on overwrite", async () => { await backend.set("a", { type: "api_key", key: "1" }); await backend.set("a", { type: "api_key", key: "2" }); const list = await backend.list(); expect(list).toEqual(["a"]); }); }); });