Open History to see the last commit
1import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ProviderAuthState,
6 type ProviderInstallState,
7 type ServerProvider,
8} from "@t3tools/contracts";
9import { userEvent } from "vite-plus/test/browser";
10import { describe, expect, it, vi } from "vite-plus/test";
11
12import { act, render } from "../../../test/render";
13
14const setup = vi.hoisted(() => ({
15 auth: null as ProviderAuthState | null,
16 installation: null as ProviderInstallState | null,
17 authState: vi.fn(() => "auth"),
18 installState: vi.fn(() => "installation"),
19 startAuth: vi.fn(),
20 completeAuth: vi.fn(),
21 respondAuth: vi.fn(),
22 cancelAuth: vi.fn(),
23 logoutAuth: vi.fn(),
24 startInstall: vi.fn(),
25 cancelInstall: vi.fn(),
26 removeInstall: vi.fn(),
27 confirm: vi.fn(),
28}));
29
30vi.mock(import("../../state/server"), async (original) => {
31 const actual = await original();
32 return {
33 ...actual,
34 serverEnvironment: {
35 ...actual.serverEnvironment,
36 providerAuthState: setup.authState as never,
37 providerInstallState: setup.installState as never,
38 startProviderAuth: setup.startAuth as never,
39 completeProviderAuth: setup.completeAuth as never,
40 respondProviderAuth: setup.respondAuth as never,
41 cancelProviderAuth: setup.cancelAuth as never,
42 logoutProviderAuth: setup.logoutAuth as never,
43 startProviderInstall: setup.startInstall as never,
44 cancelProviderInstall: setup.cancelInstall as never,
45 removeProviderInstallation: setup.removeInstall as never,
46 },
47 };
48});
49vi.mock(import("../../state/use-atom-command"), async (original) => ({
50 ...(await original()),
51 useAtomCommand: ((command: unknown) => command) as never,
52}));
53// The queries behave like live subscriptions: changing `setup.auth` and calling
54// `publish()` re-renders the sections that read them.
55const queryListeners = vi.hoisted(() => new Set<() => void>());
56vi.mock(import("../../state/query"), async (original) => {
57 const { useSyncExternalStore } = await import("react");
58 const subscribe = (listener: () => void) => {
59 queryListeners.add(listener);
60 return () => queryListeners.delete(listener);
61 };
62 return {
63 ...(await original()),
64 useEnvironmentQuery: ((atom: string) => ({
65 data: useSyncExternalStore(subscribe, () =>
66 atom === "auth" ? setup.auth : setup.installation,
67 ),
68 error: null,
69 isPending: false,
70 refresh: vi.fn(),
71 })) as never,
72 };
73});
74const publish = () =>
75 act(async () => {
76 for (const listener of queryListeners) listener();
77 });
78vi.mock(import("../../localApi"), async (original) => ({
79 ...(await original()),
80 ensureLocalApi: () => ({ dialogs: { confirm: setup.confirm } }) as never,
81}));
82
83import { ProviderSetupSection } from "./ProviderSetupSection";
84
85const environmentId = EnvironmentId.make("remote-google");
86const instanceId = ProviderInstanceId.make("antigravity_work");
87const provider: ServerProvider = {
88 instanceId,
89 driver: ProviderDriverKind.make("antigravity"),
90 installed: true,
91 enabled: true,
92 version: "test-version",
93 status: "error",
94 auth: { status: "unauthenticated" },
95 checkedAt: "2026-09-02T00:00:00.000Z",
96 models: [],
97 skills: [],
98 slashCommands: [],
99 setup: { canAuthenticate: true, canInstall: true },
100};
101
102function authState(patch: Partial<ProviderAuthState> = {}): ProviderAuthState {
103 return {
104 instanceId,
105 phase: "waiting",
106 flowId: "flow-1",
107 authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth?state=test-only",
108 expiresAt: "2026-09-02T00:05:00.000Z",
109 message: null,
110 ...patch,
111 };
112}
113
114interface SectionOptions {
115 readOnly?: boolean;
116 provider?: ServerProvider;
117 enabled?: boolean;
118 binaryPath?: string;
119}
120
121function section(options: SectionOptions = {}) {
122 return (
123 <ProviderSetupSection
124 environmentId={environmentId}
125 environmentLabel="Remote Google device"
126 instanceId={instanceId}
127 provider={options.provider ?? provider}
128 binaryPath={options.binaryPath}
129 enabled={options.enabled ?? true}
130 readOnly={options.readOnly ?? false}
131 onEnable={vi.fn()}
132 />
133 );
134}
135
136async function renderSetup(options: SectionOptions = {}) {
137 const screen = await render(section(options));
138 const { container } = screen;
139 const button = (label: string) =>
140 [...container.querySelectorAll("button")].find(
141 (element) => element.textContent === label || element.getAttribute("aria-label") === label,
142 ) ?? null;
143 return Object.assign(screen, {
144 button,
145 async click(label: string) {
146 const target = button(label);
147 expect(target, `button ${label}`).not.toBeNull();
148 expect(target!.disabled, `${label} disabled`).toBe(false);
149 await act(async () => target!.click());
150 },
151 occurrences: (text: string) => (container.textContent ?? "").split(text).length - 1,
152 callbackInput: () =>
153 container.querySelector<HTMLInputElement>(`[id="provider-callback-${instanceId}"]`)!,
154 });
155}
156
157function resetSetup() {
158 vi.clearAllMocks();
159 setup.auth = authState();
160 setup.installation = {
161 driver: ProviderDriverKind.make("antigravity"),
162 operationId: null,
163 phase: "idle",
164 downloadedBytes: 0,
165 totalBytes: null,
166 version: null,
167 installedVersion: null,
168 canRemove: false,
169 message: null,
170 };
171 for (const command of [
172 setup.startAuth,
173 setup.completeAuth,
174 setup.cancelAuth,
175 setup.logoutAuth,
176 setup.startInstall,
177 setup.cancelInstall,
178 setup.removeInstall,
179 ]) {
180 command.mockReset().mockResolvedValue({ _tag: "Success", value: undefined });
181 }
182 setup.confirm.mockReset().mockResolvedValue(false);
183}
184
185describe("Antigravity setup", () => {
186 it("waits for verified auth after submitting a callback to the selected environment", async () => {
187 resetSetup();
188 const callbackUrl = "http://127.0.0.1:5555/?state=test-only&code=test-only";
189 await using screen = await renderSetup();
190 await userEvent.fill(screen.callbackInput(), callbackUrl);
191 await screen.click("Continue");
192
193 expect(setup.completeAuth).toHaveBeenCalledWith({
194 environmentId,
195 input: { instanceId, flowId: "flow-1", callbackUrl },
196 });
197 expect(screen.occurrences("Signed in.")).toBe(0);
198 expect(screen.callbackInput().value).toBe("");
199
200 setup.auth = authState({ phase: "verifying", authorizationUrl: null });
201 await publish();
202 expect(screen.occurrences("Signed in.")).toBe(0);
203 setup.auth = authState({ phase: "succeeded", authorizationUrl: null });
204 await screen.rerender(
205 section({
206 provider: { ...provider, status: "ready", auth: { status: "authenticated" } },
207 }),
208 );
209 expect(screen.occurrences("Signed in.")).toBe(1);
210 });
211
212 it("does not send a callback left over from a replaced sign-in flow", async () => {
213 resetSetup();
214 const interaction = {
215 type: "browser" as const,
216 id: "same-interaction",
217 url: "https://example.com/login",
218 requiresConsent: false,
219 acceptsCallback: true,
220 };
221 setup.auth = authState({ interaction });
222 await using screen = await renderSetup();
223 await userEvent.fill(
224 screen.callbackInput(),
225 "http://127.0.0.1:5555/?state=old-flow&code=test-only",
226 );
227 setup.auth = authState({ flowId: "flow-2", interaction });
228 await publish();
229 expect(screen.callbackInput().value).toBe("");
230 await act(async () => screen.callbackInput().form!.requestSubmit());
231
232 expect(setup.completeAuth).not.toHaveBeenCalled();
233 expect(setup.authState).toHaveBeenLastCalledWith({ environmentId, input: { instanceId } });
234 });
235
236 it("does not let a shared managed install hide an invalid custom binary path", async () => {
237 resetSetup();
238 setup.auth = authState({ phase: "idle", flowId: null, authorizationUrl: null });
239 setup.installation = {
240 ...setup.installation!,
241 installedVersion: "test-version",
242 canRemove: true,
243 };
244 await using screen = await renderSetup({
245 provider: { ...provider, installed: false },
246 binaryPath: "/missing/antigravity",
247 });
248 expect(screen.button("Sign in")?.disabled).toBe(true);
249 expect(setup.startAuth).not.toHaveBeenCalled();
250 });
251});