import { describe, expect, it, vi } from "vitest"; import { AUTOMATION_DRY_RUN_EVENT_LIMIT, buildAutomationDryRunEventSample, loadAutomationDryRunEventSampleWithTx } from "./automation-dry-run.server.js"; function event(id: string, eventName: string, data: Record, createdAt = new Date("2026-08-01T10:00:00.000Z")) { return { id, payload: { event: eventName, data, userId: null }, createdAt }; } describe("automation dry-run event sample", () => { it("matches real domain events against the current trigger config and exposes only safe identifiers", () => { const sample = buildAutomationDryRunEventSample({ ruleId: "rule-1", triggerKey: "forms.submitted", triggerConfig: { formular: "contact-form" }, events: [ event("outbox-1", "form.submitted", { formId: "contact-form", email: "secret@example.com" }), event("outbox-2", "form.submitted", { formId: "other-form", email: "other@example.com" }), event("outbox-3", "kontakt.created", { kontaktId: "kontakt-1" }), ], }); expect(sample).toEqual({ matchedRecords: 1, inspectedEvents: 3, sampleLimitReached: false, examples: ["contact-form"] }); expect(JSON.stringify(sample)).not.toContain("secret@example.com"); }); it("matches portal events by their direct trigger key", () => { expect( buildAutomationDryRunEventSample({ ruleId: "rule-1", triggerKey: "portal.upload_completed", triggerConfig: {}, events: [event("outbox-1", "portal.upload_completed", { projectId: "project-1" })], }) ).toEqual({ matchedRecords: 1, inspectedEvents: 1, sampleLimitReached: false, examples: ["project-1"] }); }); it("caps the inspected history and reports that the sample was truncated", () => { const events = Array.from({ length: AUTOMATION_DRY_RUN_EVENT_LIMIT + 1 }, (_, index) => event(`outbox-${index}`, "time.schedule", { automationRuleId: "rule-1", recurrence: "taeglich", time: "08:00" }) ); const sample = buildAutomationDryRunEventSample({ ruleId: "rule-1", triggerKey: "time.schedule", triggerConfig: { wiederholung: "taeglich", uhrzeit: "08:00" }, events, }); expect(sample.matchedRecords).toBe(AUTOMATION_DRY_RUN_EVENT_LIMIT); expect(sample.inspectedEvents).toBe(AUTOMATION_DRY_RUN_EVENT_LIMIT); expect(sample.sampleLimitReached).toBe(true); }); it("queries only tenant-owned workflow-trigger events for the relevant event names", async () => { const findMany = vi.fn().mockResolvedValue([event("outbox-1", "kontakt.created", { kontaktId: "kontakt-1" })]); const sample = await loadAutomationDryRunEventSampleWithTx( { outboxEvent: { findMany } } as never, { tenantId: "tenant-1", ruleId: "rule-1", triggerKey: "crm.kontakt_created", triggerConfig: { quelle: "alle" } } ); expect(sample.matchedRecords).toBe(1); expect(findMany).toHaveBeenCalledWith( expect.objectContaining({ where: expect.objectContaining({ tenantId: "tenant-1", eventType: "workflow.trigger.event" }), take: AUTOMATION_DRY_RUN_EVENT_LIMIT + 1, }) ); expect(findMany.mock.calls[0]?.[0].where.OR).toEqual( expect.arrayContaining([ { payload: { path: ["event"], equals: "kontakt.created" } }, { payload: { path: ["event"], equals: "crm.kontakt_created" } }, ]) ); }); });