🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Enhance site configuration handling and improve test coverage
Bill Yange1facee7e6c9
diff --git a/server/src/analytics-script/config.test.ts b/server/src/analytics-script/config.test.tsindex f056188bc..ce42381b7 100644--- a/server/src/analytics-script/config.test.ts+++ b/server/src/analytics-script/config.test.ts@@ -56,6 +56,7 @@ describe("parseScriptConfig", () => {trackButtonClicks: false,trackCopy: false,trackFormInteractions: false,+ tag: "",skipPatterns: [],maskPatterns: [],sessionReplayBatchInterval: 5000,@@ -75,13 +76,10 @@ describe("parseScriptConfig", () => {sessionReplaySampleRate: undefined,});- expect(global.fetch).toHaveBeenCalledWith(- "https://analytics.example.com/site/tracking-config/123",- {- method: "GET",- credentials: "omit",- }- );+ expect(global.fetch).toHaveBeenCalledWith("https://analytics.example.com/site/tracking-config/123", {+ method: "GET",+ credentials: "omit",+ });});it("should use defaults when API call fails", async () => {@@ -111,6 +109,7 @@ describe("parseScriptConfig", () => {trackButtonClicks: false,trackCopy: false,trackFormInteractions: false,+ tag: "",skipPatterns: [],maskPatterns: [],sessionReplayBatchInterval: 5000,@@ -129,9 +128,7 @@ describe("parseScriptConfig", () => {sessionReplaySampleRate: undefined,});- expect(consoleWarnSpy).toHaveBeenCalledWith(- "Failed to fetch tracking config from API, using defaults"- );+ expect(consoleWarnSpy).toHaveBeenCalledWith("Failed to fetch tracking config from API, using defaults");});it("should use defaults when network error occurs", async () => {@@ -158,6 +155,7 @@ describe("parseScriptConfig", () => {trackButtonClicks: false,trackCopy: false,trackFormInteractions: false,+ tag: "",skipPatterns: [],maskPatterns: [],sessionReplayBatchInterval: 5000,@@ -176,10 +174,7 @@ describe("parseScriptConfig", () => {sessionReplaySampleRate: undefined,});- expect(consoleWarnSpy).toHaveBeenCalledWith(- "Error fetching tracking config:",- expect.any(Error)- );+ expect(consoleWarnSpy).toHaveBeenCalledWith("Error fetching tracking config:", expect.any(Error));});it("should handle missing src attribute", async () => {@@ -366,7 +361,10 @@ describe("parseScriptConfig", () => {it("should parse session replay mask text selectors", async () => {mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");mockScriptTag.setAttribute("data-site-id", "123");- mockScriptTag.setAttribute("data-replay-mask-text-selectors", '[".user-name", ".email-address", "[data-sensitive]"]');+ mockScriptTag.setAttribute(+ "data-replay-mask-text-selectors",+ '[".user-name", ".email-address", "[data-sensitive]"]'+ );// Mock successful API response(global.fetch as any).mockResolvedValueOnce({diff --git a/server/src/lib/siteConfig.test.ts b/server/src/lib/siteConfig.test.tsnew file mode 100644index 000000000..f054df6de--- /dev/null+++ b/server/src/lib/siteConfig.test.ts@@ -0,0 +1,175 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import { siteConfig } from "./siteConfig.js";++type SiteRow = {+ id: string | null;+ siteId: number;+ public: boolean | null;+ embedEnabled: boolean | null;+ saltUserIds: boolean | null;+ domain: string | null;+ blockBots: boolean;+ excludedIPs: unknown;+ excludedCountries: unknown;+ privateLinkKey: string | null;+ sessionReplay: boolean | null;+ webVitals: boolean | null;+ trackErrors: boolean | null;+ trackOutbound: boolean | null;+ trackUrlParams: boolean | null;+ trackInitialPageView: boolean | null;+ trackSpaNavigation: boolean | null;+ trackIp: boolean | null;+ trackButtonClicks: boolean | null;+ trackCopy: boolean | null;+ trackFormInteractions: boolean | null;+ tags: unknown;+};++type DrizzleCondition = {+ queryChunks?: Array<{+ name?: string;+ value?: unknown;+ }>;+};++const dbMock = vi.hoisted(() => ({+ rows: [] as SiteRow[],+ queries: [] as Array<{ column: string | undefined; value: unknown }>,+ select: vi.fn(),+}));++vi.mock("../db/postgres/postgres.js", () => ({+ db: {+ select: dbMock.select,+ },+}));++function createSiteRow(overrides: Partial<SiteRow>): SiteRow {+ return {+ id: "abcdef123456",+ siteId: 123,+ public: false,+ embedEnabled: false,+ saltUserIds: false,+ domain: "example.com",+ blockBots: true,+ excludedIPs: [],+ excludedCountries: [],+ privateLinkKey: null,+ sessionReplay: false,+ webVitals: false,+ trackErrors: false,+ trackOutbound: true,+ trackUrlParams: true,+ trackInitialPageView: true,+ trackSpaNavigation: true,+ trackIp: false,+ trackButtonClicks: false,+ trackCopy: false,+ trackFormInteractions: false,+ tags: [],+ ...overrides,+ };+}++function getConditionDetails(condition: DrizzleCondition): { column: string | undefined; value: unknown } {+ const column = condition.queryChunks?.find(chunk => chunk.name === "id" || chunk.name === "site_id")?.name;+ const value = condition.queryChunks?.find(+ chunk => Object.prototype.hasOwnProperty.call(chunk, "value") && !Array.isArray(chunk.value)+ )?.value;++ return { column, value };+}++beforeEach(() => {+ dbMock.rows.length = 0;+ dbMock.queries.length = 0;+ dbMock.select.mockReset();+ (siteConfig as unknown as { cache: Map<string, unknown> }).cache.clear();++ dbMock.select.mockImplementation(() => ({+ from: () => ({+ where: (condition: DrizzleCondition) => ({+ limit: async () => {+ const query = getConditionDetails(condition);+ dbMock.queries.push(query);++ const row = dbMock.rows.find(site => {+ if (query.column === "id") {+ return site.id === query.value;+ }++ if (query.column === "site_id") {+ return site.siteId === query.value;+ }++ return false;+ });++ return row ? [row] : [];+ },+ }),+ }),+ }));+});++describe("siteConfig.getConfig", () => {+ it("resolves digit-only string site IDs by exact sites.id before legacy site_id fallback", async () => {+ dbMock.rows.push(+ createSiteRow({+ id: "123456789012",+ siteId: 42,+ domain: "numeric-text-id.example",+ })+ );++ const config = await siteConfig.getConfig("123456789012");++ expect(config?.siteId).toBe(42);+ expect(config?.id).toBe("123456789012");+ expect(dbMock.queries).toEqual([{ column: "id", value: "123456789012" }]);+ });++ it("falls back to legacy numeric site_id when a numeric string has no exact sites.id match", async () => {+ dbMock.rows.push(+ createSiteRow({+ id: "abcdef123456",+ siteId: 21,+ domain: "legacy-id.example",+ })+ );++ const config = await siteConfig.getConfig("21");++ expect(config?.siteId).toBe(21);+ expect(config?.id).toBe("abcdef123456");+ expect(dbMock.queries).toEqual([+ { column: "id", value: "21" },+ { column: "site_id", value: 21 },+ ]);+ });++ it("keeps numeric and string cache keys separate", async () => {+ dbMock.rows.push(+ createSiteRow({+ id: "123",+ siteId: 99,+ }),+ createSiteRow({+ id: "abcdef123456",+ siteId: 123,+ })+ );++ const exactTextConfig = await siteConfig.getConfig("123");+ const legacyConfig = await siteConfig.getConfig(123);++ expect(exactTextConfig?.siteId).toBe(99);+ expect(legacyConfig?.siteId).toBe(123);+ expect(dbMock.queries).toEqual([+ { column: "id", value: "123" },+ { column: "site_id", value: 123 },+ ]);+ });+});diff --git a/server/src/lib/siteConfig.ts b/server/src/lib/siteConfig.tsindex 55caf7595..8639ec5e0 100644--- a/server/src/lib/siteConfig.ts+++ b/server/src/lib/siteConfig.ts@@ -1,4 +1,4 @@-import { eq } from "drizzle-orm";+import { eq, type SQL } from "drizzle-orm";import { db } from "../db/postgres/postgres.js";import { sites } from "../db/postgres/schema.js";import { matchesCIDR, matchesRange } from "./ipUtils.js";@@ -30,12 +30,14 @@ export interface SiteConfigData {tags: string[];}+type SiteConfigRow = typeof sites.$inferSelect;+class SiteConfig {private cache = new Map<string, { data: SiteConfigData; expires: number }>();private cacheTTL = 60 * 1000; // 1 minute TTL/**- * Helper to determine if the input is a numeric siteId or string id+ * Helper to determine if the input can be interpreted as a legacy numeric siteId*/private isNumericId(id: string | number): boolean {if (typeof id === "number") {@@ -45,11 +47,34 @@ class SiteConfig {return /^\d+$/.test(id);}+ private getCacheKey(siteIdOrId: string | number): string {+ return `${typeof siteIdOrId}:${siteIdOrId}`;+ }++ private async querySiteConfig(where: SQL): Promise<SiteConfigRow | undefined> {+ const [site] = await db.select().from(sites).where(where).limit(1);++ return site;+ }++ private async findSiteByIdentifier(siteIdOrId: string | number): Promise<SiteConfigRow | undefined> {+ if (typeof siteIdOrId === "number") {+ return Number.isInteger(siteIdOrId) ? this.querySiteConfig(eq(sites.siteId, siteIdOrId)) : undefined;+ }++ const siteByExactId = await this.querySiteConfig(eq(sites.id, siteIdOrId));+ if (siteByExactId || !this.isNumericId(siteIdOrId)) {+ return siteByExactId;+ }++ return this.querySiteConfig(eq(sites.siteId, Number(siteIdOrId)));+ }+/*** Get site by either siteId or id*/private async getSiteByAnyId(siteIdOrId: string | number): Promise<SiteConfigData | undefined> {- const cacheKey = String(siteIdOrId);+ const cacheKey = this.getCacheKey(siteIdOrId);const cached = this.cache.get(cacheKey);if (cached && cached.expires > Date.now()) {@@ -57,36 +82,7 @@ class SiteConfig {}try {- const isNumeric = this.isNumericId(siteIdOrId);-- const [site] = await db- .select({- id: sites.id,- siteId: sites.siteId,- public: sites.public,- embedEnabled: sites.embedEnabled,- saltUserIds: sites.saltUserIds,- domain: sites.domain,- blockBots: sites.blockBots,- excludedIPs: sites.excludedIPs,- excludedCountries: sites.excludedCountries,- privateLinkKey: sites.privateLinkKey,- sessionReplay: sites.sessionReplay,- webVitals: sites.webVitals,- trackErrors: sites.trackErrors,- trackOutbound: sites.trackOutbound,- trackUrlParams: sites.trackUrlParams,- trackInitialPageView: sites.trackInitialPageView,- trackSpaNavigation: sites.trackSpaNavigation,- trackIp: sites.trackIp,- trackButtonClicks: sites.trackButtonClicks,- trackCopy: sites.trackCopy,- trackFormInteractions: sites.trackFormInteractions,- tags: sites.tags,- })- .from(sites)- .where(isNumeric ? eq(sites.siteId, Number(siteIdOrId)) : eq(sites.id, String(siteIdOrId)))- .limit(1);+ const site = await this.findSiteByIdentifier(siteIdOrId);if (!site) {return undefined;@@ -146,7 +142,7 @@ class SiteConfig {.where(isNumeric ? eq(sites.siteId, Number(siteIdOrId)) : eq(sites.id, String(siteIdOrId)));// Invalidate cache after update- this.cache.delete(String(siteIdOrId));+ this.cache.clear();} catch (error) {logger.error(error as Error, `Error updating site configuration for ${siteIdOrId}`);}@@ -182,7 +178,7 @@ class SiteConfig {await db.delete(sites).where(isNumeric ? eq(sites.siteId, Number(siteIdOrId)) : eq(sites.id, String(siteIdOrId)));// Invalidate cache after deletion- this.cache.delete(String(siteIdOrId));+ this.cache.clear();} catch (error) {logger.error(error as Error, `Error removing site ${siteIdOrId}`);}