|
| 1 | +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +let page: Record<string, unknown> | null = null; |
| 4 | +let locator: Record<string, unknown> | null = null; |
| 5 | + |
| 6 | +const getPageForTargetId = vi.fn(async () => { |
| 7 | + if (!page) { |
| 8 | + throw new Error("test: page not set"); |
| 9 | + } |
| 10 | + return page; |
| 11 | +}); |
| 12 | +const ensurePageState = vi.fn(() => ({})); |
| 13 | +const restoreRoleRefsForTarget = vi.fn(() => {}); |
| 14 | +const refLocator = vi.fn(() => { |
| 15 | + if (!locator) { |
| 16 | + throw new Error("test: locator not set"); |
| 17 | + } |
| 18 | + return locator; |
| 19 | +}); |
| 20 | +const forceDisconnectPlaywrightForTarget = vi.fn(async () => {}); |
| 21 | + |
| 22 | +const resolveStrictExistingPathsWithinRoot = |
| 23 | + vi.fn<typeof import("./paths.js").resolveStrictExistingPathsWithinRoot>(); |
| 24 | + |
| 25 | +vi.mock("./pw-session.js", () => { |
| 26 | + return { |
| 27 | + ensurePageState, |
| 28 | + forceDisconnectPlaywrightForTarget, |
| 29 | + getPageForTargetId, |
| 30 | + refLocator, |
| 31 | + restoreRoleRefsForTarget, |
| 32 | + }; |
| 33 | +}); |
| 34 | + |
| 35 | +vi.mock("./paths.js", () => { |
| 36 | + return { |
| 37 | + DEFAULT_UPLOAD_DIR: "/tmp/openclaw/uploads", |
| 38 | + resolveStrictExistingPathsWithinRoot, |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +let setInputFilesViaPlaywright: typeof import("./pw-tools-core.interactions.js").setInputFilesViaPlaywright; |
| 43 | + |
| 44 | +describe("setInputFilesViaPlaywright", () => { |
| 45 | + beforeAll(async () => { |
| 46 | + ({ setInputFilesViaPlaywright } = await import("./pw-tools-core.interactions.js")); |
| 47 | + }); |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks(); |
| 51 | + page = null; |
| 52 | + locator = null; |
| 53 | + resolveStrictExistingPathsWithinRoot.mockResolvedValue({ |
| 54 | + ok: true, |
| 55 | + paths: ["/private/tmp/openclaw/uploads/ok.txt"], |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("revalidates upload paths and uses resolved canonical paths for inputRef", async () => { |
| 60 | + const setInputFiles = vi.fn(async () => {}); |
| 61 | + locator = { |
| 62 | + setInputFiles, |
| 63 | + elementHandle: vi.fn(async () => null), |
| 64 | + }; |
| 65 | + page = { |
| 66 | + locator: vi.fn(() => ({ first: () => locator })), |
| 67 | + }; |
| 68 | + |
| 69 | + await setInputFilesViaPlaywright({ |
| 70 | + cdpUrl: "http://127.0.0.1:18792", |
| 71 | + targetId: "T1", |
| 72 | + inputRef: "e7", |
| 73 | + paths: ["/tmp/openclaw/uploads/ok.txt"], |
| 74 | + }); |
| 75 | + |
| 76 | + expect(resolveStrictExistingPathsWithinRoot).toHaveBeenCalledWith({ |
| 77 | + rootDir: "/tmp/openclaw/uploads", |
| 78 | + requestedPaths: ["/tmp/openclaw/uploads/ok.txt"], |
| 79 | + scopeLabel: "uploads directory (/tmp/openclaw/uploads)", |
| 80 | + }); |
| 81 | + expect(refLocator).toHaveBeenCalledWith(page, "e7"); |
| 82 | + expect(setInputFiles).toHaveBeenCalledWith(["/private/tmp/openclaw/uploads/ok.txt"]); |
| 83 | + }); |
| 84 | + |
| 85 | + it("throws and skips setInputFiles when use-time validation fails", async () => { |
| 86 | + resolveStrictExistingPathsWithinRoot.mockResolvedValueOnce({ |
| 87 | + ok: false, |
| 88 | + error: "Invalid path: must stay within uploads directory", |
| 89 | + }); |
| 90 | + |
| 91 | + const setInputFiles = vi.fn(async () => {}); |
| 92 | + locator = { |
| 93 | + setInputFiles, |
| 94 | + elementHandle: vi.fn(async () => null), |
| 95 | + }; |
| 96 | + page = { |
| 97 | + locator: vi.fn(() => ({ first: () => locator })), |
| 98 | + }; |
| 99 | + |
| 100 | + await expect( |
| 101 | + setInputFilesViaPlaywright({ |
| 102 | + cdpUrl: "http://127.0.0.1:18792", |
| 103 | + targetId: "T1", |
| 104 | + element: "input[type=file]", |
| 105 | + paths: ["/tmp/openclaw/uploads/missing.txt"], |
| 106 | + }), |
| 107 | + ).rejects.toThrow("Invalid path: must stay within uploads directory"); |
| 108 | + |
| 109 | + expect(setInputFiles).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments