Skip to content

Commit 3141075

Browse files
committed
test(webview): pin the unparseable-2xx-body path in the key-info hooks
What: one new case per hook covering a 2xx response whose body is not valid JSON, so response.json() rejects inside the try block. Each asserts the same log call and fallback value as the existing blanket-catch cases. Why: the fetch swap moved body parsing inside the try, so a malformed 2xx body now flows through the catch instead of axios' parse step. That path was uncovered. The transport seam gained an optional jsonRejects field so json() can reject while ok stays true. Impact: no production change. Verified fail-on-shape: with the hook's catch changed to rethrow, the new case fails at isSuccess, so it pins the swallow rather than merely exercising the line.
1 parent ff30dc2 commit 3141075

3 files changed

Lines changed: 57 additions & 6 deletions

File tree

‎webview-ui/src/components/ui/hooks/__tests__/useOpenRouterKeyInfo.spec.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useOpenRouterKeyInfo } from "../useOpenRouterKeyInfo"
99
// assertion below is shared by both phases, so the unchanged green run is the
1010
// behavior-preservation proof.
1111
type TransportCall = { url: string; method: string; headers: Record<string, string> }
12-
type TransportResult = { status: number; body: unknown }
12+
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
1313

1414
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
1515
const calls: TransportCall[] = []
@@ -26,7 +26,12 @@ const stubTransport = (handler: (call: TransportCall) => TransportResult | Promi
2626
return {
2727
ok: result.status >= 200 && result.status < 300,
2828
status: result.status,
29-
json: async () => result.body,
29+
json: async () => {
30+
if (result.jsonRejects) {
31+
throw result.jsonRejects
32+
}
33+
return result.body
34+
},
3035
} as Response
3136
}) as typeof fetch)
3237

@@ -128,4 +133,16 @@ describe("useOpenRouterKeyInfo", () => {
128133
expect(result.current.data).toBeNull()
129134
expect(consoleError).toHaveBeenCalledWith("Error fetching OpenRouter key info:", failure)
130135
})
136+
137+
it("returns null and logs a fetch failure when a 2xx body is not valid JSON", async () => {
138+
const parseFailure = new SyntaxError("Unexpected token '<', \"<html>\" is not valid JSON")
139+
stubTransport(() => ({ status: 200, body: undefined, jsonRejects: parseFailure }))
140+
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {})
141+
142+
const { result } = renderHook(() => useOpenRouterKeyInfo("sk-or-v1-test"), { wrapper })
143+
144+
await waitFor(() => expect(result.current.isSuccess).toBe(true))
145+
expect(result.current.data).toBeNull()
146+
expect(consoleError).toHaveBeenCalledWith("Error fetching OpenRouter key info:", parseFailure)
147+
})
131148
})

‎webview-ui/src/components/ui/hooks/__tests__/useOpenRouterModelProviders.spec.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useOpenRouterModelProviders } from "../useOpenRouterModelProviders"
99
// assertion below is shared by both phases, so the unchanged green run is the
1010
// behavior-preservation proof.
1111
type TransportCall = { url: string; method: string; headers: Record<string, string> }
12-
type TransportResult = { status: number; body: unknown }
12+
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
1313

1414
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
1515
const calls: TransportCall[] = []
@@ -26,7 +26,12 @@ const stubTransport = (handler: (call: TransportCall) => TransportResult | Promi
2626
return {
2727
ok: result.status >= 200 && result.status < 300,
2828
status: result.status,
29-
json: async () => result.body,
29+
json: async () => {
30+
if (result.jsonRejects) {
31+
throw result.jsonRejects
32+
}
33+
return result.body
34+
},
3035
} as Response
3136
}) as typeof fetch)
3237

@@ -177,4 +182,16 @@ describe("useOpenRouterModelProviders", () => {
177182
expect(result.current.data).toEqual({})
178183
expect(consoleError).toHaveBeenCalledWith("Error fetching OpenRouter providers:", failure)
179184
})
185+
186+
it("logs a fetch failure and returns no providers when a 2xx body is not valid JSON", async () => {
187+
const parseFailure = new SyntaxError("Unexpected token '<', \"<html>\" is not valid JSON")
188+
stubTransport(() => ({ status: 200, body: undefined, jsonRejects: parseFailure }))
189+
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {})
190+
191+
const { result } = renderHook(() => useOpenRouterModelProviders("vendor/model"), { wrapper })
192+
193+
await waitFor(() => expect(result.current.isSuccess).toBe(true))
194+
expect(result.current.data).toEqual({})
195+
expect(consoleError).toHaveBeenCalledWith("Error fetching OpenRouter providers:", parseFailure)
196+
})
180197
})

‎webview-ui/src/components/ui/hooks/__tests__/useRequestyKeyInfo.spec.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useRequestyKeyInfo } from "../useRequestyKeyInfo"
99
// assertion below is shared by both phases, so the unchanged green run is the
1010
// behavior-preservation proof.
1111
type TransportCall = { url: string; method: string; headers: Record<string, string> }
12-
type TransportResult = { status: number; body: unknown }
12+
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
1313

1414
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
1515
const calls: TransportCall[] = []
@@ -26,7 +26,12 @@ const stubTransport = (handler: (call: TransportCall) => TransportResult | Promi
2626
return {
2727
ok: result.status >= 200 && result.status < 300,
2828
status: result.status,
29-
json: async () => result.body,
29+
json: async () => {
30+
if (result.jsonRejects) {
31+
throw result.jsonRejects
32+
}
33+
return result.body
34+
},
3035
} as Response
3136
}) as typeof fetch)
3237

@@ -127,4 +132,16 @@ describe("useRequestyKeyInfo", () => {
127132
expect(result.current.data).toBeNull()
128133
expect(consoleError).toHaveBeenCalledWith("Error fetching Requesty key info:", failure)
129134
})
135+
136+
it("returns null and logs a fetch failure when a 2xx body is not valid JSON", async () => {
137+
const parseFailure = new SyntaxError("Unexpected token '<', \"<html>\" is not valid JSON")
138+
stubTransport(() => ({ status: 200, body: undefined, jsonRejects: parseFailure }))
139+
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {})
140+
141+
const { result } = renderHook(() => useRequestyKeyInfo(undefined, "test-key"), { wrapper })
142+
143+
await waitFor(() => expect(result.current.isSuccess).toBe(true))
144+
expect(result.current.data).toBeNull()
145+
expect(consoleError).toHaveBeenCalledWith("Error fetching Requesty key info:", parseFailure)
146+
})
130147
})

0 commit comments

Comments
 (0)