| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtemp, readFile, rm, stat } from "node:fs/promises"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { Readable } from "node:stream"; |
| 7 | import { UploadManager } from "./browser-uploads.mjs"; |
| 8 | |
| 9 | async function fixture(t, multiple = true) { |
| 10 | const directory = await mkdtemp(join(tmpdir(), "cage-uploads-")); |
| 11 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 12 | const calls = []; |
| 13 | const manager = new UploadManager({ |
| 14 | directory, |
| 15 | cdp: async (...args) => calls.push(args), |
| 16 | }); |
| 17 | await manager.opened("tab-1", "session-1", { |
| 18 | backendNodeId: 42, |
| 19 | mode: multiple ? "selectMultiple" : "selectSingle", |
| 20 | }); |
| 21 | return { manager, directory, calls, id: manager.snapshot()[0].id }; |
| 22 | } |
| 23 | |
| 24 | test("binary files retain their names and bytes, and only the original input receives them", async (t) => { |
| 25 | const f = await fixture(t); |
| 26 | const bytes = Buffer.from([0, 255, 27, 99, 0]); |
| 27 | await f.manager.receive( |
| 28 | f.id, |
| 29 | "参考视频.mp4", |
| 30 | Readable.from([bytes.subarray(0, 2), bytes.subarray(2)]), |
| 31 | ); |
| 32 | await f.manager.receive(f.id, "empty.txt", Readable.from([])); |
| 33 | await f.manager.finish(f.id); |
| 34 | assert.equal(f.calls[0][0], "DOM.setFileInputFiles"); |
| 35 | assert.equal(f.calls[0][1].backendNodeId, 42); |
| 36 | assert.equal(f.calls[0][2], "session-1"); |
| 37 | const [file, empty] = f.calls[0][1].files; |
| 38 | assert.deepEqual(await readFile(file), bytes); |
| 39 | assert.equal((await stat(file)).mode & 0o777, 0o600); |
| 40 | assert.equal((await readFile(empty)).length, 0); |
| 41 | assert.deepEqual(f.manager.snapshot(), []); |
| 42 | assert.equal(f.manager.retained.get("tab-1").length, 1); |
| 43 | }); |
| 44 | |
| 45 | test("cancel during a stream refuses the rest and never assigns partial files", async (t) => { |
| 46 | const f = await fixture(t); |
| 47 | async function* chunks() { |
| 48 | yield Buffer.from("partial"); |
| 49 | f.manager.cancel(f.id); |
| 50 | yield Buffer.from("discarded"); |
| 51 | } |
| 52 | await assert.rejects(f.manager.receive(f.id, "video.mp4", chunks()), /取消/); |
| 53 | await assert.rejects(f.manager.finish(f.id), /关闭/); |
| 54 | assert.deepEqual(f.calls, []); |
| 55 | }); |
| 56 | |
| 57 | test("a stale chooser and traversal names cannot select arbitrary files", async (t) => { |
| 58 | const f = await fixture(t, false); |
| 59 | for (const name of ["../secret", "a/b", "a\\b", "..", "a\u0000b"]) { |
| 60 | await assert.rejects( |
| 61 | f.manager.receive(f.id, name, Readable.from([Buffer.from("data")])), |
| 62 | /文件名/, |
| 63 | ); |
| 64 | } |
| 65 | await f.manager.receive(f.id, "one.txt", Readable.from([Buffer.from("one")])); |
| 66 | await assert.rejects( |
| 67 | f.manager.receive(f.id, "two.txt", Readable.from([])), |
| 68 | /数量/, |
| 69 | ); |
| 70 | f.manager.cancelTab("tab-1"); |
| 71 | await assert.rejects(f.manager.finish(f.id), /关闭/); |
| 72 | assert.deepEqual(f.calls, []); |
| 73 | }); |
| 74 | |
| 75 | test("a removed DOM input produces a recoverable cancellation", async (t) => { |
| 76 | const f = await fixture(t); |
| 77 | await f.manager.receive(f.id, "one.txt", Readable.from([Buffer.from("one")])); |
| 78 | f.manager.cdp = async () => { |
| 79 | throw new Error("Node no longer exists"); |
| 80 | }; |
| 81 | await assert.rejects(f.manager.finish(f.id), /Node/); |
| 82 | assert.deepEqual(f.manager.snapshot(), []); |
| 83 | }); |