The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | ProjectId, |
| 4 | ProviderDriverKind, |
| 5 | ProviderInstanceId, |
| 6 | ThreadId, |
| 7 | type ServerProvider, |
| 8 | type AcpRegistryConfigurableProvider, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import type { ComponentProps } from "react"; |
| 11 | import { expect, it, vi } from "vite-plus/test"; |
| 12 | import { userEvent } from "vite-plus/test/browser"; |
| 13 | |
| 14 | import { act, render } from "../../../test/render"; |
| 15 | |
| 16 | const commands = vi.hoisted(() => ({ |
| 17 | list: vi.fn(), |
| 18 | import: vi.fn(), |
| 19 | delete: vi.fn(), |
| 20 | listProviders: vi.fn(), |
| 21 | setProvider: vi.fn(), |
| 22 | disableProvider: vi.fn(), |
| 23 | logout: vi.fn(), |
| 24 | })); |
| 25 | const grants = vi.hoisted(() => ({ scopes: new Set<string>() })); |
| 26 | const dialogs = vi.hoisted(() => ({ confirm: vi.fn() })); |
| 27 | |
| 28 | vi.mock(import("~/state/session"), async (original) => ({ |
| 29 | ...(await original()), |
| 30 | useEnvironmentScope: ((_id: unknown, scope: string) => grants.scopes.has(scope)) as never, |
| 31 | readEnvironmentScope: ((_id: unknown, scope: string) => grants.scopes.has(scope)) as never, |
| 32 | })); |
| 33 | vi.mock(import("../../state/use-atom-command"), async (original) => { |
| 34 | const { serverEnvironment } = await import("../../state/server"); |
| 35 | const byAtom = new Map<unknown, unknown>([ |
| 36 | [serverEnvironment.listAcpRegistrySessions, commands.list], |
| 37 | [serverEnvironment.importAcpRegistrySession, commands.import], |
| 38 | [serverEnvironment.deleteAcpRegistrySession, commands.delete], |
| 39 | [serverEnvironment.listAcpRegistryProviders, commands.listProviders], |
| 40 | [serverEnvironment.setAcpRegistryProvider, commands.setProvider], |
| 41 | [serverEnvironment.disableAcpRegistryProvider, commands.disableProvider], |
| 42 | [serverEnvironment.logoutAcpRegistry, commands.logout], |
| 43 | ]); |
| 44 | return { |
| 45 | ...(await original()), |
| 46 | useAtomCommand: ((atom: unknown) => byAtom.get(atom)) as never, |
| 47 | }; |
| 48 | }); |
| 49 | vi.mock(import("../../localApi"), async (original) => ({ |
| 50 | ...(await original()), |
| 51 | ensureLocalApi: () => ({ dialogs }) as never, |
| 52 | })); |
| 53 | |
| 54 | import { AcpSessionManagementSection } from "./AcpSessionManagementSection"; |
| 55 | |
| 56 | const environmentId = EnvironmentId.make("remote-device"); |
| 57 | const instanceId = ProviderInstanceId.make("acpRegistry_antigravity"); |
| 58 | const projectId = ProjectId.make("project-antigravity"); |
| 59 | const session = { |
| 60 | sessionId: "native-session-1", |
| 61 | cwd: "/workspace/antigravity", |
| 62 | additionalDirectories: [], |
| 63 | title: "Native Antigravity session", |
| 64 | updatedAt: "2026-08-23T00:00:00Z", |
| 65 | importedThreadId: null, |
| 66 | } as const; |
| 67 | const provider = { |
| 68 | instanceId, |
| 69 | driver: ProviderDriverKind.make("acpRegistry"), |
| 70 | enabled: true, |
| 71 | installed: true, |
| 72 | version: "1.0.0", |
| 73 | status: "ready", |
| 74 | auth: { status: "authenticated", canLogout: true }, |
| 75 | nativeSessions: { canList: true, canLoad: true, canResume: true, canDelete: true }, |
| 76 | configurableProviders: true, |
| 77 | checkedAt: "2026-08-23T00:00:00.000Z", |
| 78 | models: [], |
| 79 | slashCommands: [], |
| 80 | skills: [], |
| 81 | } satisfies ServerProvider; |
| 82 | |
| 83 | const project = { id: projectId, title: "Antigravity", workspaceRoot: session.cwd }; |
| 84 | const replacement = { |
| 85 | id: ProjectId.make("replacement"), |
| 86 | title: "Replacement", |
| 87 | workspaceRoot: "/replacement", |
| 88 | }; |
| 89 | type Projects = ComponentProps<typeof AcpSessionManagementSection>["projects"]; |
| 90 | |
| 91 | async function renderSection(options: { canList?: boolean; projects?: Projects } = {}) { |
| 92 | const section = (projects = options.projects ?? [project]) => ( |
| 93 | <AcpSessionManagementSection |
| 94 | environmentId={environmentId} |
| 95 | instanceId={instanceId} |
| 96 | provider={{ |
| 97 | ...provider, |
| 98 | nativeSessions: { ...provider.nativeSessions, canList: options.canList ?? true }, |
| 99 | }} |
| 100 | projects={projects} |
| 101 | readOnly={false} |
| 102 | /> |
| 103 | ); |
| 104 | const screen = await render(section()); |
| 105 | const button = (label: string) => { |
| 106 | const found = [...screen.container.querySelectorAll("button")].find( |
| 107 | (element) => element.textContent === label, |
| 108 | ); |
| 109 | expect(found, `button ${label}`).toBeDefined(); |
| 110 | return found!; |
| 111 | }; |
| 112 | return Object.assign(screen, { |
| 113 | button, |
| 114 | click: (label: string) => act(async () => button(label).click()), |
| 115 | setProjects: (projects: Projects) => screen.rerender(section(projects)), |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | function setup() { |
| 120 | grants.scopes = new Set(["orchestration:read", "orchestration:operate", "providers:manage"]); |
| 121 | commands.list.mockReset().mockResolvedValue({ |
| 122 | _tag: "Success", |
| 123 | value: { |
| 124 | sessions: [session], |
| 125 | nextCursor: null, |
| 126 | canLoad: true, |
| 127 | canResume: true, |
| 128 | canDelete: true, |
| 129 | }, |
| 130 | }); |
| 131 | commands.import.mockReset().mockResolvedValue({ |
| 132 | _tag: "Success", |
| 133 | value: { threadId: ThreadId.make("thread-imported"), imported: true }, |
| 134 | }); |
| 135 | commands.delete.mockReset().mockResolvedValue({ |
| 136 | _tag: "Success", |
| 137 | value: { deleted: true }, |
| 138 | }); |
| 139 | commands.listProviders.mockReset().mockResolvedValue({ |
| 140 | _tag: "Success", |
| 141 | value: { |
| 142 | providers: [ |
| 143 | { |
| 144 | providerId: "google", |
| 145 | supported: ["openai"], |
| 146 | required: false, |
| 147 | current: { apiType: "openai", baseUrl: "https://api.example.test/v1" }, |
| 148 | }, |
| 149 | ], |
| 150 | }, |
| 151 | }); |
| 152 | commands.setProvider.mockReset().mockResolvedValue({ |
| 153 | _tag: "Success", |
| 154 | value: { configured: true }, |
| 155 | }); |
| 156 | commands.disableProvider.mockReset().mockResolvedValue({ |
| 157 | _tag: "Success", |
| 158 | value: { disabled: true }, |
| 159 | }); |
| 160 | commands.logout.mockReset().mockResolvedValue({ |
| 161 | _tag: "Success", |
| 162 | value: { loggedOut: true }, |
| 163 | }); |
| 164 | dialogs.confirm.mockReset().mockResolvedValue(true); |
| 165 | } |
| 166 | |
| 167 | it("does not import with provider management alone", async () => { |
| 168 | setup(); |
| 169 | grants.scopes.delete("orchestration:operate"); |
| 170 | await using screen = await renderSection(); |
| 171 | await screen.click("List sessions"); |
| 172 | expect(screen.button("Import").disabled).toBe(true); |
| 173 | await screen.click("Import"); |
| 174 | expect(commands.import).not.toHaveBeenCalled(); |
| 175 | }); |
| 176 | |
| 177 | it("rechecks task access after confirming deletion", async () => { |
| 178 | setup(); |
| 179 | await using screen = await renderSection(); |
| 180 | await screen.click("List sessions"); |
| 181 | dialogs.confirm.mockImplementationOnce(async () => { |
| 182 | grants.scopes.delete("orchestration:operate"); |
| 183 | return true; |
| 184 | }); |
| 185 | await screen.click("Delete"); |
| 186 | expect(commands.delete).not.toHaveBeenCalled(); |
| 187 | }); |
| 188 | |
| 189 | it("keeps a deleted ACP session out of a pending page only", async () => { |
| 190 | setup(); |
| 191 | commands.list.mockResolvedValueOnce({ |
| 192 | _tag: "Success", |
| 193 | value: { sessions: [session], nextCursor: "page-2" }, |
| 194 | }); |
| 195 | let finishPage!: (value: unknown) => void; |
| 196 | commands.list.mockReturnValueOnce( |
| 197 | new Promise((resolve) => { |
| 198 | finishPage = resolve; |
| 199 | }), |
| 200 | ); |
| 201 | const otherSession = { ...session, sessionId: `${session.sessionId}0` }; |
| 202 | await using screen = await renderSection(); |
| 203 | const deletedRow = screen.getByText(session.sessionId, { exact: true }); |
| 204 | await screen.click("List sessions"); |
| 205 | await screen.click("Load more"); |
| 206 | await screen.click("Delete"); |
| 207 | await expect.element(deletedRow).not.toBeInTheDocument(); |
| 208 | |
| 209 | await act(async () => |
| 210 | finishPage({ |
| 211 | _tag: "Success", |
| 212 | value: { sessions: [session, otherSession], nextCursor: null }, |
| 213 | }), |
| 214 | ); |
| 215 | await expect.element(screen.getByText(otherSession.sessionId, { exact: true })).toBeVisible(); |
| 216 | await expect.element(deletedRow).not.toBeInTheDocument(); |
| 217 | |
| 218 | await screen.click("Refresh"); |
| 219 | await expect.element(deletedRow).toBeVisible(); |
| 220 | }); |
| 221 | |
| 222 | it.each(["Save", "Disable"])("keeps other provider edits through a %s refetch", async (action) => { |
| 223 | setup(); |
| 224 | const providers: AcpRegistryConfigurableProvider[] = ["google", "other"].map((providerId) => ({ |
| 225 | providerId, |
| 226 | supported: ["openai"], |
| 227 | required: false, |
| 228 | current: { apiType: "openai", baseUrl: "https://api.example.test/v1" }, |
| 229 | })); |
| 230 | const response = { _tag: "Success" as const, value: { providers } }; |
| 231 | commands.listProviders.mockResolvedValue(response); |
| 232 | await using screen = await renderSection(); |
| 233 | await screen.click("List providers"); |
| 234 | const input = (label: string) => |
| 235 | screen.container.querySelector<HTMLInputElement>(`[aria-label="${label}"]`)!; |
| 236 | await userEvent.fill(input("other base URL"), "https://other.example.test/v1"); |
| 237 | await userEvent.fill(input("other write-only headers JSON"), '{"X-Test":"unsaved"}'); |
| 238 | await userEvent.fill(input("google write-only headers JSON"), '{"X-Test":"saved"}'); |
| 239 | |
| 240 | let resolveReload!: (value: typeof response) => void; |
| 241 | const reload = new Promise<typeof response>((resolve) => { |
| 242 | resolveReload = resolve; |
| 243 | }); |
| 244 | commands.listProviders.mockReturnValueOnce(reload); |
| 245 | let resolveMutation!: () => void; |
| 246 | const mutation = new Promise<void>((resolve) => { |
| 247 | resolveMutation = resolve; |
| 248 | }); |
| 249 | const command = action === "Save" ? commands.setProvider : commands.disableProvider; |
| 250 | command.mockReturnValueOnce( |
| 251 | mutation.then(() => ({ |
| 252 | _tag: "Success", |
| 253 | value: action === "Save" ? { configured: true } : { disabled: true }, |
| 254 | })), |
| 255 | ); |
| 256 | await screen.click(action); |
| 257 | expect(screen.button("Refresh").disabled).toBe(true); |
| 258 | await act(async () => resolveMutation()); |
| 259 | for (const button of screen.container.querySelectorAll<HTMLButtonElement>("button")) { |
| 260 | if (button.textContent === "Save" || button.textContent === "Disable") { |
| 261 | expect(button.disabled).toBe(true); |
| 262 | } |
| 263 | } |
| 264 | await userEvent.fill(input("other base URL"), "https://newer.example.test/v1"); |
| 265 | await act(async () => resolveReload(response)); |
| 266 | |
| 267 | expect(input("other base URL").value).toBe("https://newer.example.test/v1"); |
| 268 | expect(input("other write-only headers JSON").value).toBe('{"X-Test":"unsaved"}'); |
| 269 | expect(input("google write-only headers JSON").value).toBe(""); |
| 270 | await screen.click("Refresh"); |
| 271 | expect(input("other base URL").value).toBe("https://api.example.test/v1"); |
| 272 | expect(input("other write-only headers JSON").value).toBe(""); |
| 273 | }); |
| 274 | |
| 275 | it.each([true, false])( |
| 276 | "enables project actions after projects arrive (sessions: %s)", |
| 277 | async (canList) => { |
| 278 | setup(); |
| 279 | await using screen = await renderSection({ canList, projects: [] }); |
| 280 | await screen.setProjects([project]); |
| 281 | const action = screen.getByRole("button", { |
| 282 | name: canList ? "List sessions" : "List providers", |
| 283 | }); |
| 284 | await expect.element(action).toBeEnabled(); |
| 285 | if (!canList) { |
| 286 | await expect.element(screen.getByRole("combobox")).not.toBeInTheDocument(); |
| 287 | } |
| 288 | await action.click(); |
| 289 | if (canList) { |
| 290 | await expect.element(screen.getByText(session.title)).toBeVisible(); |
| 291 | expect(commands.list).toHaveBeenCalledWith({ |
| 292 | environmentId, |
| 293 | input: { instanceId, projectId }, |
| 294 | }); |
| 295 | } else { |
| 296 | await expect |
| 297 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 298 | .toHaveValue("https://api.example.test/v1"); |
| 299 | expect(commands.listProviders).toHaveBeenCalledWith({ |
| 300 | environmentId, |
| 301 | input: { instanceId, projectId }, |
| 302 | }); |
| 303 | } |
| 304 | }, |
| 305 | ); |
| 306 | |
| 307 | it.each([true, false])( |
| 308 | "preserves a valid selection and clears a removed project's data (sessions: %s)", |
| 309 | async (canList) => { |
| 310 | setup(); |
| 311 | const second = { id: ProjectId.make("second"), title: "Second", workspaceRoot: "/second" }; |
| 312 | commands.list.mockResolvedValue({ |
| 313 | _tag: "Success", |
| 314 | value: { sessions: [session], nextCursor: "page-2" }, |
| 315 | }); |
| 316 | await using screen = await renderSection({ canList, projects: [project, second] }); |
| 317 | await screen.getByRole("combobox").click(); |
| 318 | if (canList) { |
| 319 | // Keys sent before the popup takes focus only move focus onto the selected option. |
| 320 | await expect.element(screen.getByRole("option", { name: "Antigravity" })).toHaveFocus(); |
| 321 | await userEvent.keyboard("{ArrowDown}{Enter}"); |
| 322 | } else await screen.getByRole("option", { name: "Second", exact: true }).click(); |
| 323 | await expect |
| 324 | .element( |
| 325 | screen.getByRole("combobox", { |
| 326 | name: canList ? "Project for ACP sessions" : "Project for ACP providers", |
| 327 | }), |
| 328 | ) |
| 329 | .toHaveFocus(); |
| 330 | if (canList) await screen.getByRole("button", { name: "List sessions" }).click(); |
| 331 | await screen.getByRole("button", { name: "List providers" }).click(); |
| 332 | await userEvent.fill( |
| 333 | screen.getByRole("textbox", { name: "google base URL" }), |
| 334 | "https://draft.test", |
| 335 | ); |
| 336 | await userEvent.fill( |
| 337 | screen.getByLabelText("google write-only headers JSON"), |
| 338 | '{"Secret":"draft"}', |
| 339 | ); |
| 340 | |
| 341 | await screen.setProjects([project, replacement, second]); |
| 342 | await expect |
| 343 | .element( |
| 344 | screen.getByRole("combobox", { |
| 345 | name: canList ? "Project for ACP sessions" : "Project for ACP providers", |
| 346 | }), |
| 347 | ) |
| 348 | .toHaveTextContent("Second"); |
| 349 | await expect |
| 350 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 351 | .toHaveValue("https://draft.test"); |
| 352 | await screen.getByRole("button", { name: "Save", exact: true }).click(); |
| 353 | expect(commands.setProvider).toHaveBeenLastCalledWith({ |
| 354 | environmentId, |
| 355 | input: { |
| 356 | instanceId, |
| 357 | projectId: second.id, |
| 358 | providerId: "google", |
| 359 | apiType: "openai", |
| 360 | baseUrl: "https://draft.test", |
| 361 | headers: { Secret: "draft" }, |
| 362 | }, |
| 363 | }); |
| 364 | |
| 365 | await screen.setProjects([project]); |
| 366 | await expect.element(screen.getByText(session.title)).not.toBeInTheDocument(); |
| 367 | await expect.element(screen.getByRole("button", { name: "Load more" })).not.toBeInTheDocument(); |
| 368 | await expect |
| 369 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 370 | .not.toBeInTheDocument(); |
| 371 | await screen.getByRole("button", { name: "List providers" }).click(); |
| 372 | expect(commands.listProviders).toHaveBeenLastCalledWith({ |
| 373 | environmentId, |
| 374 | input: { instanceId, projectId }, |
| 375 | }); |
| 376 | await expect.element(screen.getByLabelText("google write-only headers JSON")).toHaveValue(""); |
| 377 | await expect |
| 378 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 379 | .toHaveValue("https://api.example.test/v1"); |
| 380 | |
| 381 | await screen.setProjects([second, project]); |
| 382 | await expect |
| 383 | .element( |
| 384 | screen.getByRole("combobox", { |
| 385 | name: canList ? "Project for ACP sessions" : "Project for ACP providers", |
| 386 | }), |
| 387 | ) |
| 388 | .toHaveTextContent("Antigravity"); |
| 389 | await expect |
| 390 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 391 | .toHaveValue("https://api.example.test/v1"); |
| 392 | |
| 393 | await screen.setProjects([]); |
| 394 | await expect.element(screen.getByRole("button", { name: "List providers" })).toBeDisabled(); |
| 395 | await expect |
| 396 | .element(screen.getByRole("textbox", { name: "google base URL" })) |
| 397 | .not.toBeInTheDocument(); |
| 398 | }, |
| 399 | ); |
| 400 | |
| 401 | it("ignores a late listing after project replacement", async () => { |
| 402 | setup(); |
| 403 | let finish!: (value: unknown) => void; |
| 404 | commands.list.mockReturnValueOnce( |
| 405 | new Promise((resolve) => { |
| 406 | finish = resolve; |
| 407 | }), |
| 408 | ); |
| 409 | await using screen = await renderSection(); |
| 410 | await screen.click("List sessions"); |
| 411 | await screen.setProjects([replacement]); |
| 412 | await act(async () => |
| 413 | finish({ _tag: "Success", value: { sessions: [session], nextCursor: "old-page" } }), |
| 414 | ); |
| 415 | await expect.element(screen.getByText(session.title)).not.toBeInTheDocument(); |
| 416 | await expect.element(screen.getByRole("button", { name: "Load more" })).not.toBeInTheDocument(); |
| 417 | await expect.element(screen.getByRole("button", { name: "List sessions" })).toBeEnabled(); |
| 418 | }); |
| 419 | |
| 420 | it.each(["Save", "Disable"])("does not refetch the old project after a late %s", async (action) => { |
| 421 | setup(); |
| 422 | const command = action === "Save" ? commands.setProvider : commands.disableProvider; |
| 423 | let finish!: () => void; |
| 424 | command.mockReturnValueOnce( |
| 425 | new Promise<void>((resolve) => { |
| 426 | finish = resolve; |
| 427 | }).then(() => ({ |
| 428 | _tag: "Success", |
| 429 | value: action === "Save" ? { configured: true } : { disabled: true }, |
| 430 | })), |
| 431 | ); |
| 432 | await using screen = await renderSection(); |
| 433 | await screen.click("List providers"); |
| 434 | await screen.click(action); |
| 435 | expect(command).toHaveBeenCalledOnce(); |
| 436 | await screen.setProjects([replacement]); |
| 437 | await act(async () => finish()); |
| 438 | expect(commands.listProviders).toHaveBeenCalledOnce(); |
| 439 | await expect.element(screen.getByRole("button", { name: "List providers" })).toBeEnabled(); |
| 440 | }); |
| 441 | |
| 442 | it.each(["Delete", "Disable"])( |
| 443 | "does not act on a replaced project after confirming %s", |
| 444 | async (action) => { |
| 445 | setup(); |
| 446 | let finish!: (confirmed: boolean) => void; |
| 447 | dialogs.confirm.mockReturnValueOnce( |
| 448 | new Promise<boolean>((resolve) => { |
| 449 | finish = resolve; |
| 450 | }), |
| 451 | ); |
| 452 | await using screen = await renderSection(); |
| 453 | await screen.click(action === "Delete" ? "List sessions" : "List providers"); |
| 454 | await screen.click(action); |
| 455 | await screen.setProjects([replacement]); |
| 456 | await act(async () => finish(true)); |
| 457 | expect(action === "Delete" ? commands.delete : commands.disableProvider).not.toHaveBeenCalled(); |
| 458 | }, |
| 459 | ); |
| 460 | |
| 461 | it("keeps logout pending across project replacement and clears replacement sessions", async () => { |
| 462 | setup(); |
| 463 | let finish!: () => void; |
| 464 | commands.logout.mockReturnValueOnce( |
| 465 | new Promise<void>((resolve) => { |
| 466 | finish = resolve; |
| 467 | }).then(() => ({ |
| 468 | _tag: "Success", |
| 469 | value: { loggedOut: true }, |
| 470 | })), |
| 471 | ); |
| 472 | await using screen = await renderSection(); |
| 473 | await screen.click("Log out"); |
| 474 | await screen.setProjects([replacement]); |
| 475 | await screen.click("List sessions"); |
| 476 | await expect.element(screen.getByText(session.title)).toBeVisible(); |
| 477 | await expect.element(screen.getByRole("button", { name: "Logging out" })).toBeDisabled(); |
| 478 | await screen.click("Logging out"); |
| 479 | expect(commands.logout).toHaveBeenCalledOnce(); |
| 480 | await act(async () => finish()); |
| 481 | await expect.element(screen.getByText(session.title)).not.toBeInTheDocument(); |
| 482 | await expect.element(screen.getByRole("button", { name: "Log out", exact: true })).toBeEnabled(); |
| 483 | }); |