| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | import { startDisplay } from "./rfb-wire-fixture.mjs"; |
| 4 | |
| 5 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { sharedClipboard: true }); |
| 6 | const reads = [], prepared = [], cancelled = [], delivered = []; |
| 7 | const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg=="; |
| 8 | const { browser, page, state, errors } = await createWorkbench({ |
| 9 | url: display.url, mockWebSockets: false, |
| 10 | nativeRequest: request => { |
| 11 | if (request.action === "clipboardRevision") return 1; |
| 12 | if (request.action === "clipboardSyncRead") return { text: "synthetic system clipboard", revision: 1 }; |
| 13 | if (request.action === "clipboardReadContent") return new Promise(resolve => reads.push(resolve)); |
| 14 | return true; |
| 15 | }, |
| 16 | async beforeNavigate(page, state) { |
| 17 | state.status.ready = state.status.vm_running = true; |
| 18 | state.status.guest_system = state.account.guest_system = "macos"; |
| 19 | state.tabs = [ |
| 20 | { id: "input-one", kind: "web", title: "输入测试一", url: "https://one.example.test" }, |
| 21 | { id: "input-two", kind: "web", title: "输入测试二", url: "https://two.example.test" }, |
| 22 | ]; |
| 23 | state.active = "input-one"; |
| 24 | await page.route("**/api/browser/tabs", route => route.fulfill({ json: { |
| 25 | tabs: state.tabs, active: state.active, capabilities: { nativeClipboard: true, systemClipboard: false }, |
| 26 | } })); |
| 27 | await page.route("**/api/input", route => route.fulfill({ json: { available: true, context: "a".repeat(32), rect: [20, 20, 1, 24] } })); |
| 28 | await page.route("**/api/input/paste/start", route => { |
| 29 | const body = route.request().postDataJSON(); |
| 30 | const context = String(prepared.length + 1).padStart(32, "0"); |
| 31 | prepared.push({ ...body, context }); |
| 32 | return route.fulfill({ json: { available: true, context } }); |
| 33 | }); |
| 34 | await page.route("**/api/input/paste/cancel", route => { |
| 35 | cancelled.push(route.request().postDataJSON()); |
| 36 | return route.fulfill({ json: { ok: true } }); |
| 37 | }); |
| 38 | for (const kind of ["text", "image"]) await page.route(`**/api/input/${kind}`, route => { |
| 39 | delivered.push({ kind, body: route.request().postDataJSON() }); |
| 40 | return route.fulfill({ json: { ok: true } }); |
| 41 | }); |
| 42 | }, |
| 43 | }); |
| 44 | page.setDefaultTimeout(10000); |
| 45 | async function until(predicate) { |
| 46 | for (let attempt = 0; attempt < 200; attempt++) { |
| 47 | if (predicate()) return; |
| 48 | await page.waitForTimeout(20); |
| 49 | } |
| 50 | assert(predicate(), "expected private clipboard operation did not arrive"); |
| 51 | } |
| 52 | try { |
| 53 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 54 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 55 | await page.waitForFunction(() => window.cageSharedClipboardActive === true); |
| 56 | const canvas = page.locator(".browser-lane .remote-canvas canvas"); |
| 57 | await canvas.click({ position: { x: 40, y: 40 } }); |
| 58 | display.stats.keys.length = 0; |
| 59 | await page.keyboard.press("Meta+f"); |
| 60 | await until(() => display.stats.keys.some(key => key.symbol === 102 && !key.down)); |
| 61 | assert(display.stats.keys.some(key => key.symbol === 0xffeb && key.down)); |
| 62 | assert(!display.stats.keys.some(key => key.symbol === 0xffe3)); |
| 63 | await page.keyboard.press("Control+a"); |
| 64 | await until(() => display.stats.keys.some(key => key.symbol === 0xffe3 && !key.down)); |
| 65 | await page.evaluate(() => { |
| 66 | window.pasteIntent = []; |
| 67 | const fetch = window.fetch.bind(window); |
| 68 | window.fetch = (...args) => { |
| 69 | if (String(args[0]).endsWith("/input/paste/start")) window.pasteIntent.push("prepare"); |
| 70 | return fetch(...args); |
| 71 | }; |
| 72 | const bridge = window.webkit.messageHandlers.cage, post = bridge.postMessage.bind(bridge); |
| 73 | bridge.postMessage = body => { |
| 74 | if (body.action === "clipboardReadContent") window.pasteIntent.push("read"); |
| 75 | return post(body); |
| 76 | }; |
| 77 | const create = URL.createObjectURL.bind(URL); |
| 78 | URL.createObjectURL = value => { window.pasteIntent.push("decode"); return create(value); }; |
| 79 | }); |
| 80 | display.stats.keys.length = 0; |
| 81 | await page.keyboard.press("Meta+v"); |
| 82 | await until(() => reads.length === 1 && prepared.length === 1); |
| 83 | assert.deepEqual(await page.evaluate(() => window.pasteIntent), ["prepare", "read"]); |
| 84 | await page.keyboard.press("Enter"); |
| 85 | assert(!display.stats.keys.some(key => key.symbol === 0xff0d)); |
| 86 | const longText = "共享架构🙂\n".repeat(2000); |
| 87 | reads[0]({ text: longText }); |
| 88 | await until(() => delivered.length === 1 && display.stats.keys.some(key => key.symbol === 0xff0d && !key.down)); |
| 89 | assert.equal(delivered[0].kind, "text"); |
| 90 | assert.equal(delivered[0].body.text, longText); |
| 91 | assert.equal(delivered[0].body.id, "input-one"); |
| 92 | assert.equal(delivered[0].body.context, prepared[0].context); |
| 93 | assert(!display.stats.keys.some(key => key.symbol === 118), "private clipboard text must not depend on a guest OS paste chord"); |
| 94 | |
| 95 | await page.keyboard.press("Meta+v"); |
| 96 | await until(() => reads.length === 2); |
| 97 | await page.getByRole("tab", { name: "输入测试二", exact: true }).click(); |
| 98 | await until(() => state.active === "input-two"); |
| 99 | reads[1]({ png }); |
| 100 | await until(() => cancelled.some(item => item.context === prepared[1].context)); |
| 101 | assert.equal(delivered.length, 1, "an old tab must reject the delayed PNG"); |
| 102 | |
| 103 | await canvas.click({ position: { x: 50, y: 50 } }); |
| 104 | assert.deepEqual(await page.evaluate(() => [window.cageEditAction("paste"), window.cageEditAction("paste")]), [true, true]); |
| 105 | await until(() => reads.length === 4 && prepared.length === 4); |
| 106 | reads[3]({ text: "second queued paste" }); |
| 107 | await page.waitForTimeout(80); |
| 108 | assert.equal(delivered.length, 1, "the second clipboard read must wait for the first paste"); |
| 109 | reads[2]({ png }); |
| 110 | await until(() => delivered.length === 3); |
| 111 | assert.deepEqual(delivered.slice(1).map(item => item.kind), ["image", "text"]); |
| 112 | assert(delivered.slice(1).every(item => item.body.id === "input-two")); |
| 113 | |
| 114 | await page.evaluate(() => { window.pasteIntent.length = 0; }); |
| 115 | await page.getByLabel("Cage 网页输入").evaluate((element, encoded) => { |
| 116 | const data = new DataTransfer(); |
| 117 | data.items.add(new File([Uint8Array.from(atob(encoded), value => value.charCodeAt(0))], "screenshot.png", { type: "image/png" })); |
| 118 | element.dispatchEvent(new ClipboardEvent("paste", { bubbles: true, cancelable: true, clipboardData: data })); |
| 119 | }, png); |
| 120 | await until(() => delivered.length === 4); |
| 121 | assert.deepEqual(await page.evaluate(() => window.pasteIntent), ["prepare", "decode"]); |
| 122 | assert.equal(delivered[3].kind, "image"); |
| 123 | await until(() => cancelled.length === prepared.length); |
| 124 | assert.deepEqual(errors, []); |
| 125 | console.log(JSON.stringify({ result: "PASS", browser: browser.version(), textBytes: Buffer.byteLength(longText), checks: [ |
| 126 | "Mac browser Command and physical Control keep separate RFB modifiers", |
| 127 | "private clipboard stays reachable while OS sharing is active", "preparation precedes clipboard read and bitmap decode", |
| 128 | "long Unicode text uses bound text transfer before Enter", "tab change rejects a pending PNG", "successive pastes stay ordered", "unused and completed preparations are cancelled", |
| 129 | ], deliveries: delivered.map(item => ({ kind: item.kind, id: item.body.id, context: item.body.context })) })); |
| 130 | } finally { await browser.close(); await display.close(); } |