Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

39.2 KB
Raw Blame
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { fireEvent, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";

import { useInfiniteKeys } from "@/app/(dashboard)/hooks/keys/useKeys";
import { ApiError } from "@/lib/http/client";

vi.mock("./useShadowEval", () => ({
  useShadowEvalJobs: vi.fn(),
  useShadowEvalJob: vi.fn(),
  useStartShadowEval: vi.fn(),
  useStopShadowEval: vi.fn(),
}));

const authorizedRoleMock = vi.fn(() => ({ accessToken: "token", isViewOnly: false }));
vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
  default: () => ({ userId: "test-user-id", userRole: "Admin", ...authorizedRoleMock() }),
}));

vi.mock("@/app/(dashboard)/hooks/keys/useKeys", () => ({
  useInfiniteKeys: vi.fn(() => ({
    data: {
      pages: [
        {
          keys: [
            { token: "hash-alpha", token_id: "id-1", key_name: "sk-...alpha", key_alias: "prod-alpha" },
            { token: "hash-beta", token_id: "id-2", key_name: "sk-...beta", key_alias: "staging-beta" },
          ],
          total_count: 2,
          current_page: 1,
          total_pages: 1,
        },
      ],
    },
    isPending: false,
    isError: false,
    fetchNextPage: vi.fn(),
    hasNextPage: false,
    isFetchingNextPage: false,
  })),
}));

vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({
  useInfiniteTeams: vi.fn(() => ({
    data: { pages: [{ teams: [{ team_id: "team-eng", team_alias: "engineering" }], page: 1, total_pages: 1 }] },
    isLoading: false,
    fetchNextPage: vi.fn(),
    hasNextPage: false,
    isFetchingNextPage: false,
  })),
}));

vi.mock("@/app/(dashboard)/hooks/users/useUsers", () => ({
  useInfiniteUsers: vi.fn(() => ({
    data: {
      pages: [
        {
          users: [{ user_id: "dev-alice", user_alias: null, user_email: "[email protected]" }],
          page: 1,
          total_pages: 1,
        },
      ],
    },
    isPending: false,
    isError: false,
    fetchNextPage: vi.fn(),
    hasNextPage: false,
    isFetchingNextPage: false,
  })),
}));

vi.mock("@/app/(dashboard)/hooks/models/useModels", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/app/(dashboard)/hooks/models/useModels")>()),
  useAutoRouters: vi.fn(() => ({
    data: [
      { model_name: "claude-auto", litellm_params: { model: "auto_router/claude-auto" } },
      { model_name: "gpt-auto", litellm_params: { model: "auto_router/gpt-auto" } },
    ],
  })),
  usePlainModelGroups: vi.fn(() => new Set(["prod-claude", "prod-judge"])),
  usePlainChatModelGroups: vi.fn(() => new Set(["prod-claude", "prod-judge"])),
  usePlainChatModelDeployments: vi.fn(() => [
    {
      model_name: "prod-judge",
      litellm_params: { model: "anthropic/claude-sonnet-5" },
      model_info: { mode: "chat" },
    },
  ]),
}));

vi.mock("@/components/networking", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/components/networking")>()),
  modelInfoCall: vi.fn(),
}));

import { usePlainChatModelGroups, usePlainModelGroups } from "@/app/(dashboard)/hooks/models/useModels";
import { modelInfoCall } from "@/components/networking";

import ShadowEvalSection, { shadowedTargetLabel } from "./ShadowEvalSection";
import {
  useShadowEvalJob,
  useShadowEvalJobs,
  useStartShadowEval,
  useStopShadowEval,
  type ShadowEvalJob,
} from "./useShadowEval";
import { chooseSelectOption } from "../../../../../tests/test-utils";

const job = (overrides: Partial<ShadowEvalJob> = {}): ShadowEvalJob => ({
  job_id: "job-1",
  status: "running",
  router_name: "claude-auto",
  router_names: ["claude-auto"],
  models: [],
  direction: "forward",
  baseline_model: null,
  judge_model: "prod-judge",
  shadow_percentage: 10,
  targets: [
    {
      target_type: "key",
      target_id: "hashed-key-abc",
      max_turns: 10000,
      max_budget: 10,
      spend: 3.21,
      stopped_at: null,
      target_alias: "prod-alpha",
      key_name: "sk-...alpha",
    },
  ],
  judged_count: 42,
  error_count: 1,
  judge_spend: 3.21,
  results: {
    by_tier: [
      {
        group: "SIMPLE",
        turn_count: 30,
        real_win_rate_pct: 20.0,
        shadow_win_rate_pct: 55.0,
        tie_rate_pct: 25.0,
        avg_judge_confidence: 0.81,
        real_spend: 0.4,
        shadow_spend: 0.1,
        cache_hit_turns: 2,
      },
      {
        group: "REASONING",
        turn_count: 12,
        real_win_rate_pct: 50.0,
        shadow_win_rate_pct: 33.3,
        tie_rate_pct: 16.7,
        avg_judge_confidence: 0.74,
        real_spend: 0.2,
        shadow_spend: 0.2,
        cache_hit_turns: 0,
      },
    ],
    by_current_model: [
      {
        group: "gpt-4o",
        turn_count: 42,
        real_win_rate_pct: 30.0,
        shadow_win_rate_pct: 45.0,
        tie_rate_pct: 25.0,
        avg_judge_confidence: 0.8,
        real_spend: 0.6,
        shadow_spend: 0.3,
        cache_hit_turns: 2,
      },
    ],
    overall_shadow_win_rate_pct: 48.0,
    overall_tie_rate_pct: 22.0,
    sampled_real_spend: 0.6,
    sampled_shadow_spend: 0.3,
    not_sampled_count: 378,
    unjudgeable_count: 10,
    shed_count: 2,
  },
  created_at: "2026-08-07T00:00:00Z",
  ends_at: "2026-09-07T00:00:00Z",
  last_error: null,
  ...overrides,
});

const targetEntry = (
  target_id: string,
  overrides: Partial<ShadowEvalJob["targets"][number]> = {},
): ShadowEvalJob["targets"][number] => ({
  target_type: "key",
  target_id,
  max_turns: 10000,
  max_budget: 10,
  spend: 0,
  stopped_at: null,
  attempt_count: null,
  target_alias: null,
  key_name: null,
  ...overrides,
});

const mockHooks = ({
  jobs = [],
  detailsById = {},
  error = null,
  detailError = false,
  isPending = false,
}: {
  jobs?: ShadowEvalJob[];
  detailsById?: Record<string, ShadowEvalJob>;
  error?: Error | null;
  detailError?: boolean;
  isPending?: boolean;
}) => {
  vi.mocked(useShadowEvalJobs).mockReturnValue({
    data: error || isPending ? undefined : jobs,
    error,
    isPending,
  } as unknown as ReturnType<typeof useShadowEvalJobs>);
  vi.mocked(useShadowEvalJob).mockImplementation(
    (jobId) =>
      ({
        data: jobId ? detailsById[jobId] : undefined,
        isError: detailError ?? false,
      }) as unknown as ReturnType<typeof useShadowEvalJob>,
  );
  const start = { mutate: vi.fn(), isPending: false };
  const stop = { mutate: vi.fn(), isPending: false };
  vi.mocked(useStartShadowEval).mockReturnValue(start as unknown as ReturnType<typeof useStartShadowEval>);
  vi.mocked(useStopShadowEval).mockReturnValue(stop as unknown as ReturnType<typeof useStopShadowEval>);
  return { start, stop };
};

describe("ShadowEvalSection", () => {
  beforeEach(() => {
    authorizedRoleMock.mockReturnValue({ accessToken: "token", isViewOnly: false });
  });

  it("shows a key picker load failure instead of posing as no matching keys", async () => {
    const user = userEvent.setup();
    const defaultKeysImpl = vi.mocked(useInfiniteKeys).getMockImplementation();
    vi.mocked(useInfiniteKeys).mockReturnValue({
      data: undefined,
      isPending: false,
      isError: true,
      fetchNextPage: vi.fn(),
      hasNextPage: false,
      isFetchingNextPage: false,
    } as unknown as ReturnType<typeof useInfiniteKeys>);
    mockHooks({});
    render(<ShadowEvalSection />);

    await user.click(screen.getByPlaceholderText("Search keys by alias"));
    expect(await screen.findByText("Keys could not be loaded. Refresh the page to retry.")).toBeInTheDocument();
    expect(screen.queryByText("No matching keys")).not.toBeInTheDocument();
    if (defaultKeysImpl) vi.mocked(useInfiniteKeys).mockImplementation(defaultKeysImpl);
  });

  it("labels only configured judge recommendations", async () => {
    const user = userEvent.setup();
    mockHooks({});
    render(<ShadowEvalSection />);

    await user.click(screen.getByPlaceholderText("Select a judge model"));
    expect(screen.getByRole("option", { name: /prod-judge.*Recommended/ })).toBeInTheDocument();
    expect(screen.queryByRole("option", { name: /openai\/gpt-4o/ })).not.toBeInTheDocument();

    await user.keyboard("{Escape}");
    await chooseSelectOption(
      user,
      screen.getByText("Adoption check: key's traffic vs the router"),
      "Regression check: router's picks vs a baseline",
    );
    await user.click(screen.getByPlaceholderText("Select a baseline model"));
    expect(screen.getByRole("option", { name: "prod-judge", exact: true })).toBeInTheDocument();
    expect(screen.queryByText("Recommended")).not.toBeInTheDocument();
  });

  it("keeps custom models selectable through the real model hooks without widening chat choices to traffic filters", async () => {
    const hooks = await vi.importActual<typeof import("@/app/(dashboard)/hooks/models/useModels")>(
      "@/app/(dashboard)/hooks/models/useModels",
    );
    const client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
    const deployments = [
      { model_name: "custom-chat", litellm_params: { model: "openai/private-chat" } },
      { model_name: "custom-judge", litellm_params: { model: "openai/private-judge" }, model_info: { mode: null } },
      {
        model_name: "embedding",
        litellm_params: { model: "openai/private-embedding" },
        model_info: { mode: "embedding" },
      },
      {
        model_name: "responses-only",
        litellm_params: { model: "openai/private-responses" },
        model_info: { mode: "responses" },
      },
      { model_name: "auto-router", litellm_params: { model: "auto_router/complexity_router" } },
    ];
    vi.mocked(modelInfoCall).mockResolvedValue({ data: deployments, total_pages: 1 });
    const user = userEvent.setup();
    const { start } = mockHooks({});
    await vi.mocked(usePlainModelGroups).withImplementation(hooks.usePlainModelGroups, async () => {
      await vi.mocked(usePlainChatModelGroups).withImplementation(hooks.usePlainChatModelGroups, async () => {
        render(
          <QueryClientProvider client={client}>
            <ShadowEvalSection />
          </QueryClientProvider>,
        );
        await chooseSelectOption(user, screen.getByPlaceholderText("Every model the targets use"), "responses-only");
        await chooseSelectOption(user, screen.getByPlaceholderText("Every model the targets use"), "custom-chat");
        await chooseSelectOption(
          user,
          screen.getByText("Adoption check: key's traffic vs the router"),
          "Regression check: router's picks vs a baseline",
        );
        await user.click(screen.getByPlaceholderText("Search keys by alias"));
        await user.click(within(await screen.findByTestId("paginated-multi-select-list")).getByText("prod-alpha"));
        await chooseSelectOption(user, screen.getByPlaceholderText("Select up to 4 auto-routers"), "gpt-auto");
        await user.click(screen.getByPlaceholderText("Select a judge model"));
        expect(screen.getAllByRole("option")).toHaveLength(2);
        expect(screen.getByRole("option", { name: "custom-chat", exact: true })).toBeInTheDocument();
        expect(screen.getByRole("option", { name: "custom-judge", exact: true })).toBeInTheDocument();
        await user.click(screen.getByRole("option", { name: "custom-judge", exact: true }));
        await user.click(screen.getByPlaceholderText("Select a baseline model"));
        expect(screen.getAllByRole("option")).toHaveLength(2);
        expect(screen.getByRole("option", { name: "custom-chat", exact: true })).toBeInTheDocument();
        expect(screen.getByRole("option", { name: "custom-judge", exact: true })).toBeInTheDocument();
        await user.click(screen.getByRole("option", { name: "custom-chat", exact: true }));
        await user.click(screen.getByText("Start shadow eval"));
        expect(start.mutate).toHaveBeenCalledWith(
          expect.objectContaining({ judge_model: "custom-judge", baseline_model: "custom-chat", models: [] }),
        );
      });
    });
    client.clear();
  });

  it("offers the start form while the list is still loading", () => {
    mockHooks({ isPending: true });
    render(<ShadowEvalSection />);
    expect(screen.getByText("Loading evaluations...")).toBeInTheDocument();
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
  });

  it("re-offers the start form when the polled detail sees the job finish before the list does", () => {
    mockHooks({
      jobs: [job({ status: "running" })],
      detailsById: { "job-1": job({ status: "completed" }) },
    });
    render(<ShadowEvalSection />);
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
  });

  it("gives every active job its own card with a stop button, with the form still offered", () => {
    mockHooks({
      jobs: [
        job({ job_id: "job-a", status: "running", targets: [targetEntry("key-a")] }),
        job({ job_id: "job-b", status: "running", targets: [targetEntry("key-b")] }),
      ],
    });
    render(<ShadowEvalSection />);
    expect(screen.getAllByRole("button", { name: "Stop" })).toHaveLength(2);
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
    expect(screen.queryByText(/Previous evaluations/)).not.toBeInTheDocument();
  });

  it("renders the active card from the list row while its detail is still loading", () => {
    mockHooks({ jobs: [job({ status: "running" })], detailsById: {} });
    render(<ShadowEvalSection />);
    expect(screen.getByRole("button", { name: "Stop" })).toBeInTheDocument();
  });

  it("hides the start form and stop button from view-only admins", () => {
    authorizedRoleMock.mockReturnValue({ accessToken: "token", isViewOnly: true });
    mockHooks({ jobs: [job({ status: "running" })] });
    render(<ShadowEvalSection />);
    expect(screen.queryByText("Start a shadow eval")).not.toBeInTheDocument();
    expect(screen.queryByRole("button", { name: "Stop" })).not.toBeInTheDocument();
    expect(screen.getByText("running")).toBeInTheDocument();
  });

  it("never labels a collapsed previous eval as empty from a countless list row", () => {
    const countlessListRow: Partial<ShadowEvalJob> = {
      job_id: "job-old",
      status: "stopped",
      judged_count: null,
      error_count: null,
      judge_spend: null,
      results: null,
    };
    mockHooks({ jobs: [job({ status: "running" }), job(countlessListRow)] });
    render(<ShadowEvalSection />);
    fireEvent.click(screen.getByRole("button", { name: /Previous evaluations/ }));
    expect(screen.getByText("view results")).toBeInTheDocument();
    expect(screen.queryByText("no verdicts")).not.toBeInTheDocument();
    expect(screen.queryByText(/0 judged/)).not.toBeInTheDocument();
  });

  it("surfaces a non-403 list failure instead of posing as an empty state", () => {
    mockHooks({ error: new Error("boom") });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/Existing evaluations could not be loaded/)).toBeInTheDocument();
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
  });

  it("shows a failure line instead of loading forever when the detail fetch errors", () => {
    mockHooks({
      jobs: [job({ status: "completed", judged_count: 12, results: null })],
      detailsById: {},
      detailError: true,
    });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/Results could not be loaded/)).toBeInTheDocument();
    expect(screen.queryByText("Loading results...")).not.toBeInTheDocument();
  });

  it("shows the failure line over the collecting copy when an active job's detail errors", () => {
    mockHooks({ jobs: [job({ status: "running", results: null })], detailsById: {}, detailError: true });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/Results could not be loaded/)).toBeInTheDocument();
    expect(screen.queryByText(/Collecting verdicts/)).not.toBeInTheDocument();
  });

  it("never claims no verdicts for a judged job whose results have not loaded yet", () => {
    mockHooks({ jobs: [job({ status: "completed", judged_count: 12, results: null })], detailsById: {} });
    render(<ShadowEvalSection />);
    expect(screen.getByText("Loading results...")).toBeInTheDocument();
    expect(screen.queryByText(/No verdicts were recorded/)).not.toBeInTheDocument();
  });

  it("shows the start form when there are no jobs", () => {
    mockHooks({});
    render(<ShadowEvalSection />);
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
    expect(screen.getByText("Start shadow eval")).toBeInTheDocument();
  });

  it("renders the latest job's results with the headline stat, verdict split, and both stratifications", () => {
    const j = job();
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);

    expect(screen.getByText("Router matched or beat your current model")).toBeInTheDocument();
    expect(screen.getByText("70.0%")).toBeInTheDocument();
    expect(screen.getByText("of 42 judged responses")).toBeInTheDocument();
    expect(screen.getByText(/Tie 22.0%/)).toBeInTheDocument();
    expect(screen.getByText(/Current model won 30.0%/)).toBeInTheDocument();
    expect(screen.getByText("gpt-4o")).toBeInTheDocument();
    expect(screen.getByText("SIMPLE")).toBeInTheDocument();
    expect(screen.getByText("REASONING")).toBeInTheDocument();
    expect(screen.getByText("55.0%")).toBeInTheDocument();
  });

  it("shows the ends-in text while a job is still sampling", () => {
    const j = job({ ends_at: new Date(Date.now() + 3 * 86_400_000).toISOString() });
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/ends in 3 days/)).toBeInTheDocument();
  });

  it("shows recorded eval spend against the job's dollar budget", () => {
    const j = job();
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/\$3\.21 of \$10\.00 eval spend/)).toBeInTheDocument();
  });

  it("shows spend without a budget cap for a job from before spend budgets existed", () => {
    const j = job({ targets: [targetEntry("hashed-key-abc", { max_budget: null, spend: 3.21 })] });
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/\$3\.21 eval spend/)).toBeInTheDocument();
    expect(screen.queryByText(/of \$/)).not.toBeInTheDocument();
  });

  it("flags rows with fewer than 30 judged turns as low sample", () => {
    const j = job();
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getAllByText("(low sample)")).toHaveLength(1);
  });

  it("surfaces the last failure so a growing error_count is diagnosable", () => {
    const j = job({ error_count: 7, last_error: "judge call failed: LLM Provider NOT provided" });
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getByText(/LLM Provider NOT provided/)).toBeInTheDocument();
  });

  it("stops the running job from the stop button", async () => {
    const user = userEvent.setup();
    const j = job();
    const { stop } = mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);

    await user.click(screen.getByText("Stop"));

    expect(stop.mutate).toHaveBeenCalledWith("job-1");
  });

  it("hides the stop button and offers the start form once the latest job completed", () => {
    const done = job({ status: "completed" });
    mockHooks({ jobs: [done], detailsById: { "job-1": done } });
    render(<ShadowEvalSection />);
    expect(screen.queryByText("Stop")).not.toBeInTheDocument();
    expect(screen.getByText("Start a shadow eval")).toBeInTheDocument();
  });

  it("renders nothing for non-admins when the proxy answers 403", () => {
    mockHooks({ error: new ApiError("forbidden", 403, {}) });
    const { container } = render(<ShadowEvalSection />);
    expect(container).toBeEmptyDOMElement();
  });

  it("keeps the start button disabled until key, router, and judge model are picked, then submits every picked key", async () => {
    const user = userEvent.setup();
    const { start } = mockHooks({});
    render(<ShadowEvalSection />);

    expect(screen.getByText("Start shadow eval")).toBeDisabled();

    const keyInput = screen.getByPlaceholderText("Search keys by alias");
    await user.click(keyInput);
    const keyList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(keyList).getByText("prod-alpha"));
    await user.click(keyInput);
    await user.click(within(keyList).getByText("staging-beta"));
    await chooseSelectOption(user, screen.getByPlaceholderText("Select up to 4 auto-routers"), "gpt-auto");

    expect(screen.getByText("Start shadow eval")).toBeDisabled();

    await user.click(screen.getByPlaceholderText("Select a judge model"));
    expect(screen.queryByRole("option", { name: /openai\/gpt-4o/ })).not.toBeInTheDocument();
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));
    await user.click(screen.getByText("Start shadow eval"));

    const expectedBody = {
      api_key_ids: ["hash-alpha", "hash-beta"],
      team_ids: [],
      user_ids: [],
      models: [],
      router_names: ["gpt-auto"],
      direction: "forward",
      shadow_percentage: 10,
      duration_days: 7,
      max_budget: 10,
      judge_model: "prod-judge",
    };
    expect(start.mutate).toHaveBeenCalledWith(expectedBody);
  });

  it("submits a team-only job with team_ids and no keys", async () => {
    const user = userEvent.setup();
    const { start } = mockHooks({});
    render(<ShadowEvalSection />);

    expect(screen.getByText("Start shadow eval")).toBeDisabled();

    await user.click(screen.getByPlaceholderText("Search teams by alias"));
    const teamList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(teamList).getByText("engineering"));
    await chooseSelectOption(user, screen.getByPlaceholderText("Select up to 4 auto-routers"), "gpt-auto");
    await user.click(screen.getByPlaceholderText("Select a judge model"));
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));
    await user.click(screen.getByText("Start shadow eval"));

    const expectedBody = {
      api_key_ids: [],
      team_ids: ["team-eng"],
      user_ids: [],
      models: [],
      router_names: ["gpt-auto"],
      direction: "forward",
      shadow_percentage: 10,
      duration_days: 7,
      max_budget: 10,
      judge_model: "prod-judge",
    };
    expect(start.mutate).toHaveBeenCalledWith(expectedBody);
  });

  it("narrows a job to the picked model groups and shows the scope on the job headline", async () => {
    const user = userEvent.setup();
    const { start } = mockHooks({});
    render(<ShadowEvalSection />);

    await user.click(screen.getByPlaceholderText("Search teams by alias"));
    const teamList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(teamList).getByText("engineering"));
    await chooseSelectOption(user, screen.getByPlaceholderText("Every model the targets use"), "prod-claude");
    await chooseSelectOption(user, screen.getByPlaceholderText("Select up to 4 auto-routers"), "gpt-auto");
    await user.click(screen.getByPlaceholderText("Select a judge model"));
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));
    await user.click(screen.getByText("Start shadow eval"));

    expect(start.mutate).toHaveBeenCalledWith(
      expect.objectContaining({ team_ids: ["team-eng"], models: ["prod-claude"] }),
    );

    const scoped = job({ models: ["prod-claude", "prod-haiku"] });
    mockHooks({ jobs: [scoped], detailsById: { "job-1": scoped } });
    render(<ShadowEvalSection />);
    expect(screen.getByText("prod-claude, prod-haiku")).toBeInTheDocument();
  });

  it("requires a baseline model in reverse mode and submits it, while forward mode never shows the picker", async () => {
    const user = userEvent.setup();
    const { start } = mockHooks({});
    render(<ShadowEvalSection />);

    expect(screen.queryByPlaceholderText("Select a baseline model")).not.toBeInTheDocument();
    expect(screen.getByPlaceholderText("Every model the targets use")).toBeInTheDocument();

    await chooseSelectOption(
      user,
      screen.getByText("Adoption check: key's traffic vs the router"),
      "Regression check: router's picks vs a baseline",
    );
    expect(screen.queryByPlaceholderText("Every model the targets use")).not.toBeInTheDocument();
    await user.click(screen.getByPlaceholderText("Search keys by alias"));
    const keyList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(keyList).getByText("prod-alpha"));
    await chooseSelectOption(user, screen.getByPlaceholderText("Select up to 4 auto-routers"), "gpt-auto");
    await user.click(screen.getByPlaceholderText("Select a judge model"));
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));

    expect(screen.getByText("Start shadow eval")).toBeDisabled();

    await user.click(screen.getByPlaceholderText("Select a baseline model"));
    expect(screen.queryByRole("option", { name: /openai\/gpt-4o/ })).not.toBeInTheDocument();
    await user.click(screen.getByRole("option", { name: /prod-claude/ }));
    await user.click(screen.getByText("Start shadow eval"));

    const expectedBody = {
      api_key_ids: ["hash-alpha"],
      team_ids: [],
      user_ids: [],
      models: [],
      router_names: ["gpt-auto"],
      direction: "reverse",
      baseline_model: "prod-claude",
      shadow_percentage: 10,
      duration_days: 7,
      max_budget: 10,
      judge_model: "prod-judge",
    };
    expect(start.mutate).toHaveBeenCalledWith(expectedBody);
  });

  it("submits every picked auto-router so one job compares them on the same traffic", async () => {
    const user = userEvent.setup();
    const { start } = mockHooks({});
    render(<ShadowEvalSection />);

    await user.click(screen.getByPlaceholderText("Search keys by alias"));
    const keyList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(keyList).getByText("prod-alpha"));
    const routerInput = screen.getByPlaceholderText("Select up to 4 auto-routers");
    await user.click(routerInput);
    await user.click(await screen.findByText("gpt-auto"));
    await user.click(routerInput);
    await user.click(await screen.findByText("claude-auto"));
    expect(
      screen.getByText("Every router sees the same sampled requests, judged against the same live responses"),
    ).toBeInTheDocument();
    await user.click(screen.getByPlaceholderText("Select a judge model"));
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));
    await user.click(screen.getByText("Start shadow eval"));

    const expectedBody = {
      api_key_ids: ["hash-alpha"],
      team_ids: [],
      user_ids: [],
      models: [],
      router_names: ["gpt-auto", "claude-auto"],
      direction: "forward",
      shadow_percentage: 10,
      duration_days: 7,
      max_budget: 10,
      judge_model: "prod-judge",
    };
    expect(start.mutate).toHaveBeenCalledWith(expectedBody);
  });

  it("blocks starting a reverse job with more than one router and says why", async () => {
    const user = userEvent.setup();
    mockHooks({});
    render(<ShadowEvalSection />);

    await user.click(screen.getByPlaceholderText("Search keys by alias"));
    const keyList = await screen.findByTestId("paginated-multi-select-list");
    await user.click(within(keyList).getByText("prod-alpha"));
    const routerInput = screen.getByPlaceholderText("Select up to 4 auto-routers");
    await user.click(routerInput);
    await user.click(await screen.findByText("gpt-auto"));
    await user.click(routerInput);
    await user.click(await screen.findByText("claude-auto"));
    await chooseSelectOption(
      user,
      screen.getByText("Adoption check: key's traffic vs the router"),
      "Regression check: router's picks vs a baseline",
    );
    await user.click(screen.getByPlaceholderText("Select a judge model"));
    await user.click(await screen.findByRole("option", { name: /prod-judge/ }));
    await user.click(screen.getByPlaceholderText("Select a baseline model"));
    await user.click(screen.getByRole("option", { name: /prod-claude/ }));

    expect(screen.getByText("A regression check compares one router to its baseline")).toBeInTheDocument();
    expect(screen.getByText("Start shadow eval")).toBeDisabled();
  });

  it("renders a per-router comparison table only when the job ran several routers", () => {
    const routerSlice = (group: string, wins: number) => ({
      group,
      turn_count: 20,
      real_win_rate_pct: 100 - wins - 10,
      shadow_win_rate_pct: wins,
      tie_rate_pct: 10,
      avg_judge_confidence: 0.8,
      real_spend: 0.4,
      shadow_spend: 0.2,
      cache_hit_turns: 0,
    });
    const base = job();
    const multi = job({
      router_names: ["claude-auto", "gpt-auto"],
      results: { ...base.results!, by_router: [routerSlice("claude-auto", 40), routerSlice("gpt-auto", 70)] },
    });
    mockHooks({ jobs: [multi], detailsById: { "job-1": multi } });
    render(<ShadowEvalSection />);

    expect(screen.getByText("Router")).toBeInTheDocument();
    const rows = screen.getAllByRole("row").map((row) => row.textContent ?? "");
    expect(rows.some((text) => text.includes("claude-auto") && text.includes("40.0%"))).toBe(true);
    expect(rows.some((text) => text.includes("gpt-auto") && text.includes("70.0%"))).toBe(true);
    expect(
      screen.getByText(
        (_, element) =>
          element?.textContent === "Shadowing 10% of prod-alpha traffic via claude-auto, gpt-auto" &&
          element.tagName === "P",
      ),
    ).toBeInTheDocument();
  });

  it("renders a job from an older proxy that predates router_names", () => {
    const legacy = { ...job(), router_names: undefined } as unknown as ShadowEvalJob;
    mockHooks({ jobs: [legacy], detailsById: { "job-1": legacy } });
    render(<ShadowEvalSection />);

    expect(
      screen.getByText(
        (_, element) =>
          element?.textContent === "Shadowing 10% of prod-alpha traffic via claude-auto" && element.tagName === "P",
      ),
    ).toBeInTheDocument();
  });

  it("keeps the per-router table hidden for a single-router job", () => {
    const base = job();
    const single = job({ results: { ...base.results!, by_router: [] } });
    mockHooks({ jobs: [single], detailsById: { "job-1": single } });
    render(<ShadowEvalSection />);

    expect(screen.queryByText("Router")).not.toBeInTheDocument();
  });

  it("flips the arm labels and headline for a reverse job's results", () => {
    const j = job({ direction: "reverse", baseline_model: "openai/gpt-4o" });
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);

    expect(
      screen.getByText(
        (_, element) => element?.textContent === "Comparing claude-auto to openai/gpt-4o on 10% of prod-alpha traffic",
      ),
    ).toBeInTheDocument();
    expect(screen.getByText("Router matched or beat the baseline")).toBeInTheDocument();
    expect(screen.getByText("52.0%")).toBeInTheDocument();
    expect(screen.getByText(/Router won 30.0%/)).toBeInTheDocument();
    expect(screen.getByText(/Baseline won 48.0%/)).toBeInTheDocument();
    expect(screen.getAllByText("Baseline wins")).toHaveLength(2);
    expect(screen.getByText("Router pick")).toBeInTheDocument();
    expect(screen.queryByText(/Current model/)).not.toBeInTheDocument();
    expect(screen.queryByText("Compared against")).not.toBeInTheDocument();
  });

  it("labels the shadowed key by alias, then masked name, then truncated hash", () => {
    expect(shadowedTargetLabel(job().targets[0])).toBe("prod-alpha");
    expect(shadowedTargetLabel(targetEntry("hashed-key-abc", { key_name: "sk-...alpha" }))).toBe("sk-...alpha");
    expect(shadowedTargetLabel(targetEntry("hashed-key-abc"))).toBe("hashed-key…");
    expect(shadowedTargetLabel(targetEntry("team-eng", { target_type: "team" }))).toBe("team-eng");
    expect(shadowedTargetLabel(targetEntry("team-eng", { target_type: "team", target_alias: "engineering" }))).toBe(
      "engineering",
    );
  });

  it("breaks results down per key, so one key exhausting its own budget is visible while a sibling runs on", () => {
    mockHooks({
      jobs: [
        job({
          judged_count: 205,
          targets: [
            targetEntry("hash-spent", {
              max_budget: 2,
              spend: 1.5,
              stopped_at: "2026-08-08T00:00:00Z",
              verdicts: {
                group: "hash-spent",
                turn_count: 200,
                real_win_rate_pct: 20.0,
                shadow_win_rate_pct: 60.0,
                tie_rate_pct: 20.0,
                avg_judge_confidence: 0.9,
                real_spend: 0.9,
                shadow_spend: 0.5,
                cache_hit_turns: 0,
              },
            }),
            targetEntry("hash-hungry", { max_budget: 5, spend: 0.2 }),
          ],
          results: {
            by_tier: [],
            by_current_model: [],
            overall_shadow_win_rate_pct: 60.0,
            overall_tie_rate_pct: 20.0,
            sampled_real_spend: 0.9,
            sampled_shadow_spend: 0.5,
          },
        }),
      ],
    });
    render(<ShadowEvalSection />);

    const spent = screen.getByText("hash-spent…").closest("tr");
    const hungry = screen.getByText("hash-hungr…").closest("tr");
    if (!spent || !hungry) throw new Error("expected a table row per scoped key");

    expect(within(spent).getByText("stopped")).toBeInTheDocument();
    expect(within(spent).getByText("$1.50 / $2.00")).toBeInTheDocument();
    expect(within(spent).getByText("60.0%")).toBeInTheDocument();

    expect(within(hungry).getByText("running")).toBeInTheDocument();
    expect(within(hungry).getByText("$0.2000 / $5.00")).toBeInTheDocument();
    expect(within(hungry).getByText("No verdicts yet")).toBeInTheDocument();

    expect(screen.getByText(/205 turns judged/)).toBeInTheDocument();
    expect(screen.getByText(/Shadowing 10% of/)).toBeInTheDocument();
    expect(screen.getByText("2 targets")).toBeInTheDocument();
  });

  it("reads a key that spent its budget as completed even before the sweep stamps it", () => {
    const legacyTurnBudgetLeg = { max_budget: null, spend: 0.5, max_turns: 500, attempt_count: 3 };
    mockHooks({
      jobs: [
        job({
          targets: [
            targetEntry("hash-spent", { max_budget: 2, spend: 2, attempt_count: 40 }),
            targetEntry("hash-hungry", legacyTurnBudgetLeg),
          ],
        }),
      ],
    });
    render(<ShadowEvalSection />);

    const spent = screen.getByText("hash-spent…").closest("tr");
    const hungry = screen.getByText("hash-hungr…").closest("tr");
    if (!spent || !hungry) throw new Error("expected a table row per scoped key");
    expect(within(spent).getByText("completed")).toBeInTheDocument();
    expect(within(spent).getByText("$2.00 / $2.00")).toBeInTheDocument();
    expect(within(hungry).getByText("running")).toBeInTheDocument();
    expect(within(hungry).getByText("3 / 500 turns")).toBeInTheDocument();
  });

  it("shows the per-key table while a multi-key job is still collecting, before any verdicts exist", () => {
    mockHooks({
      jobs: [
        job({
          judged_count: 0,
          results: null,
          targets: [
            targetEntry("hash-spent", { max_budget: 0.5, spend: 0.5, attempt_count: 2 }),
            targetEntry("hash-hungry", { max_budget: 5, spend: 0.01, attempt_count: 1 }),
          ],
        }),
      ],
    });
    render(<ShadowEvalSection />);

    const spent = screen.getByText("hash-spent…").closest("tr");
    if (!spent) throw new Error("expected a per-key row before verdicts exist");
    expect(within(spent).getByText("completed")).toBeInTheDocument();
    expect(within(spent).getByText("$0.5000 / $0.5000")).toBeInTheDocument();
    expect(screen.getByText("Budget used")).toBeInTheDocument();
    expect(screen.queryByText("Judged turns")).not.toBeInTheDocument();
    expect(screen.getByText(/Collecting verdicts/)).toBeInTheDocument();
  });

  it("reads every key as completed once the job's window closes, whatever its own stop state", () => {
    mockHooks({
      jobs: [
        job({
          status: "completed",
          targets: [
            targetEntry("hash-spent", { max_turns: 200, stopped_at: "2026-08-08T00:00:00Z" }),
            targetEntry("hash-hungry", { max_turns: 500 }),
          ],
        }),
      ],
    });
    render(<ShadowEvalSection />);

    const hungry = screen.getByText("hash-hungr…").closest("tr");
    if (!hungry) throw new Error("expected a table row per scoped key");
    expect(within(hungry).getByText("completed")).toBeInTheDocument();
    expect(within(hungry).queryByText("running")).not.toBeInTheDocument();
  });

  it("shows the measured cost comparison with savings and both arm totals", () => {
    const j = job({});
    mockHooks({ jobs: [j], detailsById: { "job-1": j } });
    render(<ShadowEvalSection />);
    expect(screen.getByText("Router cost vs your current model")).toBeInTheDocument();
    expect(screen.getByText("-50.0%")).toBeInTheDocument();
    expect(
      screen.getByText("$0.3000 vs $0.6000 on the same judged turns; 2 cache-served turns excluded"),
    ).toBeInTheDocument();
    expect(screen.getAllByText("Router cost").length).toBeGreaterThan(0);
  });

  it("hides the cost tile when either arm has no measured spend, so a pre-measurement job never reads as a free incumbent", () => {
    const legacy = job({});
    legacy.results = {
      ...legacy.results!,
      by_tier: legacy.results!.by_tier.map((s) => ({ ...s, real_spend: 0 })),
      sampled_real_spend: 0,
      sampled_shadow_spend: 0.3,
    };
    mockHooks({ jobs: [legacy], detailsById: { "job-1": legacy } });
    render(<ShadowEvalSection />);
    expect(screen.queryByText(/Router cost vs/)).not.toBeInTheDocument();
    expect(screen.getByText("Router matched or beat your current model")).toBeInTheDocument();
  });

  it("flips the cost comparison arms for a reverse job", () => {
    const reverse = job({ direction: "reverse", baseline_model: "gpt-4o-mini" });
    mockHooks({ jobs: [reverse], detailsById: { "job-1": reverse } });
    render(<ShadowEvalSection />);
    expect(screen.getByText("Router cost vs the baseline")).toBeInTheDocument();
    expect(screen.getByText(/\$0\.6000 vs \$0\.3000 on the same judged turns/)).toBeInTheDocument();
    expect(screen.getByText("+100.0%")).toBeInTheDocument();
  });

  it("keeps an older job's verdicts reachable through the previous evaluations list", async () => {
    const user = userEvent.setup();
    const emptyOverrides: Partial<ShadowEvalJob> = {
      job_id: "job-new",
      status: "running",
      judged_count: 0,
      error_count: 0,
      results: null,
    };
    const current = job(emptyOverrides);
    const older = job({ job_id: "job-old", status: "completed", results: null });
    mockHooks({ jobs: [current, older], detailsById: { "job-new": current, "job-old": job({ job_id: "job-old" }) } });
    render(<ShadowEvalSection />);

    expect(screen.queryByText("SIMPLE")).not.toBeInTheDocument();

    await user.click(screen.getByRole("button", { name: /Previous evaluations \(1\)/ }));
    expect(screen.getByText("view results")).toBeInTheDocument();
    await user.click(screen.getByRole("button", { name: /10% of prod-alpha traffic via claude-auto/ }));

    expect(await screen.findByText("SIMPLE")).toBeInTheDocument();
    expect(screen.getByText("REASONING")).toBeInTheDocument();
  });
});