import { describe, expect, it, vi } from "vitest"; import { runAutomationSchedulerForTenantWithTx } from "./automation-scheduler.server.js"; describe("automation scheduler", () => { it("emits only the due daily schedule rule with an idempotent local-time bucket", async () => { const tx = fixtureTx([ rule("rule_daily", "time.schedule", { wiederholung: "taeglich", uhrzeit: "08:00" }), rule("rule_later", "time.schedule", { wiederholung: "taeglich", uhrzeit: "09:00" }), ]); await expect(runAutomationSchedulerForTenantWithTx(tx as never, { tenantId: "tenant_1", now: new Date("2026-08-03T06:02:00.000Z") })).resolves.toEqual({ inspectedRules: 2, scheduledEvents: 1, skippedInvalidRules: 0, truncatedRules: 0, }); expect(tx.outboxEvent.create).toHaveBeenCalledWith({ data: expect.objectContaining({ dispatchKey: "workflow-trigger:automation-time:rule_daily:2026-08-03:08-00", payload: expect.objectContaining({ event: "time.schedule", data: expect.objectContaining({ automationRuleId: "rule_daily", wiederholung: "taeglich", uhrzeit: "08:00" }), }), }), }); }); it("respects the configured weekday and day of month", async () => { const tuesday = fixtureTx([ rule("rule_weekly", "time.schedule", { wiederholung: "woechentlich", uhrzeit: "08:00", wochentag: "dienstag" }), rule("rule_monthly", "time.schedule", { wiederholung: "monatlich", uhrzeit: "08:00", monatstag: "15" }), ]); await runAutomationSchedulerForTenantWithTx(tuesday as never, { tenantId: "tenant_1", now: new Date("2026-08-04T06:01:00.000Z"), }); expect(tuesday.outboxEvent.create).toHaveBeenCalledTimes(1); const fifteenth = fixtureTx([rule("rule_monthly", "time.schedule", { wiederholung: "monatlich", uhrzeit: "08:00", monatstag: "15" })]); await runAutomationSchedulerForTenantWithTx(fifteenth as never, { tenantId: "tenant_1", now: new Date("2026-08-15T06:01:00.000Z"), }); expect(fifteenth.outboxEvent.create).toHaveBeenCalledTimes(1); }); it("queries and emits task, project and invoice deadline targets for their exact rules", async () => { const deadline = new Date("2026-08-06T06:00:00.000Z"); const tx = fixtureTx([ rule("rule_task", "projects.deadline_soon", { fristtyp: "aufgabenFaelligkeit", leadTime: "3", leadTimeUnit: "tage" }), rule("rule_project", "projects.deadline_soon", { fristtyp: "projektDeadline", leadTime: "3", leadTimeUnit: "tage" }), rule("rule_invoice", "projects.deadline_soon", { fristtyp: "zahlungsziel", leadTime: "3", leadTimeUnit: "tage" }), ]); tx.task.findMany.mockResolvedValue([{ id: "task_1", deadline, linkedEntityType: "project", linkedEntityId: "project_1" }]); tx.projekt.findMany.mockResolvedValue([{ id: "project_1", deadline }]); tx.rechnung.findMany.mockResolvedValue([{ id: "invoice_1", projektId: "project_1", faelligAm: deadline }]); await expect(runAutomationSchedulerForTenantWithTx(tx as never, { tenantId: "tenant_1", now: new Date("2026-08-03T06:02:00.000Z") })).resolves.toEqual({ inspectedRules: 3, scheduledEvents: 3, skippedInvalidRules: 0, truncatedRules: 0, }); const payloads = tx.outboxEvent.create.mock.calls.map((call) => call[0].data.payload.data); expect(payloads).toEqual( expect.arrayContaining([ expect.objectContaining({ automationRuleId: "rule_task", entityType: "task", taskId: "task_1", projectId: "project_1" }), expect.objectContaining({ automationRuleId: "rule_project", entityType: "project", projectId: "project_1" }), expect.objectContaining({ automationRuleId: "rule_invoice", entityType: "invoice", invoiceId: "invoice_1", projectId: "project_1" }), ]) ); }); it("skips malformed persisted configs instead of firing broad rules", async () => { const tx = fixtureTx([ rule("bad_time", "time.schedule", { wiederholung: "taeglich", uhrzeit: "morgen" }), rule("bad_deadline", "projects.deadline_soon", { fristtyp: "projektDeadline", leadTime: "x", leadTimeUnit: "tage" }), ]); await expect(runAutomationSchedulerForTenantWithTx(tx as never, { tenantId: "tenant_1" })).resolves.toEqual({ inspectedRules: 2, scheduledEvents: 0, skippedInvalidRules: 2, truncatedRules: 0, }); expect(tx.outboxEvent.create).not.toHaveBeenCalled(); }); }); function rule(id: string, triggerKey: string, config: Record) { return { id, triggerKey, steps: [{ stepKind: "trigger", nodeType: triggerKey, config }] }; } function fixtureTx(rules: ReturnType[]) { return { automationRule: { findMany: vi.fn().mockResolvedValue(rules) }, task: { findMany: vi.fn().mockResolvedValue([]) }, projekt: { findMany: vi.fn().mockResolvedValue([]) }, rechnung: { findMany: vi.fn().mockResolvedValue([]) }, outboxEvent: { findUnique: vi.fn().mockResolvedValue(null), create: vi.fn(async ({ data }) => ({ id: `outbox_${data.dispatchKey}`, ...data })), }, }; }