@@ -744,3 +744,83 @@ describe("sendMessageMattermost user-first resolution", () => {
744744 expect ( retryCall ?. [ 2 ] ?. initialDelayMs ) . toBe ( 1000 ) ;
745745 } ) ;
746746} ) ;
747+
748+ describe ( "sendMessageMattermost outbound cache caps" , ( ) => {
749+ function makeAccount ( token : string ) {
750+ return {
751+ accountId : "default" ,
752+ botToken : token ,
753+ baseUrl : "https://mattermost.example.com" ,
754+ config : { } ,
755+ } ;
756+ }
757+
758+ function restoreSendMocks ( ) {
759+ mockState . createMattermostClient . mockReturnValue ( { } ) ;
760+ mockState . createMattermostPost . mockResolvedValue ( { id : "post-id" } ) ;
761+ mockState . createMattermostDirectChannelWithRetry . mockResolvedValue ( { id : "dm-channel-id" } ) ;
762+ mockState . fetchMattermostMe . mockResolvedValue ( { id : "bot-id" } ) ;
763+ }
764+
765+ beforeEach ( ( ) => {
766+ vi . clearAllMocks ( ) ;
767+ restoreSendMocks ( ) ;
768+ } ) ;
769+
770+ it ( "evicts oldest DM channel cache entries after the configured cap" , async ( ) => {
771+ const token = "token-dm-cap-test" ;
772+ mockState . resolveMattermostAccount . mockReturnValue ( makeAccount ( token ) ) ;
773+
774+ const firstUserId = `dm${ "0" . repeat ( 24 ) } ` ;
775+ for ( let index = 0 ; index < 1025 ; index += 1 ) {
776+ const userId = `dm${ String ( index ) . padStart ( 24 , "0" ) } ` ;
777+ mockState . createMattermostDirectChannelWithRetry . mockResolvedValueOnce ( {
778+ id : `dm-channel-${ index } ` ,
779+ } ) ;
780+ await sendMessageMattermost ( `user:${ userId } ` , "hello" , { cfg : TEST_CFG } ) ;
781+ }
782+
783+ expect ( mockState . createMattermostDirectChannelWithRetry ) . toHaveBeenCalledTimes ( 1025 ) ;
784+
785+ vi . clearAllMocks ( ) ;
786+ restoreSendMocks ( ) ;
787+ mockState . resolveMattermostAccount . mockReturnValue ( makeAccount ( token ) ) ;
788+ mockState . createMattermostDirectChannelWithRetry . mockResolvedValueOnce ( {
789+ id : "dm-channel-0-again" ,
790+ } ) ;
791+
792+ await sendMessageMattermost ( `user:${ firstUserId } ` , "hello again" , { cfg : TEST_CFG } ) ;
793+
794+ expect ( mockState . createMattermostDirectChannelWithRetry ) . toHaveBeenCalledTimes ( 1 ) ;
795+ } ) ;
796+
797+ it ( "evicts oldest user-by-name cache entries after the configured cap" , async ( ) => {
798+ const token = "token-user-cap-test" ;
799+ mockState . resolveMattermostAccount . mockReturnValue ( makeAccount ( token ) ) ;
800+
801+ const firstUsername = "user0" ;
802+ for ( let index = 0 ; index < 513 ; index += 1 ) {
803+ const username = `user${ index } ` ;
804+ mockState . fetchMattermostUserByUsername . mockResolvedValueOnce ( { id : `uid-${ index } ` } ) ;
805+ mockState . createMattermostDirectChannelWithRetry . mockResolvedValueOnce ( {
806+ id : `dm-channel-${ index } ` ,
807+ } ) ;
808+ await sendMessageMattermost ( `@${ username } ` , "hello" , { cfg : TEST_CFG } ) ;
809+ }
810+
811+ expect ( mockState . fetchMattermostUserByUsername ) . toHaveBeenCalledTimes ( 513 ) ;
812+
813+ vi . clearAllMocks ( ) ;
814+ restoreSendMocks ( ) ;
815+ mockState . resolveMattermostAccount . mockReturnValue ( makeAccount ( token ) ) ;
816+ mockState . fetchMattermostUserByUsername . mockResolvedValueOnce ( { id : "uid-0" } ) ;
817+ mockState . createMattermostDirectChannelWithRetry . mockResolvedValueOnce ( {
818+ id : "dm-channel-0-again" ,
819+ } ) ;
820+
821+ await sendMessageMattermost ( `@${ firstUsername } ` , "hello again" , { cfg : TEST_CFG } ) ;
822+
823+ expect ( mockState . fetchMattermostUserByUsername ) . toHaveBeenCalledTimes ( 1 ) ;
824+ expect ( mockState . createMattermostDirectChannelWithRetry ) . not . toHaveBeenCalled ( ) ;
825+ } ) ;
826+ } ) ;
0 commit comments