@@ -228,8 +228,8 @@ describe("xAI lazy capability providers", () => {
228228 third ,
229229 ] ) ;
230230 expect ( runtimeMocks . transcriptionConnect ) . toHaveBeenCalledOnce ( ) ;
231- expect ( runtimeMocks . transcriptionSendAudio . mock . invocationCallOrder . at ( - 1 ) ) . toBeLessThan (
232- runtimeMocks . transcriptionConnect . mock . invocationCallOrder [ 0 ] ! ,
231+ expect ( runtimeMocks . transcriptionConnect . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
232+ runtimeMocks . transcriptionSendAudio . mock . invocationCallOrder [ 0 ] ! ,
233233 ) ;
234234 } ) ;
235235
@@ -250,30 +250,68 @@ describe("xAI lazy capability providers", () => {
250250 } ) ;
251251
252252 it ( "reopens transcription after close without replaying discarded audio" , async ( ) => {
253+ const reconnecting = createDeferred < void > ( ) ;
254+ const forwarded : Buffer [ ] = [ ] ;
255+ const events : string [ ] = [ ] ;
256+ let connectCount = 0 ;
257+ let providerClosed = false ;
258+ runtimeMocks . transcriptionConnect . mockImplementation ( ( ) => {
259+ providerClosed = false ;
260+ connectCount += 1 ;
261+ if ( connectCount === 1 ) {
262+ return Promise . resolve ( ) ;
263+ }
264+ events . push ( "connect-start" ) ;
265+ return reconnecting . promise . then ( ( ) => {
266+ events . push ( "connect-settle" ) ;
267+ } ) ;
268+ } ) ;
269+ runtimeMocks . transcriptionClose . mockImplementation ( ( ) => {
270+ providerClosed = true ;
271+ } ) ;
272+ runtimeMocks . transcriptionSendAudio . mockImplementation ( ( audio : Buffer ) => {
273+ events . push ( `${ providerClosed ? "drop" : "audio" } :${ audio [ 0 ] } ` ) ;
274+ if ( ! providerClosed ) {
275+ forwarded . push ( audio ) ;
276+ }
277+ } ) ;
253278 const lazy = await loadLazyProviders ( ) ;
254279 const session = lazy . createLazyXaiRealtimeTranscriptionProvider ( ) . createSession ( {
255280 providerConfig : { } ,
256281 } ) ;
257282 const first = Buffer . from ( [ 0x01 ] ) ;
258283 const discarded = Buffer . from ( [ 0x02 ] ) ;
259- const second = Buffer . from ( [ 0x03 ] ) ;
284+ const droppedByLimit = Buffer . alloc ( 1024 * 1024 , 0x03 ) ;
285+ const retainedFirst = Buffer . alloc ( 1024 * 1024 , 0x04 ) ;
286+ const retainedSecond = Buffer . alloc ( 1024 * 1024 , 0x05 ) ;
287+ const live = Buffer . from ( [ 0x06 ] ) ;
260288
261289 session . sendAudio ( first ) ;
262290 await session . connect ( ) ;
263291 session . close ( ) ;
264292 session . close ( ) ;
265293 session . sendAudio ( discarded ) ;
294+ events . length = 0 ;
266295
267296 const reconnectPromise = session . connect ( ) ;
268- session . sendAudio ( second ) ;
269- await reconnectPromise ;
297+ session . sendAudio ( droppedByLimit ) ;
298+ session . sendAudio ( retainedFirst ) ;
299+ session . sendAudio ( retainedSecond ) ;
300+ await vi . waitFor ( ( ) => expect ( runtimeMocks . transcriptionConnect ) . toHaveBeenCalledTimes ( 2 ) ) ;
301+ session . sendAudio ( live ) ;
270302
271- expect ( runtimeMocks . transcriptionConnect ) . toHaveBeenCalledTimes ( 2 ) ;
272303 expect ( runtimeMocks . transcriptionClose ) . toHaveBeenCalledOnce ( ) ;
273- expect ( runtimeMocks . transcriptionSendAudio . mock . calls . map ( ( [ audio ] ) => audio ) ) . toEqual ( [
274- first ,
275- second ,
304+ expect ( forwarded . map ( ( audio ) => [ audio [ 0 ] , audio . byteLength ] ) ) . toEqual ( [
305+ [ 1 , 1 ] ,
306+ [ 4 , 1024 * 1024 ] ,
307+ [ 5 , 1024 * 1024 ] ,
308+ [ 6 , 1 ] ,
276309 ] ) ;
310+ expect ( events ) . toEqual ( [ "connect-start" , "audio:4" , "audio:5" , "audio:6" ] ) ;
311+
312+ reconnecting . resolve ( ) ;
313+ await reconnectPromise ;
314+ expect ( events . at ( - 1 ) ) . toBe ( "connect-settle" ) ;
277315 } ) ;
278316
279317 it ( "preserves voice startup ordering and waits to trigger the greeting" , async ( ) => {
0 commit comments