@@ -433,15 +433,18 @@ describe("DiscordVoiceManager", () => {
433433 runtime : createRuntime ( ) ,
434434 } ) ;
435435
436- const createAgentProxyManager = ( ) =>
437- createManager ( {
438- groupPolicy : "open" ,
439- voice : {
440- enabled : true ,
441- mode : "agent-proxy" ,
442- realtime : { provider : "openai" } ,
436+ const createAgentProxyManager = ( clientOverride ?: ReturnType < typeof createClient > ) =>
437+ createManager (
438+ {
439+ groupPolicy : "open" ,
440+ voice : {
441+ enabled : true ,
442+ mode : "agent-proxy" ,
443+ realtime : { provider : "openai" } ,
444+ } ,
443445 } ,
444- } ) ;
446+ clientOverride ,
447+ ) ;
445448
446449 const expectConnectedStatus = (
447450 manager : InstanceType < typeof managerModule . DiscordVoiceManager > ,
@@ -4114,6 +4117,88 @@ describe("DiscordVoiceManager", () => {
41144117 expectUserMessageIncludes ( "third answer" ) ;
41154118 } ) ;
41164119
4120+ it ( "terminates realtime voice when retained Unicode speech exceeds the byte budget" , async ( ) => {
4121+ const client = createClient ( ) ;
4122+ client . fetchChannel . mockImplementation ( async ( channelId : string ) => {
4123+ const guildId = channelId === "2001" ? "g2" : "g1" ;
4124+ return {
4125+ id : channelId ,
4126+ guildId,
4127+ guild : { id : guildId , name : guildId } ,
4128+ type : ChannelType . GuildVoice ,
4129+ } ;
4130+ } ) ;
4131+ const manager = createAgentProxyManager ( client ) ;
4132+
4133+ await manager . join ( { guildId : "g1" , channelId : "1001" } ) ;
4134+ const entry = getSessionEntry ( manager ) ;
4135+ const realtime = entry . realtime as unknown as {
4136+ enqueueExactSpeechMessage : ( text : string ) => void ;
4137+ } ;
4138+ const connection = ( entry as unknown as { connection : { destroy : ReturnType < typeof vi . fn > } } )
4139+ . connection ;
4140+ const bridgeParams = lastRealtimeBridgeParams ( ) ;
4141+ const accepted = "😀" . repeat ( 8 * 1024 ) ;
4142+ expect ( accepted . length ) . toBe ( 16 * 1024 ) ;
4143+ expect ( Buffer . byteLength ( accepted , "utf8" ) ) . toBe ( 32 * 1024 ) ;
4144+
4145+ await manager . join ( { guildId : "g2" , channelId : "2001" } ) ;
4146+ const siblingRealtime = getSessionEntry ( manager , "g2" ) . realtime as unknown as {
4147+ enqueueExactSpeechMessage : ( text : string ) => void ;
4148+ } ;
4149+
4150+ realtime . enqueueExactSpeechMessage ( accepted ) ;
4151+ expectUserMessageIncludes ( accepted ) ;
4152+ expect ( manager . status ( ) ) . toHaveLength ( 2 ) ;
4153+
4154+ realtime . enqueueExactSpeechMessage ( "overflow" ) ;
4155+
4156+ expect ( manager . status ( ) ) . toEqual ( [
4157+ expect . objectContaining ( { guildId : "g2" , channelId : "2001" } ) ,
4158+ ] ) ;
4159+ expect ( connection . destroy ) . toHaveBeenCalledOnce ( ) ;
4160+ expect ( realtimeSessionMock . close ) . toHaveBeenCalledOnce ( ) ;
4161+ expectUserMessageNotIncludes ( "overflow" ) ;
4162+
4163+ siblingRealtime . enqueueExactSpeechMessage ( "sibling remains usable" ) ;
4164+ expectUserMessageIncludes ( "sibling remains usable" ) ;
4165+
4166+ bridgeParams . onReady ?.( ) ;
4167+ bridgeParams . onEvent ?.( { direction : "server" , type : "response.done" } ) ;
4168+ realtime . enqueueExactSpeechMessage ( "late" ) ;
4169+ entry . stop ( ) ;
4170+
4171+ expect ( connection . destroy ) . toHaveBeenCalledOnce ( ) ;
4172+ expect ( realtimeSessionMock . close ) . toHaveBeenCalledOnce ( ) ;
4173+ expectUserMessageNotIncludes ( "late" ) ;
4174+ } ) ;
4175+
4176+ it ( "terminates realtime voice when retained exact speech exceeds the message budget" , async ( ) => {
4177+ const manager = createAgentProxyManager ( ) ;
4178+
4179+ await manager . join ( { guildId : "g1" , channelId : "1001" } ) ;
4180+ const entry = getSessionEntry ( manager ) ;
4181+ const realtime = entry . realtime as unknown as {
4182+ enqueueExactSpeechMessage : ( text : string ) => void ;
4183+ } ;
4184+ const connection = ( entry as unknown as { connection : { destroy : ReturnType < typeof vi . fn > } } )
4185+ . connection ;
4186+
4187+ for ( let index = 0 ; index < 32 ; index += 1 ) {
4188+ realtime . enqueueExactSpeechMessage ( `answer-${ index } ` ) ;
4189+ }
4190+
4191+ expect ( manager . status ( ) ) . toHaveLength ( 1 ) ;
4192+ expect ( realtimeSessionMock . sendUserMessage ) . toHaveBeenCalledOnce ( ) ;
4193+
4194+ realtime . enqueueExactSpeechMessage ( "answer-overflow" ) ;
4195+
4196+ expect ( manager . status ( ) ) . toStrictEqual ( [ ] ) ;
4197+ expect ( connection . destroy ) . toHaveBeenCalledOnce ( ) ;
4198+ expect ( realtimeSessionMock . close ) . toHaveBeenCalledOnce ( ) ;
4199+ expectUserMessageNotIncludes ( "answer-overflow" ) ;
4200+ } ) ;
4201+
41174202 it ( "does not interrupt active exact speech for a later forced agent-proxy consult" , async ( ) => {
41184203 agentCommandMock
41194204 . mockResolvedValueOnce ( { payloads : [ { text : "first answer" } ] } )
0 commit comments