Skip to content

Commit 866b56f

Browse files
committed
test: bumping coverage
1 parent 843d235 commit 866b56f

9 files changed

Lines changed: 273 additions & 12 deletions

File tree

‎src/api/providers/__tests__/native-ollama.spec.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// npx vitest run api/providers/__tests__/native-ollama.spec.ts
1+
// pnpm exec vitest run api/providers/__tests__/native-ollama.spec.ts
2+
3+
import { Anthropic } from "@anthropic-ai/sdk"
24

35
import { NativeOllamaHandler } from "../native-ollama"
46
import { ApiHandlerOptions } from "../../../shared/api"
@@ -81,6 +83,55 @@ describe("NativeOllamaHandler", () => {
8183
expect(results[2]).toEqual({ type: "usage", inputTokens: 10, outputTokens: 2 })
8284
})
8385

86+
it("should map tool_result array content to a concatenated string, flushing base64 images", async () => {
87+
mockChat.mockImplementation(async function* () {
88+
yield { message: { content: "ok" } }
89+
})
90+
91+
const messages: Anthropic.Messages.MessageParam[] = [
92+
{
93+
role: "user",
94+
content: [
95+
{
96+
type: "tool_result",
97+
tool_use_id: "tool-1",
98+
content: [
99+
{ type: "text", text: "line one" },
100+
{
101+
type: "image",
102+
source: {
103+
type: "base64",
104+
media_type: "image/png",
105+
data: "imgdata",
106+
},
107+
},
108+
{ type: "text", text: "line two" },
109+
],
110+
},
111+
],
112+
},
113+
]
114+
115+
const stream = handler.createMessage("System", messages)
116+
for await (const _ of stream) {
117+
// consume stream
118+
}
119+
120+
// Text blocks are joined with "\n"; the image emits a placeholder and is
121+
// flushed separately via the `images` field rather than inlined.
122+
expect(mockChat).toHaveBeenCalledWith(
123+
expect.objectContaining({
124+
messages: expect.arrayContaining([
125+
expect.objectContaining({
126+
role: "user",
127+
content: "line one\n(see following user message for image)\nline two",
128+
images: ["imgdata"],
129+
}),
130+
]),
131+
}),
132+
)
133+
})
134+
84135
it("should not include num_ctx by default", async () => {
85136
// Mock the chat response
86137
mockChat.mockImplementation(async function* () {

‎src/api/providers/__tests__/openai-codex.spec.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// npx vitest run api/providers/__tests__/openai-codex.spec.ts
22

3+
import { Anthropic } from "@anthropic-ai/sdk"
34
import { OpenAiCodexHandler } from "../openai-codex"
5+
import { openAiCodexOAuthManager } from "../../../integrations/openai-codex/oauth"
46

57
describe("OpenAiCodexHandler.getModel", () => {
68
it.each(["gpt-5.1", "gpt-5", "gpt-5.1-codex", "gpt-5-codex", "gpt-5-codex-mini", "gpt-5.3-codex-spark"])(
@@ -42,3 +44,54 @@ describe("OpenAiCodexHandler.getModel", () => {
4244
expect(model.info).toBeDefined()
4345
})
4446
})
47+
48+
describe("OpenAiCodexHandler.createMessage", () => {
49+
it("should skip URL-sourced images in formatFullConversation (only base64 emits input_image)", async () => {
50+
const handler = new OpenAiCodexHandler({ apiModelId: "gpt-5.1-codex" })
51+
52+
vitest.spyOn(openAiCodexOAuthManager, "getAccessToken").mockResolvedValue("test-token")
53+
vitest.spyOn(openAiCodexOAuthManager, "getAccountId").mockResolvedValue("acct_test")
54+
55+
const capturedInput: any[] = []
56+
;(handler as any).client = {
57+
responses: {
58+
create: vitest.fn().mockImplementation(async (body: any) => {
59+
capturedInput.push(...(body.input ?? []))
60+
return {
61+
async *[Symbol.asyncIterator]() {
62+
yield {
63+
type: "response.completed",
64+
response: {
65+
id: "r1",
66+
status: "completed",
67+
output: [],
68+
usage: { input_tokens: 1, output_tokens: 1 },
69+
},
70+
}
71+
},
72+
}
73+
}),
74+
},
75+
}
76+
77+
const messages: Anthropic.Messages.MessageParam[] = [
78+
{
79+
role: "user",
80+
content: [
81+
{ type: "text", text: "Look at this:" },
82+
{ type: "image", source: { type: "url", url: "https://example.com/img.png" } as any },
83+
],
84+
},
85+
]
86+
87+
const stream = handler.createMessage("system", messages)
88+
for await (const _ of stream) {
89+
// consume
90+
}
91+
92+
// URL image is skipped; only the text input_text block should be present
93+
const userMsg = capturedInput.find((item: any) => item.role === "user")
94+
expect(userMsg?.content).toEqual([{ type: "input_text", text: "Look at this:" }])
95+
expect(JSON.stringify(capturedInput)).not.toContain("input_image")
96+
})
97+
})

‎src/api/providers/__tests__/openai-native.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,4 +1845,54 @@ describe("GPT-5 streaming event coverage (additional)", () => {
18451845
})
18461846
})
18471847
})
1848+
1849+
describe("URL image handling", () => {
1850+
it("should skip URL-sourced images in formatFullConversation (only base64 emits input_image)", async () => {
1851+
const mockFetch = vitest.fn().mockResolvedValue({
1852+
ok: true,
1853+
body: new ReadableStream({
1854+
start(controller) {
1855+
controller.enqueue(
1856+
new TextEncoder().encode('data: {"type":"response.output_text.delta","delta":"ok"}\n\n'),
1857+
)
1858+
controller.enqueue(new TextEncoder().encode("data: [DONE]\n\n"))
1859+
controller.close()
1860+
},
1861+
}),
1862+
})
1863+
global.fetch = mockFetch as any
1864+
1865+
mockResponsesCreate.mockRejectedValue(new Error("SDK not available"))
1866+
1867+
const localHandler = new OpenAiNativeHandler({
1868+
apiModelId: "gpt-4.1",
1869+
openAiNativeApiKey: "test-api-key",
1870+
})
1871+
1872+
const urlImageMessages: Anthropic.Messages.MessageParam[] = [
1873+
{
1874+
role: "user",
1875+
content: [
1876+
{ type: "text", text: "Look at this:" },
1877+
{
1878+
type: "image",
1879+
source: { type: "url", url: "https://example.com/img.png" } as any,
1880+
},
1881+
],
1882+
},
1883+
]
1884+
1885+
const stream = localHandler.createMessage("You are a helpful assistant.", urlImageMessages)
1886+
for await (const _ of stream) {
1887+
// consume
1888+
}
1889+
1890+
const bodyStr = (mockFetch.mock.calls[0][1] as any).body as string
1891+
const parsedBody = JSON.parse(bodyStr)
1892+
// URL image is skipped; only the text part is in the input
1893+
const userMsg = parsedBody.input[0]
1894+
expect(userMsg.content).toEqual([{ type: "input_text", text: "Look at this:" }])
1895+
expect(bodyStr).not.toContain("input_image")
1896+
})
1897+
})
18481898
})

‎src/api/transform/__tests__/openai-format.spec.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// npx vitest run api/transform/__tests__/openai-format.spec.ts
1+
// pnpm exec vitest run api/transform/__tests__/openai-format.spec.ts
22

33
import { Anthropic } from "@anthropic-ai/sdk"
44
import OpenAI from "openai"
@@ -143,7 +143,33 @@ describe("convertToOpenAiMessages", () => {
143143
expect((openAiMessages[0] as any).content).toBe("some text\n")
144144
})
145145

146-
it("should handle base64 image in tool result with placeholder and skip URL images", () => {
146+
it("should handle base64 image in tool result with placeholder", () => {
147+
const anthropicMessages: Anthropic.Messages.MessageParam[] = [
148+
{
149+
role: "user",
150+
content: [
151+
{
152+
type: "tool_result",
153+
tool_use_id: "tool-1",
154+
content: [
155+
{
156+
type: "image",
157+
source: { type: "base64", media_type: "image/png", data: "base64data" },
158+
},
159+
],
160+
},
161+
],
162+
},
163+
]
164+
165+
const openAiMessages = convertToOpenAiMessages(anthropicMessages)
166+
expect(openAiMessages[0].role).toBe("tool")
167+
// base64 images in tool results emit a placeholder; the image itself is
168+
// flushed in a separate user message (see comment block in openai-format.ts)
169+
expect((openAiMessages[0] as any).content).toBe("(see following user message for image)")
170+
})
171+
172+
it("should render [Image] placeholder for URL image in tool result", () => {
147173
const anthropicMessages: Anthropic.Messages.MessageParam[] = [
148174
{
149175
role: "user",

‎src/api/transform/__tests__/r1-format.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ describe("convertToR1Format", () => {
6969
expect(convertToR1Format(input)).toEqual(expected)
7070
})
7171

72+
it("should skip non-base64 images (e.g. URL-sourced)", () => {
73+
const input: Anthropic.Messages.MessageParam[] = [
74+
{
75+
role: "user",
76+
content: [
77+
{ type: "text", text: "Look at this:" },
78+
{
79+
type: "image",
80+
source: { type: "url", url: "https://example.com/img.png" } as any,
81+
},
82+
],
83+
},
84+
]
85+
86+
const result = convertToR1Format(input)
87+
// URL image is skipped; only the text part survives
88+
expect(result).toHaveLength(1)
89+
expect(result[0]).toEqual({ role: "user", content: "Look at this:" })
90+
})
91+
7292
it("should handle mixed text and image content", () => {
7393
const input: Anthropic.Messages.MessageParam[] = [
7494
{

‎src/api/transform/__tests__/responses-api-input.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ describe("convertToResponsesApiInput", () => {
7373
])
7474
})
7575

76+
it("should skip non-base64 images (e.g. URL-sourced)", () => {
77+
const messages: Anthropic.Messages.MessageParam[] = [
78+
{
79+
role: "user",
80+
content: [
81+
{ type: "text", text: "Look at this:" },
82+
{
83+
type: "image",
84+
source: { type: "url", url: "https://example.com/img.png" } as any,
85+
},
86+
],
87+
},
88+
]
89+
90+
const result = convertToResponsesApiInput(messages)
91+
// URL image is not emitted; only the text part survives
92+
expect(result).toEqual([{ role: "user", content: [{ type: "input_text", text: "Look at this:" }] }])
93+
})
94+
7695
it("should convert tool_result to function_call_output", () => {
7796
const messages: Anthropic.Messages.MessageParam[] = [
7897
{

‎src/api/transform/__tests__/vscode-lm-format.spec.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// npx vitest run src/api/transform/__tests__/vscode-lm-format.spec.ts
1+
// pnpm exec vitest run api/transform/__tests__/vscode-lm-format.spec.ts
22

33
import { Anthropic } from "@anthropic-ai/sdk"
44
import * as vscode from "vscode"
@@ -220,6 +220,49 @@ describe("convertToVsCodeLmMessages", () => {
220220
const toolResult = result[0].content[0] as any
221221
expect(toolResult.content[0].value).toContain("[Image (url): not supported by VSCode LM API]")
222222
})
223+
224+
it("should produce base64 image placeholder inside tool result", () => {
225+
const messages: Anthropic.Messages.MessageParam[] = [
226+
{
227+
role: "user",
228+
content: [
229+
{
230+
type: "tool_result",
231+
tool_use_id: "tool-1",
232+
content: [
233+
{
234+
type: "image",
235+
source: { type: "base64", media_type: "image/jpeg", data: "abc" },
236+
},
237+
],
238+
},
239+
],
240+
},
241+
]
242+
243+
const result = convertToVsCodeLmMessages(messages)
244+
const toolResult = result[0].content[0] as any
245+
expect(toolResult.content[0].value).toBe("[Image (base64): image/jpeg not supported by VSCode LM API]")
246+
})
247+
248+
it("should return empty string for unknown block types inside tool result", () => {
249+
const messages: Anthropic.Messages.MessageParam[] = [
250+
{
251+
role: "user",
252+
content: [
253+
{
254+
type: "tool_result",
255+
tool_use_id: "tool-1",
256+
content: [{ type: "document" } as any],
257+
},
258+
],
259+
},
260+
]
261+
262+
const result = convertToVsCodeLmMessages(messages)
263+
const toolResult = result[0].content[0] as any
264+
expect(toolResult.content[0].value).toBe("")
265+
})
223266
})
224267

225268
describe("convertToAnthropicRole", () => {

‎src/api/transform/__tests__/zai-format.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// npx vitest run api/transform/__tests__/zai-format.spec.ts
1+
// pnpm exec vitest run api/transform/__tests__/zai-format.spec.ts
22

33
import { Anthropic } from "@anthropic-ai/sdk"
44

‎src/integrations/misc/__tests__/export-markdown.spec.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ describe("export-markdown", () => {
7676
it("should format document blocks", () => {
7777
const block = {
7878
type: "document",
79-
source: { type: "base64", media_type: "application/pdf", data: "abc" },
80-
} as any as ExtendedContentBlock
79+
source: { type: "base64", media_type: "application/pdf", data: "abc" } as const,
80+
} satisfies ExtendedContentBlock
8181
expect(formatContentBlockToMarkdown(block)).toBe("[Document]")
8282
})
8383

@@ -86,17 +86,16 @@ describe("export-markdown", () => {
8686
type: "search_result",
8787
source: "https://example.com",
8888
title: "Example",
89-
content: [],
90-
} as any as ExtendedContentBlock
89+
content: [{ type: "text", text: "result text" }],
90+
} satisfies ExtendedContentBlock
9191
expect(formatContentBlockToMarkdown(block)).toBe("[Search Result]")
9292
})
9393

9494
it("should format tool_reference blocks", () => {
9595
const block = {
9696
type: "tool_reference",
97-
id: "tool-1",
98-
name: "read_file",
99-
} as any as ExtendedContentBlock
97+
tool_name: "read_file",
98+
} satisfies ExtendedContentBlock
10099
expect(formatContentBlockToMarkdown(block)).toBe("[Tool Reference]")
101100
})
102101
})

0 commit comments

Comments
 (0)