@@ -1677,6 +1677,102 @@ describe("AwsBedrockHandler", () => {
16771677 const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
16781678 expect ( sendOptions ?. abortSignal ) . toBeDefined ( )
16791679 } )
1680+
1681+ it ( "should abort immediately when signal is already aborted and timeoutMs > 0" , async ( ) => {
1682+ const mockSend = vi . fn ( )
1683+
1684+ const handler = new AwsBedrockHandler ( {
1685+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1686+ awsAccessKey : "test-access-key" ,
1687+ awsSecretKey : "test-secret-key" ,
1688+ awsRegion : "us-east-1" ,
1689+ } )
1690+
1691+ const clientInstance = ( handler as any ) . client
1692+ clientInstance . send = mockSend
1693+
1694+ mockSend . mockResolvedValueOnce ( {
1695+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1696+ } )
1697+
1698+ const controller = new AbortController ( )
1699+ controller . abort ( ) // Pre-abort the signal
1700+
1701+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
1702+
1703+ expect ( mockSend ) . toHaveBeenCalled ( )
1704+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
1705+ expect ( sendOptions ?. abortSignal ) . toBeDefined ( )
1706+ expect ( sendOptions ?. abortSignal . aborted ) . toBe ( true )
1707+ } )
1708+ it ( "should return undefined sendOptions when timeoutMs is 0 and no signal" , async ( ) => {
1709+ const mockSend = vi . fn ( )
1710+
1711+ const handler = new AwsBedrockHandler ( {
1712+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1713+ awsAccessKey : "test-access-key" ,
1714+ awsSecretKey : "test-secret-key" ,
1715+ awsRegion : "us-east-1" ,
1716+ } )
1717+
1718+ const clientInstance = ( handler as any ) . client
1719+ clientInstance . send = mockSend
1720+
1721+ mockSend . mockResolvedValueOnce ( {
1722+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1723+ } )
1724+
1725+ await handler . completePrompt ( "test prompt" , { timeoutMs : 0 } )
1726+
1727+ expect ( mockSend ) . toHaveBeenCalled ( )
1728+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
1729+ // When timeoutMs is 0 and no abortSignal, bedrock.ts returns undefined (no signal created)
1730+ expect ( sendOptions ) . toBeUndefined ( )
1731+ } )
1732+
1733+ it ( "should return empty string when response content is empty" , async ( ) => {
1734+ const mockSend = vi . fn ( )
1735+
1736+ const handler = new AwsBedrockHandler ( {
1737+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1738+ awsAccessKey : "test-access-key" ,
1739+ awsSecretKey : "test-secret-key" ,
1740+ awsRegion : "us-east-1" ,
1741+ } )
1742+
1743+ const clientInstance = ( handler as any ) . client
1744+ clientInstance . send = mockSend
1745+
1746+ mockSend . mockResolvedValueOnce ( {
1747+ output : { message : { content : [ { type : "text" , text : "" } ] } , stopReason : null } ,
1748+ } )
1749+
1750+ const result = await handler . completePrompt ( "test prompt" )
1751+
1752+ expect ( result ) . toBe ( "" )
1753+ } )
1754+
1755+ it ( "should return empty string when response content array is empty" , async ( ) => {
1756+ const mockSend = vi . fn ( )
1757+
1758+ const handler = new AwsBedrockHandler ( {
1759+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1760+ awsAccessKey : "test-access-key" ,
1761+ awsSecretKey : "test-secret-key" ,
1762+ awsRegion : "us-east-1" ,
1763+ } )
1764+
1765+ const clientInstance = ( handler as any ) . client
1766+ clientInstance . send = mockSend
1767+
1768+ mockSend . mockResolvedValueOnce ( {
1769+ output : { message : { content : [ ] } , stopReason : null } ,
1770+ } )
1771+
1772+ const result = await handler . completePrompt ( "test prompt" )
1773+
1774+ expect ( result ) . toBe ( "" )
1775+ } )
16801776 } )
16811777 } )
16821778} )
0 commit comments