Skip to content

Commit d379494

Browse files
committed
fix(outbound): retain attachments when plural media URLs are blank
1 parent e4bf242 commit d379494

4 files changed

Lines changed: 104 additions & 67 deletions

File tree

‎extensions/discord/src/monitor/native-command-reply.test.ts‎

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -155,44 +155,59 @@ describe("deliverDiscordInteractionReply", () => {
155155
expect(interaction.followUp).not.toHaveBeenCalled();
156156
});
157157

158-
it("sends the detected WebP media type across a real interaction multipart request", async () => {
159-
const loopback = await createDiscordLoopbackRest();
160-
loadWebMediaMock.mockResolvedValue({
161-
buffer: Buffer.from("webp"),
162-
fileName: "sticker.webp",
163-
contentType: "image/webp",
164-
kind: "image",
165-
});
166-
const interaction = {
167-
reply: vi.fn(async (data: unknown) =>
168-
loopback.rest.post("/interactions/123/token/callback", {
169-
body: { type: 4, data },
170-
}),
171-
),
172-
followUp: vi.fn(),
173-
};
174-
175-
try {
176-
await deliverDiscordInteractionReply({
177-
interaction: interaction as never,
178-
payload: {
179-
text: "sticker",
180-
mediaUrls: ["file:///tmp/sticker.webp"],
181-
},
182-
textLimit: 2000,
183-
preferFollowUp: false,
184-
chunkMode: "length",
158+
it.each([
159+
{
160+
name: "plural media URLs",
161+
media: { mediaUrls: ["file:///tmp/sticker.webp"] },
162+
},
163+
{
164+
name: "singular media URL after blank plural URLs",
165+
media: { mediaUrls: [" "], mediaUrl: "file:///tmp/sticker.webp" },
166+
},
167+
])(
168+
"sends detected WebP media across a real interaction multipart request ($name)",
169+
async ({ media }) => {
170+
const loopback = await createDiscordLoopbackRest();
171+
loadWebMediaMock.mockResolvedValue({
172+
buffer: Buffer.from("webp"),
173+
fileName: "sticker.webp",
174+
contentType: "image/webp",
175+
kind: "image",
185176
});
177+
const interaction = {
178+
reply: vi.fn(async (data: unknown) =>
179+
loopback.rest.post("/interactions/123/token/callback", {
180+
body: { type: 4, data },
181+
}),
182+
),
183+
followUp: vi.fn(),
184+
};
186185

187-
const upload = loopback.requests.find((request) => request.method === "POST");
188-
expect(upload?.path).toContain("/interactions/123/token/callback");
189-
expect(upload?.contentType).toMatch(/^multipart\/form-data; boundary=/);
190-
expect(upload?.body).toContain('name="files[0]"; filename="sticker.webp"');
191-
expect(upload?.body).toContain("Content-Type: image/webp");
192-
} finally {
193-
await loopback.close();
194-
}
195-
});
186+
try {
187+
const payload = { text: "sticker", ...media };
188+
expect(hasRenderableReplyPayload(payload)).toBe(true);
189+
190+
await deliverDiscordInteractionReply({
191+
interaction: interaction as never,
192+
payload,
193+
textLimit: 2000,
194+
preferFollowUp: false,
195+
chunkMode: "length",
196+
});
197+
198+
expect(loadWebMediaMock).toHaveBeenCalledWith("file:///tmp/sticker.webp", {
199+
localRoots: undefined,
200+
});
201+
const upload = loopback.requests.find((request) => request.method === "POST");
202+
expect(upload?.path).toContain("/interactions/123/token/callback");
203+
expect(upload?.contentType).toMatch(/^multipart\/form-data; boundary=/);
204+
expect(upload?.body).toContain('name="files[0]"; filename="sticker.webp"');
205+
expect(upload?.body).toContain("Content-Type: image/webp");
206+
} finally {
207+
await loopback.close();
208+
}
209+
},
210+
);
196211
});
197212

198213
describe("settleDiscordInteractionWithoutVisibleReply", () => {

‎extensions/feishu/src/reply-dispatcher.test.ts‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,20 +2517,23 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
25172517
expect(fallbackText).not.toContain(mediaPath);
25182518
});
25192519

2520-
it("falls back to legacy mediaUrl when mediaUrls is an empty array", async () => {
2521-
useNonStreamingAutoAccount();
2522-
const { options } = createDispatcherHarness();
2523-
await options.deliver(
2524-
{ text: "caption", mediaUrl: "https://example.com/a.png", mediaUrls: [] },
2525-
{ kind: "final" },
2526-
);
2520+
it.each([{ mediaUrls: [] }, { mediaUrls: [" "] }])(
2521+
"falls back to legacy mediaUrl when mediaUrls has no usable entries",
2522+
async ({ mediaUrls }) => {
2523+
useNonStreamingAutoAccount();
2524+
const { options } = createDispatcherHarness();
2525+
await options.deliver(
2526+
{ text: "caption", mediaUrl: "https://example.com/a.png", mediaUrls },
2527+
{ kind: "final" },
2528+
);
25272529

2528-
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(1);
2529-
expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
2530-
expectMockArgFields(sendMediaFeishuMock, "media send params", {
2531-
mediaUrl: "https://example.com/a.png",
2532-
});
2533-
});
2530+
expect(sendMessageFeishuMock).toHaveBeenCalledTimes(1);
2531+
expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1);
2532+
expectMockArgFields(sendMediaFeishuMock, "media send params", {
2533+
mediaUrl: "https://example.com/a.png",
2534+
});
2535+
},
2536+
);
25342537

25352538
it("sends attachments after streaming final markdown replies", async () => {
25362539
const { options } = createDispatcherHarness({

‎src/infra/outbound/reply-payload-parts.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ export function resolveOutboundMediaUrls(payload: {
2323
mediaUrls?: string[];
2424
mediaUrl?: string;
2525
}): string[] {
26-
if (payload.mediaUrls?.length) {
26+
if (payload.mediaUrls?.some((mediaUrl) => mediaUrl.trim())) {
2727
return payload.mediaUrls;
2828
}
29-
if (payload.mediaUrl) {
30-
return [payload.mediaUrl];
31-
}
32-
return [];
29+
return payload.mediaUrl ? [payload.mediaUrl] : [];
3330
}
3431

3532
/** Count outbound media items after legacy single-media fallback normalization. */

‎src/plugin-sdk/reply-payload.test.ts‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,28 @@ describe("resolveOutboundMediaUrls", () => {
600600
},
601601
expected: ["https://example.com/legacy.png"],
602602
},
603+
{
604+
name: "falls back to the legacy single-media field when plural entries are blank",
605+
payload: {
606+
mediaUrls: [" "],
607+
mediaUrl: "https://example.com/legacy.png",
608+
},
609+
expected: ["https://example.com/legacy.png"],
610+
},
611+
{
612+
name: "preserves raw plural entries and duplicates when one attachment is valid",
613+
payload: {
614+
mediaUrls: [" ", " https://example.com/a.png ", " https://example.com/a.png "],
615+
mediaUrl: "https://example.com/legacy.png",
616+
},
617+
expected: [" ", " https://example.com/a.png ", " https://example.com/a.png "],
618+
},
603619
])("$name", ({ payload, expected }) => {
604-
expect(resolveOutboundMediaUrls(payload)).toEqual(expected);
620+
const mediaUrls = resolveOutboundMediaUrls(payload);
621+
expect(mediaUrls).toEqual(expected);
622+
if (payload.mediaUrls?.some((mediaUrl) => mediaUrl.trim())) {
623+
expect(mediaUrls).toBe(payload.mediaUrls);
624+
}
605625
});
606626
});
607627

@@ -869,24 +889,26 @@ describe("deliverTextOrMediaReply", () => {
869889
expect(sendMedia).not.toHaveBeenCalled();
870890
});
871891

872-
it("ignores blank media urls before sending", async () => {
892+
it.each([
893+
{
894+
name: "mixed plural entries",
895+
payload: { text: "hello", mediaUrls: [" ", " https://a "] },
896+
},
897+
{
898+
name: "blank plural entries with a valid single attachment",
899+
payload: { text: "hello", mediaUrls: [" "], mediaUrl: " https://a " },
900+
},
901+
])("delivers the valid attachment from $name", async ({ payload }) => {
873902
const sendMedia = vi.fn(async () => undefined);
874903
const sendText = vi.fn(async () => undefined);
875904

876905
await expect(
877-
deliverTextOrMediaReply({
878-
payload: { text: "hello", mediaUrls: [" ", " https://a "] },
879-
text: "hello",
880-
sendText,
881-
sendMedia,
882-
}),
906+
deliverTextOrMediaReply({ payload, text: "hello", sendText, sendMedia }),
883907
).resolves.toBe("media");
884908

885-
expect(sendMedia).toHaveBeenCalledTimes(1);
886-
expect(sendMedia).toHaveBeenCalledWith({
887-
mediaUrl: "https://a",
888-
caption: "hello",
889-
});
909+
expect(sendMedia).toHaveBeenCalledOnce();
910+
expect(sendMedia).toHaveBeenCalledWith({ mediaUrl: "https://a", caption: "hello" });
911+
expect(sendText).not.toHaveBeenCalled();
890912
});
891913
});
892914

0 commit comments

Comments
 (0)