inspector/clients/tui/__tests__/NotificationsTab.test.tsx
cliffhall a3bb679f1e fix: strip ANSI from TUI test frames so FORCE_COLOR cannot break assertions (#2207)
Ink writes styling as escape sequences inside the styled run, so an
accelerator underline splits the word it decorates: `<Text underline>I</Text>nfo`
reaches the frame buffer with escapes between `I` and `nfo`, and a plain
`toContain("Info")` fails against a component that is rendering correctly.
Chalk only emits color when it detects a TTY, so CI stays green and the six
failures land only on a developer whose shell exports FORCE_COLOR.

Add `__tests__/helpers/renderTui.tsx` — ink-testing-library's `render` with
`lastFrame()` and `frames` ANSI-stripped — and point all 16 renderer test
files at it. `frames` is a getter reading through to the live array, since
Ink appends to it as the component re-renders; the raw bytes stay reachable
as `stdout.lastFrame()`.

Pinning FORCE_COLOR=0 would turn the six green while hiding the class: any
future assertion on styled text has the same problem, and the suite would
stop covering the styled path at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MFp2LVH7UJST5i8bDBJghw
Signed-off-by: cliffhall <cliff@futurescale.com>
2026-09-06 20:00:15 -04:00

184 lines
5.8 KiB
TypeScript

import React from "react";
import { describe, it, expect, vi } from "vitest";
import { render } from "./helpers/renderTui";
// MUST mock ink-scroll-view: the real ScrollView renders a placeholder minimap
// in the non-TTY test env and never mounts its children. This passthrough
// renders children directly and stubs scrollBy/scrollTo/getViewportHeight.
vi.mock("ink-scroll-view", () => import("./helpers/inkScrollViewMock.js"));
import type { StderrLogEntry } from "@inspector/core/mcp/index.js";
import { NotificationsTab } from "../src/components/NotificationsTab.js";
// Ink processes stdin keypresses asynchronously — await this after stdin.write
// and after rerender() before asserting.
const tick = async () => {
// Flush several macrotask cycles so an effect -> setState -> re-render chain
// settles before assertions, even on slow/loaded CI (a single tick can race).
for (let i = 0; i < 8; i++)
await new Promise((resolve) => setTimeout(resolve, 4));
};
// Real terminal escape sequences (with the leading ESC) so ink reliably parses
// them as arrow / page keys for this component's useInput handler.
const ESC = String.fromCharCode(27);
const UP = `${ESC}[A`;
const DOWN = `${ESC}[B`;
const PAGE_UP = `${ESC}[5~`;
const PAGE_DOWN = `${ESC}[6~`;
const makeLog = (message: string): StderrLogEntry => ({
timestamp: new Date("2026-06-27T12:34:56Z"),
message,
});
describe("NotificationsTab", () => {
it("renders the empty state when there are no logs", () => {
const { lastFrame } = render(
<NotificationsTab stderrLogs={[]} width={80} height={20} />,
);
const frame = lastFrame() ?? "";
expect(frame).toContain("Console (0)");
expect(frame).toContain("No stderr output yet");
});
it("renders log entries with timestamps and messages", () => {
const logs = [makeLog("first error"), makeLog("second error")];
const { lastFrame } = render(
<NotificationsTab stderrLogs={logs} width={80} height={20} />,
);
const frame = lastFrame() ?? "";
expect(frame).toContain("Console (2)");
expect(frame).toContain("first error");
expect(frame).toContain("second error");
expect(frame).not.toContain("No stderr output yet");
});
it("invokes onCountChange with the initial log count", () => {
const onCountChange = vi.fn();
render(
<NotificationsTab
stderrLogs={[makeLog("a"), makeLog("b")]}
width={80}
height={20}
onCountChange={onCountChange}
/>,
);
expect(onCountChange).toHaveBeenCalledWith(2);
});
it("re-invokes onCountChange when the log count changes", async () => {
const onCountChange = vi.fn();
const { rerender } = render(
<NotificationsTab
stderrLogs={[makeLog("a")]}
width={80}
height={20}
onCountChange={onCountChange}
/>,
);
expect(onCountChange).toHaveBeenLastCalledWith(1);
rerender(
<NotificationsTab
stderrLogs={[makeLog("a"), makeLog("b"), makeLog("c")]}
width={80}
height={20}
onCountChange={onCountChange}
/>,
);
await tick();
expect(onCountChange).toHaveBeenLastCalledWith(3);
});
it("picks up a changed onCountChange callback via the ref effect", async () => {
const first = vi.fn();
const second = vi.fn();
const { rerender } = render(
<NotificationsTab
stderrLogs={[makeLog("a")]}
width={80}
height={20}
onCountChange={first}
/>,
);
expect(first).toHaveBeenLastCalledWith(1);
// New callback identity + new count → the ref is updated, then the
// count-change effect fires the latest callback.
rerender(
<NotificationsTab
stderrLogs={[makeLog("a"), makeLog("b")]}
width={80}
height={20}
onCountChange={second}
/>,
);
await tick();
expect(second).toHaveBeenLastCalledWith(2);
});
it("renders without an onCountChange prop", () => {
const { lastFrame } = render(
<NotificationsTab
stderrLogs={[makeLog("solo")]}
width={80}
height={20}
/>,
);
expect(lastFrame() ?? "").toContain("solo");
});
it("highlights the header and handles scroll keys when focused", async () => {
const logs = [makeLog("line one"), makeLog("line two")];
const { lastFrame, stdin } = render(
<NotificationsTab stderrLogs={logs} width={80} height={20} focused />,
);
expect(lastFrame() ?? "").toContain("Console (2)");
// Drive every useInput branch
stdin.write(UP);
await tick();
stdin.write(DOWN);
await tick();
stdin.write(PAGE_UP);
await tick();
stdin.write(PAGE_DOWN);
await tick();
stdin.write("x"); // non-handled key → else fall-through
await tick();
expect(lastFrame() ?? "").toContain("line one");
});
it("handles scroll keys with no logs (ScrollView absent → null scroll ref)", async () => {
// With an empty log list the ScrollView (and its ref) is never mounted, so
// scrollViewRef.current is null. Driving the scroll keys exercises the
// optional-chaining + `getViewportHeight() || 1` fallback paths.
const { lastFrame, stdin } = render(
<NotificationsTab stderrLogs={[]} width={80} height={20} focused />,
);
expect(lastFrame() ?? "").toContain("No stderr output yet");
stdin.write(UP);
await tick();
stdin.write(DOWN);
await tick();
stdin.write(PAGE_UP);
await tick();
stdin.write(PAGE_DOWN);
await tick();
expect(lastFrame() ?? "").toContain("No stderr output yet");
});
it("does not react to keys when not focused", async () => {
const logs = [makeLog("line one")];
const { lastFrame, stdin } = render(
<NotificationsTab stderrLogs={logs} width={80} height={20} />,
);
stdin.write(DOWN);
await tick();
expect(lastFrame() ?? "").toContain("line one");
});
});