| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 5 | import { startDisplay } from "./rfb-wire-fixture.mjs"; |
| 6 | |
| 7 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { sharedClipboard: true }); |
| 8 | let revision = 1, clipboard = { text: "initial host text" }; |
| 9 | const writes = []; |
| 10 | const { browser, page, state, errors } = await createWorkbench({ |
| 11 | mockWebSockets: false, url: display.url, |
| 12 | nativeRequest: request => { |
| 13 | if (request.action === "clipboardWrite") { clipboard = { text: request.text }; revision++; return true; } |
| 14 | if (request.action === "clipboardRevision") return revision; |
| 15 | if (request.action === "clipboardSyncRead") return { ...clipboard, revision }; |
| 16 | if (request.action === "clipboardSyncWrite") { |
| 17 | if (request.revision !== revision) return { revision, superseded: true }; |
| 18 | clipboard = Object.fromEntries(Object.entries(request).filter(([key]) => ["text","png","files"].includes(key))); |
| 19 | writes.push(clipboard); return { revision: ++revision }; |
| 20 | } |
| 21 | return true; |
| 22 | }, |
| 23 | }); |
| 24 | const waitFor = async predicate => { for (let attempt = 0; attempt < 100; attempt++) { if (predicate()) return; await page.waitForTimeout(30); } assert.ok(predicate(),"clipboard state did not arrive"); }; |
| 25 | try { |
| 26 | await page.route("**/api/claude-desktop", route => route.fulfill({ json: { installed: true, running: true, failed: false, managed: true, version: "fixture" } })); |
| 27 | state.status.ready = state.status.vm_running = true; |
| 28 | await page.reload(); |
| 29 | await page.getByRole("button",{ name:"Claude Desktop", exact:true }).click(); |
| 30 | await page.getByText("正在连接画面",{ exact:true }).waitFor({ state:"hidden" }); |
| 31 | await page.waitForFunction(() => window.cageSharedClipboardActive === true); |
| 32 | assert.equal(display.stats.clipboardUpdates.at(-1).text,"initial host text"); |
| 33 | |
| 34 | for (const content of [{ text:"Claude 消息🙂\n保留换行" },{ png:"fixture-png" },{ files:"10000000-2222-4333-8444-123456789abc" }]) { |
| 35 | display.sharedClipboard(content); |
| 36 | await waitFor(() => JSON.stringify(writes.at(-1)) === JSON.stringify(content)); |
| 37 | const count = display.stats.clipboardUpdates.length; |
| 38 | await page.waitForTimeout(350); |
| 39 | assert.equal(display.stats.clipboardUpdates.length,count,"native writes must not echo"); |
| 40 | } |
| 41 | |
| 42 | clipboard = { text:"new host copy" }; revision++; |
| 43 | await page.locator(".remote-canvas canvas").click({ position:{ x:40,y:40 } }); |
| 44 | display.stats.keys.length = 0; |
| 45 | await page.keyboard.press("Meta+v"); |
| 46 | await waitFor(() => display.stats.keys.some(key => key.down && key.symbol === 118)); |
| 47 | assert.equal(display.stats.clipboardUpdates.at(-1).text,"new host copy"); |
| 48 | assert.equal(state.requests.some(request => request.path === "/input/image"),false,"synchronized paste uses the actual guest clipboard"); |
| 49 | |
| 50 | await page.getByRole("button",{ name:"工作台", exact:true }).click(); |
| 51 | await page.getByRole("region",{ name:"Claude Desktop", exact:true }).waitFor({ state:"hidden" }); |
| 52 | display.sharedClipboard({ text:"guest background copy" }); |
| 53 | await waitFor(() => clipboard.text === "guest background copy"); |
| 54 | assert.equal(display.stats.clipboardConnections,1,"hiding a canvas retains the shared clipboard"); |
| 55 | |
| 56 | await page.getByRole("button", { name: "新建终端", exact: true }).first().click(); |
| 57 | await page.getByRole("menuitem", { name: "普通终端", exact: true }).click(); |
| 58 | await page.waitForFunction(() => document.querySelector(".xterm-rows")?.textContent.includes("Terminal")); |
| 59 | await page.locator(".xterm-helper-textarea").focus(); |
| 60 | const terminalCopy = "Claude 终端复制 → 共享剪贴板"; |
| 61 | display.terminalOutput(`\x1b]52;c;${Buffer.from(terminalCopy).toString("base64")}\x07`); |
| 62 | await waitFor(() => clipboard.text === terminalCopy && display.stats.clipboardUpdates.at(-1).text === terminalCopy); |
| 63 | |
| 64 | const connections = display.stats.clipboardConnections; |
| 65 | display.disconnectClipboard(); |
| 66 | clipboard = { text:"copy during disconnection" }; revision++; |
| 67 | await waitFor(() => display.stats.clipboardConnections > connections && display.stats.clipboardUpdates.at(-1).text === "copy during disconnection"); |
| 68 | await page.waitForFunction(() => window.cageSharedClipboardActive === true); |
| 69 | assert.deepEqual(errors,[]); |
| 70 | console.log("PASS: Desktop text/image/files copy, terminal OSC 52 to shared guest clipboard, no echo, native paste, background sharing and reconnect."); |
| 71 | } finally { await browser.close(); await display.close(); } |