|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +const sendMediaFeishuMock = vi.hoisted(() => vi.fn()); |
| 7 | +const sendMessageFeishuMock = vi.hoisted(() => vi.fn()); |
| 8 | + |
| 9 | +vi.mock("./media.js", () => ({ |
| 10 | + sendMediaFeishu: sendMediaFeishuMock, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("./send.js", () => ({ |
| 14 | + sendMessageFeishu: sendMessageFeishuMock, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("./runtime.js", () => ({ |
| 18 | + getFeishuRuntime: () => ({ |
| 19 | + channel: { |
| 20 | + text: { |
| 21 | + chunkMarkdownText: (text: string) => [text], |
| 22 | + }, |
| 23 | + }, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +import { feishuOutbound } from "./outbound.js"; |
| 28 | +const sendText = feishuOutbound.sendText!; |
| 29 | + |
| 30 | +describe("feishuOutbound.sendText local-image auto-convert", () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.clearAllMocks(); |
| 33 | + sendMessageFeishuMock.mockResolvedValue({ messageId: "text_msg" }); |
| 34 | + sendMediaFeishuMock.mockResolvedValue({ messageId: "media_msg" }); |
| 35 | + }); |
| 36 | + |
| 37 | + async function createTmpImage(ext = ".png"): Promise<{ dir: string; file: string }> { |
| 38 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-feishu-outbound-")); |
| 39 | + const file = path.join(dir, `sample${ext}`); |
| 40 | + await fs.writeFile(file, "image-data"); |
| 41 | + return { dir, file }; |
| 42 | + } |
| 43 | + |
| 44 | + it("sends an absolute existing local image path as media", async () => { |
| 45 | + const { dir, file } = await createTmpImage(); |
| 46 | + try { |
| 47 | + const result = await sendText({ |
| 48 | + cfg: {} as any, |
| 49 | + to: "chat_1", |
| 50 | + text: file, |
| 51 | + accountId: "main", |
| 52 | + }); |
| 53 | + |
| 54 | + expect(sendMediaFeishuMock).toHaveBeenCalledWith( |
| 55 | + expect.objectContaining({ |
| 56 | + to: "chat_1", |
| 57 | + mediaUrl: file, |
| 58 | + accountId: "main", |
| 59 | + }), |
| 60 | + ); |
| 61 | + expect(sendMessageFeishuMock).not.toHaveBeenCalled(); |
| 62 | + expect(result).toEqual( |
| 63 | + expect.objectContaining({ channel: "feishu", messageId: "media_msg" }), |
| 64 | + ); |
| 65 | + } finally { |
| 66 | + await fs.rm(dir, { recursive: true, force: true }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps non-path text on the text-send path", async () => { |
| 71 | + await sendText({ |
| 72 | + cfg: {} as any, |
| 73 | + to: "chat_1", |
| 74 | + text: "please upload /tmp/example.png", |
| 75 | + accountId: "main", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(sendMediaFeishuMock).not.toHaveBeenCalled(); |
| 79 | + expect(sendMessageFeishuMock).toHaveBeenCalledWith( |
| 80 | + expect.objectContaining({ |
| 81 | + to: "chat_1", |
| 82 | + text: "please upload /tmp/example.png", |
| 83 | + accountId: "main", |
| 84 | + }), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("falls back to plain text if local-image media send fails", async () => { |
| 89 | + const { dir, file } = await createTmpImage(); |
| 90 | + sendMediaFeishuMock.mockRejectedValueOnce(new Error("upload failed")); |
| 91 | + try { |
| 92 | + await sendText({ |
| 93 | + cfg: {} as any, |
| 94 | + to: "chat_1", |
| 95 | + text: file, |
| 96 | + accountId: "main", |
| 97 | + }); |
| 98 | + |
| 99 | + expect(sendMediaFeishuMock).toHaveBeenCalledTimes(1); |
| 100 | + expect(sendMessageFeishuMock).toHaveBeenCalledWith( |
| 101 | + expect.objectContaining({ |
| 102 | + to: "chat_1", |
| 103 | + text: file, |
| 104 | + accountId: "main", |
| 105 | + }), |
| 106 | + ); |
| 107 | + } finally { |
| 108 | + await fs.rm(dir, { recursive: true, force: true }); |
| 109 | + } |
| 110 | + }); |
| 111 | +}); |
0 commit comments