1+ // npx jest src/api/providers/__tests__/anthropic.test.ts
2+
13import { AnthropicHandler } from "../anthropic"
24import { 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 ( )
86const mockCreate = jest . fn ( )
7+
98jest . 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
0 commit comments