Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

24.7 KB
Raw Blame
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { parseScriptConfig } from "./config.js";

// Mock fetch globally
global.fetch = vi.fn();

describe("parseScriptConfig", () => {
  let mockScriptTag: HTMLScriptElement;
  let consoleSpy: any;
  let consoleWarnSpy: any;

  beforeEach(() => {
    mockScriptTag = document.createElement("script");
    consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
    consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
    localStorage.clear();
    vi.clearAllMocks();
  });

  afterEach(() => {
    consoleSpy.mockRestore();
    consoleWarnSpy.mockRestore();
  });

  it("should parse valid configuration with API response", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");

    // Mock successful API response
    (global.fetch as any)
      .mockResolvedValueOnce({
        ok: true,
        json: async () => ({
          featureFlagsEnabled: true,
          sessionReplay: true,
          webVitals: true,
          trackErrors: false,
          trackOutbound: true,
          trackUrlParams: false,
          trackUrlFragments: true,
          trackInitialPageView: true,
          trackSpaNavigation: false,
        }),
      })
      .mockResolvedValueOnce({
        ok: true,
        json: async () => ({
          flags: {
            new_checkout: {
              key: "new_checkout",
              value: true,
              flagType: "boolean",
              version: 1,
              reason: "rollout",
              matched: true,
              rolloutPercentage: 100,
            },
          },
        }),
      });

    const config = await parseScriptConfig(mockScriptTag);

    expect(config).toEqual({
      namespace: "rybbit",
      analyticsHost: "https://analytics.example.com",
      siteId: "123",
      visitorId: expect.any(String),
      debounceDuration: 500,
      autoTrackPageview: true, // trackInitialPageView from API
      autoTrackSpa: false, // trackSpaNavigation from API
      trackQuerystring: false, // trackUrlParams from API
      trackUrlFragments: true,
      trackOutbound: true, // trackOutbound from API
      enableWebVitals: true, // webVitals from API
      trackErrors: false, // trackErrors from API
      enableSessionReplay: true, // sessionReplay from API
      trackButtonClicks: false,
      trackCopy: false,
      trackFormInteractions: false,
      tag: "",
      featureFlagsEnabled: true,
      featureFlags: {
        new_checkout: {
          key: "new_checkout",
          value: true,
          flagType: "boolean",
          version: 1,
          reason: "rollout",
          matched: true,
          rolloutPercentage: 100,
        },
      },
      skipPatterns: [],
      maskPatterns: [],
      sessionReplayBatchInterval: 5000,
      sessionReplayBatchSize: 250,
      sessionReplayMaskTextSelectors: [],
      // rrweb options (undefined when not set)
      sessionReplayBlockClass: undefined,
      sessionReplayBlockSelector: undefined,
      sessionReplayIgnoreClass: undefined,
      sessionReplayIgnoreSelector: undefined,
      sessionReplayMaskTextClass: undefined,
      sessionReplayMaskAllInputs: undefined,
      sessionReplayMaskInputOptions: undefined,
      sessionReplayCollectFonts: undefined,
      sessionReplaySampling: undefined,
      sessionReplaySlimDOMOptions: undefined,
      sessionReplaySampleRate: undefined,
    });

    expect(global.fetch).toHaveBeenCalledWith("https://analytics.example.com/site/tracking-config/123", {
      method: "GET",
      credentials: "omit",
    });
  });

  it("should not request feature flag evaluation when the site has no flags", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");

    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({ featureFlagsEnabled: false }),
    });

    const config = await parseScriptConfig(mockScriptTag);

    expect(global.fetch).toHaveBeenCalledTimes(1);
    expect(config?.featureFlagsEnabled).toBe(false);
    expect(config?.featureFlags).toEqual({});
  });

  it("evaluates feature flags using opted-in checkout fragments", async () => {
    const originalLocation = Object.getOwnPropertyDescriptor(window, "location");
    Object.defineProperty(window, "location", {
      configurable: true,
      value: { href: "https://example.com/checkout/#shipping" },
    });
    try {
      mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
      mockScriptTag.setAttribute("data-site-id", "123");
      vi.mocked(fetch)
        .mockResolvedValueOnce({
          ok: true,
          json: async () => ({ featureFlagsEnabled: true, trackUrlFragments: true }),
        } as Response)
        .mockResolvedValueOnce({ ok: true, json: async () => ({ flags: {} }) } as Response);

      await parseScriptConfig(mockScriptTag);

      const body = JSON.parse(vi.mocked(fetch).mock.calls[1][1]!.body as string);
      expect(body.pathname).toBe("/checkout/#shipping");
    } finally {
      if (originalLocation) Object.defineProperty(window, "location", originalLocation);
    }
  });

  it("reuses a stored visitor id, but replaces one longer than the server accepts", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    const mockFlagsEnabled = () =>
      vi
        .mocked(fetch)
        .mockResolvedValueOnce({ ok: true, json: async () => ({ featureFlagsEnabled: true }) } as Response)
        .mockResolvedValueOnce({ ok: true, json: async () => ({ flags: {} }) } as Response);

    localStorage.setItem("rybbit-visitor-id", "stored-visitor");
    mockFlagsEnabled();
    expect((await parseScriptConfig(mockScriptTag))?.visitorId).toBe("stored-visitor");

    localStorage.setItem("rybbit-visitor-id", "x".repeat(65));
    mockFlagsEnabled();
    const { visitorId } = (await parseScriptConfig(mockScriptTag))!;
    expect(visitorId).not.toBe("x".repeat(65));
    expect(visitorId!.length).toBeLessThanOrEqual(64);
    expect(localStorage.getItem("rybbit-visitor-id")).toBe(visitorId);
  });

  it("stops future evaluation when flags are deleted between configuration and evaluation", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");

    (global.fetch as any)
      .mockResolvedValueOnce({
        ok: true,
        json: async () => ({ featureFlagsEnabled: true }),
      })
      .mockResolvedValueOnce({
        ok: true,
        json: async () => ({ featureFlagsEnabled: false, flags: {} }),
      });

    const config = await parseScriptConfig(mockScriptTag);

    expect(global.fetch).toHaveBeenCalledTimes(2);
    expect(config?.featureFlagsEnabled).toBe(false);
    expect(config?.featureFlags).toEqual({});
  });

  it("should use defaults when API call fails", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");

    // Mock failed API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: false,
      status: 404,
    });

    const config = await parseScriptConfig(mockScriptTag);

    expect(config).toEqual({
      namespace: "rybbit",
      analyticsHost: "https://analytics.example.com",
      siteId: "123",
      visitorId: expect.any(String),
      debounceDuration: 500,
      autoTrackPageview: true,
      autoTrackSpa: true,
      trackQuerystring: true,
      trackUrlFragments: false,
      trackOutbound: true,
      enableWebVitals: false,
      trackErrors: false,
      enableSessionReplay: false,
      trackButtonClicks: false,
      trackCopy: false,
      trackFormInteractions: false,
      tag: "",
      featureFlagsEnabled: false,
      featureFlags: {},
      skipPatterns: [],
      maskPatterns: [],
      sessionReplayBatchInterval: 5000,
      sessionReplayBatchSize: 250,
      sessionReplayMaskTextSelectors: [],
      sessionReplayBlockClass: undefined,
      sessionReplayBlockSelector: undefined,
      sessionReplayIgnoreClass: undefined,
      sessionReplayIgnoreSelector: undefined,
      sessionReplayMaskTextClass: undefined,
      sessionReplayMaskAllInputs: undefined,
      sessionReplayMaskInputOptions: undefined,
      sessionReplayCollectFonts: undefined,
      sessionReplaySampling: undefined,
      sessionReplaySlimDOMOptions: undefined,
      sessionReplaySampleRate: undefined,
    });

    expect(consoleWarnSpy).toHaveBeenCalledWith("Failed to fetch tracking config from API, using defaults");
  });

  it("should use defaults when network error occurs", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");

    // Mock network error
    (global.fetch as any).mockRejectedValueOnce(new Error("Network error"));

    const config = await parseScriptConfig(mockScriptTag);

    expect(config).toEqual({
      namespace: "rybbit",
      analyticsHost: "https://analytics.example.com",
      siteId: "123",
      visitorId: expect.any(String),
      debounceDuration: 500,
      autoTrackPageview: true,
      autoTrackSpa: true,
      trackQuerystring: true,
      trackUrlFragments: false,
      trackOutbound: true,
      enableWebVitals: false,
      trackErrors: false,
      enableSessionReplay: false,
      trackButtonClicks: false,
      trackCopy: false,
      trackFormInteractions: false,
      tag: "",
      featureFlagsEnabled: false,
      featureFlags: {},
      skipPatterns: [],
      maskPatterns: [],
      sessionReplayBatchInterval: 5000,
      sessionReplayBatchSize: 250,
      sessionReplayMaskTextSelectors: [],
      sessionReplayBlockClass: undefined,
      sessionReplayBlockSelector: undefined,
      sessionReplayIgnoreClass: undefined,
      sessionReplayIgnoreSelector: undefined,
      sessionReplayMaskTextClass: undefined,
      sessionReplayMaskAllInputs: undefined,
      sessionReplayMaskInputOptions: undefined,
      sessionReplayCollectFonts: undefined,
      sessionReplaySampling: undefined,
      sessionReplaySlimDOMOptions: undefined,
      sessionReplaySampleRate: undefined,
    });

    expect(consoleWarnSpy).toHaveBeenCalledWith("Error fetching tracking config:", expect.any(Error));
  });

  it("should handle missing src attribute", async () => {
    const config = await parseScriptConfig(mockScriptTag);
    expect(config).toBeNull();
    expect(consoleSpy).toHaveBeenCalledWith("Script src attribute is missing");
  });

  it("should handle invalid analytics host", async () => {
    mockScriptTag.setAttribute("src", "/script.js"); // No host part
    const config = await parseScriptConfig(mockScriptTag);
    expect(config).toBeNull();
    expect(consoleSpy).toHaveBeenCalledWith("Please provide a valid analytics host");
  });

  it("should handle missing site ID", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    const config = await parseScriptConfig(mockScriptTag);
    expect(config).toBeNull();
    expect(consoleSpy).toHaveBeenCalledWith(
      "Please provide a valid site ID using the ?siteId= query parameter or the data-site-id attribute"
    );
  });

  it("should parse non-numeric site ID", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "my-site");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({
        sessionReplay: false,
        webVitals: false,
        trackErrors: false,
        trackOutbound: true,
        trackUrlParams: true,
        trackInitialPageView: true,
        trackSpaNavigation: true,
      }),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.siteId).toBe("my-site");
  });

  it("should parse custom debounce duration", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-debounce", "1000");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.debounceDuration).toBe(1000);
  });

  it("should handle negative debounce duration", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-debounce", "-100");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.debounceDuration).toBe(0);
  });

  it("should override API config with data attributes for testing", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-skip-patterns", '["/admin/**"]');
    mockScriptTag.setAttribute("data-mask-patterns", '["/user/**"]');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({
        sessionReplay: true,
        webVitals: true,
        trackErrors: true,
        trackOutbound: false,
        trackUrlParams: false,
        trackInitialPageView: false,
        trackSpaNavigation: false,
      }),
    });

    const config = await parseScriptConfig(mockScriptTag);

    // These should be from data attributes (overrides for testing)
    expect(config?.skipPatterns).toEqual(["/admin/**"]);
    expect(config?.maskPatterns).toEqual(["/user/**"]);

    // These should be from API
    expect(config?.enableSessionReplay).toBe(true);
    expect(config?.enableWebVitals).toBe(true);
    expect(config?.trackErrors).toBe(true);
    expect(config?.trackOutbound).toBe(false);
  });

  it("should parse skip patterns", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-skip-patterns", '["/admin/**", "/api/**"]');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.skipPatterns).toEqual(["/admin/**", "/api/**"]);
  });

  it("should parse mask patterns", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-mask-patterns", '["/user/*/profile", "/post/*"]');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.maskPatterns).toEqual(["/user/*/profile", "/post/*"]);
  });

  it("should handle invalid JSON in patterns gracefully", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-skip-patterns", "invalid-json");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.skipPatterns).toEqual([]);
  });

  it("should read the site ID from the script URL", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js?siteId=789");

    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.siteId).toBe("789");
    expect(config?.analyticsHost).toBe("https://analytics.example.com");
  });

  it("should prefer the URL site ID over the data attribute", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js?siteId=from-url");
    mockScriptTag.setAttribute("data-site-id", "from-attr");

    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.siteId).toBe("from-url");
  });

  it("should read the site ID from a relative proxied script URL", async () => {
    mockScriptTag.setAttribute("src", "/analytics/script.js?siteId=proxied");

    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.siteId).toBe("proxied");
    expect(config?.analyticsHost).toBe("/analytics");
  });

  it("should support legacy site-id attribute", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("site-id", "456");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.siteId).toBe("456");
  });

  it("should parse session replay batch settings", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-batch-size", "500");
    mockScriptTag.setAttribute("data-replay-batch-interval", "10000");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayBatchSize).toBe(500);
    expect(config?.sessionReplayBatchInterval).toBe(10000);
  });

  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]"]'
    );

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayMaskTextSelectors).toEqual([".user-name", ".email-address", "[data-sensitive]"]);
  });

  it("should parse rrweb block and ignore options", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-block-class", "my-block-class");
    mockScriptTag.setAttribute("data-replay-block-selector", ".sensitive-content");
    mockScriptTag.setAttribute("data-replay-ignore-class", "my-ignore-class");
    mockScriptTag.setAttribute("data-replay-ignore-selector", "input[type='password']");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayBlockClass).toBe("my-block-class");
    expect(config?.sessionReplayBlockSelector).toBe(".sensitive-content");
    expect(config?.sessionReplayIgnoreClass).toBe("my-ignore-class");
    expect(config?.sessionReplayIgnoreSelector).toBe("input[type='password']");
  });

  it("should parse rrweb mask text class option", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-mask-text-class", "custom-mask-class");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayMaskTextClass).toBe("custom-mask-class");
  });

  it("should parse rrweb maskAllInputs option", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-mask-all-inputs", "true");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayMaskAllInputs).toBe(true);
  });

  it("should parse rrweb maskAllInputs as false", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-mask-all-inputs", "false");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayMaskAllInputs).toBe(false);
  });

  it("should parse rrweb maskInputOptions", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-mask-input-options", '{"password":true,"email":true,"tel":true}');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayMaskInputOptions).toEqual({ password: true, email: true, tel: true });
  });

  it("should parse rrweb collectFonts option", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-collect-fonts", "false");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplayCollectFonts).toBe(false);
  });

  it("should parse rrweb sampling options", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-sampling", '{"mousemove":100,"scroll":200}');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplaySampling).toEqual({ mousemove: 100, scroll: 200 });
  });

  it("should parse rrweb slimDOMOptions as object", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-slim-dom-options", '{"script":true,"comment":true}');

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplaySlimDOMOptions).toEqual({ script: true, comment: true });
  });

  it("should parse rrweb slimDOMOptions as boolean", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-slim-dom-options", "true");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    expect(config?.sessionReplaySlimDOMOptions).toBe(true);
  });

  it("should handle invalid JSON in rrweb options gracefully", async () => {
    mockScriptTag.setAttribute("src", "https://analytics.example.com/script.js");
    mockScriptTag.setAttribute("data-site-id", "123");
    mockScriptTag.setAttribute("data-replay-sampling", "invalid-json");

    // Mock successful API response
    (global.fetch as any).mockResolvedValueOnce({
      ok: true,
      json: async () => ({}),
    });

    const config = await parseScriptConfig(mockScriptTag);
    // Should fallback to empty object for invalid JSON
    expect(config?.sessionReplaySampling).toEqual({});
  });
});