Open History to see the last commit
1import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3import { page, userEvent } from "vite-plus/test/browser";
4
5import { act, render } from "../../../test/render";
6import { Dialog, DialogPopup } from "../ui/dialog";
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, useAcpRegistrySearch } 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
67function Step({
68 configured = false,
69 onPrepared = vi.fn(),
70}: {
71 readonly configured?: boolean;
72 readonly onPrepared?: (agent: AcpRegistrySearchAgent) => void;
73}) {
74 const registry = useAcpRegistrySearch(environmentId, true);
75 return (
76 <AcpRegistrySearchStep
77 environmentId={environmentId}
78 registry={registry}
79 providerInstances={
80 configured
81 ? {
82 acpRegistry_gemini: {
83 driver: ProviderDriverKind.make("acpRegistry"),
84 config: { agentId: "gemini" },
85 },
86 }
87 : {}
88 }
89 onPrepared={onPrepared}
90 onManualConfiguration={vi.fn()}
91 />
92 );
93}
94
95function setup() {
96 state.canManage = true;
97 state.result = null;
98 state.error = null;
99 state.isPending = false;
100 state.refresh.mockReset();
101 state.search.mockReset();
102 state.prepare.mockReset().mockResolvedValue({
103 _tag: "Success",
104 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
105 });
106}
107
108it("rechecks the grant before preparing an agent", async () => {
109 setup();
110 state.result = { agents: [gemini] };
111 const onPrepared = vi.fn();
112 await using _screen = await render(<Step onPrepared={onPrepared} />);
113 state.canManage = false;
114
115 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
116
117 expect(state.prepare).not.toHaveBeenCalled();
118 expect(onPrepared).not.toHaveBeenCalled();
119});
120
121it("bounds pasted registry searches before debounce and submission", async () => {
122 setup();
123 await using screen = await render(<Step />);
124 const searchbox = screen.getByRole("searchbox", { name: "Search ACP Registry" });
125
126 await searchbox.fill("agent".repeat(26));
127 await vi.waitFor(() => {
128 expect(state.search).toHaveBeenLastCalledWith({
129 environmentId,
130 input: { query: "agent".repeat(24) },
131 });
132 });
133 await userEvent.keyboard("{Enter}");
134 expect(state.refresh).toHaveBeenCalledTimes(1);
135 await expect.element(searchbox).toHaveValue("agent".repeat(24));
136});
137
138it("leaves out Registry agents that T3 Code supports as their own providers", async () => {
139 setup();
140 state.result = { agents: [gemini, { ...gemini, id: "codex-acp", name: "Codex" }] };
141 await using _screen = await render(<Step />);
142
143 expect(page.getByRole("button", { name: "Add Gemini CLI" }).query()).not.toBeNull();
144 expect(page.getByRole("button", { name: "Add Codex" }).query()).toBeNull();
145});
146
147it("Escape in the search clears it without closing Add provider", async () => {
148 setup();
149 const onOpenChange = vi.fn();
150 await using _screen = await render(
151 <Dialog open onOpenChange={onOpenChange}>
152 <DialogPopup>
153 <Step />
154 </DialogPopup>
155 </Dialog>,
156 );
157 const search = page.getByRole("searchbox", { name: "Search ACP Registry" });
158 await userEvent.fill(search, "gemini");
159 for (const composition of [
160 { key: "Escape", isComposing: true, keyCode: 27 },
161 { key: "Escape", isComposing: false, keyCode: 229 },
162 ]) {
163 // Safari ends the composition just before the Escape keydown.
164 await act(() => {
165 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
166 search
167 .element()
168 .dispatchEvent(
169 new KeyboardEvent("keydown", { ...composition, bubbles: true, cancelable: true }),
170 );
171 });
172 await expect.element(search).toHaveValue("gemini");
173 }
174
175 await userEvent.keyboard("{Escape}");
176 await expect.element(search).toHaveValue("");
177 await expect.element(search).toHaveFocus();
178 // Holding Escape repeats the keydown, which must not also close the dialog.
179 await act(() => {
180 search.element().dispatchEvent(
181 new KeyboardEvent("keydown", {
182 key: "Escape",
183 repeat: true,
184 bubbles: true,
185 cancelable: true,
186 }),
187 );
188 });
189 expect(onOpenChange).not.toHaveBeenCalled();
190
191 await userEvent.keyboard("{Escape}");
192 expect(onOpenChange.mock.calls.map(([open]) => open)).toEqual([false]);
193});