inspector/clients/tui/__tests__/AuthTab.test.tsx
Cliff Hall f43fafc08b
test(tui): extend coverage gate to Ink components + App.tsx (closes #1501) (#1538)
* test(tui): cover useSelectableList hook + leaf Ink components (#1501)

Add ink-testing-library and renderer-based tests for useSelectableList,
SelectableItem, and Tabs. Widen the vitest include glob to .test.tsx and
lift these files from the interim React-surface coverage exclusion. The
blanket src/components/**/*.tsx exclude is expanded into an explicit
per-file list so remaining components can be lifted incrementally.

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

* test(tui): add ink-scroll-view passthrough mock for renderer tests (#1501)

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

* test(tui): add ink-form passthrough mock for modal renderer tests (#1501)

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

* test(tui): cover all Ink tab + modal components via renderer tests (#1501)

Add ink-testing-library tests for the 12 Ink components (InfoTab, AuthTab,
ResourcesTab, PromptsTab, ToolsTab, NotificationsTab, HistoryTab,
RequestsTab, and the Tool/Resource/Prompt test modals + DetailsModal),
mounting them through the ink-scroll-view / ink-form passthrough doubles
and driving keypresses via stdin. Modal components render position:absolute
(empty frame under the test renderer) so their tests assert on behavior —
injected InspectorClient fakes and onClose — while the passthrough
ScrollView still mounts the inner JSX for coverage.

Lift these 12 components from the interim React-surface exclusion in
vitest.config.ts; only App.tsx remains excluded. All files clear the
uniform 90/90/90/90 per-file gate.

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

* test(tui): add App.tsx renderer-test harness + foundation tests (#1501)

Mock the full @inspector/core surface (InspectorClient, the 7 managed-state
classes, the 8 react hooks, transport + auth) behind a controllable harness,
plus the ink-scroll-view/ink-form passthroughs, so App renders under
ink-testing-library and its keyboard state machine can be driven via stdin.
Concurrent ink mounts share raw-mode stdin, so each render is unmounted in
afterEach. Foundation covers server-list nav, tab accelerators, connect/
disconnect, OAuth-tab gating, and connected-status rendering (~50% baseline).

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

* test(tui): cover App.tsx via mocked-core renderer tests; lift final exclusion (#1501)

Extend App.test.tsx to 67 tests driving the keyboard state machine, tab
content, modals, OAuth flows, and connect/disconnect across the controllable
@inspector/core mock. App.tsx now clears the 90/90/90/90 per-file gate
(94.6/90.6/94.5/96.6).

Remove the last React-surface exclusion (src/App.tsx) from
clients/tui/vitest.config.ts. The whole clients/tui/src tree is now under
the uniform per-file coverage gate with no React-surface exclusions,
completing the interim-scope lift from #1484.

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

* test(tui): raise stdout max-listeners in App test to silence warning (#1501)

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

* test(tui): raise App.tsx branch coverage >90 and de-flake coverage runs (#1501)

Add targeted App.tsx tests for the OAuth handlers' non-Error throw arms
(quick onCallback, guided start/advance, run-to-completion) and a re-entrant
quick-auth guard, lifting App.tsx branch coverage 90.6% -> 91.48% for a
safer margin above the 90 gate.

Harden the App renderer tests against the heavier v8 coverage instrumentation,
where React+ink's multi-macrotask render scheduling races fixed ticks:
- press() now settles two ticks per key so focus/tab changes flush before the
  next key is routed (ink re-subscribes useInput on re-render);
- expectFrame()/waitUntil() poll for frame content and async flow state
  (callbackServer opts) instead of asserting after a single fixed tick;
- ResourcesTab's content-clear test polls for fetched content likewise.

Plain 'npm test' (the CI path) was already stable; this makes the local
coverage gate stable too (13/13 clean runs vs ~2/8 flaky before).

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

* test(tui): de-flake component renderer tests under slow CI (#1501)

CI flaked on AuthTab's 'renders quick auth results' under the plain test run:
its oauthFlowState is populated by a post-mount useEffect reading the
inspectorClient, and a single-macrotask tick can race that effect -> setState
-> re-render chain on a slow/loaded CI runner.

Make each component test file's tick() flush several macrotask cycles so such
chains settle deterministically before assertions (no test relies on precise
tick timing — no fake timers / debounce). App.test.tsx already polls via
expectFrame/waitUntil and passed CI, so it's unchanged.

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

* test(tui): address review — shared setupFile for max-listeners, consume-once ink-form global (#1501)

Review follow-ups on #1538 (both non-blocking cleanups):

- Hoist process.std{out,in}.setMaxListeners into a shared vitest setupFile
  (__tests__/setup.ts) wired via setupFiles, so every renderer test file
  inherits it and the MaxListenersExceededWarning is silenced across the
  whole suite — not just App.test.tsx. Remove the per-file call.
- Make the ink-form double consume (clear) __INK_FORM_SUBMIT_VALUE__ on
  submit, eliminating the latent cross-test coupling on the shared global
  regardless of how the modal test files are organized.

No production code changed; suite + coverage gate stay green (App.tsx
94.8/91.5/94.5/96.6), and the warning no longer prints.

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

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 08:19:12 -04:00

622 lines
19 KiB
TypeScript

import React from "react";
import { describe, it, expect, vi } from "vitest";
import { render } from "ink-testing-library";
import {
EMPTY_OAUTH_FLOW_STATE,
type OAuthFlowState,
} from "@inspector/core/auth/index.js";
import type { InspectorClient } from "@inspector/core/mcp/index.js";
// 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 { AuthTab } from "../src/components/AuthTab.js";
// Ink processes stdin keypresses asynchronously — await this after stdin.write.
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));
};
const ESC = String.fromCharCode(27);
const UP = `${ESC}[A`;
const DOWN = `${ESC}[B`;
const LEFT = `${ESC}[D`;
const RIGHT = `${ESC}[C`;
const PAGE_UP = `${ESC}[5~`;
const PAGE_DOWN = `${ESC}[6~`;
/** Minimal fake InspectorClient that only implements the surface AuthTab uses. */
function makeClient(state?: OAuthFlowState) {
const listeners = new Map<string, Set<() => void>>();
const client = {
getOAuthFlowState: () => state,
addEventListener: (event: string, fn: () => void) => {
if (!listeners.has(event)) listeners.set(event, new Set());
listeners.get(event)!.add(fn);
},
removeEventListener: (event: string, fn: () => void) => {
listeners.get(event)?.delete(fn);
},
};
const fire = (event: string) => {
listeners.get(event)?.forEach((fn) => fn());
};
return {
client: client as unknown as InspectorClient,
fire,
listeners,
};
}
const flow = (over: Partial<OAuthFlowState>): OAuthFlowState => ({
...EMPTY_OAUTH_FLOW_STATE,
...over,
});
// A state at "complete" with every detail field populated so getStepDetails
// returns a non-null value for every step (all rendered as completed).
const completeState = flow({
execution: "guided",
oauthStep: "complete",
resourceMetadata: {
resource: "https://api.example.com",
} as OAuthFlowState["resourceMetadata"],
oauthMetadata: {
issuer: "https://issuer.example.com",
} as OAuthFlowState["oauthMetadata"],
oauthClientInfo: {
client_id: "abc123",
} as OAuthFlowState["oauthClientInfo"],
authorizationUrl: new URL("https://auth.example.com/authorize?x=1"),
authorizationCode: "code-abcdef1234567890",
oauthTokens: {
access_token: "tok-abcdefghijklmnopqrstuvwxyz",
token_type: "Bearer",
},
});
const baseProps = {
serverName: "srv" as string | null,
serverConfig: null,
width: 120,
height: 30,
isOAuthCapable: true,
selectedAction: "guided" as "guided" | "quick" | "clear",
onSelectedActionChange: vi.fn(),
onQuickAuth: vi.fn(async () => {}),
onGuidedStart: vi.fn(async () => {}),
onGuidedAdvance: vi.fn(async () => {}),
onRunGuidedToCompletion: vi.fn(async () => {}),
onClearOAuth: vi.fn(),
};
describe("AuthTab", () => {
it("renders the placeholder when there is no server", () => {
const { lastFrame } = render(
<AuthTab
{...baseProps}
serverName={null}
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
/>,
);
expect(lastFrame() ?? "").toContain("Select an OAuth-capable server");
});
it("renders the placeholder when the server is not OAuth-capable", () => {
const { lastFrame } = render(
<AuthTab
{...baseProps}
isOAuthCapable={false}
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
/>,
);
expect(lastFrame() ?? "").toContain("Select an OAuth-capable server");
});
it("renders the guided action bar, hint, and progress (unfocused)", async () => {
const { client } = makeClient(completeState);
// tall enough that no step detail is clipped by the fixed-height Box
const { lastFrame } = render(
<AuthTab
{...baseProps}
height={80}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
const frame = lastFrame() ?? "";
expect(frame).toContain("Authentication");
expect(frame).toContain("uided Auth");
expect(frame).toContain("uick Auth");
expect(frame).toContain("Clear OAuth");
expect(frame).toContain("Press [Space] to advance one step");
expect(frame).toContain("Press [Enter] to run guided auth to completion");
expect(frame).toContain("Guided OAuth Flow Progress");
// every step label, all completed (✓)
expect(frame).toContain("Metadata Discovery");
expect(frame).toContain("Client Registration");
expect(frame).toContain("Preparing Authorization");
expect(frame).toContain("Request Authorization Code");
expect(frame).toContain("Token Request");
expect(frame).toContain("Authentication Complete");
expect(frame).toContain("✓");
// completed detail strings from getStepDetails
expect(frame).toContain("Resource:");
expect(frame).toContain("OAuth:");
expect(frame).toContain("Code received:");
expect(frame).toContain("Exchanging code for tokens...");
expect(frame).toContain("Tokens: access_token=");
// no focused footer
expect(frame).not.toContain("select, G/Q/S or Enter run");
});
it("renders an in-progress step (cyan →) and not-started steps (○)", async () => {
const midState = flow({
execution: "guided",
oauthStep: "client_registration",
oauthClientInfo: {
client_id: "mid-client",
} as OAuthFlowState["oauthClientInfo"],
});
const { client } = makeClient(midState);
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
const frame = lastFrame() ?? "";
expect(frame).toContain("(in progress)");
expect(frame).toContain("→");
expect(frame).toContain("○");
// in-progress detail (client_registration → oauthClientInfo JSON)
expect(frame).toContain("mid-client");
});
it("renders the 'authorization URL opened' block when awaiting an auth code", async () => {
const awaitingState = flow({
execution: "guided",
oauthStep: "authorization_code",
authorizationUrl: new URL("https://auth.example.com/go?code=here"),
});
const { client } = makeClient(awaitingState);
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
const frame = lastFrame() ?? "";
expect(frame).toContain("Authorization URL opened in browser");
expect(frame).toContain("auth.example.com/go");
expect(frame).toContain("Complete authorization in the browser");
});
it("covers metadata details when only the resource metadata is present", async () => {
const resourceOnly = flow({
oauthStep: "complete",
resourceMetadata: {
resource: "https://only-resource.example.com",
} as OAuthFlowState["resourceMetadata"],
});
const { client } = makeClient(resourceOnly);
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
const frame = lastFrame() ?? "";
expect(frame).toContain("Resource:");
expect(frame).not.toContain("OAuth: {");
});
it("covers metadata details when only the oauth metadata is present", async () => {
const oauthOnly = flow({
oauthStep: "complete",
oauthMetadata: {
issuer: "https://only-issuer.example.com",
} as OAuthFlowState["oauthMetadata"],
});
const { client } = makeClient(oauthOnly);
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
expect(lastFrame() ?? "").toContain("OAuth:");
});
it("renders guided progress with no details when the flow state is empty", () => {
const { client } = makeClient(flow({}));
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
const frame = lastFrame() ?? "";
expect(frame).toContain("Guided OAuth Flow Progress");
expect(frame).not.toContain("Resource:");
});
it("renders guided progress with no inspector client (no flow state)", () => {
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
/>,
);
const frame = lastFrame() ?? "";
expect(frame).toContain("Guided OAuth Flow Progress");
expect(frame).toContain("○");
});
it("renders the quick hint and 'Authenticating...' status", () => {
const { client } = makeClient(flow({ execution: "quick" }));
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="quick"
inspectorClient={client}
oauthStatus="authenticating"
oauthMessage={null}
/>,
);
const frame = lastFrame() ?? "";
expect(frame).toContain("Press [Enter] to run quick auth");
expect(frame).toContain("Authenticating...");
});
it("renders the quick error message", () => {
const { client } = makeClient(flow({ execution: "quick" }));
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="quick"
inspectorClient={client}
oauthStatus="error"
oauthMessage="Something went wrong"
/>,
);
expect(lastFrame() ?? "").toContain("Something went wrong");
});
it("renders quick auth results with client info and tokens", async () => {
const quickSuccess = flow({
execution: "quick",
oauthClientInfo: {
client_id: "quick-client",
} as OAuthFlowState["oauthClientInfo"],
oauthTokens: {
access_token: "quick-token-abcdefghijklmnop",
token_type: "Bearer",
},
});
const { client } = makeClient(quickSuccess);
const { lastFrame } = render(
<AuthTab
{...baseProps}
selectedAction="quick"
inspectorClient={client}
oauthStatus="success"
oauthMessage={null}
/>,
);
await tick();
const frame = lastFrame() ?? "";
expect(frame).toContain("Quick Auth Results");
expect(frame).toContain("quick-client");
expect(frame).toContain("Access Token:");
expect(frame).toContain("quick-token-abcdefgh");
});
it("renders the clear hint and the confirmation after pressing Enter", async () => {
const onClearOAuth = vi.fn();
const { client } = makeClient(flow({}));
const { lastFrame, stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="clear"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onClearOAuth={onClearOAuth}
/>,
);
expect(lastFrame() ?? "").toContain("Press [Enter] to clear OAuth state");
// a leading no-op key absorbs any dropped first keypress
stdin.write("x");
await tick();
stdin.write("\r");
await tick();
await tick();
expect(onClearOAuth).toHaveBeenCalled();
expect(lastFrame() ?? "").toContain("OAuth state cleared.");
});
it("shows the focused footer and header highlight when focused", () => {
const { client } = makeClient(completeState);
const { lastFrame } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
expect(lastFrame() ?? "").toContain("select, G/Q/S or Enter run");
});
it("selects actions via G/Q/S keys when focused", async () => {
const onSelectedActionChange = vi.fn();
const { client } = makeClient(flow({}));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onSelectedActionChange={onSelectedActionChange}
/>,
);
stdin.write("g");
await tick();
stdin.write("q");
await tick();
stdin.write("s");
await tick();
expect(onSelectedActionChange).toHaveBeenCalledWith("guided");
expect(onSelectedActionChange).toHaveBeenCalledWith("quick");
expect(onSelectedActionChange).toHaveBeenCalledWith("clear");
});
it("cycles selection with left/right arrows from 'guided'", async () => {
const onSelectedActionChange = vi.fn();
const { client } = makeClient(flow({}));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onSelectedActionChange={onSelectedActionChange}
/>,
);
stdin.write(LEFT);
await tick();
stdin.write(RIGHT);
await tick();
expect(onSelectedActionChange).toHaveBeenCalledWith("clear");
expect(onSelectedActionChange).toHaveBeenCalledWith("quick");
});
it("cycles selection with left/right arrows from 'quick'", async () => {
const onSelectedActionChange = vi.fn();
const { client } = makeClient(flow({ execution: "quick" }));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="quick"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onSelectedActionChange={onSelectedActionChange}
/>,
);
stdin.write(LEFT);
await tick();
stdin.write(RIGHT);
await tick();
expect(onSelectedActionChange).toHaveBeenCalledWith("guided");
expect(onSelectedActionChange).toHaveBeenCalledWith("clear");
});
it("cycles selection with left/right arrows from 'clear'", async () => {
const onSelectedActionChange = vi.fn();
const { client } = makeClient(flow({}));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="clear"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onSelectedActionChange={onSelectedActionChange}
/>,
);
stdin.write(LEFT);
await tick();
stdin.write(RIGHT);
await tick();
expect(onSelectedActionChange).toHaveBeenCalledWith("quick");
expect(onSelectedActionChange).toHaveBeenCalledWith("guided");
});
it("scrolls with up/down/pageUp/pageDown when focused", async () => {
const { client } = makeClient(completeState);
const { lastFrame, stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
stdin.write(UP);
await tick();
stdin.write(DOWN);
await tick();
stdin.write(PAGE_UP);
await tick();
stdin.write(PAGE_DOWN);
await tick();
// still rendered (scroll stubs are no-ops)
expect(lastFrame() ?? "").toContain("Guided OAuth Flow Progress");
});
it("runs guided to completion on Enter when 'guided' is selected", async () => {
const onRunGuidedToCompletion = vi.fn(async () => {});
const { client } = makeClient(flow({}));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onRunGuidedToCompletion={onRunGuidedToCompletion}
/>,
);
// a leading no-op key absorbs any dropped first keypress
stdin.write("x");
await tick();
stdin.write("\r");
await tick();
expect(onRunGuidedToCompletion).toHaveBeenCalled();
});
it("runs quick auth on Enter when 'quick' is selected", async () => {
const onQuickAuth = vi.fn(async () => {});
const { client } = makeClient(flow({ execution: "quick" }));
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="quick"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onQuickAuth={onQuickAuth}
/>,
);
stdin.write("x");
await tick();
stdin.write("\r");
await tick();
expect(onQuickAuth).toHaveBeenCalled();
});
it("advances guided one step on Space (start then advance)", async () => {
const onGuidedStart = vi.fn(async () => {});
const onGuidedAdvance = vi.fn(async () => {});
// mid-flow state so needsAuthCode/isComplete are both false on advance
const { client } = makeClient(
flow({ execution: "guided", oauthStep: "client_registration" }),
);
const { stdin } = render(
<AuthTab
{...baseProps}
focused
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
onGuidedStart={onGuidedStart}
onGuidedAdvance={onGuidedAdvance}
/>,
);
// first space starts the flow
stdin.write(" ");
await tick();
// second space advances one step
stdin.write(" ");
await tick();
// third space (in case the first was dropped) keeps both reachable
stdin.write(" ");
await tick();
expect(onGuidedStart).toHaveBeenCalled();
expect(onGuidedAdvance).toHaveBeenCalled();
});
it("does not act on input when not OAuth-capable but focused", async () => {
const onSelectedActionChange = vi.fn();
const { stdin, lastFrame } = render(
<AuthTab
{...baseProps}
focused
isOAuthCapable={false}
selectedAction="guided"
inspectorClient={null}
oauthStatus="idle"
oauthMessage={null}
onSelectedActionChange={onSelectedActionChange}
/>,
);
stdin.write("g");
await tick();
expect(onSelectedActionChange).not.toHaveBeenCalled();
expect(lastFrame() ?? "").toContain("Select an OAuth-capable server");
});
it("subscribes to oauth events and refreshes when they fire", async () => {
const { client, fire, listeners } = makeClient(completeState);
const { unmount } = render(
<AuthTab
{...baseProps}
selectedAction="guided"
inspectorClient={client}
oauthStatus="idle"
oauthMessage={null}
/>,
);
await tick();
expect(listeners.get("oauthStepChange")?.size).toBe(1);
expect(listeners.get("oauthComplete")?.size).toBe(1);
// firing the listeners runs the update() callback
fire("oauthStepChange");
fire("oauthComplete");
await tick();
// unmount runs the cleanup (removeEventListener)
unmount();
expect(listeners.get("oauthStepChange")?.size).toBe(0);
expect(listeners.get("oauthComplete")?.size).toBe(0);
});
});