From ec23b553a245d3d57f428d03002ed77a785d300a Mon Sep 17 00:00:00 2001 From: Tianren Dong Date: Thu, 10 Sep 2026 09:31:19 -0400 Subject: Improve Chrome input reliability and release 0.15.49 --- test-suite/README.md | 9 + test-suite/challenges/21-keyboard-modifiers.html | 7 +- test-suite/challenges/44-input-reliability.html | 105 ++++++ test-suite/manifest.json | 46 ++- test-suite/unit/background-policy.test.mjs | 20 ++ test-suite/unit/input-reliability.test.mjs | 401 +++++++++++++++++++++++ 6 files changed, 584 insertions(+), 4 deletions(-) create mode 100644 test-suite/challenges/44-input-reliability.html create mode 100644 test-suite/unit/input-reliability.test.mjs (limited to 'test-suite') diff --git a/test-suite/README.md b/test-suite/README.md index 59d2388..c7321a1 100644 --- a/test-suite/README.md +++ b/test-suite/README.md @@ -177,6 +177,15 @@ The dashboard renders this from `manifest.json`. In brief: 41. explicit tab lifecycle 42. strict CSP eval/snapshot via CDP (regression guard for the CSP bypass) 43. hard background: inactive-tab visibility and trusted input +44. input reliability: native rich-editor insertion, full replacement, per-character override, Shift chords, single Enter, and file upload + +### Input regressions (16, 21, 31, 44) + +Challenge 44 exercises `chrome_type`/`chrome_fill` bulk insertion into contenteditables, including long Unicode text and multiple paragraphs. It also checks `perCharacter:true`, Shift-only printable keys, one Enter per `pressEnter`, and real file contents. Read `window.__inputFixture.caption` and follow its manifest recipe. Use `domFallback:false` to ensure failures are not hidden by synthetic events. + +Challenge 16 intentionally requests `perCharacter:true`: its grader requires a caret update for each keystroke. Challenge 21 focuses its field before sending Shift+a and grades after Shift release, not during the earlier input event. Challenge 31 retains standalone upload coverage. + +`npm test` also fault-injects zero/missing/rejected upload node IDs, attachment/cleanup failures, stale UIDs, insertion failures, shortcut suppression, and authorization/option forwarding. These mocked tests cannot prove `isTrusted` or real browser selection behavior; run the browser challenges too. ### Hard-background regression (43) diff --git a/test-suite/challenges/21-keyboard-modifiers.html b/test-suite/challenges/21-keyboard-modifiers.html index 870bef6..d18d484 100644 --- a/test-suite/challenges/21-keyboard-modifiers.html +++ b/test-suite/challenges/21-keyboard-modifiers.html @@ -30,9 +30,12 @@ for (const name of ["keydown","keypress","keyup","input"]) { }); } -t.addEventListener("input", () => { - if (t.value !== "A") return; +// input fires before keyup. Grade only when Shift is released so a valid chord +// cannot fail merely because its release events have not happened yet. +t.addEventListener("keyup", (event) => { + if (event.key !== "Shift") return; const bad = []; + if (t.value !== "A") bad.push(`value=${JSON.stringify(t.value)} (need 'A')`); const downShift = log.find(e => e.name === "keydown" && e.key === "Shift"); const downA = log.find(e => e.name === "keydown" && e.code === "KeyA"); const upShift = log.find(e => e.name === "keyup" && e.key === "Shift"); diff --git a/test-suite/challenges/44-input-reliability.html b/test-suite/challenges/44-input-reliability.html new file mode 100644 index 0000000..7576376 --- /dev/null +++ b/test-suite/challenges/44-input-reliability.html @@ -0,0 +1,105 @@ + + +44 input reliability + + + + +
+

Upload and text-input reliability

+

Read window.__inputFixture.caption. Type it into the empty editor, + then fill the old multi-paragraph editor with the same text. Leave + perCharacter off for both. Native text insertion must preserve + Unicode, newlines, and literal markup without hundreds of key events.

+
Exact caption (manual baseline: copy and paste into both editors)
+ +
+ +
OLD first paragraph
OLD second paragraph
OLD third paragraph
+ +
+ + +
+ + + +
+ + +

Node-ID conversion failure is fault-injected by the Node unit tests; this page + checks the real file contents and native upload event.

+ +
+ + diff --git a/test-suite/manifest.json b/test-suite/manifest.json index 4f72c85..cd6d34e 100644 --- a/test-suite/manifest.json +++ b/test-suite/manifest.json @@ -491,7 +491,7 @@ "id": "contenteditable-selection", "file": "challenges/16-contenteditable-selection.html", "category": "editing", - "goal": "Contenteditable typing advances selection/caret like user typing.", + "goal": "Contenteditable perCharacter typing advances selection/caret for each key (bulk insertion is covered by challenge 44).", "expected": { "synthetic": "CONDITIONAL", "trusted": "PASS", @@ -502,7 +502,8 @@ "tool": "chrome_type", "params": { "selector": "#ed", - "text": "hello" + "text": "hello", + "perCharacter": true } } ], @@ -687,6 +688,10 @@ "manual": "PASS" }, "recipe": [ + { + "tool": "chrome_click", + "params": { "selector": "#t", "domFallback": false } + }, { "tool": "chrome_key", "params": { @@ -1752,5 +1757,42 @@ "flakeRisk": "medium", "manualBaseline": "unverified", "gradeSource": "both" + }, + { + "id": "input-reliability", + "file": "challenges/44-input-reliability.html", + "category": "editing", + "difficulty": "L2", + "gate": "core", + "goal": "Upload a fixture, enter long Unicode rich-editor text, replace all old paragraphs, retain per-character typing, type Shift-only chords, and submit exactly once.", + "expected": { "synthetic": "FAIL", "trusted": "PASS", "manual": "PASS" }, + "prerequisites": [ + "Reload Pi and the updated Chrome companion. Snapshot before input; replace descriptive selectors with snapshot UIDs where available.", + "Substitute the chrome_evaluate caption result for $CAPTION and expand $PWD to the repository root." + ], + "recipe": [ + { "tool": "chrome_evaluate", "params": { "expression": "window.__inputFixture.caption" } }, + { "tool": "chrome_type", "params": { "selector": "#typed", "text": "$CAPTION", "includeSnapshot": true } }, + { "tool": "chrome_fill", "params": { "selector": "#filled", "text": "$CAPTION", "domFallback": false, "includeSnapshot": true } }, + { "tool": "chrome_type", "params": { "selector": "#keys", "text": "Ab!", "perCharacter": true, "includeSnapshot": true } }, + { "tool": "chrome_click", "params": { "selector": "#shift", "domFallback": false } }, + { "tool": "chrome_key", "params": { "key": "a", "modifiers": { "shiftKey": true } } }, + { "tool": "chrome_key", "params": { "key": "1", "modifiers": { "shiftKey": true } } }, + { "tool": "chrome_key", "params": { "key": "/", "modifiers": { "shiftKey": true }, "includeSnapshot": true } }, + { "tool": "chrome_type", "params": { "selector": "#once", "text": "done", "pressEnter": true, "includeSnapshot": true } }, + { "tool": "chrome_upload_file", "params": { "selector": "#file", "paths": ["$PWD/test-suite/fixtures/pi-chrome-upload.txt"] } }, + { "tool": "chrome_click", "params": { "selector": "#verify", "domFallback": false, "includeSnapshot": true } }, + { "tool": "chrome_evaluate", "params": { "expression": "JSON.stringify({v:window.__verdict,r:window.__reason,e:window.__events.slice(-20)})" } } + ], + "requires": { "cdp": true }, + "tags": ["upload", "contenteditable", "insertText", "keyboard", "unicode", "modifiers"], + "notes": [ + "Bulk insertion emits native editing events, not a keydown per character or a clipboard paste event. Per-character behavior remains covered by challenge 16 and the explicit keys editor here.", + "Manual baseline: paste the exact caption into both editors (select all old paragraphs first), type Ab!, release Shift between each Shift chord, type done then Enter once, and choose the fixture file.", + "The page checks actual file contents/native change. Node-ID conversion failure and cleanup are covered by fault-injected worker unit tests, not forced by the page." + ], + "flakeRisk": "low", + "manualBaseline": "unverified", + "gradeSource": "page" } ] diff --git a/test-suite/unit/background-policy.test.mjs b/test-suite/unit/background-policy.test.mjs index e20d0ab..e78fa49 100644 --- a/test-suite/unit/background-policy.test.mjs +++ b/test-suite/unit/background-policy.test.mjs @@ -119,6 +119,26 @@ test("every registered page tool, tab.new, and chrome_launch(url) use the centra assert.equal(h.writes[0][1].toString(), "test", "screenshot tool still writes decoded bytes"); }); +test("typing options and verification pass through real tool registrations without bypassing authorization", async () => { + const h = piHarness({ send: async () => ({ result: { input: "chrome", typing: "keys" }, snapshot: { url: "https://fixture.test" } }) }); + for (const name of ["chrome_type", "chrome_fill"]) { + assert.equal(h.tools.get(name).parameters.perCharacter.default, false); + const result = await h.tool(name, { uid: "el-1", text: "hello", perCharacter: true, includeSnapshot: true, domFallback: false }); + assert.equal(h.calls.at(-1).params.perCharacter, true); + assert.equal(h.calls.at(-1).params.includeSnapshot, true); + assert.equal(h.calls.at(-1).params.domFallback, false); + assert.equal(h.calls.at(-1).params.background, true); + assert.equal(result.details.result.result.typing, "keys"); + assert.equal(result.details.result.snapshot.url, "https://fixture.test"); + } + h.authorize(false); + const count = h.calls.length; + for (const [name, params] of [["chrome_type", { text: "x" }], ["chrome_fill", { text: "x" }], ["chrome_key", { key: "a" }], ["chrome_upload_file", { paths: ["/fixture.txt"] }]]) { + await assert.rejects(h.tool(name, params), /Chrome control locked/); + } + assert.equal(h.calls.length, count); +}); + test("activation is rejected before dispatch; existing on/off/toggle/status commands suffice", async () => { const h = piHarness(); await assert.rejects(h.tool("chrome_tab", { action: "activate", targetId: "2", background: false }), /background mode.*\/chrome background off/); diff --git a/test-suite/unit/input-reliability.test.mjs b/test-suite/unit/input-reliability.test.mjs new file mode 100644 index 0000000..1329ed7 --- /dev/null +++ b/test-suite/unit/input-reliability.test.mjs @@ -0,0 +1,401 @@ +// Exercise the shipped worker against mocked Chrome APIs and a separate page world. +// This checks command routing and failure cleanup, not browser isTrusted semantics. +// Live input/selection fidelity is covered by challenges 16, 21, 31, and 44. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import vm from "node:vm"; +import { test } from "node:test"; + +const workerSource = fs.readFileSync(new URL("../../extensions/chrome-profile-bridge/browser-extension/service_worker.js", import.meta.url), "utf8"); +const clone = (value) => JSON.parse(JSON.stringify(value)); + +function harness({ tag = "DIV", editable = true, initial = "", nodeId = 7 } = {}) { + const calls = [], selected = [], files = [], tabLookups = []; + let selectAll = false; + const element = { + tagName: tag, type: tag === "INPUT" ? "text" : undefined, + isContentEditable: editable, isConnected: true, textContent: initial, + scrollIntoView() {}, getBoundingClientRect: () => ({ left: 10, top: 10, width: 200, height: 60 }), + contains: (el) => el === element, + }; + const selection = { + removeAllRanges() { selectAll = false; }, + addRange(range) { selected.push(range.target); selectAll = range.target === element; }, + }; + const page = vm.createContext({ + document: { + activeElement: element, + querySelector: (selector) => selector === "#target" ? element : null, + createRange: () => ({ selectNodeContents(target) { this.target = target; } }), + }, + getSelection: () => selection, + location: { href: "https://fixture.test/" }, + __PI_CHROME_STATE__: { elements: { "el-1": element } }, + }); + page.window = page; + const listener = { addListener() {}, removeListener() {} }; + const chrome = { + runtime: { id: "test", getManifest: () => ({ version: "0.0.0" }), onInstalled: listener, onStartup: listener }, + alarms: { create() {}, onAlarm: listener }, action: { onClicked: listener }, webNavigation: { onCommitted: listener }, + debugger: { onDetach: listener }, + scripting: { executeScript: async ({ func, args = [] }) => { + calls.push({ method: "scripting.executeScript" }); + const fn = vm.runInContext(`(${func.toString()})`, page); + return [{ result: await fn(...args) }]; + } }, + }; + const worker = { + chrome, console, setTimeout, clearTimeout, setInterval: () => 0, + navigator: { userAgent: "unit-test" }, fetch: async () => { throw new Error("no network in unit tests"); }, + }; + worker.self = worker; + vm.runInNewContext(workerSource, worker); + worker.sleep = async () => {}; + worker.getTabByParams = async (params) => { + tabLookups.push(clone(params)); + return { id: Number(params.targetId ?? 2), windowId: 1 }; + }; + worker.bringToFront = async () => {}; + worker.attachDebugger = async () => {}; + worker.cdpMoveTo = async () => {}; + worker.cdp = async (tabId, method, params = {}) => { + calls.push({ tabId, method, params: clone(params) }); + const error = h.failures.get(method); + if (error) throw new Error(error); + if (method === "Runtime.evaluate") { + try { + const result = vm.runInContext(params.expression, page); + if (result === element) return { result: { objectId: "upload-object" } }; + return { result: { value: clone(result) } }; + } catch (error) { + return { exceptionDetails: { text: error.message, exception: { description: error.message } } }; + } + } + if (method === "DOM.requestNode") return h.nodeResult; + if (method === "DOM.setFileInputFiles") files.push(...params.files); + if (method === "Input.insertText") element.textContent += params.text; + if (method === "Input.dispatchKeyEvent" && params.type === "keyDown") { + if (params.key === "Delete") { + // Triple-click selects a paragraph, not a whole multi-paragraph editor. + element.textContent = selectAll ? "" : element.textContent.replace(/[^\n]*$/, ""); + selectAll = false; + } else if (params.text) element.textContent += params.text; + } + return {}; + }; + worker.domFillFallback = async (_tabId, params) => { + calls.push({ method: "domFillFallback" }); + element.textContent = params.text; + return { input: "dom-fallback" }; + }; + const h = { + worker, chrome, page, calls, element, selected, files, tabLookups, failures: new Map(), nodeResult: { nodeId }, + call: (action, params = {}) => worker.dispatch(`page.${action}`, { targetId: "2", background: true, ...params }), + commands: (method) => calls.filter((call) => call.method === method), + }; + return h; +} + +for (const [name, nodeResult] of [["node ID", { nodeId: 7 }], ["zero node ID", { nodeId: 0 }], ["missing node ID", {}], ["no response", undefined]]) { + test(`upload accepts ${name} and releases the remote object`, async () => { + const h = harness({ tag: "INPUT", editable: false }); + h.element.type = "file"; + h.nodeResult = nodeResult; + const result = await h.call("upload", { uid: "el-1", paths: ["/tmp/a.txt", "/tmp/b.txt"] }); + const target = nodeResult?.nodeId ? { nodeId: 7 } : { objectId: "upload-object" }; + assert.deepEqual(h.commands("DOM.setFileInputFiles").map((c) => c.params), [{ ...target, files: ["/tmp/a.txt", "/tmp/b.txt"] }]); + assert.deepEqual(h.files, ["/tmp/a.txt", "/tmp/b.txt"]); + assert.equal(result.input, "chrome"); + assert.deepEqual(clone(result.uploaded), [{ path: "/tmp/a.txt" }, { path: "/tmp/b.txt" }]); + assert.deepEqual(h.commands("Runtime.releaseObject").map((c) => c.params), [{ objectId: "upload-object" }]); + assert.equal(h.calls.at(-1).method, "Runtime.releaseObject"); + }); +} + +test("upload falls back on requestNode failure, not on a failed file attachment", async () => { + const h = harness({ tag: "INPUT", editable: false }); + h.element.type = "file"; + h.failures.set("DOM.requestNode", "node conversion unavailable"); + await h.call("upload", { selector: "#target", paths: ["/tmp/a.txt"] }); + assert.deepEqual(h.commands("DOM.setFileInputFiles")[0].params, { objectId: "upload-object", files: ["/tmp/a.txt"] }); + h.calls.length = 0; + h.failures.set("DOM.setFileInputFiles", "attachment denied"); + h.failures.set("Runtime.releaseObject", "target closed during cleanup"); + await assert.rejects(h.call("upload", { selector: "#target", paths: ["/tmp/a.txt"] }), /attachment denied/); + assert.equal(h.commands("DOM.setFileInputFiles").length, 1, "no object-ID retry after an uncertain file attachment"); + assert.equal(h.commands("Runtime.releaseObject").length, 1, "cleanup still runs on error"); + assert.equal(h.commands("Runtime.callFunctionOn").length, 0, "no notification after failed attachment"); +}); + +test("upload releases valid node-path references on failure and tolerates notification/cleanup failure", async () => { + const h = harness({ tag: "INPUT", editable: false }); + h.element.type = "file"; + h.failures.set("DOM.setFileInputFiles", "file inaccessible"); + await assert.rejects(h.call("upload", { selector: "#target", paths: ["/tmp/a.txt"] }), /file inaccessible/); + assert.equal(h.commands("Runtime.releaseObject").length, 1); + h.failures.delete("DOM.setFileInputFiles"); + for (const method of ["DOM.enable", "Runtime.callFunctionOn", "Runtime.releaseObject"]) h.failures.set(method, "optional step failed"); + const result = await h.call("upload", { selector: "#target", paths: ["/tmp/a.txt"] }); + assert.equal(result.input, "chrome"); +}); + +test("upload rejects non-file elements, missing targets/paths, and stale UIDs without attaching anything", async () => { + const h = harness(); + await assert.rejects(h.call("upload", { selector: "#target", paths: ["/tmp/a.txt"] }), /Target must be /); + await assert.rejects(h.call("upload", { paths: ["/tmp/a.txt"] }), /selector or uid required/); + await assert.rejects(h.call("upload", { selector: "#target", paths: [] }), /no file paths/); + h.element.tagName = "INPUT"; + h.element.type = "file"; + h.element.isConnected = false; + await assert.rejects(h.call("upload", { uid: "el-1", selector: "#target", paths: ["/tmp/a.txt"] }), /snapshot uid el-1 is stale/); + await assert.rejects(h.call("upload", { uid: "missing", selector: "#target", paths: ["/tmp/a.txt"] }), /snapshot uid missing is stale/); + assert.equal(h.commands("DOM.setFileInputFiles").length, 0); +}); + +const caption = "Long caption: café, 中文, 👩🏽‍💻. Two spaces.\nSecond paragraph! ".repeat(12); +for (const target of [{ selector: "#target" }, { uid: "el-1" }, {}]) { + test(`contenteditable typing uses one native insertText (${Object.keys(target)[0] ?? "already focused"})`, async () => { + const h = harness(); + const result = await h.call("type", { ...target, text: caption }); + assert.deepEqual(h.commands("Input.insertText").map((c) => c.params), [{ text: caption }]); + assert.equal(h.commands("Input.dispatchKeyEvent").length, 0); + assert.equal(h.element.textContent, caption); + assert.equal(result.input, "chrome"); + assert.equal(result.length, caption.length); + assert.equal(result.typing, "insertText"); + }); +} + +for (const perCharacter of [false, true]) { + test(`contenteditable fill replaces every paragraph (perCharacter=${perCharacter})`, async () => { + const h = harness({ initial: "First paragraph\nSecond paragraph\nThird paragraph" }); + const text = perCharacter ? "Hello!" : caption; + const result = await h.call("fill", { uid: "el-1", text, perCharacter, domFallback: false }); + assert.equal(h.selected.length, 1); + assert.equal(h.selected[0], h.element); + assert.equal(h.element.textContent, text); + assert.equal(result.typing, perCharacter ? "keys" : "insertText"); + const deletion = h.calls.findIndex((c) => c.method === "Input.dispatchKeyEvent" && c.params.key === "Delete"); + const insertion = h.calls.findIndex((c) => c.method === "Input.insertText" || (c.method === "Input.dispatchKeyEvent" && c.params.text)); + assert.ok(deletion >= 0 && deletion < insertion); + assert.equal(h.commands("domFillFallback").length, 0); + }); +} + +test("fill refuses to select an unrelated focused editor", async () => { + const h = harness({ initial: "keep this" }); + h.page.document.activeElement = { isContentEditable: true, contains: () => false }; + await assert.rejects(h.call("fill", { selector: "#target", text: "replace", domFallback: false }), /requested contenteditable is not focused/); + assert.equal(h.selected.length, 0); + assert.equal(h.commands("Input.dispatchKeyEvent").length, 0, "no Delete in the wrong editor"); + assert.equal(h.element.textContent, "keep this"); +}); + +test("focused-editor inspection failures do not silently switch to keystrokes", async () => { + const h = harness(); + h.chrome.scripting.executeScript = async () => { throw new Error("inspection denied"); }; + await assert.rejects(h.call("type", { text: "hello" }), /inspection denied/); + assert.equal(h.commands("Input.insertText").length, 0); + assert.equal(h.commands("Input.dispatchKeyEvent").length, 0); +}); + +test("perCharacter preserves keydown-dependent editors; ordinary inputs/textareas retain key events", async () => { + for (const options of [{}, { tag: "INPUT", editable: false }, { tag: "TEXTAREA", editable: false }, { tag: "DIV", editable: false }]) { + const h = harness(options); + await h.call("type", { text: "Aa.!", ...(options.tag ? {} : { perCharacter: true }) }); + assert.equal(h.commands("Input.insertText").length, 0); + assert.equal(h.element.textContent, "Aa.!"); + assert.deepEqual(h.commands("Input.dispatchKeyEvent").filter((c) => c.params.text).map((c) => c.params.text), ["A", "a", ".", "!"]); + } +}); + +test("empty typing is a no-op; empty fill still deletes the entire editor", async () => { + const h = harness({ initial: "first\nsecond" }); + await h.call("type", { text: "" }); + assert.equal(h.element.textContent, "first\nsecond"); + assert.equal(h.commands("Input.insertText").length, 0); + assert.equal(h.commands("Input.dispatchKeyEvent").length, 0); + await h.call("fill", { selector: "#target", text: "", domFallback: false }); + assert.equal(h.element.textContent, ""); + assert.equal(h.commands("Input.insertText").length, 0); +}); + +test("insertText failures propagate without silently retrying keystrokes; fill honors domFallback:false", async () => { + for (const action of ["type", "fill"]) { + const h = harness(); + h.failures.set("Input.insertText", "insertion failed"); + await assert.rejects(h.call(action, { selector: "#target", text: "hello", domFallback: false }), /insertion failed/); + assert.equal(h.commands("Input.insertText").length, 1); + assert.equal(h.commands("Input.dispatchKeyEvent").filter((c) => c.params.text).length, 0); + assert.equal(h.commands("domFillFallback").length, 0); + } + const h = harness(); + h.failures.set("Input.insertText", "insertion failed"); + const result = await h.call("fill", { selector: "#target", text: "hello" }); + assert.equal(result.input, "dom-fallback", "existing opt-out fallback remains available"); +}); + +test("stale typing/fill UIDs never insert text or delete the old selection", async () => { + for (const action of ["type", "fill"]) { + const h = harness(); + h.element.isConnected = false; + await assert.rejects(h.call(action, { uid: "el-1", text: "hello", domFallback: false }), /snapshot uid el-1 is stale/); + assert.equal(h.commands("Input.insertText").length, 0); + assert.equal(h.commands("Input.dispatchKeyEvent").length, 0); + } +}); + +for (const [key, text, code, vk] of [ + ["a", "A", "KeyA", 65], ["A", "A", "KeyA", 65], ["1", "!", "Digit1", 49], + ["!", "!", "Digit1", 49], ["2", "@", "Digit2", 50], [".", ">", "Period", 190], + ["/", "?", "Slash", 191], [" ", " ", "Space", 32], +]) { + test(`Shift+${key} emits ${text} with the correct physical key and text`, async () => { + const h = harness({ tag: "INPUT", editable: false }); + const result = await h.call("key", { key, modifiers: { shiftKey: true } }); + const events = h.commands("Input.dispatchKeyEvent").map((c) => c.params); + assert.deepEqual(events.map((e) => [e.type, e.key]), [["keyDown", "Shift"], ["keyDown", text], ["keyUp", text], ["keyUp", "Shift"]]); + assert.equal(events[1].text, text); + assert.equal(events[1].unmodifiedText, text); + assert.equal(events[1].code, code); + assert.equal(events[1].windowsVirtualKeyCode, vk); + assert.equal(events[1].modifiers, 8); + assert.equal(events.at(-1).modifiers, 0); + assert.equal(h.element.textContent, text); + assert.equal(result.key, text); + }); +} + +test("every US shifted digit/punctuation maps without changing its physical code", () => { + const { worker } = harness(); + const unshifted = "`1234567890-=[]\\;',./"; + const shifted = '~!@#$%^&*()_+{}|:"<>?'; + assert.equal(unshifted.length, shifted.length); + for (let i = 0; i < unshifted.length; i++) { + const base = worker.cdpKeyInfo(unshifted[i]); + const shift = worker.cdpKeyInfo(unshifted[i], true); + assert.equal(shift.text, shifted[i]); + assert.equal(shift.code, base.code); + assert.equal(shift.windowsVirtualKeyCode, base.windowsVirtualKeyCode); + } +}); + +test("Ctrl/Meta/Alt shortcuts never insert their literal character, even with Shift", async () => { + for (const modifier of ["ctrlKey", "metaKey", "altKey"]) { + for (const shiftKey of [false, true]) { + const h = harness(); + await h.call("key", { key: "a", modifiers: { [modifier]: true, shiftKey } }); + const down = h.commands("Input.dispatchKeyEvent").find((c) => c.params.code === "KeyA").params; + assert.equal(down.type, "rawKeyDown"); + assert.equal(down.text, ""); + assert.equal(down.unmodifiedText, ""); + assert.equal(h.element.textContent, ""); + } + } +}); + +test("named keys keep codes and Shift+Enter carries newline text", async () => { + for (const [key, code, text] of [["ArrowLeft", "ArrowLeft", ""], ["Tab", "Tab", "\t"], ["Enter", "Enter", "\r"], ["Escape", "Escape", ""]]) { + const h = harness(); + await h.call("key", { key, modifiers: { shiftKey: true } }); + const down = h.commands("Input.dispatchKeyEvent").find((c) => c.params.key === key).params; + assert.equal(down.code, code); + assert.equal(down.text, text); + } +}); + +test("challenge 21 waits for releases, accepts a complete trusted chord, and rejects missing text/synthetic input", () => { + const html = fs.readFileSync(new URL("../challenges/21-keyboard-modifiers.html", import.meta.url), "utf8"); + const script = html.match(/