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: e2e for advanced filters

Pedro Ladeiraf77562e3e0a7
diff --git a/apps/web/tests/analytics/advanced-filters.test.ts b/apps/web/tests/analytics/advanced-filters.test.ts
new file mode 100644
index 0000000000..dd6871271c
--- /dev/null
+++ b/apps/web/tests/analytics/advanced-filters.test.ts
@@ -0,0 +1,304 @@
+import { parseFilterValue, buildFilterValue } from "@dub/utils";
+import { buildAdvancedFilters } from "@/lib/analytics/build-advanced-filters";
+import { describe, expect, test } from "vitest";
+
+describe("Advanced Filters - Unit Tests", () => {
+ describe("parseFilterValue", () => {
+ describe("Single Values", () => {
+ test("single positive value", () => {
+ const result = parseFilterValue("US");
+ expect(result).toEqual({
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["US"],
+ });
+ });
+
+ test("single negative value", () => {
+ const result = parseFilterValue("-US");
+ expect(result).toEqual({
+ operator: "IS_NOT",
+ sqlOperator: "!=",
+ values: ["US"],
+ });
+ });
+ });
+
+ describe("Multiple Values", () => {
+ test("multiple positive values", () => {
+ const result = parseFilterValue("US,BR,FR");
+ expect(result).toEqual({
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["US", "BR", "FR"],
+ });
+ });
+
+ test("multiple negative values", () => {
+ const result = parseFilterValue("-US,BR,FR");
+ expect(result).toEqual({
+ operator: "IS_NOT_ONE_OF",
+ sqlOperator: "NOT IN",
+ values: ["US", "BR", "FR"],
+ });
+ });
+
+ test("two values", () => {
+ const result = parseFilterValue("mobile,desktop");
+ expect(result).toEqual({
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["mobile", "desktop"],
+ });
+ });
+ });
+
+ describe("Edge Cases", () => {
+ test("empty string returns undefined", () => {
+ expect(parseFilterValue("")).toBeUndefined();
+ });
+
+ test("undefined returns undefined", () => {
+ expect(parseFilterValue(undefined)).toBeUndefined();
+ });
+
+ test("filters empty values from comma-separated", () => {
+ const result = parseFilterValue("US,,BR");
+ expect(result?.values).toEqual(["US", "BR"]);
+ });
+
+ test("trailing comma", () => {
+ const result = parseFilterValue("US,BR,");
+ expect(result?.values).toEqual(["US", "BR"]);
+ });
+
+ test("only commas returns undefined", () => {
+ expect(parseFilterValue(",,,")).toBeUndefined();
+ });
+
+ test("minus sign only returns undefined", () => {
+ expect(parseFilterValue("-")).toBeUndefined();
+ });
+
+ test("minus with empty values returns undefined", () => {
+ expect(parseFilterValue("-,,,")).toBeUndefined();
+ });
+ });
+
+ describe("Array Input", () => {
+ test("array with single value", () => {
+ const result = parseFilterValue(["US"]);
+ expect(result).toEqual({
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["US"],
+ });
+ });
+
+ test("array with multiple values", () => {
+ const result = parseFilterValue(["US", "BR", "FR"]);
+ expect(result).toEqual({
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["US", "BR", "FR"],
+ });
+ });
+ });
+ });
+
+ describe("buildFilterValue", () => {
+ test("rebuild single positive value", () => {
+ const result = buildFilterValue({
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["US"],
+ });
+ expect(result).toBe("US");
+ });
+
+ test("rebuild multiple positive values", () => {
+ const result = buildFilterValue({
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["US", "BR", "FR"],
+ });
+ expect(result).toBe("US,BR,FR");
+ });
+
+ test("rebuild single negative value", () => {
+ const result = buildFilterValue({
+ operator: "IS_NOT",
+ sqlOperator: "!=",
+ values: ["US"],
+ });
+ expect(result).toBe("-US");
+ });
+
+ test("rebuild multiple negative values", () => {
+ const result = buildFilterValue({
+ operator: "IS_NOT_ONE_OF",
+ sqlOperator: "NOT IN",
+ values: ["US", "BR"],
+ });
+ expect(result).toBe("-US,BR");
+ });
+
+ describe("Round-trip", () => {
+ test("single positive", () => {
+ const original = "US";
+ const parsed = parseFilterValue(original)!;
+ const rebuilt = buildFilterValue(parsed);
+ expect(rebuilt).toBe(original);
+ });
+
+ test("multiple positive", () => {
+ const original = "US,BR,FR";
+ const parsed = parseFilterValue(original)!;
+ const rebuilt = buildFilterValue(parsed);
+ expect(rebuilt).toBe(original);
+ });
+
+ test("single negative", () => {
+ const original = "-US";
+ const parsed = parseFilterValue(original)!;
+ const rebuilt = buildFilterValue(parsed);
+ expect(rebuilt).toBe(original);
+ });
+
+ test("multiple negative", () => {
+ const original = "-US,BR,FR";
+ const parsed = parseFilterValue(original)!;
+ const rebuilt = buildFilterValue(parsed);
+ expect(rebuilt).toBe(original);
+ });
+ });
+ });
+
+ describe("buildAdvancedFilters", () => {
+ test("single field with equals operator", () => {
+ const result = buildAdvancedFilters({
+ country: {
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["US"],
+ },
+ });
+ expect(result).toEqual([
+ {
+ field: "country",
+ operator: "=",
+ values: ["US"],
+ },
+ ]);
+ });
+
+ test("single field with IN operator", () => {
+ const result = buildAdvancedFilters({
+ country: {
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["US", "BR", "FR"],
+ },
+ });
+ expect(result).toEqual([
+ {
+ field: "country",
+ operator: "IN",
+ values: ["US", "BR", "FR"],
+ },
+ ]);
+ });
+
+ test("single field with NOT IN operator", () => {
+ const result = buildAdvancedFilters({
+ device: {
+ operator: "IS_NOT_ONE_OF",
+ sqlOperator: "NOT IN",
+ values: ["Mobile", "Tablet"],
+ },
+ });
+ expect(result).toEqual([
+ {
+ field: "device",
+ operator: "NOT IN",
+ values: ["Mobile", "Tablet"],
+ },
+ ]);
+ });
+
+ test("multiple fields combined", () => {
+ const result = buildAdvancedFilters({
+ country: {
+ operator: "IS_ONE_OF",
+ sqlOperator: "IN",
+ values: ["US", "BR"],
+ },
+ device: {
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["Desktop"],
+ },
+ });
+ expect(result).toHaveLength(2);
+ expect(result).toContainEqual({
+ field: "country",
+ operator: "IN",
+ values: ["US", "BR"],
+ });
+ expect(result).toContainEqual({
+ field: "device",
+ operator: "=",
+ values: ["Desktop"],
+ });
+ });
+
+ test("empty params returns empty array", () => {
+ const result = buildAdvancedFilters({});
+ expect(result).toEqual([]);
+ });
+
+ test("skips undefined fields", () => {
+ const result = buildAdvancedFilters({
+ country: {
+ operator: "IS",
+ sqlOperator: "=",
+ values: ["US"],
+ },
+ city: undefined,
+ device: undefined,
+ });
+ expect(result).toHaveLength(1);
+ expect(result[0].field).toBe("country");
+ });
+
+ test("handles all supported fields", () => {
+ const result = buildAdvancedFilters({
+ country: { operator: "IS", sqlOperator: "=", values: ["US"] },
+ city: { operator: "IS", sqlOperator: "=", values: ["NYC"] },
+ device: { operator: "IS", sqlOperator: "=", values: ["Mobile"] },
+ browser: { operator: "IS", sqlOperator: "=", values: ["Chrome"] },
+ os: { operator: "IS", sqlOperator: "=", values: ["Mac"] },
+ });
+ expect(result).toHaveLength(5);
+ expect(result.map((f) => f.field)).toEqual([
+ "country",
+ "city",
+ "device",
+ "browser",
+ "os",
+ ]);
+ });
+
+ test("maintains insertion order", () => {
+ const result = buildAdvancedFilters({
+ device: { operator: "IS", sqlOperator: "=", values: ["Mobile"] },
+ country: { operator: "IS", sqlOperator: "=", values: ["US"] },
+ browser: { operator: "IS", sqlOperator: "=", values: ["Chrome"] },
+ });
+ // Should maintain order from SUPPORTED_FIELDS, not insertion order
+ expect(result[0].field).toBe("country");
+ expect(result[1].field).toBe("device");
+ expect(result[2].field).toBe("browser");
+ });
+ });
+});
diff --git a/apps/web/tests/analytics/index.test.ts b/apps/web/tests/analytics/index.test.ts
index 13f17a0e80..a5dede3619 100644
--- a/apps/web/tests/analytics/index.test.ts
+++ b/apps/web/tests/analytics/index.test.ts
@@ -58,4 +58,174 @@ describe.runIf(env.CI).sequential("GET /analytics", async () => {
data.every((event) => event.metadata?.productId === "premiumProductId"),
).toBe(true);
});
+
+ describe("Advanced Filters", () => {
+ test("single country filter", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "count",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "US",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(data).toHaveProperty("clicks");
+ });
+
+ test("multiple countries filter (IS ONE OF)", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "countries",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "US,CA,GB",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // All returned countries should be in the filter
+ data.forEach((item: any) => {
+ expect(["US", "CA", "GB"]).toContain(item.country);
+ });
+ });
+
+ test("exclude country (IS NOT)", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "countries",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "-US",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // No country should be US
+ data.forEach((item: any) => {
+ expect(item.country).not.toBe("US");
+ });
+ });
+
+ test("exclude multiple countries (IS NOT ONE OF)", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "countries",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "-US,GB",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // No country should be US or GB
+ data.forEach((item: any) => {
+ expect(["US", "GB"]).not.toContain(item.country);
+ });
+ });
+
+ test("multiple devices filter", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "devices",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ device: "mobile,desktop",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+
+ // All returned devices should be in the filter
+ data.forEach((item: any) => {
+ expect(["Mobile", "Desktop"]).toContain(item.device);
+ });
+ });
+
+ test("country AND device filters combined", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "count",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "US",
+ device: "desktop",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(data).toHaveProperty("clicks");
+ // Should return clicks that are both from US AND desktop
+ });
+
+ test("timeseries with country filter", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "timeseries",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "US",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(Array.isArray(data)).toBe(true);
+ // Timeseries should only include US data
+ });
+
+ test("backward compatibility - old format still works", async () => {
+ const { status, data } = await http.get<any[]>({
+ path: `/analytics`,
+ query: {
+ event: "clicks",
+ groupBy: "count",
+ workspaceId,
+ interval: "30d",
+ domain: "dub.sh",
+ key: "checkly-check",
+ country: "US",
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(data).toHaveProperty("clicks");
+ // Old single-value format should still work
+ });
+ });
});