|
1 | 1 | // Mattermost tests cover the action-to-REST send path over loopback. |
2 | 2 | import { createPluginRuntimeMock } from "openclaw/plugin-sdk/plugin-test-runtime"; |
3 | 3 | import { withServer } from "openclaw/plugin-sdk/test-env"; |
4 | | -import { describe, expect, it } from "vitest"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
5 | 5 | import { mattermostPlugin } from "./channel.js"; |
6 | 6 | import type { OpenClawConfig } from "./runtime-api.js"; |
7 | 7 | import { setMattermostRuntime } from "./runtime.js"; |
8 | 8 |
|
9 | 9 | const CHANNEL_ID = "aaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 10 | +const loadOutboundMediaFromUrl = vi.hoisted(() => vi.fn()); |
| 11 | + |
| 12 | +vi.mock("./mattermost/runtime-api.js", async () => ({ |
| 13 | + ...(await vi.importActual<typeof import("./mattermost/runtime-api.js")>( |
| 14 | + "./mattermost/runtime-api.js", |
| 15 | + )), |
| 16 | + loadOutboundMediaFromUrl, |
| 17 | +})); |
10 | 18 |
|
11 | 19 | describe("Mattermost send action loopback", () => { |
12 | 20 | it("sends text with blank attachment placeholders and rejects nonblank payloads", async () => { |
@@ -92,4 +100,66 @@ describe("Mattermost send action loopback", () => { |
92 | 100 | }, |
93 | 101 | ); |
94 | 102 | }); |
| 103 | + |
| 104 | + it("infers a MIME extension for unnamed uploads", async () => { |
| 105 | + const uploads: string[] = []; |
| 106 | + |
| 107 | + await withServer( |
| 108 | + (request, response) => { |
| 109 | + let body = ""; |
| 110 | + request.setEncoding("utf8"); |
| 111 | + request.on("data", (chunk) => { |
| 112 | + body += chunk; |
| 113 | + }); |
| 114 | + request.on("end", () => { |
| 115 | + if (request.url === "/api/v4/files") { |
| 116 | + uploads.push(body); |
| 117 | + response.writeHead(201, { "content-type": "application/json" }); |
| 118 | + response.end(JSON.stringify({ file_infos: [{ id: `file-${uploads.length}` }] })); |
| 119 | + return; |
| 120 | + } |
| 121 | + response.writeHead(201, { "content-type": "application/json" }); |
| 122 | + response.end(JSON.stringify({ id: "post-loopback", channel_id: CHANNEL_ID })); |
| 123 | + }); |
| 124 | + }, |
| 125 | + async (baseUrl) => { |
| 126 | + setMattermostRuntime(createPluginRuntimeMock()); |
| 127 | + loadOutboundMediaFromUrl.mockReset(); |
| 128 | + loadOutboundMediaFromUrl.mockResolvedValueOnce({ |
| 129 | + buffer: Buffer.from("unnamed-image"), |
| 130 | + contentType: "image/png", |
| 131 | + kind: "image", |
| 132 | + }); |
| 133 | + const cfg = { |
| 134 | + channels: { |
| 135 | + mattermost: { |
| 136 | + botToken: "loopback-fixture", |
| 137 | + baseUrl, |
| 138 | + network: { dangerouslyAllowPrivateNetwork: true }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } as OpenClawConfig; |
| 142 | + const handleAction = mattermostPlugin.actions?.handleAction; |
| 143 | + if (!handleAction) { |
| 144 | + throw new Error("Mattermost send action missing"); |
| 145 | + } |
| 146 | + |
| 147 | + await handleAction({ |
| 148 | + channel: "mattermost", |
| 149 | + action: "send", |
| 150 | + params: { |
| 151 | + to: `channel:${CHANNEL_ID}`, |
| 152 | + message: "loopback media proof", |
| 153 | + mediaUrl: "https://media.example.test/unnamed", |
| 154 | + }, |
| 155 | + cfg, |
| 156 | + accountId: "default", |
| 157 | + }); |
| 158 | + }, |
| 159 | + ); |
| 160 | + |
| 161 | + expect(uploads).toHaveLength(1); |
| 162 | + expect(uploads[0]).toContain('filename="upload.png"'); |
| 163 | + expect(uploads[0]).toContain("Content-Type: image/png"); |
| 164 | + }); |
95 | 165 | }); |
0 commit comments