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

AcpRegistrySearchStep.browser.test.tsx

141 lines3.9 KB

1import { EnvironmentId, ProviderDriverKind, type AcpRegistrySearchAgent } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3import { page } from "vite-plus/test/browser";
4​
5import { act, render } from "../../../test/render";
6​
7const state = vi.hoisted(() => ({
8 result: null as { readonly agents: ReadonlyArray<AcpRegistrySearchAgent> } | null,
9 error: null as string | null,
10 isPending: false,
11 canManage: true,
12 refresh: vi.fn(),
13 search: vi.fn(),
14 prepare: vi.fn(),
15}));
16​
17vi.mock(import("../../state/server"), async (original) => {
18 const actual = await original();
19 return {
20 ...actual,
21 serverEnvironment: {
22 ...actual.serverEnvironment,
23 searchAcpRegistry: ((input: unknown) => {
24 state.search(input);
25 return "search";
26 }) as never,
27 },
28 };
29});
30vi.mock(import("../../state/session"), async (original) => ({
31 ...(await original()),
32 useEnvironmentScope: () => state.canManage,
33 readEnvironmentScope: () => state.canManage,
34}));
35vi.mock(import("../../state/query"), async (original) => ({
36 ...(await original()),
37 useEnvironmentQuery: (() => ({
38 data: state.result,
39 error: state.error,
40 isPending: state.isPending,
41 refresh: state.refresh,
42 })) as never,
43}));
44vi.mock(import("../../state/use-atom-command"), async (original) => ({
45 ...(await original()),
46 useAtomCommand: (() => state.prepare) as never,
47}));
48​
49import { AcpRegistrySearchStep } from "./AcpRegistrySearchStep";
50​
51const environmentId = EnvironmentId.make("remote-device");
52const gemini: AcpRegistrySearchAgent = {
53 id: "gemini",
54 name: "Gemini CLI",
55 version: "1.2.3",
56 description: "Google's agent",
57 authors: ["Google <gemini-cli@google.com>", "Contributor"],
58 license: "Apache-2.0",
59 website: "https://example.com/docs",
60 repository: "https://example.com/source",
61 icon: null,
62 distribution: "npx",
63 integrity: "registry",
64};
65​
66function step(options?: {
67 readonly configured?: boolean;
68 readonly onPrepared?: (agent: AcpRegistrySearchAgent) => void;
69}) {
70 return (
71 <AcpRegistrySearchStep
72 environmentId={environmentId}
73 providerInstances={
74 options?.configured
75 ? {
76 acpRegistry_gemini: {
77 driver: ProviderDriverKind.make("acpRegistry"),
78 config: { agentId: "gemini" },
79 },
80 }
81 : {}
82 }
83 onPrepared={options?.onPrepared ?? vi.fn()}
84 onManualConfiguration={vi.fn()}
85 />
86 );
88​
89function deferred<T>() {
90 let resolve!: (value: T) => void;
91 const promise = new Promise<T>((done) => {
92 resolve = done;
93 });
94 return { promise, resolve };
96​
97function setup() {
98 state.canManage = true;
99 state.result = null;
100 state.error = null;
101 state.isPending = false;
102 state.refresh.mockReset();
103 state.search.mockReset();
104 state.prepare.mockReset().mockResolvedValue({
105 _tag: "Success",
106 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
107 });
109​
110it("rechecks the grant before preparing an agent", async () => {
111 setup();
112 state.result = { agents: [gemini] };
113 const onPrepared = vi.fn();
114 await using _screen = await render(step({ onPrepared }));
115 state.canManage = false;
116​
117 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
118​
119 expect(state.prepare).not.toHaveBeenCalled();
120 expect(onPrepared).not.toHaveBeenCalled();
121});
122​
123it("ignores prepare completion after unmount", async () => {
124 setup();
125 const pending = deferred<unknown>();
126 state.prepare.mockReturnValueOnce(pending.promise);
127 state.result = { agents: [gemini] };
128 const onPrepared = vi.fn();
129 await using screen = await render(step({ onPrepared }));
130​
131 await page.getByRole("button", { name: "Add Gemini CLI" }).click();
132 await screen.unmount();
133 await act(async () =>
134 pending.resolve({
135 _tag: "Success",
136 value: { agentId: "gemini", version: "1.2.3", distribution: "npx", prepared: true },
137 }),
138 );
139​
140 expect(onPrepared).not.toHaveBeenCalled();
141});