import { describe, expect, it, vi } from "vitest"; import { LEGACY_EVENT_OUTBOX_QUEUE, LEGACY_EVENT_OUTBOX_TYPES, LegacyEventReceiverJobError, parseLegacyEventReceiverJob, runLegacyEventReceiverJob, type LegacyEventReceiverStore, } from "./legacy-event-receiver.js"; function buildJob(overrides: Record = {}): unknown { return { outboxEventId: "evt-1", tenantId: "tenant-a", eventType: "billing.subscription.ended", payload: { name: "subscription.ended", tenantId: "tenant-a", userId: null, data: JSON.stringify({ tenant: { id: "tenant-a" }, product: { title: "Pro" } }), description: "Acme subscription ended: Pro", }, ...overrides, }; } function inMemoryStore(): LegacyEventReceiverStore & { calls: number; last: unknown } { const state = { calls: 0, last: null as unknown }; return { calls: 0, last: null, async create(data) { state.calls += 1; state.last = data; this.calls = state.calls; this.last = state.last; return { id: `evt-row-${state.calls}` }; }, }; } function inMemoryIdempotency() { const claimed = new Set(); const completed = new Map(); return { async claim(key: string): Promise { if (claimed.has(key)) return false; claimed.add(key); return true; }, async complete(key: string, result: unknown): Promise { completed.set(key, result); }, async get(key: string) { if (completed.has(key)) { return { state: "complete" as const, result: completed.get(key) }; } if (claimed.has(key)) { return { state: "pending" as const }; } return null; }, async forget(key: string): Promise { claimed.delete(key); }, _claimed: claimed, }; } describe("legacy-event-receiver constants", () => { it("declares the documented outbox queue and event types", () => { expect(LEGACY_EVENT_OUTBOX_QUEUE).toBe("outbox.notification"); expect([...LEGACY_EVENT_OUTBOX_TYPES]).toEqual([ "billing.subscription.ended", "billing.subscription.cancelled", ]); }); }); describe("parseLegacyEventReceiverJob", () => { it("parses a well-formed envelope", () => { const parsed = parseLegacyEventReceiverJob(buildJob()); expect(parsed.outboxEventId).toBe("evt-1"); expect(parsed.eventType).toBe("billing.subscription.ended"); expect(parsed.payload.name).toBe("subscription.ended"); }); it("rejects when outboxEventId is missing", () => { expect(() => parseLegacyEventReceiverJob(buildJob({ outboxEventId: undefined }))).toThrow( LegacyEventReceiverJobError ); }); it("rejects unknown event types", () => { expect(() => parseLegacyEventReceiverJob(buildJob({ eventType: "mail.welcome.send" }))).toThrow( /unsupported eventType/ ); }); it("rejects when payload.name is empty", () => { expect(() => parseLegacyEventReceiverJob(buildJob({ payload: { name: "", data: "{}", description: null, userId: null, tenantId: "tenant-a" } })) ).toThrow(/payload\.name/); }); it("rejects non-object data", () => { expect(() => parseLegacyEventReceiverJob(null)).toThrow(LegacyEventReceiverJobError); expect(() => parseLegacyEventReceiverJob("nope" as unknown)).toThrow(LegacyEventReceiverJobError); }); }); describe("runLegacyEventReceiverJob", () => { it("persists the event when no idempotency store is wired", async () => { const store = inMemoryStore(); const result = await runLegacyEventReceiverJob(buildJob(), { store }); expect(result.status).toBe("persisted"); expect(store.calls).toBe(1); }); it("guards against duplicate delivery via the idempotency store", async () => { const store = inMemoryStore(); const idempotency = inMemoryIdempotency(); const first = await runLegacyEventReceiverJob(buildJob(), { store, idempotency }); const second = await runLegacyEventReceiverJob(buildJob(), { store, idempotency }); expect(first.status).toBe("persisted"); expect(second.status).toBe("skipped"); expect(store.calls).toBe(1); }); it("does not ack a redelivery while the previous receiver claim is still pending", async () => { const store = inMemoryStore(); const idempotency = inMemoryIdempotency(); await idempotency.claim("outbox:evt-1"); await expect(runLegacyEventReceiverJob(buildJob(), { store, idempotency })).rejects.toThrow(/already pending/); expect(store.calls).toBe(0); }); it("propagates store failures without poisoning the idempotency claim", async () => { const store: LegacyEventReceiverStore = { create: vi.fn(async () => { throw new Error("DB went away"); }), }; const idempotency = inMemoryIdempotency(); await expect(runLegacyEventReceiverJob(buildJob(), { store, idempotency })).rejects.toThrow(/DB went away/); // Claim must be released so the retry can re-attempt. expect(idempotency._claimed.size).toBe(0); }); });