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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import { QueryClient } from "@tanstack/react-query";
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 type { Backend } from "#/api/backend-registry/types";
import {
  __resetHealthStoreForTests,
  getBackendHealthEntry,
  recordBackendFailure,
} from "#/api/backend-registry/health-store";
import * as ToastHandlers from "#/utils/custom-toast-handlers";

interface ErrorOptions {
  directStatus?: boolean;
  message?: string;
  status?: number;
  url?: string;
}

function createAxiosError({
  directStatus = false,
  message = "Request failed",
  status,
  url,
}: ErrorOptions = {}) {
  const error = new AxiosError(
    message,
    "ERR_BAD_REQUEST",
    url ? ({ url } as never) : undefined,
    undefined,
    !directStatus && status !== undefined ? ({ status } as never) : undefined,
  );
  error.status = directStatus ? status : undefined;
  return error;
}

function createBackend(overrides: Partial<Backend> = {}): Backend {
  return {
    id: "local-backend",
    name: "Local Backend",
    host: "http://localhost:3000",
    apiKey: "test-key",
    kind: "local",
    ...overrides,
  };
}

function activateBackend(backend: Backend) {
  const selection = { backendId: backend.id, orgId: null };
  window.localStorage.setItem("openhands-backends", JSON.stringify([backend]));
  window.localStorage.setItem(
    "openhands-active-backend",
    JSON.stringify(selection),
  );
  window.sessionStorage.setItem(
    "openhands-active-backend",
    JSON.stringify(selection),
  );
  __resetActiveStoreForTests();
}

function executeFailingQuery(
  client: QueryClient,
  error: unknown,
  {
    meta,
    queryKey = ["behavior", "failure"],
  }: {
    meta?: Record<string, unknown>;
    queryKey?: readonly unknown[];
  } = {},
) {
  return client.fetchQuery({
    queryKey,
    queryFn: async () => {
      throw error;
    },
    meta,
    retry: false,
  });
}

function executeFailingMutation(
  client: QueryClient,
  error: unknown,
  meta?: Record<string, unknown>,
) {
  const mutation = client.getMutationCache().build(client, {
    mutationFn: async () => {
      throw error;
    },
    meta,
    retry: false,
  });
  return mutation.execute(undefined);
}

afterEach(() => {
  vi.useRealTimers();
  vi.unstubAllEnvs();
  vi.restoreAllMocks();
  window.localStorage.clear();
  window.sessionStorage.clear();
  delete (window as typeof window & { __OH_QUERY_CLIENT__?: QueryClient })
    .__OH_QUERY_CLIENT__;
  __resetActiveStoreForTests();
  __resetHealthStoreForTests();
});

describe("query client behavior", () => {
  it("records successful queries for string backend identifiers only", async () => {
    const client = createAgentServerQueryClient();
    const malformedBackendId = 42;
    const malformedMeta = { backendId: malformedBackendId } as unknown as {
      backendId: string;
    };
    recordBackendFailure("backend-one", new Error("offline"));
    recordBackendFailure(
      malformedBackendId as unknown as string,
      new Error("offline"),
    );

    await client.fetchQuery({
      queryKey: ["health", "backend-one"],
      queryFn: async () => "healthy",
      meta: { backendId: "backend-one" },
    });
    await client.fetchQuery({
      queryKey: ["health", "backend-two"],
      queryFn: async () => "healthy",
      meta: malformedMeta,
    });
    await client.fetchQuery({
      queryKey: ["health", "unattributed"],
      queryFn: async () => "healthy",
    });

    expect(getBackendHealthEntry("backend-one")).toBeNull();
    expect(
      getBackendHealthEntry(malformedBackendId as unknown as string),
    ).not.toBeNull();
  });

  it.each([
    { queryKey: ["settings"], description: "an unrelated query" },
    { queryKey: ["user", "profile"], description: "another user query" },
    {
      queryKey: ["settings", "authenticated"],
      description: "a non-user query ending in authenticated",
    },
  ])(
    "invalidates authentication after a 401 from $description",
    async ({ queryKey }) => {
      const client = createAgentServerQueryClient();
      const invalidateQueries = vi
        .spyOn(client, "invalidateQueries")
        .mockResolvedValue();
      const error = createAxiosError({ status: 401 });

      await expect(
        executeFailingQuery(client, error, {
          meta: { disableToast: true },
          queryKey,
        }),
      ).rejects.toBe(error);

      expect(invalidateQueries).toHaveBeenCalledWith({
        queryKey: ["user", "authenticated"],
      });
    },
  );

  it("does not recursively invalidate authentication when that query fails", async () => {
    const client = createAgentServerQueryClient();
    const invalidateQueries = vi
      .spyOn(client, "invalidateQueries")
      .mockResolvedValue();
    const error = createAxiosError({ status: 401 });

    await expect(
      executeFailingQuery(client, error, {
        meta: { disableToast: true },
        queryKey: ["user", "authenticated"],
      }),
    ).rejects.toBe(error);

    expect(invalidateQueries).not.toHaveBeenCalled();
  });

  it("recognizes a direct Axios status when a mutation receives a 401", async () => {
    const client = createAgentServerQueryClient();
    const invalidateQueries = vi
      .spyOn(client, "invalidateQueries")
      .mockResolvedValue();
    const error = createAxiosError({ directStatus: true, status: 401 });

    await expect(
      executeFailingMutation(client, error, { disableToast: true }),
    ).rejects.toBe(error);

    expect(invalidateQueries).toHaveBeenCalledWith({
      queryKey: ["user", "authenticated"],
    });
  });

  it("does not invalidate authentication for non-401 failures", async () => {
    const client = createAgentServerQueryClient();
    const invalidateQueries = vi
      .spyOn(client, "invalidateQueries")
      .mockResolvedValue();
    const error = createAxiosError({ status: 500 });

    await expect(
      executeFailingQuery(client, error, { meta: { disableToast: true } }),
    ).rejects.toBe(error);

    expect(invalidateQueries).not.toHaveBeenCalled();
  });

  it("preserves null mutation failures without invalidating authentication", async () => {
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const invalidateQueries = vi
      .spyOn(client, "invalidateQueries")
      .mockResolvedValue();

    await expect(executeFailingMutation(client, null)).rejects.toBeNull();

    expect(invalidateQueries).not.toHaveBeenCalled();
    expect(toast).toHaveBeenCalledWith(expect.any(String));
  });

  it.each([
    {
      directStatus: false,
      url: undefined,
      description: "has no request URL",
    },
    {
      directStatus: false,
      url: "https://cloud.example/api/conversations",
      description: "targets the active cloud host",
    },
    {
      directStatus: true,
      url: "https://cloud.example/api/settings",
      description: "reports 401 directly for the active cloud host",
    },
  ])(
    "suppresses a cloud authentication toast when the request $description",
    async ({ directStatus, url }) => {
      activateBackend(
        createBackend({
          id: "cloud-backend",
          name: "Cloud Backend",
          host: "https://cloud.example///",
          kind: "cloud",
        }),
      );
      const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
      const client = createAgentServerQueryClient();
      const error = createAxiosError({
        directStatus,
        message: "Cloud authentication failed",
        status: 401,
        url,
      });

      await expect(
        executeFailingQuery(client, error, {
          queryKey: ["cloud", url ?? "missing-url"],
        }),
      ).rejects.toBe(error);

      expect(toast).not.toHaveBeenCalled();
    },
  );

  it("shows a non-authentication error from the active cloud host", async () => {
    activateBackend(
      createBackend({
        id: "cloud-backend",
        host: "https://cloud.example",
        kind: "cloud",
      }),
    );
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const error = createAxiosError({
      message: "Cloud service unavailable",
      status: 503,
      url: "https://cloud.example/api/settings",
    });

    await expect(
      executeFailingQuery(client, error, {
        queryKey: ["cloud", "service-unavailable"],
      }),
    ).rejects.toBe(error);

    expect(toast).toHaveBeenCalledWith("Cloud service unavailable");
  });

  it("shows a 401 toast when a cloud request targets another host", async () => {
    activateBackend(
      createBackend({
        id: "cloud-backend",
        host: "https://cloud.example",
        kind: "cloud",
      }),
    );
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const error = createAxiosError({
      message: "Foreign host authentication failed",
      status: 401,
      url: "https://different.example/api",
    });

    await expect(
      executeFailingQuery(client, error, {
        queryKey: ["cloud", "foreign-host"],
      }),
    ).rejects.toBe(error);

    expect(toast).toHaveBeenCalledWith("Foreign host authentication failed");
  });

  it("shows a 401 toast for an active local backend", async () => {
    activateBackend(createBackend());
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const error = createAxiosError({
      message: "Local authentication failed",
      status: 401,
    });

    await expect(
      executeFailingQuery(client, error, {
        queryKey: ["local", "authentication"],
      }),
    ).rejects.toBe(error);

    expect(toast).toHaveBeenCalledWith("Local authentication failed");
  });

  it("deduplicates query toasts until the cooldown expires", async () => {
    vi.useFakeTimers();
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const first = new AxiosError("Repeated query failure");
    const second = new AxiosError("Repeated query failure");

    await expect(
      executeFailingQuery(client, first, {
        queryKey: ["dedupe", "first"],
      }),
    ).rejects.toBe(first);
    await expect(
      executeFailingQuery(client, second, {
        queryKey: ["dedupe", "second"],
      }),
    ).rejects.toBe(second);
    expect(toast).toHaveBeenCalledTimes(1);

    await vi.advanceTimersByTimeAsync(3000);
    const afterCooldown = new AxiosError("Repeated query failure");
    await expect(
      executeFailingQuery(client, afterCooldown, {
        queryKey: ["dedupe", "after-cooldown"],
      }),
    ).rejects.toBe(afterCooldown);

    expect(toast).toHaveBeenCalledTimes(2);
  });

  it("uses the translated generic query error when no message is available", async () => {
    vi.useFakeTimers();
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const first = {};
    const duplicate = {};

    await expect(
      executeFailingQuery(client, first, {
        queryKey: ["generic", "first"],
      }),
    ).rejects.toBe(first);
    await expect(
      executeFailingQuery(client, duplicate, {
        queryKey: ["generic", "duplicate"],
      }),
    ).rejects.toBe(duplicate);

    expect(toast).toHaveBeenCalledWith(expect.any(String));
    expect(toast).toHaveBeenCalledTimes(1);

    await vi.advanceTimersByTimeAsync(3000);
    const afterCooldown = {};
    await expect(
      executeFailingQuery(client, afterCooldown, {
        queryKey: ["generic", "after-cooldown"],
      }),
    ).rejects.toBe(afterCooldown);

    expect(toast).toHaveBeenCalledTimes(2);
  });

  it("honors mutation toast metadata", async () => {
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const visible = new AxiosError("Visible mutation failure");
    const suppressed = new AxiosError("Suppressed mutation failure");

    await expect(executeFailingMutation(client, visible)).rejects.toBe(visible);
    await expect(
      executeFailingMutation(client, suppressed, { disableToast: true }),
    ).rejects.toBe(suppressed);

    expect(toast).toHaveBeenCalledOnce();
    expect(toast).toHaveBeenCalledWith("Visible mutation failure");
  });

  it("suppresses matching cloud-auth mutation errors", async () => {
    activateBackend(
      createBackend({
        id: "cloud-backend",
        host: "https://cloud.example",
        kind: "cloud",
      }),
    );
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const error = createAxiosError({
      message: "Cloud mutation authentication failed",
      status: 401,
      url: "https://cloud.example/api/settings",
    });

    await expect(executeFailingMutation(client, error)).rejects.toBe(error);

    expect(toast).not.toHaveBeenCalled();
  });

  it("uses the translated generic mutation error when no message is available", async () => {
    const toast = vi.spyOn(ToastHandlers, "displayErrorToast");
    const client = createAgentServerQueryClient();
    const error = {};

    await expect(executeFailingMutation(client, error)).rejects.toBe(error);

    expect(toast).toHaveBeenCalledWith(expect.any(String));
  });
});

describe("query client selection and proxy behavior", () => {
  it("creates one default client and exposes it in development", async () => {
    vi.resetModules();
    const config = await import("#/query-client-config");

    const first = config.getDefaultQueryClient();
    const second = config.getDefaultQueryClient();

    expect(first).toBeInstanceOf(QueryClient);
    expect(second).toBe(first);
    expect(config.getQueryClient()).toBe(first);
    expect(
      (window as typeof window & { __OH_QUERY_CLIENT__?: QueryClient })
        .__OH_QUERY_CLIENT__,
    ).toBe(first);
  });

  it("selects custom clients and forwards proxy reads, calls, and writes", async () => {
    vi.resetModules();
    const config = await import("#/query-client-config");
    const custom = new QueryClient();

    expect(config.setQueryClient(custom)).toBe(custom);
    expect(config.getQueryClient()).toBe(custom);

    config.queryClient.setQueryData(["proxy", "value"], "forwarded");
    expect(custom.getQueryData(["proxy", "value"])).toBe("forwarded");

    const extendedProxy = config.queryClient as QueryClient & {
      marker?: string;
    };
    const extendedClient = custom as QueryClient & { marker?: string };
    extendedProxy.marker = "proxy-write";
    expect(extendedProxy.marker).toBe("proxy-write");
    expect(extendedClient.marker).toBe("proxy-write");

    expect(config.setQueryClient(undefined)).toBe(
      config.getDefaultQueryClient(),
    );
    expect(config.setQueryClient(null)).toBe(config.getDefaultQueryClient());
  });

  it.each([
    { mockApi: "true", expectedExposure: true },
    { mockApi: "false", expectedExposure: false },
  ])(
    "sets window exposure to $expectedExposure outside development when VITE_MOCK_API is $mockApi",
    async ({ expectedExposure, mockApi }) => {
      vi.stubEnv("DEV", false);
      vi.stubEnv("VITE_MOCK_API", mockApi);
      vi.resetModules();
      const config = await import("#/query-client-config");

      const client = config.getDefaultQueryClient();
      const exposed = (
        window as typeof window & { __OH_QUERY_CLIENT__?: QueryClient }
      ).__OH_QUERY_CLIENT__;

      if (expectedExposure) {
        expect(exposed).toBe(client);
      } else {
        expect(exposed).toBeUndefined();
      }
    },
  );
});