import { beforeEach, describe, expect, it, vi } from "vitest"; const publishOutboxEventMock = vi.fn(); vi.mock("@toolctrl/db/outbox", () => ({ publishOutboxEvent: publishOutboxEventMock, })); vi.mock("~/modules/notifications/services/.server/NotificationService", () => ({ default: { sendToRoles: vi.fn(), }, })); vi.mock("~/utils/logger.server", () => ({ createScopedLogger: () => ({ error: vi.fn(), info: vi.fn(), warn: vi.fn(), }), })); vi.mock("~/utils/db.server", () => ({ db: { $transaction: vi.fn(), }, })); describe("EventsService", () => { beforeEach(() => { vi.clearAllMocks(); }); it("publishes workflow trigger outbox events after the application event row is created", async () => { const eventCreateMock = vi.fn().mockResolvedValue({ id: "event_1" }); const tx = { event: { create: eventCreateMock, }, outboxEvent: { create: vi.fn(), findUnique: vi.fn(), }, }; const EventsService = (await import("./EventsService")).default; await EventsService.createInTransaction(tx as never, { event: "row.created", tenantId: "tenant_1", userId: "user_1", data: { id: "row_1", entity: { slug: "contacts", title: "Contacts" } }, }); expect(eventCreateMock).toHaveBeenCalledWith( expect.objectContaining({ data: expect.objectContaining({ name: "row.created", tenantId: "tenant_1", userId: "user_1", data: JSON.stringify({ id: "row_1", entity: { slug: "contacts", title: "Contacts" } }), }), }) ); expect(publishOutboxEventMock).toHaveBeenCalledWith( tx, expect.objectContaining({ tenantId: "tenant_1", type: "workflow.trigger.event", dispatchKey: "workflow-trigger:event_1", payload: { event: "row.created", data: { id: "row_1", entity: { slug: "contacts", title: "Contacts" } }, userId: "user_1", }, }) ); expect(eventCreateMock.mock.invocationCallOrder[0]).toBeLessThan(publishOutboxEventMock.mock.invocationCallOrder[0]); }); });