22import { MessageFlags } from "discord-api-types/v10" ;
33import { isRecentOutboundMessageIdentity } from "openclaw/plugin-sdk/channel-outbound" ;
44import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { makeDiscordRest } from "./send.test-harness.js" ;
56
67const recordChannelActivityMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
78const loadConfigMock = vi . hoisted ( ( ) => vi . fn ( ( ) => ( { channels : { discord : { } } } ) ) ) ;
@@ -28,6 +29,8 @@ vi.mock("openclaw/plugin-sdk/channel-activity-runtime", async () => {
2829} ) ;
2930
3031let sendWebhookMessageDiscord : typeof import ( "./send.webhook.js" ) . sendWebhookMessageDiscord ;
32+ let sendPollDiscord : typeof import ( "./send.outbound.js" ) . sendPollDiscord ;
33+ let sendStickerDiscord : typeof import ( "./send.outbound.js" ) . sendStickerDiscord ;
3134
3235type MockWithCalls = { mock : { calls : unknown [ ] [ ] } } ;
3336
@@ -39,9 +42,19 @@ function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
3942 return call ;
4043}
4144
42- describe ( "sendWebhookMessageDiscord activity" , ( ) => {
45+ async function sendStructuredMessage (
46+ kind : "poll" | "sticker" ,
47+ opts : Parameters < typeof sendStickerDiscord > [ 2 ] ,
48+ ) {
49+ return kind === "poll"
50+ ? sendPollDiscord ( "channel:789" , { question : "Lunch?" , options : [ "Pizza" , "Sushi" ] } , opts )
51+ : sendStickerDiscord ( "channel:789" , [ "123" ] , opts ) ;
52+ }
53+
54+ describe ( "Discord outbound channel activity" , ( ) => {
4355 beforeAll ( async ( ) => {
4456 ( { sendWebhookMessageDiscord } = await import ( "./send.webhook.js" ) ) ;
57+ ( { sendPollDiscord, sendStickerDiscord } = await import ( "./send.outbound.js" ) ) ;
4558 } ) ;
4659
4760 beforeEach ( ( ) => {
@@ -126,6 +139,68 @@ describe("sendWebhookMessageDiscord activity", () => {
126139 expect ( loadConfigMock ) . not . toHaveBeenCalled ( ) ;
127140 } ) ;
128141
142+ it . each ( [
143+ { kind : "poll" , accountId : undefined , defaultAccount : undefined , expectedAccountId : "default" } ,
144+ { kind : "poll" , accountId : " Work " , defaultAccount : undefined , expectedAccountId : "work" } ,
145+ { kind : "poll" , accountId : undefined , defaultAccount : "work" , expectedAccountId : "work" } ,
146+ {
147+ kind : "sticker" ,
148+ accountId : undefined ,
149+ defaultAccount : undefined ,
150+ expectedAccountId : "default" ,
151+ } ,
152+ { kind : "sticker" , accountId : " Work " , defaultAccount : undefined , expectedAccountId : "work" } ,
153+ { kind : "sticker" , accountId : undefined , defaultAccount : "work" , expectedAccountId : "work" } ,
154+ ] as const ) (
155+ "records successful $kind sends for the resolved $expectedAccountId account" ,
156+ async ( { kind, accountId, defaultAccount, expectedAccountId } ) => {
157+ const { rest, postMock } = makeDiscordRest ( ) ;
158+ postMock . mockResolvedValue ( { id : "msg-1" , channel_id : "789" } ) ;
159+ const cfg = {
160+ channels : {
161+ discord : {
162+ token : "resolved-token" ,
163+ accounts : {
164+ default : { token : "default-token" } ,
165+ work : { token : "work-token" } ,
166+ } ,
167+ ...( defaultAccount ? { defaultAccount } : { } ) ,
168+ } ,
169+ } ,
170+ } ;
171+
172+ await sendStructuredMessage ( kind , {
173+ cfg,
174+ rest,
175+ token : "test-token" ,
176+ ...( accountId ? { accountId } : { } ) ,
177+ } ) ;
178+
179+ expect ( recordChannelActivityMock ) . toHaveBeenCalledExactlyOnceWith ( {
180+ channel : "discord" ,
181+ accountId : expectedAccountId ,
182+ direction : "outbound" ,
183+ } ) ;
184+ } ,
185+ ) ;
186+
187+ it . each ( [ "poll" , "sticker" ] as const ) (
188+ "does not record outbound activity when a %s send fails" ,
189+ async ( kind ) => {
190+ const { rest, postMock } = makeDiscordRest ( ) ;
191+ postMock . mockRejectedValue ( new Error ( "provider rejected" ) ) ;
192+
193+ await expect (
194+ sendStructuredMessage ( kind , {
195+ cfg : { channels : { discord : { token : "resolved-token" } } } ,
196+ rest,
197+ token : "test-token" ,
198+ } ) ,
199+ ) . rejects . toThrow ( "provider rejected" ) ;
200+ expect ( recordChannelActivityMock ) . not . toHaveBeenCalled ( ) ;
201+ } ,
202+ ) ;
203+
129204 it ( "rewrites configured mention aliases for webhook sends" , async ( ) => {
130205 const cfg = {
131206 channels : {
0 commit comments