Skip to content

The platform for continuously improving AI agents.

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

import type { Cell, DatasetAnalytics } from "@/openapi";
import { CapabilityField, CellChecks, CheckChips } from "./cell-checks";

afterEach(cleanup);

const check = (id: string, status: string, title = id) => ({
  id,
  metrics: id === "targets.class_balance" ? { families: ["fraud"] } : {},
  status,
  summary: `${title} summary`,
  title,
});

const report = {
  checks: [
    check("targets.class_balance", "warning", "Class imbalance"),
    check("leakage.label_in_input", "warning", "Label named in the input"),
    check("targets.validity", "error", "Target validity"),
    check("inputs.missing", "pass", "Missing input values"),
    check("leakage.cross_split", "not_checked", "Duplicates and split leakage"),
    check("inputs.availability", "not_checked", "Feature availability"),
  ],
  families: [
    {
      counts: [1000, 1],
      id: "fraud",
      labels: ["No", "Yes"],
      majorityBaseline: 1000 / 1001,
      question: "Fraud?",
      rows: 1001,
    },
  ],
  fingerprint: "abc",
  rowsScanned: 1001,
  status: "completed",
  version: 3,
} as unknown as DatasetAnalytics;

it("shows errors first, folds the rest into a count and counts what was not checked", () => {
  render(<CheckChips report={report} />);
  const chips = screen.getAllByText(/Target validity|Class imbalance|\+1|not checked/);
  expect(chips.map((chip) => chip.textContent)).toEqual([
    "Target validity",
    "Class imbalance",
    "+1",
    "2 not checked",
  ]);
});

it("names what always answering the majority label scores, and lists the other checks", () => {
  render(<CellChecks report={report} />);
  expect(screen.getByText("Accuracy alone is misleading here")).toBeTruthy();
  expect(screen.getByText(/scores 99\.90% accuracy and misses all 1 “Yes” row\./)).toBeTruthy();
  expect(screen.getByText("Label named in the input summary")).toBeTruthy();
  fireEvent.click(screen.getByRole("button", { name: /2 not checked/ }));
  expect(screen.getByText("Feature availability summary")).toBeTruthy();
  expect(screen.getByRole("button", { name: /1 passed/ }).getAttribute("aria-expanded")).toBe(
    "false"
  );
});

it("offers the other capabilities in rank order, and removal", () => {
  const onChange = vi.fn();
  render(
    <CapabilityField
      cell={{ capabilityReport: { ok: false, reason: "No tool calls match" } } as unknown as Cell}
      current="triage"
      name="Triage"
      onChange={onChange}
      ranked={[
        { capability_id: "refunds", name: "Refunds", score: 0.9 },
        { capability_id: "triage", name: "Triage", score: 0.4 },
        { capability_id: "billing", name: "Billing", score: 0.1 },
      ]}
    />
  );
  expect(screen.getByText("Does not match Triage")).toBeTruthy();
  fireEvent.pointerDown(screen.getByRole("button", { name: "Change capability" }), {
    button: 0,
    ctrlKey: false,
  });
  const items = screen.getAllByRole("menuitem").map((item) => item.textContent);
  expect(items).toEqual(["Refunds90%", "Billing10%", "Remove capability"]);
  fireEvent.click(screen.getByRole("menuitem", { name: "Remove capability" }));
  expect(onChange).toHaveBeenCalledWith(null);
});