File size: 2,892 Bytes
c0af099
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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();
  });
});