Skip to main content

iphone_cli/
script.rs

1//! Scripts: JavaScript that drives the phone.
2//!
3//! `iph script` runs code as it is given; `iph run <name>` runs a saved one,
4//! from `~/.config/iph/scripts/` or from the examples in this repository's
5//! `scripts/` folder (which are built into the binary). A script calls the
6//! commands the CLI has (`read`, `tap`, `scroll` …) as functions and gets the
7//! same result objects back. A call that fails throws.
8//!
9//! The engine is QuickJS. A script has no files, no network and no
10//! processes: only the functions set up below. It runs on a thread of its
11//! own, and every call crosses over to the daemon's task, where it runs
12//! exactly as a command typed at the shell.
13//!
14//! A script cannot go on after a call whose input may or may not have
15//! reached the phone. The run ends there, with `outcome: "unknown"`.
16
17use std::{
18    sync::{
19        Arc, Mutex,
20        atomic::{AtomicBool, Ordering},
21    },
22    time::{Duration, Instant},
23};
24
25use rquickjs::{CaughtError, Context, Function, Runtime, context::EvalOptions};
26use serde_json::{Map, Value, json};
27
28use crate::{
29    actions::{Target, TapArgs, brief, parse_args, parse_target},
30    ax::tree::{BuildOptions, Screen},
31    device::session::Session,
32    fields,
33    i18n::t,
34    paths,
35    reply::{Fail, Outcome, Res, envelope},
36    selector::Selector,
37};
38
39/// Example scripts shipped in the repository's `scripts/` folder.
40const BUILTIN: &[(&str, &str)] = &[
41    ("settings-version", include_str!("../scripts/settings-version.js")),
42    ("calculator-multiply", include_str!("../scripts/calculator-multiply.js")),
43    ("clock-world-list", include_str!("../scripts/clock-world-list.js")),
44    ("settings-list", include_str!("../scripts/settings-list.js")),
45];
46
47/// What a script says it does: its first comment line.
48fn description(code: &str) -> String {
49    code.lines()
50        .map(str::trim)
51        .find(|l| !l.is_empty())
52        .and_then(|l| l.strip_prefix("//"))
53        .map(|l| l.trim().to_string())
54        .unwrap_or_default()
55}
56
57/// Saved scripts and where each comes from. Personal ones win over examples.
58pub fn list() -> Vec<(String, String, String)> {
59    let mut out: Vec<(String, String, String)> = Vec::new();
60    if let Ok(dir) = std::fs::read_dir(paths::personal_scripts()) {
61        let mut files: Vec<std::path::PathBuf> = dir
62            .filter_map(|e| e.ok().map(|e| e.path()))
63            .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("js"))
64            .collect();
65        files.sort();
66        for path in files {
67            let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("?").to_string();
68            let about = std::fs::read_to_string(&path).map(|c| description(&c)).unwrap_or_default();
69            out.push((name, about, path.display().to_string()));
70        }
71    }
72    for (name, code) in BUILTIN {
73        if !out.iter().any(|(n, _, _)| n == name) {
74            out.push((name.to_string(), description(code), format!("builtin:{name}")));
75        }
76    }
77    out
78}
79
80/// A saved script by name: the personal folder first, then the examples.
81/// Returns its code and where it came from.
82pub fn load(name: &str) -> Res<(String, String)> {
83    if name.ends_with(".js") || name.contains('/') {
84        return Err(Fail::invalid(format!("\"{name}\" is a file, not the name of a saved script")).hint(t(
85            "파일은 `iph script <파일>`로 실행합니다.",
86            "Run a file with `iph script <file>`.",
87        )));
88    }
89    let path = paths::personal_scripts().join(format!("{name}.js"));
90    if let Ok(code) = std::fs::read_to_string(&path) {
91        return Ok((code, path.display().to_string()));
92    }
93    if let Some((_, code)) = BUILTIN.iter().find(|(n, _)| *n == name) {
94        return Ok((code.to_string(), format!("builtin:{name}")));
95    }
96    Err(Fail::not_sent("script_not_found", format!("no script named \"{name}\"")).hint(t(
97        "`iph scripts`로 목록을 확인하세요. 개인 스크립트는 ~/.config/iph/scripts/<이름>.js 에 둡니다.",
98        "List them with `iph scripts`. Personal scripts live in ~/.config/iph/scripts/<name>.js.",
99    )))
100}
101
102pub fn cmd_scripts() -> Res {
103    let list: Vec<Value> = list()
104        .into_iter()
105        .map(|(name, description, source)| json!({ "name": name, "description": description, "source": source }))
106        .collect();
107    Ok(fields! { "count": list.len(), "scripts": list })
108}
109
110// ---------------------------------------------------------------------------
111// The engine
112// ---------------------------------------------------------------------------
113
114/// What a script sees. Every function goes through `__iph`, the one door to
115/// the phone, as JSON text both ways; a result that is not `ok` is thrown.
116const PRELUDE: &str = r#"
117"use strict";
118(() => {
119  const opts = (o) => (o && typeof o === "object" && !Array.isArray(o)) ? o : {};
120  const call = (cmd, args) => {
121    const r = JSON.parse(__iph(cmd, JSON.stringify(args === undefined ? {} : args)));
122    if (r.ok) return r;
123    const e = new Error((r.error && r.error.message) || "the call failed");
124    e.name = "IphError";
125    e.cmd = cmd;
126    e.code = r.error && r.error.code;
127    e.outcome = r.outcome;
128    e.hint = r.error && r.error.hint;
129    e.result = r;
130    throw e;
131  };
132  const text = (x) => (typeof x === "string" ? x : JSON.stringify(x));
133  const g = globalThis;
134  g.read = (o) => call("read", opts(o));
135  g.find = (selector, o) => call("find", { ...opts(o), selector });
136  g.tap = (target, o) => call("tap", { ...opts(o), target });
137  g.swipe = (how, o) => call("swipe", typeof how === "string" ? { ...opts(o), direction: how } : { ...opts(o), ...opts(how) });
138  g.pinch = (how, o) => call("pinch", typeof how === "string" ? { ...opts(o), direction: how } : { ...opts(o), ...opts(how) });
139  g.scroll = (direction, o) => call("scroll", { ...opts(o), direction });
140  g.type = (value, o) => call("type", { ...opts(o), text: String(value) });
141  g.key = (name, o) => call("key", { ...opts(o), key: name });
142  g.button = (name, o) => call("button", { ...opts(o), button: name });
143  g.launch = (app, o) => call("launch", { ...opts(o), app });
144  g.apps = () => call("apps", {});
145  g.waitFor = (selector, o) => call("wait_for", { ...opts(o), selector });
146  g.readField = (spec) => call("read_field", opts(spec)).value;
147  g.readList = (spec) => call("read_list", opts(spec));
148  g.sleep = (ms) => { __sleep(Number(ms) || 0); };
149  g.log = (...xs) => { __emit("log", "", xs.map(text).join(" ")); };
150  g.output = (key, value) => { __emit("output", String(key), JSON.stringify(value === undefined ? null : value)); };
151  g.console = { log: g.log, info: g.log, warn: g.log, error: g.log };
152  g.__done = (value) => JSON.stringify({ ok: true, value });
153  g.__threw = (e) => {
154    const error = {
155      name: e && e.name,
156      message: e && e.message !== undefined ? String(e.message) : String(e),
157      stack: e && e.stack ? String(e.stack) : undefined,
158    };
159    if (e && e.result) {
160      error.cmd = e.cmd;
161      error.result = e.result;
162    }
163    return JSON.stringify({ ok: false, error });
164  };
165})();
166"#;
167
168/// The name the script's code goes by in stack traces.
169const SCRIPT_FILE: &str = "script";
170
171/// What a call gets back once the run has been stopped.
172const STOPPED: &str =
173    r#"{"ok":false,"outcome":"not_sent","error":{"code":"script_stopped","message":"the script was stopped"}}"#;
174
175/// Most log lines kept from one run, and the longest a line may be.
176const MAX_LOGS: usize = 200;
177const MAX_LOG_LINE: usize = 2000;
178
179/// The longest a script may be allowed to run, whatever the config says:
180/// the resident process gives up on a request after [`crate::daemon::budget`].
181pub const MAX_TIMEOUT_SECS: u64 = 840;
182
183pub struct Limits {
184    pub timeout: Duration,
185    /// Bytes the script may hold.
186    pub memory: usize,
187}
188
189/// The phone side of a run.
190#[allow(async_fn_in_trait)]
191pub trait ScriptHost {
192    /// Run one call and give back the object the command would print.
193    async fn call(&mut self, cmd: &str, args: Value) -> Value;
194    /// True once the run must not go on.
195    fn must_stop(&self) -> bool;
196    /// A call was cut off by the time limit.
197    fn cut_off(&mut self, cmd: &str);
198}
199
200#[derive(Debug, Clone, PartialEq)]
201pub enum End {
202    /// The script ran to its end; what it returned, if anything.
203    Returned(Option<Value>),
204    /// The script threw, or a call it did not catch failed:
205    /// `{name, message, stack, cmd?, result?}`.
206    Threw(Value),
207    /// Stopped from outside: "timeout", or "unknown_outcome" after a call
208    /// that must not be followed by another.
209    Stopped(&'static str),
210    /// The engine could not run the script at all.
211    Engine(String),
212}
213
214#[derive(Debug)]
215pub struct Ended {
216    pub end: End,
217    pub outputs: Map<String, Value>,
218    pub logs: Vec<String>,
219    pub calls: u32,
220    /// How many lines the script has.
221    pub lines: u64,
222}
223
224struct Call {
225    cmd: String,
226    args: String,
227    reply: std::sync::mpsc::Sender<String>,
228}
229
230#[derive(Default)]
231struct Shared {
232    /// Set to end the run: the engine is interrupted and calls are refused.
233    stop: AtomicBool,
234    outputs: Mutex<Map<String, Value>>,
235    logs: Mutex<Vec<String>>,
236}
237
238/// The script's own thread: set the engine up, run the code, say how it ended.
239fn js_main(
240    code: &str,
241    args: &Value,
242    memory: usize,
243    shared: &Arc<Shared>,
244    calls: tokio::sync::mpsc::UnboundedSender<Call>,
245) -> End {
246    let runtime = match Runtime::new() {
247        Ok(r) => r,
248        Err(e) => return End::Engine(format!("cannot start the engine: {e}")),
249    };
250    runtime.set_memory_limit(memory);
251    runtime.set_max_stack_size(1 << 20);
252    let flag = shared.clone();
253    runtime.set_interrupt_handler(Some(Box::new(move || flag.stop.load(Ordering::Relaxed))));
254    // `full` is the language and its built-ins (JSON, Date, RegExp, Map …).
255    // Nothing that reaches outside comes with it: no modules, no std, no os.
256    let context = match Context::full(&runtime) {
257        Ok(c) => c,
258        Err(e) => return End::Engine(format!("cannot start the engine: {e}")),
259    };
260    context.with(|ctx| {
261        let ran = (|| -> rquickjs::Result<String> {
262            let globals = ctx.globals();
263            let (stop, door) = (shared.clone(), calls.clone());
264            globals.set(
265                "__iph",
266                Function::new(ctx.clone(), move |cmd: String, args: String| -> String {
267                    if stop.stop.load(Ordering::Relaxed) {
268                        return STOPPED.to_string();
269                    }
270                    let (reply, answer) = std::sync::mpsc::channel();
271                    if door.send(Call { cmd, args, reply }).is_err() {
272                        return STOPPED.to_string();
273                    }
274                    answer.recv().unwrap_or_else(|_| STOPPED.to_string())
275                })?,
276            )?;
277            let stop = shared.clone();
278            globals.set(
279                "__sleep",
280                Function::new(ctx.clone(), move |ms: f64| {
281                    let until = Instant::now() + Duration::from_millis(ms.clamp(0.0, 600_000.0) as u64);
282                    while !stop.stop.load(Ordering::Relaxed) {
283                        let left = until.saturating_duration_since(Instant::now());
284                        if left.is_zero() {
285                            break;
286                        }
287                        std::thread::sleep(left.min(Duration::from_millis(20)));
288                    }
289                })?,
290            )?;
291            let kept = shared.clone();
292            globals.set(
293                "__emit",
294                Function::new(ctx.clone(), move |kind: String, key: String, body: String| {
295                    if kind == "output" {
296                        let value = serde_json::from_str(&body).unwrap_or(Value::Null);
297                        kept.outputs.lock().unwrap().insert(key, value);
298                    } else {
299                        let mut logs = kept.logs.lock().unwrap();
300                        if logs.len() < MAX_LOGS {
301                            logs.push(body.chars().take(MAX_LOG_LINE).collect());
302                        }
303                    }
304                })?,
305            )?;
306            // Named apart, so a stack trace tells the script's lines from these.
307            let named = |name: &str| {
308                let mut options = EvalOptions::default();
309                options.filename = Some(name.into());
310                options
311            };
312            ctx.eval_with_options::<(), _>(PRELUDE, named("iph"))?;
313            // The script is the body of a function, so `return` works at its
314            // top level, and its first line is line 2 of what is evaluated.
315            let run: Function = ctx.eval_with_options(
316                format!(
317                    "(function(args){{try{{return __done((function(args){{\"use strict\";\n{code}\n}})(args))}}catch(e){{return __threw(e)}}}})"
318                ),
319                named(SCRIPT_FILE),
320            )?;
321            let args = ctx.json_parse(args.to_string())?;
322            run.call((args,))
323        })();
324        match ran {
325            Ok(text) => match serde_json::from_str::<Value>(&text) {
326                Ok(v) if v["ok"] == true => End::Returned(v.get("value").cloned().filter(|x| !x.is_null())),
327                Ok(v) => End::Threw(v["error"].clone()),
328                Err(e) => End::Engine(format!("the script's result is not JSON: {e}")),
329            },
330            Err(_) if shared.stop.load(Ordering::Relaxed) => End::Stopped("timeout"),
331            // Not thrown by the script's own code: it does not parse, or the
332            // engine ran out of memory before the script could catch it.
333            Err(e) => End::Threw(match CaughtError::from_error(&ctx, e) {
334                CaughtError::Exception(ex) => json!({
335                    "name": "Error",
336                    "message": ex.message().unwrap_or_else(|| "the script could not run".into()),
337                    "stack": ex.stack(),
338                }),
339                other => json!({ "name": "Error", "message": other.to_string() }),
340            }),
341        }
342    })
343}
344
345/// Run `code` against `host`.
346pub async fn run<H: ScriptHost>(host: &mut H, code: &str, args: &Map<String, Value>, limits: &Limits) -> Ended {
347    let shared = Arc::new(Shared::default());
348    let lines = code.lines().count().max(1) as u64;
349    let (door, mut calls_in) = tokio::sync::mpsc::unbounded_channel::<Call>();
350    let (done, finished) = tokio::sync::oneshot::channel::<End>();
351    let (thread_shared, code_owned, args_owned, memory) =
352        (shared.clone(), code.to_string(), Value::Object(args.clone()), limits.memory);
353    let spawned = std::thread::Builder::new()
354        .name("iph-script".into())
355        .stack_size(8 << 20)
356        .spawn(move || {
357            let end = js_main(&code_owned, &args_owned, memory, &thread_shared, door);
358            let _ = done.send(end);
359        });
360    if let Err(e) = spawned {
361        return Ended {
362            end: End::Engine(format!("cannot start the script: {e}")),
363            outputs: Map::new(),
364            logs: Vec::new(),
365            calls: 0,
366            lines,
367        };
368    }
369
370    let deadline = tokio::time::Instant::now() + limits.timeout;
371    let mut calls = 0u32;
372    let mut stopped: Option<&'static str> = None;
373    loop {
374        let next = tokio::select! {
375            call = calls_in.recv() => call,
376            _ = tokio::time::sleep_until(deadline) => {
377                stopped = Some("timeout");
378                None
379            }
380        };
381        // No more calls: the script's thread is done, or time is up.
382        let Some(call) = next else { break };
383        calls += 1;
384        let args = serde_json::from_str(&call.args).unwrap_or_else(|_| json!({}));
385        let reply = match tokio::time::timeout_at(deadline, host.call(&call.cmd, args)).await {
386            Ok(result) => result.to_string(),
387            Err(_) => {
388                host.cut_off(&call.cmd);
389                stopped = Some("timeout");
390                STOPPED.to_string()
391            }
392        };
393        if stopped.is_none() && host.must_stop() {
394            stopped = Some("unknown_outcome");
395        }
396        if stopped.is_some() {
397            // Before the answer goes back, so the script cannot act on it.
398            shared.stop.store(true, Ordering::Relaxed);
399        }
400        let _ = call.reply.send(reply);
401        if stopped.is_some() {
402            break;
403        }
404    }
405    if stopped.is_some() {
406        shared.stop.store(true, Ordering::Relaxed);
407    }
408    // Whatever the script still asks for is refused from here on.
409    drop(calls_in);
410    let end = match tokio::time::timeout(Duration::from_secs(10), finished).await {
411        Ok(Ok(end)) => end,
412        Ok(Err(_)) => End::Engine("the script engine stopped unexpectedly".into()),
413        Err(_) => End::Engine("the script did not stop".into()),
414    };
415    let end = match stopped {
416        Some(why) => End::Stopped(why),
417        None => end,
418    };
419    Ended {
420        end,
421        outputs: std::mem::take(&mut *shared.outputs.lock().unwrap()),
422        logs: std::mem::take(&mut *shared.logs.lock().unwrap()),
423        calls,
424        lines,
425    }
426}
427
428// ---------------------------------------------------------------------------
429// One run's bookkeeping, shared by the real phone and the tests
430// ---------------------------------------------------------------------------
431
432/// Calls that send input to the phone.
433const INPUTS: [&str; 9] = ["launch", "tap", "swipe", "pinch", "scroll", "type", "key", "button", "read_list"];
434/// Calls that may send several inputs before they fail.
435const MANY_INPUTS: [&str; 2] = ["scroll", "read_list"];
436/// Calls that act on the app the script launched, and so need it in front.
437const NEEDS_APP: [&str; 9] = ["tap", "swipe", "pinch", "scroll", "type", "key", "button", "read_field", "read_list"];
438
439/// A call's arguments after the run's rules have been applied.
440#[derive(Debug)]
441pub struct Prepared {
442    pub args: Value,
443    pub hint: Option<String>,
444}
445
446#[derive(Debug, Default)]
447pub struct RunState {
448    /// Calls that sent input, or may have.
449    pub inputs_sent: u32,
450    /// The app the last `launch` brought to the front.
451    pub expected_pid: Option<i64>,
452    /// The call after which the run may not go on, and its result.
453    pub unknown: Option<(String, Value)>,
454}
455
456impl RunState {
457    /// Apply the run's rules to one call's arguments.
458    pub fn prepare(&mut self, cmd: &str, args: Value) -> Res<Prepared> {
459        let Value::Object(mut map) = args else {
460            return Err(Fail::invalid(format!("`{cmd}` takes an object of arguments")));
461        };
462        let hint = map.remove("hint").and_then(|v| v.as_str().map(String::from));
463        Ok(Prepared { args: Value::Object(map), hint })
464    }
465
466    /// Take note of how a call ended.
467    pub fn note(&mut self, cmd: &str, result: &Value) {
468        let outcome = result["outcome"].as_str().unwrap_or("");
469        let sent = result["ok"] == true
470            || outcome == "unknown"
471            || outcome == "done"
472            // These may have scrolled before whatever went wrong.
473            || (MANY_INPUTS.contains(&cmd) && result["error"]["code"] != "invalid_argument");
474        if INPUTS.contains(&cmd) && sent {
475            self.inputs_sent += 1;
476        }
477        if outcome == "unknown" {
478            self.unknown = Some((cmd.to_string(), result.clone()));
479        }
480    }
481}
482
483/// Line of the script a stack trace points at: the first frame in the
484/// script's own code, which starts on line 2 of what is evaluated.
485fn script_line(stack: Option<&str>) -> Option<u64> {
486    let stack = stack?;
487    let mark = format!("{SCRIPT_FILE}:");
488    let mut from = 0;
489    while let Some(found) = stack[from..].find(&mark) {
490        let at = from + found;
491        from = at + mark.len();
492        // "(script:3:7)" or " script:3:7", not the tail of another name.
493        if at > 0 && !matches!(stack.as_bytes()[at - 1], b'(' | b' ') {
494            continue;
495        }
496        let digits: String = stack[from..].chars().take_while(|c| c.is_ascii_digit()).collect();
497        if let Ok(n) = digits.parse::<u64>() {
498            return Some(n.saturating_sub(1)).filter(|n| *n > 0);
499        }
500    }
501    None
502}
503
504/// Turn the way a run ended into the command's result.
505pub fn finish(ended: Ended, state: &RunState) -> Res {
506    let Ended { end, mut outputs, logs, calls, lines } = ended;
507    let sent = state.inputs_sent;
508    // The run's outcome. Anything that stops a run after input has gone out
509    // is `unknown`, because running the script again would send that input
510    // again.
511    let outcome_of = |call: Option<&Value>| match call.and_then(|c| c["outcome"].as_str()) {
512        Some("unknown") => Outcome::Unknown,
513        _ if sent > 0 => Outcome::Unknown,
514        _ => Outcome::NotSent,
515    };
516    let failed_call = |result: &Value, cmd: &str, line: Option<u64>| -> Fail {
517        let reason = &result["error"];
518        let mut f = Fail::new(
519            outcome_of(Some(result)),
520            "script_failed",
521            format!("`{cmd}` failed: {}", reason["message"].as_str().unwrap_or("")),
522        );
523        f.hint = reason["hint"].as_str().map(String::from);
524        f = f
525            .with("failed_call", json!({ "cmd": cmd, "line": line, "outcome": result["outcome"] }))
526            .with("reason", json!({ "code": reason["code"], "message": reason["message"] }));
527        // What the failed call had to say (candidates, the screen …).
528        if let Some(extra) = result.as_object() {
529            for (k, v) in extra {
530                if !matches!(k.as_str(), "ok" | "outcome" | "error" | "elapsed_ms") {
531                    f.extra.entry(k.clone()).or_insert(v.clone());
532                }
533            }
534        }
535        f
536    };
537
538    let fail = match end {
539        End::Returned(value) => {
540            match value {
541                Some(Value::Object(returned)) => outputs.extend(returned),
542                Some(other) => {
543                    outputs.insert("result".into(), other);
544                }
545                None => {}
546            }
547            return Ok(fields! { "outputs": outputs, "logs": logs, "calls": calls, "inputs_sent": sent });
548        }
549        End::Threw(error) => {
550            // Code that stops short is found out past its last line.
551            let line = script_line(error["stack"].as_str()).map(|n| n.min(lines));
552            match (error.get("result"), error["cmd"].as_str()) {
553                (Some(result), Some(cmd)) if result.is_object() => failed_call(result, cmd, line),
554                _ => Fail::new(
555                    outcome_of(None),
556                    "script_error",
557                    error["message"].as_str().unwrap_or("the script threw").to_string(),
558                )
559                .with("line", json!(line)),
560            }
561        }
562        End::Stopped("unknown_outcome") => match &state.unknown {
563            Some((cmd, result)) => failed_call(result, cmd, None),
564            None => Fail::unknown("script_stopped", "the script was stopped"),
565        },
566        End::Stopped(_) => Fail::new(
567            outcome_of(None),
568            "script_timeout",
569            "the script ran longer than it is allowed to",
570        )
571        .hint(t(
572            "config.toml의 [script] timeout_secs로 한도를 바꿀 수 있습니다. 입력을 보낸 뒤였다면 `iph read`로 화면부터 확인하세요.",
573            "[script] timeout_secs in config.toml changes the limit. If input had gone out, check the screen with `iph read` first.",
574        )),
575        End::Engine(message) => Fail::new(outcome_of(None), "script_error", message),
576    };
577    let fail = if sent > 0 && fail.hint.is_none() {
578        fail.hint(t(
579            "스크립트가 입력을 보낸 뒤에 멈췄습니다. 다시 실행하기 전에 `iph read`로 화면을 확인하세요.",
580            "The script stopped after it had sent input. Check the screen with `iph read` before running it again.",
581        ))
582    } else {
583        fail
584    };
585    Err(fail
586        .with("outputs", json!(outputs))
587        .with("logs", json!(logs))
588        .with("calls", calls)
589        .with("inputs_sent", sent))
590}
591
592// ---------------------------------------------------------------------------
593// The real phone
594// ---------------------------------------------------------------------------
595
596struct PhoneHost<'a> {
597    session: &'a mut Session,
598    state: RunState,
599}
600
601impl PhoneHost<'_> {
602    async fn tap(&mut self, args: Value) -> Res {
603        let s = &mut *self.session;
604        let Value::Object(mut map) = args else {
605            return Err(Fail::invalid("tap takes a target"));
606        };
607        // Like a person, a script waits a moment for what it wants to press
608        // and presses it once it holds still.
609        let wait = map.remove("timeout_ms").and_then(|v| v.as_u64()).unwrap_or(5000);
610        let settle = map.remove("settle").and_then(|v| v.as_bool()).unwrap_or(true);
611        let target = map.get("target").cloned().unwrap_or(Value::Null);
612        if let Some(list) = target.as_array() {
613            return Box::pin(s.tap_sequence(list)).await;
614        }
615        if let Target::Selector(sel) = parse_target(&target)? {
616            if wait > 0 {
617                Box::pin(s.wait_for(&sel, Duration::from_millis(wait))).await?;
618            }
619            if settle {
620                Box::pin(s.settle_target(&sel)).await?;
621            }
622        }
623        let tap: TapArgs = parse_args(&Value::Object(map))?;
624        Box::pin(s.cmd_tap(tap)).await
625    }
626
627    async fn dispatch(&mut self, cmd: &str, args: Value) -> Res {
628        if NEEDS_APP.contains(&cmd) {
629            Box::pin(self.session.check_expected_app(self.state.expected_pid)).await?;
630        }
631        match cmd {
632            "read" => Box::pin(self.session.cmd_read(parse_args(&args)?)).await,
633            "find" => Box::pin(self.session.cmd_find(parse_args(&args)?)).await,
634            "apps" => self.session.cmd_apps().await,
635            "launch" => {
636                let r = Box::pin(self.session.cmd_launch(parse_args(&args)?)).await?;
637                if r.get("foreground").and_then(|v| v.as_bool()) != Some(true) {
638                    return Err(Fail::unknown("launch_not_verified", "the app was launched but is not in front"));
639                }
640                self.state.expected_pid = r.get("pid").and_then(|v| v.as_i64());
641                Ok(r)
642            }
643            "tap" => self.tap(args).await,
644            "swipe" | "pinch" | "scroll" | "type" | "key" | "button" | "read_list" => {
645                let s = &mut *self.session;
646                match cmd {
647                    "swipe" => Box::pin(s.cmd_swipe(parse_args(&args)?)).await,
648                    "pinch" => Box::pin(s.cmd_pinch(parse_args(&args)?)).await,
649                    "scroll" => Box::pin(s.cmd_scroll(parse_args(&args)?)).await,
650                    "type" => Box::pin(s.cmd_type(parse_args(&args)?)).await,
651                    "key" => Box::pin(s.cmd_key(parse_args(&args)?)).await,
652                    "button" => Box::pin(s.cmd_button(parse_args(&args)?)).await,
653                    _ => Box::pin(s.read_list(&args)).await.map(|(_, rows, info)| {
654                        let count = rows.as_array().map(|a| a.len()).unwrap_or(0);
655                        fields! { "rows": rows, "count": count, "scrolls": info["scrolls"], "ended": info["ended"] }
656                    }),
657                }
658            }
659            "wait_for" => {
660                let selector = args.get("selector").cloned().unwrap_or(Value::Null);
661                let sel = Selector::from_value(&selector).map_err(|e| Fail::invalid(e.to_string()))?;
662                let timeout = args.get("timeout_ms").and_then(|v| v.as_u64()).unwrap_or(8000);
663                let el = Box::pin(self.session.wait_for(&sel, Duration::from_millis(timeout))).await?;
664                Ok(fields! { "element": brief(&el) })
665            }
666            "read_field" => {
667                let (_, value) = Box::pin(self.session.read_field(&args)).await?;
668                Ok(fields! { "value": value })
669            }
670            other => Err(Fail::invalid(format!(
671                "a script has no `{other}` (read, find, tap, swipe, pinch, scroll, type, key, button, launch, apps, waitFor, readField, readList)"
672            ))),
673        }
674    }
675}
676
677impl ScriptHost for PhoneHost<'_> {
678    async fn call(&mut self, cmd: &str, args: Value) -> Value {
679        let started = Instant::now();
680        let result = match self.state.prepare(cmd, args) {
681            Ok(Prepared { args, hint }) => {
682                Box::pin(self.dispatch(cmd, args)).await.map_err(|mut f| {
683                    // Whoever wrote the script knows what this call waits on.
684                    if let Some(hint) = hint {
685                        f.hint = Some(hint);
686                    }
687                    f
688                })
689            }
690            Err(f) => Err(f),
691        };
692        let value = envelope(result, started);
693        self.state.note(cmd, &value);
694        value
695    }
696
697    fn must_stop(&self) -> bool {
698        self.state.unknown.is_some()
699    }
700
701    fn cut_off(&mut self, cmd: &str) {
702        // Whatever was in flight is abandoned with its connections.
703        self.session.reset();
704        if INPUTS.contains(&cmd) {
705            self.state.inputs_sent += 1;
706        }
707    }
708}
709
710/// The request for the resident process: the code travels with it, so a
711/// saved script or a file is read by whoever asks, not by the daemon.
712pub fn request(code: &str, args: &Map<String, Value>, saved: Option<(&str, &str)>) -> Value {
713    let mut req = json!({ "code": code, "args": args });
714    if let Some((name, source)) = saved {
715        req["script"] = json!(name);
716        req["source"] = json!(source);
717    }
718    req
719}
720
721impl Session {
722    /// What the latest reading showed, for a failure with nothing of its own to show.
723    fn last_seen(&self) -> Option<Value> {
724        let (_, dump, _) = self.ax.as_ref()?.latest()?;
725        Some(Screen::build(&dump, BuildOptions::default()).summary(12))
726    }
727
728    pub async fn cmd_script(&mut self, code: &str, args: &Map<String, Value>) -> Res {
729        let limits = Limits {
730            timeout: Duration::from_secs(self.config.script.timeout_secs.clamp(1, MAX_TIMEOUT_SECS)),
731            memory: (self.config.script.memory_mb.clamp(8, 1024) as usize) << 20,
732        };
733        let mut host = PhoneHost { session: self, state: RunState::default() };
734        let ended = run(&mut host, code, args, &limits).await;
735        let PhoneHost { session, state } = host;
736        finish(ended, &state).map_err(|mut f| {
737            if !f.extra.contains_key("screen_summary")
738                && let Some(seen) = session.last_seen()
739            {
740                f = f.with("screen_summary", seen);
741            }
742            f
743        })
744    }
745
746    /// Serve a `script` request (see [`request`]).
747    pub async fn serve_script(&mut self, req: &Value) -> Res {
748        let code = req
749            .get("code")
750            .and_then(|v| v.as_str())
751            .ok_or_else(|| Fail::invalid("script needs `code`"))?;
752        let empty = Map::new();
753        let args = req.get("args").and_then(|v| v.as_object()).unwrap_or(&empty);
754        let saved: Vec<(&str, Value)> = ["script", "source"]
755            .into_iter()
756            .filter_map(|k| req.get(k).filter(|v| v.is_string()).map(|v| (k, v.clone())))
757            .collect();
758        match Box::pin(self.cmd_script(code, args)).await {
759            Ok(mut out) => {
760                out.extend(saved.into_iter().map(|(k, v)| (k.to_string(), v)));
761                Ok(out)
762            }
763            Err(f) => Err(saved.into_iter().fold(f, |f, (k, v)| f.with(k, v))),
764        }
765    }
766}
767
768#[cfg(test)]
769mod tests {
770    use super::*;
771
772    /// A phone that answers from a table and remembers what it was asked.
773    #[derive(Default)]
774    struct Fake {
775        state: RunState,
776        asked: Vec<(String, Value)>,
777        /// cmd → the result to give instead of plain success.
778        answers: std::collections::HashMap<String, Value>,
779        delay: Option<Duration>,
780    }
781
782    impl ScriptHost for Fake {
783        async fn call(&mut self, cmd: &str, args: Value) -> Value {
784            if let Some(d) = self.delay {
785                tokio::time::sleep(d).await;
786            }
787            let value = match self.state.prepare(cmd, args) {
788                Ok(p) => {
789                    self.asked.push((cmd.to_string(), p.args));
790                    self.answers
791                        .get(cmd)
792                        .cloned()
793                        .unwrap_or_else(|| json!({"ok": true, "outcome": "done", "cmd": cmd}))
794                }
795                Err(f) => envelope(Err(f), Instant::now()),
796            };
797            self.state.note(cmd, &value);
798            value
799        }
800        fn must_stop(&self) -> bool {
801            self.state.unknown.is_some()
802        }
803        fn cut_off(&mut self, _cmd: &str) {
804            self.state.inputs_sent += 1;
805        }
806    }
807
808    fn limits() -> Limits {
809        Limits { timeout: Duration::from_secs(5), memory: 32 << 20 }
810    }
811
812    fn go(fake: &mut Fake, code: &str, args: Value, limits: Limits) -> (Res, Ended0) {
813        let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
814        let args = args.as_object().cloned().unwrap_or_default();
815        let ended = rt.block_on(run(fake, code, &args, &limits));
816        let kept = Ended0 { end: ended.end.clone(), calls: ended.calls };
817        (finish(ended, &fake.state), kept)
818    }
819
820    struct Ended0 {
821        end: End,
822        calls: u32,
823    }
824
825    #[test]
826    fn a_script_returns_outputs_and_reads_its_arguments() {
827        let mut fake = Fake::default();
828        let (res, _) = go(
829            &mut fake,
830            "output('seen', read().cmd);\nlog('hello', {n: 1});\nreturn { who: args.name, twice: Number(args.n) * 2 };",
831            json!({"name": "설정", "n": "21"}),
832            limits(),
833        );
834        let out = res.unwrap();
835        assert_eq!(out["outputs"], json!({"seen": "read", "who": "설정", "twice": 42}));
836        assert_eq!(out["logs"], json!(["hello {\"n\":1}"]));
837        assert_eq!(out["calls"], 1);
838        assert_eq!(out["inputs_sent"], 0);
839        // A plain value comes back as `result`.
840        let (res, _) = go(&mut Fake::default(), "return 7", json!({}), limits());
841        assert_eq!(res.unwrap()["outputs"], json!({"result": 7}));
842    }
843
844    #[test]
845    fn calls_take_the_same_arguments_as_the_commands() {
846        let mut fake = Fake::default();
847        let code = r#"
848            tap("일반");
849            tap({label: "정보", role: "button"}, {long_ms: 300});
850            scroll("down", {until: "개발자", max: 5});
851            swipe("up"); swipe({from: [1, 2], to: [3, 4]}, {drag: true});
852            type("안녕", {submit: true}); key("return"); button("home");
853            launch("com.apple.Preferences", {restart: true});
854            find("label~=일"); waitFor("일반", {timeout_ms: 100});
855            pinch("out"); pinch({scale: 1.5, rotate: -30, at: "지도"}, {duration_ms: 300});
856        "#;
857        let (res, _) = go(&mut fake, code, json!({}), limits());
858        res.unwrap();
859        let asked: Vec<(&str, &Value)> = fake.asked.iter().map(|(c, a)| (c.as_str(), a)).collect();
860        assert_eq!(asked[0], ("tap", &json!({"target": "일반"})));
861        assert_eq!(asked[1], ("tap", &json!({"long_ms": 300, "target": {"label": "정보", "role": "button"}})));
862        assert_eq!(asked[2], ("scroll", &json!({"until": "개발자", "max": 5, "direction": "down"})));
863        assert_eq!(asked[3], ("swipe", &json!({"direction": "up"})));
864        assert_eq!(asked[4], ("swipe", &json!({"drag": true, "from": [1, 2], "to": [3, 4]})));
865        assert_eq!(asked[5], ("type", &json!({"submit": true, "text": "안녕"})));
866        assert_eq!(asked[8], ("launch", &json!({"restart": true, "app": "com.apple.Preferences"})));
867        assert_eq!(asked[10], ("wait_for", &json!({"timeout_ms": 100, "selector": "일반"})));
868        assert_eq!(asked[11], ("pinch", &json!({"direction": "out"})));
869        assert_eq!(asked[12], ("pinch", &json!({"scale": 1.5, "rotate": -30, "at": "지도", "duration_ms": 300})));
870        assert_eq!(fake.state.inputs_sent, 11);
871    }
872
873    #[test]
874    fn launch_passes_arguments_and_environment_on() {
875        let mut fake = Fake::default();
876        let code = r#"launch("a.b", {arguments: ["-AppleLanguages", "(ko)"], environment: {TZ: "UTC"}});"#;
877        let (res, _) = go(&mut fake, code, json!({}), limits());
878        res.unwrap();
879        assert_eq!(
880            fake.asked[0],
881            ("launch".to_string(), json!({"arguments": ["-AppleLanguages", "(ko)"], "environment": {"TZ": "UTC"}, "app": "a.b"}))
882        );
883    }
884
885    #[test]
886    fn a_failed_call_throws_and_stops_the_script_unless_caught() {
887        let not_found = json!({"ok": false, "outcome": "not_sent",
888            "error": {"code": "not_found", "message": "no element matches", "hint": "scroll"},
889            "screen_summary": {"labels": ["a", "b"]}});
890        let mut fake = Fake::default();
891        fake.answers.insert("tap".into(), not_found.clone());
892        let (res, _) = go(&mut fake, "read();\ntap('없는 것');\nread();", json!({}), limits());
893        let f = res.unwrap_err();
894        assert_eq!((f.code.as_str(), f.outcome), ("script_failed", Outcome::NotSent));
895        assert_eq!(f.extra["reason"]["code"], "not_found");
896        assert_eq!(f.extra["failed_call"], json!({"cmd": "tap", "line": 2, "outcome": "not_sent"}));
897        assert_eq!(f.extra["screen_summary"]["labels"], json!(["a", "b"]));
898        assert_eq!(f.hint.as_deref(), Some("scroll"));
899        assert_eq!(fake.asked.len(), 2, "nothing runs after the failure");
900
901        // Caught, the script goes on and can look at what went wrong.
902        let mut fake = Fake::default();
903        fake.answers.insert("tap".into(), not_found);
904        let code = "try { tap('x') } catch (e) { output('code', e.code); output('outcome', e.outcome) }\nreturn { after: read().ok }";
905        let (res, _) = go(&mut fake, code, json!({}), limits());
906        assert_eq!(res.unwrap()["outputs"], json!({"code": "not_found", "outcome": "not_sent", "after": true}));
907    }
908
909    #[test]
910    fn a_pinch_counts_as_input_so_a_later_failure_is_unknown() {
911        let not_found = json!({"ok": false, "outcome": "not_sent", "error": {"code": "not_found", "message": "no element matches"}});
912        let mut fake = Fake::default();
913        fake.answers.insert("tap".into(), not_found);
914        let (res, _) = go(&mut fake, "pinch('out');\ntap('no such label');", json!({}), limits());
915        let f = res.unwrap_err();
916        assert_eq!((f.code.as_str(), f.outcome), ("script_failed", Outcome::Unknown));
917        assert_eq!(f.extra["inputs_sent"], 1);
918        assert!(NEEDS_APP.contains(&"pinch"));
919    }
920
921    #[test]
922    fn a_script_that_throws_reports_its_line() {
923        let mut fake = Fake::default();
924        let (res, _) = go(&mut fake, "tap('a');\n\nthrow new Error('기대한 화면이 아닙니다');", json!({}), limits());
925        let f = res.unwrap_err();
926        assert_eq!(f.code, "script_error");
927        assert_eq!(f.message, "기대한 화면이 아닙니다");
928        assert_eq!(f.extra["line"], 3);
929        // A tap had gone out: the caller must look before running it again.
930        assert_eq!(f.outcome, Outcome::Unknown);
931        assert_eq!(f.extra["inputs_sent"], 1);
932        // Code that does not parse never starts.
933        let (res, kept) = go(&mut Fake::default(), "tap('a'", json!({}), limits());
934        let f = res.unwrap_err();
935        assert_eq!((f.code.as_str(), f.outcome, kept.calls), ("script_error", Outcome::NotSent, 0));
936        assert_eq!(f.extra["line"], 1);
937    }
938
939    #[test]
940    fn nothing_follows_a_call_with_an_unknown_outcome() {
941        let mut fake = Fake::default();
942        fake.answers.insert(
943            "tap".into(),
944            json!({"ok": false, "outcome": "unknown", "error": {"code": "input_failed", "message": "write failed"}}),
945        );
946        // The script tries to swallow the failure and press again.
947        let code = "for (let i = 0; i < 5; i++) { try { tap('보내기 아님') } catch (e) {} }\nreturn 'went on'";
948        let (res, kept) = go(&mut fake, code, json!({}), limits());
949        let f = res.unwrap_err();
950        assert_eq!(f.outcome, Outcome::Unknown);
951        assert_eq!(f.extra["reason"]["code"], "input_failed");
952        assert_eq!(fake.asked.len(), 1, "the tap is not sent a second time");
953        assert_eq!(kept.end, End::Stopped("unknown_outcome"));
954    }
955
956    #[test]
957    fn type_observations_reach_scripts_and_a_withheld_submit_stops_them() {
958        let mut fake = Fake::default();
959        let observation = json!({
960            "ok": true, "outcome": "done", "sent": true, "verified": true,
961            "value_before": "", "value": "", "value_changed": false,
962            "screen_changed": false, "submit_sent": false,
963            "warning": {"code": "value_unchanged", "message": "unchanged"},
964        });
965        fake.answers.insert("type".into(), observation.clone());
966        let (res, _) = go(&mut fake, "return type('hello')", json!({}), limits());
967        assert_eq!(res.unwrap()["outputs"], observation);
968
969        let mut fake = Fake::default();
970        let mut blocked = observation;
971        blocked["ok"] = json!(false);
972        blocked["outcome"] = json!("unknown");
973        blocked["error"] = json!({"code": "type_not_verified", "message": "Return was not sent"});
974        fake.answers.insert("type".into(), blocked);
975        let (res, kept) = go(
976            &mut fake,
977            "try { type('hello', {submit: true}) } catch (e) {}\nkey('return'); type('hello');",
978            json!({}),
979            limits(),
980        );
981        let fail = res.unwrap_err();
982        assert_eq!(fail.outcome, Outcome::Unknown);
983        assert_eq!(fail.extra["reason"]["code"], "type_not_verified");
984        assert_eq!(fail.extra["warning"]["code"], "value_unchanged");
985        assert_eq!(
986            fake.asked.len(),
987            1,
988            "neither Return nor a duplicate paste reaches the phone"
989        );
990        assert_eq!(kept.end, End::Stopped("unknown_outcome"));
991    }
992
993    #[test]
994    fn a_script_that_never_ends_is_stopped() {
995        let short = || Limits { timeout: Duration::from_millis(300), memory: 32 << 20 };
996        let began = Instant::now();
997        let (res, _) = go(&mut Fake::default(), "while (true) {}", json!({}), short());
998        let f = res.unwrap_err();
999        assert_eq!((f.code.as_str(), f.outcome), ("script_timeout", Outcome::NotSent));
1000        assert!(began.elapsed() < Duration::from_secs(5));
1001        // Input had gone out before it hung: unknown, not "safe to run again".
1002        let (res, _) = go(&mut Fake::default(), "tap('a'); while (true) {}", json!({}), short());
1003        assert_eq!(res.unwrap_err().outcome, Outcome::Unknown);
1004        // Asleep, or waiting on a call that does not come back: the same.
1005        let (res, _) = go(&mut Fake::default(), "sleep(60000)", json!({}), short());
1006        assert_eq!(res.unwrap_err().code, "script_timeout");
1007        let mut slow = Fake { delay: Some(Duration::from_secs(30)), ..Default::default() };
1008        let (res, _) = go(&mut slow, "tap('a')", json!({}), short());
1009        let f = res.unwrap_err();
1010        assert_eq!((f.code.as_str(), f.outcome), ("script_timeout", Outcome::Unknown));
1011    }
1012
1013    #[test]
1014    fn a_script_that_eats_memory_is_stopped() {
1015        let small = Limits { timeout: Duration::from_secs(20), memory: 8 << 20 };
1016        let code = "const all = []; while (true) { all.push(new Array(100000).fill('x'.repeat(20))) }";
1017        let (res, _) = go(&mut Fake::default(), code, json!({}), small);
1018        let f = res.unwrap_err();
1019        assert_eq!(f.code, "script_error");
1020        assert!(f.message.contains("memory"), "{}", f.message);
1021    }
1022
1023    #[test]
1024    fn a_script_has_no_way_out_of_the_engine() {
1025        let code = r#"
1026            const names = ["require", "process", "fetch", "XMLHttpRequest", "std", "os", "Deno", "Bun",
1027                           "WebSocket", "setTimeout", "importScripts", "Worker"];
1028            return { there: names.filter((n) => typeof globalThis[n] !== "undefined") };
1029        "#;
1030        let (res, _) = go(&mut Fake::default(), code, json!({}), limits());
1031        assert_eq!(res.unwrap()["outputs"]["there"], json!([]));
1032        // Modules cannot be loaded either way.
1033        let (res, _) = go(&mut Fake::default(), "import fs from 'fs'", json!({}), limits());
1034        assert_eq!(res.unwrap_err().code, "script_error");
1035        let (res, _) = go(
1036            &mut Fake::default(),
1037            "let got = 'nothing'; import('std').then(() => { got = 'loaded' }, () => { got = 'refused' }); return got",
1038            json!({}),
1039            limits(),
1040        );
1041        assert_ne!(res.map(|o| o["outputs"]["result"].clone()).unwrap_or(json!("refused")), json!("loaded"));
1042    }
1043
1044    #[test]
1045    fn the_line_comes_from_the_scripts_own_frame() {
1046        let stack = "    at call (iph:9:15)\n    at <anonymous> (iph:23:20)\n    at <anonymous> (script:4:5)\n    at <anonymous> (script:1:1)\n";
1047        assert_eq!(script_line(Some(stack)), Some(3));
1048        assert_eq!(script_line(Some("    at script:2:9\n")), Some(1));
1049        assert_eq!(script_line(Some("    at f (eval_script:7:1)\n")), None);
1050        assert_eq!(script_line(None), None);
1051    }
1052
1053    #[test]
1054    fn a_hint_on_a_call_is_what_the_person_is_told() {
1055        let mut state = RunState::default();
1056        let p = state.prepare("wait_for", json!({"selector": "홈", "hint": "폰을 봐 주세요"})).unwrap();
1057        assert_eq!(p.hint.as_deref(), Some("폰을 봐 주세요"));
1058        assert_eq!(p.args, json!({"selector": "홈"}));
1059    }
1060
1061    #[test]
1062    fn saved_scripts_are_listed_with_their_first_comment() {
1063        assert_eq!(description("// 설정에서 iOS 버전 읽기\nlaunch('x')"), "설정에서 iOS 버전 읽기");
1064        assert_eq!(description("\n\n  // spaced  \ncode"), "spaced");
1065        assert_eq!(description("launch('x')"), "");
1066        for (name, code) in BUILTIN {
1067            assert!(!description(code).is_empty(), "{name} has no description");
1068        }
1069    }
1070}