// packages/core-shared/src/instrumentation/sentry/init-client-react.test.ts
import { describe, it, expect, vi, beforeEach } from "vitest";

const { replayIntegration, feedbackIntegration } = vi.hoisted(() => {
  const replayIntegration = vi.fn((opts: unknown) => ({
    name: "Replay",
    _opts: opts,
  }));
  const feedbackIntegration = vi.fn((opts: unknown) => ({
    name: "Feedback",
    _opts: opts,
  }));
  return { replayIntegration, feedbackIntegration };
});

vi.mock("@sentry/react", () => ({
  init: vi.fn(),
  replayIntegration,
  feedbackIntegration,
}));

import * as SentryReact from "@sentry/react";
import { initSentryClientReact } from "@/instrumentation/sentry/init-client-react";

describe("initSentryClientReact", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("calls SentryReact.init with sendDefaultPii: false", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    const call = (SentryReact.init as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(call["sendDefaultPii"]).toBe(false);
  });

  it("attaches replay integration with mask flags", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    expect(replayIntegration).toHaveBeenCalledTimes(1);
    const replayOpts = (replayIntegration as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(replayOpts["maskAllText"]).toBe(true);
    expect(replayOpts["maskAllInputs"]).toBe(true);
    expect(replayOpts["blockAllMedia"]).toBe(true);
  });

  it("defaults replay sample rates", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    const call = (SentryReact.init as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(call["replaysSessionSampleRate"]).toBe(0.0);
    expect(call["replaysOnErrorSampleRate"]).toBe(1.0);
  });

  it("attaches beforeSend + beforeSendTransaction scrubbers", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    const call = (SentryReact.init as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(typeof call["beforeSend"]).toBe("function");
    expect(typeof call["beforeSendTransaction"]).toBe("function");
  });

  it("is a no-op when dsn is missing", () => {
    initSentryClientReact({ dsn: "", app: "web-tanstack" });
    expect(SentryReact.init).not.toHaveBeenCalled();
  });

  it("attaches feedbackIntegration when SentryReact.feedbackIntegration is available", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    expect(feedbackIntegration).toHaveBeenCalledTimes(1);
  });

  it("passes styleNonce and scriptNonce to feedbackIntegration when nonce provided", () => {
    initSentryClientReact({
      dsn: "https://x@y/1",
      app: "web-tanstack",
      nonce: "abc123",
    });
    const feedbackOpts = (feedbackIntegration as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(feedbackOpts["styleNonce"]).toBe("abc123");
    expect(feedbackOpts["scriptNonce"]).toBe("abc123");
  });

  it("omits nonce props from feedbackIntegration when nonce not provided", () => {
    initSentryClientReact({ dsn: "https://x@y/1", app: "web-tanstack" });
    const feedbackOpts = (feedbackIntegration as ReturnType<typeof vi.fn>).mock
      .calls[0]![0] as Record<string, unknown>;
    expect(feedbackOpts["styleNonce"]).toBeUndefined();
    expect(feedbackOpts["scriptNonce"]).toBeUndefined();
  });
});
