@@ -1534,6 +1534,28 @@ function getGuardedFetchCall(fetchMock: typeof fetchWithSsrFGuardMock): GuardedF
15341534 return ( fetchMock . mock . calls . at ( 0 ) ?. [ 0 ] as GuardedFetchCall | undefined ) ?? { url : "" } ;
15351535}
15361536
1537+ function cancelTrackedResponse (
1538+ text : string ,
1539+ init : ResponseInit ,
1540+ ) : {
1541+ response : Response ;
1542+ wasCanceled : ( ) => boolean ;
1543+ } {
1544+ let canceled = false ;
1545+ const stream = new ReadableStream < Uint8Array > ( {
1546+ start ( controller ) {
1547+ controller . enqueue ( new TextEncoder ( ) . encode ( text ) ) ;
1548+ } ,
1549+ cancel ( ) {
1550+ canceled = true ;
1551+ } ,
1552+ } ) ;
1553+ return {
1554+ response : new Response ( stream , init ) ,
1555+ wasCanceled : ( ) => canceled ,
1556+ } ;
1557+ }
1558+
15371559async function createOllamaTestStream ( params : {
15381560 baseUrl : string ;
15391561 defaultHeaders ?: Record < string , string > ;
@@ -2684,12 +2706,14 @@ describe("createOllamaStreamFn", () => {
26842706 ) ;
26852707 } ) ;
26862708
2687- it ( "surfaces non-2xx HTTP response as status-prefixed error" , async ( ) => {
2709+ it ( "surfaces bounded non-2xx HTTP response text as a status-prefixed error" , async ( ) => {
2710+ const tracked = cancelTrackedResponse ( `${ "Service Unavailable " . repeat ( 1024 ) } tail` , {
2711+ status : 503 ,
2712+ statusText : "Service Unavailable" ,
2713+ } ) ;
2714+ const textSpy = vi . spyOn ( tracked . response , "text" ) . mockRejectedValue ( new Error ( "unbounded" ) ) ;
26882715 fetchWithSsrFGuardMock . mockResolvedValue ( {
2689- response : new Response ( "Service Unavailable" , {
2690- status : 503 ,
2691- statusText : "Service Unavailable" ,
2692- } ) ,
2716+ response : tracked . response ,
26932717 release : vi . fn ( async ( ) => undefined ) ,
26942718 } ) ;
26952719 try {
@@ -2705,6 +2729,10 @@ describe("createOllamaStreamFn", () => {
27052729 // The error message must start with the HTTP status code so that
27062730 // extractLeadingHttpStatus can parse it for failover/retry logic.
27072731 expect ( errorEvent . error . errorMessage ) . toMatch ( / ^ 5 0 3 \b / ) ;
2732+ expect ( errorEvent . error . errorMessage ) . toContain ( "Service Unavailable" ) ;
2733+ expect ( errorEvent . error . errorMessage ) . not . toContain ( "tail" ) ;
2734+ expect ( tracked . wasCanceled ( ) ) . toBe ( true ) ;
2735+ expect ( textSpy ) . not . toHaveBeenCalled ( ) ;
27082736 } finally {
27092737 fetchWithSsrFGuardMock . mockReset ( ) ;
27102738 }
0 commit comments