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

vi.mock("@sentry/nextjs", () => ({
  init: vi.fn(),
  replayIntegration: vi.fn(() => ({ name: "Replay" })),
}));

import * as Sentry from "@sentry/nextjs";
import { initSentryServer } from "@/instrumentation/sentry/init-server";

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

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

  it("passes the configured DSN", () => {
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    const call = (Sentry.init as ReturnType<typeof vi.fn>).mock.calls[0]![0] as Record<
      string,
      unknown
    >;
    expect(call["dsn"]).toBe("https://x@y/1");
  });

  it("attaches beforeSend + beforeSendTransaction scrubbers", () => {
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    const call = (Sentry.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("uses SENTRY_TRACES_SAMPLE_RATE env when set", () => {
    vi.stubEnv("SENTRY_TRACES_SAMPLE_RATE", "0.25");
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    const call = (Sentry.init as ReturnType<typeof vi.fn>).mock.calls[0]![0] as Record<
      string,
      unknown
    >;
    expect(call["tracesSampleRate"]).toBe(0.25);
    vi.unstubAllEnvs();
  });

  it("defaults tracesSampleRate to 1.0 in dev, 0.1 in production", () => {
    // Save and clear rate env to test default logic
    const prevRate = process.env["SENTRY_TRACES_SAMPLE_RATE"];
    delete process.env["SENTRY_TRACES_SAMPLE_RATE"];

    vi.stubEnv("NODE_ENV", "development");
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    expect(
      ((Sentry.init as ReturnType<typeof vi.fn>).mock.calls[0]![0] as Record<string, unknown>)[
        "tracesSampleRate"
      ],
    ).toBe(1.0);

    (Sentry.init as ReturnType<typeof vi.fn>).mockClear();
    vi.stubEnv("NODE_ENV", "production");
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    expect(
      ((Sentry.init as ReturnType<typeof vi.fn>).mock.calls[0]![0] as Record<string, unknown>)[
        "tracesSampleRate"
      ],
    ).toBe(0.1);

    vi.unstubAllEnvs();
    if (prevRate !== undefined) process.env["SENTRY_TRACES_SAMPLE_RATE"] = prevRate;
  });

  it("tags events with the app name", () => {
    initSentryServer({ dsn: "https://x@y/1", app: "web-next" });
    const call = (Sentry.init as ReturnType<typeof vi.fn>).mock.calls[0]![0] as Record<
      string,
      unknown
    >;
    const initialScope = call["initialScope"] as { tags?: Record<string, string> };
    expect(initialScope?.tags?.["app"]).toBe("web-next");
  });

  it("is a no-op when dsn is empty/undefined", () => {
    initSentryServer({ dsn: "", app: "web-next" });
    expect(Sentry.init).not.toHaveBeenCalled();
    initSentryServer({ dsn: undefined as unknown as string, app: "web-next" });
    expect(Sentry.init).not.toHaveBeenCalled();
  });
});
