import { getFiltersFromSearchParams, getDateTimeRange, maskBrowserVersion, } from "../utils"; import { describe, test, expect, beforeEach, afterEach, vi } from "vitest"; import dayjs from "dayjs"; import utc from "dayjs/plugin/utc"; import timezone from "dayjs/plugin/timezone"; dayjs.extend(utc); dayjs.extend(timezone); // test this getfiltersfromsearchparams function describe("getFiltersFromSearchParams", () => { test("it should return an object with the correct keys", () => { const searchParams = new URLSearchParams( "?path=/about&referrer=google.com&deviceType=mobile&country=us&browserName=chrome&browserVersion=118", ); expect(getFiltersFromSearchParams(searchParams)).toEqual({ path: "/about", referrer: "google.com", deviceType: "mobile", country: "us", browserName: "chrome", browserVersion: "118", }); }); test("it should ignore keys it doesn't recognize", () => { // only path is valid; unknown should be discarded const searchParams = new URLSearchParams("?unknown=foo&path=/about"); expect(getFiltersFromSearchParams(searchParams)).toEqual({ path: "/about", }); }); test("it should handle UTM parameters correctly", () => { const searchParams = new URLSearchParams( "?utmSource=google&utmMedium=cpc&utmCampaign=summer_sale&utmTerm=analytics&utmContent=banner_ad&path=/landing", ); expect(getFiltersFromSearchParams(searchParams)).toEqual({ utmSource: "google", utmMedium: "cpc", utmCampaign: "summer_sale", utmTerm: "analytics", utmContent: "banner_ad", path: "/landing", }); }); test("it should handle mixed standard and UTM parameters", () => { const searchParams = new URLSearchParams( "?path=/about&referrer=google.com&utmSource=google&utmMedium=email&browserName=chrome", ); expect(getFiltersFromSearchParams(searchParams)).toEqual({ path: "/about", referrer: "google.com", utmSource: "google", utmMedium: "email", browserName: "chrome", }); }); }); describe("getDateTimeRange", () => { const tz = "America/Los_Angeles"; const mockDate = new Date("2024-01-15T15:00:00Z"); // 7am PST on Jan 15, 2024 beforeEach(() => { vi.setSystemTime(mockDate); }); afterEach(() => { vi.restoreAllMocks(); }); test("handles 'today' interval", () => { const { startDate, endDate } = getDateTimeRange("today", tz); // Start date should be Jan 15, 2024 00:00 PST expect(startDate).toEqual(new Date("2024-01-15T08:00:00Z")); // 00:00 PST = 08:00 UTC expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z")); // Current time (7am PST = 15:00 UTC) }); test("handles 'yesterday' interval", () => { const { startDate, endDate } = getDateTimeRange("yesterday", tz); expect(startDate).toEqual(new Date("2024-01-14T08:00:00Z")); expect(endDate).toEqual(new Date("2024-01-15T08:00:00.001Z")); }); test("handles '7d' interval", () => { const { startDate, endDate } = getDateTimeRange("7d", tz); expect(startDate).toEqual(new Date("2024-01-08T08:00:00Z")); expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z")); }); test("handles '1d' interval", () => { const { startDate, endDate } = getDateTimeRange("1d", tz); expect(startDate).toEqual(new Date("2024-01-14T15:00:00Z")); expect(endDate).toEqual(new Date("2024-01-15T15:00:00Z")); }); test("works with different timezones", () => { const tokyoTz = "Asia/Tokyo"; // UTC+9 const londonTz = "Europe/London"; // UTC+0 in January // When it's 7am PST (15:00 UTC) on Jan 15: // - Tokyo: 00:00 JST on Jan 16 // - London: 15:00 GMT on Jan 15 // - LA: 7:00 PST on Jan 15 const tokyo = getDateTimeRange("today", tokyoTz); const london = getDateTimeRange("today", londonTz); // Tokyo's start date should be Jan 15 15:00 UTC (00:00 JST Jan 16) expect(tokyo.startDate).toEqual(new Date("2024-01-15T15:00:00Z")); // London's start date should be Jan 15 00:00 UTC expect(london.startDate).toEqual(new Date("2024-01-15T00:00:00Z")); }); }); describe("maskBrowserVersion", () => { const browserVersions = [ ["Microsoft Edge", "119.0.0.0", "119.x.x.x"], ["Google Chrome", "119.0.0.0", "119.x.x.x"], ["Mozilla Firefox", "119.0", "119.x"], ["Safari", "605.1.15", "605.x.x"], ["DuckDuckGo", "5", "5"], ["Brave", "129.0.6668.54", "129.x.x.x"], ["Opera", "117.0.0.0", "117.x.x.x"], ]; test.each(browserVersions)("%s", (_, version, expected) => { expect(maskBrowserVersion(version)).toEqual(expected); }); });