| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import vm from "node:vm"; |
| 5 | |
| 6 | const source = readFileSync(new URL("./file-transfer.js", import.meta.url), "utf8") |
| 7 | .replace(/^import .*;\n/, "").replaceAll("export ", ""); |
| 8 | function fixture({ fail, switchAfterUpload = false } = {}) { |
| 9 | const calls = []; let active = true; |
| 10 | const functions = vm.runInNewContext(source + "\n({sendFiles,shellFilePaths})", { |
| 11 | URLSearchParams, |
| 12 | api: async (path, body) => { calls.push([path, body]); return { id: "upload-id" }; }, |
| 13 | uploadFile: async (path, file) => { |
| 14 | calls.push([path, file]); if (fail === file.name) throw Error("connection lost"); |
| 15 | if (switchAfterUpload) active = false; |
| 16 | return { ok: true, path: "/home/cage/attachments/" + file.name }; |
| 17 | }, |
| 18 | native: async (action, body) => { calls.push([action, body]); return { ok: true, paths: ["/home/cage/attachments/video.mp4"] }; }, |
| 19 | }); |
| 20 | return { ...functions, calls, valid: () => active }; |
| 21 | } |
| 22 | |
| 23 | test("video and document files remain one ordered batch and bypass image conversion", async () => { |
| 24 | const f = fixture(); |
| 25 | const files = [{ name: "原视频.mp4", size: 8 << 20 }, { name: "中文.txt", size: 13 }]; |
| 26 | const result = await f.sendFiles({ files, kind: "drop", x: 0.2, y: 0.8 }, "browser", "original-tab"); |
| 27 | assert.equal(f.calls[0][1].id, "original-tab"); |
| 28 | assert.equal(f.calls[1][1], files[0]); assert.equal(f.calls[2][1], files[1]); |
| 29 | assert.equal(f.calls.at(-1)[0], "/input/files/finish"); |
| 30 | assert.equal(result.paths.length, 2); |
| 31 | }); |
| 32 | |
| 33 | test("a failed file or switched target cancels a batch without delivering partial attachments", async () => { |
| 34 | for (const options of [{ fail: "second.mp4" }, { switchAfterUpload: true }]) { |
| 35 | const f = fixture(options); |
| 36 | await assert.rejects(f.sendFiles({ files: [{ name: "first.txt", size: 1 }, { name: "second.mp4", size: 2 }] }, "browser", "tab", { valid: f.valid })); |
| 37 | assert.equal(f.calls.at(-1)[0], "/input/files/cancel"); |
| 38 | assert.equal(f.calls.some(([path]) => path === "/input/files/finish"), false); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | test("native copied files consume an opaque selection handle for the original destination", async () => { |
| 43 | const f = fixture(); |
| 44 | const result = await f.sendFiles({ fileSelection: "selected-files" }, "terminal", "original-terminal"); |
| 45 | assert.equal(f.calls[1][0], "uploadHostFiles"); |
| 46 | assert.equal(f.calls[1][1].selection, "selected-files"); |
| 47 | assert.equal(f.calls[1][1].target, "terminal"); |
| 48 | assert.equal(f.calls[1][1].id, "upload-id"); |
| 49 | assert.equal(result.paths[0], "/home/cage/attachments/video.mp4"); |
| 50 | }); |
| 51 | |
| 52 | test("attachment paths preserve apostrophes in both PowerShell and POSIX shells", () => { |
| 53 | const f = fixture(); |
| 54 | assert.equal(f.shellFilePaths(["C:\\Users\\Cage\\O'Brien.txt"]), "'C:\\Users\\Cage\\O''Brien.txt' "); |
| 55 | assert.equal(f.shellFilePaths(["/home/cage/O'Brien.txt"]), "'/home/cage/O'\\''Brien.txt' "); |
| 56 | }); |