|
2 | 2 | * Tests chat reply media handling for gateway message delivery. |
3 | 3 | */ |
4 | 4 | import fs from "node:fs/promises"; |
| 5 | +import http from "node:http"; |
| 6 | +import type { AddressInfo } from "node:net"; |
5 | 7 | import path from "node:path"; |
6 | 8 | import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
7 | 9 | import { consumePendingToolMediaIntoReply } from "../../agents/embedded-agent-subscribe.handlers.messages.replies.js"; |
8 | 10 | import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 11 | +import { createPinnedLookup } from "../../infra/net/ssrf.js"; |
9 | 12 | import { getAgentScopedMediaLocalRoots } from "../../media/local-roots.js"; |
| 13 | +import { setMediaStoreNetworkDepsForTest } from "../../media/store.test-support.js"; |
10 | 14 | import { |
11 | 15 | createOpenClawTestState, |
12 | 16 | type OpenClawTestState, |
@@ -47,6 +51,7 @@ describe("normalizeWebchatReplyMediaPathsForDisplay", () => { |
47 | 51 | }); |
48 | 52 |
|
49 | 53 | afterEach(async () => { |
| 54 | + setMediaStoreNetworkDepsForTest(); |
50 | 55 | await testState.cleanup(); |
51 | 56 | }); |
52 | 57 |
|
@@ -182,35 +187,84 @@ describe("normalizeWebchatReplyMediaPathsForDisplay", () => { |
182 | 187 | ); |
183 | 188 | }); |
184 | 189 |
|
185 | | - it("preserves a visible warning when mixed media normalization rejects one item", async () => { |
| 190 | + it("preserves ordered document and image metadata beside one rejected SVG", async () => { |
186 | 191 | const { workspaceDir, cfg } = createMediaTestContext({ allowRead: true }); |
187 | | - const imagePath = path.join(workspaceDir, "image.png"); |
188 | | - const unsupportedPath = path.join(workspaceDir, "script.js"); |
| 192 | + const documentPath = path.join(workspaceDir, "artifact.json"); |
| 193 | + const localImagePath = path.join(workspaceDir, "local.png"); |
| 194 | + const unsupportedPath = path.join(workspaceDir, "vector.svg"); |
189 | 195 | await fs.mkdir(workspaceDir, { recursive: true }); |
190 | | - await fs.writeFile(imagePath, PNG_BYTES); |
191 | | - await fs.writeFile(unsupportedPath, "export default true;\n"); |
| 196 | + await fs.writeFile(documentPath, '{"ready":true}\n'); |
| 197 | + await fs.writeFile(localImagePath, PNG_BYTES); |
| 198 | + await fs.writeFile(unsupportedPath, "<svg><script/></svg>\n"); |
| 199 | + const upstream = http.createServer((req, res) => { |
| 200 | + expect(req.url).toBe("/remote.png?sig=secret"); |
| 201 | + res.statusCode = 200; |
| 202 | + res.setHeader("content-type", "image/png"); |
| 203 | + res.end(PNG_BYTES); |
| 204 | + }); |
| 205 | + await new Promise<void>((resolve) => { |
| 206 | + upstream.listen(0, "127.0.0.1", resolve); |
| 207 | + }); |
| 208 | + const address = upstream.address() as AddressInfo; |
| 209 | + setMediaStoreNetworkDepsForTest({ |
| 210 | + resolvePinnedHostname: async (hostname) => ({ |
| 211 | + hostname, |
| 212 | + addresses: ["127.0.0.1"], |
| 213 | + lookup: createPinnedLookup({ hostname, addresses: ["127.0.0.1"] }), |
| 214 | + }), |
| 215 | + }); |
192 | 216 |
|
193 | | - const payload = await normalizeReplyMedia({ |
194 | | - cfg, |
195 | | - payloads: [ |
| 217 | + try { |
| 218 | + const remoteImageUrl = `http://127.0.0.1:${address.port}/remote.png?sig=secret`; |
| 219 | + const payload = await normalizeReplyMedia({ |
| 220 | + cfg, |
| 221 | + payloads: [ |
| 222 | + { |
| 223 | + text: "Artifacts ready", |
| 224 | + mediaUrls: [documentPath, remoteImageUrl, localImagePath, unsupportedPath], |
| 225 | + }, |
| 226 | + ], |
| 227 | + }); |
| 228 | + |
| 229 | + expect(payload).toMatchObject({ |
| 230 | + text: "Artifacts ready\n⚠️ Media failed. Try sending a smaller supported file or a different format.", |
| 231 | + attachments: [ |
| 232 | + expect.objectContaining({ name: "artifact.json", mimeType: "application/json" }), |
| 233 | + {}, |
| 234 | + expect.objectContaining({ name: "local.png", mimeType: "image/png" }), |
| 235 | + ], |
| 236 | + }); |
| 237 | + expect(payload?.mediaUrls).toHaveLength(3); |
| 238 | + const content = await buildAssistantDisplayContentFromReplyPayloads({ |
| 239 | + sessionKey: TEST_SESSION_KEY, |
| 240 | + agentId: "main", |
| 241 | + payloads: payload ? [payload] : [], |
| 242 | + managedMediaLocalRoots: getAgentScopedMediaLocalRoots(cfg, "main"), |
| 243 | + }); |
| 244 | + expect(content).toEqual([ |
196 | 245 | { |
197 | | - text: "Artifacts ready", |
198 | | - mediaUrls: [imagePath, unsupportedPath], |
| 246 | + type: "text", |
| 247 | + text: "Artifacts ready\n⚠️ Media failed. Try sending a smaller supported file or a different format.", |
199 | 248 | }, |
200 | | - ], |
201 | | - }); |
202 | | - |
203 | | - expect(payload).toMatchObject({ |
204 | | - text: "Artifacts ready\n⚠️ Media failed. Try sending a smaller supported file or a different format.", |
205 | | - mediaUrls: [expect.stringMatching(/\.png$/u)], |
206 | | - attachments: [ |
207 | 249 | expect.objectContaining({ |
208 | | - name: "image.png", |
209 | | - mimeType: "image/png", |
210 | | - trustedLocalMedia: true, |
| 250 | + type: "attachment", |
| 251 | + attachment: expect.objectContaining({ |
| 252 | + label: "artifact.json", |
| 253 | + mimeType: "application/json", |
| 254 | + }), |
211 | 255 | }), |
212 | | - ], |
213 | | - }); |
| 256 | + expect.objectContaining({ type: "image", alt: "remote.png", mimeType: "image/png" }), |
| 257 | + expect.objectContaining({ type: "image", alt: "local.png", mimeType: "image/png" }), |
| 258 | + ]); |
| 259 | + const serialized = JSON.stringify(content); |
| 260 | + expect(serialized).not.toContain("vector.svg"); |
| 261 | + expect(serialized.match(/Media failed/gu)).toHaveLength(1); |
| 262 | + expect(serialized).not.toContain("sig=secret"); |
| 263 | + } finally { |
| 264 | + await new Promise<void>((resolve, reject) => { |
| 265 | + upstream.close((error) => (error ? reject(error) : resolve())); |
| 266 | + }); |
| 267 | + } |
214 | 268 | }); |
215 | 269 |
|
216 | 270 | it("preserves rejection warnings and metadata beside trusted local audio", async () => { |
|
0 commit comments