Skip to content

Commit a29e3f8

Browse files
committed
test(browser): avoid screenshot share temp files
1 parent 3dbc870 commit a29e3f8

1 file changed

Lines changed: 6 additions & 14 deletions

File tree

‎extensions/browser/src/browser/screenshot-sharing.test.ts‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2-
import os from "node:os";
3-
import path from "node:path";
41
import { afterEach, describe, expect, it, vi } from "vitest";
52

63
const mocks = vi.hoisted(() => ({
4+
readFile: vi.fn(),
75
normalizeBrowserScreenshot: vi.fn(),
86
saveMediaBuffer: vi.fn(),
97
}));
108

9+
vi.mock("node:fs/promises", () => ({ readFile: mocks.readFile }));
1110
vi.mock("./screenshot.js", () => ({
1211
DEFAULT_BROWSER_SCREENSHOT_MAX_BYTES: 5 * 1024 * 1024,
1312
DEFAULT_BROWSER_SCREENSHOT_MAX_SIDE: 2000,
@@ -19,23 +18,16 @@ vi.mock("openclaw/plugin-sdk/media-store", () => ({
1918

2019
import { stageBrowserScreenshotForSharing } from "./screenshot-sharing.js";
2120

22-
let tempDir: string | undefined;
23-
24-
afterEach(async () => {
21+
afterEach(() => {
2522
vi.clearAllMocks();
26-
if (tempDir) {
27-
await rm(tempDir, { recursive: true, force: true });
28-
tempDir = undefined;
29-
}
3023
});
3124

3225
describe("stageBrowserScreenshotForSharing", () => {
3326
it("stages a bounded copy in the outbound media store", async () => {
34-
tempDir = await mkdtemp(path.join(os.tmpdir(), "openclaw-browser-share-"));
35-
const sourcePath = path.join(tempDir, "private-shot.png");
27+
const sourcePath = "/state/media/browser/private-shot.png";
3628
const source = Buffer.from("private screenshot");
3729
const normalized = Buffer.from("bounded screenshot");
38-
await writeFile(sourcePath, source);
30+
mocks.readFile.mockResolvedValue(source);
3931
mocks.normalizeBrowserScreenshot.mockResolvedValue({
4032
buffer: normalized,
4133
contentType: "image/jpeg",
@@ -47,7 +39,7 @@ describe("stageBrowserScreenshotForSharing", () => {
4739
await expect(stageBrowserScreenshotForSharing(sourcePath, 1200)).resolves.toBe(
4840
"/state/media/outbound/share.jpg",
4941
);
50-
expect(await readFile(sourcePath)).toEqual(source);
42+
expect(mocks.readFile).toHaveBeenCalledWith(sourcePath);
5143
expect(mocks.normalizeBrowserScreenshot).toHaveBeenCalledWith(source, {
5244
maxSide: 1200,
5345
maxBytes: 5 * 1024 * 1024,

0 commit comments

Comments
 (0)