π OpenHands: AI-Driven Development
fix(backends): keep stacked backend forms on Escape, drop the shared 404 toast, close failed login popups (#18423)
Engel Nyst39e5ef1a116b
diff --git a/__tests__/components/backends/device-flow-login.test.tsx b/__tests__/components/backends/device-flow-login.test.tsxindex e2fa4157cc..14df545a5b 100644--- a/__tests__/components/backends/device-flow-login.test.tsx+++ b/__tests__/components/backends/device-flow-login.test.tsx@@ -544,6 +544,44 @@ describe("device-flow login", () => {expect(flow.reset).not.toHaveBeenCalled();});+ it("closes each attempt's blank popup when the flow fails", async () => {+ const user = userEvent.setup();+ const idleFlow = createFlow();+ const errorFlow = createFlow({+ status: "error",+ error: "Failed to start device flow: Failed to fetch",+ });+ const startingFlow = createFlow({ status: "starting" });+ useDeviceFlowMock.mockReset();+ useDeviceFlowMock+ .mockReturnValueOnce(idleFlow)+ .mockReturnValueOnce(errorFlow)+ .mockReturnValueOnce(startingFlow)+ .mockReturnValue(errorFlow);+ const first = createPopup();+ const second = createPopup();+ const open = vi+ .spyOn(window, "open")+ .mockReturnValueOnce(first.popup)+ .mockReturnValueOnce(second.popup);+ const props = createProps();++ const { rerender, unmount } = render(<DeviceFlowAuth {...props} />);+ await user.click(screen.getByTestId("cloud-login-login-button"));+ rerender(<DeviceFlowAuth {...props} />);+ expect(first.close).toHaveBeenCalledTimes(1);++ await user.click(screen.getByTestId("cloud-login-auth-retry"));+ rerender(<DeviceFlowAuth {...props} />);+ rerender(<DeviceFlowAuth {...props} />);++ expect(open).toHaveBeenCalledTimes(2);+ expect(second.close).toHaveBeenCalledTimes(1);+ unmount();+ expect(first.close).toHaveBeenCalledTimes(1);+ expect(second.close).toHaveBeenCalledTimes(1);+ });+it("shows an authorization error and retries an explicit host", async () => {const user = userEvent.setup();const flow = createFlow({diff --git a/__tests__/components/backends/manage-backends-modal.test.tsx b/__tests__/components/backends/manage-backends-modal.test.tsxindex 29b82f7142..a981af831d 100644--- a/__tests__/components/backends/manage-backends-modal.test.tsx+++ b/__tests__/components/backends/manage-backends-modal.test.tsx@@ -498,6 +498,55 @@ describe("ManageBackendsModal", () => {expect(screen.getByTestId("manage-backends-modal")).toBeInTheDocument();});+ it("keeps the edit form and its draft open on Escape", async () => {+ vi.stubEnv("VITE_LOCK_TO_CLOUD", "");+ delete (window as unknown as Record<string, unknown>)+ .__AGENT_CANVAS_LOCK_TO_CLOUD__;++ const user = userEvent.setup();+ const onClose = vi.fn();++ renderWithProviders(+ <TestSeed+ onMount={(ctx) => {+ ctx.addBackend({+ name: "Acme Local",+ host: "http://localhost:9000",+ apiKey: "old-key",+ kind: "local",+ });+ }}+ >+ <ManageBackendsModal onClose={onClose} />+ </TestSeed>,+ );++ await user.click(+ await screen.findByTestId("manage-backends-edit-Acme Local"),+ );+ const nameInput = await screen.findByTestId("edit-backend-name");+ await user.clear(nameInput);+ await user.type(nameInput, "Draft name");+ await user.keyboard("{Escape}");++ expect(onClose).not.toHaveBeenCalled();+ expect(screen.getByTestId("manage-backends-modal")).toBeInTheDocument();+ expect(screen.getByTestId("edit-backend-name")).toHaveValue("Draft name");+ });++ it("keeps the add form open on Escape", async () => {+ const user = userEvent.setup();+ const onClose = vi.fn();+ renderWithProviders(<ManageBackendsModal onClose={onClose} />);++ await user.click(screen.getByTestId("manage-backends-add"));+ await screen.findByTestId("add-backend-modal");+ await user.keyboard("{Escape}");++ expect(onClose).not.toHaveBeenCalled();+ expect(screen.getByTestId("add-backend-modal")).toBeInTheDocument();+ });+it("shows each cloud backend's connected organization so same-named backends are distinguishable", async () => {// Two cloud backends share the same name and host; each API key is bound// to a different org, which is the only thing that tells them apart.diff --git a/__tests__/hooks/query/use-shared-conversation-events.test.tsx b/__tests__/hooks/query/use-shared-conversation-events.test.tsxnew file mode 100644index 0000000000..f777edaa26--- /dev/null+++ b/__tests__/hooks/query/use-shared-conversation-events.test.tsx@@ -0,0 +1,51 @@+import React from "react";+import { QueryClientProvider } from "@tanstack/react-query";+import { renderHook, waitFor } from "@testing-library/react";+import { afterEach, describe, expect, it, vi } from "vitest";+import { createAgentServerQueryClient } from "#/query-client-config";+import { useSharedConversationEvents } from "#/hooks/query/use-shared-conversation-events";+import * as ToastHandlers from "#/utils/custom-toast-handlers";++const searchSharedEventsMock = vi.hoisted(() => vi.fn());++vi.mock("@openhands/typescript-client/clients", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@openhands/typescript-client/clients")+ >();+ return {+ ...actual,+ SharedClient: vi.fn().mockImplementation(function SharedClient() {+ return { searchSharedEvents: searchSharedEventsMock };+ }),+ };+});++afterEach(() => {+ searchSharedEventsMock.mockReset();+ vi.restoreAllMocks();+});++describe("useSharedConversationEvents", () => {+ it("leaves a missing shared conversation to the page's not-found state, without a toast", async () => {+ const toast = vi+ .spyOn(ToastHandlers, "displayErrorToast")+ .mockImplementation(() => {});+ searchSharedEventsMock.mockRejectedValue(+ new Error('HTTP request failed (404 Not Found): {"detail":"Not Found"}'),+ );+ const client = createAgentServerQueryClient();+ const wrapper = ({ children }: { children: React.ReactNode }) => (+ <QueryClientProvider client={client}>{children}</QueryClientProvider>+ );++ const { result } = renderHook(+ () => useSharedConversationEvents("missing-id"),+ { wrapper },+ );++ await waitFor(() => expect(result.current.isError).toBe(true));+ expect(searchSharedEventsMock).toHaveBeenCalledTimes(1);+ expect(toast).not.toHaveBeenCalled();+ });+});diff --git a/src/components/features/backends/device-flow-auth.tsx b/src/components/features/backends/device-flow-auth.tsxindex 63e4178916..a3ef280a86 100644--- a/src/components/features/backends/device-flow-auth.tsx+++ b/src/components/features/backends/device-flow-auth.tsx@@ -121,6 +121,15 @@ export function DeviceFlowAuth({}}, [deviceFlow.status, deviceFlow.verificationUrl]);+ // A failed flow has nothing left to show in its popup; Try again opens a+ // fresh one.+ React.useEffect(() => {+ if (deviceFlow.status === "error") {+ popupRef.current?.close();+ popupRef.current = null;+ }+ }, [deviceFlow.status]);+// Call onSuccess when authentication completesReact.useEffect(() => {if (deviceFlow.status === "success" && deviceFlow.apiKey) {diff --git a/src/components/features/backends/manage-backends-modal.tsx b/src/components/features/backends/manage-backends-modal.tsxindex dd6c9571b9..5b86ed737c 100644--- a/src/components/features/backends/manage-backends-modal.tsx+++ b/src/components/features/backends/manage-backends-modal.tsx@@ -145,9 +145,14 @@ export function ManageBackendsModal({[lockedCloudBackend, updateBackend],);- // A removal confirmation on top owns Escape and outside clicks; closing this- // modal under it would unmount it mid-stop and swallow a refused destroy.- const isClosable = !recoveryMode && pendingRemoval === null;+ // A modal stacked on top (removal confirmation, Add or Edit form) owns Escape+ // and outside clicks; closing this modal under it would unmount it, losing a+ // form draft or swallowing a refused destroy mid-stop.+ const isClosable =+ !recoveryMode &&+ pendingRemoval === null &&+ editingBackend === null &&+ !showAddForm;return (<>diff --git a/src/hooks/query/use-shared-conversation-events.ts b/src/hooks/query/use-shared-conversation-events.tsindex 53a4811181..3800d9db9c 100644--- a/src/hooks/query/use-shared-conversation-events.ts+++ b/src/hooks/query/use-shared-conversation-events.ts@@ -26,4 +26,6 @@ export const useSharedConversationEvents = (conversationId?: string) =>getNextPageParam: (lastPage) => lastPage.next_page_id ?? undefined,enabled: !!conversationId,retry: false, // Don't retry for shared conversations+ // The shared page renders its own not-found state.+ meta: { disableToast: true },});