|
| 1 | +/** |
| 2 | + * Tests for the error-classification behaviour of `checkAgentName`. |
| 3 | + * |
| 4 | + * Issue #3041: when the backend returns a non-200 response (e.g. a 500 with |
| 5 | + * a database error, a 422 from misbehaving routing, or any other 4xx/5xx |
| 6 | + * not in the 502/503/504 set), the UI used to swallow the backend detail |
| 7 | + * into a generic "Could not verify name availability" fallback because the |
| 8 | + * page-level catch block only handled `reason === "backend_unreachable"`. |
| 9 | + * |
| 10 | + * The fix carries the raw backend detail as `AgentNameCheckError.detail` |
| 11 | + * (distinct from `message`, which always has a non-empty value because |
| 12 | + * `checkAgentName` substitutes a generated fallback when the backend sent |
| 13 | + * no detail). The UI uses `detail` to decide whether to surface a real |
| 14 | + * backend string or fall back to the localised "could not verify" copy. |
| 15 | + * |
| 16 | + * These tests pin both halves of the contract so a future refactor doesn't |
| 17 | + * silently drop the detail or leak the generated fallback into the UI. |
| 18 | + */ |
| 19 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 20 | + |
| 21 | +vi.mock("@/core/api/fetcher", () => ({ |
| 22 | + fetch: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("@/core/config", () => ({ |
| 26 | + getBackendBaseURL: () => "", |
| 27 | +})); |
| 28 | + |
| 29 | +import { AgentsApiDisabledError, checkAgentName } from "@/core/agents/api"; |
| 30 | +import { fetch as fetcher } from "@/core/api/fetcher"; |
| 31 | + |
| 32 | +const mockedFetch = vi.mocked(fetcher); |
| 33 | + |
| 34 | +function jsonResponse(status: number, body: unknown): Response { |
| 35 | + return new Response(JSON.stringify(body), { |
| 36 | + status, |
| 37 | + headers: { "Content-Type": "application/json" }, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +beforeEach(() => { |
| 42 | + mockedFetch.mockReset(); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("checkAgentName", () => { |
| 46 | + test("returns availability payload on 200", async () => { |
| 47 | + mockedFetch.mockResolvedValueOnce( |
| 48 | + jsonResponse(200, { available: true, name: "dealagent" }), |
| 49 | + ); |
| 50 | + const result = await checkAgentName("dealagent"); |
| 51 | + expect(result).toEqual({ available: true, name: "dealagent" }); |
| 52 | + }); |
| 53 | + |
| 54 | + test("treats network-layer fetch rejection as backend_unreachable", async () => { |
| 55 | + mockedFetch.mockRejectedValueOnce(new TypeError("Failed to fetch")); |
| 56 | + await expect(checkAgentName("dealagent")).rejects.toMatchObject({ |
| 57 | + name: "AgentNameCheckError", |
| 58 | + reason: "backend_unreachable", |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + test.each([502, 503, 504])( |
| 63 | + "treats HTTP %i as backend_unreachable", |
| 64 | + async (status) => { |
| 65 | + mockedFetch.mockResolvedValueOnce( |
| 66 | + jsonResponse(status, { detail: "Bad Gateway" }), |
| 67 | + ); |
| 68 | + await expect(checkAgentName("dealagent")).rejects.toMatchObject({ |
| 69 | + name: "AgentNameCheckError", |
| 70 | + reason: "backend_unreachable", |
| 71 | + }); |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + test("recognises agents_api disabled detail and throws AgentsApiDisabledError", async () => { |
| 76 | + const detail = |
| 77 | + "Custom-agent management API is disabled. Set agents_api.enabled=true to expose agent and user-profile routes over HTTP."; |
| 78 | + mockedFetch.mockResolvedValueOnce(jsonResponse(403, { detail })); |
| 79 | + await expect(checkAgentName("dealagent")).rejects.toBeInstanceOf( |
| 80 | + AgentsApiDisabledError, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + test("carries backend 422 detail through AgentNameCheckError.detail (issue #3041)", async () => { |
| 85 | + // This is the exact response shape produced by `_validate_agent_name` |
| 86 | + // when the user submits a name with disallowed characters — e.g. a |
| 87 | + // trailing space, a dot, a Chinese character, or invisible whitespace |
| 88 | + // pasted in from another window. |
| 89 | + const detail = |
| 90 | + "Invalid agent name 'deal agent'. Must match ^[A-Za-z0-9-]+$ (letters, digits, and hyphens only)."; |
| 91 | + mockedFetch.mockResolvedValueOnce(jsonResponse(422, { detail })); |
| 92 | + |
| 93 | + await expect(checkAgentName("deal agent")).rejects.toMatchObject({ |
| 94 | + name: "AgentNameCheckError", |
| 95 | + reason: "request_failed", |
| 96 | + // The full detail is preserved on both `detail` (for the UI to |
| 97 | + // recognise "real backend detail vs generated fallback") and |
| 98 | + // `message` (for stack traces / logs). |
| 99 | + detail, |
| 100 | + message: detail, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + test("falls back to statusText in message but leaves detail null when backend returns no detail", async () => { |
| 105 | + // The fallback message must NOT mask the absence of a real backend |
| 106 | + // detail — the page-level catch relies on `detail === null` to choose |
| 107 | + // the localised generic fallback rather than rendering the bare |
| 108 | + // "Failed to check agent name: Internal Server Error" string. |
| 109 | + mockedFetch.mockResolvedValueOnce( |
| 110 | + new Response("", { status: 500, statusText: "Internal Server Error" }), |
| 111 | + ); |
| 112 | + await expect(checkAgentName("dealagent")).rejects.toMatchObject({ |
| 113 | + name: "AgentNameCheckError", |
| 114 | + reason: "request_failed", |
| 115 | + detail: null, |
| 116 | + message: expect.stringContaining("Internal Server Error"), |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test("treats non-string detail as null (defence against future schema drift)", async () => { |
| 121 | + // If the backend ever returns `{detail: {code, message}}` (the shape |
| 122 | + // used by auth errors today) on this endpoint, we must not surface a |
| 123 | + // `[object Object]` string. `detail` should fall back to null so the |
| 124 | + // page uses its localised fallback. |
| 125 | + mockedFetch.mockResolvedValueOnce( |
| 126 | + jsonResponse(500, { detail: { code: "x", message: "y" } }), |
| 127 | + ); |
| 128 | + await expect(checkAgentName("dealagent")).rejects.toMatchObject({ |
| 129 | + name: "AgentNameCheckError", |
| 130 | + reason: "request_failed", |
| 131 | + detail: null, |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + test("does not misclassify a 422 with unrelated detail as agents_api disabled", async () => { |
| 136 | + // Defence-in-depth: the disabled detector matches on the substring |
| 137 | + // "agents_api.enabled", so a 422 whose detail accidentally contains |
| 138 | + // the same substring would be misclassified. The validation detail |
| 139 | + // produced by `_validate_agent_name` never contains it; this test |
| 140 | + // simply asserts that "Invalid agent name ..." stays in the |
| 141 | + // request_failed branch, which is where the page now surfaces it. |
| 142 | + const detail = |
| 143 | + "Invalid agent name 'deal.agent'. Must match ^[A-Za-z0-9-]+$ (letters, digits, and hyphens only)."; |
| 144 | + mockedFetch.mockResolvedValueOnce(jsonResponse(422, { detail })); |
| 145 | + await expect(checkAgentName("deal.agent")).rejects.not.toBeInstanceOf( |
| 146 | + AgentsApiDisabledError, |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments