@@ -51,6 +51,7 @@ beforeEach(() => {
5151} ) ;
5252
5353afterEach ( ( ) => {
54+ vi . unstubAllGlobals ( ) ;
5455 vi . useRealTimers ( ) ;
5556} ) ;
5657
@@ -137,6 +138,79 @@ function createRuntime(): { runtime: PluginRuntime; mocks: LineRuntimeMocks } {
137138}
138139
139140describe ( "line outbound sendPayload" , ( ) => {
141+ it ( "returns the provider receipt for a Flex-only legacy text send" , async ( ) => {
142+ const { runtime, mocks } = createRuntime ( ) ;
143+ setLineRuntime ( runtime ) ;
144+ const cfg = {
145+ channels : { line : { channelAccessToken : "line-fixture-token" } } ,
146+ } as OpenClawConfig ;
147+ const providerResponse = new Response ( JSON . stringify ( { sentMessages : [ { id : "m-flex" } ] } ) , {
148+ status : 200 ,
149+ headers : { "Content-Type" : "application/json" } ,
150+ } ) ;
151+ const fetch = vi . fn ( async ( ) => providerResponse ) ;
152+ vi . stubGlobal ( "fetch" , fetch ) ;
153+ const onDeliveryResult = vi . fn ( ) ;
154+
155+ const result = await lineOutboundAdapter . sendText ! ( {
156+ to : "line:user:U123" ,
157+ text : "| Name | Status |\n|---|---|\n| OpenClaw | ready |" ,
158+ accountId : "default" ,
159+ cfg,
160+ onDeliveryResult,
161+ } ) ;
162+
163+ expect ( result . messageId ) . toBe ( "m-flex" ) ;
164+ expect ( result . receipt ?. platformMessageIds ) . toEqual ( [ "m-flex" ] ) ;
165+ expect ( result . receipt ?. threadId ) . toBe ( "c1" ) ;
166+ expect ( mocks . pushFlexMessage ) . toHaveBeenCalledOnce ( ) ;
167+ expect ( onDeliveryResult ) . toHaveBeenCalledOnce ( ) ;
168+ expect ( onDeliveryResult ) . toHaveBeenCalledWith ( expect . objectContaining ( { messageId : "m-flex" } ) ) ;
169+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
170+ } ) ;
171+
172+ it ( "publishes completed Flex receipts before a later legacy text send fails" , async ( ) => {
173+ const { runtime, mocks } = createRuntime ( ) ;
174+ setLineRuntime ( runtime ) ;
175+ const cfg = {
176+ channels : { line : { channelAccessToken : "line-fixture-token" } } ,
177+ } as OpenClawConfig ;
178+ const laterFailure = new Error ( "second LINE Flex send failed" ) ;
179+ const fetch = vi
180+ . fn ( )
181+ . mockResolvedValueOnce (
182+ new Response ( JSON . stringify ( { sentMessages : [ { id : "m-first-flex" } ] } ) , {
183+ status : 200 ,
184+ headers : { "Content-Type" : "application/json" } ,
185+ } ) ,
186+ )
187+ . mockRejectedValueOnce ( laterFailure ) ;
188+ vi . stubGlobal ( "fetch" , fetch ) ;
189+ mocks . pushFlexMessage
190+ . mockResolvedValueOnce ( lineResult ( "m-first-flex" ) )
191+ . mockRejectedValueOnce ( laterFailure ) ;
192+ const onDeliveryResult = vi . fn ( ) ;
193+
194+ await expect (
195+ lineOutboundAdapter . sendText ! ( {
196+ to : "line:user:U123" ,
197+ text : "```js\nfirst()\n```\n\n```js\nsecond()\n```" ,
198+ accountId : "default" ,
199+ cfg,
200+ onDeliveryResult,
201+ } ) ,
202+ ) . rejects . toThrow ( "second LINE Flex send failed" ) ;
203+
204+ expect ( onDeliveryResult ) . toHaveBeenCalledOnce ( ) ;
205+ expect ( onDeliveryResult ) . toHaveBeenCalledWith (
206+ expect . objectContaining ( {
207+ messageId : "m-first-flex" ,
208+ receipt : expect . objectContaining ( { platformMessageIds : [ "m-first-flex" ] } ) ,
209+ } ) ,
210+ ) ;
211+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
212+ } ) ;
213+
140214 it ( "sends flex message without dropping text" , async ( ) => {
141215 const { runtime, mocks } = createRuntime ( ) ;
142216 setLineRuntime ( runtime ) ;
@@ -195,6 +269,62 @@ describe("line outbound sendPayload", () => {
195269 ] ) ;
196270 } ) ;
197271
272+ it ( "preserves every provider receipt and conversation for an inline LINE batch" , async ( ) => {
273+ const { runtime, mocks } = createRuntime ( ) ;
274+ setLineRuntime ( runtime ) ;
275+ const cfg = { channels : { line : { } } } as OpenClawConfig ;
276+ const providerMessageIds = [ "line-provider-first" , "line-provider-second" ] as const ;
277+ mocks . pushMessagesLine . mockResolvedValueOnce ( {
278+ messageId : providerMessageIds [ 0 ] ,
279+ chatId : "C123" ,
280+ receipt : createLineSendReceipt ( {
281+ messageId : providerMessageIds [ 0 ] ,
282+ messageIds : providerMessageIds ,
283+ chatId : "C123" ,
284+ kind : "card" ,
285+ messageCount : 2 ,
286+ } ) ,
287+ } ) ;
288+ const onDeliveryResult = vi . fn ( ) ;
289+
290+ const result = await lineOutboundAdapter . sendPayload ! ( {
291+ to : "line:group:C123" ,
292+ text : "" ,
293+ payload : {
294+ text : "" ,
295+ channelData : {
296+ line : {
297+ quickReplies : [ "Confirm" ] ,
298+ flexMessage : { altText : "Review" , contents : { type : "bubble" } } ,
299+ location : {
300+ title : "Meet here" ,
301+ address : "1 Main Street" ,
302+ latitude : 37.7 ,
303+ longitude : - 122.4 ,
304+ } ,
305+ } ,
306+ } ,
307+ } ,
308+ accountId : "default" ,
309+ cfg,
310+ onDeliveryResult,
311+ } ) ;
312+
313+ expect ( result . messageId ) . toBe ( "line-provider-first" ) ;
314+ expect ( result . receipt ?. platformMessageIds ) . toEqual ( providerMessageIds ) ;
315+ expect ( result . receipt ?. parts . map ( ( part ) => part . platformMessageId ) ) . toEqual ( providerMessageIds ) ;
316+ expect ( result . receipt ?. threadId ) . toBe ( "C123" ) ;
317+ expect ( onDeliveryResult ) . toHaveBeenCalledWith (
318+ expect . objectContaining ( {
319+ messageId : "line-provider-first" ,
320+ receipt : expect . objectContaining ( {
321+ platformMessageIds : providerMessageIds ,
322+ threadId : "C123" ,
323+ } ) ,
324+ } ) ,
325+ ) ;
326+ } ) ;
327+
198328 it ( "sends template message without dropping text" , async ( ) => {
199329 const { runtime, mocks } = createRuntime ( ) ;
200330 setLineRuntime ( runtime ) ;
0 commit comments