const dispatcher = require("../../../../src/services/integrations/dispatcher"); describe("Integration Dispatcher", () => { it("dispatches erp.reimbursement.paid", async () => { const result = await dispatcher.dispatchOutboxEvent({ config: { erpSyncEnabled: true, erpSyncProvider: "mock" }, eventType: "erp.reimbursement.paid", payload: { reimbursementId: "rb-1", formNumber: "BX-001" }, }); expect(result.channel).toBe("erp"); expect(result.code).toBe("mock_synced"); }); it("dispatches notification.im with mock channel", async () => { const result = await dispatcher.dispatchOutboxEvent({ prisma: { notificationChannel: { findUnique: vi.fn().mockResolvedValue({ id: "channel-1", channel_type: "wecom", name: "test", enabled: true, config_json: { mode: "mock", routing: {} }, template_json: null, }), }, }, config: { imNotifierEnabled: true }, eventType: "notification.im", payload: { channelId: "channel-1", channelType: "wecom", title: "title", body: "body" }, }); expect(result.channel).toBe("im"); expect(result.code).toBe("mock_sent"); }); it("marks encrypted secret decrypt failure as non-retryable IM dispatch error", async () => { const encryptedSecret = "v1:bad:payload:value"; await expect( dispatcher.dispatchOutboxEvent({ prisma: { notificationChannel: { findUnique: vi.fn().mockResolvedValue({ id: "channel-2", channel_type: "wecom", name: "test", enabled: true, config_json: { mode: "wecom_webhook", webhookUrl: "https://example.com/wecom", secretEncrypted: encryptedSecret, timeoutMs: 500, routing: {}, }, template_json: null, }), }, }, config: { imNotifierEnabled: true, fieldEncryptionKey: Buffer.alloc(32, 7).toString("base64"), }, eventType: "notification.im", payload: { channelId: "channel-2", channelType: "wecom", title: "title", body: "body" }, }), ).rejects.toMatchObject({ name: "IntegrationDispatchError", code: "wecom_secret_decrypt_failed", retryable: false, }); }); it("throws error for unsupported event", async () => { await expect( dispatcher.dispatchOutboxEvent({ config: {}, eventType: "unknown.event", }), ).rejects.toThrow("unsupported_outbox_event:unknown.event"); }); });