Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/worker/src/jobs/email-sequence-scheduler.test.ts
T

75 lines
2.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
EMAIL_SEQUENCE_QUEUE,
EmailSequenceStepJobError,
parseEmailSequenceStepJob,
runEmailSequenceStepJob,
} from "./email-sequence-scheduler.js";
import type { StepRunnerDeps } from "@toolctrl/email-sequences";
function buildDeps(): StepRunnerDeps {
return {
store: {
loadRun: vi.fn(async () => null),
loadSteps: vi.fn(async () => []),
loadTriggerContext: vi.fn(async () => null),
markDelivery: vi.fn(),
createRun: vi.fn(),
findRunByTrigger: vi.fn(async () => null),
updateRunStatus: vi.fn(),
createDelivery: vi.fn(),
},
queue: { scheduleStep: vi.fn() },
renderTemplate: () => ({ html: "<p>x</p>", text: "x", subject: "s" }),
sendMail: async () => ({ ok: true }),
buildUnsubscribeUrl: () => "https://app.example.com/unsubscribe/tok",
};
}
afterEach(() => {
delete process.env.MARKETING_SEQUENCES_ENABLED;
});
beforeEach(() => {
process.env.MARKETING_SEQUENCES_ENABLED = "true";
});
describe("EMAIL_SEQUENCE_QUEUE", () => {
it("exposes the queue name", () => {
expect(EMAIL_SEQUENCE_QUEUE).toBe("email-sequence.step");
});
});
describe("parseEmailSequenceStepJob", () => {
it("accepts a well-formed envelope", () => {
const job = parseEmailSequenceStepJob({ runId: "run_1", stepIndex: 2 });
expect(job.runId).toBe("run_1");
expect(job.stepIndex).toBe(2);
});
it("rejects missing runId", () => {
expect(() => parseEmailSequenceStepJob({ stepIndex: 0 })).toThrow(EmailSequenceStepJobError);
});
it("rejects negative stepIndex", () => {
expect(() => parseEmailSequenceStepJob({ runId: "run_1", stepIndex: -1 })).toThrow(EmailSequenceStepJobError);
});
it("rejects non-integer stepIndex", () => {
expect(() => parseEmailSequenceStepJob({ runId: "run_1", stepIndex: 0.5 })).toThrow(EmailSequenceStepJobError);
});
});
describe("runEmailSequenceStepJob", () => {
it("returns cancelled when killswitch active", async () => {
process.env.MARKETING_SEQUENCES_ENABLED = "false";
const result = await runEmailSequenceStepJob({ runId: "run_1", stepIndex: 0 }, buildDeps());
expect(result.action).toBe("cancelled");
});
it("invokes runStep when enabled", async () => {
const deps = buildDeps();
const result = await runEmailSequenceStepJob({ runId: "run_1", stepIndex: 0 }, deps);
// runStep cancels because loadRun returns null in our fake — that is
// the right deterministic outcome here. We just verify the wiring.
expect(result.action).toBe("cancelled");
expect(deps.store.loadRun).toHaveBeenCalledWith("run_1");
});
});