Skip to content

Commit 408143e

Browse files
author
Bertan Ari
committed
test(vscode-lm): split image forwarding cases and fix context-window fixture
1 parent ec0a35d commit 408143e

1 file changed

Lines changed: 66 additions & 64 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -168,66 +168,67 @@ describe("VsCodeLmHandler", () => {
168168
},
169169
)
170170

171-
it.each(["message", "tool_result"] as const)(
172-
"forwards image bytes in the %s path only when supported",
173-
async (path) => {
171+
it.each(
172+
(["message", "tool_result"] as const).flatMap((path) =>
173+
[true, false, undefined].map((supportsImages) => ({ path, supportsImages })),
174+
),
175+
)(
176+
"forwards image bytes in the $path path only when supported ($supportsImages)",
177+
async ({ path, supportsImages }) => {
174178
await handler.initializeClient()
175-
for (const supportsImages of [true, false, undefined]) {
176-
const sendRequest = vi.fn<vscode.LanguageModelChat["sendRequest"]>().mockResolvedValue({
177-
stream: asyncStreamFrom([new vscode.LanguageModelTextPart("done")]),
178-
text: asyncStreamFrom(["done"]),
179+
const sendRequest = vi.fn<vscode.LanguageModelChat["sendRequest"]>().mockResolvedValue({
180+
stream: asyncStreamFrom([new vscode.LanguageModelTextPart("done")]),
181+
text: asyncStreamFrom(["done"]),
182+
})
183+
const countTokens = vi.fn<vscode.LanguageModelChat["countTokens"]>().mockResolvedValue(37)
184+
handler["client"] = {
185+
...mockLanguageModelChat,
186+
...{ capabilities: { supportsImageToText: supportsImages } },
187+
sendRequest,
188+
countTokens,
189+
}
190+
const messages: Anthropic.Messages.MessageParam[] =
191+
path === "message"
192+
? [{ role: "user", content: [{ type: "text", text: "look" }, image] }]
193+
: [
194+
{
195+
role: "assistant",
196+
content: [{ type: "tool_use", id: "image-tool", name: "read_file", input: {} }],
197+
},
198+
{
199+
role: "user",
200+
content: [
201+
{
202+
type: "tool_result",
203+
tool_use_id: "image-tool",
204+
content: [{ type: "text", text: "look" }, image],
205+
},
206+
],
207+
},
208+
]
209+
const chunks = await collectStream(handler.createMessage("system", messages))
210+
const sent = sendRequest.mock.calls[0][0]
211+
expect(chunks).toContainEqual({ type: "usage", inputTokens: sent.length * 37, outputTokens: 37 })
212+
const counted = countTokens.mock.calls.map(([content]) => content)
213+
if (supportsImages) {
214+
expect(counted).toEqual(expect.arrayContaining(sent))
215+
} else {
216+
expect(counted.every((content) => typeof content === "string")).toBe(true)
217+
}
218+
const content = sent[sent.length - 1].content
219+
const parts =
220+
path === "tool_result" && content[0] instanceof vscode.LanguageModelToolResultPart
221+
? content[0].content
222+
: content
223+
expect(parts[0]).toMatchObject({ value: "look" })
224+
if (supportsImages) {
225+
expect(parts[1]).toMatchObject({
226+
mimeType: "image/png",
227+
data: Buffer.from("iVBORw==", "base64"),
179228
})
180-
const countTokens = vi.fn<vscode.LanguageModelChat["countTokens"]>().mockResolvedValue(37)
181-
handler["client"] = {
182-
...mockLanguageModelChat,
183-
...{ capabilities: { supportsImageToText: supportsImages } },
184-
sendRequest,
185-
countTokens,
186-
}
187-
const messages: Anthropic.Messages.MessageParam[] =
188-
path === "message"
189-
? [{ role: "user", content: [{ type: "text", text: "look" }, image] }]
190-
: [
191-
{
192-
role: "assistant",
193-
content: [{ type: "tool_use", id: "image-tool", name: "read_file", input: {} }],
194-
},
195-
{
196-
role: "user",
197-
content: [
198-
{
199-
type: "tool_result",
200-
tool_use_id: "image-tool",
201-
content: [{ type: "text", text: "look" }, image],
202-
},
203-
],
204-
},
205-
]
206-
const chunks = await collectStream(handler.createMessage("system", messages))
207-
const sent = sendRequest.mock.calls[0][0]
208-
expect(chunks).toContainEqual({ type: "usage", inputTokens: sent.length * 37, outputTokens: 37 })
209-
if (supportsImages) {
210-
for (const message of sent) {
211-
expect(countTokens).toHaveBeenCalledWith(message, expect.anything())
212-
}
213-
} else {
214-
expect(countTokens.mock.calls.every(([content]) => typeof content === "string")).toBe(true)
215-
}
216-
const content = sent[sent.length - 1].content
217-
const parts =
218-
path === "tool_result" && content[0] instanceof vscode.LanguageModelToolResultPart
219-
? content[0].content
220-
: content
221-
expect(parts[0]).toMatchObject({ value: "look" })
222-
if (supportsImages) {
223-
expect(parts[1]).toMatchObject({
224-
mimeType: "image/png",
225-
data: Buffer.from("iVBORw==", "base64"),
226-
})
227-
} else {
228-
expect(parts[1]).toBeInstanceOf(vscode.LanguageModelTextPart)
229-
expect(parts[1]).toMatchObject({ value: expect.stringContaining("not supported") })
230-
}
229+
} else {
230+
expect(parts[1]).toBeInstanceOf(vscode.LanguageModelTextPart)
231+
expect(parts[1]).toMatchObject({ value: expect.stringContaining("not supported") })
231232
}
232233
},
233234
)
@@ -278,12 +279,13 @@ describe("VsCodeLmHandler", () => {
278279
...{ capabilities: fixture.capabilities },
279280
maxInputTokens: fixture.liveWindow,
280281
sendRequest,
281-
// Text-only models count extracted strings; every one exceeds the window on its own.
282-
countTokens: vi
283-
.fn<vscode.LanguageModelChat["countTokens"]>()
284-
.mockImplementation(async (content) =>
285-
typeof content === "string" || content.role === userRole ? fixture.inputTokens : 0,
286-
),
282+
// Image models count whole messages; text-only models count extracted strings.
283+
countTokens: vi.fn<vscode.LanguageModelChat["countTokens"]>().mockImplementation(async (content) => {
284+
if (typeof content === "string") {
285+
return fixture.capabilities === imageCapable ? 0 : fixture.inputTokens
286+
}
287+
return content.role === userRole ? fixture.inputTokens : 0
288+
}),
287289
}
288290
const windowSpy = vi.spyOn(handler, "getCondenseContextWindow").mockReturnValue(fixture.curatedWindow)
289291
try {

0 commit comments

Comments
 (0)