22
33import { Anthropic } from "@anthropic-ai/sdk"
44import { AnthropicVertex } from "@anthropic-ai/vertex-sdk"
5+ import { GoogleAuth } from "google-auth-library"
56
67import { VERTEX_1M_CONTEXT_MODEL_IDS } from "@roo-code/types"
78
89import { ApiStreamChunk } from "../../transform/stream"
910
1011import { AnthropicVertexHandler } from "../anthropic-vertex"
1112
13+ vitest . mock ( "../utils/timeout-config" , ( ) => ( {
14+ getApiRequestTimeout : vitest . fn ( ) . mockReturnValue ( 300_000 ) ,
15+ } ) )
16+
17+ const MOCK_TIMEOUT_MS = 300_000
18+
19+ vitest . mock ( "google-auth-library" , ( ) => ( {
20+ GoogleAuth : vitest . fn ( ) . mockImplementation ( function ( opts ) {
21+ return { __googleAuthOptions : opts }
22+ } ) ,
23+ } ) )
24+
1225vitest . mock ( "@anthropic-ai/vertex-sdk" , ( ) => ( {
1326 AnthropicVertex : vitest . fn ( ) . mockImplementation ( function ( ) {
1427 return {
@@ -56,6 +69,11 @@ describe("VertexHandler", () => {
5669 let handler : AnthropicVertexHandler
5770
5871 describe ( "constructor" , ( ) => {
72+ beforeEach ( ( ) => {
73+ ; ( AnthropicVertex as any ) . mockClear ( )
74+ ; ( GoogleAuth as any ) . mockClear ( )
75+ } )
76+
5977 it ( "should initialize with provided config for Claude" , ( ) => {
6078 handler = new AnthropicVertexHandler ( {
6179 apiModelId : "claude-3-5-sonnet-v2@20241022" ,
@@ -66,7 +84,58 @@ describe("VertexHandler", () => {
6684 expect ( AnthropicVertex ) . toHaveBeenCalledWith ( {
6785 projectId : "test-project" ,
6886 region : "us-central1" ,
87+ timeout : MOCK_TIMEOUT_MS ,
88+ } )
89+ } )
90+
91+ it ( "should pass timeout when initializing with vertexJsonCredentials" , ( ) => {
92+ const credentials = {
93+ type : "service_account" ,
94+ client_email : "test@test-project.iam.gserviceaccount.com" ,
95+ private_key : "-----BEGIN PRIVATE KEY-----\nfake\n-----END PRIVATE KEY-----\n" ,
96+ }
97+
98+ handler = new AnthropicVertexHandler ( {
99+ apiModelId : "claude-3-5-sonnet-v2@20241022" ,
100+ vertexProjectId : "test-project" ,
101+ vertexRegion : "us-central1" ,
102+ vertexJsonCredentials : JSON . stringify ( credentials ) ,
69103 } )
104+
105+ expect ( GoogleAuth ) . toHaveBeenCalledWith ( {
106+ scopes : [ "https://www.googleapis.com/auth/cloud-platform" ] ,
107+ credentials,
108+ } )
109+ expect ( AnthropicVertex ) . toHaveBeenCalledWith (
110+ expect . objectContaining ( {
111+ projectId : "test-project" ,
112+ region : "us-central1" ,
113+ googleAuth : expect . any ( Object ) ,
114+ timeout : MOCK_TIMEOUT_MS ,
115+ } ) ,
116+ )
117+ } )
118+
119+ it ( "should pass timeout when initializing with vertexKeyFile" , ( ) => {
120+ handler = new AnthropicVertexHandler ( {
121+ apiModelId : "claude-3-5-sonnet-v2@20241022" ,
122+ vertexProjectId : "test-project" ,
123+ vertexRegion : "us-central1" ,
124+ vertexKeyFile : "/tmp/sa-key.json" ,
125+ } )
126+
127+ expect ( GoogleAuth ) . toHaveBeenCalledWith ( {
128+ scopes : [ "https://www.googleapis.com/auth/cloud-platform" ] ,
129+ keyFile : "/tmp/sa-key.json" ,
130+ } )
131+ expect ( AnthropicVertex ) . toHaveBeenCalledWith (
132+ expect . objectContaining ( {
133+ projectId : "test-project" ,
134+ region : "us-central1" ,
135+ googleAuth : expect . any ( Object ) ,
136+ timeout : MOCK_TIMEOUT_MS ,
137+ } ) ,
138+ )
70139 } )
71140 } )
72141
0 commit comments