feat(admin): AI quality preview showing per-(severity x provider) helpful-rate

v1.4.16 phase B5e: ship the `/admin/ai-quality` route that surfaces
the daily aggregator's helpful-rate per (severity x metricSourceType
x provider x prompt-version) bucket. Renders as a compact 7-column
table with severity tinted by clinical-importance colour and the
helpful-rate column tinted green/yellow/orange by 0.8/0.5
thresholds.

New endpoint `GET /api/admin/ai-quality` (requireAdmin-gated) returns
the raw `AppSettings.adminAiInsightsFeedbackSummary` blob so the
v1.4.17 ratchet that consumes the same shape for prompt-tuning can
read what the worker writes without an envelope-reshape step.

Empty state ("no summary yet" / "buckets[] is empty") rendered as a
quiet message rather than a fake zero row so the operator can tell
"no feedback" apart from "all-zeros". Loading + error banners follow
the existing host-metrics pattern.

Section wiring:
  - new `ai-quality` slug in ADMIN_SECTION_SLUGS (between
    integrations and feedback so the IA flows General → Integrations
    → AI Quality → Feedback Inbox)
  - new Sparkles icon entry in admin-shell
  - new switch case in [section]/renderer.tsx
  - EN+DE i18n keys: admin.section.ai-quality.{title,subtitle} +
    admin.aiQuality.{title,loading,loadError,empty,windowLabel,
    generatedAtLabel,col*}

Tests:
  - 5 new ai-quality-section tests (loading, empty-null, empty-buckets,
    table-render with multiple buckets, error)
  - 1 admin-sections smoke test ensures the empty branch renders +
    1 slug-list parity test

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-10 01:44:47 +02:00
1 parent 5117266756
commit 887928211b
9 files changed
+437

No files matched your search

@@ -0,0 +1,123 @@
import { describe, it, expect, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { I18nProvider } from "@/lib/i18n/context";
/**
* v1.4.16 phase B5e — coverage for the admin AI quality preview.
*
* Three branches:
* 1. loading state (Loader2 spinner + label)
* 2. empty state (no summary OR summary with empty buckets)
* 3. table-render (one row per bucket with the helpful-rate column)
*
* The fetch is replaced via a hoisted vi.fn so each test can stage a
* different response; the component reads through TanStack Query but
* we mock the hook to bypass the suspend-and-fetch dance and feed the
* data directly.
*/
const mockQueryState = vi.hoisted(() => ({
data: null as null | object,
isLoading: false,
isError: false,
}));
vi.mock("@tanstack/react-query", () => ({
useQuery: () => ({
data: mockQueryState.data,
isLoading: mockQueryState.isLoading,
isError: mockQueryState.isError,
}),
}));
vi.mock("@/lib/i18n/context", async (orig) => {
const actual = (await orig()) as Record<string, unknown>;
return actual;
});
import { AiQualitySection } from "../ai-quality-section";
function render(node: React.ReactElement) {
return renderToStaticMarkup(
<I18nProvider initialLocale="en">{node}</I18nProvider>,
);
}
describe("<AiQualitySection>", () => {
it("renders the loading state when query.isLoading", () => {
mockQueryState.data = null;
mockQueryState.isLoading = true;
mockQueryState.isError = false;
const html = render(<AiQualitySection />);
expect(html).toContain("Loading feedback summary");
});
it("renders the empty state when summary is null", () => {
mockQueryState.data = null;
mockQueryState.isLoading = false;
mockQueryState.isError = false;
const html = render(<AiQualitySection />);
expect(html).toContain("AI Quality");
expect(html).toContain("No feedback yet");
});
it("renders the empty state when buckets[] is empty", () => {
mockQueryState.data = {
generatedAt: "2026-05-09T04:00:00.000Z",
windowDays: 30,
buckets: [],
};
mockQueryState.isLoading = false;
mockQueryState.isError = false;
const html = render(<AiQualitySection />);
expect(html).toContain("No feedback yet");
});
it("renders one row per bucket with helpful-rate %", () => {
mockQueryState.data = {
generatedAt: "2026-05-09T04:00:00.000Z",
windowDays: 30,
buckets: [
{
severity: "important",
metricSourceType: "bloodPressure",
providerType: "codex",
promptVersion: "4.16.0",
helpful: 8,
notHelpful: 2,
total: 10,
helpfulRate: 0.8,
},
{
severity: "info",
metricSourceType: "weight",
providerType: "openai",
promptVersion: "4.16.0",
helpful: 1,
notHelpful: 4,
total: 5,
helpfulRate: 0.2,
},
],
};
mockQueryState.isLoading = false;
mockQueryState.isError = false;
const html = render(<AiQualitySection />);
expect(html).toContain("important");
expect(html).toContain("bloodPressure");
expect(html).toContain("codex");
expect(html).toContain("80%");
expect(html).toContain("20%");
// Two `data-slot="ai-quality-bucket"` rows.
const matches = html.match(/data-slot="ai-quality-bucket"/g);
expect(matches?.length).toBe(2);
});
it("renders a load-error banner when query.isError", () => {
mockQueryState.data = null;
mockQueryState.isLoading = false;
mockQueryState.isError = true;
const html = render(<AiQualitySection />);
expect(html).toContain("Failed to load AI quality summary");
});
});
@@ -42,6 +42,7 @@ vi.mock("@/hooks/use-auth", () => ({
import { I18nProvider } from "@/lib/i18n/context";
import { ADMIN_SECTION_SLUGS } from "../section-slugs";
import { AiQualitySection } from "../ai-quality-section";
import { ApiTokenOverviewSection } from "../api-token-overview-section";
import { BackupsSection } from "../backups-section";
import { DangerZoneSection } from "../danger-zone-section";
@@ -133,4 +134,18 @@ describe("admin sections — SSR smoke", () => {
const html = render(<DangerZoneSection />);
expect(html).toContain("Danger Zone");
});
// v1.4.16 phase B5e — admin AI quality preview. The mocked useQuery
// returns `data: null, isLoading: false` so the empty-state branch
// paints; the table-render branch is exercised by the dedicated
// ai-quality-section test.
it("<AiQualitySection> renders the empty-state when no summary exists", () => {
const html = render(<AiQualitySection />);
expect(html).toContain("AI Quality");
expect(html).toContain("No feedback yet");
});
it("admin slug list contains ai-quality (renderer parity)", () => {
expect(ADMIN_SECTION_SLUGS).toContain("ai-quality");
});
});