|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { MEDIA_MAX_BYTES } from "../media/store.js"; |
| 5 | +import { createTempHomeEnv, type TempHomeEnv } from "../test-utils/temp-home.js"; |
| 6 | +import { persistBrowserProxyFiles } from "./proxy-files.js"; |
| 7 | + |
| 8 | +describe("persistBrowserProxyFiles", () => { |
| 9 | + let tempHome: TempHomeEnv; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + tempHome = await createTempHomeEnv("openclaw-browser-proxy-files-"); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(async () => { |
| 16 | + await tempHome.restore(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("persists browser proxy files under the shared media store", async () => { |
| 20 | + const sourcePath = "/tmp/proxy-file.txt"; |
| 21 | + const mapping = await persistBrowserProxyFiles([ |
| 22 | + { |
| 23 | + path: sourcePath, |
| 24 | + base64: Buffer.from("hello from browser proxy").toString("base64"), |
| 25 | + mimeType: "text/plain", |
| 26 | + }, |
| 27 | + ]); |
| 28 | + |
| 29 | + const savedPath = mapping.get(sourcePath); |
| 30 | + expect(typeof savedPath).toBe("string"); |
| 31 | + expect(path.normalize(savedPath ?? "")).toContain( |
| 32 | + `${path.sep}.openclaw${path.sep}media${path.sep}browser${path.sep}`, |
| 33 | + ); |
| 34 | + await expect(fs.readFile(savedPath ?? "", "utf8")).resolves.toBe("hello from browser proxy"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("rejects browser proxy files that exceed the shared media size limit", async () => { |
| 38 | + const oversized = Buffer.alloc(MEDIA_MAX_BYTES + 1, 0x41); |
| 39 | + |
| 40 | + await expect( |
| 41 | + persistBrowserProxyFiles([ |
| 42 | + { |
| 43 | + path: "/tmp/oversized.bin", |
| 44 | + base64: oversized.toString("base64"), |
| 45 | + mimeType: "application/octet-stream", |
| 46 | + }, |
| 47 | + ]), |
| 48 | + ).rejects.toThrow("Media exceeds 5MB limit"); |
| 49 | + |
| 50 | + await expect( |
| 51 | + fs.stat(path.join(tempHome.home, ".openclaw", "media", "browser")), |
| 52 | + ).rejects.toThrow(); |
| 53 | + }); |
| 54 | +}); |
0 commit comments