@@ -7,21 +7,13 @@ import {
77import Bedrock from "./Bedrock.js" ;
88
99// Mock AWS SDK
10- const mockSend = jest . fn ( ) ;
11- const mockMiddlewareStackAdd = jest . fn ( ) ;
12-
1310jest . mock ( "@aws-sdk/client-bedrock-runtime" , ( ) => ( {
1411 BedrockRuntimeClient : jest . fn ( ) . mockImplementation ( ( ) => ( {
15- send : mockSend ,
16- middlewareStack : { add : mockMiddlewareStackAdd } ,
12+ send : jest . fn ( ) ,
13+ middlewareStack : { add : jest . fn ( ) } ,
1714 } ) ) ,
1815 ConverseStreamCommand : jest . fn ( ) ,
19- ConverseCommand : jest . fn ( ) ,
2016 InvokeModelCommand : jest . fn ( ) ,
21- ConversationRole : {
22- USER : "user" ,
23- ASSISTANT : "assistant" ,
24- } ,
2517} ) ) ;
2618
2719// Mock credential provider
@@ -98,8 +90,6 @@ class TestBedrock extends Bedrock {
9890describe ( "Bedrock" , ( ) => {
9991 beforeEach ( ( ) => {
10092 jest . clearAllMocks ( ) ;
101- mockSend . mockReset ( ) ;
102- mockMiddlewareStackAdd . mockReset ( ) ;
10393 } ) ;
10494
10595 describe ( "constructor" , ( ) => {
@@ -314,142 +304,6 @@ describe("Bedrock", () => {
314304 } ) ;
315305 } ) ;
316306
317- describe ( "streaming vs non-streaming behavior" , ( ) => {
318- let bedrock : Bedrock ;
319- const mockAbortSignal = new AbortController ( ) . signal ;
320-
321- beforeEach ( ( ) => {
322- bedrock = new Bedrock ( {
323- apiKey : "test-key" ,
324- model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
325- region : "us-east-1" ,
326- } ) ;
327- } ) ;
328-
329- it ( "should route to streaming path when stream is not false" , async ( ) => {
330- const messages : ChatMessage [ ] = [
331- { role : "user" , content : "Hello" } as UserChatMessage ,
332- ] ;
333-
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-
347- const options : CompletionOptions = {
348- model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
349- stream : true , // Explicitly set streaming
350- } ;
351-
352- const results = [ ] ;
353- for await ( const message of bedrock [ "_streamChat" ] (
354- messages ,
355- mockAbortSignal ,
356- options ,
357- ) ) {
358- results . push ( message ) ;
359- }
360-
361- expect ( streamingMethodSpy ) . toHaveBeenCalled ( ) ;
362- expect ( nonStreamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
363- expect ( results ) . toEqual ( [
364- { role : "assistant" , content : "Streaming response" } ,
365- ] ) ;
366-
367- streamingMethodSpy . mockRestore ( ) ;
368- nonStreamingMethodSpy . mockRestore ( ) ;
369- } ) ;
370-
371- it ( "should route to non-streaming path when stream is false" , async ( ) => {
372- const messages : ChatMessage [ ] = [
373- { role : "user" , content : "Hello" } as UserChatMessage ,
374- ] ;
375-
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-
389- const options : CompletionOptions = {
390- model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
391- stream : false , // Explicitly set non-streaming
392- } ;
393-
394- const results = [ ] ;
395- for await ( const message of bedrock [ "_streamChat" ] (
396- messages ,
397- mockAbortSignal ,
398- options ,
399- ) ) {
400- results . push ( message ) ;
401- }
402-
403- expect ( streamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
404- expect ( nonStreamingMethodSpy ) . toHaveBeenCalled ( ) ;
405- expect ( results ) . toEqual ( [
406- { role : "assistant" , content : "Non-streaming response" } ,
407- ] ) ;
408-
409- streamingMethodSpy . mockRestore ( ) ;
410- nonStreamingMethodSpy . mockRestore ( ) ;
411- } ) ;
412-
413- it ( "should default to streaming path when stream option is undefined" , async ( ) => {
414- const messages : ChatMessage [ ] = [
415- { role : "user" , content : "Hello" } as UserChatMessage ,
416- ] ;
417-
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-
431- const options : CompletionOptions = {
432- model : "anthropic.claude-3-sonnet-20240229-v1:0" ,
433- // stream option is undefined, should default to streaming
434- } ;
435-
436- const results = [ ] ;
437- for await ( const message of bedrock [ "_streamChat" ] (
438- messages ,
439- mockAbortSignal ,
440- options ,
441- ) ) {
442- results . push ( message ) ;
443- }
444-
445- expect ( streamingMethodSpy ) . toHaveBeenCalled ( ) ;
446- expect ( nonStreamingMethodSpy ) . not . toHaveBeenCalled ( ) ;
447-
448- streamingMethodSpy . mockRestore ( ) ;
449- nonStreamingMethodSpy . mockRestore ( ) ;
450- } ) ;
451- } ) ;
452-
453307 describe ( "message conversion" , ( ) => {
454308 let bedrock : TestBedrock ;
455309
0 commit comments