Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,174 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { runStep, type StepRunnerStore, type StepRunnerDeps } from "./step-runner.js";
|
||||
import type { SequenceStep, SequenceTriggerContext } from "./types.js";
|
||||
import type { SequenceRunRow } from "./engine.js";
|
||||
|
||||
const NOW = () => new Date("2026-05-17T12:00:00Z");
|
||||
|
||||
function buildRun(overrides: Partial<SequenceRunRow> = {}): SequenceRunRow {
|
||||
return {
|
||||
id: "run_1",
|
||||
sequenceId: "seq_1",
|
||||
leadId: "lead_1",
|
||||
tenantId: null,
|
||||
triggerEventId: "evt_1",
|
||||
status: "running",
|
||||
startedAt: NOW(),
|
||||
completedAt: null,
|
||||
stepStateJson: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function buildStore(opts: {
|
||||
run?: SequenceRunRow | null;
|
||||
steps?: readonly SequenceStep[];
|
||||
trigger?: SequenceTriggerContext | null;
|
||||
}): StepRunnerStore & { delivered: unknown[]; statusUpdates: Array<{ runId: string; status: string }> } {
|
||||
const delivered: unknown[] = [];
|
||||
const statusUpdates: Array<{ runId: string; status: string }> = [];
|
||||
return {
|
||||
delivered,
|
||||
statusUpdates,
|
||||
loadRun: vi.fn(async () => opts.run ?? null),
|
||||
loadSteps: vi.fn(async () => opts.steps ?? []),
|
||||
loadTriggerContext: vi.fn(async () => opts.trigger ?? null),
|
||||
markDelivery: vi.fn(async (input) => {
|
||||
delivered.push(input);
|
||||
}),
|
||||
createRun: vi.fn(),
|
||||
findRunByTrigger: vi.fn(async () => null),
|
||||
updateRunStatus: vi.fn(async (runId, status) => {
|
||||
statusUpdates.push({ runId, status });
|
||||
}),
|
||||
createDelivery: vi.fn(async ({ runId, stepIndex, scheduledAt }) => ({
|
||||
id: `del_${stepIndex}`,
|
||||
runId,
|
||||
stepIndex,
|
||||
status: "pending" as const,
|
||||
scheduledAt,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function buildDeps(store: StepRunnerStore, overrides: Partial<StepRunnerDeps> = {}): StepRunnerDeps {
|
||||
return {
|
||||
store,
|
||||
queue: { scheduleStep: vi.fn() },
|
||||
now: NOW,
|
||||
renderTemplate: () => ({ html: "<p>x</p>", text: "x", subject: "Hello" }),
|
||||
sendMail: async () => ({ ok: true, messageId: "msg_1" }),
|
||||
buildUnsubscribeUrl: () => "https://app.example.com/unsubscribe/tok",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
const STEPS: readonly SequenceStep[] = [
|
||||
{ stepIndex: 0, delayHours: 0, template: "welcome", subject: "S0" },
|
||||
{ stepIndex: 1, delayHours: 24, template: "generic-step", subject: "S1" },
|
||||
];
|
||||
|
||||
const TRIGGER: SequenceTriggerContext = {
|
||||
triggerEventId: "evt_1",
|
||||
leadId: "lead_1",
|
||||
tenantId: null,
|
||||
email: "alice@example.com",
|
||||
recipientName: "Alice",
|
||||
payload: { lead: { status: "new" } },
|
||||
};
|
||||
|
||||
describe("runStep", () => {
|
||||
it("cancels when the run is not found", async () => {
|
||||
const store = buildStore({ run: null });
|
||||
const deps = buildDeps(store);
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("cancelled");
|
||||
});
|
||||
|
||||
it("skips when run is unsubscribed mid-sequence", async () => {
|
||||
const store = buildStore({ run: buildRun({ status: "unsubscribed" }), steps: STEPS, trigger: TRIGGER });
|
||||
const deps = buildDeps(store);
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 1 }, deps);
|
||||
expect(result.action).toBe("cancelled");
|
||||
expect(store.delivered).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("skips when step conditions evaluate false and advances", async () => {
|
||||
const conditionalSteps: readonly SequenceStep[] = [
|
||||
{ stepIndex: 0, delayHours: 0, template: "welcome", subject: "S0", conditions: [{ path: "lead.status", op: "eq", value: "qualified" }] },
|
||||
{ stepIndex: 1, delayHours: 24, template: "generic-step", subject: "S1" },
|
||||
];
|
||||
const store = buildStore({ run: buildRun(), steps: conditionalSteps, trigger: TRIGGER });
|
||||
const queueSchedule = vi.fn();
|
||||
const deps = buildDeps(store, { queue: { scheduleStep: queueSchedule } });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("skipped");
|
||||
expect(store.delivered).toEqual([{ runId: "run_1", stepIndex: 0, status: "skipped" }]);
|
||||
expect(queueSchedule).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("skips and advances without sending when trigger context is missing", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: null });
|
||||
const sendMail = vi.fn();
|
||||
const queueSchedule = vi.fn();
|
||||
const deps = buildDeps(store, { sendMail, queue: { scheduleStep: queueSchedule } });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("skipped");
|
||||
expect(result.completed).toBe(false);
|
||||
expect(sendMail).not.toHaveBeenCalled();
|
||||
expect(store.delivered).toEqual([
|
||||
{
|
||||
runId: "run_1",
|
||||
stepIndex: 0,
|
||||
status: "skipped",
|
||||
errorMessage: "trigger_context_missing",
|
||||
},
|
||||
]);
|
||||
expect(queueSchedule).toHaveBeenCalledOnce();
|
||||
expect(queueSchedule.mock.calls[0]?.[0]).toMatchObject({ runId: "run_1", stepIndex: 1 });
|
||||
});
|
||||
|
||||
it("sends the step and advances to the next when conditions pass", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: true, messageId: "msg_42" });
|
||||
const queueSchedule = vi.fn();
|
||||
const deps = buildDeps(store, { sendMail, queue: { scheduleStep: queueSchedule } });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("sent");
|
||||
expect(result.completed).toBe(false);
|
||||
expect(sendMail).toHaveBeenCalledOnce();
|
||||
expect(store.delivered[0]).toMatchObject({ status: "sent", providerMessageId: "msg_42" });
|
||||
expect(queueSchedule).toHaveBeenCalledOnce();
|
||||
expect(queueSchedule.mock.calls[0]?.[0].stepIndex).toBe(1);
|
||||
});
|
||||
|
||||
it("marks run completed after the last step sends", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const deps = buildDeps(store);
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 1 }, deps);
|
||||
expect(result.action).toBe("sent");
|
||||
expect(result.completed).toBe(true);
|
||||
expect(store.statusUpdates).toEqual([{ runId: "run_1", status: "completed" }]);
|
||||
});
|
||||
|
||||
it("marks delivery bounced + run failed on hard bounce", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: false, bounced: true, errorTag: "hard_bounce" });
|
||||
const deps = buildDeps(store, { sendMail });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("failed");
|
||||
expect(result.completed).toBe(true);
|
||||
expect(store.delivered[0]).toMatchObject({ status: "bounced", errorMessage: "hard_bounce" });
|
||||
expect(store.statusUpdates).toEqual([{ runId: "run_1", status: "failed" }]);
|
||||
});
|
||||
|
||||
it("marks delivery failed without bounce status when sendMail fails transiently", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: false, errorTag: "timeout" });
|
||||
const deps = buildDeps(store, { sendMail, logger: { error: vi.fn() } });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result.action).toBe("failed");
|
||||
expect(result.completed).toBe(false);
|
||||
expect(store.delivered[0]).toMatchObject({ status: "failed", errorMessage: "timeout" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user