mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 07:27:40 +00:00
feat(discord): download attachments from forwarded messages (#17049)
Co-authored-by: Shadow <shadow@openclaw.ai>
This commit is contained in:
@@ -1,9 +1,26 @@
|
||||
import type { Message } from "@buape/carbon";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveDiscordMessageChannelId } from "./message-utils.js";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const fetchRemoteMedia = vi.fn();
|
||||
const saveMediaBuffer = vi.fn();
|
||||
|
||||
vi.mock("../../media/fetch.js", () => ({
|
||||
fetchRemoteMedia: (...args: unknown[]) => fetchRemoteMedia(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../media/store.js", () => ({
|
||||
saveMediaBuffer: (...args: unknown[]) => saveMediaBuffer(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../globals.js", () => ({
|
||||
logVerbose: () => {},
|
||||
}));
|
||||
|
||||
const { resolveDiscordMessageChannelId, resolveForwardedMediaList } =
|
||||
await import("./message-utils.js");
|
||||
|
||||
function asMessage(payload: Record<string, unknown>): Message {
|
||||
return payload as unknown as Message;
|
||||
return payload as Message;
|
||||
}
|
||||
|
||||
describe("resolveDiscordMessageChannelId", () => {
|
||||
@@ -36,3 +53,72 @@ describe("resolveDiscordMessageChannelId", () => {
|
||||
expect(channelId).toBe("789");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveForwardedMediaList", () => {
|
||||
beforeEach(() => {
|
||||
fetchRemoteMedia.mockReset();
|
||||
saveMediaBuffer.mockReset();
|
||||
});
|
||||
|
||||
it("downloads forwarded attachments", async () => {
|
||||
const attachment = {
|
||||
id: "att-1",
|
||||
url: "https://cdn.discordapp.com/attachments/1/image.png",
|
||||
filename: "image.png",
|
||||
content_type: "image/png",
|
||||
};
|
||||
fetchRemoteMedia.mockResolvedValueOnce({
|
||||
buffer: Buffer.from("image"),
|
||||
contentType: "image/png",
|
||||
});
|
||||
saveMediaBuffer.mockResolvedValueOnce({
|
||||
path: "/tmp/image.png",
|
||||
contentType: "image/png",
|
||||
});
|
||||
|
||||
const result = await resolveForwardedMediaList(
|
||||
asMessage({
|
||||
rawData: {
|
||||
message_snapshots: [{ message: { attachments: [attachment] } }],
|
||||
},
|
||||
}),
|
||||
512,
|
||||
);
|
||||
|
||||
expect(fetchRemoteMedia).toHaveBeenCalledTimes(1);
|
||||
expect(fetchRemoteMedia).toHaveBeenCalledWith({
|
||||
url: attachment.url,
|
||||
filePathHint: attachment.filename,
|
||||
});
|
||||
expect(saveMediaBuffer).toHaveBeenCalledTimes(1);
|
||||
expect(saveMediaBuffer).toHaveBeenCalledWith(expect.any(Buffer), "image/png", "inbound", 512);
|
||||
expect(result).toEqual([
|
||||
{
|
||||
path: "/tmp/image.png",
|
||||
contentType: "image/png",
|
||||
placeholder: "<media:image>",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns empty when no snapshots are present", async () => {
|
||||
const result = await resolveForwardedMediaList(asMessage({}), 512);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
expect(fetchRemoteMedia).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips snapshots without attachments", async () => {
|
||||
const result = await resolveForwardedMediaList(
|
||||
asMessage({
|
||||
rawData: {
|
||||
message_snapshots: [{ message: { content: "hello" } }],
|
||||
},
|
||||
}),
|
||||
512,
|
||||
);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
expect(fetchRemoteMedia).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user