Open History to see the last commit
1import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6import { page, userEvent } from "vite-plus/test/browser";
7
8const state = vi.hoisted(() => ({
9 result: null as { readonly agents: ReadonlyArray<AcpRegistrySearchAgent> } | null,
10 error: null as string | null,
11 isPending: false,
12 canManage: true,
13 refresh: vi.fn(),
14 search: vi.fn(),
15 prepare: vi.fn(),
16}));
17
18vi.mock(import("../../state/server"), async (original) => {
19 const actual = await original();
20 return {
21 ...actual,
22 serverEnvironment: {
23 ...actual.serverEnvironment,
24 searchAcpRegistry: ((input: unknown) => {
25 state.search(input);
26 return "search";
27 }) as never,
28 },
29 };
30});
31vi.mock(import("../../state/session"), async (original) => ({
32 ...(await original()),
33 useEnvironmentScope: () => state.canManage,
34 readEnvironmentScope: () => state.canManage,
35}));
36vi.mock(import("../../state/query"), async (original) => ({
37 ...(await original()),
38 useEnvironmentQuery: (() => ({
39 data: state.result,
40 error: state.error,
41 isPending: state.isPending,
42 refresh: state.refresh,
43 })) as never,
44}));
45vi.mock(import("../../state/use-atom-command"), async (original) => ({
46 ...(await original()),
47 useAtomCommand: (() => state.prepare) as never,
48}));
49
50import { AcpRegistrySearchStep } from "./AcpRegistrySearchStep";
51
52const environmentId = EnvironmentId.make("remote-device");
53const gemini: AcpRegistrySearchAgent = {
54 id: "gemini",
55 name: "Gemini CLI",
56 version: "1.2.3",
57 description: "Google's agent",
58 authors: ["Google <gemini-cli@google.com>", "Contributor"],
59 license: "Apache-2.0",
60 website: "https://example.com/docs",
61 repository: "https://example.com/source",
62 icon: null,
63 distribution: "npx",
64 integrity: "registry",
65};
66
67const roots: Root[] = [];
68let root: Root;
69
70function render(options?: {
71 readonly configured?: boolean;
72 readonly onPrepared?: (agent: AcpRegistrySearchAgent) => void;
73}) {
74 return act(async () =>
75 root.render(
76 <AcpRegistrySearchStep
77 environmentId={environmentId}
78 providerInstances={
79 options?.configured
80 ? {
81 acpRegistry_gemini: {
82 driver: ProviderDriverKind.make("acpRegistry"),
83 config: { agentId: "gemini" },
84 },
85 }
86 : {}
87 }
88 onPrepared={options?.onPrepared ?? vi.fn()}
89 onManualConfiguration={vi.fn()}
90 />,
91 ),
92 );
93}
94
95const searchBox = () => page.getByRole("searchbox", { name: "Search ACP Registry" });
96const lastQuery = () =>
97 (state.search.mock.lastCall?.[0] as { input: { query: string } } | undefined)?.input.query;
98
99function deferred<T>() {
100 let resolve!: (value: T) => void;
101 const promise = new Promise<T>((done) => {
102 resolve = done;
103 });
104 return { promise, resolve };
105}
106
107beforeEach(() => {
108 state.canManage = true;
109 state.result = null;
110 state.error = null;
111 state.isPending = false;
112 state.refresh.mockReset();
113 state.search.mockReset();
114 state.prepare.mockReset().mockResolvedValue({
115 _tag: "Success",
116 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
117 });
118 const container = document.createElement("div");
119 document.body.append(container);
120 root = createRoot(container);
121 roots.push(root);
122});
123
124afterEach(async () => {
125 vi.useRealTimers();
126 for (const mounted of roots.splice(0)) await act(async () => mounted.unmount());
127 document.body.replaceChildren();
128});
129
130it("loads the catalog immediately and searches only after typing settles", async () => {
131 await render();
132 expect(state.search).toHaveBeenLastCalledWith({ environmentId, input: { query: "" } });
133
134 vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
135 await userEvent.fill(searchBox(), " Gemini ");
136 await act(async () => vi.advanceTimersByTime(200));
137 await userEvent.fill(searchBox(), " Gemini CLI ");
138 await act(async () => vi.advanceTimersByTime(299));
139 expect(lastQuery()).toBe("");
140 await act(async () => vi.advanceTimersByTime(1));
141 expect(lastQuery()).toBe("Gemini CLI");
142
143 await userEvent.clear(searchBox());
144 await act(async () => vi.advanceTimersByTime(300));
145 expect(lastQuery()).toBe("");
146});
147
148it("shows the loading, error, and empty states", async () => {
149 state.isPending = true;
150 await render();
151 await expect.element(page.getByText("Searching the registry...")).toBeVisible();
152
153 state.isPending = false;
154 state.error = "Registry unavailable.";
155 await render();
156 await expect.element(page.getByText("Registry unavailable.")).toBeVisible();
157
158 state.error = null;
159 state.result = { agents: [] };
160 await render();
161 await expect.element(page.getByText("No compatible agents found")).toBeVisible();
162});
163
164it("submits a typed query at once and refreshes when the same query is submitted again", async () => {
165 await render();
166 await userEvent.fill(searchBox(), "Codex");
167 await userEvent.keyboard("{Enter}");
168 expect(lastQuery()).toBe("Codex");
169 expect(state.refresh).not.toHaveBeenCalled();
170
171 state.result = { agents: [gemini] };
172 await render();
173 await userEvent.click(searchBox());
174 await userEvent.keyboard("{Enter}");
175 expect(state.refresh).toHaveBeenCalledOnce();
176
177 state.isPending = true;
178 await render();
179 await expect
180 .element(page.getByRole("status").filter({ hasText: "Refreshing ACP Registry results." }))
181 .toBeInTheDocument();
182 await expect.element(page.getByRole("button", { name: "Add Gemini CLI" })).toBeVisible();
183});
184
185it("prepares a result before handing it back to the wizard", async () => {
186 state.prepare.mockResolvedValueOnce({
187 _tag: "Success",
188 value: { agentId: "gemini", version: "2.0.0", distribution: "binary", prepared: true },
189 });
190 state.result = { agents: [gemini] };
191 const onPrepared = vi.fn();
192 await render({ onPrepared });
193
194 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
195
196 expect(state.prepare).toHaveBeenCalledWith({ environmentId, input: { agentId: "gemini" } });
197 await vi.waitFor(() =>
198 expect(onPrepared).toHaveBeenCalledWith({
199 ...gemini,
200 version: "2.0.0",
201 distribution: "binary",
202 }),
203 );
204});
205
206it("shows progress, locks the step while an agent prepares, and reports a failure", async () => {
207 const pending = deferred<unknown>();
208 state.prepare.mockReturnValueOnce(pending.promise);
209 state.result = { agents: [{ ...gemini, distribution: "binary" }] };
210 await render();
211
212 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
213 await expect.element(page.getByRole("button", { name: "Downloading Gemini CLI" })).toBeDisabled();
214 await expect.element(searchBox()).toBeDisabled();
215
216 await act(async () =>
217 pending.resolve({ _tag: "Failure", cause: Cause.fail(new Error("Registry download failed.")) }),
218 );
219 await expect.element(page.getByText("Registry download failed.")).toBeVisible();
220 await expect.element(page.getByRole("button", { name: "Add Gemini CLI" })).toBeEnabled();
221});
222
223it("rechecks the grant before preparing an agent", async () => {
224 state.result = { agents: [gemini] };
225 const onPrepared = vi.fn();
226 await render({ onPrepared });
227 state.canManage = false;
228
229 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
230
231 expect(state.prepare).not.toHaveBeenCalled();
232 expect(onPrepared).not.toHaveBeenCalled();
233});
234
235it("ignores prepare completion after unmount", async () => {
236 const pending = deferred<unknown>();
237 state.prepare.mockReturnValueOnce(pending.promise);
238 state.result = { agents: [gemini] };
239 const onPrepared = vi.fn();
240 await render({ onPrepared });
241
242 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
243 await act(async () => root.unmount());
244 await act(async () =>
245 pending.resolve({
246 _tag: "Success",
247 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
248 }),
249 );
250
251 expect(onPrepared).not.toHaveBeenCalled();
252});
253
254it("shows an existing registry configuration as already added", async () => {
255 state.result = { agents: [gemini] };
256 await render({ configured: true });
257 await expect
258 .element(page.getByRole("button", { name: "Already added Gemini CLI" }))
259 .toBeDisabled();
260});