| 1 | import assert from "node:assert/strict"; |
| 2 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 3 | |
| 4 | const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a/e8AAAAASUVORK5CYII="; |
| 5 | for (const platform of ["MacIntel", "Win32", "Linux x86_64"]) { |
| 6 | const inputs = [], images = []; |
| 7 | let clipboard = { png }, reads = 0, release, fail = false; |
| 8 | const { page, browser, state, errors } = await createWorkbench({ |
| 9 | mockWebSockets: false, |
| 10 | beforeNavigate: page => page.addInitScript(platform => { |
| 11 | Object.defineProperty(navigator, "platform", { value: platform }); |
| 12 | }, platform), |
| 13 | nativeRequest: request => { |
| 14 | if (request.action === "clipboardReadContent") { reads++; return clipboard; } |
| 15 | }, |
| 16 | }); |
| 17 | const wait = async predicate => { |
| 18 | const deadline = Date.now() + 5000; |
| 19 | while (!predicate() && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 10)); |
| 20 | assert.ok(predicate(), "terminal paste did not finish"); |
| 21 | }; |
| 22 | try { |
| 23 | await page.route("**/api/terminals", route => { |
| 24 | if (route.request().method() !== "POST") return route.fallback(); |
| 25 | const terminal = { id: "claude-image", kind: "claude", title: "Claude 1", workspace: state.settings.workspace }; |
| 26 | state.terminals = [terminal]; |
| 27 | return route.fulfill({ json: terminal }); |
| 28 | }); |
| 29 | await page.route("**/api/input/paste/start", route => route.fulfill({ json: { available: true, context: "claude-image" } })); |
| 30 | await page.route("**/api/input/image", async route => { |
| 31 | images.push(route.request().postDataJSON()); |
| 32 | await new Promise(resolve => { release = resolve; }); |
| 33 | await route.fulfill(fail ? { status: 502, contentType: "text/plain", body: "测试:图片传递失败" } |
| 34 | : { json: { path: "/guest/attachments/image-test.png" } }); |
| 35 | }); |
| 36 | await page.routeWebSocket("**/api/**", socket => { |
| 37 | if (!socket.url().includes("/terminals/")) return socket.close(); |
| 38 | socket.onMessage(message => { const value = JSON.parse(message); if (value.type === "input") inputs.push(value.data); }); |
| 39 | socket.send(Buffer.from("\x1b[?2004hClaude image paste\r\n> ")); |
| 40 | }); |
| 41 | await page.reload(); |
| 42 | await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click(); |
| 43 | await page.getByRole("button", { name: "新建终端", exact: true }).first().click(); |
| 44 | await page.getByRole("menuitem", { name: /Claude/ }).first().click(); |
| 45 | await page.waitForFunction(() => document.querySelector(".xterm-rows")?.textContent.includes("Claude")); |
| 46 | await page.locator(".xterm-helper-textarea").focus(); |
| 47 | const expected = "\x1b[200~'/guest/attachments/image-test.png' \x1b[201~"; |
| 48 | |
| 49 | await page.keyboard.press("Control+v"); |
| 50 | await wait(() => images.length === 1); |
| 51 | await page.keyboard.press("Enter"); |
| 52 | assert.deepEqual(inputs, []); |
| 53 | release(); |
| 54 | await wait(() => inputs.length === 2); |
| 55 | assert.deepEqual(inputs, [expected, "\r"]); |
| 56 | assert.equal(images[0].png, png); |
| 57 | assert.equal(images[0].target, "terminal"); |
| 58 | assert.equal(images[0].id, "claude-image"); |
| 59 | |
| 60 | await page.keyboard.press(platform === "MacIntel" ? "Meta+v" : "Control+Shift+V"); |
| 61 | await wait(() => images.length === 2); |
| 62 | release(); |
| 63 | await wait(() => inputs.length === 3); |
| 64 | assert.equal(inputs.at(-1), expected); |
| 65 | assert.equal(reads, 2); |
| 66 | |
| 67 | |
| 68 | assert.equal(await page.evaluate(() => window.cageEditAction("paste")), true); |
| 69 | await wait(() => images.length === 3); |
| 70 | release(); |
| 71 | await wait(() => inputs.length === 4); |
| 72 | assert.equal(inputs.at(-1), expected); |
| 73 | |
| 74 | fail = true; |
| 75 | await page.keyboard.press("Control+v"); |
| 76 | await wait(() => images.length === 4); |
| 77 | await page.keyboard.press("Enter"); |
| 78 | release(); |
| 79 | await page.getByText("测试:图片传递失败", { exact: true }).waitFor(); |
| 80 | assert.equal(inputs.length, 4, "a failed image must cancel its queued Enter"); |
| 81 | fail = false; |
| 82 | clipboard = { text: "paste after failure" }; |
| 83 | await page.keyboard.press("Control+v"); |
| 84 | await wait(() => inputs.length === 5); |
| 85 | assert.equal(inputs.at(-1), "\x1b[200~paste after failure\x1b[201~"); |
| 86 | assert.deepEqual(errors, []); |
| 87 | console.log(`${platform}: native Ctrl+V, primary paste, Edit > Paste, image/Enter ordering and failure recovery passed`); |
| 88 | } catch (error) { |
| 89 | console.error({ platform, reads, inputs, images: images.length, terminals: state.terminals, |
| 90 | recentRequests: state.requests.slice(-8), screen: await page.locator(".terminal-view").allTextContents(), errors }); |
| 91 | throw error; |
| 92 | } finally { await browser.close(); } |
| 93 | } |