@@ -315,45 +315,35 @@ describe("Bedrock", () => {
315315 } ) ;
316316
317317 describe ( "streaming vs non-streaming behavior" , ( ) => {
318- class TestableStreamingBedrock extends Bedrock {
319- public streamingMethodCalled = false ;
320- public nonStreamingMethodCalled = false ;
321-
322- protected async * _streamChatStreaming (
323- messages : ChatMessage [ ] ,
324- signal : AbortSignal ,
325- options : CompletionOptions ,
326- ) : AsyncGenerator < ChatMessage > {
327- this . streamingMethodCalled = true ;
328- yield { role : "assistant" , content : "Streaming response" } ;
329- }
330-
331- protected async * _streamChatNonStreaming (
332- messages : ChatMessage [ ] ,
333- signal : AbortSignal ,
334- options : CompletionOptions ,
335- ) : AsyncGenerator < ChatMessage > {
336- this . nonStreamingMethodCalled = true ;
337- yield { role : "assistant" , content : "Non-streaming response" } ;
338- }
339- }
340-
341- let bedrock : TestableStreamingBedrock ;
318+ let bedrock : Bedrock ;
342319 const mockAbortSignal = new AbortController ( ) . signal ;
343320
344321 beforeEach ( ( ) => {
345- bedrock = new TestableStreamingBedrock ( {
322+ bedrock = new Bedrock ( {
346323 apiKey : "test-key" ,
347324 model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
348325 region : "us-east-1" ,
349326 } ) ;
350327 } ) ;
351328
352- it ( "should use streaming method when stream is not false" , async ( ) => {
329+ it ( "should route to streaming path when stream is not false" , async ( ) => {
353330 const messages : ChatMessage [ ] = [
354331 { role : "user" , content : "Hello" } as UserChatMessage ,
355332 ] ;
356333
334+ // Mock the private methods by spying on them
335+ const streamingMethodSpy = jest
336+ . spyOn ( bedrock as any , "_streamChatStreaming" )
337+ . mockImplementation ( async function * ( ) {
338+ yield { role : "assistant" , content : "Streaming response" } ;
339+ } ) ;
340+
341+ const nonStreamingMethodSpy = jest
342+ . spyOn ( bedrock as any , "_streamChatNonStreaming" )
343+ . mockImplementation ( async function * ( ) {
344+ yield { role : "assistant" , content : "Non-streaming response" } ;
345+ } ) ;
346+
357347 const options : CompletionOptions = {
358348 model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
359349 stream : true , // Explicitly set streaming
@@ -368,18 +358,34 @@ describe("Bedrock", () => {
368358 results . push ( message ) ;
369359 }
370360
371- expect ( bedrock . streamingMethodCalled ) . toBe ( true ) ;
372- expect ( bedrock . nonStreamingMethodCalled ) . toBe ( false ) ;
361+ expect ( streamingMethodSpy ) . toHaveBeenCalled ( ) ;
362+ expect ( nonStreamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
373363 expect ( results ) . toEqual ( [
374364 { role : "assistant" , content : "Streaming response" } ,
375365 ] ) ;
366+
367+ streamingMethodSpy . mockRestore ( ) ;
368+ nonStreamingMethodSpy . mockRestore ( ) ;
376369 } ) ;
377370
378- it ( "should use non-streaming method when stream is false" , async ( ) => {
371+ it ( "should route to non-streaming path when stream is false" , async ( ) => {
379372 const messages : ChatMessage [ ] = [
380373 { role : "user" , content : "Hello" } as UserChatMessage ,
381374 ] ;
382375
376+ // Mock the private methods by spying on them
377+ const streamingMethodSpy = jest
378+ . spyOn ( bedrock as any , "_streamChatStreaming" )
379+ . mockImplementation ( async function * ( ) {
380+ yield { role : "assistant" , content : "Streaming response" } ;
381+ } ) ;
382+
383+ const nonStreamingMethodSpy = jest
384+ . spyOn ( bedrock as any , "_streamChatNonStreaming" )
385+ . mockImplementation ( async function * ( ) {
386+ yield { role : "assistant" , content : "Non-streaming response" } ;
387+ } ) ;
388+
383389 const options : CompletionOptions = {
384390 model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
385391 stream : false , // Explicitly set non-streaming
@@ -394,18 +400,34 @@ describe("Bedrock", () => {
394400 results . push ( message ) ;
395401 }
396402
397- expect ( bedrock . streamingMethodCalled ) . toBe ( false ) ;
398- expect ( bedrock . nonStreamingMethodCalled ) . toBe ( true ) ;
403+ expect ( streamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
404+ expect ( nonStreamingMethodSpy ) . toHaveBeenCalled ( ) ;
399405 expect ( results ) . toEqual ( [
400406 { role : "assistant" , content : "Non-streaming response" } ,
401407 ] ) ;
408+
409+ streamingMethodSpy . mockRestore ( ) ;
410+ nonStreamingMethodSpy . mockRestore ( ) ;
402411 } ) ;
403412
404- it ( "should default to streaming method when stream option is undefined" , async ( ) => {
413+ it ( "should default to streaming path when stream option is undefined" , async ( ) => {
405414 const messages : ChatMessage [ ] = [
406415 { role : "user" , content : "Hello" } as UserChatMessage ,
407416 ] ;
408417
418+ // Mock the private methods by spying on them
419+ const streamingMethodSpy = jest
420+ . spyOn ( bedrock as any , "_streamChatStreaming" )
421+ . mockImplementation ( async function * ( ) {
422+ yield { role : "assistant" , content : "Streaming response" } ;
423+ } ) ;
424+
425+ const nonStreamingMethodSpy = jest
426+ . spyOn ( bedrock as any , "_streamChatNonStreaming" )
427+ . mockImplementation ( async function * ( ) {
428+ yield { role : "assistant" , content : "Non-streaming response" } ;
429+ } ) ;
430+
409431 const options : CompletionOptions = {
410432 model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
411433 // stream option is undefined, should default to streaming
@@ -420,13 +442,14 @@ describe("Bedrock", () => {
420442 results . push ( message ) ;
421443 }
422444
423- expect ( bedrock . streamingMethodCalled ) . toBe ( true ) ;
424- expect ( bedrock . nonStreamingMethodCalled ) . toBe ( false ) ;
445+ expect ( streamingMethodSpy ) . toHaveBeenCalled ( ) ;
446+ expect ( nonStreamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
447+
448+ streamingMethodSpy . mockRestore ( ) ;
449+ nonStreamingMethodSpy . mockRestore ( ) ;
425450 } ) ;
426451 } ) ;
427452
428-
429-
430453 describe ( "message conversion" , ( ) => {
431454 let bedrock : TestBedrock ;
432455
0 commit comments