@@ -74,6 +74,14 @@ function requireMattermostReplyToModeResolver() {
7474 return resolveReplyToMode ;
7575}
7676
77+ function requireMattermostThreadTargetMatcher ( ) {
78+ const matchesToolContextTarget = mattermostPlugin . threading ?. matchesToolContextTarget ;
79+ if ( ! matchesToolContextTarget ) {
80+ throw new Error ( "mattermost threading.matchesToolContextTarget missing" ) ;
81+ }
82+ return matchesToolContextTarget ;
83+ }
84+
7785function requireMattermostSendText ( ) {
7886 const sendText = mattermostPlugin . outbound ?. sendText ;
7987 if ( ! sendText ) {
@@ -236,6 +244,27 @@ describe("mattermostPlugin", () => {
236244 } ,
237245 ) ;
238246
247+ it ( "matches bare Mattermost channel ids against the active channel target" , ( ) => {
248+ const matchesToolContextTarget = requireMattermostThreadTargetMatcher ( ) ;
249+
250+ expect (
251+ matchesToolContextTarget ( {
252+ target : "tqfek9psh7fw8mpa5berwyytqw" ,
253+ toolContext : {
254+ currentChannelId : "channel:tqfek9psh7fw8mpa5berwyytqw" ,
255+ } ,
256+ } ) ,
257+ ) . toBe ( true ) ;
258+ expect (
259+ matchesToolContextTarget ( {
260+ target : "tqfek9psh7fw8mpa5berwyytqw" ,
261+ toolContext : {
262+ currentChannelId : "channel:kqfek9psh7fw8mpa5berwyytqw" ,
263+ } ,
264+ } ) ,
265+ ) . toBe ( false ) ;
266+ } ) ;
267+
239268 it ( "exposes the effective reply root as the transport thread" , ( ) => {
240269 const resolveReplyTransport = mattermostPlugin . threading ?. resolveReplyTransport ;
241270 if ( ! resolveReplyTransport ) {
@@ -249,8 +278,30 @@ describe("mattermostPlugin", () => {
249278 threadId : "other-thread" ,
250279 } ) ,
251280 ) . toEqual ( {
252- replyToId : "post-parent" ,
253- threadId : "post-parent" ,
281+ replyToId : "other-thread" ,
282+ threadId : "other-thread" ,
283+ } ) ;
284+ expect (
285+ resolveReplyTransport ( {
286+ cfg : { } ,
287+ replyToId : "child-post" ,
288+ replyToIsExplicit : true ,
289+ threadId : "root-post" ,
290+ } ) ,
291+ ) . toEqual ( {
292+ replyToId : "root-post" ,
293+ threadId : "root-post" ,
294+ } ) ;
295+ expect (
296+ resolveReplyTransport ( {
297+ cfg : { } ,
298+ replyToId : "child-post" ,
299+ replyToIsExplicit : false ,
300+ threadId : "root-post" ,
301+ } ) ,
302+ ) . toEqual ( {
303+ replyToId : "root-post" ,
304+ threadId : "root-post" ,
254305 } ) ;
255306 expect (
256307 resolveReplyTransport ( {
@@ -402,6 +453,17 @@ describe("mattermostPlugin", () => {
402453 } ,
403454 } ) ,
404455 ) . toBeUndefined ( ) ;
456+ expect (
457+ resolveAutoThreadId ( {
458+ cfg : { } ,
459+ to : "tqfek9psh7fw8mpa5berwyytqw" ,
460+ toolContext : {
461+ currentChannelId : "channel:tqfek9psh7fw8mpa5berwyytqw" ,
462+ currentThreadTs : "root-1" ,
463+ replyToMode : "all" ,
464+ } ,
465+ } ) ,
466+ ) . toBe ( "root-1" ) ;
405467 expect (
406468 resolveAutoThreadId ( {
407469 cfg : { } ,
@@ -714,7 +776,28 @@ describe("mattermostPlugin", () => {
714776 expect ( options . replyToId ) . toBe ( "post-root" ) ;
715777 } ) ;
716778
717- it ( "keeps explicit reply precedence when threadId is also provided" , async ( ) => {
779+ it ( "uses threadId as the Mattermost root when generic replyTo names a child post" , async ( ) => {
780+ const cfg = createMattermostTestConfig ( ) ;
781+
782+ await mattermostPlugin . actions ?. handleAction ?.(
783+ createMattermostActionContext ( {
784+ action : "send" ,
785+ params : {
786+ to : "channel:CHAN1" ,
787+ message : "hello" ,
788+ threadId : "post-root" ,
789+ replyTo : "child-post" ,
790+ } ,
791+ cfg,
792+ accountId : "default" ,
793+ } ) ,
794+ ) ;
795+
796+ const options = expectSingleMattermostSend ( "channel:CHAN1" , "hello" ) ;
797+ expect ( options . replyToId ) . toBe ( "post-root" ) ;
798+ } ) ;
799+
800+ it ( "keeps explicit replyToId precedence when threadId is also provided" , async ( ) => {
718801 const cfg = createMattermostTestConfig ( ) ;
719802
720803 await mattermostPlugin . actions ?. handleAction ?.(
@@ -723,6 +806,7 @@ describe("mattermostPlugin", () => {
723806 params : {
724807 to : "channel:CHAN1" ,
725808 message : "hello" ,
809+ replyToId : "explicit-root" ,
726810 threadId : "post-root" ,
727811 replyTo : "child-post" ,
728812 } ,
@@ -732,7 +816,7 @@ describe("mattermostPlugin", () => {
732816 ) ;
733817
734818 const options = expectSingleMattermostSend ( "channel:CHAN1" , "hello" ) ;
735- expect ( options . replyToId ) . toBe ( "child-post " ) ;
819+ expect ( options . replyToId ) . toBe ( "explicit-root " ) ;
736820 } ) ;
737821
738822 it ( "routes filePath send actions through Mattermost media upload options" , async ( ) => {
0 commit comments