75 lines
3.7 KiB
TypeScript
75 lines
3.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
AUTOMATION_MODULE_KEY,
|
|
AUTOMATION_READ_PERMISSION,
|
|
automationActionCatalog,
|
|
automationTemplateCatalog,
|
|
automationTriggerCatalog,
|
|
buildAutomationIdempotencyKey,
|
|
buildReverseWindowExpiresAt,
|
|
evaluateLoopGuard,
|
|
evaluateTriggerVolume,
|
|
listAutomationNodes,
|
|
planAutomationRetry,
|
|
} from "./automation.registry";
|
|
|
|
describe("automation registry contract", () => {
|
|
it("uses the R-P12a module and permission keys", () => {
|
|
expect(AUTOMATION_MODULE_KEY).toBe("automation");
|
|
expect(AUTOMATION_READ_PERMISSION).toBe("automation.read");
|
|
});
|
|
|
|
it("covers the required trigger and action groups", () => {
|
|
expect(new Set(automationTriggerCatalog.map((node) => node.group))).toEqual(new Set(["crm", "lead", "sales", "projects", "email", "forms", "booking", "calendar", "time", "webhook"]));
|
|
expect(new Set(automationActionCatalog.map((node) => node.group))).toEqual(new Set(["crm", "notification", "http", "task", "flow_control", "kwp"]));
|
|
expect(automationActionCatalog.find((node) => node.key === "kwp.push_project")?.availability).toBe("extension_slot");
|
|
expect(listAutomationNodes()).toHaveLength(automationTriggerCatalog.length + automationActionCatalog.length);
|
|
});
|
|
|
|
it("ships platform templates that can become rules", () => {
|
|
expect(automationTemplateCatalog.map((template) => template.key)).toEqual(["lead-qualified-follow-up", "project-deadline-warning", "form-submission-create-task"]);
|
|
for (const template of automationTemplateCatalog) {
|
|
expect(template.steps[0]?.kind).toBe("trigger");
|
|
expect(template.steps.map((step) => step.stepKey)).toContain("trigger");
|
|
}
|
|
});
|
|
|
|
it("detects dependency cycles for loop guard warnings", () => {
|
|
expect(
|
|
evaluateLoopGuard([
|
|
{ stepKey: "a", nodeType: "trigger", parentStepIds: ["c"] },
|
|
{ stepKey: "b", nodeType: "task.create", parentStepIds: ["a"] },
|
|
{ stepKey: "c", nodeType: "notification.send", parentStepIds: ["b"] },
|
|
])
|
|
).toMatchObject({ ok: false, cycleStepKeys: ["a", "b", "c", "a"] });
|
|
|
|
expect(
|
|
evaluateLoopGuard([
|
|
{ stepKey: "trigger", nodeType: "lead.qualified", parentStepIds: [] },
|
|
{ stepKey: "task", nodeType: "task.create", parentStepIds: ["trigger"] },
|
|
]).ok
|
|
).toBe(true);
|
|
});
|
|
|
|
it("builds idempotency keys and 24h reverse windows deterministically", () => {
|
|
expect(buildAutomationIdempotencyKey({ tenantId: "t1", ruleId: "r1", triggerKey: "lead.qualified", sourceEventId: "evt1" })).toBe("t1:r1:lead.qualified:evt1");
|
|
expect(buildReverseWindowExpiresAt(new Date("2026-07-01T10:00:00.000Z")).toISOString()).toBe("2026-07-02T10:00:00.000Z");
|
|
});
|
|
|
|
it("pauses anomalous trigger volumes with Klartext diagnostics", () => {
|
|
const result = evaluateTriggerVolume({ triggerKey: "lead.qualified", countInWindow: 2000, windowMinutes: 5, typicalDailyMin: 5, typicalDailyMax: 10, hardLimitInWindow: 2000 });
|
|
|
|
expect(result.action).toBe("pause");
|
|
expect(result.summaryText).toContain("2000 automation triggers in 5 minutes");
|
|
expect(result.summaryText).toContain("typical volume is 5-10 per day");
|
|
});
|
|
|
|
it("plans bounded auto retries for resumable executions", () => {
|
|
const retry = planAutomationRetry({ attempt: 1, baseDelayMs: 1_000, now: new Date("2026-07-01T10:00:00.000Z") });
|
|
expect(retry).toMatchObject({ shouldRetry: true, nextAttempt: 2, summaryKey: "automation.guards.retryScheduled" });
|
|
expect(retry.nextRetryAt?.toISOString()).toBe("2026-07-01T10:00:01.000Z");
|
|
|
|
expect(planAutomationRetry({ attempt: 3, maxAttempts: 3 })).toMatchObject({ shouldRetry: false, nextAttempt: 4, summaryKey: "automation.guards.retryExhausted" });
|
|
});
|
|
});
|