| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BrowserClipboard } from "./browser-clipboard.mjs"; |
| 4 | |
| 5 | test("clipboard events require a live default context and private binding token", async () => { |
| 6 | const copies = []; |
| 7 | const observer = new BrowserClipboard( |
| 8 | async () => ({}), |
| 9 | (value) => copies.push(value), |
| 10 | ); |
| 11 | await observer.attach("session", "tab"); |
| 12 | const state = observer.sessions.get("session"); |
| 13 | const event = (method, params) => |
| 14 | observer.event({ sessionId: "session", method, params }); |
| 15 | const copied = ( |
| 16 | text, |
| 17 | token = state.token, |
| 18 | executionContextId = 3, |
| 19 | context = "context-a", |
| 20 | ) => |
| 21 | event("Runtime.bindingCalled", { |
| 22 | name: state.name, |
| 23 | executionContextId, |
| 24 | payload: JSON.stringify({ token, text, context }), |
| 25 | }); |
| 26 | copied("before context"); |
| 27 | event("Runtime.executionContextCreated", { |
| 28 | context: { id: 3, auxData: { isDefault: true, frameId: "frame" } }, |
| 29 | }); |
| 30 | event("Runtime.executionContextCreated", { |
| 31 | context: { id: 4, auxData: { isDefault: false, frameId: "frame" } }, |
| 32 | }); |
| 33 | copied("forged", "wrong token"); |
| 34 | copied("isolated", state.token, 4); |
| 35 | copied("x".repeat(65537)); |
| 36 | copied("invalid context", state.token, 3, null); |
| 37 | copied("invalid context", state.token, 3, "x".repeat(65)); |
| 38 | copied("exact🙂"); |
| 39 | assert.deepEqual(copies, [ |
| 40 | { id: "tab", text: "exact🙂", context: "context-a" }, |
| 41 | ]); |
| 42 | event("Page.frameNavigated", { frame: { id: "frame" } }); |
| 43 | copied("old document"); |
| 44 | event("Runtime.executionContextCreated", { |
| 45 | context: { id: 5, auxData: { isDefault: true, frameId: "frame" } }, |
| 46 | }); |
| 47 | copied("new document", state.token, 5); |
| 48 | observer.detachTab("tab"); |
| 49 | copied("closed tab", state.token, 5); |
| 50 | assert.deepEqual( |
| 51 | copies.map((value) => value.text), |
| 52 | ["exact🙂", "new document"], |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | test("host copy context reaches every existing frame and newly attached child", async () => { |
| 57 | const calls = []; |
| 58 | const observer = new BrowserClipboard( |
| 59 | async (method, params, sessionId) => { |
| 60 | calls.push({ method, params, sessionId }); |
| 61 | if (method === "Runtime.enable" && sessionId === "child") |
| 62 | observer.event({ |
| 63 | sessionId, |
| 64 | method: "Runtime.executionContextCreated", |
| 65 | params: { |
| 66 | context: { |
| 67 | id: 3, |
| 68 | auxData: { isDefault: true, frameId: "cross-process-frame" }, |
| 69 | }, |
| 70 | }, |
| 71 | }); |
| 72 | return {}; |
| 73 | }, |
| 74 | () => {}, |
| 75 | ); |
| 76 | await observer.attach("root", "tab"); |
| 77 | observer.event({ |
| 78 | sessionId: "root", |
| 79 | method: "Runtime.executionContextCreated", |
| 80 | params: { |
| 81 | context: { id: 1, auxData: { isDefault: true, frameId: "root-frame" } }, |
| 82 | }, |
| 83 | }); |
| 84 | await observer.setContext("tab", "context-a"); |
| 85 | observer.sessions.get("root").contexts.set(2, "same-process-frame"); |
| 86 | await observer.attach("child", "tab"); |
| 87 | assert.ok( |
| 88 | calls.some( |
| 89 | (call) => |
| 90 | call.sessionId === "child" && |
| 91 | call.method === "Runtime.evaluate" && |
| 92 | call.params.expression.includes('"context-a"'), |
| 93 | ), |
| 94 | ); |
| 95 | const before = calls.length; |
| 96 | await observer.setContext("tab", "context-b"); |
| 97 | const updates = calls |
| 98 | .slice(before) |
| 99 | .filter((call) => call.method === "Runtime.evaluate"); |
| 100 | assert.deepEqual( |
| 101 | updates.map((call) => [call.sessionId, call.params.contextId]), |
| 102 | [ |
| 103 | ["root", 1], |
| 104 | ["root", 2], |
| 105 | ["child", 3], |
| 106 | ], |
| 107 | ); |
| 108 | assert.ok( |
| 109 | updates.every((call) => call.params.expression.includes('"context-b"')), |
| 110 | ); |
| 111 | observer.detachTab("tab"); |
| 112 | assert.equal(observer.copyContexts.has("tab"), false); |
| 113 | assert.equal(observer.sessions.size, 0); |
| 114 | }); |
| 115 | |
| 116 | test("private clipboard format notifications require an enabled receiver and the binding token", async () => { |
| 117 | for (const includeChanges of [false, true]) { |
| 118 | const copies = []; |
| 119 | const observer = new BrowserClipboard(async () => ({}), value => copies.push(value), { includeChanges }); |
| 120 | await observer.attach("session", "tab"); |
| 121 | const state = observer.sessions.get("session"); |
| 122 | observer.event({ sessionId: "session", method: "Runtime.executionContextCreated", params: { context: { id: 1, auxData: { isDefault: true, frameId: "frame" } } } }); |
| 123 | for (const token of ["wrong", state.token]) observer.event({ sessionId: "session", method: "Runtime.bindingCalled", params: { |
| 124 | name: state.name, executionContextId: 1, payload: JSON.stringify({ token, context: "gesture", changed: true }), |
| 125 | } }); |
| 126 | assert.deepEqual(copies, includeChanges ? [{ id: "tab", context: "gesture", changed: true }] : []); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | test("failed setup removes the session and allows retry", async () => { |
| 131 | let fail = true; |
| 132 | const observer = new BrowserClipboard( |
| 133 | async () => { |
| 134 | if (fail) throw new Error("CDP unavailable"); |
| 135 | return {}; |
| 136 | }, |
| 137 | () => {}, |
| 138 | ); |
| 139 | await assert.rejects(observer.attach("s", "t"), /CDP unavailable/); |
| 140 | assert.equal(observer.sessions.size, 0); |
| 141 | fail = false; |
| 142 | assert.deepEqual(await observer.attach("s", "t"), { skipped: [] }); |
| 143 | }); |
| 144 | |
| 145 | test("copy context waits for pending frame installation and frames discovered during the wait", async () => { |
| 146 | const calls = []; |
| 147 | const gates = new Map(); |
| 148 | for (const id of ["child", "nested"]) { |
| 149 | let release, entered; |
| 150 | const pending = new Promise((resolve) => { |
| 151 | release = resolve; |
| 152 | }); |
| 153 | const reached = new Promise((resolve) => { |
| 154 | entered = resolve; |
| 155 | }); |
| 156 | gates.set(id, { pending, reached, release, entered }); |
| 157 | } |
| 158 | const observer = new BrowserClipboard( |
| 159 | async (method, params, sessionId) => { |
| 160 | calls.push({ method, params, sessionId }); |
| 161 | if (method === "Runtime.enable") |
| 162 | observer.event({ |
| 163 | sessionId, |
| 164 | method: "Runtime.executionContextCreated", |
| 165 | params: { |
| 166 | context: { |
| 167 | id: 1, |
| 168 | auxData: { isDefault: true, frameId: sessionId + "-frame" }, |
| 169 | }, |
| 170 | }, |
| 171 | }); |
| 172 | if ( |
| 173 | method === "Page.addScriptToEvaluateOnNewDocument" && |
| 174 | gates.has(sessionId) |
| 175 | ) { |
| 176 | gates.get(sessionId).entered(); |
| 177 | await gates.get(sessionId).pending; |
| 178 | } |
| 179 | return {}; |
| 180 | }, |
| 181 | () => {}, |
| 182 | ); |
| 183 | await observer.attach("root", "tab"); |
| 184 | const child = observer.attach("child", "tab"); |
| 185 | await gates.get("child").reached; |
| 186 | let completed = false; |
| 187 | const update = observer.setContext("tab", "context-a").then(() => { |
| 188 | completed = true; |
| 189 | }); |
| 190 | await new Promise((resolve) => setImmediate(resolve)); |
| 191 | assert.equal(completed, false, "the handshake must wait for the child hook"); |
| 192 | assert.equal( |
| 193 | calls.some( |
| 194 | (call) => |
| 195 | call.sessionId === "child" && call.method === "Runtime.evaluate", |
| 196 | ), |
| 197 | false, |
| 198 | ); |
| 199 | const nested = observer.attach("nested", "tab"); |
| 200 | await gates.get("nested").reached; |
| 201 | gates.get("child").release(); |
| 202 | await child; |
| 203 | await new Promise((resolve) => setImmediate(resolve)); |
| 204 | assert.equal( |
| 205 | completed, |
| 206 | false, |
| 207 | "the handshake must include a newly discovered frame", |
| 208 | ); |
| 209 | gates.get("nested").release(); |
| 210 | await Promise.all([nested, update]); |
| 211 | assert.equal(completed, true); |
| 212 | for (const id of ["child", "nested"]) |
| 213 | assert.ok( |
| 214 | calls.some( |
| 215 | (call) => |
| 216 | call.sessionId === id && |
| 217 | call.method === "Runtime.evaluate" && |
| 218 | call.params.expression.includes('"context-a"'), |
| 219 | ), |
| 220 | ); |
| 221 | }); |