import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { ErrorBoundary } from "../ErrorBoundary";
import * as telemetry from "../../utils/telemetry";

// Mock telemetry to verify it's called
vi.mock("../../utils/telemetry");

function ExplodingChild(): React.ReactElement {
  throw new Error("boom");
}

describe("ErrorBoundary", () => {
  it("renders the fallback UI when a child throws", () => {
    render(
      <ErrorBoundary>
        <ExplodingChild />
      </ErrorBoundary>,
    );

    expect(screen.getByTestId("page-error-boundary")).toBeInTheDocument();
    expect(screen.getByText("Something went wrong")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: /reload/i })).toBeInTheDocument();
  });

  it("renders children normally when no error occurs", () => {
    render(
      <ErrorBoundary>
        <div data-testid="safe-child">safe</div>
      </ErrorBoundary>,
    );

    expect(screen.getByTestId("safe-child")).toBeInTheDocument();
    expect(screen.queryByTestId("page-error-boundary")).not.toBeInTheDocument();
  });
});
