Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+207
@@ -0,0 +1,207 @@
|
||||
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, SequenceEngineLogger } 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) => void delivered.push(input)),
|
||||
createRun: vi.fn(),
|
||||
findRunByTrigger: vi.fn(async () => null),
|
||||
updateRunStatus: vi.fn(async (runId, status) => void statusUpdates.push({ runId, status })),
|
||||
createDelivery: vi.fn(async ({ runId, stepIndex, scheduledAt }) => ({
|
||||
id: `del_${stepIndex}`,
|
||||
runId,
|
||||
stepIndex,
|
||||
status: "pending" as const,
|
||||
scheduledAt,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
function makeLogger(): SequenceEngineLogger & {
|
||||
info: ReturnType<typeof vi.fn>;
|
||||
warn: ReturnType<typeof vi.fn>;
|
||||
error: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
return { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
|
||||
}
|
||||
|
||||
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 — unknown / out-of-range step", () => {
|
||||
it("cancels without delivering when the requested step index is not in the run", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const logger = makeLogger();
|
||||
const queueSchedule = vi.fn();
|
||||
const deps = buildDeps(store, { logger, queue: { scheduleStep: queueSchedule } });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 99 }, deps);
|
||||
expect(result).toEqual({ action: "cancelled", completed: true });
|
||||
expect(store.delivered).toHaveLength(0);
|
||||
expect(queueSchedule).not.toHaveBeenCalled();
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.unknown_step", stepIndex: 99 }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("runStep — logger wiring on every branch", () => {
|
||||
it("logs a warning when the run row is missing", async () => {
|
||||
const store = buildStore({ run: null });
|
||||
const logger = makeLogger();
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger }));
|
||||
expect(result.action).toBe("cancelled");
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.run_missing" }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
|
||||
it("logs an info line when the run is already in a terminal state", async () => {
|
||||
const store = buildStore({ run: buildRun({ status: "completed" }), steps: STEPS, trigger: TRIGGER });
|
||||
const logger = makeLogger();
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger }));
|
||||
expect(result.action).toBe("cancelled");
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.run_inactive", status: "completed" }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
|
||||
it("logs a warning + skip when the trigger context has vanished", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: null });
|
||||
const logger = makeLogger();
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger }));
|
||||
expect(result.action).toBe("skipped");
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.no_trigger" }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
|
||||
it("logs an info line when step conditions are not met", 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 logger = makeLogger();
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger }));
|
||||
expect(result.action).toBe("skipped");
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.conditions_false", stepIndex: 0 }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
|
||||
it("logs the provider message id on a successful send", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const logger = makeLogger();
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: true, messageId: "msg_77" });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger, sendMail }));
|
||||
expect(result.action).toBe("sent");
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.sent", providerMessageId: "msg_77" }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
|
||||
it("logs a warning and fails the run on a hard bounce", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const logger = makeLogger();
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: false, bounced: true, errorTag: "hard_bounce" });
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, buildDeps(store, { logger, sendMail }));
|
||||
expect(result).toEqual({ action: "failed", completed: true });
|
||||
expect(store.statusUpdates).toEqual([{ runId: "run_1", status: "failed" }]);
|
||||
expect(logger.warn).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ op: "sequence.step.bounced", errorTag: "hard_bounce" }),
|
||||
expect.any(String)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("runStep — send wiring details", () => {
|
||||
it("forwards subject/recipient/unsubscribe data and the optional list-unsubscribe mailto", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: true, messageId: "m1" });
|
||||
const renderTemplate = vi.fn().mockReturnValue({ html: "<b>hi</b>", text: "hi", subject: "Renamed Subject" });
|
||||
const buildUnsubscribeMailto = vi.fn().mockReturnValue("mailto:unsub@example.com");
|
||||
await runStep(
|
||||
{ runId: "run_1", stepIndex: 0 },
|
||||
buildDeps(store, { sendMail, renderTemplate, buildUnsubscribeMailto })
|
||||
);
|
||||
expect(sendMail).toHaveBeenCalledWith({
|
||||
to: "alice@example.com",
|
||||
subject: "Renamed Subject",
|
||||
html: "<b>hi</b>",
|
||||
text: "hi",
|
||||
unsubscribeUrl: "https://app.example.com/unsubscribe/tok",
|
||||
listUnsubscribeMailto: "mailto:unsub@example.com",
|
||||
});
|
||||
expect(renderTemplate).toHaveBeenCalledWith(STEPS[0], TRIGGER, "run_1");
|
||||
});
|
||||
|
||||
it("falls back to a fresh Date when no `now` is injected on a transient failure", async () => {
|
||||
const store = buildStore({ run: buildRun(), steps: STEPS, trigger: TRIGGER });
|
||||
const sendMail = vi.fn().mockResolvedValue({ ok: false, errorTag: "smtp_timeout" });
|
||||
// no `now` override → exercises the `?? (() => new Date())` fallback
|
||||
const deps = buildDeps(store, { sendMail, logger: makeLogger() });
|
||||
delete (deps as { now?: () => Date }).now;
|
||||
const result = await runStep({ runId: "run_1", stepIndex: 0 }, deps);
|
||||
expect(result).toEqual({ action: "failed", completed: false });
|
||||
expect(store.delivered[0]).toMatchObject({ status: "failed", errorMessage: "smtp_timeout" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user