@@ -4906,110 +4906,191 @@ describe("slack implicit mention policy", () => {
49064906 storeFixture . setup ( ) ;
49074907 } ) ;
49084908
4909+ beforeEach ( ( ) => {
4910+ clearSlackThreadParticipationCache ( ) ;
4911+ } ) ;
4912+
49094913 afterAll ( ( ) => {
49104914 storeFixture . cleanup ( ) ;
49114915 } ) ;
49124916
4913- function createCtxWithImplicitMentions ( implicitMentions ?: {
4914- replyToBot ?: boolean ;
4915- threadParticipation ?: boolean ;
4916- } ) {
4917+ function createCtxWithImplicitMentions (
4918+ implicitMentions ?: {
4919+ replyToBot ?: boolean ;
4920+ threadParticipation ?: boolean ;
4921+ } ,
4922+ options ?: Pick <
4923+ Parameters < typeof createInboundSlackTestContext > [ 0 ] ,
4924+ "channelsConfig" | "groupPolicy"
4925+ > ,
4926+ ) {
49174927 const ctx = createInboundSlackTestContext ( {
49184928 cfg : {
4919- channels : { slack : { enabled : true , implicitMentions } } ,
4929+ channels : {
4930+ slack : {
4931+ enabled : true ,
4932+ implicitMentions,
4933+ ...( options ?. channelsConfig ? { channels : options . channelsConfig } : { } ) ,
4934+ ...( options ?. groupPolicy ? { groupPolicy : options . groupPolicy } : { } ) ,
4935+ } ,
4936+ } ,
49204937 session : { } ,
49214938 } as OpenClawConfig ,
4939+ ...options ,
49224940 } ) ;
49234941 ctx . resolveUserName = async ( ) => ( { name : "Alice" } ) ;
49244942 return ctx ;
49254943 }
49264944
4927- it ( "drops a reply to the bot when replyToBot is disabled" , async ( ) => {
4928- const ctx = createCtxWithImplicitMentions ( { replyToBot : false } ) ;
4945+ async function prepareThreadMessage ( params : {
4946+ ctx : SlackMonitorContext ;
4947+ message ?: Partial < SlackMessageEvent > ;
4948+ eventScope ?: SlackEventScope ;
4949+ } ) {
49294950 const { storePath } = storeFixture . makeTmpStorePath ( ) ;
49304951 vi . spyOn (
49314952 await import ( "openclaw/plugin-sdk/session-store-runtime" ) ,
49324953 "resolveStorePath" ,
49334954 ) . mockReturnValue ( storePath ) ;
4934- const account = createSlackTestAccount ( ) ;
4935- const message : SlackMessageEvent = {
4936- type : "message" ,
4937- channel : "C123" ,
4938- channel_type : "channel" ,
4939- user : "U1" ,
4940- text : "hello" ,
4941- ts : "1700000001.000001" ,
4942- thread_ts : "1700000000.000000" ,
4943- parent_user_id : "B1" , // bot is thread parent
4944- } ;
4945- const result = await prepareSlackMessage ( {
4955+ return await prepareSlackMessage ( {
4956+ ctx : params . ctx ,
4957+ account : createSlackTestAccount ( ) ,
4958+ message : {
4959+ type : "message" ,
4960+ channel : "C123" ,
4961+ channel_type : "channel" ,
4962+ user : "U1" ,
4963+ text : "hello" ,
4964+ ts : "1700000001.000001" ,
4965+ thread_ts : "1700000000.000000" ,
4966+ parent_user_id : "U2" ,
4967+ ...params . message ,
4968+ } ,
4969+ opts : {
4970+ source : "message" ,
4971+ ...( params . eventScope ? { eventScope : params . eventScope } : { } ) ,
4972+ } ,
4973+ } ) ;
4974+ }
4975+
4976+ it ( "drops a reply to the bot when replyToBot is disabled" , async ( ) => {
4977+ const ctx = createCtxWithImplicitMentions ( { replyToBot : false } ) ;
4978+ const result = await prepareThreadMessage ( {
49464979 ctx,
4947- account,
4948- message,
4949- opts : { source : "message" } ,
4980+ message : { parent_user_id : "B1" } ,
49504981 } ) ;
49514982 expect ( result ) . toBeNull ( ) ;
49524983 } ) ;
49534984
49544985 it ( "allows an explicit mention when all implicit thread signals are disabled" , async ( ) => {
4955- const ctx = createCtxWithImplicitMentions ( {
4956- replyToBot : false ,
4957- threadParticipation : false ,
4958- } ) ;
4959- const { storePath } = storeFixture . makeTmpStorePath ( ) ;
4960- vi . spyOn (
4961- await import ( "openclaw/plugin-sdk/session-store-runtime" ) ,
4962- "resolveStorePath" ,
4963- ) . mockReturnValue ( storePath ) ;
4964- const account = createSlackTestAccount ( ) ;
4965- const message : SlackMessageEvent = {
4966- type : "message" ,
4967- channel : "C123" ,
4968- channel_type : "channel" ,
4969- user : "U1" ,
4970- text : "<@B1> hello" ,
4971- ts : "1700000001.000002" ,
4972- thread_ts : "1700000000.000000" ,
4973- parent_user_id : "B1" ,
4974- } ;
4975- const result = await prepareSlackMessage ( {
4986+ const ctx = createCtxWithImplicitMentions (
4987+ { replyToBot : false , threadParticipation : false } ,
4988+ { channelsConfig : { C123 : { requireMention : true } } } ,
4989+ ) ;
4990+ const result = await prepareThreadMessage ( {
49764991 ctx,
4977- account,
4978- message,
4979- opts : { source : "message" } ,
4992+ message : { text : "<@B1> hello" , parent_user_id : "B1" } ,
49804993 } ) ;
4981- if ( ! result ) {
4982- throw new Error ( "expected Slack thread reply message" ) ;
4983- }
4994+ expect ( result ?. ctxPayload . MentionSource ) . toBe ( "explicit_bot" ) ;
49844995 } ) ;
49854996
49864997 it ( "controls persisted thread participation independently from replies to the bot" , async ( ) => {
49874998 const threadTs = "1700000000.000000" ;
49884999 recordSlackThreadParticipation ( "default" , "C123" , threadTs ) ;
49895000 const ctx = createCtxWithImplicitMentions ( { threadParticipation : false } ) ;
4990- const { storePath } = storeFixture . makeTmpStorePath ( ) ;
4991- vi . spyOn (
4992- await import ( "openclaw/plugin-sdk/session-store-runtime" ) ,
4993- "resolveStorePath" ,
4994- ) . mockReturnValue ( storePath ) ;
4995- const account = createSlackTestAccount ( ) ;
4996- const message : SlackMessageEvent = {
4997- type : "message" ,
4998- channel : "C123" ,
4999- channel_type : "channel" ,
5000- user : "U1" ,
5001- text : "hello" ,
5002- ts : "1700000001.000003" ,
5003- thread_ts : threadTs ,
5004- parent_user_id : "U2" ,
5005- } ;
5006- const result = await prepareSlackMessage ( {
5001+ const result = await prepareThreadMessage ( {
50075002 ctx,
5008- account,
5009- message,
5010- opts : { source : "message" } ,
5003+ message : { thread_ts : threadTs } ,
50115004 } ) ;
50125005 expect ( result ) . toBeNull ( ) ;
50135006 } ) ;
5007+
5008+ it ( "accepts an unmentioned reply more than 24 hours after joining a required-mention thread" , async ( ) => {
5009+ const threadTs = "1700000000.000000" ;
5010+ const initialNow = 1_700_000_000_000 ;
5011+ const nowSpy = vi . spyOn ( Date , "now" ) . mockReturnValue ( initialNow ) ;
5012+
5013+ try {
5014+ recordSlackThreadParticipation ( "default" , "C123" , threadTs ) ;
5015+ nowSpy . mockReturnValue ( initialNow + 25 * 60 * 60 * 1000 ) ;
5016+
5017+ const ctx = createCtxWithImplicitMentions ( undefined , {
5018+ channelsConfig : { C123 : { requireMention : true } } ,
5019+ } ) ;
5020+ const result = await prepareThreadMessage ( { ctx, message : { thread_ts : threadTs } } ) ;
5021+
5022+ expect ( result ?. ctxPayload . MentionSource ) . toBe ( "implicit_thread" ) ;
5023+ expect ( result ?. ctxPayload . ImplicitMentionKinds ) . toEqual ( [ "bot_thread_participant" ] ) ;
5024+ } finally {
5025+ nowSpy . mockRestore ( ) ;
5026+ }
5027+ } ) ;
5028+
5029+ it ( "continues requiring a mention in an unrelated thread the bot never joined" , async ( ) => {
5030+ recordSlackThreadParticipation ( "default" , "C123" , "1700000000.000999" ) ;
5031+ const ctx = createCtxWithImplicitMentions ( undefined , {
5032+ channelsConfig : { C123 : { requireMention : true } } ,
5033+ } ) ;
5034+
5035+ expect ( await prepareThreadMessage ( { ctx } ) ) . toBeNull ( ) ;
5036+ } ) ;
5037+
5038+ it ( "preserves explicit channel settings that do not require mentions" , async ( ) => {
5039+ const ctx = createCtxWithImplicitMentions ( undefined , {
5040+ channelsConfig : { C123 : { requireMention : false } } ,
5041+ } ) ;
5042+
5043+ const result = await prepareThreadMessage ( { ctx } ) ;
5044+
5045+ expect ( result ?. ctxPayload . MentionSource ) . toBe ( "none" ) ;
5046+ expect ( result ?. ctxPayload . ImplicitMentionKinds ) . toBeUndefined ( ) ;
5047+ } ) ;
5048+
5049+ const unauthorizedThreadCases : Array < {
5050+ authorization : string ;
5051+ options : Pick <
5052+ Parameters < typeof createInboundSlackTestContext > [ 0 ] ,
5053+ "channelsConfig" | "groupPolicy"
5054+ > ;
5055+ } > = [
5056+ {
5057+ authorization : "channel" ,
5058+ options : {
5059+ channelsConfig : { C_ALLOWED : { enabled : true , requireMention : true } } ,
5060+ groupPolicy : "allowlist" as const ,
5061+ } ,
5062+ } ,
5063+ {
5064+ authorization : "sender" ,
5065+ options : {
5066+ channelsConfig : { C123 : { requireMention : true , users : [ "U_ALLOWED" ] } } ,
5067+ } ,
5068+ } ,
5069+ ] ;
5070+
5071+ it . each ( unauthorizedThreadCases ) (
5072+ "rejects a joined thread when $authorization authorization fails" ,
5073+ async ( { options } ) => {
5074+ recordSlackThreadParticipation ( "default" , "C123" , "1700000000.000000" ) ;
5075+ const ctx = createCtxWithImplicitMentions ( undefined , options ) ;
5076+
5077+ expect ( await prepareThreadMessage ( { ctx } ) ) . toBeNull ( ) ;
5078+ } ,
5079+ ) ;
5080+
5081+ it ( "does not accept participation recorded in a different enterprise workspace" , async ( ) => {
5082+ recordSlackThreadParticipation ( "default" , "C123" , "1700000000.000000" , {
5083+ teamId : "T_OTHER" ,
5084+ } ) ;
5085+ const ctx = createCtxWithImplicitMentions ( undefined , {
5086+ channelsConfig : { C123 : { requireMention : true } } ,
5087+ } ) ;
5088+ const eventScope = {
5089+ teamId : "T1" ,
5090+ client : { } as SlackEventScope [ "client" ] ,
5091+ } satisfies SlackEventScope ;
5092+
5093+ expect ( await prepareThreadMessage ( { ctx, eventScope } ) ) . toBeNull ( ) ;
5094+ } ) ;
50145095} ) ;
50155096/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */
0 commit comments