| 1 | |
| 2 | |
| 3 | |
| 4 | import assert from "node:assert/strict"; |
| 5 | import { readFile } from "node:fs/promises"; |
| 6 | import { createRequire } from "node:module"; |
| 7 | import { fileURLToPath } from "node:url"; |
| 8 | import path from "node:path"; |
| 9 | |
| 10 | const require = createRequire(import.meta.url); |
| 11 | const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright"); |
| 12 | const root = fileURLToPath(new URL("../", import.meta.url)); |
| 13 | const origin = "http://127.0.0.1:51234"; |
| 14 | const browser = await chromium.launch({ |
| 15 | headless: true, |
| 16 | executablePath: process.env.CAGE_TEST_BROWSER_PATH, |
| 17 | }); |
| 18 | const checks = []; |
| 19 | const problems = []; |
| 20 | |
| 21 | async function fixture(platform) { |
| 22 | const page = await browser.newPage(); |
| 23 | page.on("pageerror", (error) => problems.push(error.message)); |
| 24 | await page.addInitScript((value) => { |
| 25 | Object.defineProperty(navigator, "platform", { get: () => value }); |
| 26 | |
| 27 | window.WebSocket = class { |
| 28 | constructor() { throw new Error("Network sockets are forbidden in this fixture"); } |
| 29 | }; |
| 30 | }, platform); |
| 31 | await page.route("**/*", async (route) => { |
| 32 | const url = new URL(route.request().url()); |
| 33 | if (url.origin !== origin) { |
| 34 | problems.push(`Unexpected external request: ${url.origin}`); |
| 35 | await route.abort(); |
| 36 | return; |
| 37 | } |
| 38 | if (url.pathname === "/caps-lock-smoke") { |
| 39 | await route.fulfill({ contentType: "text/html", body: '<div id="remote"><canvas width="1000" height="500" tabindex="-1"></canvas></div>' }); |
| 40 | return; |
| 41 | } |
| 42 | const relative = decodeURIComponent(url.pathname).slice(1); |
| 43 | const file = path.resolve(root, relative); |
| 44 | if (!file.startsWith(root) || !/^(src|\.generated\/novnc)\//.test(relative)) { |
| 45 | problems.push(`Unexpected fixture request: ${url.pathname}`); |
| 46 | await route.abort(); |
| 47 | return; |
| 48 | } |
| 49 | try { |
| 50 | const body = await readFile(path.extname(file) ? file : `${file}.js`); |
| 51 | await route.fulfill({ contentType: "text/javascript", body }); |
| 52 | } catch (error) { |
| 53 | problems.push(`${url.pathname}: ${error.message}`); |
| 54 | await route.abort(); |
| 55 | } |
| 56 | }); |
| 57 | await page.goto(`${origin}/caps-lock-smoke`); |
| 58 | await page.evaluate(async (platform) => { |
| 59 | const [{ default: RFB }, { default: Keyboard }, { installRemoteInput }] = await Promise.all([ |
| 60 | import("/.generated/novnc/core/rfb.js"), |
| 61 | import("/.generated/novnc/core/input/keyboard.js"), |
| 62 | import("/src/remote-input.js"), |
| 63 | ]); |
| 64 | const container = document.querySelector("#remote"); |
| 65 | const rfb = new EventTarget(); |
| 66 | for (const name of ["sendKey", "_cageSendKey", "_cageQueueInput", "_handleKeyEvent", "cageSetKeyboardTarget", "focus", "blur"]) |
| 67 | rfb[name] = RFB.prototype[name].bind(rfb); |
| 68 | rfb._canvas = container.querySelector("canvas"); |
| 69 | rfb._keyboard = new Keyboard(rfb._canvas); |
| 70 | rfb._rfbConnectionState = "connected"; |
| 71 | rfb._remoteCapsLock = rfb._remoteNumLock = null; |
| 72 | let bytes = []; |
| 73 | const packets = [], observed = [], requests = []; |
| 74 | rfb._sock = { |
| 75 | sQpush8(value) { bytes.push(value); }, |
| 76 | sQpush16(value) { bytes.push((value >> 8) & 255, value & 255); }, |
| 77 | sQpush32(value) { bytes.push(value >>> 24, (value >> 16) & 255, (value >> 8) & 255, value & 255); }, |
| 78 | flush() { packets.push(bytes); bytes = []; }, |
| 79 | }; |
| 80 | window.cageNativeIMEAvailable = true; |
| 81 | window.webkit = { messageHandlers: { cage: { postMessage: async (value) => { |
| 82 | if (value.action === "clipboardRevision") return 0; |
| 83 | if (value.action !== "remoteInput") throw new Error(`Unexpected native operation: ${value.action}`); |
| 84 | requests.push(value); |
| 85 | return true; |
| 86 | } } } }; |
| 87 | const input = installRemoteInput({ |
| 88 | events: rfb, |
| 89 | setKeyboardTarget: (target) => rfb.cageSetKeyboardTarget(target), |
| 90 | sendKey: (...key) => rfb.sendKey(...key), |
| 91 | focus: () => rfb.focus(), |
| 92 | pasteText() { throw new Error("ASCII key fixture must not use paste"); }, |
| 93 | runKeyAction: (action) => rfb._cageQueueInput(() => action((...key) => rfb._cageSendKey(...key))), |
| 94 | }, container, { |
| 95 | mode: () => /mac/i.test(platform) ? "mac" : "native", |
| 96 | isActive: () => true, |
| 97 | tabId: () => "fixture-tab", |
| 98 | caretRect: async () => ({ available: true, context: "a".repeat(32), rect: [20, 20, 1, 24] }), |
| 99 | onStatus() {}, onFallback() {}, |
| 100 | insertText() { throw new Error("ASCII key fixture must not use text insertion"); }, |
| 101 | }); |
| 102 | rfb.focus(); |
| 103 | const target = container.querySelector("textarea"); |
| 104 | target.addEventListener("keydown", (event) => { |
| 105 | observed.push({ key: event.key, caps: event.getModifierState("CapsLock"), shift: event.shiftKey }); |
| 106 | }, true); |
| 107 | window.probe = { |
| 108 | requests, input, |
| 109 | run({ remoteCapsLock, qemu = false, sequence }) { |
| 110 | packets.length = 0; |
| 111 | observed.length = 0; |
| 112 | rfb._remoteCapsLock = remoteCapsLock; |
| 113 | rfb._remoteNumLock = null; |
| 114 | rfb._qemuExtKeyEventSupported = qemu; |
| 115 | const owner = requests.find((value) => value.operation === "activate").owner; |
| 116 | for (const value of sequence) { |
| 117 | if (Object.hasOwn(value, "feedback")) { |
| 118 | |
| 119 | rfb._remoteCapsLock = value.feedback; |
| 120 | } else { |
| 121 | window.dispatchEvent(new CustomEvent("cage-native-input", { detail: { owner, ...value } })); |
| 122 | } |
| 123 | } |
| 124 | return { |
| 125 | observed, |
| 126 | keys: packets.map((packet) => ({ |
| 127 | sym: ((packet[4] << 24) | (packet[5] << 16) | (packet[6] << 8) | packet[7]) >>> 0, |
| 128 | down: Boolean(packet[0] === 255 ? packet[3] : packet[1]), |
| 129 | protocol: packet[0] === 255 ? "qemu" : "rfb", |
| 130 | })), |
| 131 | }; |
| 132 | }, |
| 133 | }; |
| 134 | }, platform); |
| 135 | await page.waitForFunction(() => probe.requests.some((value) => value.operation === "activate")); |
| 136 | return page; |
| 137 | } |
| 138 | |
| 139 | const keyPair = (key, code, caps, shift = false) => ["keydown", "keyup"].map((kind) => ({ |
| 140 | kind, key, code, textKey: key.length === 1, shiftKey: shift, |
| 141 | ...(caps === undefined ? {} : { modifierCapsLock: caps }), |
| 142 | })); |
| 143 | const expectKeys = (result, expected, label, qemu) => { |
| 144 | assert.deepEqual(result.keys.map(({ sym, down }) => [sym, down]), expected, label); |
| 145 | assert.ok(result.keys.every(({ protocol }) => protocol === (qemu ? "qemu" : "rfb")), label); |
| 146 | checks.push(label); |
| 147 | }; |
| 148 | const press = (sym) => [[sym, true], [sym, false]]; |
| 149 | |
| 150 | try { |
| 151 | const windows = await fixture("Win32"); |
| 152 | for (const qemu of [false, true]) { |
| 153 | const suffix = qemu ? "QEMU" : "RFB"; |
| 154 | const run = (remoteCapsLock, sequence) => windows.evaluate((value) => probe.run(value), { remoteCapsLock, qemu, sequence }); |
| 155 | const missing = await run(true, keyPair("A", "KeyA")); |
| 156 | assert.equal(missing.observed[0].caps, false, "Missing native state defaults to false in a real KeyboardEvent"); |
| 157 | expectKeys(missing, [...press(0xffe5), ...press(65)], `negative control: omitted lock state wrongly toggles CapsLock (${suffix})`, qemu); |
| 158 | |
| 159 | const on = await run(true, keyPair("A", "KeyA", true)); |
| 160 | assert.equal(on.observed[0].caps, true); |
| 161 | expectKeys(on, press(65), `Windows CapsLock on stays on (${suffix})`, qemu); |
| 162 | const off = await run(false, keyPair("a", "KeyA", false)); |
| 163 | assert.equal(off.observed[0].caps, false); |
| 164 | expectKeys(off, press(97), `Windows CapsLock off stays off (${suffix})`, qemu); |
| 165 | |
| 166 | expectKeys(await run(false, keyPair("A", "KeyA", true)), [...press(0xffe5), ...press(65)], |
| 167 | `Windows local CapsLock on resynchronizes stale remote off (${suffix})`, qemu); |
| 168 | expectKeys(await run(true, keyPair("a", "KeyA", false)), [...press(0xffe5), ...press(97)], |
| 169 | `Windows local CapsLock off resynchronizes stale remote on (${suffix})`, qemu); |
| 170 | |
| 171 | expectKeys(await run(false, [ |
| 172 | ...keyPair("CapsLock", "CapsLock", true), { feedback: true }, ...keyPair("A", "KeyA", true), |
| 173 | ...keyPair("CapsLock", "CapsLock", false), { feedback: false }, ...keyPair("a", "KeyA", false), |
| 174 | ]), [...press(0xffe5), ...press(65), ...press(0xffe5), ...press(97)], |
| 175 | `Windows CapsLock on/off sequence has no extra toggles after LED replies (${suffix})`, qemu); |
| 176 | |
| 177 | for (const caps of [false, true]) { |
| 178 | const letter = caps ? "a" : "A"; |
| 179 | const shifted = await run(caps, [ |
| 180 | { kind: "keydown", key: "Shift", code: "ShiftLeft", modifierCapsLock: caps, shiftKey: true }, |
| 181 | ...keyPair(letter, "KeyA", caps, true), |
| 182 | { kind: "keyup", key: "Shift", code: "ShiftLeft", modifierCapsLock: caps, shiftKey: false }, |
| 183 | ]); |
| 184 | assert.ok(shifted.observed.every((event) => event.caps === caps && event.shift)); |
| 185 | expectKeys(shifted, [[0xffe1, true], ...press(letter.codePointAt(0)), [0xffe1, false]], |
| 186 | `Windows ${caps ? "CapsLock+Shift lowercase" : "Shift uppercase"} preserves modifiers (${suffix})`, qemu); |
| 187 | } |
| 188 | } |
| 189 | await windows.evaluate(() => probe.input.dispose()); |
| 190 | await windows.close(); |
| 191 | |
| 192 | const mac = await fixture("MacIntel"); |
| 193 | for (const caps of [true, false]) { |
| 194 | const letter = caps ? "A" : "a"; |
| 195 | const result = await mac.evaluate((value) => probe.run(value), { remoteCapsLock: caps, sequence: keyPair(letter, "KeyA", caps) }); |
| 196 | assert.equal(result.observed[0].caps, caps); |
| 197 | expectKeys(result, press(letter.codePointAt(0)), `existing macOS native payload preserves CapsLock ${caps ? "on" : "off"}`, false); |
| 198 | } |
| 199 | await mac.evaluate(() => probe.input.dispose()); |
| 200 | await mac.close(); |
| 201 | assert.deepEqual(problems, [], "The fixture must have no page errors or unexpected requests"); |
| 202 | console.log(JSON.stringify({ |
| 203 | passed: true, |
| 204 | checks, |
| 205 | coverage: "Real Chromium KeyboardEvent, production installRemoteInput native-event bridge, noVNC keyboard, RFB lock synchronization, and final wire encoding; in-memory transport only.", |
| 206 | limitation: "Native Windows/GTK/AppKit event production, physical keyboard LED state, and remote X11 application behavior require separate runtime acceptance. Mac platform detection is simulated in Chromium, not WebKit.", |
| 207 | }, null, 2)); |
| 208 | } finally { |
| 209 | await browser.close(); |
| 210 | } |