Skip to content

Commit 1eac043

Browse files
committed
test(webview): strengthen fetch hook coverage and lint guardrails
1 parent ca0ab53 commit 1eac043

6 files changed

Lines changed: 68 additions & 115 deletions

File tree

‎webview-ui/eslint.config.mjs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ export default [
8080
"error",
8181
{
8282
paths: [
83+
{
84+
name: "axios",
85+
message:
86+
"axios was removed from the webview bundle in favour of global fetch. Use fetch() for network calls. The dependency is no longer declared in webview-ui/package.json, so an import here only resolves through the hoisted store and would fail at build time.",
87+
},
8388
{
8489
name: "@tanstack/react-query",
8590
importNames: ["QueryClientProvider"],

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

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,9 @@ import { renderHook, waitFor } from "@testing-library/react"
22
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
33
import React from "react"
44

5-
import { useOpenRouterKeyInfo } from "../useOpenRouterKeyInfo"
6-
7-
// --- transport seam (phase 2: fetch) -----------------------------------------
8-
// Only this block changed between phase 1 (axios) and phase 2 (fetch). Every
9-
// assertion below is shared by both phases, so the unchanged green run is the
10-
// behavior-preservation proof.
11-
type TransportCall = { url: string; method: string; headers: Record<string, string> }
12-
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
13-
14-
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
15-
const calls: TransportCall[] = []
16-
17-
vi.spyOn(globalThis, "fetch").mockImplementation((async (input: string, init?: RequestInit) => {
18-
const call: TransportCall = {
19-
url: String(input),
20-
method: init?.method ?? "GET",
21-
headers: { ...((init?.headers ?? {}) as Record<string, string>) },
22-
}
23-
calls.push(call)
24-
const result = await handler(call)
25-
26-
return {
27-
ok: result.status >= 200 && result.status < 300,
28-
status: result.status,
29-
json: async () => {
30-
if (result.jsonRejects) {
31-
throw result.jsonRejects
32-
}
33-
return result.body
34-
},
35-
} as Response
36-
}) as typeof fetch)
5+
import { stubTransport } from "@src/utils/test-utils"
376

38-
return { calls }
39-
}
40-
// -----------------------------------------------------------------------------
7+
import { useOpenRouterKeyInfo } from "../useOpenRouterKeyInfo"
418

429
const KEY_INFO = {
4310
label: "sk-or-v1-...abc",
@@ -82,7 +49,7 @@ describe("useOpenRouterKeyInfo", () => {
8249
{
8350
url: "https://openrouter.ai/api/v1/key",
8451
method: "GET",
85-
headers: { Authorization: "Bearer sk-or-v1-test" },
52+
headers: { authorization: "Bearer sk-or-v1-test" },
8653
},
8754
])
8855
})

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

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,10 @@ import { renderHook, waitFor } from "@testing-library/react"
22
import { QueryClientProvider } from "@tanstack/react-query"
33
import React from "react"
44

5-
import { createTestQueryClient } from "@src/utils/test-utils"
5+
import { createTestQueryClient, stubTransport } from "@src/utils/test-utils"
66

77
import { useOpenRouterModelProviders } from "../useOpenRouterModelProviders"
88

9-
// --- transport seam (phase 2: fetch) -----------------------------------------
10-
// Only this block changed between phase 1 (axios) and phase 2 (fetch). Every
11-
// assertion below is shared by both phases, so the unchanged green run is the
12-
// behavior-preservation proof.
13-
type TransportCall = { url: string; method: string; headers: Record<string, string> }
14-
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
15-
16-
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
17-
const calls: TransportCall[] = []
18-
19-
vi.spyOn(globalThis, "fetch").mockImplementation((async (input: string, init?: RequestInit) => {
20-
const call: TransportCall = {
21-
url: String(input),
22-
method: init?.method ?? "GET",
23-
headers: { ...((init?.headers ?? {}) as Record<string, string>) },
24-
}
25-
calls.push(call)
26-
const result = await handler(call)
27-
28-
return {
29-
ok: result.status >= 200 && result.status < 300,
30-
status: result.status,
31-
json: async () => {
32-
if (result.jsonRejects) {
33-
throw result.jsonRejects
34-
}
35-
return result.body
36-
},
37-
} as Response
38-
}) as typeof fetch)
39-
40-
return { calls }
41-
}
42-
// -----------------------------------------------------------------------------
43-
449
const endpointsPayload = (overrides: Record<string, unknown> = {}) => ({
4510
data: {
4611
id: "vendor/model",
@@ -53,7 +18,12 @@ const endpointsPayload = (overrides: Record<string, unknown> = {}) => ({
5318
tag: "provider-a",
5419
context_length: 128000,
5520
max_completion_tokens: 4096,
56-
pricing: { prompt: "0.000003", completion: "0.000015", input_cache_read: "0.0000003" },
21+
pricing: {
22+
prompt: "0.000003",
23+
completion: "0.000015",
24+
input_cache_read: "0.0000003",
25+
input_cache_write: "0.0000036",
26+
},
5727
},
5828
{
5929
name: "Provider B",
@@ -102,7 +72,7 @@ describe("useOpenRouterModelProviders", () => {
10272
supportsImages: true,
10373
supportsPromptCache: true,
10474
cacheReadsPrice: 0.3,
105-
cacheWritesPrice: undefined,
75+
cacheWritesPrice: expect.closeTo(3.6, 10),
10676
inputPrice: 3,
10777
outputPrice: 15,
10878
description: "A test model",
@@ -123,6 +93,21 @@ describe("useOpenRouterModelProviders", () => {
12393
})
12494
})
12595

96+
it.each([
97+
{ name: "text-only architecture", architecture: { input_modalities: ["text"], output_modalities: ["text"] } },
98+
{ name: "omitted architecture", architecture: undefined },
99+
])("disables image support for $name", async ({ architecture }) => {
100+
stubTransport(() => ({ status: 200, body: endpointsPayload({ architecture }) }))
101+
102+
const { result } = renderHook(() => useOpenRouterModelProviders("vendor/model"), { wrapper })
103+
104+
await waitFor(() => expect(result.current.isSuccess).toBe(true))
105+
expect(result.current.data).toMatchObject({
106+
"provider-a": { supportsImages: false },
107+
"Provider B": { supportsImages: false },
108+
})
109+
})
110+
126111
it("uses a caller-supplied baseUrl", async () => {
127112
const { calls } = stubTransport(() => ({ status: 200, body: endpointsPayload() }))
128113

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

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,9 @@ import { renderHook, waitFor } from "@testing-library/react"
22
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
33
import React from "react"
44

5-
import { useRequestyKeyInfo } from "../useRequestyKeyInfo"
6-
7-
// --- transport seam (phase 2: fetch) -----------------------------------------
8-
// Only this block changed between phase 1 (axios) and phase 2 (fetch). Every
9-
// assertion below is shared by both phases, so the unchanged green run is the
10-
// behavior-preservation proof.
11-
type TransportCall = { url: string; method: string; headers: Record<string, string> }
12-
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
13-
14-
const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
15-
const calls: TransportCall[] = []
16-
17-
vi.spyOn(globalThis, "fetch").mockImplementation((async (input: string, init?: RequestInit) => {
18-
const call: TransportCall = {
19-
url: String(input),
20-
method: init?.method ?? "GET",
21-
headers: { ...((init?.headers ?? {}) as Record<string, string>) },
22-
}
23-
calls.push(call)
24-
const result = await handler(call)
25-
26-
return {
27-
ok: result.status >= 200 && result.status < 300,
28-
status: result.status,
29-
json: async () => {
30-
if (result.jsonRejects) {
31-
throw result.jsonRejects
32-
}
33-
return result.body
34-
},
35-
} as Response
36-
}) as typeof fetch)
5+
import { stubTransport } from "@src/utils/test-utils"
376

38-
return { calls }
39-
}
40-
// -----------------------------------------------------------------------------
7+
import { useRequestyKeyInfo } from "../useRequestyKeyInfo"
418

429
const KEY_INFO = {
4310
name: "my-key",
@@ -81,7 +48,7 @@ describe("useRequestyKeyInfo", () => {
8148
{
8249
url: "https://api.requesty.ai/x/apikey",
8350
method: "GET",
84-
headers: { Authorization: "Bearer test-key", "Content-Type": "application/json" },
51+
headers: { authorization: "Bearer test-key", "content-type": "application/json" },
8552
},
8653
])
8754
})

‎webview-ui/src/components/ui/hooks/useOpenRouterModelProviders.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ async function getOpenRouterProvidersForModel(modelId: string, baseUrl?: string)
9090
models[providerName] = modelInfo
9191
}
9292
} catch (error) {
93-
if (error instanceof z.ZodError) {
94-
console.error(`OpenRouter API response validation failed:`, error.issues)
95-
} else {
96-
console.error(`Error fetching OpenRouter providers:`, error)
97-
}
93+
console.error(`Error fetching OpenRouter providers:`, error)
9894
}
9995

10096
return models

‎webview-ui/src/utils/test-utils.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,36 @@ export * from "@testing-library/react"
8484

8585
// override render method
8686
export { customRender as render }
87+
88+
type TransportCall = { url: string; method: string; headers: Record<string, string> }
89+
type TransportResult = { status: number; body: unknown; jsonRejects?: Error }
90+
91+
// Stubs global fetch and records the calls.
92+
export const stubTransport = (handler: (call: TransportCall) => TransportResult | Promise<TransportResult>) => {
93+
const calls: TransportCall[] = []
94+
95+
vi.spyOn(globalThis, "fetch").mockImplementation(async (input, init) => {
96+
const request = typeof input === "object" && "url" in input ? input : undefined
97+
const call: TransportCall = {
98+
url: request?.url ?? String(input),
99+
method: init?.method ?? request?.method ?? "GET",
100+
headers: Object.fromEntries(new Headers(init?.headers ?? request?.headers).entries()),
101+
}
102+
calls.push(call)
103+
const result = await handler(call)
104+
105+
// The hooks consume only ok, status, and json from this response double.
106+
return {
107+
ok: result.status >= 200 && result.status < 300,
108+
status: result.status,
109+
json: async () => {
110+
if (result.jsonRejects) {
111+
throw result.jsonRejects
112+
}
113+
return result.body
114+
},
115+
} as Response
116+
})
117+
118+
return { calls }
119+
}

0 commit comments

Comments
 (0)