Skip to content

The platform for continuously improving AI agents.

3.1 KB
Raw Blame
// covers: console-logic
import { describe, expect, it, vi } from "vitest";

import type { ClassMetrics, FinetuningJudgeEvalRow } from "@/hooks/use-finetuning";
import { buildClassSeries, buildMacroSeries } from "./class-metrics";
import { latestClassMetricsOf } from "./judge-eval-table";

// Provider-logo ESM subpaths do not resolve in Vitest; selection uses no logo rendering.
vi.mock("@/components/model-provider-chip", () => ({ ModelProviderChip: () => null }));

const metricsAt = (f1: number): ClassMetrics => ({
  aggregates: {
    accuracy: f1,
    macro: { f1, precision: f1 + 0.01, recall: f1 - 0.01 },
    n: 10,
  },
  classes: [
    { f1, label: "refund", precision: f1 + 0.1, recall: f1 - 0.1, support: 6 },
    { f1: f1 / 2, label: "fraud", precision: f1 / 2, recall: f1 / 2, support: 4 },
  ],
  confusion_matrix: {
    labels: ["fraud", "refund"],
    matrix: [
      [3, 1],
      [1, 5],
    ],
  },
});

const row = (
  kind: string,
  step: number | null,
  cm: ClassMetrics | null
): FinetuningJudgeEvalRow => ({
  checkpoint_step: step,
  class_metrics: cm,
  id: `${kind}-${step}`,
  kind,
  status: "completed",
});

const rows: FinetuningJudgeEvalRow[] = [
  row("baseline", null, metricsAt(0.4)),
  row("checkpoint", 24, metricsAt(0.6)),
  row("final", null, metricsAt(0.8)),
  row("checkpoint", 12, null),
];

describe("latestClassMetricsOf", () => {
  it("prefers final over checkpoints over baseline", () => {
    expect(latestClassMetricsOf(rows)?.row.kind).toBe("final");
    expect(latestClassMetricsOf(rows.slice(0, 2))?.row.kind).toBe("checkpoint");
    expect(latestClassMetricsOf([rows[0]])?.row.kind).toBe("baseline");
    expect(latestClassMetricsOf([rows[3]])).toBeNull();
  });

  it("ranks the incumbent after checkpoints and the base model before them", () => {
    const incumbent = row("incumbent_after", null, metricsAt(0.7));
    const base = row("model_before", null, metricsAt(0.5));
    expect(latestClassMetricsOf([...rows, incumbent, base])?.row.kind).toBe("final");
    expect(latestClassMetricsOf([...rows.slice(0, 2), incumbent, base])?.row).toBe(incumbent);
    expect(latestClassMetricsOf([...rows.slice(0, 2), base])?.row.kind).toBe("checkpoint");
    expect(latestClassMetricsOf([rows[0], base])?.row).toBe(base);
  });
});

describe("buildClassSeries", () => {
  it("builds one sorted series per class for the chosen metric", () => {
    const series = buildClassSeries(rows, "f1");
    expect(series.map((s) => s.id).sort()).toEqual(["fraud", "refund"]);
    const refund = series.find((s) => s.id === "refund")!;
    expect(refund.points).toEqual([
      { step: 0, value: 0.4 },
      { step: 24, value: 0.6 },
      { step: 25, value: 0.8 },
    ]);
  });

  it("is empty when no row has class metrics", () => {
    expect(buildClassSeries([rows[3]], "precision")).toEqual([]);
  });
});

describe("buildMacroSeries", () => {
  it("returns precision, recall, and F1 macro lines", () => {
    const series = buildMacroSeries(rows);
    expect(series.map((s) => s.id)).toEqual(["macro-precision", "macro-recall", "macro-f1"]);
    expect(series[2].points.map((p) => p.value)).toEqual([0.4, 0.6, 0.8]);
  });
});