import { describe, expect, it } from "vitest"; import { buildLagoBillingSyncDispatchKey, buildLagoBillingSyncOutboxArgs, buildMolliePaymentSyncDispatchKey, buildMolliePaymentSyncOutboxArgs, buildMolliePaymentSyncOutboxArgsFromPayment, LAGO_BILLING_SYNC_EVENT_TYPE, MOLLIE_PAYMENT_SYNC_EVENT_TYPE, } from "./sync-outbox.js"; import { buildLagoWebhookSyncOutboxArgs, parseLagoWebhookEvent } from "./webhook.js"; describe("Lago billing sync outbox", () => { it("builds idempotent outbox args for Lago pull sync actions", () => { const args = buildLagoBillingSyncOutboxArgs({ tenantId: "tenant_1", action: "subscription.upsert", externalId: "sub_1", source: "pull", receivedAt: new Date("2026-07-04T00:00:00Z"), }); expect(args).toEqual({ tenantId: "tenant_1", type: LAGO_BILLING_SYNC_EVENT_TYPE, dispatchKey: "lago:subscription.upsert:tenant_1:sub_1", payload: { provider: "lago", action: "subscription.upsert", tenantId: "tenant_1", externalId: "sub_1", source: "pull", receivedAt: "2026-07-04T00:00:00.000Z", }, }); }); it("sanitizes dispatch keys to the outbox-safe character set", () => { expect( buildLagoBillingSyncDispatchKey({ tenantId: "tenant/with/slashes", action: "invoice.refresh", externalId: "invoice 123", }) ).toBe("lago:invoice.refresh:tenant_with_slashes:invoice_123"); }); it("maps Lago webhook events to idempotent sync events", () => { const event = parseLagoWebhookEvent({ event_type: "invoice.finalized", lago_id: "invoice_1", external_customer_id: "tenant_1", data: { status: "finalized" }, }); expect( buildLagoWebhookSyncOutboxArgs({ tenantId: "tenant_1", event, receivedAt: new Date("2026-07-04T00:00:00Z"), }) ).toMatchObject({ tenantId: "tenant_1", type: LAGO_BILLING_SYNC_EVENT_TYPE, dispatchKey: "lago:invoice.refresh:tenant_1:invoice_1", payload: { action: "invoice.refresh", externalId: "invoice_1", source: "webhook", }, }); }); }); describe("Mollie payment sync outbox", () => { it("builds idempotent outbox args for paid payment events", () => { const args = buildMolliePaymentSyncOutboxArgs({ tenantId: "tenant_1", invoiceExternalId: "invoice_1", providerPaymentId: "tr_123", paymentExternalId: "payment_1", status: "paid", amountCents: 4900, amountRefundedCents: 0, amountRemainingCents: 0, source: "webhook", receivedAt: new Date("2026-07-04T00:00:00Z"), }); expect(args).toEqual({ tenantId: "tenant_1", type: MOLLIE_PAYMENT_SYNC_EVENT_TYPE, dispatchKey: "mollie:payment.paid:tenant_1:tr_123:paid:0", payload: { provider: "mollie", action: "payment.paid", tenantId: "tenant_1", invoiceExternalId: "invoice_1", providerPaymentId: "tr_123", paymentExternalId: "payment_1", status: "paid", amountCents: 4900, amountRefundedCents: 0, amountRemainingCents: 0, source: "webhook", receivedAt: "2026-07-04T00:00:00.000Z", }, }); }); it("records failure, expired and refund statuses as distinct billing events", () => { expect( buildMolliePaymentSyncOutboxArgs({ tenantId: "tenant_1", invoiceExternalId: "invoice_1", providerPaymentId: "tr_123", paymentExternalId: null, status: "failed", amountCents: 4900, amountRefundedCents: 0, amountRemainingCents: 4900, source: "webhook", }).payload ).toMatchObject({ action: "payment.failed" }); expect( buildMolliePaymentSyncOutboxArgs({ tenantId: "tenant_1", invoiceExternalId: "invoice_1", providerPaymentId: "tr_123", paymentExternalId: null, status: "expired", amountCents: 4900, amountRefundedCents: 0, amountRemainingCents: 4900, source: "webhook", }).payload ).toMatchObject({ action: "payment.expired" }); expect( buildMolliePaymentSyncOutboxArgs({ tenantId: "tenant_1", invoiceExternalId: "invoice_1", providerPaymentId: "tr_123", paymentExternalId: null, status: "refunded", amountCents: 4900, amountRefundedCents: 4900, amountRemainingCents: 0, source: "webhook", }).payload ).toMatchObject({ action: "payment.refunded" }); }); it("sanitizes Mollie dispatch keys and can build them from verified payments", () => { expect( buildMolliePaymentSyncDispatchKey({ tenantId: "tenant/with/slashes", providerPaymentId: "tr 123", status: "refunded", amountRefundedCents: 4900, }) ).toBe("mollie:payment.refunded:tenant_with_slashes:tr_123:refunded:4900"); expect( buildMolliePaymentSyncOutboxArgsFromPayment( { provider: "mollie", providerPaymentId: "tr_123", externalId: "payment_1", status: "refunded", amountCents: 4900, amountRefundedCents: 4900, amountRemainingCents: 0, currency: "EUR", checkoutUrl: null, paidAt: null, expiresAt: null, metadata: { tenantId: "tenant_1", invoiceExternalId: "invoice_1", paymentExternalId: "payment_1", lagoCustomerId: null, lagoSubscriptionId: null, planCode: null, seatQuantity: null, }, }, { source: "webhook", receivedAt: new Date("2026-07-04T00:00:00Z") } ) ).toMatchObject({ tenantId: "tenant_1", dispatchKey: "mollie:payment.refunded:tenant_1:tr_123:refunded:4900", payload: { action: "payment.refunded", invoiceExternalId: "invoice_1", }, }); }); });