Files
pi2d/web/tests/components.test.tsx
T
yuez c4d0057b0b test(web): pin the dot matrix, the shared blink clock and markSeen
sessionDot had no test at all, which is how its priority order drifted away
from the plugin. session-dot.test.ts locks the whole matrix: the parent
order (exited > error > attention > done > busy > compacting > idle), every
collision that used to be wrong, the viewed -> current overlay, the separate
child matrix (steady red while viewed, never green, never attention) and the
subagent table.

blink.test.ts covers the one shared clock: phase lockstep, ref counting
(two subscribers, one interval), idempotent unsubscribe, and
prefers-reduced-motion pinning the dots solid.

chat.tree.test.tsx grows up from "a .dot exists" to the actual classes;
sessions-store.test.ts covers markSeen; components.test.tsx covers the dead
status and the steady/current modifiers.

Also makes the current-tab overlay a positive check (idle | busy |
compacting), matching the plugin markable list: a session that has not
reported a status yet ("starting") no longer claims the blue marker.
2026-10-27 19:00:26 +08:00

164 lines
6.4 KiB
TypeScript

import { fireEvent, render, screen } from "@testing-library/react";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it, vi } from "vitest";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardSub, CardTitle } from "@/components/ui/card";
import { Modal } from "@/components/ui/dialog";
import { StatusDot } from "@/components/ui/status-dot";
describe("Button", () => {
it("renders the base class and default variant", () => {
render(<Button>Open</Button>);
const button = screen.getByRole("button", { name: "Open" });
expect(button).toHaveClass("btn");
expect(button).toHaveAttribute("type", "button");
});
it("applies variant and size classes", () => {
render(
<Button variant="primary" size="sm">
Save
</Button>,
);
expect(screen.getByRole("button", { name: "Save" })).toHaveClass("btn", "btn--primary", "btn--sm");
});
it("supports disabled state", () => {
render(<Button disabled>Stop</Button>);
expect(screen.getByRole("button", { name: "Stop" })).toBeDisabled();
});
});
describe("Card", () => {
it("renders the card surface with title/sub", () => {
render(
<Card>
<CardTitle>Server info</CardTitle>
<CardSub>hostname</CardSub>
</Card>,
);
expect(screen.getByText("Server info").closest(".card")).toBeInTheDocument();
expect(screen.getByText("Server info")).toHaveClass("card__title");
expect(screen.getByText("hostname")).toHaveClass("card__sub");
});
it("renders the hover variant", () => {
const { container } = render(<Card hover>hover</Card>);
expect(container.firstElementChild).toHaveClass("card", "card--hover");
});
});
describe("StatusDot", () => {
it.each([
["busy", "dot--busy"],
["compacting", "dot--compacting"],
["idle", "dot--idle"],
["ok", "dot--ok"],
["err", "dot--err"],
["attention", "dot--attention"],
["exited", "dot--exited"],
["dead", "dot--dead"],
] as const)("maps %s to %s", (status, expected) => {
const { container } = render(<StatusDot status={status} />);
expect(container.firstElementChild).toHaveClass("dot", expected);
});
it("applies dot--steady modifier when steady is true", () => {
const { container } = render(<StatusDot status="err" steady={true} />);
expect(container.firstElementChild).toHaveClass("dot", "dot--err", "dot--steady");
});
it("does not apply dot--steady when steady is false or omitted", () => {
const { container: c1 } = render(<StatusDot status="err" steady={false} />);
expect(c1.firstElementChild).not.toHaveClass("dot--steady");
const { container: c2 } = render(<StatusDot status="err" />);
expect(c2.firstElementChild).not.toHaveClass("dot--steady");
});
it("applies dot--current modifier when current is true", () => {
const { container } = render(<StatusDot status="busy" current={true} />);
expect(container.firstElementChild).toHaveClass("dot", "dot--busy", "dot--current");
});
it("does not apply dot--current when current is false or omitted", () => {
const { container: c1 } = render(<StatusDot status="busy" current={false} />);
expect(c1.firstElementChild).not.toHaveClass("dot--current");
const { container: c2 } = render(<StatusDot status="busy" />);
expect(c2.firstElementChild).not.toHaveClass("dot--current");
});
it("supports steady and current modifiers together", () => {
const { container } = render(<StatusDot status="busy" steady current />);
expect(container.firstElementChild).toHaveClass("dot", "dot--busy", "dot--steady", "dot--current");
});
it("preserves custom className alongside modifiers", () => {
const { container } = render(<StatusDot status="idle" steady current className="custom-dot" />);
expect(container.firstElementChild).toHaveClass("dot", "dot--idle", "dot--steady", "dot--current", "custom-dot");
});
it("exposes the state label when a title is provided", () => {
render(<StatusDot status="ok" title="Online" />);
expect(screen.getByLabelText("Online")).toHaveAttribute("title", "Online");
});
});
describe("Badge", () => {
it("maps semantic tones", () => {
const { container } = render(<Badge tone="warn">degraded</Badge>);
expect(container.firstElementChild).toHaveClass("badge", "badge--warn");
});
});
describe("Modal", () => {
function renderModal(onClick = vi.fn()) {
render(
<Modal
open
onOpenChange={() => undefined}
title="Confirm connection"
description="Pairing details"
footer={
<Button variant="primary" onClick={onClick}>
Connect now
</Button>
}
>
<p>host:port</p>
</Modal>,
);
return onClick;
}
it("nests the panel inside the backdrop so the scrim cannot intercept clicks", () => {
renderModal();
const dialog = screen.getByRole("dialog");
expect(dialog).toHaveClass("modal", "modal-content");
// Sibling content kept painting under `.modal-backdrop` (z-index 60) and
// Playwright reported "modal-backdrop intercepts pointer events".
expect(dialog.parentElement).toHaveClass("modal-backdrop");
});
it("keeps footer buttons clickable", () => {
const onClick = renderModal();
const footerButton = screen.getByRole("button", { name: "Connect now" });
expect(onClick).not.toHaveBeenCalled();
fireEvent.click(footerButton);
expect(onClick).toHaveBeenCalledTimes(1);
});
it("stacks the panel above the backdrop in app.css", () => {
const css = readFileSync(resolve(process.cwd(), "src/styles/app.css"), "utf8");
const backdrop = /\.modal-backdrop\s*{[^}]*}/.exec(css)?.[0] ?? "";
const modal = /\.modal\s*{[^}]*}/.exec(css)?.[0] ?? "";
expect(backdrop).toContain("z-index: 60");
expect(modal).toContain("position: relative");
expect(modal).toContain("z-index: 61");
});
});