@@ -6,6 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
66import { WebSocket , WebSocketServer } from "ws" ;
77import { configureAiTransportHost } from "../host.js" ;
88import { cleanupSessionResources } from "../session-resources.js" ;
9+ import { MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE } from "../transports/transport-utils.js" ;
910import type { Context , Model } from "../types.js" ;
1011import {
1112 closeOpenAICodexWebSocketSessions ,
@@ -387,6 +388,108 @@ describe("ChatGPT Responses cached transport", () => {
387388 ) . toEqual ( [ ] ) ;
388389 } ) ;
389390
391+ it . each ( [
392+ {
393+ label : "valid UTF-8 JSON" ,
394+ frame : new TextEncoder ( ) . encode ( JSON . stringify ( completion ( "resp_binary" ) ) ) . buffer ,
395+ } ,
396+ {
397+ label : "malformed bytes" ,
398+ frame : Uint8Array . from ( [ 0xff , 0xfe , 0xfd ] ) . buffer ,
399+ } ,
400+ ] ) ( "rejects $label in a binary frame and invalidates cached state" , async ( { frame } ) => {
401+ const sockets : ProtocolFrameWebSocket [ ] = [ ] ;
402+ const sentPayloads : Array < Record < string , unknown > > = [ ] ;
403+ let connectionCount = 0 ;
404+
405+ class ProtocolFrameWebSocket extends EventTarget {
406+ readonly connectionId = ++ connectionCount ;
407+ readonly listeners = new Map < string , Set < EventListenerOrEventListenerObject > > ( ) ;
408+ readyState = 1 ;
409+ sendCount = 0 ;
410+
411+ constructor ( ) {
412+ super ( ) ;
413+ sockets . push ( this ) ;
414+ queueMicrotask ( ( ) => this . dispatchEvent ( new Event ( "open" ) ) ) ;
415+ }
416+
417+ override addEventListener (
418+ type : string ,
419+ listener : EventListenerOrEventListenerObject ,
420+ options ?: boolean | AddEventListenerOptions ,
421+ ) : void {
422+ super . addEventListener ( type , listener , options ) ;
423+ const listeners = this . listeners . get ( type ) ?? new Set ( ) ;
424+ listeners . add ( listener ) ;
425+ this . listeners . set ( type , listeners ) ;
426+ }
427+
428+ override removeEventListener (
429+ type : string ,
430+ listener : EventListenerOrEventListenerObject ,
431+ options ?: boolean | EventListenerOptions ,
432+ ) : void {
433+ super . removeEventListener ( type , listener , options ) ;
434+ this . listeners . get ( type ) ?. delete ( listener ) ;
435+ }
436+
437+ send ( payload : string ) : void {
438+ this . sendCount += 1 ;
439+ sentPayloads . push ( JSON . parse ( payload ) as Record < string , unknown > ) ;
440+ queueMicrotask ( ( ) => {
441+ const data =
442+ this . connectionId === 1 && this . sendCount === 2
443+ ? frame
444+ : JSON . stringify ( completion ( `resp_${ this . connectionId } _${ this . sendCount } ` ) ) ;
445+ this . dispatchEvent ( Object . assign ( new Event ( "message" ) , { data } ) ) ;
446+ } ) ;
447+ }
448+
449+ close ( ) : void {
450+ this . readyState = 3 ;
451+ }
452+
453+ activeStreamListenerCount ( ) : number {
454+ return [ "message" , "error" , "close" ] . reduce (
455+ ( count , type ) => count + ( this . listeners . get ( type ) ?. size ?? 0 ) ,
456+ 0 ,
457+ ) ;
458+ }
459+ }
460+
461+ vi . stubGlobal ( "WebSocket" , ProtocolFrameWebSocket ) ;
462+ const options = {
463+ apiKey : createJwt ( ) ,
464+ sessionId : `binary-frame-${ connectionCount } ` ,
465+ transport : "websocket-cached" as const ,
466+ } ;
467+ const followUpContext = {
468+ messages : [ ...context . messages , { role : "user" , content : "follow-up" , timestamp : 2 } ] ,
469+ } satisfies Context ;
470+
471+ expect ( ( await streamOpenAICodexResponses ( model , context , options ) . result ( ) ) . stopReason ) . toBe (
472+ "stop" ,
473+ ) ;
474+ expect ( sockets [ 0 ] ?. activeStreamListenerCount ( ) ) . toBe ( 0 ) ;
475+
476+ const rejected = await streamOpenAICodexResponses ( model , followUpContext , options ) . result ( ) ;
477+ expect ( rejected ) . toMatchObject ( {
478+ stopReason : "error" ,
479+ errorMessage : MALFORMED_STREAMING_FRAGMENT_ERROR_MESSAGE ,
480+ } ) ;
481+ expect ( sockets [ 0 ] ) . toMatchObject ( { readyState : 3 , sendCount : 2 } ) ;
482+ expect ( sockets [ 0 ] ?. activeStreamListenerCount ( ) ) . toBe ( 0 ) ;
483+ expect ( sentPayloads [ 1 ] ?. previous_response_id ) . toBe ( "resp_1_1" ) ;
484+
485+ expect (
486+ ( await streamOpenAICodexResponses ( model , followUpContext , options ) . result ( ) ) . stopReason ,
487+ ) . toBe ( "stop" ) ;
488+ expect ( sockets ) . toHaveLength ( 2 ) ;
489+ expect ( sockets [ 1 ] ?. activeStreamListenerCount ( ) ) . toBe ( 0 ) ;
490+ expect ( sentPayloads [ 2 ] ?. previous_response_id ) . toBeUndefined ( ) ;
491+ } ) ;
492+
390493 it ( "closes the concurrent acquire loser promptly without leaking its socket" , async ( ) => {
391494 const apiKey = createJwt ( ) ;
392495 const sessionId = "concurrent-acquire-loser" ;
0 commit comments