| 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 writes = []; |
| 8 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198"); |
| 9 | const { browser, page, state, errors } = await createWorkbench({ |
| 10 | mockWebSockets: false, url: display.url, |
| 11 | nativeRequest: body => { if (body.action === "clipboardWrite") writes.push(body.text); return true; }, |
| 12 | }); |
| 13 | await page.addInitScript(() => { |
| 14 | window.EventSource = class extends EventTarget { |
| 15 | constructor() { super(); window.metadataEvents = this; queueMicrotask(() => this.onopen?.()); } |
| 16 | close() {} |
| 17 | }; |
| 18 | }); |
| 19 | const sendState = () => page.evaluate(snapshot => { |
| 20 | window.metadataEvents.onmessage({ data: JSON.stringify(snapshot) }); |
| 21 | }, { tabs: state.tabs, active: state.active, downloads: [], uploads: [] }); |
| 22 | const copyContext = () => state.requests.filter(item => item.path === "/browser/copy-context").at(-1)?.body.context; |
| 23 | const copy = (id, text, context = copyContext()) => page.evaluate(value => { |
| 24 | window.metadataEvents.dispatchEvent(new MessageEvent("clipboard", { data: JSON.stringify(value) })); |
| 25 | }, { id, text, context }); |
| 26 | const clickCanvas = async () => { |
| 27 | const prepared = page.waitForResponse("**/api/browser/copy-context"); |
| 28 | await page.locator(".remote-canvas canvas").click({ position: { x: 50, y: 50 } }); |
| 29 | await prepared; |
| 30 | }; |
| 31 | const settle = () => page.waitForTimeout(100); |
| 32 | try { |
| 33 | state.status.ready = state.status.vm_running = true; |
| 34 | state.active = "first"; |
| 35 | const favicon = await page.evaluate(() => { |
| 36 | const canvas = document.createElement("canvas"); |
| 37 | canvas.width = canvas.height = 32; |
| 38 | canvas.getContext("2d").fillRect(0, 0, 32, 32); |
| 39 | return canvas.toDataURL("image/png"); |
| 40 | }); |
| 41 | state.tabs = [ |
| 42 | { id: "first", kind: "web", title: "First site", url: "https://example.test/first", favicon }, |
| 43 | { id: "second", kind: "web", title: "Second site", url: "https://example.test/second", favicon: "" }, |
| 44 | ]; |
| 45 | const unexpected = []; |
| 46 | page.on("request", request => { if (new URL(request.url()).origin !== display.url) unexpected.push(request.url()); }); |
| 47 | await page.reload(); |
| 48 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 49 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 50 | const canvas = page.locator(".remote-canvas canvas"); |
| 51 | await canvas.waitFor(); |
| 52 | await page.waitForFunction(() => document.querySelector('[role="tab"] .tab-favicon')?.naturalWidth === 32); |
| 53 | await page.getByRole("button", { name: "搜索标签页", exact: true }).click(); |
| 54 | assert.equal(await page.locator(".all-tabs-results .tab-favicon").count(), 1); |
| 55 | await page.keyboard.press("Escape"); |
| 56 | |
| 57 | await clickCanvas(); |
| 58 | display.clipboard("unrelated guest update"); |
| 59 | await settle(); |
| 60 | assert.deepEqual(writes, [], "ordinary clicks must not authorize RFB clipboard writes"); |
| 61 | await copy("first", "复制按钮的文字🙂"); |
| 62 | await page.getByText("已复制到本机", { exact: true }).waitFor(); |
| 63 | assert.deepEqual(writes, ["复制按钮的文字🙂"]); |
| 64 | |
| 65 | await page.getByRole("textbox", { name: "网页地址或搜索" }).click(); |
| 66 | await copy("first", "late local input"); |
| 67 | await settle(); |
| 68 | assert.equal(writes.length, 1); |
| 69 | await page.getByRole("tab", { name: "Second site", exact: true }).click(); |
| 70 | await clickCanvas(); |
| 71 | await copy("first", "late previous tab"); |
| 72 | await settle(); |
| 73 | assert.equal(writes.length, 1); |
| 74 | await copy("second", "当前标签"); |
| 75 | await settle(); |
| 76 | assert.equal(writes.at(-1), "当前标签"); |
| 77 | const oldContext = copyContext(); |
| 78 | await page.getByRole("tab", { name: "First site", exact: true }).click(); |
| 79 | await clickCanvas(); |
| 80 | await page.getByRole("tab", { name: "Second site", exact: true }).click(); |
| 81 | await clickCanvas(); |
| 82 | assert.notEqual(copyContext(), oldContext); |
| 83 | await copy("second", "late copy after tab round-trip", oldContext); |
| 84 | await settle(); |
| 85 | assert.equal(writes.length, 2); |
| 86 | |
| 87 | state.tabs[0].url += "#route"; |
| 88 | await sendState(); |
| 89 | assert.equal(await page.getByRole("tab", { name: "First site", exact: true }).locator("img").count(), 1); |
| 90 | state.tabs[0].favicon = "data:image/png;base64,aW52YWxpZA=="; |
| 91 | await sendState(); |
| 92 | await page.getByRole("tab", { name: "First site", exact: true }).locator("img").waitFor({ state: "detached" }); |
| 93 | state.tabs[0].favicon = "https://third-party.test/icon.png"; |
| 94 | await sendState(); |
| 95 | assert.equal(await page.getByRole("tab", { name: "First site", exact: true }).locator("img").count(), 0); |
| 96 | await page.getByRole("button", { name: "工作台", exact: true }).click(); |
| 97 | await copy("second", "hidden view"); |
| 98 | await settle(); |
| 99 | assert.equal(writes.length, 2); |
| 100 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 101 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 102 | await clickCanvas(); |
| 103 | await copy("second", "late copy after reconnect", oldContext); |
| 104 | await settle(); |
| 105 | assert.equal(writes.length, 2); |
| 106 | assert.deepEqual(unexpected, []); |
| 107 | assert.deepEqual(errors, []); |
| 108 | console.log("PASS: tab/search favicons, SPA retention, broken icon fallback, no host icon requests, explicit copy events, unrelated RFB rejection, local focus/tab/hidden-view isolation"); |
| 109 | } finally { |
| 110 | await browser.close(); |
| 111 | await display.close(); |
| 112 | } |