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]

104 KB
Raw Blame
import { fireEvent, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { chooseSelectOption, renderWithProviders, testQueryClient } from "../../../tests/test-utils";
import { KeyResponse } from "../key_team_helpers/key_list";
import { MODEL_MAX_BUDGET_PREMIUM_HINT } from "../key_team_helpers/ModelMaxBudgetEditor";
import {
  fetchMCPServers,
  getPassThroughEndpointsCall,
  getPoliciesList,
  getUiSettings,
  getPromptsList,
  listMCPTools,
  modelAvailableCall,
  vectorStoreListCall,
} from "../networking";
import { KeyEditView } from "./key_edit_view";

const can = vi.fn();
vi.mock("@/app/(dashboard)/hooks/useCan", () => ({
  default: (...args: unknown[]) => can(...args),
}));

vi.mock("../networking", async () => {
  const actual = await vi.importActual("../networking");
  return {
    ...actual,
    getUiSettings: vi.fn().mockResolvedValue({ values: { enable_projects_ui: false } }),
    getPromptsList: vi.fn().mockResolvedValue({
      prompts: [{ prompt_id: "prompt-1" }, { prompt_id: "prompt-2" }],
    }),
    modelAvailableCall: vi.fn().mockResolvedValue({
      data: [{ id: "gpt-4" }, { id: "gpt-3.5-turbo" }],
    }),
    tagListCall: vi.fn().mockResolvedValue({
      tag1: { name: "tag1", description: "Test tag 1" },
      tag2: { name: "tag2", description: "Test tag 2" },
    }),
    getGuardrailsList: vi.fn().mockResolvedValue({
      guardrails: [{ guardrail_name: "guardrail-1" }],
    }),
    getPoliciesList: vi.fn().mockResolvedValue({
      policies: [{ policy_name: "policy-1" }],
    }),
    getPassThroughEndpointsCall: vi.fn().mockResolvedValue({
      endpoints: [],
    }),
    vectorStoreListCall: vi.fn().mockResolvedValue({
      data: [],
    }),
    agentListCall: vi.fn().mockResolvedValue({
      data: [],
    }),
    fetchMCPServers: vi.fn().mockResolvedValue([]),
    fetchMCPAccessGroups: vi.fn().mockResolvedValue([]),
    listMCPTools: vi.fn().mockResolvedValue({
      tools: [],
      error: null,
      message: null,
      stack_trace: null,
    }),
    getAgentsList: vi.fn().mockResolvedValue({
      agents: [],
    }),
    getAgentAccessGroups: vi.fn().mockResolvedValue([]),
    getClaudeCodePluginsList: vi.fn().mockResolvedValue({ plugins: [], count: 0 }),
  };
});

vi.mock("../organisms/create_key_button", () => ({
  fetchTeamModels: vi.fn().mockResolvedValue(["team-model-1", "team-model-2"]),
}));

vi.mock("@/app/(dashboard)/hooks/budgets/useBudgetOptions", () => ({
  useBudgetOptions: () => ({
    data: [
      { budget_id: "svc-a-budget", max_budget: 0.5, created_at: "", updated_at: "" },
      { budget_id: "svc-b-budget", max_budget: 100, created_at: "", updated_at: "" },
    ],
  }),
}));

const routerSettingsMocks = vi.hoisted(() => ({
  receivedValue: undefined as { router_settings: Record<string, unknown> } | undefined,
  editedValue: null as Record<string, unknown> | null,
}));

vi.mock("../common_components/RouterSettingsAccordion", async () => {
  const { forwardRef, useImperativeHandle } = await import("react");
  return {
    default: forwardRef(({ value }: { value?: { router_settings: Record<string, unknown> } }, ref) => {
      routerSettingsMocks.receivedValue = value;
      useImperativeHandle(ref, () => ({
        getValue: () => ({ router_settings: routerSettingsMocks.editedValue ?? value?.router_settings ?? {} }),
      }));
      return <div data-testid="router-settings-accordion" />;
    }),
  };
});

vi.mock("@/app/(dashboard)/hooks/organizations/useOrganizations", () => ({
  useOrganizations: vi.fn().mockReturnValue({
    data: [
      {
        organization_id: "org-1",
        organization_alias: "Engineering",
        members: [{ user_id: "user-orbit", user_role: "org_admin" }],
      },
      { organization_id: "org-2", organization_alias: "Sales" },
    ],
    isLoading: false,
  }),
}));

vi.mock("@/app/(dashboard)/hooks/accessGroups/useAccessGroups", () => ({
  useAccessGroups: vi.fn().mockReturnValue({
    data: [
      { access_group_id: "ag-1", access_group_name: "Group 1" },
      { access_group_id: "ag-2", access_group_name: "Group 2" },
    ],
    isLoading: false,
    isError: false,
  }),
}));

vi.mock("../mcp_server_management/MCPServerSelector", () => ({
  default: ({
    value,
    onChange,
    teamId,
    userId,
  }: {
    value?: { servers?: string[]; accessGroups?: string[]; toolsets?: string[] };
    onChange?: (v: { servers: string[]; accessGroups: string[]; toolsets: string[] }) => void;
    teamId?: string | null;
    userId?: string | null;
  }) => (
    <button
      type="button"
      data-testid="mcp-server-selector"
      data-team-id={teamId ?? ""}
      data-user-id={userId ?? ""}
      onClick={() => onChange?.({ servers: ["mcp-1"], accessGroups: [], toolsets: value?.toolsets ?? [] })}
    >
      pick mcp server
    </button>
  ),
}));

vi.mock("../agent_management/AgentSelector", () => ({
  default: ({ onChange }: { onChange?: (v: { agents: string[]; accessGroups: string[] }) => void }) => (
    <button
      type="button"
      data-testid="agent-selector"
      onClick={() => onChange?.({ agents: ["agent-1"], accessGroups: [] })}
    >
      pick agent
    </button>
  ),
}));

vi.mock("../skills/SkillSelector", () => ({
  default: ({ onChange }: { onChange: (selected: string[]) => void }) => (
    <button type="button" data-testid="skill-selector" onClick={() => onChange(["private-skill"])}>
      pick skill
    </button>
  ),
}));

vi.mock("../common_components/AccessGroupSelector", () => ({
  default: ({ value = [], onChange }: { value?: string[]; onChange?: (v: string[]) => void }) => (
    <input
      data-testid="access-group-selector"
      value={Array.isArray(value) ? value.join(",") : ""}
      onChange={(e) => onChange?.(e.target.value ? e.target.value.split(",").map((s) => s.trim()) : [])}
    />
  ),
}));

const visibleOptions = (): HTMLElement[] => screen.queryAllByRole("option");

const isOptionDisabled = (option: HTMLElement): boolean => option.getAttribute("aria-disabled") === "true";

const optionByContent = (label: string): HTMLElement | undefined =>
  visibleOptions().find((el) => el.textContent === label);

describe("KeyEditView", () => {
  const MOCK_KEY_DATA: KeyResponse = {
    token: "test-token-123",
    token_id: "test-token-123",
    key_name: "sk-...TUuw",
    key_alias: "asdasdas",
    spend: 0,
    total_spend: 0,
    max_budget: 0,
    expires: "null",
    models: [],
    aliases: {},
    config: {},
    user_id: "default_user_id",
    team_id: null,
    project_id: null,
    key_type: null,
    last_active: null,
    max_parallel_requests: 10,
    metadata: {
      logging: [],
      tags: ["test-tag"],
    },
    tpm_limit: 10,
    rpm_limit: 10,
    tpd_limit: 250000,
    duration: "30d",
    budget_duration: "30d",
    budget_reset_at: "never",
    allowed_cache_controls: [],
    allowed_routes: [],
    permissions: {},
    model_spend: {},
    model_max_budget: {},
    soft_budget_cooldown: false,
    blocked: false,
    litellm_budget_table: {},
    organization_id: null,
    created_at: "2025-10-29T01:26:41.613000Z",
    updated_at: "2025-10-29T01:47:33.980000Z",
    team_spend: 100,
    team_alias: "",
    team_tpm_limit: 100,
    team_rpm_limit: 100,
    team_max_budget: 100,
    team_models: [],
    team_blocked: false,
    soft_budget: 200,
    team_model_aliases: {},
    team_member_spend: 0,
    team_metadata: {},
    end_user_id: "default_user_id",
    end_user_tpm_limit: 10,
    end_user_rpm_limit: 10,
    end_user_max_budget: 0,
    last_refreshed_at: Date.now(),
    api_key: "sk-...TUuw",
    user_role: "user",
    rpm_limit_per_model: {},
    tpm_limit_per_model: {},
    user_tpm_limit: 10,
    user_rpm_limit: 10,
    user_email: "[email protected]",
    object_permission: {
      object_permission_id: "067002ed-3b01-4bb3-b942-cefa400f0049",
      mcp_servers: [],
      mcp_access_groups: [],
      mcp_tool_permissions: {},
      vector_stores: [],
    },
    auto_rotate: false,
    rotation_interval: undefined,
    last_rotation_at: undefined,
    key_rotation_at: undefined,
  };
  describe("router settings", () => {
    const UNSUPPORTED_STORED_FIELD = { tag_routing_prefix: "team-" };
    const STORED_ROUTER_SETTINGS = {
      num_retries: 2,
      fallbacks: [{ "gpt-4": ["gpt-4o"] }],
      ...UNSUPPORTED_STORED_FIELD,
    };

    const renderWithRouterSettings = (onSubmit: (values: Record<string, unknown>) => Promise<void>) =>
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, router_settings: STORED_ROUTER_SETTINGS }}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken="test-token"
          userID="test-user"
          userRole="proxy_admin"
          premiumUser={true}
        />,
      );

    beforeEach(() => {
      routerSettingsMocks.receivedValue = undefined;
      routerSettingsMocks.editedValue = null;
    });

    it("should load the fields it renders into the editor and withhold the ones it does not", async () => {
      renderWithRouterSettings(async () => {});

      await waitFor(() => {
        expect(routerSettingsMocks.receivedValue).toStrictEqual({
          router_settings: { num_retries: 2, fallbacks: [{ "gpt-4": ["gpt-4o"] }] },
        });
      });
    });

    it("should submit edited fallbacks alongside routing fields the editor cannot show", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithRouterSettings(onSubmit);
      routerSettingsMocks.editedValue = { num_retries: 2, fallbacks: [{ "gpt-4": ["gpt-4o", "gpt-4o-mini"] }] };

      fireEvent.click(screen.getByText("Save Changes"));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalledWith(
          expect.objectContaining({
            router_settings: expect.objectContaining({
              ...UNSUPPORTED_STORED_FIELD,
              num_retries: 2,
              fallbacks: [{ "gpt-4": ["gpt-4o", "gpt-4o-mini"] }],
            }),
          }),
          expect.any(Array),
        );
      });
    });

    it("should submit cleared router settings so removing every fallback is persisted", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithRouterSettings(onSubmit);
      routerSettingsMocks.editedValue = { num_retries: null, fallbacks: null };

      fireEvent.click(screen.getByText("Save Changes"));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalledWith(
          expect.objectContaining({
            router_settings: expect.objectContaining({
              ...UNSUPPORTED_STORED_FIELD,
              num_retries: null,
              fallbacks: null,
            }),
          }),
          expect.any(Array),
        );
      });
    });
  });

  it("should render", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Save Changes")).toBeInTheDocument();
    });
  });

  it("should render tags", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("test-tag")).toBeInTheDocument();
    });
  });

  it("should not render tags in metadata textarea", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    const metadataTextarea = screen.getByLabelText("Metadata") as HTMLTextAreaElement;
    await waitFor(() => {
      expect(metadataTextarea).toHaveValue("{}");
    });
  });

  beforeEach(() => {
    vi.clearAllMocks();
    can.mockReturnValue(true);
    vi.mocked(getUiSettings).mockResolvedValue({ values: { enable_projects_ui: false } });
    testQueryClient.removeQueries({ queryKey: ["uiSettings"] });
  });

  describe("policy and prompt fields", () => {
    const renderAs = (userRole: string) =>
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole={userRole}
          premiumUser={true}
        />,
      );

    it("locks the prompts control for a non-premium admin so an unsavable value cannot be entered", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"Admin"}
          premiumUser={false}
        />,
      );

      const prompts = await screen.findByLabelText(/Prompts/);
      expect(prompts).toBeDisabled();

      await userEvent.type(prompts, "sneaky-prompt{Enter}");

      expect(screen.queryByLabelText("sneaky-prompt")).not.toBeInTheDocument();
    });

    it("leaves the prompts control usable for a premium admin", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"Admin"}
          premiumUser={true}
        />,
      );

      const prompts = await screen.findByLabelText(/Prompts/);
      expect(prompts).toBeEnabled();

      await userEvent.type(prompts, "allowed-prompt{Enter}");

      expect(await screen.findByLabelText("allowed-prompt")).toBeInTheDocument();
    });

    it("should render both fields and load prompts for an admin", async () => {
      renderAs("Admin");

      await waitFor(() => {
        expect(getPromptsList).toHaveBeenCalledWith("test-token");
      });
      expect(screen.getByText("Prompts", { selector: "label" })).toBeInTheDocument();
      expect(screen.getByText("Policies")).toBeInTheDocument();
    });

    it("lists a prompt existing in several environments once in the dropdown", async () => {
      vi.mocked(getPromptsList).mockResolvedValueOnce({
        prompts: [
          { prompt_id: "envgreet", litellm_params: {}, prompt_info: { prompt_type: "db" }, environment: "development" },
          { prompt_id: "envgreet", litellm_params: {}, prompt_info: { prompt_type: "db" }, environment: "production" },
        ],
      });

      renderAs("Admin");

      const prompts = await screen.findByLabelText(/Prompts/);
      await userEvent.type(prompts, "envgreet");

      expect(await screen.findAllByRole("option", { name: "envgreet" })).toHaveLength(1);
    });

    it("should omit both fields and fire neither admin-only request for an internal user", async () => {
      renderAs("Internal User");

      await waitFor(() => {
        expect(modelAvailableCall).toHaveBeenCalled();
      });

      expect(getPromptsList).not.toHaveBeenCalled();
      expect(getPoliciesList).not.toHaveBeenCalled();
      expect(screen.queryByText("Prompts", { selector: "label" })).not.toBeInTheDocument();
      expect(screen.queryByText("Policies")).not.toBeInTheDocument();
    });
  });

  it("should call onCancel without submitting the form when cancel button is clicked", async () => {
    const onCancelMock = vi.fn();
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={onCancelMock}
        onSubmit={onSubmitMock}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    const cancelButton = await screen.findByRole("button", { name: /cancel/i });
    await userEvent.click(cancelButton);

    expect(onCancelMock).toHaveBeenCalledTimes(1);
    expect(onSubmitMock).not.toHaveBeenCalled();
  });

  it("should display key alias input field", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByLabelText("Key Alias")).toBeInTheDocument();
    });
  });

  it("should display models select field", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Models")).toBeInTheDocument();
    });
  });

  it("should display max budget input field", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByLabelText("Max Budget (USD)")).toBeInTheDocument();
    });
  });

  it("should display allowed routes input field", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByLabelText(/allowed routes/i)).toBeInTheDocument();
    });
  });

  it("should call onSubmit with form values when form is submitted", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByRole("button", { name: /save changes/i })).toBeInTheDocument();
    });

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
    });
  });

  it("should initialize and submit throttle_on_budget_exceeded from key metadata", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithThrottle = {
      ...MOCK_KEY_DATA,
      metadata: { ...MOCK_KEY_DATA.metadata, throttle_on_budget_exceeded: true },
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithThrottle}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Throttle on budget exceeded")).toBeInTheDocument();
    });

    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalledWith(
        expect.objectContaining({ throttle_on_budget_exceeded: true }),
        expect.any(Array),
      );
    });
  });

  it("should initialize and submit enable_prompt_caching from key metadata", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithPromptCaching = {
      ...MOCK_KEY_DATA,
      metadata: { ...MOCK_KEY_DATA.metadata, enable_prompt_caching: true },
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithPromptCaching}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Enable Prompt Caching")).toBeInTheDocument();
    });

    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalledWith(
        expect.objectContaining({ enable_prompt_caching: true }),
        expect.any(Array),
      );
    });
  });

  it("should disable models field when management routes are selected", async () => {
    const keyDataWithManagementRoutes = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["management_routes"],
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithManagementRoutes}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Models field is disabled for this key type")).toBeInTheDocument();
    });
  });

  it("should disable models field when info routes are selected", async () => {
    const keyDataWithInfoRoutes = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["info_routes"],
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithInfoRoutes}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Models field is disabled for this key type")).toBeInTheDocument();
    });
  });

  it("should disable guardrails selector when user is not premium and has no write access role", async () => {
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={"test-token"}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Guardrails")).toBeInTheDocument();
    });
  });

  it("should parse comma-separated allowed routes on submit", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByLabelText(/allowed routes/i)).toBeInTheDocument();
    });

    const allowedRoutesInput = screen.getByLabelText(/allowed routes/i);
    await userEvent.clear(allowedRoutesInput);
    await userEvent.type(allowedRoutesInput, "route1, route2, route3");

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(Array.isArray(callArgs.allowed_routes)).toBe(true);
      expect(callArgs.allowed_routes).toEqual(["route1", "route2", "route3"]);
    });
  });

  it("should handle empty allowed routes string on submit", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithRoutes = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["llm_api_routes"],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithRoutes}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByLabelText(/allowed routes/i)).toBeInTheDocument();
    });

    const allowedRoutesInput = screen.getByLabelText(/allowed routes/i);
    await userEvent.clear(allowedRoutesInput);

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.allowed_routes).toEqual([]);
    });
  });

  it("should omit allowed_routes from submit when value is unchanged", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const aiApisKeyData = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["llm_api_routes"],
    };
    renderWithProviders(
      <KeyEditView
        keyData={aiApisKeyData}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByRole("button", { name: /save changes/i })).toBeInTheDocument();
    });

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect("allowed_routes" in callArgs).toBe(false);
    });
  });

  it("should omit allowed_routes from submit when keyData.allowed_routes is null and form is untouched", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataNullRoutes = {
      ...MOCK_KEY_DATA,
      allowed_routes: null as unknown as string[],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataNullRoutes}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByRole("button", { name: /save changes/i })).toBeInTheDocument();
    });

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect("allowed_routes" in callArgs).toBe(false);
    });
  });

  it("should omit allowed_routes from submit when server returned routes in a different order", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataReordered = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["beta_routes", "alpha_routes"],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataReordered}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByRole("button", { name: /save changes/i })).toBeInTheDocument();
    });

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect("allowed_routes" in callArgs).toBe(false);
    });
  });

  it("should pass access_group_ids to onSubmit when saving key with access groups", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithAccessGroups = {
      ...MOCK_KEY_DATA,
      access_group_ids: ["ag-1"],
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithAccessGroups}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken="test-token"
        userID="test-user"
        userRole="admin"
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByTestId("access-group-selector")).toBeInTheDocument();
    });

    const accessGroupInput = screen.getByTestId("access-group-selector");
    await userEvent.clear(accessGroupInput);
    await userEvent.type(accessGroupInput, "ag-1,ag-2");

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.access_group_ids).toEqual(["ag-1", "ag-2"]);
    });
  });

  it("should keep mcp_toolsets when saving an edit that does not touch the MCP selector", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithToolset = {
      ...MOCK_KEY_DATA,
      object_permission: {
        ...MOCK_KEY_DATA.object_permission!,
        mcp_toolsets: ["ts-1"],
      },
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithToolset}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken="test-token"
        userID="test-user"
        userRole="admin"
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Save Changes")).toBeInTheDocument();
    });

    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
    });
    expect(onSubmitMock.mock.calls[0][0].mcp_servers_and_groups.toolsets).toEqual(["ts-1"]);
  });

  it("should submit budget_limits: [] when the last budget window is deleted", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithWindow = {
      ...MOCK_KEY_DATA,
      budget_limits: [{ budget_duration: "30d", max_budget: 100 }],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithWindow}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const deleteWindowButton = await screen.findByRole("button", { name: "✕" });
    await userEvent.click(deleteWindowButton);

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_limits).toEqual([]);
    });
  });

  it("should preserve a budget draft when the same key refreshes before saving", async () => {
    const onSubmit = vi.fn().mockResolvedValue(undefined);
    const props = {
      keyData: MOCK_KEY_DATA,
      onCancel: vi.fn(),
      onSubmit,
      accessToken: "test-token",
      userID: "test-user",
      userRole: "admin",
    };
    const view = renderWithProviders(<KeyEditView {...props} />);

    fireEvent.change(await screen.findByRole("spinbutton", { name: "Max Budget (USD)" }), {
      target: { value: "12.5" },
    });
    view.rerender(
      <KeyEditView
        {...props}
        keyData={{
          ...MOCK_KEY_DATA,
          max_budget: 99,
          rpm_limit: 75,
          models: ["gpt-4"],
          metadata: { note: "new" },
          object_permission: { ...MOCK_KEY_DATA.object_permission, vector_stores: ["store-new"] },
        }}
      />,
    );
    await chooseSelectOption(userEvent, screen.getByLabelText("Reset Budget"), "monthly");
    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    const expected = {
      max_budget: "12.5",
      budget_duration: "30d",
      rpm_limit: 75,
      models: ["gpt-4"],
      metadata: JSON.stringify({ note: "new" }, null, 2),
      vector_stores: ["store-new"],
    };
    await waitFor(() => expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining(expected), expect.any(Array)));
    const [, dirtyFields] = onSubmit.mock.calls[0];
    expect(dirtyFields).toContain("max_budget");
    expect(dirtyFields).not.toContain("rpm_limit");
  });

  const mcpServer = (server_id: string) => ({
    server_id,
    server_name: server_id,
    url: `https://${server_id}.example`,
    transport: "http",
    auth_type: "none",
    created_at: "",
    created_by: "",
    updated_at: "",
    updated_by: "",
  });
  const mcpTools = (names: string[]) => ({
    tools: names.map((name) => ({ name })),
    error: null,
    message: null,
    stack_trace: null,
  });

  it("should not mark MCP tool defaults as an edit when saving only a budget", async () => {
    vi.mocked(fetchMCPServers).mockResolvedValue([mcpServer("srv-a")]);
    vi.mocked(listMCPTools).mockResolvedValue(mcpTools(["read_page", "delete_page"]));
    testQueryClient.clear();
    const onSubmit = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={{ ...MOCK_KEY_DATA, object_permission: { mcp_servers: ["srv-a"] } }}
        onCancel={vi.fn()}
        onSubmit={onSubmit}
        accessToken="test-token"
        userID="test-user"
        userRole="admin"
      />,
    );
    await waitFor(() => expect(listMCPTools).toHaveBeenCalledWith("test-token", "srv-a"));

    fireEvent.change(await screen.findByRole("spinbutton", { name: "Max Budget (USD)" }), {
      target: { value: "12.5" },
    });
    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
    const [values, dirtyFields] = onSubmit.mock.calls[0];
    expect(values.mcp_tool_permissions).toHaveProperty("srv-a");
    expect(dirtyFields).toEqual(["max_budget"]);
    vi.mocked(fetchMCPServers).mockResolvedValue([]);
    vi.mocked(listMCPTools).mockResolvedValue(mcpTools([]));
  });

  it("should keep default tool limits for a newly added MCP server when the same key refreshes", async () => {
    vi.mocked(fetchMCPServers).mockResolvedValue([mcpServer("mcp-1")]);
    vi.mocked(listMCPTools).mockResolvedValue(mcpTools(["read-page", "delete-page"]));
    testQueryClient.clear();
    const onSubmit = vi.fn().mockResolvedValue(undefined);
    const props = {
      keyData: MOCK_KEY_DATA,
      onCancel: vi.fn(),
      onSubmit,
      accessToken: "test-token",
      userID: "test-user",
      userRole: "admin",
    };
    const view = renderWithProviders(<KeyEditView {...props} />);

    await userEvent.click(await screen.findByTestId("mcp-server-selector"));
    await waitFor(() => expect(listMCPTools).toHaveBeenCalledWith("test-token", "mcp-1"));
    view.rerender(<KeyEditView {...props} keyData={{ ...MOCK_KEY_DATA, rpm_limit: 75 }} />);
    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => expect(onSubmit).toHaveBeenCalled());
    expect(onSubmit.mock.calls[0][0]).toMatchObject({
      rpm_limit: 75,
      mcp_tool_permissions: { "mcp-1": ["read-page"] },
    });
    vi.mocked(fetchMCPServers).mockResolvedValue([]);
    vi.mocked(listMCPTools).mockResolvedValue(mcpTools([]));
  });

  it("should start a fresh budget draft when switching between keys identified only by token_id", async () => {
    const onSubmit = vi.fn().mockResolvedValue(undefined);
    const props = {
      keyData: { ...MOCK_KEY_DATA, token: "", token_id: "key-one" },
      onCancel: vi.fn(),
      onSubmit,
      accessToken: "test-token",
      userID: "test-user",
      userRole: "admin",
    };
    const view = renderWithProviders(<KeyEditView {...props} />);
    fireEvent.change(await screen.findByRole("spinbutton", { name: "Max Budget (USD)" }), {
      target: { value: "12.5" },
    });
    view.rerender(
      <KeyEditView {...props} keyData={{ ...MOCK_KEY_DATA, token: "", token_id: "key-two", max_budget: 25 }} />,
    );
    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() =>
      expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ max_budget: 25 }), expect.any(Array)),
    );
  });

  it("should start a fresh budget draft when switching to another key", async () => {
    const onSubmit = vi.fn().mockResolvedValue(undefined);
    const props = {
      keyData: MOCK_KEY_DATA,
      onCancel: vi.fn(),
      onSubmit,
      accessToken: "test-token",
      userID: "test-user",
      userRole: "admin",
    };
    const view = renderWithProviders(<KeyEditView {...props} />);
    fireEvent.change(await screen.findByRole("spinbutton", { name: "Max Budget (USD)" }), {
      target: { value: "12.5" },
    });
    view.rerender(
      <KeyEditView
        {...props}
        keyData={{ ...MOCK_KEY_DATA, token: "different-key", token_id: "different-key", max_budget: 25 }}
      />,
    );
    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() =>
      expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ max_budget: 25 }), expect.any(Array)),
    );
  });

  it("should persist a canonical budget_duration value, not a word-form the backend cannot parse", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await chooseSelectOption(userEvent, await screen.findByLabelText("Reset Budget"), "weekly");

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_duration).toBe("7d");
    });
  });

  it("should keep an existing canonical budget_duration canonical when saved untouched", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const submitButton = await screen.findByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_duration).toBe("30d");
    });
  });

  it("should heal a legacy word-form budget_duration to canonical when saved untouched", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const legacyKeyData = { ...MOCK_KEY_DATA, budget_duration: "monthly" };
    renderWithProviders(
      <KeyEditView
        keyData={legacyKeyData}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const submitButton = await screen.findByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_duration).toBe("30d");
    });
  });

  it("should send an explicit null budget_duration when a previously-set Reset Budget is cleared", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const resetBudget = await screen.findByLabelText("Reset Budget");
    await chooseSelectOption(userEvent, resetBudget, "Never resets");

    await waitFor(() => {
      expect(resetBudget).toHaveTextContent("Never resets");
    });

    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
    });
    const callArgs = onSubmitMock.mock.calls[0][0];
    expect(callArgs.budget_duration).toBeNull();
    expect(JSON.stringify({ ...callArgs })).toContain('"budget_duration":null');
  });

  it("should send an explicit null budget_duration when a legacy word-form Reset Budget is cleared", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const legacyKeyData = { ...MOCK_KEY_DATA, budget_duration: "monthly" };
    renderWithProviders(
      <KeyEditView
        keyData={legacyKeyData}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await chooseSelectOption(userEvent, await screen.findByLabelText("Reset Budget"), "Never resets");

    await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
    });
    expect(onSubmitMock.mock.calls[0][0].budget_duration).toBeNull();
  });

  it("should omit budget_limits when existing windows are left untouched (issue #33246)", async () => {
    // The backend treats any budget_limits in the payload as an admin-only
    // budget change, so re-sending untouched windows 403s a non-admin owner.
    // Leaving the field off keeps the stored windows and passes the gate.
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithWindow = {
      ...MOCK_KEY_DATA,
      budget_limits: [{ budget_duration: "30d", max_budget: 100, reset_at: "2026-08-01T00:00:00" }],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithWindow}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const submitButton = await screen.findByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_limits).toBeUndefined();
    });
  });

  it("should omit budget_limits on a key that has no windows (issue #33246 repro)", async () => {
    // Core repro: a non-admin owner edits a non-budget field on a key with no
    // budget windows. The form previously always sent budget_limits: [], which
    // the backend read as a budget change and rejected.
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA} // no budget_limits
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const submitButton = await screen.findByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_limits).toBeUndefined();
    });
  });

  it("should send budget_limits when a window's cap is changed", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithWindow = {
      ...MOCK_KEY_DATA,
      budget_limits: [{ budget_duration: "30d", max_budget: 100 }],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithWindow}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const maxBudgetInput = await screen.findByPlaceholderText("Max spend ($)");
    await userEvent.clear(maxBudgetInput);
    await userEvent.type(maxBudgetInput, "200");

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_limits).toEqual([{ budget_duration: "30d", max_budget: 200 }]);
    });
  });

  it("should omit budget_limits (not clear stored windows) when a window is left incomplete", async () => {
    const onSubmitMock = vi.fn().mockResolvedValue(undefined);
    const keyDataWithWindow = {
      ...MOCK_KEY_DATA,
      budget_limits: [{ budget_duration: "30d", max_budget: 100 }],
    };
    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithWindow}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    const maxBudgetInput = await screen.findByPlaceholderText("Max spend ($)");
    await userEvent.clear(maxBudgetInput);

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.budget_limits).toBeUndefined();
    });
  });

  describe("per-model budgets", () => {
    const keyDataWithBudgets = {
      ...MOCK_KEY_DATA,
      model_max_budget: { "gpt-4": { budget_limit: 5, time_period: "30d" } },
    };

    const renderWith = (premiumUser: boolean) => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={keyDataWithBudgets}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"admin"}
          premiumUser={premiumUser}
        />,
      );
      return onSubmit;
    };

    // Same hazard as the user edit form: keyData changes while this component
    // stays mounted (the effect re-seeds the form for exactly that reason), and
    // the editor's rows live in state seeded once.
    it("re-seeds the editor when a different key is loaded", async () => {
      const withBudget = (limit: number, token: string) => ({
        ...MOCK_KEY_DATA,
        token,
        model_max_budget: { "gpt-4": { budget_limit: limit, time_period: "1h" } },
      });

      const { rerender } = renderWithProviders(
        <KeyEditView
          keyData={withBudget(5, "tok-a")}
          onCancel={() => {}}
          onSubmit={vi.fn().mockResolvedValue(undefined)}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"admin"}
          premiumUser={true}
        />,
      );
      expect(await screen.findByPlaceholderText("Max spend ($)")).toHaveValue(5);

      rerender(
        <KeyEditView
          keyData={withBudget(99, "tok-b")}
          onCancel={() => {}}
          onSubmit={vi.fn().mockResolvedValue(undefined)}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"admin"}
          premiumUser={true}
        />,
      );

      expect(await screen.findByPlaceholderText("Max spend ($)")).toHaveValue(99);
    });

    it("should say why the editor is locked when the proxy has no enterprise license", async () => {
      renderWith(false);

      expect(await screen.findByText(MODEL_MAX_BUDGET_PREMIUM_HINT)).toBeInTheDocument();
    });

    it("should leave the editor usable when the proxy has one", async () => {
      renderWith(true);

      expect(await screen.findByText(/Cap spend per model over its own window/)).toBeInTheDocument();
      expect(screen.queryByText(MODEL_MAX_BUDGET_PREMIUM_HINT)).not.toBeInTheDocument();
    });

    // /key/update validates model_max_budget whenever the field is present and
    // rejects it without a license, so re-sending an untouched budget would turn
    // every unrelated edit into a 400.
    it("should leave model_max_budget out of an edit that did not touch it", async () => {
      const onSubmit = renderWith(true);

      await userEvent.click(await screen.findByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalled();
      });
      expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("model_max_budget");
    });
  });

  it("should display 'AI APIs' label for the llm_api key type option", async () => {
    const keyDataWithLlmApiRoutes = {
      ...MOCK_KEY_DATA,
      allowed_routes: ["llm_api_routes"],
    };

    renderWithProviders(
      <KeyEditView
        keyData={keyDataWithLlmApiRoutes}
        onCancel={() => {}}
        onSubmit={async () => {}}
        accessToken={""}
        userID={""}
        userRole={""}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByText("Key Type")).toBeInTheDocument();
    });

    // The selected key type label should show "AI APIs" (not "LLM API")
    await userEvent.click(screen.getByLabelText("Key Type"));

    await waitFor(() => {
      // Verify "AI APIs" appears as an option label
      const optionTexts = visibleOptions().map((el) => el.textContent);
      const hasAIAPIs = optionTexts.some((text) => text?.includes("AI APIs"));
      expect(hasAIAPIs).toBe(true);

      // Verify old "LLM API" label does NOT appear
      const hasLLMAPI = optionTexts.some((text) => text?.includes("LLM API"));
      expect(hasLLMAPI).toBe(false);
    });
  });

  it("should display cancel button during submission", async () => {
    let resolveSubmit: (() => void) | undefined;
    const submitPromise = new Promise<void>((resolve) => {
      resolveSubmit = resolve;
    });
    const onSubmitMock = vi.fn(() => submitPromise);

    renderWithProviders(
      <KeyEditView
        keyData={MOCK_KEY_DATA}
        onCancel={() => {}}
        onSubmit={onSubmitMock}
        accessToken={"test-token"}
        userID={"test-user"}
        userRole={"admin"}
        premiumUser={false}
      />,
    );

    await waitFor(() => {
      expect(screen.getByRole("button", { name: /cancel/i })).toBeInTheDocument();
    });

    const submitButton = screen.getByRole("button", { name: /save changes/i });
    await userEvent.click(submitButton);

    // Wait for onSubmit to be called, which means handleSubmit has started and isKeySaving should be true
    await waitFor(() => {
      expect(onSubmitMock).toHaveBeenCalled();
    });

    // Wait for the cancel button to actually be disabled (state update may take a moment)
    await waitFor(
      () => {
        const cancelButton = screen.getByRole("button", { name: /cancel/i });
        expect(cancelButton).toBeDisabled();
      },
      { timeout: 3000 },
    );

    // Clean up: resolve the promise to allow the form to complete
    if (resolveSubmit) {
      resolveSubmit();
    }
  });

  describe("organization dropdown", () => {
    it("should render the organization dropdown", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Organization")).toBeInTheDocument();
      });
    });

    it("should disable the organization dropdown for non-admin users", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Internal User"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Organization")).toBeInTheDocument();
      });

      await userEvent.click(screen.getByLabelText("Organization"));

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

    it("should not disable the organization dropdown for admin users", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Organization")).toBeInTheDocument();
      });

      await userEvent.click(screen.getByLabelText("Organization"));

      expect(await screen.findByText("Engineering")).toBeInTheDocument();
    });

    it("should initialize organization from keyData", async () => {
      const keyWithOrg = {
        ...MOCK_KEY_DATA,
        organization_id: "org-1",
      };

      renderWithProviders(
        <KeyEditView
          keyData={keyWithOrg}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByLabelText("Organization")).toHaveValue("Engineering");
      });
    });

    it("scopes the MCP server selector to the key's team and owner", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, team_id: "group-maple", user_id: "owner-7" }}
          onCancel={() => {}}
          onSubmit={vi.fn()}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      const selector = await screen.findByTestId("mcp-server-selector");
      expect(selector).toHaveAttribute("data-team-id", "group-maple");
      expect(selector).toHaveAttribute("data-user-id", "owner-7");
    });

    it("keeps a saved MCP grant editable when it falls outside the key's team scope", async () => {
      const server = (server_id: string, server_name: string) => ({
        server_id,
        server_name,
        url: `https://${server_id}.example`,
        transport: "http",
        auth_type: "none",
        created_at: "",
        created_by: "",
        updated_at: "",
        updated_by: "",
      });
      vi.mocked(fetchMCPServers).mockImplementation(async (_token, teamId) =>
        teamId ? [server("srv-a", "team-wiki")] : [server("srv-a", "team-wiki"), server("srv-b", "direct-grant")],
      );
      testQueryClient.clear();
      renderWithProviders(
        <KeyEditView
          keyData={{
            ...MOCK_KEY_DATA,
            team_id: "group-maple",
            object_permission: { mcp_servers: ["srv-b"], mcp_tool_permissions: { "srv-b": ["read"] } },
          }}
          onCancel={() => {}}
          onSubmit={vi.fn()}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      expect(await screen.findByText("direct-grant")).toBeInTheDocument();
      expect(screen.getByTestId("mcp-server-selector")).toHaveAttribute("data-team-id", "group-maple");
      vi.mocked(fetchMCPServers).mockResolvedValue([]);
    });

    it("rescopes the MCP server selector and drops stale MCP grants when the team is changed before saving", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={{
            ...MOCK_KEY_DATA,
            organization_id: "org-1",
            team_id: "group-maple",
            object_permission: { mcp_servers: ["srv-a"], mcp_tool_permissions: { "srv-a": ["read"] } },
          }}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      const selector = await screen.findByTestId("mcp-server-selector");
      expect(selector).toHaveAttribute("data-team-id", "group-maple");
      await waitFor(() => {
        expect(screen.getAllByRole("button", { name: "Clear" })).toHaveLength(2);
      });
      await userEvent.click(screen.getAllByRole("button", { name: "Clear" })[1]);

      await waitFor(() => {
        expect(screen.getByTestId("mcp-server-selector")).toHaveAttribute("data-team-id", "");
      });
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalled();
      });
      expect(onSubmit.mock.calls[0][0]).toMatchObject({
        mcp_servers_and_groups: { servers: [], accessGroups: [], toolsets: [] },
        mcp_tool_permissions: {},
      });
    });

    it("keeps the team change's cleared MCP grants when the same key refreshes before saving", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      const keyData = {
        ...MOCK_KEY_DATA,
        organization_id: "org-1",
        team_id: "group-maple",
        object_permission: { mcp_servers: ["srv-a"], mcp_tool_permissions: { "srv-a": ["read"] } },
      };
      const renderEditor = (data: KeyResponse) => (
        <KeyEditView
          keyData={data}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />
      );
      const view = renderWithProviders(renderEditor(keyData));

      await waitFor(() => {
        expect(screen.getAllByRole("button", { name: "Clear" })).toHaveLength(2);
      });
      await userEvent.click(screen.getAllByRole("button", { name: "Clear" })[1]);
      await waitFor(() => {
        expect(screen.getByTestId("mcp-server-selector")).toHaveAttribute("data-team-id", "");
      });
      view.rerender(renderEditor({ ...keyData, rpm_limit: 75 }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalled();
      });
      expect(onSubmit.mock.calls[0][0]).toMatchObject({
        mcp_servers_and_groups: { servers: [], accessGroups: [], toolsets: [] },
        mcp_tool_permissions: {},
      });
    });

    it("clears the organization and its dependent team in the update payload", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, organization_id: "org-1", team_id: "group-maple" }}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByLabelText("Organization")).toHaveValue("Engineering");
      });
      const clearButtons = screen.getAllByRole("button", { name: "Clear" });
      expect(clearButtons).toHaveLength(2);
      await userEvent.click(clearButtons[0]);
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalledWith(
          expect.objectContaining({ organization_id: null, team_id: null }),
          expect.any(Array),
        );
      });
      expect(JSON.parse(JSON.stringify(onSubmit.mock.calls[0][0]))).toMatchObject({
        organization_id: null,
        team_id: null,
      });
    });

    it("should save an explicit project detach while keeping parents locked until reopening the editor", async () => {
      vi.mocked(getUiSettings).mockResolvedValue({ values: { enable_projects_ui: true } });
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      const onCancel = vi.fn();
      const key = { ...MOCK_KEY_DATA, organization_id: "org-1", team_id: "group-maple", project_id: "project-orbit" };
      const team = {
        team_id: "group-maple",
        organization_id: "org-1",
        members_with_roles: [] as { user_id: string; role: string }[],
        team_member_permissions: [] as string[],
      };
      const renderEditor = (keyData: KeyResponse = key, role = "Admin", editorTeam = team) => (
        <KeyEditView
          keyData={keyData}
          teams={[editorTeam]}
          onCancel={onCancel}
          onSubmit={onSubmit}
          accessToken=""
          userID="user-orbit"
          userRole={role}
          premiumUser={false}
        />
      );
      const view = renderWithProviders(renderEditor());
      await userEvent.click(await screen.findByRole("button", { name: "Detach from project" }));
      expect(screen.getByRole("combobox", { name: "Organization" })).toBeDisabled();
      expect(screen.getByRole("combobox", { name: "Team" })).toBeDisabled();
      await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
      expect(onCancel).toHaveBeenCalledOnce();
      expect(onSubmit).not.toHaveBeenCalled();
      // KeyInfoView unmounts the editor on cancel and after a successful save.
      view.rerender(<></>);
      view.rerender(renderEditor({ ...key }));
      await userEvent.click(await screen.findByRole("button", { name: "Detach from project" }));
      view.rerender(renderEditor({ ...key, spend: 1 }));
      expect(screen.getByRole("button", { name: "Keep project" })).toBeVisible();
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));
      const expectedDetach = { project_id: null, organization_id: "org-1", team_id: "group-maple", models: key.models };
      await waitFor(() =>
        expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining(expectedDetach), expect.any(Array)),
      );
      expect(screen.getByRole("combobox", { name: "Team" })).toBeDisabled();
      view.rerender(<></>);
      view.rerender(renderEditor({ ...key, project_id: null }));
      expect(screen.getByRole("combobox", { name: "Team" })).toBeEnabled();
      expect(screen.queryByRole("button", { name: "Detach from project" })).not.toBeInTheDocument();
      view.rerender(<></>);
      view.rerender(renderEditor(key, "Internal User"));
      expect(screen.queryByRole("button", { name: "Detach from project" })).not.toBeInTheDocument();
      view.rerender(renderEditor(key, "Org Admin"));
      expect(screen.queryByRole("button", { name: "Detach from project" })).not.toBeInTheDocument();
      const memberTeam = { ...team, members_with_roles: [{ user_id: "user-orbit", role: "user" }] };
      view.rerender(renderEditor(key, "Org Admin", memberTeam));
      expect(screen.queryByRole("button", { name: "Detach from project" })).not.toBeInTheDocument();
      const permittedTeam = { ...memberTeam, team_member_permissions: ["/key/update"] };
      view.rerender(renderEditor(key, "Org Admin", permittedTeam));
      expect(await screen.findByRole("button", { name: "Detach from project" })).toBeInTheDocument();
      const adminTeam = { ...team, members_with_roles: [{ user_id: "user-orbit", role: "admin" }] };
      view.rerender(renderEditor(key, "Internal User", adminTeam));
      expect(await screen.findByRole("button", { name: "Detach from project" })).toBeInTheDocument();
    });

    it("keeps project key relationships locked and omits project updates when the project UI is disabled", async () => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, organization_id: "org-1", team_id: "group-maple", project_id: "project-orbit" }}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );
      expect(await screen.findByRole("combobox", { name: "Organization" })).toBeDisabled();
      expect(screen.getByRole("combobox", { name: "Team" })).toBeDisabled();
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));
      await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
      expect(onSubmit.mock.calls[0][0]).toMatchObject({ organization_id: "org-1", team_id: "group-maple" });
      expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("project_id");
    });
  });

  describe("models dropdown team gating", () => {
    const openModelsDropdown = async () => {
      await userEvent.click(screen.getByLabelText("Models"));
    };

    it("should offer all-proxy-models but not all-team-models for a teamless key", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      await waitFor(() => {
        expect(screen.getAllByText("gpt-4").length).toBeGreaterThan(0);
      });

      expect(screen.getAllByText("All Proxy Models").length).toBeGreaterThan(0);
      expect(screen.queryAllByText("All Team Models")).toHaveLength(0);
    });

    it("should offer all-team-models but hide all-proxy-models for a team key", async () => {
      const teamKeyData = { ...MOCK_KEY_DATA, team_id: "team-1" };
      const teams = [{ team_id: "team-1", models: ["all-proxy-models", "team-model-1"] }];

      renderWithProviders(
        <KeyEditView
          keyData={teamKeyData}
          teams={teams}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      await waitFor(() => {
        expect(screen.getAllByText("team-model-1").length).toBeGreaterThan(0);
      });

      expect(screen.getAllByText("All Team Models").length).toBeGreaterThan(0);
      expect(screen.queryAllByText("All Proxy Models")).toHaveLength(0);
      expect(screen.queryAllByText("all-proxy-models")).toHaveLength(0);
    });

    it("should not offer all-team-models for a team key whose team has not loaded yet", async () => {
      const teamKeyData = { ...MOCK_KEY_DATA, team_id: "team-1" };

      renderWithProviders(
        <KeyEditView
          keyData={teamKeyData}
          teams={[]}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      expect(screen.queryAllByText("All Team Models")).toHaveLength(0);
      expect(screen.queryAllByText("All Proxy Models")).toHaveLength(0);
    });

    it("should not duplicate the all-proxy-models option when the teamless model list already carries the sentinel", async () => {
      vi.mocked(modelAvailableCall).mockResolvedValueOnce({
        data: [{ id: "all-proxy-models" }, { id: "gpt-4" }],
      });

      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      const proxyOptionLabels = () => visibleOptions().map((option) => option.textContent);

      await waitFor(() => {
        expect(proxyOptionLabels()).toContain("gpt-4");
      });

      const labels = proxyOptionLabels();
      expect(labels.filter((label) => label === "All Proxy Models")).toHaveLength(1);
      expect(labels).not.toContain("all-proxy-models");
    });

    it("should collapse the selection to all-proxy-models when the sentinel is picked alongside a model", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);

      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={onSubmitMock}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      const clickOption = async (label: string) => {
        const option = await waitFor(() => {
          const match = optionByContent(label);
          expect(match).toBeTruthy();
          return match as HTMLElement;
        });
        fireEvent.click(option);
      };

      await clickOption("gpt-4");
      await clickOption("All Proxy Models");
      await userEvent.keyboard("{Escape}");

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].models).toEqual(["all-proxy-models"]);
    });

    it("should disable the individual model options once all-proxy-models is selected", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="user-123"
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await waitFor(() => {
        expect(screen.getByText("Models", { selector: "label" })).toBeInTheDocument();
      });

      await openModelsDropdown();

      const findOption = (label: string) => optionByContent(label);

      const gpt4Before = await waitFor(() => {
        const match = findOption("gpt-4");
        expect(match).toBeTruthy();
        return match!;
      });
      expect(isOptionDisabled(gpt4Before)).toBe(false);

      fireEvent.click(
        await waitFor(() => {
          const match = findOption("All Proxy Models");
          expect(match).toBeTruthy();
          return match!;
        }),
      );

      await waitFor(() => {
        expect(isOptionDisabled(findOption("gpt-4")!)).toBe(true);
      });
    });
  });

  describe("default customer budget", () => {
    const serviceAccountKey = (endUserBudgetId?: string): KeyResponse => ({
      ...MOCK_KEY_DATA,
      metadata: {
        service_account_id: "svc-a",
        ...(endUserBudgetId === undefined ? {} : { end_user_budget_id: endUserBudgetId }),
      },
    });

    const renderEditView = (keyData: KeyResponse, userRole: string = "Admin") => {
      const onSubmit = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={keyData}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={userRole}
          premiumUser={false}
        />,
      );
      return onSubmit;
    };

    const budgetField = () => screen.findByRole("combobox", { name: "Default Customer Budget" });
    const save = async () => userEvent.click(await screen.findByRole("button", { name: /save changes/i }));

    it("shows the stored budget and leaves it off an edit that did not touch it", async () => {
      const onSubmit = renderEditView(serviceAccountKey("svc-a-budget"));

      expect(await budgetField()).toHaveValue("svc-a-budget");
      await save();

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalled();
      });
      expect(onSubmit.mock.calls[0][0]).not.toHaveProperty("end_user_budget_id");
    });

    it("sends the newly chosen budget id", async () => {
      const onSubmit = renderEditView(serviceAccountKey());
      const user = userEvent.setup();

      await chooseSelectOption(user, await budgetField(), /svc-b-budget/);
      await save();

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalledWith(
          expect.objectContaining({ end_user_budget_id: "svc-b-budget" }),
          expect.any(Array),
        );
      });
    });

    it("sends an empty string when the stored budget is cleared so the backend removes it", async () => {
      const onSubmit = renderEditView(serviceAccountKey("svc-a-budget"));

      await budgetField();
      await userEvent.click(screen.getByRole("button", { name: "Clear" }));
      await save();

      await waitFor(() => {
        expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ end_user_budget_id: "" }), expect.any(Array));
      });
    });

    it("keeps the stored budget visible but read-only for a non-admin", async () => {
      renderEditView(serviceAccountKey("svc-a-budget"), "Internal User");

      const field = await budgetField();
      expect(field).toHaveValue("svc-a-budget");
      expect(field).toBeDisabled();
    });

    it("does not render the control on a plain key that has no budget to show", async () => {
      renderEditView(MOCK_KEY_DATA);

      await screen.findByRole("button", { name: /save changes/i });
      expect(screen.queryByRole("combobox", { name: "Default Customer Budget" })).not.toBeInTheDocument();
    });
  });

  describe("estimated output tokens", () => {
    const renderEditView = (
      keyData: KeyResponse,
      onSubmit: (values: any) => Promise<void>,
      userRole: string = "Admin",
    ) =>
      renderWithProviders(
        <KeyEditView
          keyData={keyData}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={userRole}
          premiumUser={false}
        />,
      );

    it("refuses to save an invalid per-model estimate, and saves once it is corrected", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderEditView(MOCK_KEY_DATA, onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });
      const perModel = screen.getByLabelText("Estimated Output Tokens Per Model");

      fireEvent.change(perModel, { target: { value: "not json" } });
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      expect(await screen.findByText(/JSON object of positive integers/)).toBeInTheDocument();
      expect(onSubmitMock).not.toHaveBeenCalled();

      fireEvent.change(perModel, { target: { value: '{"gpt-4": 4096}' } });
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
    });

    it("refuses to save a fractional estimate, and saves once it is corrected", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderEditView(MOCK_KEY_DATA, onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });
      const estimate = screen.getByLabelText("Estimated Output Tokens");

      fireEvent.change(estimate, { target: { value: "12.5" } });
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).not.toHaveBeenCalled();
      });

      fireEvent.change(estimate, { target: { value: "2048" } });
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
    });

    it("loads the estimates from key metadata and resubmits them unchanged", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderEditView(
        {
          ...MOCK_KEY_DATA,
          metadata: {
            ...MOCK_KEY_DATA.metadata,
            default_estimated_output_tokens: 512,
            default_estimated_output_tokens_per_model: { "gpt-4": 4096 },
          },
        },
        onSubmitMock,
      );

      await waitFor(() => {
        expect(screen.getByLabelText("Estimated Output Tokens")).toHaveValue(512);
      });
      expect(screen.getByLabelText("Estimated Output Tokens Per Model")).toHaveValue('{"gpt-4":4096}');

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.default_estimated_output_tokens).toBe(512);
      expect(callArgs.default_estimated_output_tokens_per_model).toEqual({ "gpt-4": 4096 });
    });

    it("submits edited estimates as a number and a parsed object", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderEditView(MOCK_KEY_DATA, onSubmitMock);

      await waitFor(() => {
        expect(screen.getByLabelText("Estimated Output Tokens")).toBeInTheDocument();
      });

      fireEvent.change(screen.getByLabelText("Estimated Output Tokens"), { target: { value: "2048" } });
      fireEvent.change(screen.getByLabelText("Estimated Output Tokens Per Model"), {
        target: { value: '{"gpt-5": 8192}' },
      });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs.default_estimated_output_tokens).toBe(2048);
      expect(callArgs.default_estimated_output_tokens_per_model).toEqual({ "gpt-5": 8192 });
    });

    it("omits both estimates from the payload when the controls are blank", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderEditView(MOCK_KEY_DATA, onSubmitMock);

      await waitFor(() => {
        expect(screen.getByLabelText("Estimated Output Tokens Per Model")).toHaveValue("");
      });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const callArgs = onSubmitMock.mock.calls[0][0];
      expect(callArgs).not.toHaveProperty("default_estimated_output_tokens");
      expect(callArgs).not.toHaveProperty("default_estimated_output_tokens_per_model");
    });

    it.each(["Internal User", "Admin Viewer", "org_admin"])(
      "leaves both controls read-only for %s and still resubmits the stored values",
      async (userRole) => {
        const onSubmitMock = vi.fn().mockResolvedValue(undefined);
        renderEditView(
          {
            ...MOCK_KEY_DATA,
            metadata: {
              ...MOCK_KEY_DATA.metadata,
              default_estimated_output_tokens: 512,
              default_estimated_output_tokens_per_model: { "gpt-4": 4096 },
            },
          },
          onSubmitMock,
          userRole,
        );

        await waitFor(() => {
          expect(screen.getByLabelText("Estimated Output Tokens")).toBeDisabled();
        });
        expect(screen.getByLabelText("Estimated Output Tokens Per Model")).toBeDisabled();

        await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

        await waitFor(() => {
          expect(onSubmitMock).toHaveBeenCalled();
        });
        const callArgs = onSubmitMock.mock.calls[0][0];
        expect(callArgs.default_estimated_output_tokens).toBe(512);
        expect(callArgs.default_estimated_output_tokens_per_model).toEqual({ "gpt-4": 4096 });
      },
    );

    it.each(["Admin", "proxy_admin"])("leaves both controls editable for %s", async (userRole) => {
      renderEditView(MOCK_KEY_DATA, vi.fn().mockResolvedValue(undefined), userRole);

      await waitFor(() => {
        expect(screen.getByLabelText("Estimated Output Tokens")).toBeEnabled();
      });
      expect(screen.getByLabelText("Estimated Output Tokens Per Model")).toBeEnabled();
    });
  });

  const UNTOUCHED_SAVE_PAYLOAD = {
    key_alias: "asdasdas",
    models: [],
    max_budget: 0,
    soft_budget: null,
    budget_duration: "30d",
    tpm_limit: 10,
    tpm_limit_type: null,
    rpm_limit: 10,
    rpm_limit_type: null,
    tpd_limit: 250000,
    throttle_on_budget_exceeded: false,
    enable_prompt_caching: false,
    max_parallel_requests: 10,
    model_tpm_limit: undefined,
    model_rpm_limit: undefined,
    guardrails: undefined,
    disable_global_guardrails: false,
    policies: undefined,
    tags: ["test-tag"],
    prompts: undefined,
    access_group_ids: [],
    allowed_passthrough_routes: undefined,
    vector_stores: [],
    mcp_servers_and_groups: { servers: [], accessGroups: [], toolsets: [] },
    mcp_tool_permissions: {},
    agents_and_groups: { agents: [], accessGroups: [] },
    skills: [],
    organization_id: null,
    team_id: null,
    logging_settings: [],
    metadata: "{}",
    duration: "30d",
    token: "test-token-123",
    disabled_callbacks: [],
    auto_rotate: false,
    rotation_interval: undefined,
    tag_rpm_limit: {},
  };

  describe("submit payload contract", () => {
    const renderForPayload = (
      onSubmit: (values: Record<string, unknown>) => Promise<void>,
      keyData: KeyResponse = MOCK_KEY_DATA,
    ) =>
      renderWithProviders(
        <KeyEditView
          keyData={keyData}
          onCancel={() => {}}
          onSubmit={onSubmit}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"Admin"}
          premiumUser={true}
        />,
      );

    it("sends exactly the bound form fields on an untouched save, and no server-only key data", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toStrictEqual(UNTOUCHED_SAVE_PAYLOAD);
    });

    it("drops the policy and prompt keys entirely for a role that cannot see those fields", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={onSubmitMock}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"Internal User"}
          premiumUser={true}
        />,
      );
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const payload = onSubmitMock.mock.calls[0][0];
      expect(payload).not.toHaveProperty("policies");
      expect(payload).not.toHaveProperty("prompts");
      expect(payload).toHaveProperty("guardrails");
    });

    it("routes the shared lifecycle and rate-limit-type controls into their own payload keys", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      const duration = screen.getByPlaceholderText("e.g., 30d");
      await userEvent.clear(duration);
      await userEvent.type(duration, "45d");

      await chooseSelectOption(userEvent, screen.getByLabelText(/TPM Rate Limit Type/), /^Guaranteed throughput/);

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const payload = onSubmitMock.mock.calls[0][0];
      expect(payload.duration).toBe("45d");
      expect(payload.tpm_limit_type).toBe("guaranteed_throughput");
      expect(payload.rpm_limit_type).toBeNull();
    });

    it("blanks duration rather than dropping the key when Never Expire is ticked", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock, { ...MOCK_KEY_DATA, expires: "2026-01-01T00:00:00Z" });
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("checkbox", { name: /never expire/i }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toHaveProperty("duration", null);
    });

    it("carries a typed value from every free-text and numeric control into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      const retype = async (label: RegExp | string, text: string) => {
        const control = screen.getByLabelText(label);
        await userEvent.clear(control);
        await userEvent.type(control, text);
      };

      await retype("Key Alias", "typed-alias");
      await retype("Max Budget (USD)", "12.5");
      await retype("TPM Limit", "111");
      await retype("RPM Limit", "222");
      await retype("Max Parallel Requests", "3");
      await retype("Model TPM Limit", '{{"gpt-4": 7}');
      await retype("Model RPM Limit", '{{"gpt-4": 8}');
      await retype("Metadata", '{{"typed": true}');

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toMatchObject({
        key_alias: "typed-alias",
        max_budget: "12.5",
        tpm_limit: "111",
        rpm_limit: "222",
        max_parallel_requests: "3",
        model_tpm_limit: '{"gpt-4": 7}',
        model_rpm_limit: '{"gpt-4": 8}',
        metadata: '{"typed": true}',
      });
    });

    it("carries every toggle driven off its default into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("switch", { name: /throttle on budget exceeded/i }));
      await userEvent.click(screen.getByRole("switch", { name: /enable prompt caching/i }));
      await userEvent.click(screen.getByRole("switch", { name: /disable global guardrails/i }));

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toMatchObject({
        throttle_on_budget_exceeded: true,
        enable_prompt_caching: true,
        disable_global_guardrails: true,
      });
    });

    it("carries a tag typed into the tags control into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.type(screen.getByLabelText("Tags"), "typed-tag{Enter}");

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].tags).toEqual(["test-tag", "typed-tag"]);
    });

    it("moves a tags array typed into the metadata JSON into the Tags control on blur", async () => {
      renderForPayload(vi.fn().mockResolvedValue(undefined));
      await screen.findByRole("button", { name: /save changes/i });

      const metadata = screen.getByLabelText("Metadata");
      fireEvent.change(metadata, { target: { value: '{"tags": ["pilot-tag"], "env": "non-prod"}' } });
      fireEvent.blur(metadata);

      expect(await screen.findByText("pilot-tag")).toBeInTheDocument();
      expect(metadata).toHaveValue('{\n  "env": "non-prod"\n}');
    });

    it("carries a tags array typed into the metadata JSON into the payload even without a blur", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      fireEvent.change(screen.getByLabelText("Metadata"), { target: { value: '{"tags": ["pilot-tag"]}' } });
      fireEvent.submit(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toMatchObject({ tags: ["test-tag", "pilot-tag"], metadata: "{}" });
    });

    const pickFromCombobox = async (inputLabel: RegExp | string, optionName: RegExp | string) => {
      await userEvent.click(screen.getByLabelText(inputLabel));
      await userEvent.click(await screen.findByRole("option", { name: optionName }));
      await userEvent.keyboard("{Escape}");
    };

    it("carries a picked guardrail into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await pickFromCombobox("Select guardrails", "guardrail-1");
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].guardrails).toEqual(["guardrail-1"]);
    });

    it("carries a picked policy into the payload", async () => {
      vi.mocked(getPoliciesList).mockResolvedValueOnce({
        policies: [{ policy_name: "policy-1", version_number: 1, version_status: "production" }],
      });
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await pickFromCombobox(/Select policies/, /policy-1/);
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].policies).toEqual(["policy-1"]);
    });

    it("carries a typed prompt into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.type(screen.getByLabelText("Prompts"), "prompt-1{Enter}");
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].prompts).toEqual(["prompt-1"]);
    });

    it("carries the RPM rate limit type into its own payload key", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await chooseSelectOption(userEvent, screen.getByLabelText(/RPM Rate Limit Type/), /^Guaranteed throughput/);

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      const payload = onSubmitMock.mock.calls[0][0];
      expect(payload.rpm_limit_type).toBe("guaranteed_throughput");
      expect(payload.tpm_limit_type).toBeNull();
    });

    it("carries a picked vector store into the payload", async () => {
      vi.mocked(vectorStoreListCall).mockResolvedValueOnce({
        data: [{ vector_store_id: "vs-1", vector_store_name: "VS One" }],
      });
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await pickFromCombobox("Select vector stores", /VS One/);
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].vector_stores).toEqual(["vs-1"]);
    });

    it("carries a picked pass through route into the payload", async () => {
      vi.mocked(getPassThroughEndpointsCall).mockResolvedValueOnce({
        endpoints: [{ path: "/bria", methods: ["POST"] }],
      });
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await pickFromCombobox(/allowed pass through routes/, /\/bria/);
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].allowed_passthrough_routes).toEqual(["/bria"]);
    });

    it("carries a picked team into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          teams={[{ team_id: "team-9", team_alias: "Team Nine" }]}
          onCancel={() => {}}
          onSubmit={onSubmitMock}
          accessToken={"test-token"}
          userID={"test-user"}
          userRole={"Admin"}
          premiumUser={true}
        />,
      );
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("combobox", { name: "Team" }));
      await userEvent.click(await screen.findByRole("option", { name: /Team Nine/ }));

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].team_id).toBe("team-9");
    });

    it("shows team aliases and IDs in the Team combobox", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, team_id: "team-1" }}
          teams={[
            { team_id: "team-1", team_alias: "Alpha" },
            { team_id: "team-2", team_alias: "Beta" },
          ]}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      const teamCombobox = screen.getByRole("combobox", { name: "Team" });
      expect(teamCombobox).toHaveValue("Alpha");
      await userEvent.click(teamCombobox);

      const alphaOption = await screen.findByRole("option", { name: /Alpha/ });
      const betaOption = screen.getByRole("option", { name: /Beta/ });
      expect(alphaOption).toHaveTextContent("team-1");
      expect(betaOption).toHaveTextContent("team-2");
    });

    it("shows a dash with the ID underneath for a team without an alias", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, team_id: null }}
          teams={[{ team_id: "team-3", team_alias: null }]}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      await userEvent.click(screen.getByRole("combobox", { name: "Team" }));

      expect(await screen.findByRole("option")).toHaveTextContent(/^-team-3$/);
    });

    it("clears the team in the update payload and keeps the organization", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, organization_id: "org-1", team_id: "team-1" }}
          teams={[{ team_id: "team-1", team_alias: "Alpha", organization_id: "org-1" }]}
          onCancel={() => {}}
          onSubmit={onSubmitMock}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      const teamCombobox = screen.getByRole("combobox", { name: "Team" });
      expect(teamCombobox).toHaveValue("Alpha");
      const [, teamClearButton] = screen.getAllByRole("button", { name: "Clear" });
      await userEvent.click(teamClearButton);
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalledWith(
          expect.objectContaining({ team_id: null, organization_id: "org-1" }),
          expect.any(Array),
        );
      });
    });

    it("shows the selected team alias when it is outside the visible teams", async () => {
      renderWithProviders(
        <KeyEditView
          keyData={{ ...MOCK_KEY_DATA, organization_id: "org-1", team_id: "team-9" }}
          teams={[{ team_id: "team-9", team_alias: "Gamma", organization_id: "org-2" }]}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken=""
          userID=""
          userRole="Admin"
          premiumUser={false}
        />,
      );

      expect(screen.getByRole("combobox", { name: "Team" })).toHaveValue("Gamma");
    });

    it("carries a picked MCP server into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: "pick mcp server" }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].mcp_servers_and_groups.servers).toEqual(["mcp-1"]);
    });

    it("carries a picked agent into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: "pick agent" }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].agents_and_groups.agents).toEqual(["agent-1"]);
    });

    it("carries a picked skill into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: "pick skill" }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].skills).toEqual(["private-skill"]);
    });

    it("preloads the stored skills into the payload when the selector is left untouched", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock, {
        ...MOCK_KEY_DATA,
        object_permission: { ...MOCK_KEY_DATA.object_permission, skills: ["stored-skill"] },
      } as KeyResponse);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].skills).toEqual(["stored-skill"]);
    });

    it("carries an added logging integration into the payload", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /add integration/i }));
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0].logging_settings).toEqual([
        { callback_name: "", callback_type: "success", callback_vars: {} },
      ]);
    });

    it("resends stored budget fallbacks on an untouched save", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock, {
        ...MOCK_KEY_DATA,
        budget_fallbacks: { "gpt-4": ["gpt-4o-mini"] },
      } as KeyResponse);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toHaveProperty("budget_fallbacks", { "gpt-4": ["gpt-4o-mini"] });
    });

    it("omits budget fallbacks entirely for a key that has none", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).not.toHaveProperty("budget_fallbacks");
    });

    it("resends the stored per-tag rpm limits on an untouched save", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock, {
        ...MOCK_KEY_DATA,
        metadata: { ...MOCK_KEY_DATA.metadata, tag_rpm_limit: { "test-tag": 7 } },
      } as KeyResponse);
      await screen.findByRole("button", { name: /save changes/i });

      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalled();
      });
      expect(onSubmitMock.mock.calls[0][0]).toHaveProperty("tag_rpm_limit", { "test-tag": 7 });
    });

    const setRpmLimit = (value: string) => {
      fireEvent.change(screen.getByLabelText("RPM Limit"), { target: { value } });
    };

    it("carries an edited RPM limit and the key identifier onto the wire", async () => {
      const onSubmitMock = vi.fn().mockResolvedValue(undefined);
      renderForPayload(onSubmitMock);
      await screen.findByRole("button", { name: /save changes/i });

      setRpmLimit("25");
      await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

      await waitFor(() => {
        expect(onSubmitMock).toHaveBeenCalledTimes(1);
      });
      expect(onSubmitMock.mock.calls[0][0]).toMatchObject({ token: "test-token-123", rpm_limit: "25" });
    });

    it.fails(
      "sends max_budget as an explicit null when the field is cleared (expected to fail until the forms revamp, tri-state PATCH tracker: today the view hands KeyInfoView an empty string and handleKeyUpdate maps it to null)",
      async () => {
        const onSubmitMock = vi.fn().mockResolvedValue(undefined);
        renderForPayload(onSubmitMock);
        await screen.findByRole("button", { name: /save changes/i });

        await userEvent.clear(screen.getByLabelText("Max Budget (USD)"));
        await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

        await waitFor(() => {
          expect(onSubmitMock).toHaveBeenCalledTimes(1);
        });
        expect(onSubmitMock.mock.calls[0][0]).toHaveProperty("max_budget", null);
      },
    );

    it.fails(
      "sends only the key identifier and the edited RPM limit (expected to fail until the forms revamp, tri-state PATCH tracker)",
      async () => {
        const onSubmitMock = vi.fn().mockResolvedValue(undefined);
        renderForPayload(onSubmitMock);
        await screen.findByRole("button", { name: /save changes/i });

        setRpmLimit("25");
        await userEvent.click(screen.getByRole("button", { name: /save changes/i }));

        await waitFor(() => {
          expect(onSubmitMock).toHaveBeenCalledTimes(1);
        });
        expect(onSubmitMock.mock.calls[0][0]).toStrictEqual({ token: "test-token-123", rpm_limit: "25" });
      },
    );
  });

  describe("disable_global_guardrails toggle gating", () => {
    const renderAs = (userRole: string) =>
      renderWithProviders(
        <KeyEditView
          keyData={MOCK_KEY_DATA}
          onCancel={() => {}}
          onSubmit={async () => {}}
          accessToken="test-token"
          userID="test-user"
          userRole={userRole}
          premiumUser={true}
        />,
      );

    it("hides the switch from a non-admin", async () => {
      renderAs("Internal User");
      await screen.findByRole("button", { name: /save changes/i });

      expect(screen.queryByRole("switch", { name: /disable global guardrails/i })).not.toBeInTheDocument();
    });

    it("shows the switch to a proxy admin", async () => {
      renderAs("Admin");

      expect(await screen.findByRole("switch", { name: /disable global guardrails/i })).toBeInTheDocument();
    });
  });
});