Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions crates/tinycomputer-desktop/src/surface/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,23 @@ 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),
Restore::Clear => steps.clipboard_clear(),
}
.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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,113 @@ 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<usize>)> {
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().and_then(|i| usize::try_from(i).ok()))
.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);
}
24 changes: 15 additions & 9 deletions crates/tinycomputer-engine/src/agentic/captcha/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +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
/// otherwise changes nothing.
/// 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 = [
["recaptcha", "checkbox", 'iframe[src*="/recaptcha/"][src*="/anchor"]'],
Expand All @@ -33,24 +33,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),
Expand Down
Loading