import { describe, expect, it, vi } from "vitest"; import { ADMIN_CALL_DISPOSITIONS, createAdminTelephonyService, type AdminTelephonyRepository, } from "./admin-telephony.server.js"; function repository( overrides: Partial = {} ): AdminTelephonyRepository { return { activateConnection: vi.fn().mockResolvedValue({ id: "phone_connection_1" }), getCallSnapshot: vi.fn().mockResolvedValue(null), listPhoneCandidates: vi.fn().mockResolvedValue([]), persistCallEvent: vi.fn().mockResolvedValue({ call: { id: "call_1", matchStatus: "unmatched" }, duplicate: false, matchStatus: "unmatched", }), ...overrides, } as never; } describe("admin telephony service", () => { it("keeps the supplied disposition catalog stable and ordered", () => { expect(ADMIN_CALL_DISPOSITIONS).toHaveLength(11); expect(ADMIN_CALL_DISPOSITIONS.map(({ code }) => code)).toEqual([ "reached", "voicemail", "not_interested", "interested", "interested_1_3_months", "poor_fit", "declined", "gatekeeper_no_prospect_contact", "wrong_or_old_number", "customer", "rescheduling", ]); }); it("normalizes an Aircall connection only after the integrations permission", async () => { const repo = repository(); const authorize = vi.fn(); const service = createAdminTelephonyService({ repository: repo, authorize, }); await service.activateConnection({ adminUserId: "admin_1", connectionId: "connection_1", providerUserId: "user_1", providerUserEmail: " Sales@Example.test ", defaultProviderNumberId: "number_1", webhookRegistrationId: "webhook_1", webhookTokenHash: "A".repeat(64), phoneNumbers: [ { providerNumberId: "number_1", label: " Berlin ", e164: "+49 (30) 12345678", }, ], actor: { adminUserId: "admin_1", reason: " Aircall verbinden ", }, }); expect(authorize).toHaveBeenCalledWith( "admin_sales.integrations.manage" ); expect(repo.activateConnection).toHaveBeenCalledWith( expect.objectContaining({ providerUserEmail: "sales@example.test", webhookTokenHash: "a".repeat(64), phoneNumbers: [ expect.objectContaining({ label: "Berlin", e164: "+493012345678", selected: true, }), ], }) ); }); it("rejects a default number that was not returned by Aircall", async () => { const service = createAdminTelephonyService({ repository: repository(), authorize: vi.fn(), }); await expect( service.activateConnection({ adminUserId: "admin_1", connectionId: "connection_1", providerUserId: "user_1", defaultProviderNumberId: "number_missing", webhookRegistrationId: "webhook_1", webhookTokenHash: "b".repeat(64), phoneNumbers: [ { providerNumberId: "number_1", label: "Berlin", e164: "+493012345678", }, ], actor: { adminUserId: "admin_1", reason: "Aircall verbinden" }, }) ).rejects.toMatchObject({ code: "default_number_unavailable", }); }); it("persists a verified provider event without an interactive permission bypass", async () => { const repo = repository(); const authorize = vi.fn(); const service = createAdminTelephonyService({ repository: repo, authorize, }); const occurredAt = new Date("2026-07-24T08:00:00.000Z"); await service.persistVerifiedProviderEvent({ phoneConnectionId: "phone_connection_1", adminUserId: "admin_1", providerCallId: "provider_call_1", dedupeKey: "event_1", providerEvent: "call.ended", occurredAt, externalNumber: "+49 30 12345678", snapshot: { direction: "inbound", status: "completed", fromNumber: "+493012345678", toNumber: "+493099999999", startedAt: occurredAt, endedAt: occurredAt, durationSeconds: 42, }, candidates: [ { entityType: "contact", entityId: "contact_1", confidenceBasisPoints: 10_000, matchReason: "e164", }, ], }); expect(authorize).not.toHaveBeenCalled(); expect(repo.persistCallEvent).toHaveBeenCalledWith( expect.objectContaining({ externalNumber: "+493012345678", candidates: [ expect.objectContaining({ entityId: "contact_1", confidenceBasisPoints: 10_000, }), ], }) ); }); });