Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

test: initial tests for award bounty workflow

Pedro Ladeira955faa6fd806
diff --git a/apps/web/tests/workflows/award-bounty-workflow.test.ts b/apps/web/tests/workflows/award-bounty-workflow.test.ts
new file mode 100644
index 0000000000..4aed013af3
--- /dev/null
+++ b/apps/web/tests/workflows/award-bounty-workflow.test.ts
@@ -0,0 +1,300 @@
+import { EnrolledPartnerProps } from "@/lib/types";
+import { Bounty } from "@dub/prisma/client";
+import { prisma } from "@dub/prisma";
+import { describe, expect, test, onTestFinished } from "vitest";
+import { randomEmail } from "../utils/helpers";
+import { IntegrationHarness } from "../utils/integration";
+import { E2E_TRACK_CLICK_HEADERS } from "../utils/resource";
+import { verifyBountySubmission } from "./utils/verify-bounty-submission";
+
+async function trackLeads(
+ http: any,
+ partnerLink: { domain: string; key: string },
+ count: number,
+) {
+ for (let i = 0; i < count; i++) {
+ const { status: clickStatus, data: clickData } = await http.post({
+ path: "/track/click",
+ headers: E2E_TRACK_CLICK_HEADERS,
+ body: {
+ domain: partnerLink.domain,
+ key: partnerLink.key,
+ },
+ });
+
+ expect(clickStatus).toEqual(200);
+ expect(clickData.clickId).toBeDefined();
+
+ const { status: leadStatus } = await http.post({
+ path: "/track/lead",
+ body: {
+ clickId: clickData.clickId,
+ eventName: `Signup-${i}-${Date.now()}`,
+ customerExternalId: `e2e-customer-${i}-${Date.now()}`,
+ customerEmail: `customer${i}@example.com`,
+ },
+ });
+
+ expect(leadStatus).toEqual(200);
+
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ }
+}
+
+describe.sequential("Workflow - AwardBounty", async () => {
+ const h = new IntegrationHarness();
+ const { http } = await h.init();
+
+ test("Workflow executes when partner reaches goal", async () => {
+ const { status: bountyStatus, data: bounty } = await http.post<Bounty>({
+ path: "/bounties",
+ body: {
+ name: "E2E POC Performance Bounty",
+ description: "Get 10 leads to earn $10",
+ type: "performance",
+ startsAt: new Date().toISOString(),
+ endsAt: null,
+ rewardAmount: 1000,
+ performanceScope: "new",
+ groupIds: [],
+ performanceCondition: {
+ attribute: "totalLeads",
+ operator: "gte",
+ value: 10,
+ },
+ },
+ });
+
+ expect(bountyStatus).toEqual(200);
+ expect(bounty.id).toBeDefined();
+
+ onTestFinished(async () => {
+ await h.deleteBounty(bounty.id);
+ });
+
+ const { status: partnerStatus, data: partner } = await http.post<
+ EnrolledPartnerProps
+ >({
+ path: "/partners",
+ body: {
+ name: "E2E POC Test Partner",
+ email: randomEmail(),
+ },
+ });
+
+ expect(partnerStatus).toEqual(201);
+ expect(partner.id).toBeDefined();
+
+ expect(partner.links).toBeDefined();
+ expect(partner.links).not.toBeNull();
+ expect(partner.links!.length).toBeGreaterThan(0);
+
+ const partnerLink = partner.links![0];
+ const partnerLinkKey = partnerLink.key;
+ const partnerLinkDomain = partnerLink.domain;
+
+ await trackLeads(
+ http,
+ { domain: partnerLinkDomain, key: partnerLinkKey },
+ 11,
+ );
+
+ const submission = await verifyBountySubmission({
+ bountyId: bounty.id,
+ partnerId: partner.id,
+ expectedStatus: "submitted",
+ minPerformanceCount: 10,
+ });
+
+ expect(submission.status).toBe("submitted");
+ expect(submission.performanceCount).toBeGreaterThanOrEqual(10);
+ expect(submission.completedAt).not.toBeNull();
+ expect(submission.bountyId).toBe(bounty.id);
+ expect(submission.partnerId).toBe(partner.id);
+ });
+
+ test("Workflow doesn't execute when goal not reached", async () => {
+ const { status: bountyStatus, data: bounty } = await http.post<Bounty>({
+ path: "/bounties",
+ body: {
+ name: "E2E Performance Bounty - Not Reached",
+ description: "Get 10 leads to earn $10",
+ type: "performance",
+ startsAt: new Date().toISOString(),
+ endsAt: null,
+ rewardAmount: 1000,
+ performanceScope: "new",
+ groupIds: [],
+ performanceCondition: {
+ attribute: "totalLeads",
+ operator: "gte",
+ value: 10,
+ },
+ },
+ });
+
+ expect(bountyStatus).toEqual(200);
+
+ onTestFinished(async () => {
+ await h.deleteBounty(bounty.id);
+ });
+
+ const { status: partnerStatus, data: partner } = await http.post<
+ EnrolledPartnerProps
+ >({
+ path: "/partners",
+ body: {
+ name: "E2E Test Partner - Not Reached",
+ email: randomEmail(),
+ },
+ });
+
+ expect(partnerStatus).toEqual(201);
+ expect(partner.links).not.toBeNull();
+
+ const partnerLink = partner.links![0];
+
+ await trackLeads(http, partnerLink, 5);
+
+ await new Promise((resolve) => setTimeout(resolve, 10000));
+
+ const submission = await prisma.bountySubmission.findFirst({
+ where: {
+ bountyId: bounty.id,
+ partnerId: partner.id,
+ },
+ });
+
+ expect(submission).not.toBeNull();
+ expect(submission?.status).toBe("draft");
+ expect(submission?.performanceCount).toBe(5);
+ expect(submission?.completedAt).toBeNull();
+ });
+
+ test("Disabled workflow doesn't execute", async () => {
+ const { status: bountyStatus, data: bounty } = await http.post<Bounty>({
+ path: "/bounties",
+ body: {
+ name: "E2E Performance Bounty - Disabled",
+ description: "Get 10 leads to earn $10",
+ type: "performance",
+ startsAt: new Date().toISOString(),
+ endsAt: null,
+ rewardAmount: 1000,
+ performanceScope: "new",
+ groupIds: [],
+ performanceCondition: {
+ attribute: "totalLeads",
+ operator: "gte",
+ value: 10,
+ },
+ },
+ });
+
+ expect(bountyStatus).toEqual(200);
+
+ onTestFinished(async () => {
+ await h.deleteBounty(bounty.id);
+ });
+
+ const workflow = await prisma.workflow.findFirst({
+ where: { bounty: { id: bounty.id } },
+ });
+
+ expect(workflow).not.toBeNull();
+
+ await prisma.workflow.update({
+ where: { id: workflow!.id },
+ data: { disabledAt: new Date() },
+ });
+
+ const { status: partnerStatus, data: partner } = await http.post<
+ EnrolledPartnerProps
+ >({
+ path: "/partners",
+ body: {
+ name: "E2E Test Partner - Disabled",
+ email: randomEmail(),
+ },
+ });
+
+ expect(partnerStatus).toEqual(201);
+ expect(partner.links).not.toBeNull();
+
+ const partnerLink = partner.links![0];
+
+ await trackLeads(http, partnerLink, 11);
+
+ await new Promise((resolve) => setTimeout(resolve, 10000));
+
+ const submission = await prisma.bountySubmission.findFirst({
+ where: {
+ bountyId: bounty.id,
+ partnerId: partner.id,
+ },
+ });
+
+ expect(submission).toBeNull();
+ });
+
+ test("No duplicate execution on multiple triggers", async () => {
+ const { status: bountyStatus, data: bounty } = await http.post<Bounty>({
+ path: "/bounties",
+ body: {
+ name: "E2E Performance Bounty - No Duplicates",
+ description: "Get 10 leads to earn $10",
+ type: "performance",
+ startsAt: new Date().toISOString(),
+ endsAt: null,
+ rewardAmount: 1000,
+ performanceScope: "new",
+ groupIds: [],
+ performanceCondition: {
+ attribute: "totalLeads",
+ operator: "gte",
+ value: 10,
+ },
+ },
+ });
+
+ expect(bountyStatus).toEqual(200);
+
+ onTestFinished(async () => {
+ await h.deleteBounty(bounty.id);
+ });
+
+ const { status: partnerStatus, data: partner } = await http.post<
+ EnrolledPartnerProps
+ >({
+ path: "/partners",
+ body: {
+ name: "E2E Test Partner - No Duplicates",
+ email: randomEmail(),
+ },
+ });
+
+ expect(partnerStatus).toEqual(201);
+ expect(partner.links).not.toBeNull();
+
+ const partnerLink = partner.links![0];
+
+ await trackLeads(http, partnerLink, 15);
+
+ await verifyBountySubmission({
+ bountyId: bounty.id,
+ partnerId: partner.id,
+ expectedStatus: "submitted",
+ minPerformanceCount: 10,
+ });
+
+ const submissions = await prisma.bountySubmission.findMany({
+ where: {
+ bountyId: bounty.id,
+ partnerId: partner.id,
+ },
+ });
+
+ expect(submissions).toHaveLength(1);
+ expect(submissions[0].status).toBe("submitted");
+ });
+});
diff --git a/apps/web/tests/workflows/utils/verify-bounty-submission.ts b/apps/web/tests/workflows/utils/verify-bounty-submission.ts
new file mode 100644
index 0000000000..e2c04649ef
--- /dev/null
+++ b/apps/web/tests/workflows/utils/verify-bounty-submission.ts
@@ -0,0 +1,53 @@
+import { prisma } from "@dub/prisma";
+import { expect } from "vitest";
+
+interface VerifyBountySubmissionProps {
+ bountyId: string;
+ partnerId: string;
+ expectedStatus?: "draft" | "submitted" | "approved" | "rejected";
+ minPerformanceCount?: number;
+}
+
+const POLL_INTERVAL_MS = 5000; // 5 seconds
+const TIMEOUT_MS = 30000; // 30 seconds
+
+export const verifyBountySubmission = async ({
+ bountyId,
+ partnerId,
+ expectedStatus = "submitted",
+ minPerformanceCount,
+}: VerifyBountySubmissionProps) => {
+ const startTime = Date.now();
+
+ while (Date.now() - startTime < TIMEOUT_MS) {
+ const submission = await prisma.bountySubmission.findFirst({
+ where: {
+ bountyId,
+ partnerId,
+ },
+ });
+
+ if (submission) {
+ expect(submission.status).toBe(expectedStatus);
+
+ if (minPerformanceCount !== undefined) {
+ expect(submission.performanceCount).toBeGreaterThanOrEqual(
+ minPerformanceCount,
+ );
+ }
+
+ if (expectedStatus === "submitted") {
+ expect(submission.completedAt).not.toBeNull();
+ }
+
+ return submission;
+ }
+
+ await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
+ }
+
+ throw new Error(
+ `Bounty submission not found within ${TIMEOUT_MS / 1000} seconds. ` +
+ `bountyId: ${bountyId}, partnerId: ${partnerId}`,
+ );
+};