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

AddProviderInstanceDialog.environment.test.tsx

250 lines8.5 KB

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 });
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!;
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 );
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 (findByChildren(tree, "Next").props.onClick as () => void)();
121 tree = render();
122 (findByChildren(tree, "Next").props.onClick as () => void)();
123 tree = render();
124 (findByChildren(tree, "Add instance").props.onClick as () => void)();
125 await Promise.resolve();
126 expect(settingsHooks.mutate).toHaveBeenCalledWith({
127 operation: "create",
128 instanceId: "codex_3",
129 instance: { driver: "codex", enabled: true, displayName: "Codex" },
130 });
131 });
132​
133 it("reads and writes settings through the supplied environment", () => {
134 render();
135​
136 expect(settingsHooks.read).toHaveBeenCalledWith(remoteEnvironmentId);
137 expect(settingsHooks.useMutation).toHaveBeenCalledWith(remoteEnvironmentId);
138 });
139​
140 it("awaits an atomic create before closing and retains prepared ACP metadata", async () => {
141 const onOpenChange = vi.fn();
142 let resolveMutation!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
143 settingsHooks.mutate.mockReturnValueOnce(
144 new Promise((resolve) => {
145 resolveMutation = resolve;
146 }),
147 );
148 await selectPreparedAcp();
149​
150 const identityStep = render(onOpenChange);
151 expect(
152 visitElements(identityStep, (element) => {
153 const content = JSON.stringify(element.props.children);
154 return content?.includes("4.2.0") === true && content.includes("binary");
155 }),
156 ).not.toBeNull();
157 (
158 findByChildren(identityStep, "Continue to sign-in").props.onClick as (() => void) | undefined
159 )?.();
160 expect(settingsHooks.mutate).toHaveBeenCalledWith({
161 operation: "create",
162 instanceId: "acpRegistry_kilo_code",
163 instance: {
164 driver: ProviderDriverKind.make("acpRegistry"),
165 enabled: true,
166 displayName: "Kilo Code",
167 config: {
168 agentId: "kilo",
169 distribution: "auto",
170 registryIconUrl: "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
171 },
172 },
173 });
174 expect(onOpenChange).not.toHaveBeenCalled();
175​
176 resolveMutation({ _tag: "Success", value: {} });
177 await Promise.resolve();
178 await Promise.resolve();
179 expect(onOpenChange).not.toHaveBeenCalled();
180 const signInStep = render(onOpenChange);
181 const authentication = visitElements(
182 signInStep,
183 (element) =>
184 typeof element.type === "function" &&
185 element.type.name === "ProviderWizardAuthenticationStep",
186 );
187 expect(authentication?.props.instanceId).toBe("acpRegistry_kilo_code");
188 expect(authentication?.props.environmentId).toBe(remoteEnvironmentId);
189 expect(settingsHooks.mutate).toHaveBeenCalledTimes(1);
190 (authentication!.props.onFinish as () => void)();
191 expect(onOpenChange).toHaveBeenCalledWith(false);
192 });
193​
194 it("configures a manually entered registry agent from the first provider screen", async () => {
195 let tree = render();
196 const search = visitElements(
197 tree,
198 (element) =>
199 typeof element.type === "function" && element.type.name === "AcpRegistrySearchStep",
200 );
201 (search!.props.onManualConfiguration as () => void)();
202 tree = render();
203 const configuration = visitElements(
204 tree,
205 (element) => element.props.idPrefix === "add-provider-acpRegistry-manual",
206 );
207 (configuration!.props.onChange as (value: Record<string, unknown>) => void)({
208 agentId: "devin",
209 });
210 tree = render();
211 (findByChildren(tree, "Next").props.onClick as () => void)();
212 tree = render();
213 (findByChildren(tree, "Continue to sign-in").props.onClick as () => void)();
214 await Promise.resolve();
215 await Promise.resolve();
216 expect(settingsHooks.mutate).toHaveBeenCalledWith({
217 operation: "create",
218 instanceId: "acpRegistry_custom",
219 instance: {
220 driver: "acpRegistry",
221 enabled: true,
222 config: { agentId: "devin" },
223 },
224 });
225 tree = render();
226 expect(
227 visitElements(
228 tree,
229 (element) =>
230 typeof element.type === "function" &&
231 element.type.name === "ProviderWizardAuthenticationStep",
232 ),
233 ).not.toBeNull();
234 });
235​
236 it("keeps the dialog open when the atomic upsert fails", async () => {
237 settingsHooks.mutate.mockResolvedValueOnce({ _tag: "Failure", cause: new Error("Conflict") });
238 const onOpenChange = vi.fn();
239 await selectPreparedAcp();
240​
241 const identityStep = render(onOpenChange);
242 (
243 findByChildren(identityStep, "Continue to sign-in").props.onClick as (() => void) | undefined
244 )?.();
245 await Promise.resolve();
246 await Promise.resolve();
247​
248 expect(onOpenChange).not.toHaveBeenCalled();
249 });
250});