22
33import { AnthropicHandler } from "../anthropic"
44import { ApiHandlerOptions } from "../../../shared/api"
5+ import Anthropic from "@anthropic-ai/sdk"
56
67const mockCreate = jest . fn ( )
8+ const mockAnthropicConstructor = Anthropic . Anthropic as unknown as jest . Mock
79
810jest . mock ( "@anthropic-ai/sdk" , ( ) => {
911 return {
@@ -69,6 +71,7 @@ describe("AnthropicHandler", () => {
6971 }
7072 handler = new AnthropicHandler ( mockOptions )
7173 mockCreate . mockClear ( )
74+ mockAnthropicConstructor . mockClear ( )
7275 } )
7376
7477 describe ( "constructor" , ( ) => {
@@ -94,6 +97,40 @@ describe("AnthropicHandler", () => {
9497 } )
9598 expect ( handlerWithCustomUrl ) . toBeInstanceOf ( AnthropicHandler )
9699 } )
100+
101+ it ( "use apiKey for passing token if anthropicUseAuthToken is not set" , ( ) => {
102+ const handlerWithCustomUrl = new AnthropicHandler ( {
103+ ...mockOptions ,
104+ } )
105+ expect ( handlerWithCustomUrl ) . toBeInstanceOf ( AnthropicHandler )
106+ expect ( mockAnthropicConstructor ) . toHaveBeenCalledTimes ( 1 )
107+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . apiKey ) . toEqual ( "test-api-key" )
108+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . authToken ) . toBeUndefined ( )
109+ } )
110+
111+ it ( "use apiKey for passing token if anthropicUseAuthToken is set but custom base URL is not given" , ( ) => {
112+ const handlerWithCustomUrl = new AnthropicHandler ( {
113+ ...mockOptions ,
114+ anthropicUseAuthToken : true ,
115+ } )
116+ expect ( handlerWithCustomUrl ) . toBeInstanceOf ( AnthropicHandler )
117+ expect ( mockAnthropicConstructor ) . toHaveBeenCalledTimes ( 1 )
118+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . apiKey ) . toEqual ( "test-api-key" )
119+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . authToken ) . toBeUndefined ( )
120+ } )
121+
122+ it ( "use authToken for passing token if both of anthropicBaseUrl and anthropicUseAuthToken are set" , ( ) => {
123+ const customBaseUrl = "https://custom.anthropic.com"
124+ const handlerWithCustomUrl = new AnthropicHandler ( {
125+ ...mockOptions ,
126+ anthropicBaseUrl : customBaseUrl ,
127+ anthropicUseAuthToken : true ,
128+ } )
129+ expect ( handlerWithCustomUrl ) . toBeInstanceOf ( AnthropicHandler )
130+ expect ( mockAnthropicConstructor ) . toHaveBeenCalledTimes ( 1 )
131+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . authToken ) . toEqual ( "test-api-key" )
132+ expect ( mockAnthropicConstructor . mock . lastCall [ 0 ] . apiKey ) . toBeUndefined ( )
133+ } )
97134 } )
98135
99136 describe ( "createMessage" , ( ) => {
0 commit comments