Open History to see the last commit
1import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4import { visitElements } from "../../test/reactElementTree";
5import { reactHookHarness as hooks } from "../../test/reactHookHarness";
6
7const settingsHooks = vi.hoisted(() => ({
8 read: vi.fn(() => ({ providerInstances: {} })),
9 mutate: vi.fn(),
10 useMutation: vi.fn(),
11}));
12
13vi.mock("react", async (importOriginal) => {
14 const actual = await importOriginal<typeof import("react")>();
15 const { reactHookHarness } = await import("../../test/reactHookHarness");
16 return {
17 ...actual,
18 useMemo: reactHookHarness.useMemo,
19 useState: reactHookHarness.useState,
20 };
21});
22
23vi.mock("react/compiler-runtime", async () => {
24 const { reactHookHarness } = await import("../../test/reactHookHarness");
25 return { c: reactHookHarness.useMemoCache };
26});
27
28vi.mock("@t3tools/client-runtime/state/runtime", async (importOriginal) => ({
29 ...(await importOriginal<typeof import("@t3tools/client-runtime/state/runtime")>()),
30 squashAtomCommandFailure: () => new Error("The settings update failed."),
31}));
32
33vi.mock("../../hooks/useSettings", () => ({
34 useEnvironmentSettings: settingsHooks.read,
35 usePersistEnvironmentProviderInstanceMutation: settingsHooks.useMutation,
36}));
37
38import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
39
40const remoteEnvironmentId = EnvironmentId.make("remote-device");
41const preparedAgent: AcpRegistrySearchAgent = {
42 id: "kilo",
43 name: "Kilo Code",
44 version: "4.2.0",
45 description: "Kilo ACP agent",
46 authors: ["Kilo"],
47 license: "Apache-2.0",
48 website: null,
49 repository: null,
50 icon: "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
51 distribution: "binary",
52 integrity: "sha256",
53};
54
55function render(onOpenChange = vi.fn()) {
56 hooks.beginRender();
57 return AddProviderInstanceDialog({
58 open: true,
59 environmentId: remoteEnvironmentId,
60 environmentLabel: "Remote device",
61 onOpenChange,
62 });
63}
64
65function findByChildren(tree: ReturnType<typeof render>, children: string) {
66 const result = visitElements(tree, (element) => element.props.children === children);
67 expect(result).not.toBeNull();
68 return result!;
69}
70
71async function selectPreparedAcp() {
72 const searchStep = render();
73 const search = visitElements(
74 searchStep,
75 (element) =>
76 typeof element.type === "function" && element.type.name === "AcpRegistrySearchStep",
77 );
78 expect(search).not.toBeNull();
79 (search?.props.onPrepared as ((agent: AcpRegistrySearchAgent) => void) | undefined)?.(
80 preparedAgent,
81 );
82}
83
84describe("AddProviderInstanceDialog environment routing", () => {
85 beforeEach(() => {
86 hooks.reset();
87 settingsHooks.read.mockReset().mockReturnValue({ providerInstances: {} });
88 settingsHooks.mutate.mockReset().mockResolvedValue({ _tag: "Success", value: {} });
89 settingsHooks.useMutation.mockReset().mockReturnValue(settingsHooks.mutate);
90 });
91
92 it("creates a provider with its default identity without typing", async () => {
93 let tree = render();
94 const group = visitElements(
95 tree,
96 (element) => element.props["aria-labelledby"] === "add-instance-driver-label",
97 );
98 (group!.props.onValueChange as (value: string) => void)("grok");
99 tree = render();
100 (findByChildren(tree, "Next").props.onClick as () => void)();
101 tree = render();
102 (findByChildren(tree, "Next").props.onClick as () => void)();
103 tree = render();
104 (findByChildren(tree, "Add instance").props.onClick as () => void)();
105 await Promise.resolve();
106 expect(settingsHooks.mutate).toHaveBeenCalledWith({
107 operation: "create",
108 instanceId: "grok",
109 instance: { driver: "grok", enabled: true, displayName: "Grok" },
110 });
111 });
112
113 it("chooses an unused identity for another account without replacing configured instances", async () => {
114 settingsHooks.read.mockReturnValue({
115 providerInstances: {
116 codex_2: { driver: "codex", enabled: false },
117 },
118 });
119 let tree = render();
120 // Codex offers ChatGPT sign-in first; manual setup keeps the existing CLI flow.
121 (findByChildren(tree, "Configure manually").props.onClick as () => void)();
122 tree = render();
123 (findByChildren(tree, "Next").props.onClick as () => void)();
124 tree = render();
125 (findByChildren(tree, "Add instance").props.onClick as () => void)();
126 await Promise.resolve();
127 expect(settingsHooks.mutate).toHaveBeenCalledWith({
128 operation: "create",
129 instanceId: "codex_3",
130 instance: {
131 driver: "codex",
132 enabled: true,
133 displayName: "Codex",
134 config: { setupMode: "existing" },
135 },
136 });
137 });
138
139 it("reads and writes settings through the supplied environment", () => {
140 render();
141
142 expect(settingsHooks.read).toHaveBeenCalledWith(remoteEnvironmentId);
143 expect(settingsHooks.useMutation).toHaveBeenCalledWith(remoteEnvironmentId);
144 });
145
146 it("awaits an atomic create before closing and retains prepared ACP metadata", async () => {
147 const onOpenChange = vi.fn();
148 let resolveMutation!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
149 settingsHooks.mutate.mockReturnValueOnce(
150 new Promise((resolve) => {
151 resolveMutation = resolve;
152 }),
153 );
154 await selectPreparedAcp();
155
156 const identityStep = render(onOpenChange);
157 expect(
158 visitElements(identityStep, (element) => {
159 const content = JSON.stringify(element.props.children);
160 return content?.includes("4.2.0") === true && content.includes("binary");
161 }),
162 ).not.toBeNull();
163 (
164 findByChildren(identityStep, "Continue to sign-in").props.onClick as (() => void) | undefined
165 )?.();
166 expect(settingsHooks.mutate).toHaveBeenCalledWith({
167 operation: "create",
168 instanceId: "acpRegistry_kilo_code",
169 instance: {
170 driver: ProviderDriverKind.make("acpRegistry"),
171 enabled: true,
172 displayName: "Kilo Code",
173 config: {
174 agentId: "kilo",
175 distribution: "auto",
176 registryIconUrl: "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
177 },
178 },
179 });
180 expect(onOpenChange).not.toHaveBeenCalled();
181
182 resolveMutation({ _tag: "Success", value: {} });
183 await Promise.resolve();
184 await Promise.resolve();
185 expect(onOpenChange).not.toHaveBeenCalled();
186 const signInStep = render(onOpenChange);
187 const authentication = visitElements(
188 signInStep,
189 (element) =>
190 typeof element.type === "function" &&
191 element.type.name === "ProviderWizardAuthenticationStep",
192 );
193 expect(authentication?.props.instanceId).toBe("acpRegistry_kilo_code");
194 expect(authentication?.props.environmentId).toBe(remoteEnvironmentId);
195 expect(settingsHooks.mutate).toHaveBeenCalledTimes(1);
196 (authentication!.props.onFinish as () => void)();
197 expect(onOpenChange).toHaveBeenCalledWith(false);
198 });
199
200 it("configures a manually entered registry agent from the first provider screen", async () => {
201 let tree = render();
202 const search = visitElements(
203 tree,
204 (element) =>
205 typeof element.type === "function" && element.type.name === "AcpRegistrySearchStep",
206 );
207 (search!.props.onManualConfiguration as () => void)();
208 tree = render();
209 const configuration = visitElements(
210 tree,
211 (element) => element.props.idPrefix === "add-provider-acpRegistry-manual",
212 );
213 (configuration!.props.onChange as (value: Record<string, unknown>) => void)({
214 agentId: "devin",
215 });
216 tree = render();
217 (findByChildren(tree, "Next").props.onClick as () => void)();
218 tree = render();
219 (findByChildren(tree, "Continue to sign-in").props.onClick as () => void)();
220 await Promise.resolve();
221 await Promise.resolve();
222 expect(settingsHooks.mutate).toHaveBeenCalledWith({
223 operation: "create",
224 instanceId: "acpRegistry_custom",
225 instance: {
226 driver: "acpRegistry",
227 enabled: true,
228 config: { agentId: "devin" },
229 },
230 });
231 tree = render();
232 expect(
233 visitElements(
234 tree,
235 (element) =>
236 typeof element.type === "function" &&
237 element.type.name === "ProviderWizardAuthenticationStep",
238 ),
239 ).not.toBeNull();
240 });
241
242 it("keeps the dialog open when the atomic upsert fails", async () => {
243 settingsHooks.mutate.mockResolvedValueOnce({ _tag: "Failure", cause: new Error("Conflict") });
244 const onOpenChange = vi.fn();
245 await selectPreparedAcp();
246
247 const identityStep = render(onOpenChange);
248 (
249 findByChildren(identityStep, "Continue to sign-in").props.onClick as (() => void) | undefined
250 )?.();
251 await Promise.resolve();
252 await Promise.resolve();
253
254 expect(onOpenChange).not.toHaveBeenCalled();
255 });
256});