@@ -744,3 +744,93 @@ describe("sendMessageMattermost user-first resolution", () => {
744744 expect ( retryCall ?. [ 2 ] ?. initialDelayMs ) . toBe ( 1000 ) ;
745745 } ) ;
746746} ) ;
747+
748+ describe ( "sendMessageMattermost outbound cache bounds" , ( ) => {
749+ const baseUrl = "https://mattermost.example.com" ;
750+
751+ beforeEach ( async ( ) => {
752+ vi . resetModules ( ) ;
753+ vi . clearAllMocks ( ) ;
754+ mockState . resolveMattermostAccount . mockReturnValue ( {
755+ accountId : "default" ,
756+ botToken : "default-token" ,
757+ baseUrl,
758+ config : { } ,
759+ } ) ;
760+ mockState . createMattermostClient . mockReturnValue ( { } ) ;
761+ mockState . createMattermostPost . mockResolvedValue ( { id : "post-id" } ) ;
762+ mockState . createMattermostDirectChannelWithRetry . mockImplementation (
763+ async ( _client , userIds : string [ ] ) => ( { id : `dm-${ userIds [ 1 ] } ` } ) ,
764+ ) ;
765+ mockState . fetchMattermostMe . mockResolvedValue ( { id : "bot-id" } ) ;
766+ mockState . fetchMattermostUserByUsername . mockImplementation (
767+ async ( _client , username : string ) => ( {
768+ id : `user-${ username } ` ,
769+ } ) ,
770+ ) ;
771+ mockState . fetchMattermostUserTeams . mockResolvedValue ( [ { id : "team-id" } ] ) ;
772+ mockState . fetchMattermostChannelByName . mockImplementation (
773+ async ( _client , _teamId : string , name : string ) => ( { id : `channel-${ name } ` } ) ,
774+ ) ;
775+ ( { sendMessageMattermost } = await import ( "./send.js" ) ) ;
776+ } ) ;
777+
778+ const send = async ( to : string , token : string ) =>
779+ await sendMessageMattermost ( to , "hello" , {
780+ cfg : TEST_CFG ,
781+ botToken : token ,
782+ baseUrl,
783+ } ) ;
784+
785+ it ( "bounds DM channel entries without refreshing insertion order on reads" , async ( ) => {
786+ const token = "dm-cache-token" ;
787+ for ( let index = 0 ; index < 1024 ; index += 1 ) {
788+ await send ( `user:user-${ index } ` , token ) ;
789+ }
790+ await send ( "user:user-0" , token ) ;
791+ await send ( "user:user-1024" , token ) ;
792+ await send ( "user:user-0" , token ) ;
793+ await send ( "user:user-1024" , token ) ;
794+
795+ expect ( mockState . createMattermostDirectChannelWithRetry ) . toHaveBeenCalledTimes ( 1026 ) ;
796+ } ) ;
797+
798+ it ( "bounds username entries and retains the newest resolved target" , async ( ) => {
799+ const token = "username-cache-token" ;
800+ for ( let index = 0 ; index < 1024 ; index += 1 ) {
801+ await send ( `@name-${ index } ` , token ) ;
802+ }
803+ await send ( "@name-0" , token ) ;
804+ await send ( "@name-1024" , token ) ;
805+ await send ( "@name-0" , token ) ;
806+ await send ( "@name-1024" , token ) ;
807+
808+ expect ( mockState . fetchMattermostUserByUsername ) . toHaveBeenCalledTimes ( 1026 ) ;
809+ expect ( mockState . createMattermostDirectChannelWithRetry ) . toHaveBeenCalledTimes ( 1026 ) ;
810+ } ) ;
811+
812+ it ( "bounds channel-name entries and retains the newest resolved target" , async ( ) => {
813+ const token = "channel-cache-token" ;
814+ for ( let index = 0 ; index < 1024 ; index += 1 ) {
815+ await send ( `#channel-${ index } ` , token ) ;
816+ }
817+ await send ( "#channel-0" , token ) ;
818+ await send ( "#channel-1024" , token ) ;
819+ await send ( "#channel-0" , token ) ;
820+ await send ( "#channel-1024" , token ) ;
821+
822+ expect ( mockState . fetchMattermostChannelByName ) . toHaveBeenCalledTimes ( 1026 ) ;
823+ } ) ;
824+
825+ it ( "bounds bot-user entries independently from DM channel entries" , async ( ) => {
826+ for ( let index = 0 ; index < 64 ; index += 1 ) {
827+ await send ( `user:user-${ index } ` , `bot-cache-token-${ index } ` ) ;
828+ }
829+ await send ( "user:probe-before-overflow" , "bot-cache-token-0" ) ;
830+ await send ( "user:user-64" , "bot-cache-token-64" ) ;
831+ await send ( "user:probe-after-overflow" , "bot-cache-token-0" ) ;
832+ await send ( "user:newest-probe" , "bot-cache-token-64" ) ;
833+
834+ expect ( mockState . fetchMattermostMe ) . toHaveBeenCalledTimes ( 66 ) ;
835+ } ) ;
836+ } ) ;
0 commit comments