import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ addIntegration: vi.fn(), getReplay: vi.fn(), isInitialized: vi.fn(), replayIntegration: vi.fn((options: Record) => ({ name: "Replay", options })), })); vi.mock("@sentry/react-router", () => mocks); import { disableSentryReplay, enableSentryReplay } from "./sentry-replay.client"; describe("consent-gated Sentry Replay", () => { beforeEach(() => { vi.clearAllMocks(); mocks.isInitialized.mockReturnValue(true); mocks.getReplay.mockReturnValue(undefined); }); it("does not install Replay before Sentry is ready or when sampling is disabled", () => { mocks.isInitialized.mockReturnValue(false); expect(enableSentryReplay({ replaysSessionSampleRate: 1, replaysOnErrorSampleRate: 1 })).toBe(false); mocks.isInitialized.mockReturnValue(true); expect(enableSentryReplay({ replaysSessionSampleRate: 0, replaysOnErrorSampleRate: 0 })).toBe(false); expect(mocks.addIntegration).not.toHaveBeenCalled(); }); it("adds a fully masked Replay integration only after the consent caller enables it", () => { expect(enableSentryReplay({ replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1 })).toBe(true); expect(mocks.replayIntegration).toHaveBeenCalledWith({ blockAllMedia: true, maskAllInputs: true, maskAllText: true }); expect(mocks.addIntegration).toHaveBeenCalledWith(expect.objectContaining({ name: "Replay" })); }); it("restarts and stops an existing Replay instance without duplicate integration", async () => { const replay = { start: vi.fn(), stop: vi.fn(async () => undefined) }; mocks.getReplay.mockReturnValue(replay); expect(enableSentryReplay({ replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1 })).toBe(true); expect(replay.start).toHaveBeenCalledTimes(1); expect(mocks.addIntegration).not.toHaveBeenCalled(); await expect(disableSentryReplay()).resolves.toBe(true); expect(replay.stop).toHaveBeenCalledTimes(1); }); });