@@ -3,6 +3,8 @@ import { createPluginRuntimeMock } from "openclaw/plugin-sdk/plugin-test-runtime
33import { withServer } from "openclaw/plugin-sdk/test-env" ;
44import { describe , expect , it , vi } from "vitest" ;
55import { mattermostPlugin } from "./channel.js" ;
6+ import { deliverMattermostReplyPayload } from "./mattermost/reply-delivery.js" ;
7+ import { sendMessageMattermost } from "./mattermost/send.js" ;
68import type { OpenClawConfig } from "./runtime-api.js" ;
79import { setMattermostRuntime } from "./runtime.js" ;
810
@@ -17,6 +19,76 @@ vi.mock("./mattermost/runtime-api.js", async () => ({
1719} ) ) ;
1820
1921describe ( "Mattermost send action loopback" , ( ) => {
22+ it ( "reuses the inbound provider channel when delivering a direct reply" , async ( ) => {
23+ const requests : Array < { path : string ; body ?: unknown } > = [ ] ;
24+
25+ await withServer (
26+ ( request , response ) => {
27+ let body = "" ;
28+ request . setEncoding ( "utf8" ) ;
29+ request . on ( "data" , ( chunk ) => {
30+ body += chunk ;
31+ } ) ;
32+ request . on ( "end" , ( ) => {
33+ const path = request . url ?? "" ;
34+ requests . push ( { path, ...( body ? { body : JSON . parse ( body ) as unknown } : { } ) } ) ;
35+ response . writeHead ( 201 , { "content-type" : "application/json" } ) ;
36+ if ( path === "/api/v4/users/me" ) {
37+ response . end ( JSON . stringify ( { id : "cccccccccccccccccccccccccc" } ) ) ;
38+ return ;
39+ }
40+ if ( path === "/api/v4/channels/direct" ) {
41+ response . end ( JSON . stringify ( { id : CHANNEL_ID } ) ) ;
42+ return ;
43+ }
44+ response . end (
45+ JSON . stringify ( {
46+ id : "post-loopback" ,
47+ channel_id : CHANNEL_ID ,
48+ message : "prepared direct reply" ,
49+ } ) ,
50+ ) ;
51+ } ) ;
52+ } ,
53+ async ( baseUrl ) => {
54+ const core = createPluginRuntimeMock ( ) ;
55+ setMattermostRuntime ( core ) ;
56+ const cfg = {
57+ channels : {
58+ mattermost : {
59+ botToken : "prepared-inbound-loopback" ,
60+ baseUrl,
61+ network : { dangerouslyAllowPrivateNetwork : true } ,
62+ } ,
63+ } ,
64+ } as OpenClawConfig ;
65+
66+ const result = await deliverMattermostReplyPayload ( {
67+ core,
68+ cfg,
69+ payload : { text : "prepared direct reply" } ,
70+ channelId : CHANNEL_ID ,
71+ accountId : "default" ,
72+ textLimit : 4000 ,
73+ tableMode : "off" ,
74+ sendMessage : sendMessageMattermost ,
75+ } ) ;
76+
77+ expect ( result ) . toMatchObject ( {
78+ outcome : "text" ,
79+ messageIds : [ "post-loopback" ] ,
80+ visibleReplySent : true ,
81+ } ) ;
82+ expect ( requests ) . toEqual ( [
83+ {
84+ path : "/api/v4/posts" ,
85+ body : { channel_id : CHANNEL_ID , message : "prepared direct reply" } ,
86+ } ,
87+ ] ) ;
88+ } ,
89+ ) ;
90+ } ) ;
91+
2092 it ( "sends text with blank attachment placeholders and rejects nonblank payloads" , async ( ) => {
2193 const requests : Array < { path : string ; body : unknown } > = [ ] ;
2294
0 commit comments