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]

4.2 KB
Raw Blame
import { screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { ReactNode } from "react";
import { describe, expect, it, vi } from "vitest";

import { render } from "../../../../tests/test-utils";
import type { LogEntry } from "../types";
import { DrawerHeader } from "./DrawerHeader";

const logEntry = (overrides: Partial<LogEntry>): LogEntry =>
  ({
    request_id: "170d64ea-69f0-431a-be72-332f8f78c18a",
    api_key: "key-1",
    team_id: "team-1",
    model: "gpt-4o",
    model_id: "model-1",
    custom_llm_provider: "openai",
    call_type: "acompletion",
    spend: 0.01,
    total_tokens: 10,
    prompt_tokens: 5,
    completion_tokens: 5,
    startTime: "2026-07-07T09:50:13Z",
    endTime: "2026-07-07T09:50:14Z",
    cache_hit: "false",
    messages: [],
    response: {},
    ...overrides,
  }) as LogEntry;

const renderHeader = (log: LogEntry, isSidebarCollapsed: boolean) =>
  render(
    <DrawerHeader
      log={log}
      onClose={vi.fn()}
      onPrevious={vi.fn()}
      onNext={vi.fn()}
      isSidebarCollapsed={isSidebarCollapsed}
      onToggleSidebar={vi.fn()}
      statusLabel="Failure"
      statusColor="error"
      environment="default"
    />,
  );

const expandToggle = () => screen.getByLabelText("Expand trace sidebar");

describe("DrawerHeader sidebar toggle", () => {
  it("stays out of the header while the sidebar owns it", () => {
    renderHeader(logEntry({}), false);

    expect(screen.queryByLabelText("Expand trace sidebar")).not.toBeInTheDocument();
    expect(screen.queryByLabelText("Collapse trace sidebar")).not.toBeInTheDocument();
  });

  it("shares the model row once the sidebar is collapsed", () => {
    renderHeader(logEntry({}), true);

    const row = expandToggle().parentElement as HTMLElement;
    expect(within(row).getByText("gpt-4o")).toBeInTheDocument();
  });

  it("shows the x-litellm-call-id with its own copy button when it differs from the request id", () => {
    renderHeader(logEntry({ request_id: "chatcmpl-h", litellm_call_id: "call-h" }), false);

    expect(screen.getByText("chatcmpl-h")).toBeInTheDocument();
    expect(screen.getByText("call-h")).toBeInTheDocument();
    expect(screen.getByText("x-litellm-call-id:")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Copy Request ID" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Copy x-litellm-call-id" })).toBeInTheDocument();
  });

  it("omits the x-litellm-call-id line and button when the ids match", () => {
    renderHeader(logEntry({ request_id: "same-h", litellm_call_id: "same-h" }), false);

    expect(screen.queryByText("x-litellm-call-id:")).not.toBeInTheDocument();
    expect(screen.queryByRole("button", { name: "Copy x-litellm-call-id" })).not.toBeInTheDocument();
  });

  it("falls back to the request id row when the log names no model", () => {
    renderHeader(logEntry({ model: "", custom_llm_provider: "" }), true);

    const row = expandToggle().parentElement as HTMLElement;
    expect(within(row).getByText("170d64ea-69f0-431a-be72-332f8f78c18a")).toBeInTheDocument();
  });
});

describe("DrawerHeader back link", () => {
  const renderWithBack = (backTo?: { label: string; icon: ReactNode }) => {
    const onClose = vi.fn();
    render(
      <DrawerHeader
        log={logEntry({})}
        onClose={onClose}
        onPrevious={vi.fn()}
        onNext={vi.fn()}
        isSidebarCollapsed={false}
        onToggleSidebar={vi.fn()}
        statusLabel="Success"
        statusColor="success"
        environment="default"
        backTo={backTo}
      />,
    );
    return onClose;
  };

  it("names where the drawer was opened from and closes back to it", async () => {
    const onClose = renderWithBack({ label: "Lens trace", icon: <svg data-testid="lens-icon" /> });
    const back = screen.getByRole("button", { name: "Back to Lens trace" });
    expect(within(back).getByTestId("lens-icon")).toBeInTheDocument();
    await userEvent.click(back);
    expect(onClose).toHaveBeenCalledOnce();
  });

  it("shows no back link when opened from the logs page", () => {
    renderWithBack();
    expect(screen.queryByRole("button", { name: /^Back to/ })).not.toBeInTheDocument();
  });
});