File size: 11,928 Bytes
fd2c364 | 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 | /**
* Tests the error-tolerance logic in useBashCommandLogs.
*
* Cloud sandboxes can be paused, starting, errored, or deleted entirely
* β and even when `sandbox_status` says "RUNNING" the runtime can be
* unreachable for a moment. The hook is responsible for translating
* each of these states into a stable `sandboxIssue` code so the modal
* can render a targeted empty state instead of a raw axios error.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { AxiosError, AxiosHeaders } from "axios";
import { HttpError } from "@openhands/typescript-client";
import type { AppConversation } from "#/api/conversation-service/agent-server-conversation-service.types";
import { useBashCommandLogs } from "#/hooks/query/use-bash-command-logs";
// ββ Mocks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const { useUserConversationMock, useActiveBackendMock, listOutputsMock } =
vi.hoisted(() => ({
useUserConversationMock: vi.fn(),
useActiveBackendMock: vi.fn(),
listOutputsMock: vi.fn(),
}));
vi.mock("#/hooks/query/use-user-conversation", () => ({
useUserConversation: (...args: unknown[]) => useUserConversationMock(...args),
}));
vi.mock("#/contexts/active-backend-context", () => ({
useActiveBackend: () => useActiveBackendMock(),
}));
vi.mock("#/api/bash-service/bash-service.api", () => ({
default: { listOutputs: (...args: unknown[]) => listOutputsMock(...args) },
}));
// ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function wrapper({ children }: { children: React.ReactNode }) {
const client = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return <QueryClientProvider client={client}>{children}</QueryClientProvider>;
}
function setActiveBackend(kind: "cloud" | "local") {
useActiveBackendMock.mockReturnValue({
backend: { id: "bk-1", kind },
orgId: null,
});
}
function setConversation(partial: Partial<AppConversation> | null) {
useUserConversationMock.mockReturnValue({
data: partial,
isFetched: true,
isPending: false,
});
}
beforeEach(() => {
useUserConversationMock.mockReset();
useActiveBackendMock.mockReset();
listOutputsMock.mockReset();
});
afterEach(() => {
vi.clearAllMocks();
});
// ββ Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("useBashCommandLogs β cloud sandbox state handling", () => {
beforeEach(() => setActiveBackend("cloud"));
it.each([
{ status: "MISSING" as const, issue: "missing" },
{ status: "PAUSED" as const, issue: "paused" },
{ status: "STARTING" as const, issue: "starting" },
{ status: "ERROR" as const, issue: "errored" },
])(
"reports sandboxIssue=$issue and skips the fetch when sandbox_status=$status",
async ({ status, issue }) => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: status,
});
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
expect(result.current.sandboxIssue).toBe(issue);
// Critical: we did NOT fire a doomed request at a known-bad sandbox.
expect(listOutputsMock).not.toHaveBeenCalled();
},
);
it("reports sandboxIssue=missing when conversation has no runtime URL", () => {
setConversation({
conversation_url: null,
sandbox_status: "RUNNING",
});
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
expect(result.current.sandboxIssue).toBe("missing");
expect(listOutputsMock).not.toHaveBeenCalled();
});
it("reports conversationMissing when the lookup returns null", () => {
setConversation(null);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
expect(result.current.conversationMissing).toBe(true);
expect(result.current.sandboxIssue).toBeNull();
expect(listOutputsMock).not.toHaveBeenCalled();
});
it("fires the fetch and returns data when sandbox is RUNNING", async () => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
listOutputsMock.mockResolvedValueOnce([
{ kind: "BashOutput", stdout: "hi", stderr: null },
]);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() => expect(result.current.data).toBeTruthy());
expect(result.current.sandboxIssue).toBeNull();
expect(listOutputsMock).toHaveBeenCalledTimes(1);
});
it.each([
{ status: 404, label: "404 not-found" },
{ status: 502, label: "502 bad-gateway" },
{ status: 503, label: "503 unavailable" },
{ status: 504, label: "504 gateway-timeout" },
])(
"classifies $label responses as sandboxIssue=unreachable",
async ({ status }) => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
const headers = new AxiosHeaders();
listOutputsMock.mockRejectedValueOnce(
new AxiosError(
`Request failed with status code ${status}`,
"ERR",
{
headers,
} as never,
null,
{
status,
data: {},
statusText: "",
headers,
config: { headers },
} as never,
),
);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() =>
expect(result.current.sandboxIssue).toBe("unreachable"),
);
// Don't double-report: when an issue is detected we hide the raw
// error so the modal doesn't render both the empty state AND the
// generic "Failed: AxiosError" line.
expect(result.current.error).toBeNull();
},
);
it("classifies network errors (no response) as sandboxIssue=unreachable", async () => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
const err = new AxiosError("Network Error", "ERR_NETWORK");
err.response = undefined;
listOutputsMock.mockRejectedValueOnce(err);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() =>
expect(result.current.sandboxIssue).toBe("unreachable"),
);
});
it("does NOT collapse 401/403 into unreachable (auth bugs surface as errors)", async () => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
const headers = new AxiosHeaders();
listOutputsMock.mockRejectedValueOnce(
new AxiosError("Forbidden", "ERR", { headers } as never, null, {
status: 403,
data: {},
statusText: "Forbidden",
headers,
config: { headers },
} as never),
);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() => expect(result.current.error).toBeTruthy());
expect(result.current.sandboxIssue).toBeNull();
});
it.each([
{ status: 404, label: "404 not-found" },
{ status: 503, label: "503 unavailable" },
])(
"classifies shared-client HttpError $label responses as sandboxIssue=unreachable",
async ({ status }) => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
listOutputsMock.mockRejectedValueOnce(
new HttpError(status, "Error", { detail: "sandbox gone" }),
);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() =>
expect(result.current.sandboxIssue).toBe("unreachable"),
);
expect(result.current.error).toBeNull();
},
);
it("does NOT collapse an HttpError 401 into unreachable (auth bugs surface as errors)", async () => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
listOutputsMock.mockRejectedValueOnce(
new HttpError(401, "Unauthorized", { detail: "bad token" }),
);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() => expect(result.current.error).toBeTruthy());
expect(result.current.sandboxIssue).toBeNull();
});
it.each([
{
label: "network TypeError",
makeError: () => new TypeError("Failed to fetch"),
},
{
label: "TimeoutError abort",
makeError: () =>
Object.assign(new Error("The operation timed out"), {
name: "TimeoutError",
}),
},
{
label: "timeout wrapped with cause",
makeError: () =>
new Error("Request timeout after 30000ms", {
cause: Object.assign(new Error("timed out"), {
name: "TimeoutError",
}),
}),
},
])(
"classifies fetch transport failures ($label) as sandboxIssue=unreachable",
async ({ makeError }) => {
setConversation({
conversation_url: "https://runtime.example.com",
sandbox_status: "RUNNING",
});
listOutputsMock.mockRejectedValueOnce(makeError());
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() =>
expect(result.current.sandboxIssue).toBe("unreachable"),
);
},
);
});
describe("useBashCommandLogs β local backend", () => {
beforeEach(() => setActiveBackend("local"));
it("never reports a sandboxIssue (local has no sandbox lifecycle)", async () => {
setConversation({
conversation_url: null,
sandbox_status: null,
});
listOutputsMock.mockResolvedValueOnce([]);
const { result } = renderHook(
() =>
useBashCommandLogs({
conversationId: "conv-1",
bashCommandId: "cmd-1",
}),
{ wrapper },
);
await waitFor(() => expect(result.current.data).toEqual([]));
expect(result.current.sandboxIssue).toBeNull();
// Even network/5xx errors stay as raw errors in local mode β they
// typically mean the local agent-server is misconfigured, not a
// sandbox lifecycle issue.
expect(listOutputsMock).toHaveBeenCalledTimes(1);
});
});
|