Files
openclaw/extensions/msteams/src/monitor-handler/inbound-media.test.ts
Ted-developer dd080b6fb0 fix(msteams): download DM inline images via Graph API (#52212)
Fix three bugs preventing inline image downloads in Teams 1:1 DM chats: wrong conversation ID format for Graph API, missing media URL extraction, and incorrect content type detection.

Fixes #24797

Thanks @Ted-developer
2026-04-02 22:14:02 -05:00

76 lines
2.6 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
vi.mock("../attachments.js", () => ({
downloadMSTeamsAttachments: vi.fn(async () => []),
downloadMSTeamsGraphMedia: vi.fn(async () => ({ media: [] })),
buildMSTeamsGraphMessageUrls: vi.fn(() => [
"https://graph.microsoft.com/v1.0/chats/c/messages/m",
]),
}));
import {
downloadMSTeamsAttachments,
downloadMSTeamsGraphMedia,
buildMSTeamsGraphMessageUrls,
} from "../attachments.js";
import { resolveMSTeamsInboundMedia } from "./inbound-media.js";
const baseParams = {
maxBytes: 1024 * 1024,
tokenProvider: { getAccessToken: vi.fn(async () => "token") },
conversationType: "personal",
conversationId: "19:user_bot@unq.gbl.spaces",
activity: { id: "msg-1", replyToId: undefined, channelData: {} },
log: { debug: vi.fn() },
};
describe("resolveMSTeamsInboundMedia graph fallback trigger", () => {
it("triggers Graph fallback when some attachments are text/html (some() behavior)", async () => {
vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
vi.mocked(downloadMSTeamsGraphMedia).mockResolvedValue({
media: [{ path: "/tmp/img.png", contentType: "image/png", placeholder: "[image]" }],
});
await resolveMSTeamsInboundMedia({
...baseParams,
attachments: [
{ contentType: "text/html", content: "<div><img src='x'/></div>" },
{ contentType: "image/png", contentUrl: "https://example.com/img.png" },
],
});
expect(buildMSTeamsGraphMessageUrls).toHaveBeenCalled();
expect(downloadMSTeamsGraphMedia).toHaveBeenCalled();
});
it("does NOT trigger Graph fallback when no attachments are text/html", async () => {
vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([]);
vi.mocked(downloadMSTeamsGraphMedia).mockClear();
vi.mocked(buildMSTeamsGraphMessageUrls).mockClear();
await resolveMSTeamsInboundMedia({
...baseParams,
attachments: [
{ contentType: "image/png", contentUrl: "https://example.com/img.png" },
{ contentType: "application/pdf", contentUrl: "https://example.com/doc.pdf" },
],
});
expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
});
it("does NOT trigger Graph fallback when direct download succeeds", async () => {
vi.mocked(downloadMSTeamsAttachments).mockResolvedValue([
{ path: "/tmp/img.png", contentType: "image/png", placeholder: "[image]" },
]);
vi.mocked(downloadMSTeamsGraphMedia).mockClear();
await resolveMSTeamsInboundMedia({
...baseParams,
attachments: [{ contentType: "text/html", content: "<div><img src='x'/></div>" }],
});
expect(downloadMSTeamsGraphMedia).not.toHaveBeenCalled();
});
});