From ac294f5d4c571974c4f866af4a1d82f084d13594 Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:26:58 +0000 Subject: [PATCH 1/6] test(surface): cover failed select all during paste Add a test asserting that when select all fails, the paste is never pressed and the pasteboard is still restored, so staged text cannot land appended to the old contents. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- .../src/surface/surface_tests/paste_tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/tinycomputer-desktop/src/surface/surface_tests/paste_tests.rs b/crates/tinycomputer-desktop/src/surface/surface_tests/paste_tests.rs index e1efae4b2..39c5cd38f 100644 --- a/crates/tinycomputer-desktop/src/surface/surface_tests/paste_tests.rs +++ b/crates/tinycomputer-desktop/src/surface/surface_tests/paste_tests.rs @@ -244,3 +244,16 @@ fn a_pasteboard_that_cannot_be_put_back_is_reported_beside_the_delivered_text() assert!(reply.ok); assert_eq!(reply.data, Some(json!({"clipboard_restored": false}))); } + +#[test] +fn a_failed_select_all_never_presses_paste_and_still_puts_the_pasteboard_back() { + let mut steps = Steps::new(); + steps.failing.push("select"); + let reply = paste_through(&steps, "Notes", &field(&["SetValue"]), "typed"); + assert!(!reply.ok, "the select failure is the paste's failure"); + assert_eq!( + steps.calls(), + ["get", "focus", "stage", "select", "restore"], + "no paste, no settle wait: the staged text must not land appended to old text" + ); +} From f5490e559089455a27388ffb55ffeab476ad9bdf Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:27:04 +0000 Subject: [PATCH 2/6] fix(paste): skip paste when select-all fails The paste step now only runs when the preceding select-all succeeded, so a failed selection is reported instead of pasting beside the old text rather than replacing it. The outcome is computed once and passed through to restoration. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- crates/tinycomputer-desktop/src/surface/paste.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/crates/tinycomputer-desktop/src/surface/paste.rs b/crates/tinycomputer-desktop/src/surface/paste.rs index 1bfe31fce..5f4df1a4c 100644 --- a/crates/tinycomputer-desktop/src/surface/paste.rs +++ b/crates/tinycomputer-desktop/src/surface/paste.rs @@ -60,8 +60,15 @@ pub(super) fn paste_through( } else { DesktopResponse::ok("press", json!({})) }; - let pasted = steps.press(press_at(app, "cmd+v")); - let _settled = steps.wait(WaitRequest::sleep(150)); + // Without a successful select-all the paste would land beside the old + // text instead of replacing it, so skip it and report the select failure. + let outcome = if selected.ok { + let pasted = steps.press(press_at(app, "cmd+v")); + let _settled = steps.wait(WaitRequest::sleep(150)); + pasted + } else { + selected + }; let restored = restore_plan(&previous).is_none_or(|restore| { match restore { Restore::Set(request) => steps.clipboard_set(request), @@ -69,7 +76,7 @@ pub(super) fn paste_through( } .ok }); - with_restoration(if selected.ok { pasted } else { selected }, restored) + with_restoration(outcome, restored) } /// What to put back on the pasteboard once a paste is done with it. From bcb8441d7693ab814f9f6d6a6048fd299827b4cf Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:27:30 +0000 Subject: [PATCH 3/6] test(captcha): cover scan script widget answering and scroll targeting Adds node-backed tests that run the captcha scan script against a fake DOM to verify it scrolls to the first unanswered widget rather than the first visible one, treats a response field shared by two checkboxes as ambiguous, and marks a lone checkbox beside a filled field as answered. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- .../captcha/captcha_tests/scan_tests.rs | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs index ee75f6779..67bab819b 100644 --- a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs +++ b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs @@ -67,3 +67,102 @@ fn the_scan_script_looks_for_every_known_widget_and_token() { assert!(SCAN_SCRIPT.contains(needle), "{needle}"); } } + +/// A just-enough DOM to run the scan script under `node`: a tree of boxes +/// where an iframe is a widget frame and an input is a response field. +const FAKE_DOM: &str = r#" +const make = (tag, props = {}, kids = []) => { + const el = { tag, ...props, parentElement: null, children: kids, scrolled: false, + getBoundingClientRect() { return this.rect || { top: 0, left: 0, bottom: 80, right: 300, width: 300, height: 80 }; }, + scrollIntoView() { this.scrolled = true; }, + querySelectorAll(sel) { return all(this).filter((e) => matches(e, sel)); } }; + kids.forEach((k) => { k.parentElement = el; }); + return el; +}; +const all = (root) => root.children.flatMap((c) => [c, ...all(c)]); +const matches = (e, sel) => sel.split(/,(?![^\[]*\])/).some((part) => { + const attrs = [...part.matchAll(/\[(\w+)([*]?)="([^"]*)"\]/g)]; + const tag = part.match(/^[a-z]+/); + if (tag && tag[0] !== e.tag) return false; + return attrs.length > 0 && attrs.every(([, k, star, v]) => { + const have = k === "src" ? e.src : e.name; + return star ? (have || "").includes(v) : have === v; + }); +}); +const frame = (extra = {}) => make("iframe", { src: "https://www.google.com/recaptcha/api2/anchor?k=1", ...extra }); +const field = (value) => make("textarea", { name: "g-recaptcha-response", value }); +const OFF = { top: 2000, left: 0, bottom: 2080, right: 300, width: 300, height: 80 }; +const run = (body) => { + const root = make("html", {}, body); + global.window = { innerWidth: 1280, innerHeight: 800 }; + global.getComputedStyle = () => ({ visibility: "visible", display: "block", opacity: "1" }); + global.document = { querySelectorAll: (s) => root.querySelectorAll(s), + getElementsByName: (n) => all(root).filter((e) => e.name === n) }; + const result = eval(process.argv[1]); + return { result, all: all(root) }; +}; +"#; + +/// Runs the scan script over a fake page and returns `(scan, scrolled frame +/// indexes)`; `None` when `node` is not installed. +fn run_scan(page: &str) -> Option<(serde_json::Value, Vec)> { + use std::io::Write; + use std::process::{Command, Stdio}; + let program = format!( + "{FAKE_DOM}\nconst out = run({page});\n\ + const frames = out.all.filter((e) => e.tag === 'iframe');\n\ + console.log(JSON.stringify({{ scan: out.result, scrolled: frames.flatMap((f, i) => f.scrolled ? [i] : []) }}));" + ); + let mut child = Command::new("node") + .args(["-e", "process.argv[1] = require('fs').readFileSync(0, 'utf8'); eval(process.argv[2])", "x"]) + .arg(&program) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .ok()?; + child.stdin.take()?.write_all(SCAN_SCRIPT.as_bytes()).ok()?; + let output = child.wait_with_output().ok()?; + assert!(output.status.success(), "node failed"); + let parsed: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?; + let scrolled = parsed["scrolled"] + .as_array()? + .iter() + .filter_map(|i| i.as_u64().map(|i| i as usize)) + .collect(); + Some((parsed["scan"].clone(), scrolled)) +} + +#[test] +fn the_scan_scrolls_to_the_first_unanswered_checkbox_not_the_first_shown_one() { + // An answered checkbox in view, then an unanswered one far below. + let page = "[make('div', {}, [frame(), field('token')]), \ + make('div', {}, [frame({ rect: OFF }), field('')])]"; + let Some((scan, scrolled)) = run_scan(page) else { + return; + }; + assert_eq!(scrolled, [1], "the waiting widget is scrolled to, not the answered one"); + assert_eq!(scan["widgets"][0]["answered"], true); + assert_eq!(scan["widgets"][1]["answered"], false); + assert_eq!(scan["solved"], false); +} + +#[test] +fn a_response_field_shared_by_two_checkboxes_does_not_answer_either() { + let page = "[make('div', {}, [frame(), frame(), field('token')])]"; + let Some((scan, _)) = run_scan(page) else { + return; + }; + assert_eq!(scan["widgets"][0]["answered"], false, "ownership is ambiguous"); + assert_eq!(scan["widgets"][1]["answered"], false); + assert_eq!(scan["solved"], false); +} + +#[test] +fn a_lone_checkbox_beside_a_filled_field_is_answered() { + let page = "[make('div', {}, [frame(), field('token')])]"; + let Some((scan, _)) = run_scan(page) else { + return; + }; + assert_eq!(scan["widgets"][0]["answered"], true); + assert_eq!(scan["solved"], true); +} From 2f341e56b0e19ec72c3c35158475389210e671da Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:27:51 +0000 Subject: [PATCH 4/6] fix(captcha): skip answered checkboxes when scrolling and treat shared fields as unanswered The scan now scrolls to the first visible checkbox that is still unanswered rather than the first shown one, and a response field shared by several checkboxes no longer marks any of them as answered. The token lookup moved ahead of the scroll decision so it can be reused, and the doc comment was updated to match. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- .../captcha/captcha_tests/scan_tests.rs | 17 +++++++++++--- .../src/agentic/captcha/scan.rs | 23 ++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs index 67bab819b..6818a98c9 100644 --- a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs +++ b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs @@ -114,7 +114,11 @@ fn run_scan(page: &str) -> Option<(serde_json::Value, Vec)> { console.log(JSON.stringify({{ scan: out.result, scrolled: frames.flatMap((f, i) => f.scrolled ? [i] : []) }}));" ); let mut child = Command::new("node") - .args(["-e", "process.argv[1] = require('fs').readFileSync(0, 'utf8'); eval(process.argv[2])", "x"]) + .args([ + "-e", + "process.argv[1] = require('fs').readFileSync(0, 'utf8'); eval(process.argv[2])", + "x", + ]) .arg(&program) .stdin(Stdio::piped()) .stdout(Stdio::piped()) @@ -140,7 +144,11 @@ fn the_scan_scrolls_to_the_first_unanswered_checkbox_not_the_first_shown_one() { let Some((scan, scrolled)) = run_scan(page) else { return; }; - assert_eq!(scrolled, [1], "the waiting widget is scrolled to, not the answered one"); + assert_eq!( + scrolled, + [1], + "the waiting widget is scrolled to, not the answered one" + ); assert_eq!(scan["widgets"][0]["answered"], true); assert_eq!(scan["widgets"][1]["answered"], false); assert_eq!(scan["solved"], false); @@ -152,7 +160,10 @@ fn a_response_field_shared_by_two_checkboxes_does_not_answer_either() { let Some((scan, _)) = run_scan(page) else { return; }; - assert_eq!(scan["widgets"][0]["answered"], false, "ownership is ambiguous"); + assert_eq!( + scan["widgets"][0]["answered"], false, + "ownership is ambiguous" + ); assert_eq!(scan["widgets"][1]["answered"], false); assert_eq!(scan["solved"], false); } diff --git a/crates/tinycomputer-engine/src/agentic/captcha/scan.rs b/crates/tinycomputer-engine/src/agentic/captcha/scan.rs index 66b0e7052..067943adb 100644 --- a/crates/tinycomputer-engine/src/agentic/captcha/scan.rs +++ b/crates/tinycomputer-engine/src/agentic/captcha/scan.rs @@ -11,7 +11,8 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; /// The script the scan evaluates in the top document. It scrolls a widget -/// that is off screen into view, only when no challenge is showing, and +/// that is off screen and still unanswered into view, only when no challenge +/// is showing, and /// otherwise changes nothing. pub(super) const SCAN_SCRIPT: &str = r#"(() => { const kinds = [ @@ -33,24 +34,30 @@ pub(super) const SCAN_SCRIPT: &str = r#"(() => { for (const [vendor, role, selector] of kinds) { for (const el of document.querySelectorAll(selector)) found.push({ vendor, role, el }); } - const challenge = found.some((w) => w.role === "challenge" && shown(w.el) - && inView(w.el.getBoundingClientRect())); - if (!challenge) { - const box = found.find((w) => w.role === "checkbox" && shown(w.el)); - if (box && !inView(box.el.getBoundingClientRect())) box.el.scrollIntoView({ block: "center" }); - } const names = ["g-recaptcha-response", "h-captcha-response", "cf-turnstile-response"]; + const checkboxSelector = kinds.filter(([, role]) => role === "checkbox").map(([, , sel]) => sel).join(","); const filled = (field) => typeof field.value === "string" && field.value.length > 0; // A widget's own response field sits beside its frame, so look only in the // nearest few ancestors that hold one: another widget's token is not its. + // When that ancestor holds several checkboxes the field could belong to + // any of them, so none counts as answered. const holdsToken = (el) => { let node = el.parentElement; for (let depth = 0; node && depth < 4; depth++, node = node.parentElement) { const fields = names.flatMap((name) => Array.from(node.querySelectorAll(`[name="${name}"]`))); - if (fields.length > 0) return fields.some(filled); + if (fields.length > 0) { + if (node.querySelectorAll(checkboxSelector).length > 1) return false; + return fields.some(filled); + } } return false; }; + const challenge = found.some((w) => w.role === "challenge" && shown(w.el) + && inView(w.el.getBoundingClientRect())); + if (!challenge) { + const box = found.find((w) => w.role === "checkbox" && shown(w.el) && !holdsToken(w.el)); + if (box && !inView(box.el.getBoundingClientRect())) box.el.scrollIntoView({ block: "center" }); + } const widgets = found.map(({ vendor, role, el }) => { const r = el.getBoundingClientRect(); return { vendor, role, visible: shown(el) && inView(r), From 1e5541ca20bc1620b309436e7c01748cc7efd0bc Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:27:59 +0000 Subject: [PATCH 5/6] docs(captcha): reflow scan script doc comment Joined the wrapped line in the SCAN_SCRIPT doc comment so the sentence reads continuously. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- crates/tinycomputer-engine/src/agentic/captcha/scan.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/tinycomputer-engine/src/agentic/captcha/scan.rs b/crates/tinycomputer-engine/src/agentic/captcha/scan.rs index 067943adb..cbcd98706 100644 --- a/crates/tinycomputer-engine/src/agentic/captcha/scan.rs +++ b/crates/tinycomputer-engine/src/agentic/captcha/scan.rs @@ -12,8 +12,7 @@ use serde_json::Value; /// The script the scan evaluates in the top document. It scrolls a widget /// that is off screen and still unanswered into view, only when no challenge -/// is showing, and -/// otherwise changes nothing. +/// is showing, and otherwise changes nothing. pub(super) const SCAN_SCRIPT: &str = r#"(() => { const kinds = [ ["recaptcha", "checkbox", 'iframe[src*="/recaptcha/"][src*="/anchor"]'], From de5322cb47334ab473f45dbd9ed9f354f84d199e Mon Sep 17 00:00:00 2001 From: Steven Enamakel Date: Sat, 14 Nov 2026 16:28:59 +0000 Subject: [PATCH 6/6] test(captcha): use checked conversion for scrolled indices in scan tests Replace the unchecked `as usize` cast with `usize::try_from` so the test helper cannot silently truncate values on platforms with narrower pointer widths. Auto-committed-on: cuttlefish Co-authored-by: Medulla --- .../src/agentic/captcha/captcha_tests/scan_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs index 6818a98c9..fc8b412d0 100644 --- a/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs +++ b/crates/tinycomputer-engine/src/agentic/captcha/captcha_tests/scan_tests.rs @@ -131,7 +131,7 @@ fn run_scan(page: &str) -> Option<(serde_json::Value, Vec)> { let scrolled = parsed["scrolled"] .as_array()? .iter() - .filter_map(|i| i.as_u64().map(|i| i as usize)) + .filter_map(|i| i.as_u64().and_then(|i| usize::try_from(i).ok())) .collect(); Some((parsed["scan"].clone(), scrolled)) }