import { describe, expect, it, vi } from "vitest"; import { processOutboxEvent, type OutboxTransport } from "./processor"; function record(overrides: Partial[0]> = {}) { return { topic: "page:72df08ab-50dd-4cbd-9a69-70949d34cf9f", aggregateId: "72df08ab-50dd-4cbd-9a69-70949d34cf9f", eventType: "page.updated", payload: { id: "72df08ab-50dd-4cbd-9a69-70949d34cf9f", version: 8, privateContent: "must not leave PostgreSQL", }, ...overrides, }; } describe("transactional outbox processor", () => { it("invalidates before publishing a privacy-safe event", async () => { const calls: string[] = []; const transport: OutboxTransport = { invalidate: vi.fn(async () => { calls.push("invalidate"); }), publish: vi.fn(async () => { calls.push("publish"); }), }; const event = await processOutboxEvent(record(), transport, 1234); expect(calls).toEqual(["invalidate", "publish"]); expect(transport.invalidate).toHaveBeenCalledWith( ["buzz:page:72df08ab-50dd-4cbd-9a69-70949d34cf9f"], 1234, ); expect(transport.publish).toHaveBeenCalledWith( "page:72df08ab-50dd-4cbd-9a69-70949d34cf9f", { type: "page.updated", id: "72df08ab-50dd-4cbd-9a69-70949d34cf9f", version: 8, }, ); expect(event).not.toHaveProperty("privateContent"); }); it("rejects a mismatched topic or malformed version", async () => { const transport: OutboxTransport = { invalidate: vi.fn(), publish: vi.fn(), }; await expect( processOutboxEvent(record({ topic: "admin" }), transport), ).rejects.toThrow("invalid public envelope"); await expect( processOutboxEvent(record({ payload: { id: record().aggregateId, version: "8" } }), transport), ).rejects.toThrow("invalid public envelope"); expect(transport.invalidate).not.toHaveBeenCalled(); }); });