Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpRegistrySearchStep.test.tsx

327 lines10.5 KB

1import type { ReactElement } from "react";
2import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4​
5import { visitElements } from "../../test/reactElementTree";
6import { reactHookHarness as hooks } from "../../test/reactHookHarness";
7​
8const atoms = vi.hoisted(() => ({
9 search: Symbol("acp-search"),
10 prepare: Symbol("acp-prepare"),
11}));
12​
13const state = vi.hoisted(() => ({
14 result: null as { readonly agents: ReadonlyArray<AcpRegistrySearchAgent> } | null,
15 error: null as string | null,
16 isPending: false,
17 refresh: vi.fn(),
18 search: vi.fn(() => atoms.search),
19 prepare: vi.fn(),
20}));
21​
22const lifecycle = vi.hoisted(() => ({
23 cleanups: [] as Array<() => void>,
24}));
25​
26vi.mock("react", async (importOriginal) => {
27 const actual = await importOriginal<typeof import("react")>();
28 const { reactHookHarness } = await import("../../test/reactHookHarness");
29 return {
30 ...actual,
31 useEffect: (effect: () => void | (() => void)) => {
32 const cleanup = effect();
33 if (cleanup) lifecycle.cleanups.push(cleanup);
34 },
35 useRef: reactHookHarness.useRef,
36 useState: reactHookHarness.useState,
37 };
38});
39​
40vi.mock("react/compiler-runtime", async () => {
41 const { reactHookHarness } = await import("../../test/reactHookHarness");
42 return { c: reactHookHarness.useMemoCache };
43});
44​
45vi.mock("../../state/server", () => ({
46 serverEnvironment: {
47 searchAcpRegistry: state.search,
48 prepareAcpRegistryAgent: atoms.prepare,
49 },
50}));
51​
52vi.mock("../../state/query", () => ({
53 useEnvironmentQuery: () => ({
54 data: state.result,
55 error: state.error,
56 isPending: state.isPending,
57 refresh: state.refresh,
58 }),
59}));
60​
61vi.mock("../../state/use-atom-command", () => ({
62 useAtomCommand: () => state.prepare,
63}));
64​
65vi.mock("@t3tools/client-runtime/state/runtime", () => ({
66 isAtomCommandInterrupted: () => false,
67 squashAtomCommandFailure: () => new Error("Prepare failed."),
68}));
69​
70import { AcpRegistrySearchStep } from "./AcpRegistrySearchStep";
71​
72const environmentId = EnvironmentId.make("remote-device");
73const gemini: AcpRegistrySearchAgent = {
74 id: "gemini",
75 name: "Gemini CLI",
76 version: "1.2.3",
77 description: "Google's agent",
78 authors: ["Google <gemini-cli@google.com>", "Contributor"],
79 license: "Apache-2.0",
80 website: "https://example.com/docs",
81 repository: "https://example.com/source",
82 icon: "https://example.com/icon.png",
83 distribution: "npx",
84 integrity: "registry",
85};
86​
87function render(options?: {
88 readonly configured?: boolean;
89 readonly onPrepared?: (agent: AcpRegistrySearchAgent) => void;
90}): ReactElement<Record<string, unknown>> {
91 hooks.beginRender();
92 return AcpRegistrySearchStep({
93 environmentId,
94 providerInstances: options?.configured
95 ? {
96 acpRegistry_gemini: {
97 driver: ProviderDriverKind.make("acpRegistry"),
98 config: { agentId: "gemini" },
99 },
100 }
101 : {},
102 onPrepared: options?.onPrepared ?? vi.fn(),
103 onManualConfiguration: vi.fn(),
104 }) as ReactElement<Record<string, unknown>>;
106​
107function findByAriaLabel(
108 tree: ReactElement<Record<string, unknown>>,
109 label: string,
110): ReactElement<Record<string, unknown>> {
111 const found = visitElements(tree, (element) => element.props["aria-label"] === label);
112 expect(found).not.toBeNull();
113 return found!;
115​
116describe("AcpRegistrySearchStep", () => {
117 beforeEach(() => {
118 hooks.reset();
119 state.result = null;
120 state.error = null;
121 state.isPending = false;
122 state.refresh.mockReset();
123 state.search.mockClear();
124 state.prepare.mockReset().mockResolvedValue({
125 _tag: "Success",
126 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
127 });
128 lifecycle.cleanups = [];
129 vi.stubGlobal(
130 "fetch",
131 vi.fn(() => new Promise<Response>(() => {})),
132 );
133 });
134​
135 it("loads the full compatible catalog on open and narrows it with a search", () => {
136 const initial = render();
137 expect(state.search).toHaveBeenLastCalledWith({
138 environmentId,
139 input: { query: "" },
140 });
141​
142 const input = findByAriaLabel(initial, "Search ACP Registry");
143 expect(input.props.size).toBe("sm");
144 (input.props.onChange as ((event: { currentTarget: { value: string } }) => void) | undefined)?.(
145 { currentTarget: { value: " Gemini " } },
146 );
147 const narrowedDraft = render();
148 const form = visitElements(narrowedDraft, (element) => element.type === "form");
149 (form?.props.onSubmit as ((event: { preventDefault: () => void }) => void) | undefined)?.({
150 preventDefault: vi.fn(),
151 });
152​
153 render();
154 expect(state.search).toHaveBeenLastCalledWith({
155 environmentId,
156 input: { query: "Gemini" },
157 });
158 });
159​
160 it("renders deterministic loading, error, and empty states", () => {
161 state.isPending = true;
162 expect(
163 visitElements(render(), (element) => element.props.children === "Searching the registry..."),
164 ).not.toBeNull();
165​
166 state.isPending = false;
167 state.error = "Registry unavailable.";
168 expect(
169 visitElements(render(), (element) => element.props.children === "Registry unavailable."),
170 ).not.toBeNull();
171​
172 state.error = null;
173 state.result = { agents: [] };
174 expect(
175 visitElements(render(), (element) => element.props.children === "No compatible agents found"),
176 ).not.toBeNull();
177 });
178​
179 it("keeps same-query refreshes visible and announced while retaining results", () => {
180 const first = render();
181 const suggestion = visitElements(first, (element) => element.props.children === "Codex");
182 (suggestion?.props.onClick as (() => void) | undefined)?.();
183​
184 state.result = { agents: [gemini] };
185 const resultTree = render();
186 expect(state.search).toHaveBeenCalledWith({
187 environmentId,
188 input: { query: "Codex" },
189 });
190 const form = visitElements(resultTree, (element) => element.type === "form");
191 (form?.props.onSubmit as ((event: { preventDefault: () => void }) => void) | undefined)?.({
192 preventDefault: vi.fn(),
193 });
194 expect(state.refresh).toHaveBeenCalledOnce();
195​
196 state.isPending = true;
197 const refreshingTree = render();
198 expect(
199 visitElements(
200 refreshingTree,
201 (element) => element.props.children === "Refreshing registry results...",
202 ),
203 ).not.toBeNull();
204 expect(
205 visitElements(
206 refreshingTree,
207 (element) =>
208 element.props.role === "status" &&
209 element.props.children === "Refreshing ACP Registry results.",
210 ),
211 ).not.toBeNull();
212 expect(findByAriaLabel(refreshingTree, "Add Gemini CLI")).not.toBeNull();
213 });
214​
215 it("prepares a result before handing it back to the wizard", async () => {
216 state.prepare.mockResolvedValueOnce({
217 _tag: "Success",
218 value: { agentId: "gemini", version: "2.0.0", distribution: "binary", prepared: true },
219 });
220 state.result = { agents: [gemini] };
221 const onPrepared = vi.fn();
222 const tree = render({ onPrepared });
223​
224 const add = findByAriaLabel(tree, "Add Gemini CLI");
225 (add.props.onClick as (() => void) | undefined)?.();
226 await Promise.resolve();
227 await Promise.resolve();
228​
229 expect(state.prepare).toHaveBeenCalledWith({
230 environmentId,
231 input: { agentId: "gemini" },
232 });
233 expect(onPrepared).toHaveBeenCalledWith({
234 ...gemini,
235 version: "2.0.0",
236 distribution: "binary",
237 });
238 });
239​
240 it("ignores a stale prepare completion", async () => {
241 let resolveFirst!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
242 let resolveSecond!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
243 state.prepare
244 .mockImplementationOnce(
245 () =>
246 new Promise((resolve) => {
247 resolveFirst = resolve;
248 }),
249 )
250 .mockImplementationOnce(
251 () =>
252 new Promise((resolve) => {
253 resolveSecond = resolve;
254 }),
255 );
256 state.result = { agents: [gemini] };
257 const onPrepared = vi.fn();
258 const tree = render({ onPrepared });
259 const add = findByAriaLabel(tree, "Add Gemini CLI");
260​
261 (add.props.onClick as (() => void) | undefined)?.();
262 (add.props.onClick as (() => void) | undefined)?.();
263 resolveFirst({ _tag: "Success", value: {} });
264 await Promise.resolve();
265 expect(onPrepared).not.toHaveBeenCalled();
266​
267 resolveSecond({ _tag: "Success", value: {} });
268 await Promise.resolve();
269 expect(onPrepared).toHaveBeenCalledOnce();
270 });
271​
272 it("ignores prepare completion after unmount", async () => {
273 let resolvePrepare!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
274 state.prepare.mockImplementationOnce(
275 () =>
276 new Promise((resolve) => {
277 resolvePrepare = resolve;
278 }),
279 );
280 state.result = { agents: [gemini] };
281 const onPrepared = vi.fn();
282 const tree = render({ onPrepared });
283​
284 const add = findByAriaLabel(tree, "Add Gemini CLI");
285 (add.props.onClick as (() => void) | undefined)?.();
286 for (const cleanup of lifecycle.cleanups) cleanup();
287 resolvePrepare({ _tag: "Success", value: {} });
288 await Promise.resolve();
289​
290 expect(onPrepared).not.toHaveBeenCalled();
291 });
292​
293 it("renders existing registry configuration as already added", () => {
294 state.result = { agents: [gemini] };
295 const tree = render({ configured: true });
296 const added = findByAriaLabel(tree, "Already added Gemini CLI");
297​
298 expect(added.props.disabled).toBe(true);
299 });
300​
301 it("shows catalog metadata, uniquely named reference links, and a local icon fallback", () => {
302 state.result = { agents: [gemini] };
303 const tree = render();
304​
305 // Author emails are stripped, registry-integrity gets no marker, and the
306 // version renders beside the name instead of inside the meta line.
307 for (const item of ["Google +1", "v1.2.3", "npx", "Apache-2.0"]) {
308 expect(visitElements(tree, (element) => element.props.children === item)).not.toBeNull();
309 }
310 for (const absent of ["Registry", "✓ checksum"]) {
311 expect(visitElements(tree, (element) => element.props.children === absent)).toBeNull();
312 }
313 expect(
314 visitElements(
315 tree,
316 (element) => element.props["aria-label"] === "Open documentation for Gemini CLI (gemini)",
317 ),
318 ).not.toBeNull();
319 expect(
320 visitElements(
321 tree,
322 (element) => element.props["aria-label"] === "Open source for Gemini CLI (gemini)",
323 ),
324 ).not.toBeNull();
325 expect(visitElements(tree, (element) => element.type === "img")).toBeNull();
326 });
327});