Files
OpenHands/__tests__/query-client-config.test.ts
3c40e8fdc4 Fix Cloud credential expiration recovery (#1383)
* Fix cloud credential expiration recovery

Co-authored-by: openhands <openhands@all-hands.dev>

* Improve cloud re-login dialog styling

Co-authored-by: openhands <openhands@all-hands.dev>

* Use icon-only cloud re-login button

Co-authored-by: openhands <openhands@all-hands.dev>

* Use retry icon for cloud re-login

Co-authored-by: openhands <openhands@all-hands.dev>

* Fix root recovery test for icon login

Co-authored-by: openhands <openhands@all-hands.dev>

* chore: Remove PR-only artifacts

---------

Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: allhands-bot <allhands-bot@users.noreply.github.com>
Co-authored-by: Rohit Malhotra <rohitvinodmalhotra@gmail.com>
2026-06-16 21:03:03 +00:00

93 lines
2.8 KiB
TypeScript

import { AxiosError } from "axios";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createAgentServerQueryClient } from "#/query-client-config";
import { __resetActiveStoreForTests } from "#/api/backend-registry/active-store";
import * as ToastHandlers from "#/utils/custom-toast-handlers";
afterEach(() => {
window.localStorage.clear();
window.sessionStorage.clear();
__resetActiveStoreForTests();
vi.restoreAllMocks();
});
describe("createAgentServerQueryClient", () => {
it("does not show a toast when query meta disables toasts", async () => {
const toastSpy = vi.spyOn(ToastHandlers, "displayErrorToast");
const client = createAgentServerQueryClient();
await expect(
client.fetchQuery({
queryKey: ["config", "suppressed"],
queryFn: async () => {
throw new AxiosError("suppressed query error");
},
meta: { disableToast: true },
retry: false,
}),
).rejects.toThrow("suppressed query error");
expect(toastSpy).not.toHaveBeenCalled();
});
it("shows a toast when query meta does not disable toasts", async () => {
const toastSpy = vi.spyOn(ToastHandlers, "displayErrorToast");
const client = createAgentServerQueryClient();
await expect(
client.fetchQuery({
queryKey: ["config", "toast"],
queryFn: async () => {
throw new AxiosError("query error with toast");
},
retry: false,
}),
).rejects.toThrow("query error with toast");
expect(toastSpy).toHaveBeenCalledWith("query error with toast");
});
it("does not show raw 401 toasts while the active cloud backend is logged out", async () => {
const toastSpy = vi.spyOn(ToastHandlers, "displayErrorToast");
const backend = {
id: "cloud-expired",
name: "OpenHands Cloud",
host: "https://app.all-hands.dev",
apiKey: "expired-token",
kind: "cloud",
};
window.localStorage.setItem(
"openhands-backends",
JSON.stringify([backend]),
);
window.localStorage.setItem(
"openhands-active-backend",
JSON.stringify({ backendId: backend.id, orgId: null }),
);
window.sessionStorage.setItem(
"openhands-active-backend",
JSON.stringify({ backendId: backend.id, orgId: null }),
);
__resetActiveStoreForTests();
const client = createAgentServerQueryClient();
await expect(
client.fetchQuery({
queryKey: ["cloud", "logged-out"],
queryFn: async () => {
throw new AxiosError(
"Request failed with status code 401",
"ERR_BAD_REQUEST",
undefined,
undefined,
{ status: 401 } as never,
);
},
retry: false,
}),
).rejects.toThrow("Request failed with status code 401");
expect(toastSpy).not.toHaveBeenCalled();
});
});