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]

8.5 KB
Raw Blame
import React, { useState } from "react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { chooseSelectOption, fireEvent, renderWithProviders, screen } from "../../../tests/test-utils";
import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
import ClassificationMethodConfig from "./ClassificationMethodConfig";
import AutoRouterClassifierTabs from "./AutoRouterClassifierTabs";
import JevEditor from "./JevClassifierConfig";
import { type ComplexityRouterConfigValue } from "./ComplexityRouterConfig";
import {
  buildUpdatedComplexityRouterConfig,
  hydrateComplexityRouterConfig,
} from "../edit_auto_router/edit_auto_router_modal";
import { applyTierSetAction } from "./tier_set_actions";
import { testAutoRouterRouting } from "../networking";
import { JEV_CONNECTION_TEST_PROMPT } from "./build_auto_router_routing_test_request";

vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({
  default: vi.fn(() => ({
    isLoading: false,
    isAuthorized: true,
    token: "token",
    accessToken: "token",
    userId: "user",
    userEmail: "[email protected]",
    userRole: "Admin",
    userRoleLabel: "Admin",
    isViewOnly: false,
    premiumUser: false,
    disabledPersonalKeyCreation: false,
    showSSOBanner: false,
  })),
}));

vi.mock("@/components/networking", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@/components/networking")>()),
  getComplexityScorerDefaults: vi.fn(async () => ({
    tier_boundaries: {},
    token_thresholds: {},
    dimension_weights: {},
  })),
  testAutoRouterRouting: vi.fn(async () => ({ status: "error", error: "fixture" })),
}));

const initial: ComplexityRouterConfigValue = {
  classifier_type: "llm",
  classifier_llm_config: { model: "judge", timeout_ms: 1000 },
  tiers: { SIMPLE: ["fast"], MEDIUM: ["mid"], COMPLEX: ["strong"], REASONING: ["reasoner"] },
};

function Form() {
  const [value, setValue] = useState(initial);
  return (
    <AutoRouterClassifierTabs value={value} onChange={setValue}>
      <ClassificationMethodConfig
        value={value}
        onChange={setValue}
        modelOptions={[{ value: "judge", label: "judge" }]}
        effortOptionsByModel={{ judge: ["low"] }}
        customTechnicalKeywords={[]}
        onCustomTechnicalKeywordsChange={() => {}}
      />
      <button
        onClick={() =>
          setValue(
            applyTierSetAction(value, [], {
              kind: "patch",
              id: "SIMPLE",
              patch: { name: "QUICK", definition: "Quick tasks" },
            }).value,
          )
        }
      >
        Customize tiers
      </button>
      <button
        onClick={() =>
          setValue(hydrateComplexityRouterConfig(buildUpdatedComplexityRouterConfig({}, value), undefined))
        }
      >
        Save and reload
      </button>
      <button
        onClick={() => {
          const request = {
            prompt: JEV_CONNECTION_TEST_PROMPT,
            complexity_router_config: buildUpdatedComplexityRouterConfig({}, value),
          };
          void testAutoRouterRouting("token", request);
        }}
      >
        Probe current config
      </button>
    </AutoRouterClassifierTabs>
  );
}

describe("JEV classifier editor", () => {
  afterEach(() => vi.mocked(useAuthorized).mockReset());
  it.each([
    ["jev", "Jev", "jev-test"],
    ["laya", "Laya", "multilingual"],
    ["bespoke", "Bespoke Nimble", "bespokelabs/Bespoke-Nimble-9B"],
    ["databricks", "Databricks", "databricks-openjev-qwen35-4b"],
  ] as const)(
    "preserves %s, custom tiers and context through save, reload and probe",
    async (provider, label, model) => {
      const typesTheModel = provider === "jev" || provider === "databricks";
      renderWithProviders(<Form />);
      expect(screen.getByLabelText("Judge model")).toBeInTheDocument();
      expect(screen.getByText("Reasoning Effort")).toBeInTheDocument();
      expect(screen.getByText("Classifier Prompt")).toBeInTheDocument();
      expect(screen.getByRole("switch", { name: "Use images for classification" })).toBeInTheDocument();
      fireEvent.click(screen.getByRole("radio", { name: /^Decisions Model$/ }));
      expect(screen.getByRole("radio", { name: /^Decisions Model$/ })).toBeChecked();
      expect(screen.getByLabelText("Classifier Model")).toHaveValue("jev-latest");
      expect(screen.getByLabelText("Classifier Instructions")).toBeEnabled();
      expect(screen.queryByLabelText("Judge model")).not.toBeInTheDocument();
      expect(screen.queryByText("Reasoning Effort")).not.toBeInTheDocument();
      expect(screen.queryByText("Classifier Prompt")).not.toBeInTheDocument();
      expect(screen.queryByRole("switch", { name: "Use images for classification" })).not.toBeInTheDocument();
      await chooseSelectOption(userEvent, screen.getByRole("combobox", { name: "Decision model" }), "Laya");
      expect(screen.getByLabelText("Classifier Model")).toHaveTextContent("english");
      await chooseSelectOption(userEvent, screen.getByRole("combobox", { name: "Decision model" }), "Jev");
      expect(screen.getByLabelText("Classifier Model")).toHaveValue("jev-latest");
      await chooseSelectOption(userEvent, screen.getByRole("combobox", { name: "Decision model" }), label);
      if (provider === "bespoke") expect(screen.getByLabelText("Classifier Model")).toHaveTextContent("nimble-latest");
      if (provider === "databricks") expect(screen.getByLabelText("Classifier Model")).toHaveValue("ai_decide");
      if (typesTheModel) {
        fireEvent.change(screen.getByLabelText("Classifier Model"), { target: { value: model } });
      } else {
        await chooseSelectOption(userEvent, screen.getByLabelText("Classifier Model"), model);
      }
      fireEvent.change(screen.getByLabelText("Classifier Timeout (ms)"), { target: { value: "4200" } });
      fireEvent.change(screen.getByLabelText("Context Window Size"), { target: { value: "6" } });
      fireEvent.change(screen.getByLabelText("Circuit breaker cooldown (seconds)"), { target: { value: "50" } });
      fireEvent.click(screen.getByRole("switch", { name: "Classifier circuit breaker" }));
      fireEvent.click(screen.getByRole("button", { name: "Customize tiers" }));
      fireEvent.click(screen.getByRole("button", { name: "Save and reload" }));
      expect(screen.getByRole("radio", { name: /^Decisions Model$/ })).toBeChecked();
      expect(screen.getByRole("combobox", { name: "Decision model" })).toHaveTextContent(label);
      if (typesTheModel) expect(screen.getByLabelText("Classifier Model")).toHaveValue(model);
      else expect(screen.getByLabelText("Classifier Model")).toHaveTextContent(model);
      expect(screen.getByLabelText("Classifier Timeout (ms)")).toHaveValue(4200);
      expect(screen.getByLabelText("Context Window Size")).toHaveValue("6");
      expect(screen.getByRole("switch", { name: "Classifier circuit breaker" })).not.toBeChecked();
      fireEvent.click(screen.getByRole("button", { name: "Probe current config" }));
      expect(testAutoRouterRouting).toHaveBeenCalledWith(
        "token",
        expect.objectContaining({
          complexity_router_config: expect.objectContaining({
            classifier_type: "oss_classifier",
            opensource_classifier_config: {
              provider,
              model,
              timeout_ms: 4200,
              circuit_breaker_enabled: false,
              circuit_breaker_cooldown_seconds: 50,
            },
            tiers: expect.objectContaining({ QUICK: ["fast"] }),
          }),
        }),
      );
    },
  );

  it("allows licensed instructions and can restore built-in instructions", () => {
    const authorized = useAuthorized();
    vi.mocked(useAuthorized).mockReturnValue({ ...authorized, premiumUser: true });
    const LicensedForm = () => {
      const [value, setValue] = useState<ComplexityRouterConfigValue>({
        ...initial,
        classifier_type: "jev",
        jev_classifier_config: { model: "jev-latest", timeout_ms: 3000, instructions: "Existing instructions" },
      });
      return <JevEditor value={value} onChange={setValue} />;
    };
    renderWithProviders(<LicensedForm />);
    expect(screen.getByLabelText("Classifier Instructions")).toBeEnabled();
    fireEvent.change(screen.getByLabelText("Classifier Instructions"), { target: { value: "New instructions" } });
    expect(screen.getByLabelText("Classifier Instructions")).toHaveValue("New instructions");
    fireEvent.click(screen.getByRole("button", { name: "Restore built-in instructions" }));
    expect(screen.getByLabelText("Classifier Instructions")).toHaveValue("");
  });
});