Skip to content

The platform for continuously improving AI agents.

8.4 KB
Raw Blame
// @vitest-environment jsdom
// covers: measured-facts
import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

// The @lobehub icon ESM subpaths behind the provider chip don't resolve under vitest's
// node resolver; the chip's chrome isn't the assertion.
vi.mock("@/components/model-provider-chip", () => ({
  getModelProviderInfo: () => ({}),
  getProviderIcon: () => null,
  ModelProviderChip: ({ model }: { model: string }) => <span>{model}</span>,
  ProviderLogo: () => null,
}));

import type { ModelDraft } from "@/components/finetuning/train/model-config";
import { ModelsPanel } from "@/components/finetuning/train/models-panel";
import type { TrainWizard } from "@/components/finetuning/train/use-train-wizard";
import { TooltipProvider } from "@/components/ui/tooltip";
import type { ModelCatalog } from "@/hooks/use-finetuning";
import type {
  Dataset,
  FinetuningEstimateResponse,
  FinetuningEvidence,
  FinetuningExperiment,
  FinetuningRecommendationResponse,
  FinetuningSkillScore,
} from "@/openapi";

afterEach(cleanup);

const SKILL_SCORES: FinetuningSkillScore[] = [
  {
    fieldN: 30,
    percentileGlobal: 74.4,
    percentileInField: 98.3,
    rankInField: 1,
    skill: "Instruction Following",
    weight: 0.5,
  },
];

const evidence = (benchmark: string): FinetuningEvidence => ({
  benchmark,
  cohortN: 450,
  percentile: 88.2,
  provenance: "measured",
  skill: "Instruction Following",
  source: "Artificial Analysis",
  url: "https://artificialanalysis.ai/evaluations/ifbench",
});

function draft(id: string, model: string, displayName: string): ModelDraft {
  return {
    confidence: "high",
    displayName,
    evidence: [evidence(`${displayName}-bench`), evidence("IFBench")],
    grade: 78,
    hyperparams: { batch_size: 8, learning_rate: 1e-4, n_epochs: 3, warmup_ratio: 0.05 },
    id,
    labClaimedOnly: false,
    learningRateFull: 1e-5,
    learningRateLora: 1e-4,
    loraParams: { lora_alpha: 32, lora_dropout: 0, lora_r: 16 },
    match: 98,
    matchPool: 30,
    matchRank: 1,
    maxBatchSize: 32,
    minBatchSize: 1,
    model,
    nBenchmarks: 2,
    params: "27B",
    skillScores: SKILL_SCORES,
    supportsFull: false,
    supportsLora: true,
    tier: "mid",
    useLora: true,
  };
}

const DRAFTS = [
  draft("d-1", "Qwen/Qwen3.5-27B", "Qwen3.5 27B"),
  draft("d-2", "meta/llama-3.3-8b", "Llama 3.3 8B"),
];

const REC = {
  benchmarkSnapshot: {
    generatedAt: "2026-08-11",
    sources: [
      {
        attributionRequired: false,
        name: "Artificial Analysis",
        url: "https://artificialanalysis.ai/evaluations/",
      },
      {
        attributionRequired: false,
        name: "HuggingFace eval results",
        url: "https://huggingface.co",
      },
    ],
  },
  candidates: [],
  dataset: { hasToolCalling: false, maxTokenLength: 193, rows: 342, totalTokens: 60_000 },
  excluded: [],
  selected: [],
  skillWeights: { Faithfulness: 0.3, "Instruction Following": 0.5, Reasoning: 0.2 },
  taskType: "extraction",
  taskTypeSource: "heuristic",
} as unknown as FinetuningRecommendationResponse;

const CANDIDATES = [
  { evidence: [evidence("IFBench"), evidence("AA-LCR")], model: "Qwen/Qwen3.5-27B" },
] as unknown as FinetuningExperiment[];

function wizard(over: Partial<TrainWizard>): TrainWizard {
  return {
    addModel: () => {},
    candidateByModel: new Map<string, FinetuningExperiment>(),
    candidates: CANDIDATES,
    catalog: { models: {}, tiers: [] } as unknown as ModelCatalog,
    catalogModelById: () => null,
    dataReady: true,
    drafts: DRAFTS,
    estimates: new Map(),
    evalDataset: { activeVersion: "1.0", rows: 30 } as unknown as Dataset,
    excluded: [],
    rec: REC,
    recommendQuery: { error: null, isLoading: false },
    removeModel: () => {},
    replaceDraftModel: () => {},
    selectedDrafts: DRAFTS,
    toggleSelected: () => {},
    updateDraft: () => {},
    validation: { stats: { trainExamples: 273, valExamples: 69 }, valid: true },
    ...over,
  } as unknown as TrainWizard;
}

function setup(over: Partial<TrainWizard> = {}) {
  return render(
    <TooltipProvider>
      <ModelsPanel wizard={wizard(over)} />
    </TooltipProvider>
  );
}

const cards = () =>
  within(screen.getByRole("list", { name: "Experiments" })).getAllByRole("listitem");

describe("candidate list", () => {
  it("omits unmeasured estimates while keeping measured duration ranges", () => {
    setup({
      drafts: [{ ...DRAFTS[0], evidence: [], match: null }, DRAFTS[1]],
      estimates: new Map([
        [
          DRAFTS[0].id,
          {
            forecast: { basis: "unmeasured_recipe", training_seconds: null },
            timeEstimate: { human: "Unmeasured recipe", seconds: null },
          } as unknown as FinetuningEstimateResponse,
        ],
        [
          DRAFTS[1].id,
          {
            forecast: { basis: "measured_recipe", training_seconds: [60, 120] },
            timeEstimate: { human: "1–2 min", seconds: null },
          } as unknown as FinetuningEstimateResponse,
        ],
      ]),
    });
    const [ungraded, measured] = cards();
    expect(
      within(ungraded).queryByText(
        /Not graded|Unmeasured recipe|Cost uncalibrated|Ranked below|other costs pending/
      )
    ).toBeNull();
    expect(within(measured).getByText("~1–2 min")).toBeTruthy();
    expect(within(measured).getByText("Match 98.00/100")).toBeTruthy();
  });

  it("shows the serving reservation separately from training parameters", () => {
    const candidate = {
      ...CANDIDATES[0],
      servingContext: {
        estimated: true,
        inputTokens: 6000,
        maxModelLen: 16384,
        modelContextLimit: 131072,
        outputTokens: 8192,
        requiredContext: 14704,
        rows: 150,
      },
    };
    setup({ candidateByModel: new Map([[candidate.model, candidate]]) });
    expect(screen.getByText(/Serving context 16,384 tokens/)).toBeTruthy();
    expect(screen.getByText(/Output budget 8,192 tokens/)).toBeTruthy();
  });

  it("shows why a selected model cannot fit the evaluation", () => {
    setup({
      excluded: [{ model: DRAFTS[0].model, reason: "Evaluation needs a larger serving context." }],
    });
    expect(within(cards()[0]).getByText("Evaluation needs a larger serving context.")).toBeTruthy();
  });

  it("opens the evidence chart under the card it belongs to", () => {
    setup();
    const [first, second] = cards();

    fireEvent.click(within(first).getByRole("button", { name: "Why this model" }));

    expect(within(first).getByText("Instruction Following")).toBeTruthy();
    expect(within(second).queryByText("Instruction Following")).toBeNull();
  });

  it("opens the tuning fields under the card it belongs to", () => {
    setup();
    const [first, second] = cards();

    fireEvent.click(within(second).getByRole("button", { name: "Parameters" }));

    expect(within(second).getByLabelText("Epochs")).toBeTruthy();
    expect(within(first).queryByLabelText("Epochs")).toBeNull();
  });

  it("closes the disclosure on a second click of the same tab", () => {
    setup();
    const [first] = cards();
    const tab = within(first).getByRole("button", { name: "Parameters" });

    fireEvent.click(tab);
    fireEvent.click(tab);

    expect(within(first).queryByLabelText("Epochs")).toBeNull();
  });
});

describe("analysis strip", () => {
  it("identifies capability context separately from the training rows", () => {
    setup({ rec: { ...REC, taskType: "code_generation", taskTypeSource: "capability" } });

    expect(screen.getByText("Code generation").getAttribute("title")).toBe(
      "Classified from capability codebase context"
    );
    expect(screen.getByText("Capability context")).toBeTruthy();
    expect(screen.getByText("Training data")).toBeTruthy();
    expect(screen.getByText("342 rows")).toBeTruthy();
  });

  it("labels the dataset fallback", () => {
    setup();
    expect(screen.getByText("Dataset")).toBeTruthy();
    expect(screen.queryByText("Capability context")).toBeNull();
  });

  it("keeps an unresolved capability task distinct from a dataset classification", () => {
    setup({ rec: { ...REC, skillWeights: {}, taskType: "unknown", taskTypeSource: "unknown" } });
    expect(screen.getByText("Not classified").getAttribute("title")).toBe(
      "Task has not been classified from available context"
    );
    expect(screen.queryByText("Capability context")).toBeNull();
    expect(screen.getByText("No skill weights")).toBeTruthy();
  });
});