import { beforeEach, describe, expect, it } from "vitest"; import { checkCatalogDrift, clearIntegrationAdapters, getIntegrationAdapter, listIntegrationAdapterKeys, registerIntegrationAdapter, type IntegrationAdapter, } from "./integration-adapters.js"; // R-P5 — type-safe adapter registry + drift check (DoD #2). A new integration is // added by registering an adapter (no core change); drift between registry and // the App catalog surfaces a Klartext warning. const fakeAdapter = (key: string): IntegrationAdapter => ({ key, connectionType: "api_key", validateConnection: () => Promise.resolve({ ok: true, health: "active", messageKey: "integrations.health.active" }), healthCheck: () => Promise.resolve({ ok: true, health: "active", messageKey: "integrations.health.active" }), sync: () => Promise.resolve({ items: [], summaryKey: "integrations.sync.summary.empty" }), }); beforeEach(() => clearIntegrationAdapters()); describe("integration adapter registry", () => { it("registers and looks up an adapter by key", () => { registerIntegrationAdapter(fakeAdapter("acme-crm")); expect(getIntegrationAdapter("acme-crm")?.connectionType).toBe("api_key"); expect(getIntegrationAdapter("missing")).toBeUndefined(); }); it("lists registered keys sorted", () => { registerIntegrationAdapter(fakeAdapter("zeta")); registerIntegrationAdapter(fakeAdapter("alpha")); expect(listIntegrationAdapterKeys()).toEqual(["alpha", "zeta"]); }); }); describe("checkCatalogDrift", () => { it("reports in-sync when adapters and catalog match", () => { const drift = checkCatalogDrift(["gmail", "mollie"], ["mollie", "gmail"]); expect(drift.inSync).toBe(true); expect(drift.missingAdapters).toEqual([]); expect(drift.orphanAdapters).toEqual([]); }); it("flags a catalog App with no adapter (cannot connect)", () => { const drift = checkCatalogDrift(["gmail"], ["gmail", "aircall"]); expect(drift.inSync).toBe(false); expect(drift.missingAdapters).toEqual(["aircall"]); expect(drift.orphanAdapters).toEqual([]); }); it("flags an orphan adapter with no catalog App (invisible)", () => { const drift = checkCatalogDrift(["gmail", "legacy"], ["gmail"]); expect(drift.inSync).toBe(false); expect(drift.missingAdapters).toEqual([]); expect(drift.orphanAdapters).toEqual(["legacy"]); }); });