import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { Pcre2TracePanel } from "./Pcre2TracePanel";

function renderPanel(flavour: "ecmascript" | "pcre2") {
  return render(
    <Pcre2TracePanel
      active
      flavour={flavour}
      pattern="(a+)+$"
      flags={["g"]}
      options={
        flavour === "pcre2"
          ? {
              matchLimit: 1_000_000,
              depthLimit: 1_000,
              heapLimitKib: 32_768,
            }
          : {}
      }
      subject="aaaa!"
      timeoutMs={2_000}
      onClose={vi.fn()}
      onSelectRanges={vi.fn()}
    />,
  );
}

describe("Pcre2TracePanel", () => {
  it("discloses reported versus derived trace semantics and hard caps", () => {
    renderPanel("pcre2");

    expect(
      screen.getByRole("heading", { name: "Execution trace" }),
    ).toBeInTheDocument();
    expect(
      screen.getByText(/actual stream reported by PCRE2_AUTO_CALLOUT/u),
    ).toBeInTheDocument();
    expect(
      screen.getByText(/Movement labels are derived only/u),
    ).toBeInTheDocument();
    expect(
      screen.getByRole("spinbutton", { name: "Maximum events" }),
    ).toHaveAttribute("max", "50000");
    expect(
      screen.getByRole("spinbutton", {
        name: "Maximum serialized bytes",
      }),
    ).toHaveAttribute("max", String(10 * 1024 * 1024));
    expect(screen.getByRole("button", { name: "Run trace" })).toBeEnabled();
  });

  it("does not offer an invented ECMAScript trace", () => {
    renderPanel("ecmascript");

    expect(screen.getByRole("button", { name: "Run trace" })).toBeDisabled();
    expect(screen.getByText(/Select PCRE2 to enable/u)).toBeInTheDocument();
  });
});
