|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { expect, it } from "vitest"; |
| 4 | +import { createChatFlowE2eSuite, installMockGateway } from "./chat-flow.test-support.ts"; |
| 5 | + |
| 6 | +const suite = createChatFlowE2eSuite(); |
| 7 | + |
| 8 | +suite.define(() => { |
| 9 | + it("allows tilde local media previews when the preview root home contains a literal $ pattern", async () => { |
| 10 | + const artifactDir = process.env.OPENCLAW_UI_E2E_ARTIFACT_DIR?.trim(); |
| 11 | + const context = await suite.newBrowserContext({ |
| 12 | + locale: "en-US", |
| 13 | + serviceWorkers: "block", |
| 14 | + viewport: { height: 900, width: 1280 }, |
| 15 | + }); |
| 16 | + const page = await context.newPage(); |
| 17 | + const source = "~/media/report-voice.mp3"; |
| 18 | + const requestedMediaUrls: URL[] = []; |
| 19 | + |
| 20 | + await page.route("**/__openclaw__/assistant-media?**", async (route) => { |
| 21 | + const url = new URL(route.request().url()); |
| 22 | + requestedMediaUrls.push(url); |
| 23 | + if (url.searchParams.get("meta") === "1") { |
| 24 | + await route.fulfill({ |
| 25 | + contentType: "application/json", |
| 26 | + body: JSON.stringify({ |
| 27 | + available: true, |
| 28 | + mediaTicket: "ticket-dollar-home", |
| 29 | + mediaTicketExpiresAt: new Date(Date.now() + 5 * 60_000).toISOString(), |
| 30 | + }), |
| 31 | + }); |
| 32 | + return; |
| 33 | + } |
| 34 | + await route.fulfill({ |
| 35 | + contentType: "audio/mpeg", |
| 36 | + body: Buffer.from("ID3\u0003\u0000\u0000\u0000\u0000\u0000\u0000"), |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + await installMockGateway(page, { |
| 41 | + localMediaPreviewRoots: ["/home/us$&r/media"], |
| 42 | + historyMessages: [ |
| 43 | + { |
| 44 | + id: "assistant-dollar-home-audio", |
| 45 | + role: "assistant", |
| 46 | + content: [ |
| 47 | + { type: "text", text: "Your recording" }, |
| 48 | + { |
| 49 | + type: "attachment", |
| 50 | + attachment: { |
| 51 | + kind: "audio", |
| 52 | + label: "report-voice.mp3", |
| 53 | + mimeType: "audio/mpeg", |
| 54 | + url: source, |
| 55 | + }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + timestamp: Date.now(), |
| 59 | + }, |
| 60 | + ], |
| 61 | + }); |
| 62 | + |
| 63 | + try { |
| 64 | + await page.goto(`${suite.server.baseUrl}chat`); |
| 65 | + const audio = page.locator(".chat-assistant-attachment-card audio"); |
| 66 | + await audio.waitFor({ state: "attached", timeout: 10_000 }); |
| 67 | + await expect.poll(() => requestedMediaUrls.length, { timeout: 10_000 }).toBe(2); |
| 68 | + expect(requestedMediaUrls[0]?.searchParams.get("meta")).toBe("1"); |
| 69 | + expect(requestedMediaUrls[0]?.searchParams.get("source")).toBe(source); |
| 70 | + expect(requestedMediaUrls[1]?.searchParams.get("mediaTicket")).toBe("ticket-dollar-home"); |
| 71 | + expect(requestedMediaUrls[1]?.searchParams.get("source")).toBe(source); |
| 72 | + expect(await page.getByText("Outside allowed folders").count()).toBe(0); |
| 73 | + if (artifactDir) { |
| 74 | + await mkdir(artifactDir, { recursive: true }); |
| 75 | + await page.screenshot({ |
| 76 | + fullPage: true, |
| 77 | + path: path.join(artifactDir, "local-media-dollar-home-allowed.png"), |
| 78 | + }); |
| 79 | + } |
| 80 | + } finally { |
| 81 | + await suite.closeBrowserContext(context); |
| 82 | + } |
| 83 | + }); |
| 84 | +}); |
0 commit comments