@@ -102,6 +102,12 @@ function requireFirstMockArg<T>(mock: { mock: { calls: T[][] } }, label: string)
102102 return expectDefined ( arg , "arg test invariant" ) ;
103103}
104104
105+ function encodeSequence ( value : number ) : Buffer {
106+ const frame = Buffer . allocUnsafe ( 2 ) ;
107+ frame . writeUInt16BE ( value ) ;
108+ return frame ;
109+ }
110+
105111describe ( "createRealtimeTranscriptionWebSocketSession" , ( ) => {
106112 it ( "flushes queued binary audio after an open-ready connection" , async ( ) => {
107113 const frames : Buffer [ ] = [ ] ;
@@ -133,6 +139,136 @@ describe("createRealtimeTranscriptionWebSocketSession", () => {
133139 session . close ( ) ;
134140 } ) ;
135141
142+ it ( "drops the oldest queued audio by bytes and flushes the retained tail in order" , async ( ) => {
143+ const frames : Buffer [ ] = [ ] ;
144+ const framesReady = createSignal ( ) ;
145+ const server = await createRealtimeServer ( {
146+ onBinary : ( payload ) => {
147+ frames . push ( payload ) ;
148+ if ( frames . length === 3 ) {
149+ framesReady . resolve ( ) ;
150+ }
151+ } ,
152+ } ) ;
153+ const session = createRealtimeTranscriptionWebSocketSession ( {
154+ providerId : "test" ,
155+ callbacks : { } ,
156+ url : server . url ,
157+ maxQueuedBytes : 7 ,
158+ readyOnOpen : true ,
159+ sendAudio : ( audio , transport ) => {
160+ transport . sendBinary ( audio ) ;
161+ } ,
162+ } ) ;
163+
164+ const shiftSpy = vi . spyOn ( Array . prototype , "shift" ) ;
165+ let shiftCalls = 0 ;
166+ try {
167+ for ( let index = 0 ; index < 13_000 ; index += 1 ) {
168+ session . sendAudio ( encodeSequence ( index ) ) ;
169+ }
170+ session . sendAudio ( Buffer . from ( [ 0xaa , 0xbb , 0xcc ] ) ) ;
171+ shiftCalls = shiftSpy . mock . calls . length ;
172+ } finally {
173+ shiftSpy . mockRestore ( ) ;
174+ }
175+ expect ( shiftCalls ) . toBe ( 0 ) ;
176+
177+ await session . connect ( ) ;
178+ await framesReady . promise ;
179+ expect ( frames ) . toEqual ( [
180+ encodeSequence ( 12_998 ) ,
181+ encodeSequence ( 12_999 ) ,
182+ Buffer . from ( [ 0xaa , 0xbb , 0xcc ] ) ,
183+ ] ) ;
184+ session . close ( ) ;
185+ } ) ;
186+
187+ it ( "flushes a large retained audio tail in order after reconnect" , async ( ) => {
188+ const connections : WebSocket [ ] = [ ] ;
189+ const frames : Buffer [ ] = [ ] ;
190+ const framesReady = createSignal ( ) ;
191+ const server = await createRealtimeServer ( {
192+ onConnection : ( ws ) => connections . push ( ws ) ,
193+ onBinary : ( payload ) => {
194+ frames . push ( payload ) ;
195+ if ( frames . length === 4 ) {
196+ framesReady . resolve ( ) ;
197+ }
198+ } ,
199+ } ) ;
200+ const session = createRealtimeTranscriptionWebSocketSession < { type ?: string } > ( {
201+ providerId : "test" ,
202+ callbacks : { } ,
203+ url : server . url ,
204+ maxQueuedBytes : 8 ,
205+ maxReconnectAttempts : 1 ,
206+ reconnectDelayMs : 1 ,
207+ onMessage : ( event , transport ) => {
208+ if ( event . type === "session.created" ) {
209+ transport . markReady ( ) ;
210+ }
211+ } ,
212+ sendAudio : ( audio , transport ) => {
213+ transport . sendBinary ( audio ) ;
214+ } ,
215+ } ) ;
216+
217+ const connecting = session . connect ( ) ;
218+ await vi . waitFor ( ( ) => expect ( connections ) . toHaveLength ( 1 ) ) ;
219+ connections [ 0 ] ?. send ( JSON . stringify ( { type : "session.created" } ) ) ;
220+ await connecting ;
221+
222+ connections [ 0 ] ?. close ( 1011 , "reconnect" ) ;
223+ await vi . waitFor ( ( ) => expect ( session . isConnected ( ) ) . toBe ( false ) ) ;
224+ await vi . waitFor ( ( ) => expect ( connections ) . toHaveLength ( 2 ) ) ;
225+ for ( let index = 0 ; index < 13_000 ; index += 1 ) {
226+ session . sendAudio ( encodeSequence ( index ) ) ;
227+ }
228+ connections [ 1 ] ?. send ( JSON . stringify ( { type : "session.created" } ) ) ;
229+
230+ await framesReady . promise ;
231+ expect ( frames ) . toEqual ( [
232+ encodeSequence ( 12_996 ) ,
233+ encodeSequence ( 12_997 ) ,
234+ encodeSequence ( 12_998 ) ,
235+ encodeSequence ( 12_999 ) ,
236+ ] ) ;
237+ session . close ( ) ;
238+ } ) ;
239+
240+ it ( "discards a large queued audio tail when closed before connecting" , async ( ) => {
241+ const frames : Buffer [ ] = [ ] ;
242+ const framesReady = createSignal ( ) ;
243+ const server = await createRealtimeServer ( {
244+ onBinary : ( payload ) => {
245+ frames . push ( payload ) ;
246+ framesReady . resolve ( ) ;
247+ } ,
248+ } ) ;
249+ const session = createRealtimeTranscriptionWebSocketSession ( {
250+ providerId : "test" ,
251+ callbacks : { } ,
252+ url : server . url ,
253+ maxQueuedBytes : 8 ,
254+ readyOnOpen : true ,
255+ sendAudio : ( audio , transport ) => {
256+ transport . sendBinary ( audio ) ;
257+ } ,
258+ } ) ;
259+
260+ for ( let index = 0 ; index < 13_000 ; index += 1 ) {
261+ session . sendAudio ( encodeSequence ( index ) ) ;
262+ }
263+ session . close ( ) ;
264+
265+ await session . connect ( ) ;
266+ session . sendAudio ( Buffer . from ( "live" ) ) ;
267+ await framesReady . promise ;
268+ expect ( frames ) . toEqual ( [ Buffer . from ( "live" ) ] ) ;
269+ session . close ( ) ;
270+ } ) ;
271+
136272 it ( "lets providers mark ready after a JSON handshake" , async ( ) => {
137273 const frames : unknown [ ] = [ ] ;
138274 const framesReady = createSignal ( ) ;
0 commit comments