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 initial = render();
73 const driverGroup = visitElements(
74 initial,
75 (element) => element.props["aria-labelledby"] === "add-instance-driver-label",
76 );
77 (driverGroup?.props.onValueChange as ((value: string) => void) | undefined)?.("acpRegistry");
78
79 const driverStep = render();
80 (findByChildren(driverStep, "Next").props.onClick as (() => void) | undefined)?.();
81
82 const searchStep = render();
83 const search = visitElements(
84 searchStep,
85 (element) =>
86 typeof element.type === "function" && element.type.name === "AcpRegistrySearchStep",
87 );
88 expect(search).not.toBeNull();
89 (search?.props.onPrepared as ((agent: AcpRegistrySearchAgent) => void) | undefined)?.(
90 preparedAgent,
91 );
92}
93
94describe("AddProviderInstanceDialog environment routing", () => {
95 beforeEach(() => {
96 hooks.reset();
97 settingsHooks.read.mockClear();
98 settingsHooks.mutate.mockReset().mockResolvedValue({ _tag: "Success", value: {} });
99 settingsHooks.useMutation.mockReset().mockReturnValue(settingsHooks.mutate);
100 });
101
102 it("reads and writes settings through the supplied environment", () => {
103 render();
104
105 expect(settingsHooks.read).toHaveBeenCalledWith(remoteEnvironmentId);
106 expect(settingsHooks.useMutation).toHaveBeenCalledWith(remoteEnvironmentId);
107 });
108
109 it("awaits an atomic create before closing and retains prepared ACP metadata", async () => {
110 const onOpenChange = vi.fn();
111 let resolveMutation!: (value: { readonly _tag: "Success"; readonly value: unknown }) => void;
112 settingsHooks.mutate.mockReturnValueOnce(
113 new Promise((resolve) => {
114 resolveMutation = resolve;
115 }),
116 );
117 await selectPreparedAcp();
118
119 const identityStep = render(onOpenChange);
120 expect(
121 visitElements(identityStep, (element) => {
122 const content = JSON.stringify(element.props.children);
123 return content?.includes("4.2.0") === true && content.includes("binary");
124 }),
125 ).not.toBeNull();
126 (findByChildren(identityStep, "Add instance").props.onClick as (() => void) | undefined)?.();
127 expect(settingsHooks.mutate).toHaveBeenCalledWith({
128 operation: "create",
129 instanceId: "acpRegistry_kilo_code",
130 instance: {
131 driver: ProviderDriverKind.make("acpRegistry"),
132 enabled: true,
133 displayName: "Kilo Code",
134 config: {
135 agentId: "kilo",
136 distribution: "auto",
137 registryIconUrl: "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
138 },
139 },
140 });
141 expect(onOpenChange).not.toHaveBeenCalled();
142
143 resolveMutation({ _tag: "Success", value: {} });
144 await Promise.resolve();
145 await Promise.resolve();
146 expect(onOpenChange).toHaveBeenCalledWith(false);
147 });
148
149 it("keeps the dialog open when the atomic upsert fails", async () => {
150 settingsHooks.mutate.mockResolvedValueOnce({ _tag: "Failure", cause: new Error("Conflict") });
151 const onOpenChange = vi.fn();
152 await selectPreparedAcp();
153
154 const identityStep = render(onOpenChange);
155 (findByChildren(identityStep, "Add instance").props.onClick as (() => void) | undefined)?.();
156 await Promise.resolve();
157 await Promise.resolve();
158
159 expect(onOpenChange).not.toHaveBeenCalled();
160 });
161});