@@ -722,6 +722,70 @@ test("chat-native non-stream fold preserves tool calls and finish reason", async
722722 }
723723} ) ;
724724
725+
726+ test ( "chat-native non-stream budget overflow returns 413 without hanging" , async ( ) => {
727+ // Two sequential requests: first overflows the bounded JSON reader (413), second proves server still responsive.
728+ let calls = 0 ;
729+ const upstream = Bun . serve ( {
730+ port : 0 ,
731+ fetch ( ) {
732+ calls += 1 ;
733+ if ( calls === 1 ) {
734+ const chunk = new TextEncoder ( ) . encode ( '{"id":"chatcmpl_x","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"' + "y" . repeat ( 33 * 1024 * 1024 ) + '"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}' ) ;
735+ return new Response ( chunk , { headers : { "content-type" : "application/json" } } ) ;
736+ }
737+ return Response . json ( { id : "chatcmpl_ok" , object : "chat.completion" , choices : [ { index : 0 , message : { role : "assistant" , content : "ok" } , finish_reason : "stop" } ] , usage : { prompt_tokens : 1 , completion_tokens : 1 } } ) ;
738+ } ,
739+ } ) ;
740+ saveConfig ( mockConfig ( `${ upstream . url . toString ( ) . replace ( / \/ \$ / , "" ) } /v1` ) ) ;
741+ const server = startServer ( 0 ) ;
742+ try {
743+ const response = await fetch ( new URL ( "/v1/chat/completions" , server . url ) , {
744+ method : "POST" ,
745+ headers : { "content-type" : "application/json" } ,
746+ body : JSON . stringify ( { model : "mock/test-model" , stream : false , messages : [ { role : "user" , content : "hi" } ] } ) ,
747+ } ) ;
748+ expect ( response . status ) . toBe ( 413 ) ;
749+ const json = await response . json ( ) as { error ?: { code ?: string } } ;
750+ expect ( json . error ?. code ) . toBe ( "translation_buffer_limit" ) ;
751+ const response2 = await fetch ( new URL ( "/v1/chat/completions" , server . url ) , {
752+ method : "POST" ,
753+ headers : { "content-type" : "application/json" } ,
754+ body : JSON . stringify ( { model : "mock/test-model" , stream : false , messages : [ { role : "user" , content : "hi" } ] } ) ,
755+ } ) ;
756+ expect ( response2 . status ) . toBe ( 200 ) ;
757+ // Prove the overflow path released the body/reader: second request consumed a fresh upstream response.
758+ expect ( calls ) . toBe ( 2 ) ;
759+ } finally {
760+ await server . stop ( true ) ;
761+ upstream . stop ( true ) ;
762+ }
763+ } ) ;
764+
765+ test ( "chat-native non-stream invalid JSON is forwarded as 200 passthrough" , async ( ) => {
766+ // The native JSON path forwards verbatim when parsing fails (bridges legacy behavior for untrusted upstreams).
767+ const upstream = Bun . serve ( {
768+ port : 0 ,
769+ fetch ( ) {
770+ return new Response ( "not-json-at-all" , { headers : { "content-type" : "application/json" } } ) ;
771+ } ,
772+ } ) ;
773+ saveConfig ( mockConfig ( `${ upstream . url . toString ( ) . replace ( / \/ \$ / , "" ) } /v1` ) ) ;
774+ const server = startServer ( 0 ) ;
775+ try {
776+ const response = await fetch ( new URL ( "/v1/chat/completions" , server . url ) , {
777+ method : "POST" ,
778+ headers : { "content-type" : "application/json" } ,
779+ body : JSON . stringify ( { model : "mock/test-model" , stream : false , messages : [ { role : "user" , content : "hi" } ] } ) ,
780+ } ) ;
781+ expect ( response . status ) . toBe ( 200 ) ;
782+ expect ( await response . text ( ) ) . toBe ( "not-json-at-all" ) ;
783+ } finally {
784+ await server . stop ( true ) ;
785+ upstream . stop ( true ) ;
786+ }
787+ } ) ;
788+
725789test ( "POST /v1/chat/completions direct mode forwards caller Authorization" , async ( ) => {
726790 const seen : Array < { authorization : string | null } > = [ ] ;
727791 const upstream = Bun . serve ( {
0 commit comments