The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
test(workflow): new case scenarios for send-campaign tests
Pedro Ladeira6e8149d0b6e0
diff --git a/apps/web/tests/workflows/move-group-workflow.test.ts b/apps/web/tests/workflows/move-group-workflow.test.tsindex 47bc5b9d5b..65f8ea50fc 100644--- a/apps/web/tests/workflows/move-group-workflow.test.ts+++ b/apps/web/tests/workflows/move-group-workflow.test.ts@@ -475,7 +475,6 @@ describe.sequential("Workflow - MoveGroup", async () => {expectedGroupId: targetGroup.id,});- // Verify partner is in the target group exactly once (no intermediate states)const enrollment = await prisma.programEnrollment.findUnique({where: {partnerId_programId: {diff --git a/apps/web/tests/workflows/send-campaign-workflow.test.ts b/apps/web/tests/workflows/send-campaign-workflow.test.tsindex 02e8da9aad..2422145a10 100644--- a/apps/web/tests/workflows/send-campaign-workflow.test.ts+++ b/apps/web/tests/workflows/send-campaign-workflow.test.ts@@ -1,7 +1,24 @@+import { EnrolledPartnerProps } from "@/lib/types";import { prisma } from "@dub/prisma";import { Campaign } from "@dub/prisma/client";+import { subHours } from "date-fns";import { describe, expect, test, onTestFinished } from "vitest";+import { randomEmail } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";+import { E2E_PROGRAM, E2E_USER_ID } from "../utils/resource";++async function callCronWorkflow(baseUrl: string, workflowId: string) {+ const response = await fetch(`${baseUrl}/api/cron/workflows/${workflowId}`, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({}),+ });++ return {+ status: response.status,+ body: await response.text(),+ };+}describe.sequential("Workflow - SendCampaign", async () => {const h = new IntegrationHarness();@@ -134,7 +151,7 @@ describe.sequential("Workflow - SendCampaign", async () => {expect(workflow?.disabledAt).not.toBeNull();});- test("Disabled workflow doesn't send campaign", async () => {+ test("Cron executes send campaign workflow", async () => {const { status: createStatus, data: campaign } = await http.post<{id: string;}>({@@ -155,8 +172,61 @@ describe.sequential("Workflow - SendCampaign", async () => {await http.patch({path: `/campaigns/${campaignId}`,body: {- name: "E2E Disabled Campaign",- subject: "This should not be sent",+ name: "E2E Cron Campaign",+ subject: "Welcome!",+ bodyJson: {+ type: "doc",+ content: [+ {+ type: "paragraph",+ content: [{ type: "text", text: "Test content" }],+ },+ ],+ },+ triggerCondition: {+ attribute: "partnerEnrolledDays",+ operator: "gte",+ value: 1,+ },+ status: "active",+ },+ });++ const workflow = await prisma.workflow.findFirst({+ where: { campaign: { id: campaignId } },+ });++ expect(workflow).not.toBeNull();++ const { status, body } = await callCronWorkflow(h.baseUrl, workflow!.id);++ expect(status).toEqual(200);+ expect(body).toContain("Finished executing workflow");+ });++ test("Cron skips disabled send campaign workflow", async () => {+ const { status: createStatus, data: campaign } = await http.post<{+ id: string;+ }>({+ path: "/campaigns",+ body: {+ type: "transactional",+ },+ });++ expect(createStatus).toEqual(201);++ const campaignId = campaign.id;++ onTestFinished(async () => {+ await h.deleteCampaign(campaignId);+ });++ await http.patch({+ path: `/campaigns/${campaignId}`,+ body: {+ name: "E2E Disabled Cron Campaign",+ subject: "Should not be sent",bodyJson: {type: "doc",content: [@@ -180,19 +250,282 @@ describe.sequential("Workflow - SendCampaign", async () => {});expect(workflow).not.toBeNull();- expect(workflow?.disabledAt).toBeNull();await prisma.workflow.update({where: { id: workflow!.id },data: { disabledAt: new Date() },});- const disabledWorkflow = await prisma.workflow.findUnique({- where: { id: workflow!.id },- select: { disabledAt: true },+ const { status, body } = await callCronWorkflow(h.baseUrl, workflow!.id);++ expect(status).toEqual(200);+ expect(body).toContain("disabled");++ const emailsSent = await prisma.notificationEmail.findMany({+ where: {+ campaignId,+ type: "Campaign",+ },+ });++ expect(emailsSent).toHaveLength(0);+ });++ test("Cron processes eligible partner enrollment", async () => {+ const { status: createStatus, data: campaign } = await http.post<{+ id: string;+ }>({+ path: "/campaigns",+ body: {+ type: "transactional",+ },+ });++ expect(createStatus).toEqual(201);++ const campaignId = campaign.id;++ onTestFinished(async () => {+ await h.deleteCampaign(campaignId);+ });++ await http.patch({+ path: `/campaigns/${campaignId}`,+ body: {+ name: "E2E Send Campaign",+ subject: "Welcome partner!",+ bodyJson: {+ type: "doc",+ content: [+ {+ type: "paragraph",+ content: [{ type: "text", text: "Hello!" }],+ },+ ],+ },+ triggerCondition: {+ attribute: "partnerEnrolledDays",+ operator: "gte",+ value: 1,+ },+ status: "active",+ },+ });++ const workflow = await prisma.workflow.findFirst({+ where: { campaign: { id: campaignId } },+ });++ expect(workflow).not.toBeNull();++ const { status: partnerStatus, data: partner } = await http.post<+ EnrolledPartnerProps+ >({+ path: "/partners",+ body: {+ name: "E2E Test Partner - Campaign Send",+ email: randomEmail(),+ },+ });++ expect(partnerStatus).toEqual(201);++ const programId = E2E_PROGRAM.id;++ await prisma.programEnrollment.update({+ where: {+ partnerId_programId: {+ partnerId: partner.id,+ programId,+ },+ },+ data: { createdAt: subHours(new Date(), 18) },+ });++ const { status, body } = await callCronWorkflow(h.baseUrl, workflow!.id);++ expect(status).toEqual(200);+ expect(body).toContain("Finished executing workflow");+ });++ test("Cron doesn't send campaign when partner doesn't meet conditions", async () => {+ const { status: createStatus, data: campaign } = await http.post<{+ id: string;+ }>({+ path: "/campaigns",+ body: {+ type: "transactional",+ },+ });++ expect(createStatus).toEqual(201);++ const campaignId = campaign.id;++ onTestFinished(async () => {+ await h.deleteCampaign(campaignId);+ });++ await http.patch({+ path: `/campaigns/${campaignId}`,+ body: {+ name: "E2E No Match Campaign",+ subject: "Should not be sent",+ bodyJson: {+ type: "doc",+ content: [+ {+ type: "paragraph",+ content: [{ type: "text", text: "Test" }],+ },+ ],+ },+ triggerCondition: {+ attribute: "partnerEnrolledDays",+ operator: "gte",+ value: 1,+ },+ status: "active",+ },+ });++ const workflow = await prisma.workflow.findFirst({+ where: { campaign: { id: campaignId } },+ });++ expect(workflow).not.toBeNull();++ const { status: partnerStatus, data: partner } = await http.post<+ EnrolledPartnerProps+ >({+ path: "/partners",+ body: {+ name: "E2E Test Partner - No Match",+ email: randomEmail(),+ },+ });++ expect(partnerStatus).toEqual(201);++ const { status, body } = await callCronWorkflow(h.baseUrl, workflow!.id);++ expect(status).toEqual(200);+ expect(body).toContain("Finished executing workflow");++ const emailSent = await prisma.notificationEmail.findFirst({+ where: {+ campaignId,+ type: "Campaign",+ partnerId: partner.id,+ },+ });++ expect(emailSent).toBeNull();+ });++ test("No duplicate campaign sends on multiple cron executions", async () => {+ const { status: createStatus, data: campaign } = await http.post<{+ id: string;+ }>({+ path: "/campaigns",+ body: {+ type: "transactional",+ },+ });++ expect(createStatus).toEqual(201);++ const campaignId = campaign.id;++ onTestFinished(async () => {+ await prisma.notificationEmail.deleteMany({+ where: { campaignId, type: "Campaign" },+ });+ await h.deleteCampaign(campaignId);+ });++ await http.patch({+ path: `/campaigns/${campaignId}`,+ body: {+ name: "E2E No Dup Campaign",+ subject: "No duplicates!",+ bodyJson: {+ type: "doc",+ content: [+ {+ type: "paragraph",+ content: [{ type: "text", text: "Hello!" }],+ },+ ],+ },+ triggerCondition: {+ attribute: "partnerEnrolledDays",+ operator: "gte",+ value: 1,+ },+ status: "active",+ },+ });++ const workflow = await prisma.workflow.findFirst({+ where: { campaign: { id: campaignId } },+ });++ expect(workflow).not.toBeNull();++ const { status: partnerStatus, data: partner } = await http.post<+ EnrolledPartnerProps+ >({+ path: "/partners",+ body: {+ name: "E2E Test Partner - No Dup Campaign",+ email: randomEmail(),+ },+ });++ expect(partnerStatus).toEqual(201);++ const programId = E2E_PROGRAM.id;++ await prisma.programEnrollment.update({+ where: {+ partnerId_programId: {+ partnerId: partner.id,+ programId,+ },+ },+ data: { createdAt: subHours(new Date(), 18) },+ });++ const existingEmail = await prisma.notificationEmail.create({+ data: {+ id: `em_e2e_dedup_${Date.now()}`,+ type: "Campaign",+ emailId: `resend_e2e_dedup_${Date.now()}`,+ campaignId,+ programId,+ partnerId: partner.id,+ recipientUserId: E2E_USER_ID,+ },+ });++ expect(existingEmail).not.toBeNull();++ const { status, body } = await callCronWorkflow(h.baseUrl, workflow!.id);++ expect(status).toEqual(200);+ expect(body).toContain("Finished executing workflow");++ const emails = await prisma.notificationEmail.findMany({+ where: {+ campaignId,+ type: "Campaign",+ partnerId: partner.id,+ },});- expect(disabledWorkflow?.disabledAt).not.toBeNull();+ expect(emails).toHaveLength(1);+ expect(emails[0].id).toBe(existingEmail.id);});test("Campaign workflow configuration can be updated", async () => {