287 lines
8.3 KiB
TypeScript
287 lines
8.3 KiB
TypeScript
import type { LoaderFunctionArgs } from "react-router";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { loader } from "./$id.stats";
|
|
import { dbRead } from "~/utils/db.server";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
|
|
const dbMocks = vi.hoisted(() => ({
|
|
sequenceFindFirst: vi.fn(),
|
|
runGroupBy: vi.fn(),
|
|
deliveryGroupBy: vi.fn(),
|
|
deliveryFindMany: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/utils/db.server", () => ({
|
|
dbRead: {
|
|
emailSequence: {
|
|
findFirst: dbMocks.sequenceFindFirst,
|
|
},
|
|
emailSequenceRun: {
|
|
groupBy: dbMocks.runGroupBy,
|
|
},
|
|
emailSequenceStepDelivery: {
|
|
groupBy: dbMocks.deliveryGroupBy,
|
|
findMany: dbMocks.deliveryFindMany,
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
|
verifyUserHasPermission: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("i18next", () => ({
|
|
t: (key: string) => key,
|
|
}));
|
|
|
|
async function callLoader() {
|
|
const request = new Request("https://admin.example.test/email-sequences/seq-1/stats");
|
|
const response = (await loader({
|
|
request,
|
|
params: { id: "seq-1" },
|
|
context: {},
|
|
unstable_pattern: "admin/email-sequences/:id/stats",
|
|
} as LoaderFunctionArgs)) as Response;
|
|
return response.json();
|
|
}
|
|
|
|
describe("admin email sequence stats loader", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never);
|
|
vi.mocked(dbRead.emailSequence.findFirst).mockResolvedValue({
|
|
id: "seq-1",
|
|
name: "Onboarding",
|
|
triggerEvent: "lead.created",
|
|
isEnabled: true,
|
|
stepsJson: [
|
|
{ stepIndex: 0, template: "welcome", subject: "Welcome" },
|
|
{ stepIndex: 1, template: "follow-up", subject: "Checking in" },
|
|
],
|
|
} as never);
|
|
vi.mocked(dbRead.emailSequenceRun.groupBy).mockResolvedValue([
|
|
{ status: "completed", _count: { _all: 2 } },
|
|
{ status: "running", _count: { _all: 1 } },
|
|
{ status: "cancelled", _count: { _all: 1 } },
|
|
] as never);
|
|
vi.mocked(dbRead.emailSequenceStepDelivery.groupBy)
|
|
.mockResolvedValueOnce([
|
|
{ stepIndex: 0, status: "sent", _count: { _all: 2 } },
|
|
{ stepIndex: 0, status: "bounced", _count: { _all: 1 } },
|
|
{ stepIndex: 1, status: "skipped", _count: { _all: 1 } },
|
|
{ stepIndex: 1, status: "failed", _count: { _all: 1 } },
|
|
] as never)
|
|
.mockResolvedValueOnce([
|
|
{ stepIndex: 0, _count: { _all: 2 } },
|
|
{ stepIndex: 1, _count: { _all: 1 } },
|
|
] as never)
|
|
.mockResolvedValueOnce([{ stepIndex: 0, _count: { _all: 1 } }] as never);
|
|
});
|
|
|
|
it("aggregates delivery stats in the database instead of loading deliveries", async () => {
|
|
const data = await callLoader();
|
|
|
|
expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.marketing.leads.view");
|
|
expect(dbRead.emailSequenceStepDelivery.findMany).not.toHaveBeenCalled();
|
|
expect(dbRead.emailSequenceStepDelivery.groupBy).toHaveBeenCalledTimes(3);
|
|
expect(dbRead.emailSequenceStepDelivery.groupBy).toHaveBeenNthCalledWith(1, {
|
|
by: ["stepIndex", "status"],
|
|
where: { run: { sequenceId: "seq-1" } },
|
|
_count: { _all: true },
|
|
});
|
|
expect(dbRead.emailSequenceStepDelivery.groupBy).toHaveBeenNthCalledWith(2, {
|
|
by: ["stepIndex"],
|
|
where: { run: { sequenceId: "seq-1" }, openedAt: { not: null } },
|
|
_count: { _all: true },
|
|
});
|
|
expect(dbRead.emailSequenceStepDelivery.groupBy).toHaveBeenNthCalledWith(3, {
|
|
by: ["stepIndex"],
|
|
where: { run: { sequenceId: "seq-1" }, clickedAt: { not: null } },
|
|
_count: { _all: true },
|
|
});
|
|
expect(data).toMatchObject({
|
|
totals: {
|
|
enrolled: 4,
|
|
completed: 2,
|
|
active: 1,
|
|
unsubscribed: 0,
|
|
failed: 1,
|
|
},
|
|
stepStats: [
|
|
{
|
|
stepIndex: 0,
|
|
template: "welcome",
|
|
subject: "Welcome",
|
|
scheduled: 3,
|
|
sent: 2,
|
|
opened: 2,
|
|
clicked: 1,
|
|
bounced: 1,
|
|
skipped: 0,
|
|
failed: 0,
|
|
},
|
|
{
|
|
stepIndex: 1,
|
|
template: "follow-up",
|
|
subject: "Checking in",
|
|
scheduled: 2,
|
|
sent: 0,
|
|
opened: 1,
|
|
clicked: 0,
|
|
bounced: 0,
|
|
skipped: 1,
|
|
failed: 1,
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
it("keeps live status totals and malformed step shapes stable until the stats cron exists", async () => {
|
|
vi.mocked(dbRead.emailSequence.findFirst).mockResolvedValueOnce({
|
|
id: "seq-1",
|
|
name: "Onboarding",
|
|
triggerEvent: "lead.created",
|
|
isEnabled: true,
|
|
stepsJson: [
|
|
{ template: "welcome" },
|
|
{ stepIndex: 4, subject: "Missing template" },
|
|
"not-a-step-object",
|
|
],
|
|
} as never);
|
|
vi.mocked(dbRead.emailSequenceRun.groupBy).mockResolvedValueOnce([
|
|
{ status: "pending", _count: { _all: 2 } },
|
|
{ status: "running", _count: { _all: 3 } },
|
|
{ status: "failed", _count: { _all: 4 } },
|
|
{ status: "cancelled", _count: { _all: 5 } },
|
|
{ status: "unsubscribed", _count: { _all: 6 } },
|
|
] as never);
|
|
vi.mocked(dbRead.emailSequenceStepDelivery.groupBy).mockReset();
|
|
vi.mocked(dbRead.emailSequenceStepDelivery.groupBy)
|
|
.mockResolvedValueOnce([
|
|
{ stepIndex: 0, status: "queued", _count: { _all: 7 } },
|
|
{ stepIndex: 4, status: "sent", _count: { _all: 0 } },
|
|
{ stepIndex: 4, status: "failed", _count: { _all: 2 } },
|
|
{ stepIndex: 2, status: "skipped", _count: { _all: 1 } },
|
|
] as never)
|
|
.mockResolvedValueOnce([
|
|
{ stepIndex: 4, _count: { _all: 3 } },
|
|
{ stepIndex: 99, _count: { _all: 9 } },
|
|
] as never)
|
|
.mockResolvedValueOnce([{ stepIndex: 4, _count: { _all: 1 } }] as never);
|
|
|
|
const data = await callLoader();
|
|
|
|
expect(data.totals).toEqual({
|
|
enrolled: 20,
|
|
completed: 0,
|
|
active: 5,
|
|
unsubscribed: 6,
|
|
failed: 9,
|
|
});
|
|
expect(data.stepStats).toEqual([
|
|
{
|
|
stepIndex: 0,
|
|
template: "welcome",
|
|
subject: "?",
|
|
scheduled: 7,
|
|
sent: 0,
|
|
opened: 0,
|
|
clicked: 0,
|
|
bounced: 0,
|
|
skipped: 0,
|
|
failed: 0,
|
|
},
|
|
{
|
|
stepIndex: 4,
|
|
template: "?",
|
|
subject: "Missing template",
|
|
scheduled: 2,
|
|
sent: 0,
|
|
opened: 3,
|
|
clicked: 1,
|
|
bounced: 0,
|
|
skipped: 0,
|
|
failed: 2,
|
|
},
|
|
{
|
|
stepIndex: 2,
|
|
template: "?",
|
|
subject: "?",
|
|
scheduled: 1,
|
|
sent: 0,
|
|
opened: 0,
|
|
clicked: 0,
|
|
bounced: 0,
|
|
skipped: 1,
|
|
failed: 0,
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("keeps delivery-only step indexes visible when sequence JSON was edited after runs started", async () => {
|
|
vi.mocked(dbRead.emailSequenceStepDelivery.groupBy).mockReset();
|
|
vi.mocked(dbRead.emailSequenceStepDelivery.groupBy)
|
|
.mockResolvedValueOnce([
|
|
{ stepIndex: 0, status: "sent", _count: { _all: 1 } },
|
|
{ stepIndex: 8, status: "bounced", _count: { _all: 2 } },
|
|
] as never)
|
|
.mockResolvedValueOnce([{ stepIndex: 8, _count: { _all: 1 } }] as never)
|
|
.mockResolvedValueOnce([{ stepIndex: 8, _count: { _all: 1 } }] as never);
|
|
|
|
const data = await callLoader();
|
|
|
|
expect(data.stepStats).toEqual([
|
|
{
|
|
stepIndex: 0,
|
|
template: "welcome",
|
|
subject: "Welcome",
|
|
scheduled: 1,
|
|
sent: 1,
|
|
opened: 0,
|
|
clicked: 0,
|
|
bounced: 0,
|
|
skipped: 0,
|
|
failed: 0,
|
|
},
|
|
{
|
|
stepIndex: 1,
|
|
template: "follow-up",
|
|
subject: "Checking in",
|
|
scheduled: 0,
|
|
sent: 0,
|
|
opened: 0,
|
|
clicked: 0,
|
|
bounced: 0,
|
|
skipped: 0,
|
|
failed: 0,
|
|
},
|
|
{
|
|
stepIndex: 8,
|
|
template: "?",
|
|
subject: "?",
|
|
scheduled: 2,
|
|
sent: 0,
|
|
opened: 1,
|
|
clicked: 1,
|
|
bounced: 2,
|
|
skipped: 0,
|
|
failed: 0,
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("returns no step stats when persisted stepsJson is not an array", async () => {
|
|
vi.mocked(dbRead.emailSequence.findFirst).mockResolvedValueOnce({
|
|
id: "seq-1",
|
|
name: "Onboarding",
|
|
triggerEvent: "lead.created",
|
|
isEnabled: true,
|
|
stepsJson: { invalid: true },
|
|
} as never);
|
|
|
|
const data = await callLoader();
|
|
|
|
expect(data.stepStats).toEqual([]);
|
|
});
|
|
});
|