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.

6.0 KB
Raw Blame
import { clickEventResponseSchema } from "@/lib/zod/schemas/clicks";
import { CustomerSchema } from "@/lib/zod/schemas/customers";
import { leadEventResponseSchema as leadEventResponseSchemaRaw } from "@/lib/zod/schemas/leads";
import { saleEventResponseSchema as saleEventResponseSchemaRaw } from "@/lib/zod/schemas/sales";
import { describe, expect, test } from "vitest";
import * as z from "zod/v4";
import { env } from "../utils/env";
import { IntegrationHarness } from "../utils/integration";
import { E2E_TENANT_ID } from "../utils/resource";

const customerSchemaExtended = CustomerSchema.extend({
  createdAt: z.string().transform((str) => new Date(str)), // because the date is in UTC string in JSON
});

const leadEventResponseSchema = leadEventResponseSchemaRaw.extend({
  customer: customerSchemaExtended,
});

const saleEventResponseSchema = saleEventResponseSchemaRaw.extend({
  customer: customerSchemaExtended,
});

describe.runIf(env.CI).sequential("GET /events", async () => {
  const h = new IntegrationHarness();
  const { workspace, http } = await h.init();
  const workspaceId = workspace.id;

  test("get click events", async () => {
    const { status, data } = await http.get<any[]>({
      path: "/events",
      query: {
        event: "clicks",
        workspaceId,
        interval: "30d",
        domain: "dub.sh",
        key: "checkly-check",
      },
    });

    const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);

    expect(status).toEqual(200);
    expect(parsed.success).toBeTruthy();
  });

  test("get lead events", async () => {
    const { status, data } = await http.get<any[]>({
      path: "/events",
      query: {
        event: "leads",
        workspaceId,
        interval: "30d",
      },
    });

    const parsed = z.array(leadEventResponseSchema.strict()).safeParse(data);

    expect(status).toEqual(200);
    expect(parsed.success).toBeTruthy();
  });

  test("get sale events", async () => {
    const { status, data } = await http.get<any[]>({
      path: "/events",
      query: {
        event: "sales",
        workspaceId,
        interval: "30d",
      },
    });

    const parsed = z.array(saleEventResponseSchema.strict()).safeParse(data);

    expect(status).toEqual(200);
    expect(parsed.success).toBeTruthy();
  });

  describe("Advanced Filters", () => {
    test("filter events by country", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "clicks",
          workspaceId,
          interval: "30d",
          domain: "dub.sh",
          key: "checkly-check",
          country: "US",
        },
      });

      const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);

      expect(status).toEqual(200);
      expect(parsed.success).toBeTruthy();
      // expect all events to have country US
      data.forEach((event) => {
        expect(event.click.country).toBe("US");
      });
    });

    test("filter events by multiple countries (IS ONE OF)", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "clicks",
          workspaceId,
          interval: "30d",
          domain: "dub.sh",
          key: "checkly-check",
          country: "US,CA,GB",
        },
      });

      expect(status).toEqual(200);
      const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);
      expect(parsed.success).toBeTruthy();
      // expect all events to have country US, CA, or GB
      parsed.data?.forEach((event) => {
        expect(["US", "CA", "GB"]).toContain(event.click.country);
      });
    });

    test("exclude country (IS NOT)", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "clicks",
          workspaceId,
          interval: "30d",
          domain: "dub.sh",
          key: "checkly-check",
          country: "-US",
        },
      });

      expect(status).toEqual(200);
      const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);
      expect(parsed.success).toBeTruthy();
      // expect all events to not have country US
      parsed.data?.forEach((event) => {
        expect(event.click.country).not.toBe("US");
      });
    });

    test("filter events by metadata query", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "sales",
          workspaceId,
          interval: "30d",
          query: "metadata['productId']:premiumProductId",
        },
      });

      expect(status).toEqual(200);
      const parsed = z.array(saleEventResponseSchema.strict()).safeParse(data);
      expect(parsed.success).toBeTruthy();
      // expect all events to have metadata.productId equal to premiumProductId
      parsed.data?.forEach((event) => {
        expect(event.metadata?.productId).toBe("premiumProductId");
      });
    });

    test("filter events by single tenantId", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "clicks",
          workspaceId,
          interval: "all",
          tenantId: E2E_TENANT_ID,
        },
      });

      expect(status).toEqual(200);
      const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);
      expect(parsed.success).toBeTruthy();
      parsed.data?.forEach((event) => {
        expect(event.link.tenantId).toBe(E2E_TENANT_ID);
      });
    });

    test("exclude events by tenantId (IS NOT)", async () => {
      const { status, data } = await http.get<any[]>({
        path: "/events",
        query: {
          event: "clicks",
          workspaceId,
          interval: "all",
          tenantId: `-${E2E_TENANT_ID}`,
        },
      });

      expect(status).toEqual(200);
      const parsed = z.array(clickEventResponseSchema.strict()).safeParse(data);
      expect(parsed.success).toBeTruthy();
      parsed.data?.forEach((event) => {
        expect(event.link.tenantId).not.toBe(E2E_TENANT_ID);
      });
    });
  });
});