Skip to content

Commit 543b68f

Browse files
committed
Fix tests
1 parent 22391e5 commit 543b68f

File tree

4 files changed

+33
-60
lines changed

4 files changed

+33
-60
lines changed

src/api/providers/__tests__/anthropic.test.ts

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,13 @@
1+
// npx jest src/api/providers/__tests__/anthropic.test.ts
2+
13
import { AnthropicHandler } from "../anthropic"
24
import { ApiHandlerOptions } from "../../../shared/api"
3-
import { ApiStream } from "../../transform/stream"
4-
import { Anthropic } from "@anthropic-ai/sdk"
55

6-
// Mock Anthropic client
7-
const mockBetaCreate = jest.fn()
86
const mockCreate = jest.fn()
7+
98
jest.mock("@anthropic-ai/sdk", () => {
109
return {
1110
Anthropic: jest.fn().mockImplementation(() => ({
12-
beta: {
13-
promptCaching: {
14-
messages: {
15-
create: mockBetaCreate.mockImplementation(async () => ({
16-
async *[Symbol.asyncIterator]() {
17-
yield {
18-
type: "message_start",
19-
message: {
20-
usage: {
21-
input_tokens: 100,
22-
output_tokens: 50,
23-
cache_creation_input_tokens: 20,
24-
cache_read_input_tokens: 10,
25-
},
26-
},
27-
}
28-
yield {
29-
type: "content_block_start",
30-
index: 0,
31-
content_block: {
32-
type: "text",
33-
text: "Hello",
34-
},
35-
}
36-
yield {
37-
type: "content_block_delta",
38-
delta: {
39-
type: "text_delta",
40-
text: " world",
41-
},
42-
}
43-
},
44-
})),
45-
},
46-
},
47-
},
4811
messages: {
4912
create: mockCreate.mockImplementation(async (options) => {
5013
if (!options.stream) {
@@ -65,16 +28,26 @@ jest.mock("@anthropic-ai/sdk", () => {
6528
type: "message_start",
6629
message: {
6730
usage: {
68-
input_tokens: 10,
69-
output_tokens: 5,
31+
input_tokens: 100,
32+
output_tokens: 50,
33+
cache_creation_input_tokens: 20,
34+
cache_read_input_tokens: 10,
7035
},
7136
},
7237
}
7338
yield {
7439
type: "content_block_start",
40+
index: 0,
7541
content_block: {
7642
type: "text",
77-
text: "Test response",
43+
text: "Hello",
44+
},
45+
}
46+
yield {
47+
type: "content_block_delta",
48+
delta: {
49+
type: "text_delta",
50+
text: " world",
7851
},
7952
}
8053
},
@@ -95,7 +68,6 @@ describe("AnthropicHandler", () => {
9568
apiModelId: "claude-3-5-sonnet-20241022",
9669
}
9770
handler = new AnthropicHandler(mockOptions)
98-
mockBetaCreate.mockClear()
9971
mockCreate.mockClear()
10072
})
10173

@@ -126,17 +98,6 @@ describe("AnthropicHandler", () => {
12698

12799
describe("createMessage", () => {
128100
const systemPrompt = "You are a helpful assistant."
129-
const messages: Anthropic.Messages.MessageParam[] = [
130-
{
131-
role: "user",
132-
content: [
133-
{
134-
type: "text" as const,
135-
text: "Hello!",
136-
},
137-
],
138-
},
139-
]
140101

141102
it("should handle prompt caching for supported models", async () => {
142103
const stream = handler.createMessage(systemPrompt, [
@@ -173,9 +134,8 @@ describe("AnthropicHandler", () => {
173134
expect(textChunks[0].text).toBe("Hello")
174135
expect(textChunks[1].text).toBe(" world")
175136

176-
// Verify beta API was used
177-
expect(mockBetaCreate).toHaveBeenCalled()
178-
expect(mockCreate).not.toHaveBeenCalled()
137+
// Verify API
138+
expect(mockCreate).toHaveBeenCalled()
179139
})
180140
})
181141

src/api/transform/__tests__/bedrock-converse-format.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// npx jest src/api/transform/__tests__/bedrock-converse-format.test.ts
2+
13
import { convertToBedrockConverseMessages, convertToAnthropicMessage } from "../bedrock-converse-format"
24
import { Anthropic } from "@anthropic-ai/sdk"
35
import { ContentBlock, ToolResultContentBlock } from "@aws-sdk/client-bedrock-runtime"
@@ -187,6 +189,8 @@ describe("bedrock-converse-format", () => {
187189
usage: {
188190
input_tokens: 10,
189191
output_tokens: 20,
192+
cache_creation_input_tokens: null,
193+
cache_read_input_tokens: null,
190194
},
191195
})
192196
})
@@ -205,7 +209,7 @@ describe("bedrock-converse-format", () => {
205209
expect(result).toEqual({
206210
type: "message",
207211
role: "assistant",
208-
content: [{ type: "text", text: "Hello" }],
212+
content: [{ type: "text", text: "Hello", citations: null }],
209213
model: "test-model",
210214
})
211215
})
@@ -224,7 +228,7 @@ describe("bedrock-converse-format", () => {
224228
expect(result).toEqual({
225229
type: "message",
226230
role: "assistant",
227-
content: [{ type: "text", text: " world" }],
231+
content: [{ type: "text", text: " world", citations: null }],
228232
model: "test-model",
229233
})
230234
})

src/api/transform/__tests__/openai-format.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// npx jest src/api/transform/__tests__/openai-format.test.ts
2+
13
import { convertToOpenAiMessages, convertToAnthropicMessage } from "../openai-format"
24
import { Anthropic } from "@anthropic-ai/sdk"
35
import OpenAI from "openai"
@@ -172,11 +174,14 @@ describe("OpenAI Format Transformations", () => {
172174
expect(anthropicMessage.content[0]).toEqual({
173175
type: "text",
174176
text: "Hello there!",
177+
citations: null,
175178
})
176179
expect(anthropicMessage.stop_reason).toBe("end_turn")
177180
expect(anthropicMessage.usage).toEqual({
178181
input_tokens: 10,
179182
output_tokens: 5,
183+
cache_creation_input_tokens: null,
184+
cache_read_input_tokens: null,
180185
})
181186
})
182187

@@ -221,6 +226,7 @@ describe("OpenAI Format Transformations", () => {
221226
expect(anthropicMessage.content[0]).toEqual({
222227
type: "text",
223228
text: "Let me check the weather.",
229+
citations: null,
224230
})
225231
expect(anthropicMessage.content[1]).toEqual({
226232
type: "tool_use",

src/api/transform/__tests__/vscode-lm-format.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// npx jest src/api/transform/__tests__/vscode-lm-format.test.ts
2+
13
import { Anthropic } from "@anthropic-ai/sdk"
24
import * as vscode from "vscode"
35
import { convertToVsCodeLmMessages, convertToAnthropicRole, convertToAnthropicMessage } from "../vscode-lm-format"
@@ -216,6 +218,7 @@ describe("vscode-lm-format", () => {
216218
expect(result.content[0]).toEqual({
217219
type: "text",
218220
text: "Hello",
221+
citations: null,
219222
})
220223
expect(result.id).toBe("test-uuid")
221224
})

0 commit comments

Comments
 (0)