import { describe, expect, it, vi } from "vitest"; import { bootstrapAuditEventPersist, type AuditEventPersistBootstrapDeps, type AuditEventPersistWorkerHandle, } from "./audit-event-persist-bootstrap.js"; type WorkerImpl = NonNullable; function fakeWorker(): AuditEventPersistWorkerHandle { return { name: "outbox.audit", async close() {} }; } describe("bootstrapAuditEventPersist", () => { it("disables itself when WORKER_QUEUE_GROUP is not general", () => { const result = bootstrapAuditEventPersist({ WORKER_QUEUE_GROUP: "mail" }, { runInTransaction: async (fn) => fn({} as never) }); expect(result).toEqual({ status: "disabled", reason: "non-general worker group" }); }); it("disables itself when AUDIT_EVENT_PERSIST_ENABLED=false", () => { const result = bootstrapAuditEventPersist( { WORKER_QUEUE_GROUP: "general", AUDIT_EVENT_PERSIST_ENABLED: "false" }, { runInTransaction: async (fn) => fn({} as never) } ); expect(result).toEqual({ status: "disabled", reason: "AUDIT_EVENT_PERSIST_ENABLED=false" }); }); it("disables itself when no transaction runner is wired", () => { const result = bootstrapAuditEventPersist({ WORKER_QUEUE_GROUP: "general" }, {}); expect(result.status).toBe("disabled"); if (result.status === "disabled") { expect(result.reason).toContain("no transaction runner wired"); } }); it("subscribes a worker to outbox.audit with the default concurrency", () => { const inner = vi.fn(() => fakeWorker()); const createWorkerImpl = inner as unknown as WorkerImpl; const result = bootstrapAuditEventPersist( { WORKER_QUEUE_GROUP: "general" }, { runInTransaction: async (fn) => fn({} as never), createWorkerImpl } ); expect(result.status).toBe("started"); expect(inner).toHaveBeenCalledTimes(1); const args = inner.mock.calls[0] as unknown as [string, unknown, { concurrency?: number }]; expect(args[0]).toBe("outbox.audit"); expect(args[2]).toEqual({ concurrency: 5 }); }); it("honours AUDIT_EVENT_PERSIST_CONCURRENCY override", () => { const inner = vi.fn(() => fakeWorker()); const createWorkerImpl = inner as unknown as WorkerImpl; bootstrapAuditEventPersist( { WORKER_QUEUE_GROUP: "general", AUDIT_EVENT_PERSIST_CONCURRENCY: "12" }, { runInTransaction: async (fn) => fn({} as never), createWorkerImpl } ); const args = inner.mock.calls[0] as unknown as [string, unknown, { concurrency?: number }]; expect(args[2]).toEqual({ concurrency: 12 }); }); it("invokes the underlying processor when BullMQ delivers a job", async () => { let captured: ((job: { data: unknown }) => Promise) | null = null; const inner = vi.fn((_queue: unknown, processor: unknown) => { captured = processor as (job: { data: unknown }) => Promise; return fakeWorker(); }); const createWorkerImpl = inner as unknown as WorkerImpl; const runInTransaction = vi.fn(async (fn: (tx: unknown) => Promise) => fn({ auditEvent: { findFirst: async () => null, create: async () => ({ id: "audit_1", eventHash: "h1" }), }, }) ); bootstrapAuditEventPersist({ WORKER_QUEUE_GROUP: "general" }, { runInTransaction: runInTransaction as never, createWorkerImpl }); expect(captured).not.toBeNull(); if (captured) { await (captured as (job: { data: unknown }) => Promise)({ data: { outboxEventId: "outbox_1", tenantId: "tenant_1", eventType: "audit.event.create", payload: { action: "x.do", actorType: "system" }, }, }); } expect(runInTransaction).toHaveBeenCalledTimes(1); }); });