11import type { OpenClawPluginToolContext } from "openclaw/plugin-sdk/plugin-entry" ;
22import { describe , expect , it , vi } from "vitest" ;
3- import { buildDiscordActivityCustomId } from "../component-custom-id.js" ;
4- import type { sendMessageDiscord } from "../send.js" ;
3+ import {
4+ buildDiscordActivityCustomId ,
5+ parseDiscordActivityCustomIdForInteraction ,
6+ } from "../component-custom-id.js" ;
7+ import { buildDiscordComponentMessage } from "../components.js" ;
8+ import type { sendDiscordComponentMessage } from "../send.components.js" ;
9+ import { createDiscordSendReceipt } from "../send.receipt.js" ;
510import { createActivityTestRuntime } from "./test-helpers.test-support.js" ;
611import { createDiscordWidgetTool } from "./tool.js" ;
712
@@ -25,14 +30,14 @@ describe("discord_widget", () => {
2530
2631 it ( "stores a wrapped widget and posts its launch button" , async ( ) => {
2732 const runtime = createActivityTestRuntime ( ) ;
28- const send = vi . fn ( async ( ..._args : Parameters < typeof sendMessageDiscord > ) => ( {
33+ const send = vi . fn ( async ( ..._args : Parameters < typeof sendDiscordComponentMessage > ) => ( {
2934 messageId : "message-1" ,
3035 channelId : "987654321" ,
3136 receipt : { } ,
3237 } ) ) ;
3338 const tool = createDiscordWidgetTool ( discordContext ( ) , {
3439 runtime,
35- sendMessage : send as unknown as typeof sendMessageDiscord ,
40+ sendComponentMessage : send as unknown as typeof sendDiscordComponentMessage ,
3641 now : ( ) => 7 ,
3742 } ) ;
3843 if ( ! tool ) {
@@ -56,24 +61,38 @@ describe("discord_widget", () => {
5661 } ) ;
5762 expect ( stored ?. html ) . toContain ( "<!doctype html>" ) ;
5863 expect ( stored ?. html ) . toContain ( "<button" ) ;
59- const options = send . mock . calls [ 0 ] ?. [ 2 ] as { components ?: Array < { serialize ( ) : unknown } > } ;
60- expect ( send ) . toHaveBeenCalledWith ( "channel:987654321" , "Status" , expect . any ( Object ) ) ;
61- expect ( options . components ?. [ 0 ] ?. serialize ( ) ) . toEqual ( {
62- type : 1 ,
63- components : [
64+ const spec = send . mock . calls [ 0 ] ?. [ 1 ] ;
65+ const customId = buildDiscordActivityCustomId ( details . widgetId ) ;
66+ expect ( send ) . toHaveBeenCalledWith ( "channel:987654321" , expect . any ( Object ) , expect . any ( Object ) ) ;
67+ expect ( send . mock . calls [ 0 ] ?. [ 2 ] ) . toMatchObject ( { allowedMentions : { parse : [ ] } } ) ;
68+ if ( ! spec ) {
69+ throw new Error ( "expected Discord component spec" ) ;
70+ }
71+ expect ( spec ) . toEqual ( {
72+ text : "Status" ,
73+ blocks : [
6474 {
65- type : 2 ,
66- style : 1 ,
67- custom_id : buildDiscordActivityCustomId ( details . widgetId ) ,
68- label : "Open widget" ,
75+ type : "actions" ,
76+ buttons : [
77+ {
78+ label : "Open widget" ,
79+ style : "secondary" ,
80+ internalCustomId : customId ,
81+ } ,
82+ ] ,
6983 } ,
7084 ] ,
7185 } ) ;
86+ expect ( JSON . stringify ( buildDiscordComponentMessage ( { spec } ) . components ) ) . toContain ( customId ) ;
87+ expect ( parseDiscordActivityCustomIdForInteraction ( customId ) ) . toEqual ( {
88+ key : "ocactivity" ,
89+ data : { widgetId : details . widgetId } ,
90+ } ) ;
7291 } ) ;
7392
7493 it ( "resolves a provider-prefixed forum thread target" , async ( ) => {
7594 const runtime = createActivityTestRuntime ( ) ;
76- const send = vi . fn ( async ( ..._args : Parameters < typeof sendMessageDiscord > ) => ( {
95+ const send = vi . fn ( async ( ..._args : Parameters < typeof sendDiscordComponentMessage > ) => ( {
7796 messageId : "message-1" ,
7897 channelId : "987654321" ,
7998 receipt : { } ,
@@ -85,7 +104,7 @@ describe("discord_widget", () => {
85104 } ) ,
86105 {
87106 runtime,
88- sendMessage : send as unknown as typeof sendMessageDiscord ,
107+ sendComponentMessage : send as unknown as typeof sendDiscordComponentMessage ,
89108 } ,
90109 ) ;
91110 if ( ! tool ) {
@@ -98,20 +117,24 @@ describe("discord_widget", () => {
98117 } ) ;
99118
100119 expect ( result . details ) . toMatchObject ( { channelId : "987654321" } ) ;
101- expect ( send ) . toHaveBeenCalledWith ( "channel:987654321" , "Forum widget" , expect . any ( Object ) ) ;
120+ expect ( send ) . toHaveBeenCalledWith (
121+ "channel:987654321" ,
122+ expect . objectContaining ( { text : "Forum widget" } ) ,
123+ expect . any ( Object ) ,
124+ ) ;
102125 } ) ;
103126
104127 it ( "keeps full documents unchanged and rejects oversized HTML" , async ( ) => {
105128 const document = "<!doctype html><html><body>full</body></html>" ;
106129 const runtime = createActivityTestRuntime ( ) ;
107- const send = vi . fn ( async ( ..._args : Parameters < typeof sendMessageDiscord > ) => ( {
130+ const send = vi . fn ( async ( ..._args : Parameters < typeof sendDiscordComponentMessage > ) => ( {
108131 messageId : "message-1" ,
109132 channelId : "987654321" ,
110133 receipt : { } ,
111134 } ) ) ;
112135 const tool = createDiscordWidgetTool ( discordContext ( ) , {
113136 runtime,
114- sendMessage : send as unknown as typeof sendMessageDiscord ,
137+ sendComponentMessage : send as unknown as typeof sendDiscordComponentMessage ,
115138 } ) ;
116139 if ( ! tool ) {
117140 throw new Error ( "expected Discord widget tool" ) ;
@@ -143,8 +166,8 @@ describe("discord_widget", () => {
143166 const failure = new Error ( "send failed" ) ;
144167 const send = vi . fn ( async ( ) => {
145168 throw failure ;
146- } ) as unknown as typeof sendMessageDiscord ;
147- const tool = createDiscordWidgetTool ( discordContext ( ) , { runtime, sendMessage : send } ) ;
169+ } ) as unknown as typeof sendDiscordComponentMessage ;
170+ const tool = createDiscordWidgetTool ( discordContext ( ) , { runtime, sendComponentMessage : send } ) ;
148171 if ( ! tool ) {
149172 throw new Error ( "expected Discord widget tool" ) ;
150173 }
@@ -157,10 +180,42 @@ describe("discord_widget", () => {
157180 ) . resolves . toMatchObject ( { id : existingId , widget : { title : "Existing" } } ) ;
158181 } ) ;
159182
183+ it ( "keeps a widget when component bookkeeping fails after delivery" , async ( ) => {
184+ const runtime = createActivityTestRuntime ( ) ;
185+ const failure = new Error ( "registry failed" ) ;
186+ const send = vi . fn ( async ( ...args : Parameters < typeof sendDiscordComponentMessage > ) => {
187+ await args [ 2 ] . onDeliveryResult ?.( {
188+ messageId : "delivered-message" ,
189+ channelId : "987654321" ,
190+ receipt : createDiscordSendReceipt ( {
191+ platformMessageIds : [ "delivered-message" ] ,
192+ channelId : "987654321" ,
193+ kind : "text" ,
194+ } ) ,
195+ } ) ;
196+ throw failure ;
197+ } ) as unknown as typeof sendDiscordComponentMessage ;
198+ const tool = createDiscordWidgetTool ( discordContext ( ) , { runtime, sendComponentMessage : send } ) ;
199+ if ( ! tool ) {
200+ throw new Error ( "expected Discord widget tool" ) ;
201+ }
202+
203+ const result = await tool . execute ( "delivered-send" , {
204+ html : "<p>delivered</p>" ,
205+ title : "Delivered" ,
206+ } ) ;
207+ const details = result . details as { widgetId : string ; messageId : string } ;
208+
209+ expect ( details . messageId ) . toBe ( "delivered-message" ) ;
210+ await expect ( runtime . store . lookupWidget ( details . widgetId ) ) . resolves . toMatchObject ( {
211+ title : "Delivered" ,
212+ } ) ;
213+ } ) ;
214+
160215 it ( "requires a concrete channel target" , async ( ) => {
161216 const tool = createDiscordWidgetTool ( discordContext ( { nativeChannelId : undefined } ) , {
162217 runtime : createActivityTestRuntime ( ) ,
163- sendMessage : vi . fn ( ) as unknown as typeof sendMessageDiscord ,
218+ sendComponentMessage : vi . fn ( ) as unknown as typeof sendDiscordComponentMessage ,
164219 } ) ;
165220 if ( ! tool ) {
166221 throw new Error ( "expected Discord widget tool" ) ;
@@ -178,7 +233,7 @@ describe("discord_widget", () => {
178233 } ) ,
179234 {
180235 runtime : createActivityTestRuntime ( ) ,
181- sendMessage : vi . fn ( ) as unknown as typeof sendMessageDiscord ,
236+ sendComponentMessage : vi . fn ( ) as unknown as typeof sendDiscordComponentMessage ,
182237 } ,
183238 ) ;
184239 if ( ! tool ) {
0 commit comments