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

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

import * as SentryNode from "@sentry/node";
import { initSentryServerNode } from "@/instrumentation/sentry/init-server-node";

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

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

  it("attaches beforeSend + beforeSendTransaction scrubbers", () => {
    initSentryServerNode({ dsn: "https://x@y/1", app: "web-tanstack" });
    const call = (SentryNode.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("tags events with the app name", () => {
    initSentryServerNode({ dsn: "https://x@y/1", app: "web-tanstack" });
    const call = (SentryNode.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-tanstack");
  });

  it("is a no-op when dsn is missing", () => {
    initSentryServerNode({ dsn: "", app: "web-tanstack" });
    expect(SentryNode.init).not.toHaveBeenCalled();
    initSentryServerNode({ dsn: undefined as unknown as string, app: "web-tanstack" });
    expect(SentryNode.init).not.toHaveBeenCalled();
  });
});
