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,167 @@
import { describe, it, expect, vi } from "vitest";
import userEvent from "@testing-library/user-event";
import type {
Resource,
ResourceTemplate,
ReadResourceResult,
} from "@modelcontextprotocol/sdk/types.js";
import { renderWithMantine, screen } from "../../../test/renderWithMantine";
import { ResourcesScreen } from "./ResourcesScreen";
const resources: Resource[] = [
{ uri: "file:///x", name: "x.txt" },
{ uri: "file:///y", name: "y.txt" },
];
const templates: ResourceTemplate[] = [
{ uriTemplate: "file:///{path}", name: "files" },
];
const baseProps = {
resources,
templates,
subscriptions: [],
listChanged: false,
onRefreshList: vi.fn(),
onReadResource: vi.fn(),
onSubscribeResource: vi.fn(),
onUnsubscribeResource: vi.fn(),
};
const okResult: ReadResourceResult = {
contents: [{ uri: "file:///x", text: "embedded contents" }],
};
describe("ResourcesScreen", () => {
it("renders empty preview state when nothing is selected", () => {
renderWithMantine(<ResourcesScreen {...baseProps} />);
expect(
screen.getByText("Select a resource to preview"),
).toBeInTheDocument();
});
it("shows the read error alert when error and a resource is selected", async () => {
const user = userEvent.setup();
renderWithMantine(
<ResourcesScreen
{...baseProps}
readState={{ status: "error", error: "boom" }}
/>,
);
await user.click(screen.getByText("x.txt"));
expect(screen.getByText("Read Error")).toBeInTheDocument();
expect(screen.getByText("boom")).toBeInTheDocument();
});
it("falls back to default error when error message is missing", async () => {
const user = userEvent.setup();
renderWithMantine(
<ResourcesScreen {...baseProps} readState={{ status: "error" }} />,
);
await user.click(screen.getByText("x.txt"));
expect(screen.getByText("Failed to read resource")).toBeInTheDocument();
});
it("shows the loading state when reading", async () => {
const user = userEvent.setup();
renderWithMantine(
<ResourcesScreen {...baseProps} readState={{ status: "pending" }} />,
);
await user.click(screen.getByText("x.txt"));
expect(screen.getByText("Reading resource...")).toBeInTheDocument();
});
it("renders the preview panel when readState has a result", async () => {
const user = userEvent.setup();
renderWithMantine(
<ResourcesScreen
{...baseProps}
readState={{
status: "ok",
uri: "file:///x",
result: okResult,
}}
/>,
);
await user.click(screen.getByText("x.txt"));
expect(screen.getByText("embedded contents")).toBeInTheDocument();
});
it("synthesizes a Resource for template-expanded URIs not in the list", () => {
renderWithMantine(
<ResourcesScreen
{...baseProps}
resources={[]}
readState={{
status: "ok",
uri: "file:///synthetic",
result: { contents: [{ uri: "file:///synthetic", text: "syn" }] },
}}
/>,
);
expect(
screen.getByText("Select a resource to preview"),
).toBeInTheDocument();
});
it("renders the template panel when a template is selected", async () => {
const user = userEvent.setup();
renderWithMantine(<ResourcesScreen {...baseProps} />);
await user.click(screen.getByText("Templates (1)"));
await user.click(screen.getByText("files"));
expect(
screen.getByText("Enter a URI and click Read to preview"),
).toBeInTheDocument();
});
it("renders empty state when a resource is selected but no readState", async () => {
const user = userEvent.setup();
renderWithMantine(<ResourcesScreen {...baseProps} />);
await user.click(screen.getByText("x.txt"));
expect(screen.getByText("Click to read this resource")).toBeInTheDocument();
});
it("forwards refresh and subscribe events from the preview panel", async () => {
const user = userEvent.setup();
const onReadResource = vi.fn();
const onSubscribeResource = vi.fn();
renderWithMantine(
<ResourcesScreen
{...baseProps}
onReadResource={onReadResource}
onSubscribeResource={onSubscribeResource}
readState={{
status: "ok",
uri: "file:///x",
result: okResult,
isSubscribed: false,
}}
/>,
);
await user.click(screen.getByText("x.txt"));
await user.click(screen.getByRole("button", { name: "Refresh" }));
expect(onReadResource).toHaveBeenCalledWith("file:///x");
await user.click(screen.getByRole("button", { name: "Subscribe" }));
expect(onSubscribeResource).toHaveBeenCalledWith("file:///x");
});
it("invokes onUnsubscribeResource when already subscribed", async () => {
const user = userEvent.setup();
const onUnsubscribeResource = vi.fn();
renderWithMantine(
<ResourcesScreen
{...baseProps}
onUnsubscribeResource={onUnsubscribeResource}
readState={{
status: "ok",
uri: "file:///x",
result: okResult,
isSubscribed: true,
}}
/>,
);
await user.click(screen.getByText("x.txt"));
await user.click(screen.getByRole("button", { name: "Unsubscribe" }));
expect(onUnsubscribeResource).toHaveBeenCalledWith("file:///x");
});
});