test(coverage): bootstrap vitest unit infra and add tests at 90%+ per file (#1245) (#1269)

* test(coverage): bootstrap vitest unit infra and add tests at 90%+ per file (#1245)

Establishes the unit-test runtime AGENTS.md mandates: a vitest "unit"
project alongside the existing storybook project, RTL + jest-dom +
happy-dom, a Mantine-aware render helper, and a 90%-line per-file
coverage gate enforced by CI. Adds 66 test files covering all 62
components under elements/groups/screens/views, plus core/mcp and
clients/web/src/utils. Total: 577 tests, 98.7% line coverage globally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(coverage): address PR #1269 review feedback

- AGENTS.md: correct coverage gate description (lines ≥ 90, statements ≥ 85,
  functions ≥ 80, branches ≥ 50) with rationale for the relaxed branch
  threshold
- InspectorView.test.tsx: document what the Math.random() mock values
  (0.1 / 0.99) control vs. STUB_SUCCESS_RATE in InspectorView.tsx
- vite.config.ts / tsconfig.test.json: drop unused `globals: true` and the
  `vitest/globals` type now that every test file imports describe/it/expect/vi
  explicitly; manual `afterEach(cleanup)` in setup.ts remains required because
  testing-library auto-cleanup relies on globalThis.afterEach

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cliff Hall 2026-05-01 15:15:32 -04:00 • committed by GitHub
commit 75f74d64c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,215 @@
import { describe, it, expect, vi } from "vitest";
import userEvent from "@testing-library/user-event";
import type {
CreateMessageRequestParams,
CreateMessageResult,
} from "@modelcontextprotocol/sdk/types.js";
import { renderWithMantine, screen } from "../../../test/renderWithMantine";
import { InlineSamplingRequest } from "./InlineSamplingRequest";
const textRequest: CreateMessageRequestParams = {
messages: [
{
role: "user",
content: { type: "text", text: "Please analyze this code." },
},
],
maxTokens: 1024,
};
const arrayContentRequest: CreateMessageRequestParams = {
messages: [
{
role: "user",
content: [
// Note: SamplingMessage content is a single block, but the component
// also handles arrays defensively. Cast to satisfy types in tests.
{ type: "text", text: "from array" },
] as unknown as CreateMessageRequestParams["messages"][number]["content"],
},
],
maxTokens: 1024,
};
const nonTextSingleRequest: CreateMessageRequestParams = {
messages: [
{
role: "user",
content: {
type: "image",
data: "abc",
mimeType: "image/png",
},
},
],
maxTokens: 1024,
};
const emptyRequest: CreateMessageRequestParams = {
messages: [],
maxTokens: 1024,
};
const textDraft: CreateMessageResult = {
role: "assistant",
model: "claude-sonnet-4-20250514",
content: { type: "text", text: "draft response" },
};
const imageDraft: CreateMessageResult = {
role: "assistant",
model: "claude-sonnet-4-20250514",
content: { type: "image", data: "abc", mimeType: "image/png" },
};
const audioDraft: CreateMessageResult = {
role: "assistant",
model: "claude-sonnet-4-20250514",
content: { type: "audio", data: "abc", mimeType: "audio/wav" },
};
const baseProps = {
queuePosition: "1 of 1",
onAutoRespond: vi.fn(),
onEditAndSend: vi.fn(),
onReject: vi.fn(),
onViewDetails: vi.fn(),
};
describe("InlineSamplingRequest", () => {
it("renders the badge, queue position, and message preview", () => {
renderWithMantine(
<InlineSamplingRequest {...baseProps} request={textRequest} />,
);
expect(screen.getByText("sampling/createMessage")).toBeInTheDocument();
expect(screen.getByText("1 of 1")).toBeInTheDocument();
expect(screen.getByText("Please analyze this code.")).toBeInTheDocument();
});
it("renders model hints when provided", () => {
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={{
...textRequest,
modelPreferences: {
hints: [{ name: "claude-opus" }, { name: "claude-sonnet" }],
},
}}
/>,
);
expect(
screen.getByText("Model hints: claude-opus, claude-sonnet"),
).toBeInTheDocument();
});
it("does not render hints when none are present", () => {
renderWithMantine(
<InlineSamplingRequest {...baseProps} request={textRequest} />,
);
expect(screen.queryByText(/^Model hints:/)).not.toBeInTheDocument();
});
it("does not render hints when hints array is empty", () => {
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={{
...textRequest,
modelPreferences: { hints: [] },
}}
/>,
);
expect(screen.queryByText(/^Model hints:/)).not.toBeInTheDocument();
});
it("renders the text draft preview when a text draft result is provided", () => {
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={textRequest}
draftResult={textDraft}
/>,
);
expect(screen.getByText("draft response")).toBeInTheDocument();
});
it("renders the image draft preview when an image draft result is provided", () => {
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={textRequest}
draftResult={imageDraft}
/>,
);
expect(screen.getByText("[Image content]")).toBeInTheDocument();
});
it("renders the audio draft preview when an audio draft result is provided", () => {
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={textRequest}
draftResult={audioDraft}
/>,
);
expect(screen.getByText("[Audio content]")).toBeInTheDocument();
});
it("handles requests with array-shaped content", () => {
renderWithMantine(
<InlineSamplingRequest {...baseProps} request={arrayContentRequest} />,
);
expect(screen.getByText("from array")).toBeInTheDocument();
});
it("renders a non-text content type label for non-text single-content messages", () => {
renderWithMantine(
<InlineSamplingRequest {...baseProps} request={nonTextSingleRequest} />,
);
expect(screen.getByText("[image]")).toBeInTheDocument();
});
it("handles requests with no messages", () => {
renderWithMantine(
<InlineSamplingRequest {...baseProps} request={emptyRequest} />,
);
expect(screen.getByText("sampling/createMessage")).toBeInTheDocument();
});
it("invokes onViewDetails when View Details is clicked", async () => {
const user = userEvent.setup();
const onViewDetails = vi.fn();
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={textRequest}
onViewDetails={onViewDetails}
/>,
);
await user.click(screen.getByRole("button", { name: "View Details" }));
expect(onViewDetails).toHaveBeenCalledTimes(1);
});
it("invokes onAutoRespond, onEditAndSend, and onReject when their buttons are clicked", async () => {
const user = userEvent.setup();
const onAutoRespond = vi.fn();
const onEditAndSend = vi.fn();
const onReject = vi.fn();
renderWithMantine(
<InlineSamplingRequest
{...baseProps}
request={textRequest}
onAutoRespond={onAutoRespond}
onEditAndSend={onEditAndSend}
onReject={onReject}
/>,
);
await user.click(screen.getByRole("button", { name: "Auto-respond" }));
await user.click(screen.getByRole("button", { name: /Edit.*Send/ }));
await user.click(screen.getByRole("button", { name: "Reject" }));
expect(onAutoRespond).toHaveBeenCalledTimes(1);
expect(onEditAndSend).toHaveBeenCalledTimes(1);
expect(onReject).toHaveBeenCalledTimes(1);
});
});