@@ -3837,3 +3837,96 @@ describe("reasoning input content channel", () => {
38373837 expect ( out . encrypted_content ) . toBe ( "upstream-issued-blob" ) ;
38383838 } ) ;
38393839} ) ;
3840+
3841+
3842+ describe ( "raw usage passthrough on the forward path (#41980 parity, #37138 adjacency)" , ( ) => {
3843+ const usageExtras = {
3844+ input_tokens : 7 ,
3845+ output_tokens : 4 ,
3846+ total_tokens : 11 ,
3847+ subscription : { window : { used_percent : 12 } } ,
3848+ future_counter_v2 : "wire-value" ,
3849+ } ;
3850+ const config = {
3851+ port : 0 ,
3852+ defaultProvider : "fixture" ,
3853+ providers : {
3854+ fixture : {
3855+ adapter : "openai-responses" ,
3856+ baseUrl : "https://fixture.test/v1" ,
3857+ authMode : "key" as const ,
3858+ apiKey : "fixture-key" ,
3859+ } ,
3860+ } ,
3861+ } as OcxConfig ;
3862+ const requestBody = ( stream : boolean ) => JSON . stringify ( {
3863+ model : "fixture/model" ,
3864+ stream,
3865+ input : [ { role : "user" , content : [ { type : "input_text" , text : "hi" } ] } ] ,
3866+ } ) ;
3867+ const call = ( stream : boolean ) => handleResponses ( new Request ( "http://localhost/v1/responses" , {
3868+ method : "POST" ,
3869+ headers : { "content-type" : "application/json" } ,
3870+ body : requestBody ( stream ) ,
3871+ } ) , config , { model : "" , provider : "" } ) ;
3872+
3873+ test ( "streamed response.completed with usage extras reaches the client byte-identical" , async ( ) => {
3874+ const savedFetch = globalThis . fetch ;
3875+ globalThis . fetch = ( async ( ) => new Response ( [
3876+ 'event: response.completed' ,
3877+ `data: ${ JSON . stringify ( { type : "response.completed" , response : {
3878+ id : "resp_x" , status : "completed" , output : [
3879+ { type : "message" , status : "completed" , content : [ { type : "output_text" , text : "hi" } ] } ,
3880+ ] , usage : usageExtras ,
3881+ } } ) } `,
3882+ "" ,
3883+ "data: [DONE]" ,
3884+ "" ,
3885+ ] . join ( "\n" ) , { headers : { "content-type" : "text/event-stream" } } ) ) as typeof fetch ;
3886+ try {
3887+ const response = await call ( true ) ;
3888+ const text = await response . text ( ) ;
3889+ const completedLine = text . split ( "\n" ) . find ( line => line . startsWith ( "data:" ) && line . includes ( "response.completed" ) ) ;
3890+ expect ( completedLine ) . toBeDefined ( ) ;
3891+ const payload = JSON . parse ( completedLine ! . slice ( 5 ) . trim ( ) ) as { response : { usage : unknown } } ;
3892+ expect ( payload . response . usage ) . toEqual ( usageExtras ) ;
3893+ } finally {
3894+ globalThis . fetch = savedFetch ;
3895+ }
3896+ } ) ;
3897+
3898+ test ( "non-streaming forward JSON keeps usage extras" , async ( ) => {
3899+ const savedFetch = globalThis . fetch ;
3900+ globalThis . fetch = ( async ( ) => new Response ( JSON . stringify ( {
3901+ id : "resp_x" ,
3902+ status : "completed" ,
3903+ output : [ { type : "message" , status : "completed" , content : [ { type : "output_text" , text : "hi" } ] } ] ,
3904+ usage : usageExtras ,
3905+ } ) , { headers : { "content-type" : "application/json" } } ) ) as typeof fetch ;
3906+ try {
3907+ const response = await call ( false ) ;
3908+ const json = await response . json ( ) as { usage : unknown } ;
3909+ expect ( json . usage ) . toEqual ( usageExtras ) ;
3910+ } finally {
3911+ globalThis . fetch = savedFetch ;
3912+ }
3913+ } ) ;
3914+
3915+ test ( "response.completed without usage is accepted (Codex tolerates usage: null, #37138)" , async ( ) => {
3916+ const savedFetch = globalThis . fetch ;
3917+ globalThis . fetch = ( async ( ) => new Response ( [
3918+ 'data: {"type":"response.completed","response":{"id":"resp_x","status":"completed","output":[{"type":"message","status":"completed","content":[{"type":"output_text","text":"hi"}]}]}}' ,
3919+ "" ,
3920+ "data: [DONE]" ,
3921+ "" ,
3922+ ] . join ( "\n" ) , { headers : { "content-type" : "text/event-stream" } } ) ) as typeof fetch ;
3923+ try {
3924+ const response = await call ( true ) ;
3925+ const text = await response . text ( ) ;
3926+ expect ( text ) . toContain ( "response.completed" ) ;
3927+ expect ( text ) . not . toContain ( '"usage"' ) ;
3928+ } finally {
3929+ globalThis . fetch = savedFetch ;
3930+ }
3931+ } ) ;
3932+ } ) ;
0 commit comments