Skip to content

Commit 67abd59

Browse files
author
Peter Steinberger
committed
fix(feishu): unify outbound delivery lifecycle
1 parent b521ce6 commit 67abd59

2 files changed

Lines changed: 392 additions & 126 deletions

File tree

‎extensions/feishu/src/outbound.test.ts‎

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,21 @@ describe("feishuOutbound.sendText local-image auto-convert", () => {
310310
adapter,
311311
proofs: {
312312
text: async () => {
313+
const onDeliveryResult = vi.fn();
313314
const result = await adapterSendText({
314315
cfg: emptyConfig,
315316
to: "chat:chat-1",
316317
text: "hello",
317318
accountId: "default",
319+
onDeliveryResult,
318320
});
319321
expect(sendMessageCall()?.to).toBe("chat:chat-1");
320322
expect(sendMessageCall()?.text).toBe("hello");
321323
expect(sendMessageCall()?.accountId).toBe("default");
322324
expect(result.receipt.platformMessageIds).toEqual(["feishu-text-1"]);
325+
expect(onDeliveryResult.mock.calls[0]?.[0]?.receipt.platformMessageIds).toEqual([
326+
"feishu-text-1",
327+
]);
323328
},
324329
media: async () => {
325330
const onDeliveryResult = vi.fn();
@@ -567,6 +572,29 @@ describe("feishuOutbound.sendText local-image auto-convert", () => {
567572
}
568573
});
569574

575+
it("does not send fallback text when accepted local-image progress cannot be persisted", async () => {
576+
const { dir, file } = await createTmpImage();
577+
const onDeliveryResult = vi.fn().mockRejectedValueOnce(new Error("progress write failed"));
578+
579+
try {
580+
await expect(
581+
sendText({
582+
cfg: emptyConfig,
583+
to: "chat_1",
584+
text: file,
585+
accountId: "main",
586+
mediaLocalRoots: [dir],
587+
onDeliveryResult,
588+
}),
589+
).rejects.toThrow("progress write failed");
590+
expect(sendMediaFeishuMock).toHaveBeenCalledOnce();
591+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
592+
expect(onDeliveryResult).toHaveBeenCalledOnce();
593+
} finally {
594+
await fs.rm(dir, { recursive: true, force: true });
595+
}
596+
});
597+
570598
it("uses markdown cards when renderMode=card", async () => {
571599
const result = await sendText({
572600
cfg: cardRenderConfig,
@@ -651,6 +679,52 @@ describe("feishuOutbound.sendPayload native cards", () => {
651679
return { dir, file };
652680
}
653681

682+
it("records delegated post subchunks once without duplicating parent receipts", async () => {
683+
sendMessageFeishuMock.mockImplementation(async () => ({
684+
messageId: `chunk_${sendMessageFeishuMock.mock.calls.length}`,
685+
}));
686+
const onDeliveryResult = vi.fn();
687+
const text = Array.from({ length: 2_200 }, () => "a").join("\n");
688+
689+
await feishuOutbound.sendPayload?.({
690+
cfg: emptyConfig,
691+
to: "chat_1",
692+
text,
693+
accountId: "main",
694+
payload: { text },
695+
onDeliveryResult,
696+
});
697+
698+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
699+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual(
700+
sendMessageFeishuMock.mock.calls.map((_call, index) => `chunk_${index + 1}`),
701+
);
702+
});
703+
704+
it("records separate fallback media and each expanded text chunk once", async () => {
705+
sendMessageFeishuMock.mockImplementation(async () => ({
706+
messageId: `chunk_${sendMessageFeishuMock.mock.calls.length}`,
707+
}));
708+
const onDeliveryResult = vi.fn();
709+
const text = Array.from({ length: 2_200 }, () => "a").join("\n");
710+
711+
await feishuOutbound.sendPayload?.({
712+
cfg: emptyConfig,
713+
to: "chat_1",
714+
text,
715+
accountId: "main",
716+
payload: { text, mediaUrl: "https://example.com/image.png" },
717+
onDeliveryResult,
718+
});
719+
720+
expect(sendMediaFeishuMock).toHaveBeenCalledOnce();
721+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
722+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual([
723+
"media_msg",
724+
...sendMessageFeishuMock.mock.calls.map((_call, index) => `chunk_${index + 1}`),
725+
]);
726+
});
727+
654728
it("renders presentation-only payloads into Feishu channelData cards for core delivery", async () => {
655729
const presentation: MessagePresentation = {
656730
title: "Approval",
@@ -2315,6 +2389,82 @@ describe("feishuOutbound.sendText replyToId forwarding", () => {
23152389
}
23162390
});
23172391

2392+
it("keeps explicit first-mode replies sticky across expanded post-md chunks", async () => {
2393+
await sendText({
2394+
cfg: emptyConfig,
2395+
to: "chat_1",
2396+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2397+
replyToId: "om_explicit_reply",
2398+
replyToIdSource: "explicit",
2399+
replyToMode: "first",
2400+
accountId: "main",
2401+
});
2402+
2403+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
2404+
for (const [params] of sendMessageFeishuMock.mock.calls) {
2405+
expect(params.replyToMessageId).toBe("om_explicit_reply");
2406+
}
2407+
});
2408+
2409+
it("records each accepted expanded text chunk before the next send", async () => {
2410+
sendMessageFeishuMock.mockImplementation(async () => ({
2411+
messageId: `chunk_${sendMessageFeishuMock.mock.calls.length}`,
2412+
}));
2413+
const onDeliveryResult = vi.fn();
2414+
2415+
await sendText({
2416+
cfg: emptyConfig,
2417+
to: "chat_1",
2418+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2419+
accountId: "main",
2420+
onDeliveryResult,
2421+
});
2422+
2423+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
2424+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual(
2425+
sendMessageFeishuMock.mock.calls.map((_call, index) => `chunk_${index + 1}`),
2426+
);
2427+
});
2428+
2429+
it("preserves the first accepted text chunk when the following send fails", async () => {
2430+
sendMessageFeishuMock
2431+
.mockResolvedValueOnce({ messageId: "accepted_chunk" })
2432+
.mockRejectedValueOnce(new Error("second chunk failed"));
2433+
const onDeliveryResult = vi.fn();
2434+
2435+
await expect(
2436+
sendText({
2437+
cfg: emptyConfig,
2438+
to: "chat_1",
2439+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2440+
accountId: "main",
2441+
onDeliveryResult,
2442+
}),
2443+
).rejects.toThrow("second chunk failed");
2444+
2445+
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(2);
2446+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual([
2447+
"accepted_chunk",
2448+
]);
2449+
});
2450+
2451+
it("stops text fanout immediately when accepted delivery cannot be persisted", async () => {
2452+
const onDeliveryResult = vi.fn().mockRejectedValueOnce(new Error("progress write failed"));
2453+
2454+
await expect(
2455+
sendText({
2456+
cfg: emptyConfig,
2457+
to: "chat_1",
2458+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2459+
accountId: "main",
2460+
onDeliveryResult,
2461+
}),
2462+
).rejects.toThrow("progress write failed");
2463+
2464+
expect(sendMessageFeishuMock).toHaveBeenCalledOnce();
2465+
expect(onDeliveryResult).toHaveBeenCalledOnce();
2466+
});
2467+
23182468
it("re-chunks expanded post-md text at the selected account limit", async () => {
23192469
await sendText({
23202470
cfg: {
@@ -2380,6 +2530,26 @@ describe("feishuOutbound.sendMedia replyToId forwarding", () => {
23802530
resetOutboundMocks();
23812531
});
23822532

2533+
it("sends and records text-only media requests exactly once", async () => {
2534+
const onDeliveryResult = vi.fn();
2535+
2536+
const result = await feishuOutbound.sendMedia?.({
2537+
cfg: emptyConfig,
2538+
to: "chat_1",
2539+
text: "text without an attachment",
2540+
replyToId: "om_reply_target",
2541+
accountId: "main",
2542+
onDeliveryResult,
2543+
});
2544+
2545+
expect(sendMessageFeishuMock).toHaveBeenCalledOnce();
2546+
expect(sendMediaFeishuMock).not.toHaveBeenCalled();
2547+
expect(onDeliveryResult.mock.calls.map(([delivery]) => delivery.messageId)).toEqual([
2548+
"text_msg",
2549+
]);
2550+
expectFeishuResult(result, "text_msg");
2551+
});
2552+
23832553
it("forwards replyToId to sendMediaFeishu", async () => {
23842554
await feishuOutbound.sendMedia?.({
23852555
cfg: emptyConfig,
@@ -2485,6 +2655,25 @@ describe("feishuOutbound.sendMedia replyToId forwarding", () => {
24852655
expect(sendMediaCall()?.replyToMessageId).toBe("om_reply_target");
24862656
});
24872657

2658+
it("keeps explicit first-mode targets sticky across every caption chunk and attachment", async () => {
2659+
await feishuOutbound.sendMedia?.({
2660+
cfg: emptyConfig,
2661+
to: "chat_1",
2662+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2663+
mediaUrl: "https://example.com/image.png",
2664+
replyToId: "om_explicit_reply",
2665+
replyToIdSource: "explicit",
2666+
replyToMode: "first",
2667+
accountId: "main",
2668+
});
2669+
2670+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
2671+
for (const [params] of sendMessageFeishuMock.mock.calls) {
2672+
expect(params.replyToMessageId).toBe("om_explicit_reply");
2673+
}
2674+
expect(sendMediaCall()?.replyToMessageId).toBe("om_explicit_reply");
2675+
});
2676+
24882677
it("keeps native topic roots sticky across captions, attachments, and fallback", async () => {
24892678
sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed"));
24902679

@@ -2651,6 +2840,71 @@ describe("feishuOutbound.sendMedia replyToId forwarding", () => {
26512840
expectFeishuResult(result, "fallback_msg");
26522841
});
26532842

2843+
it("records every accepted caption chunk before recording its attachment", async () => {
2844+
sendMessageFeishuMock.mockImplementation(async () => ({
2845+
messageId: `caption_${sendMessageFeishuMock.mock.calls.length}`,
2846+
}));
2847+
const onDeliveryResult = vi.fn();
2848+
2849+
await feishuOutbound.sendMedia?.({
2850+
cfg: emptyConfig,
2851+
to: "chat_1",
2852+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2853+
mediaUrl: "https://example.com/image.png",
2854+
accountId: "main",
2855+
onDeliveryResult,
2856+
});
2857+
2858+
expect(sendMessageFeishuMock.mock.calls.length).toBeGreaterThan(1);
2859+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual([
2860+
...sendMessageFeishuMock.mock.calls.map((_call, index) => `caption_${index + 1}`),
2861+
"media_msg",
2862+
]);
2863+
});
2864+
2865+
it("preserves accepted caption chunks when a later chunk fails before media", async () => {
2866+
sendMessageFeishuMock
2867+
.mockResolvedValueOnce({ messageId: "accepted_caption" })
2868+
.mockRejectedValueOnce(new Error("second caption failed"));
2869+
const onDeliveryResult = vi.fn();
2870+
2871+
await expect(
2872+
feishuOutbound.sendMedia?.({
2873+
cfg: emptyConfig,
2874+
to: "chat_1",
2875+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2876+
mediaUrl: "https://example.com/image.png",
2877+
accountId: "main",
2878+
onDeliveryResult,
2879+
}),
2880+
).rejects.toThrow("second caption failed");
2881+
2882+
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(2);
2883+
expect(sendMediaFeishuMock).not.toHaveBeenCalled();
2884+
expect(onDeliveryResult.mock.calls.map(([result]) => result.messageId)).toEqual([
2885+
"accepted_caption",
2886+
]);
2887+
});
2888+
2889+
it("stops before later caption chunks and media when delivery persistence fails", async () => {
2890+
const onDeliveryResult = vi.fn().mockRejectedValueOnce(new Error("progress write failed"));
2891+
2892+
await expect(
2893+
feishuOutbound.sendMedia?.({
2894+
cfg: emptyConfig,
2895+
to: "chat_1",
2896+
text: Array.from({ length: 2_200 }, () => "a").join("\n"),
2897+
mediaUrl: "https://example.com/image.png",
2898+
accountId: "main",
2899+
onDeliveryResult,
2900+
}),
2901+
).rejects.toThrow("progress write failed");
2902+
2903+
expect(sendMessageFeishuMock).toHaveBeenCalledOnce();
2904+
expect(sendMediaFeishuMock).not.toHaveBeenCalled();
2905+
expect(onDeliveryResult).toHaveBeenCalledOnce();
2906+
});
2907+
26542908
it("does not send fallback text after an accepted media send loses its receipt", async () => {
26552909
const acceptedError = createChannelPartialDeliveryError(
26562910
new Error("Feishu image send failed: no message_id returned"),

0 commit comments

Comments
 (0)