Skip to content

Commit 247ec36

Browse files
author
Peter Steinberger
committed
fix(line): unify legacy text delivery receipts
1 parent db7c3ee commit 247ec36

3 files changed

Lines changed: 168 additions & 29 deletions

File tree

‎extensions/line/src/channel.sendPayload.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ beforeEach(() => {
5151
});
5252

5353
afterEach(() => {
54+
vi.unstubAllGlobals();
5455
vi.useRealTimers();
5556
});
5657

@@ -137,6 +138,79 @@ function createRuntime(): { runtime: PluginRuntime; mocks: LineRuntimeMocks } {
137138
}
138139

139140
describe("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);

‎extensions/line/src/outbound.ts‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,35 +275,13 @@ export const lineOutboundAdapter: NonNullable<ChannelPlugin<ResolvedLineAccount>
275275
},
276276
...createAttachedChannelResultAdapter({
277277
channel: "line",
278-
sendText: async ({ cfg, to, text, accountId }) => {
279-
const outboundRuntime = await loadLineOutboundRuntime();
280-
const sendText = outboundRuntime.pushMessageLine;
281-
const sendFlex = outboundRuntime.pushFlexMessage;
282-
const processed = outboundRuntime.processLineMessage(text);
283-
let result: LineSendResult;
284-
if (processed.text.trim()) {
285-
result = await sendText(to, processed.text, {
286-
verbose: false,
287-
cfg,
288-
accountId: accountId ?? undefined,
289-
});
290-
} else {
291-
result = {
292-
messageId: "processed",
293-
chatId: to,
294-
receipt: createLineSendReceipt({ messageId: "processed", chatId: to, kind: "card" }),
295-
};
296-
}
297-
for (const flexMsg of processed.flexMessages) {
298-
const flexContents = flexMsg.contents;
299-
await sendFlex(to, flexMsg.altText, flexContents, {
300-
verbose: false,
301-
cfg,
302-
accountId: accountId ?? undefined,
303-
});
304-
}
305-
return result;
306-
},
278+
// The payload owner records each physical send before the next fallible step;
279+
// bypassing it fabricates Flex-only ids and loses partial-delivery evidence.
280+
sendText: async (ctx) =>
281+
await lineOutboundAdapter.sendPayload!({
282+
...ctx,
283+
payload: { text: ctx.text },
284+
}),
307285
sendMedia: async ({ cfg, to, text, mediaUrl, accountId }) =>
308286
await (
309287
await loadLineOutboundRuntime()

‎extensions/line/src/send.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,37 @@ describe("LINE send helpers", () => {
560560
provider.mockResolvedValueOnce({ sentMessages: [] });
561561

562562
await expect(send()).rejects.toThrow(/sent message id/i);
563+
expect(recordChannelActivityMock).not.toHaveBeenCalled();
564+
},
565+
);
566+
567+
it.each([
568+
{
569+
label: "push",
570+
send: () =>
571+
sendModule.pushMessagesLine("U123", [{ type: "text", text: "Hello" }], {
572+
cfg: LINE_TEST_CFG,
573+
}),
574+
provider: pushMessageMock,
575+
},
576+
{
577+
label: "reply",
578+
send: () =>
579+
sendModule.sendMessageLine("U123", "Hello", {
580+
cfg: LINE_TEST_CFG,
581+
replyToken: "reply-token",
582+
}),
583+
provider: replyMessageMock,
584+
},
585+
])(
586+
"rejects a partially invalid $label provider receipt without recording delivery",
587+
async ({ send, provider }) => {
588+
provider.mockResolvedValueOnce({
589+
sentMessages: [{ id: "line-provider-delivered" }, { id: " " }],
590+
});
591+
592+
await expect(send()).rejects.toThrow(/sent message id/i);
593+
expect(recordChannelActivityMock).not.toHaveBeenCalled();
563594
},
564595
);
565596

0 commit comments

Comments
 (0)