11import type { MockedFunction } from "vitest"
22import { BedrockRuntimeClient , InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime"
33
4+ import { NodeHttpHandler } from "@smithy/node-http-handler"
5+ import { HttpProxyAgent } from "http-proxy-agent"
6+ import { HttpsProxyAgent } from "https-proxy-agent"
7+
48import { BedrockEmbedder } from "../bedrock"
59import { MAX_ITEM_TOKENS , INITIAL_RETRY_DELAY_MS } from "../../constants"
10+ import { getSystemProxyUrl } from "../../../../utils/networkProxy"
611
712import { clearAllMocks } from "../../../../test-utils/reset"
813
@@ -26,6 +31,17 @@ vitest.mock("@aws-sdk/credential-providers", () => ({
2631 fromIni : vitest . fn ( ) . mockReturnValue ( Promise . resolve ( { } ) ) ,
2732} ) )
2833
34+ vitest . mock ( "../../../../utils/networkProxy" , ( ) => ( {
35+ getSystemProxyUrl : vitest . fn ( ) . mockReturnValue ( undefined ) ,
36+ } ) )
37+ vitest . mock ( "@smithy/node-http-handler" , ( ) => ( {
38+ NodeHttpHandler : vitest . fn ( ) . mockImplementation ( function ( options : unknown ) {
39+ return { options }
40+ } ) ,
41+ } ) )
42+ vitest . mock ( "http-proxy-agent" , ( ) => ( { HttpProxyAgent : vitest . fn ( ) } ) )
43+ vitest . mock ( "https-proxy-agent" , ( ) => ( { HttpsProxyAgent : vitest . fn ( ) } ) )
44+
2945// Mock TelemetryService
3046vitest . mock ( "@roo-code/telemetry" , ( ) => ( {
3147 TelemetryService : {
@@ -114,6 +130,42 @@ describe("BedrockEmbedder", () => {
114130 } ) ,
115131 )
116132 } )
133+
134+ it ( "should tunnel through the system proxy when one is configured" , ( ) => {
135+ // The embedder built in beforeEach already consumed these mocks.
136+ vitest . mocked ( getSystemProxyUrl ) . mockReturnValue ( "http://proxy.corp.local:3128" )
137+ vitest . mocked ( NodeHttpHandler ) . mockClear ( )
138+
139+ new BedrockEmbedder ( "us-east-1" , "test-profile" , "amazon.titan-embed-text-v2:0" )
140+
141+ expect ( HttpProxyAgent ) . toHaveBeenCalledWith ( "http://proxy.corp.local:3128" )
142+ expect ( HttpsProxyAgent ) . toHaveBeenCalledWith ( "http://proxy.corp.local:3128" )
143+
144+ // Both agents must reach the handler: a client that tunnels only https still
145+ // resolves http DNS locally, which is the ENOTFOUND this fix is about.
146+ expect ( NodeHttpHandler ) . toHaveBeenCalledOnce ( )
147+ // The constructor accepts options or a provider function; only the object form is used here.
148+ const handlerArg = vitest . mocked ( NodeHttpHandler ) . mock . calls [ 0 ] [ 0 ]
149+ const handlerOptions = typeof handlerArg === "function" ? undefined : handlerArg
150+ expect ( handlerOptions ?. httpAgent ) . toBe ( vitest . mocked ( HttpProxyAgent ) . mock . instances [ 0 ] )
151+ expect ( handlerOptions ?. httpsAgent ) . toBe ( vitest . mocked ( HttpsProxyAgent ) . mock . instances [ 0 ] )
152+
153+ // ...and the handler must reach the client.
154+ const clientConfig = vitest . mocked ( BedrockRuntimeClient ) . mock . calls . at ( - 1 ) ?. [ 0 ]
155+ expect ( clientConfig ?. requestHandler ) . toBe ( vitest . mocked ( NodeHttpHandler ) . mock . instances [ 0 ] )
156+ } )
157+
158+ it ( "should not install a request handler when no proxy is configured" , ( ) => {
159+ vitest . mocked ( getSystemProxyUrl ) . mockReturnValue ( undefined )
160+ vitest . mocked ( NodeHttpHandler ) . mockClear ( )
161+
162+ new BedrockEmbedder ( "us-east-1" , "test-profile" , "amazon.titan-embed-text-v2:0" )
163+
164+ expect ( NodeHttpHandler ) . not . toHaveBeenCalled ( )
165+ expect ( BedrockRuntimeClient ) . toHaveBeenLastCalledWith (
166+ expect . not . objectContaining ( { requestHandler : expect . anything ( ) } ) ,
167+ )
168+ } )
117169 } )
118170
119171 describe ( "createEmbeddings" , ( ) => {
0 commit comments