π OpenHands: AI-Driven Development
fix(settings): show the active backend's values after a backend switch (#18401)
Engel Nyst2a75b07b495a
diff --git a/__tests__/routes/app-settings.test.tsx b/__tests__/routes/app-settings.test.tsxindex dda0d14f9a..aaa8c7f27d 100644--- a/__tests__/routes/app-settings.test.tsx+++ b/__tests__/routes/app-settings.test.tsx@@ -9,12 +9,13 @@ import { Settings } from "#/types/settings";import ProfilesService from "#/api/profiles-service/profiles-service.api";const activeBackendState = vi.hoisted(() => ({+ id: "local",kind: "local" as "local" | "cloud",}));vi.mock("#/contexts/active-backend-context", () => ({useActiveBackend: () => ({- backend: { kind: activeBackendState.kind },+ backend: { id: activeBackendState.id, kind: activeBackendState.kind },orgId: null,}),}));@@ -35,24 +36,40 @@ function buildSettings(overrides: Partial<Settings> = {}): Settings {}function renderAppSettingsScreen() {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });return render(<AppSettingsScreen />, {wrapper: ({ children }) => (- <QueryClientProvider- client={- new QueryClient({- defaultOptions: { queries: { retry: false } },- })- }- >- {children}- </QueryClientProvider>+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>),});}+/** Serves each backend's own Git identity, as two agent servers would. */+function mockSettingsPerBackend() {+ const gitUserNames: Record<string, string> = {+ local: "openhands",+ "qa-second": "QA Second Git",+ };+ vi.spyOn(SettingsService, "getSettings").mockImplementation(async () =>+ buildSettings({ git_user_name: gitUserNames[activeBackendState.id] }),+ );+}++/** Makes `backendId` active the way the backend selector does: in place. */+function switchBackend(+ rerender: (ui: React.ReactElement) => void,+ backendId: string,+) {+ activeBackendState.id = backendId;+ rerender(<AppSettingsScreen />);+}+describe("AppSettingsScreen", () => {beforeEach(() => {vi.restoreAllMocks();+ activeBackendState.id = "local";activeBackendState.kind = "local";});@@ -199,4 +216,59 @@ describe("AppSettingsScreen", () => {);});});++ it("shows the active backend's values after switching back to a backend with cached settings", async () => {+ mockSettingsPerBackend();+ const { rerender } = renderAppSettingsScreen();+ expect(await screen.findByTestId("git-user-name-input")).toHaveValue(+ "openhands",+ );+ switchBackend(rerender, "qa-second");+ await waitFor(() =>+ expect(screen.getByTestId("git-user-name-input")).toHaveValue(+ "QA Second Git",+ ),+ );++ switchBackend(rerender, "local");++ await waitFor(() =>+ expect(screen.getByTestId("git-user-name-input")).toHaveValue(+ "openhands",+ ),+ );+ });++ it("drops unsaved edits on a backend switch and saves only the active backend's values", async () => {+ mockSettingsPerBackend();+ const saveSettingsSpy = vi+ .spyOn(SettingsService, "saveSettings")+ .mockResolvedValue(true);+ const user = userEvent.setup();+ const { rerender } = renderAppSettingsScreen();+ await screen.findByTestId("git-user-name-input");+ switchBackend(rerender, "qa-second");+ await waitFor(() =>+ expect(screen.getByTestId("git-user-name-input")).toHaveValue(+ "QA Second Git",+ ),+ );+ await user.type(screen.getByTestId("git-user-name-input"), " edited");++ switchBackend(rerender, "local");+ await waitFor(() =>+ expect(screen.getByTestId("git-user-name-input")).toHaveValue(+ "openhands",+ ),+ );+ expect(screen.getByTestId("submit-button")).toBeDisabled();+ await user.click(screen.getByTestId("enable-sound-notifications-switch"));+ await user.click(screen.getByTestId("submit-button"));++ await waitFor(() =>+ expect(saveSettingsSpy).toHaveBeenCalledWith(+ expect.objectContaining({ git_user_name: "openhands" }),+ ),+ );+ });});diff --git a/src/routes/app-settings.tsx b/src/routes/app-settings.tsxindex 2375c2dee6..3ffda1ef40 100644--- a/src/routes/app-settings.tsx+++ b/src/routes/app-settings.tsx@@ -29,7 +29,19 @@ import { getLockedCloudHost } from "#/api/agent-server-config";const AUTOMATIC_TITLE_LLM_PROFILE_KEY = "__automatic__";+/**+ * The form's inputs are uncontrolled and seeded from the loaded settings, so a+ * switch to a backend whose settings are already cached would keep the+ * previous backend's values (and Save would write them to the new backend).+ * Keying by the settings query's backend identity remounts the form with the+ * active backend's values and drops unsaved edits made on the previous one.+ */export function AppSettingsScreen() {+ const { backend, orgId } = useActiveBackend();+ return <AppSettingsForm key={`${backend.id}:${orgId ?? ""}`} />;+}++function AppSettingsForm() {const { t } = useTranslation("openhands");const { mutate: saveSettings, isPending } = useSaveSettings();