| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | for (const platform of ["MacIntel", "Win32", "Linux x86_64"]) { |
| 5 | const writes = [], inputs = []; |
| 6 | const { browser, page, errors } = await createWorkbench({ |
| 7 | mockWebSockets: false, |
| 8 | nativeRequest({ action, text }) { |
| 9 | if (action === "clipboardWrite") writes.push(text); |
| 10 | }, |
| 11 | }); |
| 12 | try { |
| 13 | let terminalSocket; |
| 14 | const interrupted = Promise.withResolvers(); |
| 15 | await page.addInitScript((platform) => { |
| 16 | Object.defineProperty(navigator, "platform", { value: platform }); |
| 17 | }, platform); |
| 18 | await page.routeWebSocket("**/api/**", (socket) => { |
| 19 | if (!socket.url().includes("/terminals/")) return socket.close(); |
| 20 | terminalSocket = socket; |
| 21 | socket.onMessage((message) => { |
| 22 | const { type, data } = JSON.parse(message); |
| 23 | if (type === "input") { |
| 24 | inputs.push(data); |
| 25 | if (data === "\x03") interrupted.resolve(); |
| 26 | if (data === "\x1b[99;6u") socket.send(Buffer.from(`\x1b]52;c;${Buffer.from("Claude 选中文本 🖼️").toString("base64")}\x07`)); |
| 27 | } |
| 28 | }); |
| 29 | socket.send(JSON.stringify({ type: "history" })); |
| 30 | socket.send(Buffer.from(`\x1b]52;c;${Buffer.from("旧复制不得重放").toString("base64")}\x07CopyMe1234\r\n$ `)); |
| 31 | }); |
| 32 | await page.reload(); |
| 33 | await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click(); |
| 34 | await page.getByRole("button", { name: "新建终端", exact: true }).first().click(); |
| 35 | await page.getByRole("menuitem", { name: "普通终端", exact: true }).click(); |
| 36 | const line = await page.locator(".xterm-rows").getByText("CopyMe1234", { exact: true }).first().boundingBox(); |
| 37 | const drag = async () => { |
| 38 | await page.mouse.move(line.x + 1, line.y + line.height / 2); |
| 39 | await page.mouse.down(); |
| 40 | await page.mouse.move(line.x + line.width * 0.6, line.y + line.height / 2, { steps: 5 }); |
| 41 | await page.mouse.up(); |
| 42 | }; |
| 43 | const mac = platform === "MacIntel"; |
| 44 | const copy = () => page.keyboard.press(mac ? "Meta+c" : "Control+Shift+C"); |
| 45 | assert.deepEqual(writes, [], "replayed OSC 52 must not overwrite the clipboard"); |
| 46 | await drag(); |
| 47 | await copy(); |
| 48 | await page.getByText("已复制终端选中文本", { exact: true }).waitFor(); |
| 49 | assert.deepEqual(writes, ["CopyMe"]); |
| 50 | |
| 51 | |
| 52 | |
| 53 | terminalSocket.send(JSON.stringify({ type: "history" })); |
| 54 | terminalSocket.send(Buffer.from("\x1b]52;c;c3RhbGU=\x07REPLAY_DONE\r\n")); |
| 55 | await page.locator(".xterm-rows").getByText(/REPLAY_DONE/).waitFor(); |
| 56 | assert.deepEqual(writes, ["CopyMe"]); |
| 57 | |
| 58 | terminalSocket.send(Buffer.from("\x1b[?1002h\x1b[?1006h")); |
| 59 | await page.locator(".xterm.enable-mouse-events").waitFor(); |
| 60 | await drag(); |
| 61 | await copy(); |
| 62 | await page.getByText("已复制终端选中文本", { exact: true }).waitFor(); |
| 63 | const deadline = Date.now() + 3000; |
| 64 | while (writes.length < 2 && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 10)); |
| 65 | assert.deepEqual(writes, ["CopyMe", "Claude 选中文本 🖼️"]); |
| 66 | assert.ok(inputs.some(data => data.startsWith("\x1b[<"))); |
| 67 | |
| 68 | const previousInputs = inputs.length; |
| 69 | const modifier = mac ? "Alt" : "Shift"; |
| 70 | await page.keyboard.down(modifier); |
| 71 | await drag(); |
| 72 | await page.keyboard.up(modifier); |
| 73 | await copy(); |
| 74 | await page.getByText("已复制终端选中文本", { exact: true }).waitFor(); |
| 75 | const copyDeadline = Date.now() + 3000; |
| 76 | while (writes.length < 3 && Date.now() < copyDeadline) await new Promise(resolve => setTimeout(resolve, 10)); |
| 77 | assert.deepEqual(writes, ["CopyMe", "Claude 选中文本 🖼️", "CopyMe"]); |
| 78 | assert.equal(inputs.length, previousInputs); |
| 79 | await page.keyboard.press("Control+c"); |
| 80 | await Promise.race([ |
| 81 | interrupted.promise, |
| 82 | new Promise((_, reject) => setTimeout(() => reject(new Error("Ctrl+C 未传给终端")), 3000).unref()), |
| 83 | ]); |
| 84 | |
| 85 | await page.evaluate(() => { |
| 86 | const input = document.createElement("input"); input.id = "local-copy-target"; |
| 87 | document.body.append(input); input.focus(); |
| 88 | }); |
| 89 | terminalSocket.send(Buffer.from(`\x1b]52;c;${Buffer.from("background copy").toString("base64")}\x07BACKGROUND_DONE`)); |
| 90 | await page.locator(".xterm-rows").getByText(/BACKGROUND_DONE/).waitFor(); |
| 91 | assert.equal(writes.length, 3); |
| 92 | assert.deepEqual(errors, []); |
| 93 | console.log(`${platform}: xterm/Claude selections, OSC 52, history/focus isolation and Ctrl+C passed`); |
| 94 | } finally { |
| 95 | await browser.close(); |
| 96 | } |
| 97 | } |