The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Move bounty API tests from Vitest to Playwright.
Kiran K5a264ed5ed56
diff --git a/apps/web/playwright/api/bounties/bounties.spec.ts b/apps/web/playwright/api/bounties/bounties.spec.tsnew file mode 100644index 0000000000..86c9384be5--- /dev/null+++ b/apps/web/playwright/api/bounties/bounties.spec.ts@@ -0,0 +1,949 @@+import type { BountyProps } from "@/lib/types";+import { expect } from "@playwright/test";+import { BountyStartMode, type Program } from "@prisma/client";+import { addDays, addMonths, subDays } from "date-fns";+import { randomName } from "../../utils";+import { test, type ApiClient } from "../fixtures";++test.describe.configure({+ mode: "parallel",+});++type BountyJson = Omit<+ BountyProps,+ "startsAt" | "endsAt" | "submissionsOpenAt" | "socialMetricsLastSyncedAt"+> & {+ startsAt: string | null;+ endsAt: string | null;+ submissionsOpenAt: string | null;+ socialMetricsLastSyncedAt?: string | null;+};++type ProgramFixture = Pick<Program, "id" | "defaultGroupId">;++function futureStartsAt() {+ return new Date(Date.now() + 5 * 60 * 1000).toISOString();+}++function bountyPayload(+ program: ProgramFixture,+ overrides: Record<string, unknown> = {},+) {+ return {+ name: randomName("bounty"),+ description: "some description about the bounty",+ type: "submission",+ startsAt: futureStartsAt(),+ endsAt: null,+ rewardAmount: 1000,+ submissionRequirements: {+ image: { max: 4 },+ url: { max: 10 },+ },+ groupIds: [program.defaultGroupId],+ ...overrides,+ };+}++async function createBounty(+ api: ApiClient,+ program: ProgramFixture,+ overrides: Record<string, unknown> = {},+) {+ return api.post<BountyJson>("/api/bounties", {+ ...bountyPayload(program, overrides),+ });+}++async function deleteBounty(api: ApiClient, id: string | undefined) {+ if (!id) return;+ await api.delete(`/api/bounties/${id}`);+}++const expectedBountyDefaults = {+ id: expect.any(String),+ endsAt: null,+ startMode: "absolute",+ endsAfterDays: null,+ submissionsOpenAt: null,+ submissionFrequency: null,+ maxSubmissions: 1,+ rewardDescription: null,+ performanceCondition: null,+ performanceScope: null,+ socialMetricsLastSyncedAt: null,+};++const unprocessable = (message: string) => ({+ status: 422,+ data: {+ error: {+ code: "unprocessable_entity",+ message,+ doc_url: "https://dub.co/docs/api-reference/errors#unprocessable-entity",+ },+ },+});++const badRequest = (message: string) => ({+ status: 400,+ data: {+ error: {+ code: "bad_request",+ message,+ doc_url: "https://dub.co/docs/api-reference/errors#bad-request",+ },+ },+});++const notFound = (bountyId: string) => ({+ status: 404,+ data: {+ error: {+ code: "not_found",+ message: `Bounty ${bountyId} not found.`,+ doc_url: "https://dub.co/docs/api-reference/errors#not-found",+ },+ },+});++test("POST /bounties", async ({ api, program }) => {+ let id: string | undefined;+ const body = bountyPayload(program);++ try {+ const { status, data } = await api.post<BountyJson>("/api/bounties", body);+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toStrictEqual({+ ...expectedBountyDefaults,+ name: body.name,+ description: body.description,+ type: "submission",+ startsAt: body.startsAt,+ rewardAmount: 1000,+ submissionRequirements: body.submissionRequirements,+ groups: [{ id: program.defaultGroupId }],+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – performance based", async ({ api, program }) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ name: "ignored",+ description: "some description about the bounty",+ type: "performance",+ submissionRequirements: undefined,+ performanceScope: "new",+ performanceCondition: {+ attribute: "totalLeads",+ operator: "gte",+ value: 100,+ },+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ id: expect.any(String),+ name: "Earn $10 after generating 100 leads",+ type: "performance",+ rewardAmount: 1000,+ performanceScope: "new",+ performanceCondition: {+ attribute: "totalLeads",+ operator: "gte",+ value: 100,+ },+ maxSubmissions: 1,+ submissionFrequency: null,+ submissionRequirements: null,+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – performance based with performanceScope set to new", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const name = randomName("bounty");++ try {+ const { status, data } = await createBounty(api, program, {+ name,+ type: "performance",+ submissionRequirements: undefined,+ performanceScope: "new",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ name,+ type: "performance",+ performanceScope: "new",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – submission based with rewardDescription", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ rewardAmount: null,+ rewardDescription: "some reward description",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ rewardAmount: null,+ rewardDescription: "some reward description",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – submission based with submissionsOpenAt", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const now = new Date();+ const startsAt = addDays(now, 1);+ const endsAt = addDays(startsAt, 30);+ const submissionsOpenAt = subDays(endsAt, 2);++ try {+ const { status, data } = await createBounty(api, program, {+ startsAt: startsAt.toISOString(),+ endsAt: endsAt.toISOString(),+ submissionsOpenAt: submissionsOpenAt.toISOString(),+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ startsAt: startsAt.toISOString(),+ endsAt: endsAt.toISOString(),+ submissionsOpenAt: submissionsOpenAt.toISOString(),+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("GET /bounties/{bountyId}", async ({ api, program }) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ const { status, data } = await api.get<BountyJson>(`/api/bounties/${id}`);++ expect(status).toEqual(200);+ expect(data).toStrictEqual(created);+ } finally {+ await deleteBounty(api, id);+ }+});++test("GET /bounties", async ({ api, program }) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ const { status, data } = await api.get<BountyJson[]>("/api/bounties");++ expect(status).toEqual(200);+ expect(data).toEqual(expect.arrayContaining([created]));+ } finally {+ await deleteBounty(api, id);+ }+});++test("GET /bounties/{bountyId}/submissions", async ({ api, program }) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ const { status, data } = await api.get(`/api/bounties/${id}/submissions`);++ expect(status).toEqual(200);+ expect(data).toEqual([]);+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId}", async ({ api, program }) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ const endsAt = addDays(new Date(), 30).toISOString();+ const toUpdate = {+ name: `${created.name} updated`,+ endsAt,+ rewardAmount: 2000,+ };++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ {+ ...toUpdate,+ type: "performance",+ },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ ...toUpdate,+ type: "submission",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("DELETE /bounties/{bountyId}", async ({ api, program }) => {+ const { data: created } = await createBounty(api, program);++ const { status, data } = await api.delete<{ id: string }>(+ `/api/bounties/${created.id}`,+ );++ expect(status).toEqual(200);+ expect(data).toStrictEqual({ id: created.id });+ expect(await api.get(`/api/bounties/${created.id}`)).toEqual(+ notFound(created.id),+ );+});++test("POST /bounties – maxSubmissions persists for submission bounty", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ maxSubmissions: 5,+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ maxSubmissions: 5,+ submissionFrequency: null,+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – submissionFrequency week with maxSubmissions", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ try {+ const { status, data } = await createBounty(api, program, {+ startsAt,+ endsAt,+ maxSubmissions: 4,+ submissionFrequency: "week",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ maxSubmissions: 4,+ submissionFrequency: "week",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – submissionFrequency month with maxSubmissions", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ try {+ const { status, data } = await createBounty(api, program, {+ startsAt,+ endsAt,+ maxSubmissions: 3,+ submissionFrequency: "month",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ maxSubmissions: 3,+ submissionFrequency: "month",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – maxSubmissions and submissionFrequency are ignored for performance bounties", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ type: "performance",+ submissionRequirements: undefined,+ performanceScope: "new",+ performanceCondition: {+ attribute: "totalLeads",+ operator: "gte",+ value: 50,+ },+ maxSubmissions: 5,+ submissionFrequency: "week",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ maxSubmissions: 1,+ submissionFrequency: null,+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – submissionFrequency with relative endsAfterDays is accepted", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAt: null,+ endsAfterDays: 30,+ maxSubmissions: 4,+ submissionFrequency: "week",+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAt: null,+ endsAfterDays: 30,+ maxSubmissions: 4,+ submissionFrequency: "week",+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – update maxSubmissions", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program, {+ maxSubmissions: 5,+ });+ id = created.id;++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { maxSubmissions: 6 },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({ maxSubmissions: 6 });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – update submissionFrequency", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ try {+ const { data: created } = await createBounty(api, program, {+ startsAt,+ endsAt,+ maxSubmissions: 3,+ submissionFrequency: "month",+ });+ id = created.id;++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { submissionFrequency: "day" },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({ submissionFrequency: "day" });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – clear submissionFrequency to null", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ try {+ const { data: created } = await createBounty(api, program, {+ startsAt,+ endsAt,+ maxSubmissions: 3,+ submissionFrequency: "month",+ });+ id = created.id;++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { submissionFrequency: null },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({ submissionFrequency: null });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – clear maxSubmissions to null", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program, {+ maxSubmissions: 5,+ });+ id = created.id;++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { maxSubmissions: null },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({ maxSubmissions: 1 });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – update submissionsOpenAt", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();+ const submissionsOpenAt = addDays(new Date(startsAt), 5).toISOString();++ try {+ const { data: created } = await createBounty(api, program, {+ startsAt,+ endsAt,+ });+ id = created.id;++ const { status, data } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { submissionsOpenAt },+ );++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ submissionsOpenAt: expect.any(String),+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – relative with endsAfterDays", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { status, data } = await createBounty(api, program, {+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAt: null,+ endsAfterDays: 30,+ });+ id = data.id;++ expect(status).toEqual(200);+ expect(data).toMatchObject({+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAt: null,+ endsAfterDays: 30,+ });++ const { status: patchStatus, data: updated } = await api.patch<BountyJson>(+ `/api/bounties/${id}`,+ { endsAfterDays: 180 },+ );++ expect(patchStatus).toEqual(200);+ expect(updated).toMatchObject({+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAfterDays: 180,+ });+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – submissionFrequency requires endsAt on the bounty", async ({+ api,+ program,+}) => {+ let id: string | undefined;+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ try {+ const { data: created } = await createBounty(api, program, {+ startsAt,+ endsAt,+ });+ id = created.id;++ const { status: clearStatus } = await api.patch(`/api/bounties/${id}`, {+ endsAt: null,+ });+ expect(clearStatus).toEqual(200);++ expect(+ await api.patch(`/api/bounties/${id}`, {+ submissionFrequency: "week",+ maxSubmissions: 4,+ }),+ ).toEqual(+ badRequest(+ "`endsAt` or `endsAfterDays` is required when `submissionFrequency` is set.",+ ),+ );+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – submissionsOpenAt without endsAt is rejected", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ expect(+ await api.patch(`/api/bounties/${id}`, {+ submissionsOpenAt: addDays(new Date(), 5).toISOString(),+ }),+ ).toEqual(+ badRequest("`endsAt` is required when `submissionsOpenAt` is set."),+ );+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – maxSubmissions below minimum is rejected", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ expect(+ await api.patch(`/api/bounties/${id}`, { maxSubmissions: 1 }),+ ).toEqual(+ unprocessable(+ "too_small: maxSubmissions: If `maxSubmissions` is set, it must be at least 2",+ ),+ );+ } finally {+ await deleteBounty(api, id);+ }+});++test("PATCH /bounties/{bountyId} – maxSubmissions above maximum is rejected", async ({+ api,+ program,+}) => {+ let id: string | undefined;++ try {+ const { data: created } = await createBounty(api, program);+ id = created.id;++ expect(+ await api.patch(`/api/bounties/${id}`, { maxSubmissions: 51 }),+ ).toEqual(+ unprocessable(+ "too_big: maxSubmissions: Too big: expected number to be <=50",+ ),+ );+ } finally {+ await deleteBounty(api, id);+ }+});++test("POST /bounties – invalid group IDs", async ({ api, program }) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, { groupIds: ["invalid-group-id"] }),+ }),+ ).toEqual(badRequest("Invalid group IDs detected: invalid-group-id"));+});++test("POST /bounties – maxSubmissions below minimum is rejected", async ({+ api,+ program,+}) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, { maxSubmissions: 1 }),+ }),+ ).toEqual(+ unprocessable(+ "too_small: maxSubmissions: If `maxSubmissions` is set, it must be at least 2",+ ),+ );+});++test("POST /bounties – maxSubmissions above maximum is rejected", async ({+ api,+ program,+}) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, { maxSubmissions: 51 }),+ }),+ ).toEqual(+ unprocessable(+ "too_big: maxSubmissions: Too big: expected number to be <=50",+ ),+ );+});++test("POST /bounties – submissionFrequency without maxSubmissions is rejected", async ({+ api,+ program,+}) => {+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startsAt,+ endsAt,+ submissionFrequency: "week",+ }),+ }),+ ).toEqual(+ badRequest(+ "`maxSubmissions` is required when `submissionFrequency` is set.",+ ),+ );+});++test("POST /bounties – submissionFrequency without endsAt is rejected", async ({+ api,+ program,+}) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ endsAt: null,+ maxSubmissions: 4,+ submissionFrequency: "week",+ }),+ }),+ ).toEqual(+ badRequest(+ "`endsAt` or `endsAfterDays` is required when `submissionFrequency` is set.",+ ),+ );+});++test("POST /bounties – submissionsOpenAt without endsAt is rejected", async ({+ api,+ program,+}) => {+ const startsAt = futureStartsAt();++ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startsAt,+ endsAt: null,+ submissionsOpenAt: addDays(new Date(startsAt), 5).toISOString(),+ }),+ }),+ ).toEqual(+ badRequest("`endsAt` is required when `submissionsOpenAt` is set."),+ );+});++test("POST /bounties – submissionsOpenAt before startsAt is rejected", async ({+ api,+ program,+}) => {+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startsAt,+ endsAt,+ submissionsOpenAt: subDays(new Date(startsAt), 1).toISOString(),+ }),+ }),+ ).toEqual(badRequest("`submissionsOpenAt` must be on or after `startsAt`."));+});++test("POST /bounties – submissionsOpenAt after endsAt is rejected", async ({+ api,+ program,+}) => {+ const startsAt = futureStartsAt();+ const endsAt = addMonths(new Date(startsAt), 1).toISOString();++ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startsAt,+ endsAt,+ submissionsOpenAt: addDays(new Date(endsAt), 1).toISOString(),+ }),+ }),+ ).toEqual(badRequest("`submissionsOpenAt` must be on or before `endsAt`."));+});++test("POST /bounties – relative with startsAt is rejected", async ({+ api,+ program,+}) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startMode: BountyStartMode.relative,+ startsAt: futureStartsAt(),+ endsAfterDays: 30,+ }),+ }),+ ).toEqual(+ badRequest(+ "`startsAt` is not supported when the `startMode` is `relative`.",+ ),+ );+});++test("POST /bounties – both endsAt and endsAfterDays is rejected", async ({+ api,+ program,+}) => {+ expect(+ await api.post("/api/bounties", {+ ...bountyPayload(program, {+ startMode: BountyStartMode.relative,+ startsAt: null,+ endsAt: addDays(new Date(), 30).toISOString(),+ endsAfterDays: 30,+ }),+ }),+ ).toEqual(+ badRequest("Bounties cannot have both `endsAt` and `endsAfterDays`."),+ );+});++const unknownBountyId = "bnty_does_not_exist";++test("GET /bounties/{bountyId} – not found", async ({ api }) => {+ expect(await api.get(`/api/bounties/${unknownBountyId}`)).toEqual(+ notFound(unknownBountyId),+ );+});++test("PATCH /bounties/{bountyId} – not found", async ({ api }) => {+ expect(+ await api.patch(`/api/bounties/${unknownBountyId}`, { name: "x" }),+ ).toEqual(notFound(unknownBountyId));+});++test("DELETE /bounties/{bountyId} – not found", async ({ api }) => {+ expect(await api.delete(`/api/bounties/${unknownBountyId}`)).toEqual(+ notFound(unknownBountyId),+ );+});diff --git a/apps/web/tests/bounties/index.test.ts b/apps/web/tests/bounties/index.test.tsdeleted file mode 100644index eb3191d361..0000000000--- a/apps/web/tests/bounties/index.test.ts+++ /dev/null@@ -1,677 +0,0 @@-import { Bounty, BountyStartMode } from "@prisma/client";-import { addDays, addMonths, subDays } from "date-fns";-import { E2E_PARTNER_GROUP } from "tests/utils/resource";-import { describe, expect, onTestFinished, test } from "vitest";-import { IntegrationHarness } from "../utils/integration";--// start 5 mins from now to make sure the bounty is fully deleted so it doesn't trigger email sends-const startsAt = new Date(Date.now() + 5 * 60 * 1000).toISOString();--const performanceBounty = {- name: "Earn $10 after generating 100 leads",- description: "some description about the bounty",- type: "performance",- startsAt,- endsAt: null,- rewardAmount: 1000,- performanceScope: "new",-};--const submissionBounty = {- name: "Submission Bounty",- description: "some description about the bounty",- type: "submission",- startsAt,- endsAt: null,- submissionsOpenAt: null,- rewardAmount: 1000,- submissionRequirements: {- image: {- max: 4,- },- url: {- max: 10,- },- },-};--describe.sequential("/bounties/**", async () => {- const h = new IntegrationHarness();- const { http } = await h.init();-- let submissionBountyId = "";-- test("POST /bounties - performance based", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...performanceBounty,- groupIds: [E2E_PARTNER_GROUP.id],- performanceCondition: {- attribute: "totalLeads",- operator: "gte",- value: 100,- },- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...performanceBounty,- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - performance based with performanceScope set to new", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...performanceBounty,- groupIds: [E2E_PARTNER_GROUP.id],- performanceScope: "new",- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...performanceBounty,- performanceScope: "new",- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - submission based", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...submissionBounty,- groupIds: [E2E_PARTNER_GROUP.id],- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...submissionBounty,- });-- submissionBountyId = bounty.id;- });-- test("POST /bounties - submission based with rewardDescription", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...submissionBounty,- groupIds: [E2E_PARTNER_GROUP.id],- rewardAmount: null,- rewardDescription: "some reward description",- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...submissionBounty,- rewardAmount: null,- rewardDescription: "some reward description",- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - submission based with submissionsOpenAt", async () => {- const now = new Date();- const startsAt = addDays(now, 1);- const endsAt = addDays(startsAt, 30);- const submissionsOpenAt = subDays(endsAt, 2);-- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...submissionBounty,- startsAt,- endsAt,- submissionsOpenAt,- groupIds: [E2E_PARTNER_GROUP.id],- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...submissionBounty,- startsAt: startsAt.toISOString(),- endsAt: endsAt.toISOString(),- submissionsOpenAt: submissionsOpenAt.toISOString(),- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - invalid group IDs", async () => {- const { status, data } = await http.post({- path: "/bounties",- body: {- ...submissionBounty,- groupIds: ["invalid-group-id"],- },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "Invalid group IDs detected: invalid-group-id",- code: "bad_request",- },- });- });-- test("GET /bounties/{bountyId}", async () => {- const { status, data: bounty } = await http.get<Bounty>({- path: `/bounties/${submissionBountyId}`,- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...submissionBounty,- });- });-- test("GET /bounties", async () => {- const { status, data: bounties } = await http.get<Bounty[]>({- path: `/bounties`,- });-- expect(status).toEqual(200);- expect(bounties.length).toBeGreaterThanOrEqual(1);- });-- test("PATCH /bounties/{bountyId}", async () => {- const now = new Date();- const endsAt = addDays(now, 30);-- const toUpdate = {- name: "Submission Bounty Updated",- endsAt: endsAt.toISOString(),- rewardAmount: 2000,- };-- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${submissionBountyId}`,- body: {- ...toUpdate,- type: "performance", // should skip the type update- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- ...submissionBounty,- ...toUpdate,- });- });-- test("DELETE /bounties/{bountyId}", async () => {- const { status, data: bounty } = await http.delete<{ id: string }>({- path: `/bounties/${submissionBountyId}`,- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: submissionBountyId,- });- });-});--describe.sequential(- "/bounties - multiple submissions & frequency",- async () => {- const h = new IntegrationHarness();- const { http } = await h.init();-- const bountyStartsAt = new Date(Date.now() + 5 * 60 * 1000);- const bountyEndsAt = addMonths(bountyStartsAt, 1);-- const base = {- name: "Multi-Submission Bounty",- type: "submission",- startsAt: bountyStartsAt.toISOString(),- endsAt: bountyEndsAt.toISOString(),- rewardAmount: 1000,- submissionRequirements: { image: { max: 4 } },- groupIds: [E2E_PARTNER_GROUP.id],- };-- let bountyId = "";-- test("POST /bounties - maxSubmissions persists for submission bounty", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: { ...base, maxSubmissions: 5 },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- id: expect.any(String),- maxSubmissions: 5,- submissionFrequency: null,- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - submissionFrequency 'week' with maxSubmissions", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: { ...base, maxSubmissions: 4, submissionFrequency: "week" },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- maxSubmissions: 4,- submissionFrequency: "week",- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - submissionFrequency 'month' with maxSubmissions", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: { ...base, maxSubmissions: 3, submissionFrequency: "month" },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- maxSubmissions: 3,- submissionFrequency: "month",- });-- bountyId = bounty.id;- });-- test("POST /bounties - maxSubmissions and submissionFrequency are ignored for performance bounties", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- type: "performance",- startsAt: bountyStartsAt.toISOString(),- endsAt: null,- rewardAmount: 1000,- performanceScope: "new",- groupIds: [E2E_PARTNER_GROUP.id],- performanceCondition: {- attribute: "totalLeads",- operator: "gte",- value: 50,- },- maxSubmissions: 5,- submissionFrequency: "week",- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- maxSubmissions: 1,- submissionFrequency: null,- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - maxSubmissions below minimum (1) is rejected", async () => {- const { status } = await http.post({- path: "/bounties",- body: { ...base, maxSubmissions: 1 },- });-- expect(status).toEqual(422);- });-- test("POST /bounties - maxSubmissions above maximum (50) is rejected", async () => {- const { status } = await http.post({- path: "/bounties",- body: { ...base, maxSubmissions: 51 },- });-- expect(status).toEqual(422);- });-- test("POST /bounties - submissionFrequency without maxSubmissions is rejected", async () => {- const { status, data } = await http.post({- path: "/bounties",- body: { ...base, submissionFrequency: "week" },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message:- "`maxSubmissions` is required when `submissionFrequency` is set.",- },- });- });-- test("POST /bounties - submissionFrequency without endsAt is rejected", async () => {- const { status, data } = await http.post({- path: "/bounties",- body: {- ...base,- endsAt: null,- maxSubmissions: 4,- submissionFrequency: "week",- },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message:- "`endsAt` or `endsAfterDays` is required when `submissionFrequency` is set.",- },- });- });-- test("POST /bounties - submissionFrequency with relative endsAfterDays is accepted", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...base,- startMode: BountyStartMode.relative,- startsAt: null,- endsAt: null,- endsAfterDays: 30,- maxSubmissions: 4,- submissionFrequency: "week",- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- startMode: BountyStartMode.relative,- startsAt: null,- endsAt: null,- endsAfterDays: 30,- maxSubmissions: 4,- submissionFrequency: "week",- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - submissionsOpenAt without endsAt is rejected", async () => {- const submissionsOpenAt = addDays(bountyStartsAt, 5).toISOString();-- const { status, data } = await http.post({- path: "/bounties",- body: { ...base, endsAt: null, submissionsOpenAt },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "`endsAt` is required when `submissionsOpenAt` is set.",- },- });- });-- test("POST /bounties - submissionsOpenAt before startsAt is rejected", async () => {- const submissionsOpenAt = subDays(bountyStartsAt, 1).toISOString();-- const { status, data } = await http.post({- path: "/bounties",- body: { ...base, submissionsOpenAt },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "`submissionsOpenAt` must be on or after `startsAt`.",- },- });- });-- test("POST /bounties - submissionsOpenAt after endsAt is rejected", async () => {- const submissionsOpenAt = addDays(bountyEndsAt, 1).toISOString();-- const { status, data } = await http.post({- path: "/bounties",- body: { ...base, submissionsOpenAt },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "`submissionsOpenAt` must be on or before `endsAt`.",- },- });- });-- test("PATCH /bounties/{bountyId} - update maxSubmissions", async () => {- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${bountyId}`,- body: { maxSubmissions: 6 },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({ maxSubmissions: 6 });- });-- test("PATCH /bounties/{bountyId} - update submissionFrequency", async () => {- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${bountyId}`,- body: { submissionFrequency: "day" },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({ submissionFrequency: "day" });- });-- test("PATCH /bounties/{bountyId} - clear submissionFrequency to null", async () => {- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${bountyId}`,- body: { submissionFrequency: null },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({ submissionFrequency: null });- });-- test("PATCH /bounties/{bountyId} - clear maxSubmissions to null", async () => {- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${bountyId}`,- body: { maxSubmissions: null },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({ maxSubmissions: 1 });- });-- test("PATCH /bounties/{bountyId} - update submissionsOpenAt", async () => {- const submissionsOpenAt = addDays(bountyStartsAt, 5).toISOString();-- const { status, data: bounty } = await http.patch<Bounty>({- path: `/bounties/${bountyId}`,- body: { submissionsOpenAt },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- submissionsOpenAt: expect.any(String),- });- });-- test("PATCH /bounties/{bountyId} - submissionFrequency requires endsAt on the bounty", async () => {- const { status: clearStatus } = await http.patch({- path: `/bounties/${bountyId}`,- body: { endsAt: null },- });- expect(clearStatus).toEqual(200);-- const { status, data } = await http.patch({- path: `/bounties/${bountyId}`,- body: { submissionFrequency: "week", maxSubmissions: 4 },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message:- "`endsAt` or `endsAfterDays` is required when `submissionFrequency` is set.",- },- });- });-- test("PATCH /bounties/{bountyId} - submissionsOpenAt without endsAt is rejected", async () => {- const submissionsOpenAt = addDays(bountyStartsAt, 5).toISOString();-- const { status, data } = await http.patch({- path: `/bounties/${bountyId}`,- body: { submissionsOpenAt },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "`endsAt` is required when `submissionsOpenAt` is set.",- },- });- });-- test("PATCH /bounties/{bountyId} - maxSubmissions below minimum (1) is rejected", async () => {- const { status } = await http.patch({- path: `/bounties/${bountyId}`,- body: { maxSubmissions: 1 },- });-- expect(status).toEqual(422);- });-- test("PATCH /bounties/{bountyId} - maxSubmissions above maximum (50) is rejected", async () => {- const { status } = await http.patch({- path: `/bounties/${bountyId}`,- body: { maxSubmissions: 51 },- });-- expect(status).toEqual(422);- });-- test("DELETE /bounties/{bountyId}", async () => {- const { status } = await http.delete({ path: `/bounties/${bountyId}` });- expect(status).toEqual(200);- });- },-);--describe.sequential("/bounties - relative start mode", async () => {- const h = new IntegrationHarness();- const { http } = await h.init();-- const relativeSubmissionBase = {- name: "Relative Submission Bounty",- description: "starts when a partner joins",- type: "submission",- startMode: BountyStartMode.relative,- startsAt: null,- endsAt: null,- rewardAmount: 1000,- submissionRequirements: { image: { max: 4 } },- groupIds: [E2E_PARTNER_GROUP.id],- };-- test("POST /bounties - relative with endsAfterDays", async () => {- const { status, data: bounty } = await http.post<Bounty>({- path: "/bounties",- body: {- ...relativeSubmissionBase,- endsAfterDays: 30,- },- });-- expect(status).toEqual(200);- expect(bounty).toMatchObject({- startMode: BountyStartMode.relative,- startsAt: null,- endsAt: null,- endsAfterDays: 30,- });-- const { status: patchStatus, data: updated } = await http.patch<Bounty>({- path: `/bounties/${bounty.id}`,- body: { endsAfterDays: 180 },- });-- expect(patchStatus).toEqual(200);- expect(updated).toMatchObject({- startMode: BountyStartMode.relative,- startsAt: null,- endsAfterDays: 180,- });-- onTestFinished(async () => {- await h.deleteBounty(bounty.id);- });- });-- test("POST /bounties - relative with startsAt is rejected", async () => {- const { status, data } = await http.post({- path: "/bounties",- body: {- ...relativeSubmissionBase,- startsAt: new Date(Date.now() + 5 * 60 * 1000).toISOString(),- endsAfterDays: 30,- },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message:- "`startsAt` is not supported when the `startMode` is `relative`.",- code: "bad_request",- },- });- });-- test("POST /bounties - both endsAt and endsAfterDays is rejected", async () => {- const { status, data } = await http.post({- path: "/bounties",- body: {- ...relativeSubmissionBase,- endsAt: addDays(new Date(), 30).toISOString(),- endsAfterDays: 30,- },- });-- expect(status).toEqual(400);- expect(data).toMatchObject({- error: {- message: "Bounties cannot have both `endsAt` and `endsAfterDays`.",- code: "bad_request",- },- });- });-});