111 lines
4.3 KiB
TypeScript
111 lines
4.3 KiB
TypeScript
// R-P12c / R-P12b DoD 4 — manual run path: startAutomationExecutionForRule
|
|
// dispatches queued live runs durably to the forward executor (outbox row in
|
|
// the same transaction), dedups on the idempotency key and falls back to the
|
|
// rule's own trigger key for run-now/retry/resume.
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
tx: {
|
|
automationRule: { findFirst: vi.fn(), update: vi.fn() },
|
|
automationExecution: { findFirst: vi.fn(), create: vi.fn() },
|
|
automationTriggerMeter: { upsert: vi.fn(), update: vi.fn() },
|
|
outboxEvent: { create: vi.fn(), findUnique: vi.fn() },
|
|
},
|
|
}));
|
|
|
|
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
|
|
withTenantRLS: async (_tenantId: string, callback: (tx: typeof mocks.tx) => Promise<unknown>) => callback(mocks.tx),
|
|
}));
|
|
|
|
import { startAutomationExecutionForRule } from "./automation.server";
|
|
|
|
const RULE = {
|
|
id: "rule-1",
|
|
enabled: true,
|
|
status: "active",
|
|
triggerKey: "forms.submitted",
|
|
steps: [
|
|
{ id: "rs-1", stepKey: "trigger", nodeType: "forms.submitted", stepKind: "trigger", parentStepIds: [], sortOrder: 0, summaryText: "t" },
|
|
{ id: "rs-2", stepKey: "create-task", nodeType: "task.create", stepKind: "action", parentStepIds: ["trigger"], sortOrder: 1, summaryText: "a" },
|
|
],
|
|
};
|
|
|
|
describe("startAutomationExecutionForRule", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.tx.automationRule.findFirst.mockResolvedValue(RULE);
|
|
mocks.tx.automationExecution.findFirst.mockResolvedValue(null);
|
|
mocks.tx.automationExecution.create.mockResolvedValue({ id: "exec-1", status: "queued" });
|
|
mocks.tx.automationTriggerMeter.upsert.mockResolvedValue({ count: 1 });
|
|
mocks.tx.outboxEvent.create.mockImplementation(async (args: { data: unknown }) => args.data);
|
|
});
|
|
|
|
it("queues a run with the rule's own trigger key and publishes the executor dispatch in the same tx", async () => {
|
|
const result = await startAutomationExecutionForRule({
|
|
tenantId: "tenant-1",
|
|
tenantSlug: "tenant-1",
|
|
userId: "user-1",
|
|
ruleId: "rule-1",
|
|
triggerPayload: { source: "manual" },
|
|
sourceEventId: "manual:abc",
|
|
});
|
|
|
|
expect(result).toEqual({ id: "exec-1", status: "queued", deduplicated: false });
|
|
expect(mocks.tx.automationExecution.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ triggerKey: "forms.submitted" }) })
|
|
);
|
|
expect(mocks.tx.outboxEvent.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
data: expect.objectContaining({ eventType: "automation.execution.requested", dispatchKey: "automation-exec:exec-1:initial" }),
|
|
})
|
|
);
|
|
});
|
|
|
|
it("does not dispatch dry runs or deduplicated runs", async () => {
|
|
await startAutomationExecutionForRule({
|
|
tenantId: "tenant-1",
|
|
tenantSlug: "tenant-1",
|
|
userId: "user-1",
|
|
ruleId: "rule-1",
|
|
triggerPayload: {},
|
|
dryRun: true,
|
|
});
|
|
expect(mocks.tx.outboxEvent.create).not.toHaveBeenCalled();
|
|
|
|
mocks.tx.automationExecution.findFirst.mockResolvedValue({ id: "exec-0", status: "success" });
|
|
const result = await startAutomationExecutionForRule({
|
|
tenantId: "tenant-1",
|
|
tenantSlug: "tenant-1",
|
|
userId: "user-1",
|
|
ruleId: "rule-1",
|
|
triggerPayload: {},
|
|
sourceEventId: "manual:abc",
|
|
});
|
|
expect(result.deduplicated).toBe(true);
|
|
expect(mocks.tx.outboxEvent.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes resumeFromStepId through to the new execution", async () => {
|
|
await startAutomationExecutionForRule({
|
|
tenantId: "tenant-1",
|
|
tenantSlug: "tenant-1",
|
|
userId: "user-1",
|
|
ruleId: "rule-1",
|
|
triggerPayload: {},
|
|
sourceEventId: "resume:exec-0:xyz",
|
|
resumeFromStepId: "rs-2",
|
|
});
|
|
expect(mocks.tx.automationExecution.create).toHaveBeenCalledWith(
|
|
expect.objectContaining({ data: expect.objectContaining({ resumeFromStepId: "rs-2" }) })
|
|
);
|
|
});
|
|
|
|
it("fails closed when the rule has no trigger key", async () => {
|
|
mocks.tx.automationRule.findFirst.mockResolvedValue({ ...RULE, triggerKey: null });
|
|
await expect(
|
|
startAutomationExecutionForRule({ tenantId: "tenant-1", tenantSlug: "tenant-1", userId: "user-1", ruleId: "rule-1", triggerPayload: {} })
|
|
).rejects.toThrow();
|
|
});
|
|
});
|