import { describe, expect, it } from "vitest"; import { automationActionCatalog, automationTriggerCatalog } from "./automation.registry"; import { applyAutomationEditorOperation, buildAutomationDryRunPreview, buildAutomationEditorFlow, buildAutomationReversePlan, canConfirmAutomationDryRun, serializeAutomationEditorFlow, validateAutomationEditorFlow, } from "./automation.editor"; const catalog = [...automationTriggerCatalog, ...automationActionCatalog]; describe("automation editor contract", () => { it("validates missing triggers, missing parents, and loops with Klartext keys", () => { const missingTrigger = buildAutomationEditorFlow({ flowData: {}, steps: [{ stepKey: "task", stepKind: "action", nodeType: "task.create", parentStepIds: [] }], }); expect(validateAutomationEditorFlow(missingTrigger).issues.map((issue) => issue.code)).toContain("missing_trigger"); expect(validateAutomationEditorFlow(missingTrigger).issues.map((issue) => issue.code)).toContain("unconnected_step"); const loop = buildAutomationEditorFlow({ flowData: {}, steps: [ { stepKey: "trigger", stepKind: "trigger", nodeType: "lead.qualified", parentStepIds: ["task"] }, { stepKey: "task", stepKind: "action", nodeType: "task.create", parentStepIds: ["trigger"] }, ], }); expect(validateAutomationEditorFlow(loop).issues).toContainEqual(expect.objectContaining({ code: "loop_detected", messageKey: "automation.editor.validation.loopDetected" })); }); it("applies graph operations with undo and redo history", () => { const initial = buildAutomationEditorFlow({ flowData: {}, steps: [] }); const withTrigger = applyAutomationEditorOperation(initial, { type: "add-node", nodeType: "lead.qualified" }, catalog); const withTask = applyAutomationEditorOperation(withTrigger, { type: "add-node", nodeType: "task.create", parentStepKey: withTrigger.steps[0]?.stepKey }, catalog); const connected = applyAutomationEditorOperation(withTask, { type: "connect", fromStepKey: withTrigger.steps[0]!.stepKey, toStepKey: withTask.steps[1]!.stepKey }, catalog); expect(validateAutomationEditorFlow(connected).canActivate).toBe(true); expect(serializeAutomationEditorFlow(connected).steps).toHaveLength(2); const undone = applyAutomationEditorOperation(connected, { type: "undo" }, catalog); expect(undone.history.future).toHaveLength(1); const redone = applyAutomationEditorOperation(undone, { type: "redo" }, catalog); expect(redone.steps).toHaveLength(2); }); it("builds dry-run previews with confirmation threshold", () => { const flow = buildAutomationEditorFlow({ flowData: {}, steps: [ { stepKey: "trigger", stepKind: "trigger", nodeType: "lead.qualified", parentStepIds: [] }, { stepKey: "task", stepKind: "action", nodeType: "task.create", parentStepIds: ["trigger"] }, ], }); const preview = buildAutomationDryRunPreview({ flow, matchedRecords: 101, examples: ["Lead A", "Lead B", "Lead C", "Lead D", "Lead E", "Lead F"] }); expect(preview.requiresConfirmation).toBe(true); expect(preview.examples).toHaveLength(5); expect(canConfirmAutomationDryRun({ matchedRecords: 101, confirmationPhrase: "falsch" })).toBe(false); expect(canConfirmAutomationDryRun({ matchedRecords: 101, confirmationPhrase: preview.confirmationPhrase })).toBe(true); }); it("builds 24h reverse plans and marks deliveries as not reversible", () => { const plan = buildAutomationReversePlan({ reverseWindowExpiresAt: "2026-07-02T10:00:00.000Z", now: new Date("2026-07-02T09:00:00.000Z"), steps: [ { stepKey: "task", nodeType: "task.create" }, { stepKey: "notify", nodeType: "notification.send" }, ], }); expect(plan.canReverse).toBe(true); expect(plan.items).toEqual([ expect.objectContaining({ actionKey: "automation.editor.reverse.actions.deleteTask", reversible: true }), expect.objectContaining({ actionKey: "automation.editor.reverse.actions.deliveryNotReversible", reversible: false }), ]); }); });