Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

51.8 KB
Raw Blame
//! 守护进程控制接口:Unix socket(0600,默认位置见 [`default_socket`]),每行一个 JSON。
//! MCP 进程和 CLI 都通过它访问设备;本机不开 TCP 端口。

pub mod env;

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

/// 以 root 运行(系统服务)时的控制接口和数据目录
pub const SYSTEM_SOCKET: &str = "/run/iphone-control/ctl.sock";
pub const SYSTEM_DATA_DIR: &str = "/var/lib/iphone-control";

pub fn running_as_root() -> bool {
    // SAFETY: geteuid 没有前置条件,也不会失败
    unsafe { libc::geteuid() == 0 }
}

/// 默认数据目录:root 用 [`SYSTEM_DATA_DIR`];普通用户(从源码运行)用 `$XDG_DATA_HOME/iphone-control`,
/// 即 `~/.local/share/iphone-control`
pub fn default_data_dir() -> PathBuf {
    if running_as_root() {
        return SYSTEM_DATA_DIR.into();
    }
    let xdg = std::env::var_os("XDG_DATA_HOME").filter(|v| !v.is_empty()).map(PathBuf::from);
    match xdg
        .or_else(|| std::env::var_os("HOME").filter(|v| !v.is_empty()).map(|h| PathBuf::from(h).join(".local/share")))
    {
        Some(base) => base.join("iphone-control"),
        None => SYSTEM_DATA_DIR.into(),
    }
}

/// 默认控制接口:root 用 [`SYSTEM_SOCKET`];普通用户用 `$XDG_RUNTIME_DIR/iphone-control/ctl.sock`,
/// 没有这个变量时放进默认数据目录。守护进程、MCP、CLI 用同一个函数算,不用另外配置就能找到彼此
pub fn default_socket() -> PathBuf {
    if running_as_root() {
        return SYSTEM_SOCKET.into();
    }
    match std::env::var_os("XDG_RUNTIME_DIR").filter(|v| !v.is_empty()) {
        Some(run) => PathBuf::from(run).join("iphone-control/ctl.sock"),
        None => default_data_dir().join("ctl.sock"),
    }
}

/// [`Request::TapElement`] 屏幕上找不到时边滑边找:`down` 看下面的内容(手指往上滑),否则看上面的;最多滑 `max` 次
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScrollSearch {
    pub down: bool,
    pub max: u32,
}

/// [`ScrollSearch::max`] 的上限:每次慢滑加等列表停下约 2.5 秒,滑满 15 次也就半分钟出头
pub const MAX_SEARCH_SCROLLS: u32 = 15;
pub const MAX_SCROLL_ROWS: usize = 150;

/// Bounds and matching rules for a standalone list scan.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ScrollCollect {
    pub down: bool,
    pub max_scrolls: u32,
    pub filter: String,
    pub until: String,
    pub limit: usize,
    /// Where to swipe across, in points from the left; the middle of the screen if not given. An iPad split view
    /// scrolls the column swiped in: the sidebar needs an x inside it
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub x: Option<f64>,
}

fn is_zero(v: &f64) -> bool {
    *v == 0.0
}

/// [`Request::Tap`] 一次最多连点几下
pub const MAX_TAP_COUNT: u32 = 10;

fn one() -> u32 {
    1
}

fn is_one(n: &u32) -> bool {
    *n == 1
}
/// [`Request::TapSequence`] 一个手势最多点几个点(同 `iphone_batch` 最多几步)
pub const MAX_SEQUENCE_POINTS: usize = 20;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TouchPhase {
    Prepare,
    Down,
    Move,
    Up,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Request {
    /// MCP 的 iphone_status:与 toolkit 相同的文字
    Status,
    /// The phone's own screen video (CoreDevice screen media stream, HEVC, iOS 27 and later) for the control page.
    /// Streaming: the daemon answers one line ([`Response::Text`] when it streams, [`Response::Error`] when it
    /// can't), then sends [`ScreenMsg`] messages on the same connection until either side closes it. The client may
    /// write [`SCREEN_KEYFRAME`] to ask for a keyframe. Use [`open_screen_stream`]
    ScreenStream,
    /// 区域单位为点;None 表示整屏
    Screenshot {
        region: Option<(f64, f64, f64, f64)>,
    },
    ListElements {
        filter: String,
        limit: usize,
        /// 守护进程缓存的元素树不超过这么旧就直接用,不再读(0 = 总是重读)。
        /// 用于「等界面稳定」之后紧接着列元素:Settle 刚读过
        #[serde(default)]
        max_age_ms: u64,
        /// 手机锁着时不自动解锁,直接报错(控制台的只读令牌:只能看,不能让守护进程输入锁屏密码)
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        keep_locked: bool,
    },
    /// Inspect successive visible viewports, leaving a matching element on screen.
    ScrollCollect(ScrollCollect),
    Tap {
        x: f64,
        y: f64,
        /// 在这一点连点几下(一个手势),1 = 普通点击。旧版本客户端不带,按 1;为 1 时不写进请求,旧版本守护进程照常认
        #[serde(default = "one", skip_serializing_if = "is_one")]
        count: u32,
    },
    /// 依次点几个点,一个手势(`iphone_batch` 的 `one_gesture`):同一个键盘上连按。旧版本守护进程不认识,报错,不会只点一部分
    TapSequence {
        points: Vec<(f64, f64)>,
    },
    TapElement {
        text: String,
        index: usize,
        /// 同 ListElements。调用方声明这一串点击不改变界面布局(例如在同一个键盘上连按)时才给
        #[serde(default)]
        max_age_ms: u64,
        /// 屏幕上没有时边滑边找。旧版本守护进程不认识这个字段,只在当前屏幕上找
        #[serde(default, skip_serializing_if = "Option::is_none")]
        scroll: Option<ScrollSearch>,
    },
    LongPress {
        x: f64,
        y: f64,
        seconds: f64,
    },
    /// `hold`:先在起点按住几秒再移动(拖动图标、列表排序),0 是普通滑动。旧版本守护进程不认识这个字段,当普通滑动做
    Swipe {
        from: (f64, f64),
        to: (f64, f64),
        seconds: f64,
        #[serde(default, skip_serializing_if = "is_zero")]
        hold: f64,
    },
    TypeText {
        text: String,
        submit: bool,
    },
    /// A separate operation so an older daemon cannot silently insert the text instead.
    ReplaceText {
        text: String,
        submit: bool,
    },
    PressButton {
        button: String,
    },
    /// A finger that follows the pointer in the web viewer: `down` touches at the point, `move` drags it there, `up`
    /// lifts it there; `prepare` only opens the HID session, so the first touch doesn't wait for it. Over CoreDevice
    /// HID (iOS 27 and later), one report each, so the screen moves while the pointer does
    Touch {
        phase: TouchPhase,
        #[serde(default)]
        x: f64,
        #[serde(default)]
        y: f64,
    },
    ListApps {
        include_system: bool,
    },
    LaunchApp {
        app: String,
    },
    TerminateApp {
        app: String,
    },
    OpenUrl {
        url: String,
    },
    /// 等界面稳定:连续两次读到的元素一样,最多等 `max_ms`。操作后顺带返回界面时先调它
    Settle {
        max_ms: u64,
    },
    /// 查屏幕上有没有挡着的系统弹窗:有就返回提醒文字,没有返回空文字。不带 then 的操作做完后调
    AlertCheck,
    /// 等包含 `text` 的元素出现,最多等 `timeout_ms`(代替 agent 反复截图轮询)
    WaitFor {
        text: String,
        timeout_ms: u64,
    },
    WaitForDisappearance {
        text: String,
        timeout_ms: u64,
    },
    /// 运维:守护进程和设备的状态快照(JSON 文本)
    Daemon,
    /// 运维:逐层诊断,返回多行文字
    Doctor,
    Resign {
        force: bool,
    },
    UnlockReset,
    /// 列出 USB 上接着的全部设备,标出正在控制哪台
    ListDevices,
    /// 改为控制另一台设备:`device` 是 UDID、设备名或 `auto`(取消选定,改回自动选)
    SelectDevice {
        device: String,
    },
    /// Developer tools: the phone's unified log. `process` is an app (name or bundle ID) or a process name, empty for
    /// all; `grep` matches message or subsystem; only lines from the last `since_secs` seconds
    AppLogs {
        #[serde(default)]
        process: String,
        #[serde(default)]
        grep: String,
        since_secs: u64,
        limit: usize,
        #[serde(default)]
        errors_only: bool,
    },
    /// Developer tools: list crash reports (filtered by `filter`), or return the one called `name`
    CrashReports {
        #[serde(default)]
        filter: String,
        #[serde(default)]
        name: String,
    },
    /// Developer tools: install a signed .ipa or .app from a path on the daemon's machine
    InstallApp {
        path: String,
    },
    /// Developer tools: remove an app (never the WebDriverAgent runner)
    UninstallApp {
        app: String,
    },
    /// Developer tools: inspectable pages and WebViews (Web Inspector)
    WebTargets,
    /// Developer tools: evaluate JavaScript in a page. `target` is an id from `WebTargets`, or text in its URL or title
    WebEval {
        #[serde(default)]
        target: String,
        expression: String,
        timeout_ms: u64,
    },
    /// Tasks (docs/task-control.md): opens a task bound to the controlled device. Streaming: the daemon answers one
    /// line ([`Response::Text`] whose text is `{"id","token"}`, or [`Response::Error`]), then sends the task's
    /// events (one JSON [`TaskEvent`] per line) until it ends. The connection is the task's lease: when it closes
    /// before the task ended, the task is abandoned. Use [`open_task`]
    TaskOpen {
        title: String,
        /// Who runs it, shown on the control page (`claude`, `codex`, the name of an external platform…)
        #[serde(default)]
        owner: String,
        /// Apps (names or bundle IDs) the task may act in; outside them each action waits for the user. Empty: no
        /// limit
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        apps: Vec<String>,
    },
    /// Streaming like [`Request::TaskOpen`]: the task's events so far, then new ones, until it ends
    TaskEvents {
        id: String,
    },
    /// Recent tasks, newest last, as a JSON array of [`TaskInfo`]
    TaskList,
    /// `pause` / `resume` / `takeover` / `handback` / `stop` / `confirm` (with `approve` and the waiting `step`).
    /// The user's: refused with a task's token, or from the task's agent ([`Request::TaskAgent`]), for everything but
    /// `stop`
    TaskControl {
        id: String,
        action: String,
        #[serde(default)]
        step: Option<u64>,
        #[serde(default)]
        approve: bool,
        /// Who asks, for the task's log: `viewer`, `cli`, `owner`…
        #[serde(default, skip_serializing_if = "Option::is_none")]
        by: Option<String>,
        /// The process the request comes from when it is passed on (the page's API: the local client that called
        /// it); checked like the connection's own process
        #[serde(default, skip_serializing_if = "Option::is_none")]
        for_pid: Option<u32>,
    },
    /// From the process that opened the task, with its token, before it starts the agent: every process it starts
    /// from now on (and the ones those start) is the task's agent, whose control requests are refused
    /// ([`Request::TaskControl`])
    TaskAgent,
    /// With a task's token: the task's id while it is active, an error once it ended or for a token nobody knows.
    /// The viewer lets an agent on another machine use `/mcp` by its task's token alone (docs/task-control.md 7.6)
    TaskCheck,
    /// With the task's token ([`TASK`]): a tool call starts. Waits while the task isn't running, then records the
    /// step; the text is the step number. The calls the tool makes carry it ([`TASK_STEP`])
    TaskStep {
        tool: String,
        #[serde(default)]
        args: serde_json::Value,
    },
    /// With the task's token: the tool call's result, shown on the page
    TaskStepDone {
        step: u64,
        ok: bool,
        #[serde(default)]
        ms: u64,
        #[serde(default)]
        text: String,
    },
    /// With the task's token: waits until the user approves (text) or rejects (error) a sensitive action
    TaskConfirm {
        action: String,
        #[serde(default)]
        detail: String,
    },
    /// With the task's token: what the agent says it is doing, shown with the next step
    TaskNote {
        text: String,
    },
    /// With the task's token: the task ended on its own
    TaskFinish {
        ok: bool,
        #[serde(default)]
        summary: String,
    },
}

/// One event of a task, as the daemon streams it and the control page shows it: `step`, `confirm`, `state`, `done`
/// (and `task`, the first one, with the task's [`TaskInfo`])
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskEvent {
    pub event: String,
    pub data: serde_json::Value,
}

/// A task as [`Request::TaskList`] lists it
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskInfo {
    pub id: String,
    pub title: String,
    pub owner: String,
    pub udid: String,
    /// `running` / `paused` / `takeover` / `waiting`, or how it ended: `completed` / `failed` / `cancelled` /
    /// `rejected` / `abandoned` / `interrupted`
    pub state: String,
    /// Unix milliseconds
    pub started: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ended: Option<u64>,
    /// Steps so far
    #[serde(default)]
    pub steps: u64,
    /// Bundle IDs the task may act in (empty: no limit), including those the user let it into
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub apps: Vec<String>,
}

impl TaskInfo {
    pub fn active(&self) -> bool {
        matches!(self.state.as_str(), "running" | "paused" | "takeover" | "waiting")
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Response {
    Text {
        text: String,
        /// What the text reports, as fields, so clients don't have to read the text (which depends on the
        /// language). Daemons before this field existed don't send it; their text is always Chinese
        #[serde(default, skip_serializing_if = "Option::is_none")]
        outcome: Option<Outcome>,
        /// The same facts as data, for programs such as the control page ([`Data`]): the text is worded for people
        /// and agents, in their language, and may change. Only some requests have it
        #[serde(default, skip_serializing_if = "Option::is_none")]
        data: Option<Data>,
    },
    /// JPEG base64 + 说明文字(尺寸、放大倍数、坐标换算式)
    Image { jpeg_base64: String, caption: String },
    Error {
        message: String,
        /// The touch Down could not open a HID session and sent no contact report.
        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        retry_as_gesture: bool,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        failure: Option<ActionFailure>,
    },
}

/// Delivery of the requested input. Acknowledging the input does not verify its effect on screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Delivery {
    NotSent,
    Unknown,
    Sent,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FailureStage {
    Preflight,
    Connect,
    Send,
    Reply,
    Readback,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActionFailure {
    pub delivery: Delivery,
    pub stage: FailureStage,
}

/// Facts about a [`Response::Text`] that clients act on (see [`tapped_nothing`], [`alert_reported`])
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Outcome {
    /// `iphone_tap_element` tapped nothing: no match, index out of range, or the element is covered
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub tapped_nothing: bool,
    /// The text already reports a system alert blocking the screen
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub alert_reported: bool,
    /// Non-empty text input: whether a subsequent field read saw the requested text.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub field_verified: Option<bool>,
    /// A replacement can verify the desired value without sending any input.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delivery: Option<Delivery>,
}

/// What [`Request::Status`], [`Request::ListElements`], [`Request::ListApps`] and [`Request::AlertCheck`] report, as
/// data ([`Response::Text`]'s `data`)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Data {
    Status(StatusData),
    Elements {
        elements: Vec<ElementData>,
        /// Matches in the observed tree before the caller's limit. Older daemons do not report this.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        matched_count: Option<usize>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        truncated: Option<bool>,
    },
    ScrollCollection {
        stop_reason: ScrollStop,
        scrolls: u32,
        rows: Vec<ScrollRow>,
        matched: Option<Box<ScrollMatch>>,
    },
    Apps {
        apps: Vec<AppData>,
    },
    /// A system alert in front (its text), or none
    Alert {
        text: Option<String>,
    },
    /// Nothing was read: a [`BACKGROUND`] request gave way to an action
    Busy,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct StatusData {
    pub wda_ready: bool,
    /// `None` when WDA couldn't say (it isn't running)
    pub locked: Option<bool>,
    /// Looks like the lock screen though WDA says unlocked (`locked` is then true too)
    pub possibly_locked: bool,
    /// The foreground app's bundle ID
    pub foreground: Option<String>,
    /// The screen in points, as it is turned now
    pub width: Option<f64>,
    pub height: Option<f64>,
    /// Driven natively (iOS 27 and later without WDA: remoteAXService, HID, CoreDevice)
    #[serde(default)]
    pub native: bool,
}

/// An element on screen: its type, the text it shows, and its box in points (top left, size)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ElementData {
    #[serde(rename = "type")]
    pub kind: String,
    pub label: String,
    pub x: f64,
    pub y: f64,
    pub width: f64,
    pub height: f64,
}

/// The names are stable across daemon and MCP languages.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScrollStop {
    Interrupted,
    Found,
    End,
    MaxScrolls,
    Limit,
}

/// The matched element's visible bounds and an uncovered point in the final viewport.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ScrollMatch {
    #[serde(flatten)]
    pub element: ElementData,
    pub point: (f64, f64),
}

/// A row observed along the way. Its original screen coordinates can already be stale.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScrollRow {
    #[serde(rename = "type")]
    pub kind: String,
    pub label: String,
    pub name: String,
    pub value: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AppData {
    pub name: String,
    pub bundle_id: String,
}

impl Response {
    pub fn text(s: impl Into<String>) -> Self {
        Self::Text { text: s.into(), outcome: None, data: None }
    }
    /// Text with its [`Outcome`] (the daemon answers every text request this way)
    pub fn text_with(s: impl Into<String>, outcome: Outcome) -> Self {
        Self::Text { text: s.into(), outcome: Some(outcome), data: None }
    }
    /// Text with its [`Outcome`] and the same facts as [`Data`]
    pub fn text_data(s: impl Into<String>, outcome: Outcome, data: Option<Data>) -> Self {
        Self::Text { text: s.into(), outcome: Some(outcome), data }
    }
    pub fn error(s: impl Into<String>) -> Self {
        Self::Error { message: s.into(), retry_as_gesture: false, failure: None }
    }
    pub fn with_failure(mut self, delivery: Delivery, stage: FailureStage) -> Self {
        if let Self::Error { failure, .. } = &mut self {
            *failure = Some(ActionFailure { delivery, stage });
        }
        self
    }

    pub fn failure_stage(&self) -> Option<FailureStage> {
        match self {
            Self::Error { failure, .. } => failure.map(|f| f.stage),
            _ => None,
        }
    }

    pub fn field_readback(&self, req: &Request) -> Option<&'static str> {
        if !matches!(req, Request::ReplaceText { .. })
            && !matches!(req, Request::TypeText { text, .. } if !text.is_empty())
        {
            return None;
        }
        match self {
            Self::Text { outcome: Some(o), .. } => o.field_verified.map(|v| if v { "verified" } else { "unverified" }),
            Self::Error { failure: Some(f), .. } if f.stage == FailureStage::Readback => Some("failed"),
            _ => None,
        }
    }

    /// For callers reporting a device action. Older daemons have no error classification.
    pub fn delivery(&self, req: &Request) -> Delivery {
        match self {
            Self::Text { text, outcome, .. }
                if matches!(req, Request::TapElement { .. }) && tapped_nothing(text, *outcome) =>
            {
                Delivery::NotSent
            }
            Self::Text { .. } if matches!(req, Request::TypeText { text, submit: false } if text.is_empty()) => {
                Delivery::NotSent
            }
            Self::Text { outcome, .. } if matches!(req, Request::ReplaceText { .. }) => {
                outcome.and_then(|o| o.delivery).unwrap_or(Delivery::Unknown)
            }
            Self::Text { .. } if req.reports_delivery() => Delivery::Sent,
            Self::Error { failure: Some(f), .. } => f.delivery,
            _ => Delivery::Unknown,
        }
    }
}

impl Request {
    /// Inputs whose normal reply acknowledges sending the requested gesture or text.
    pub fn reports_delivery(&self) -> bool {
        matches!(
            self,
            Self::Tap { .. }
                | Self::TapElement { .. }
                | Self::TypeText { .. }
                | Self::ReplaceText { .. }
                | Self::Swipe { .. }
                | Self::LongPress { .. }
        )
    }

    /// Shared by the daemon and the MCP batch preflight, before any earlier steps run.
    pub fn check_required_text(&self) -> Result<(), String> {
        match self {
            // an empty ReplaceText empties the field
            Self::WaitFor { text, .. } | Self::WaitForDisappearance { text, .. } if text.is_empty() => {
                Err(ictl_l10n::tr!("text 不能为空", "text can't be empty").into())
            }
            _ => Ok(()),
        }
    }

    /// 会改变手机状态的操作。守护进程忙时排队太久就放弃,免得点击落到已经变了的屏幕上
    /// Every variant is listed (no `_`), so a new request type has to be classified here
    pub fn is_device_action(&self) -> bool {
        match self {
            Self::Tap { .. }
            | Self::ScrollCollect(_)
            | Self::TapElement { .. }
            | Self::TapSequence { .. }
            | Self::LongPress { .. }
            | Self::Swipe { .. }
            | Self::TypeText { .. }
            | Self::ReplaceText { .. }
            | Self::PressButton { .. }
            | Self::Touch { .. }
            | Self::LaunchApp { .. }
            | Self::TerminateApp { .. }
            | Self::OpenUrl { .. }
            | Self::SelectDevice { .. }
            | Self::InstallApp { .. }
            | Self::UninstallApp { .. }
            | Self::WebEval { .. } => true,
            Self::Status
            | Self::ScreenStream
            | Self::Screenshot { .. }
            | Self::ListElements { .. }
            | Self::ListApps { .. }
            | Self::Settle { .. }
            | Self::AlertCheck
            | Self::WaitFor { .. }
            | Self::WaitForDisappearance { .. }
            | Self::Daemon
            | Self::Doctor
            | Self::Resign { .. }
            | Self::UnlockReset
            | Self::ListDevices
            | Self::AppLogs { .. }
            | Self::CrashReports { .. }
            | Self::WebTargets
            | Self::TaskOpen { .. }
            | Self::TaskEvents { .. }
            | Self::TaskList
            | Self::TaskControl { .. }
            | Self::TaskAgent
            | Self::TaskCheck
            | Self::TaskStep { .. }
            | Self::TaskStepDone { .. }
            | Self::TaskConfirm { .. }
            | Self::TaskNote { .. }
            | Self::TaskFinish { .. } => false,
        }
    }
}

/// Messages after a [`Request::ScreenStream`] answer: one type byte, a 4-byte big-endian length, the payload
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ScreenMsg {
    /// JSON: `codec` (WebCodecs string), before the first frame and whenever it changes
    Config = 0,
    /// A keyframe, Annex-B, with its parameter sets
    Key = 1,
    /// Any other frame, Annex-B
    Delta = 2,
    /// JSON: `rotation` in degrees (0, 90, 180, 270: how far the portrait top edge has turned clockwise), when it
    /// becomes known or changes. The video itself is always portrait
    Info = 3,
}

impl ScreenMsg {
    pub fn from_byte(b: u8) -> Option<Self> {
        Some(match b {
            0 => Self::Config,
            1 => Self::Key,
            2 => Self::Delta,
            3 => Self::Info,
            _ => return None,
        })
    }

    /// The message with its header
    pub fn frame(self, payload: &[u8]) -> Vec<u8> {
        let mut m = Vec::with_capacity(5 + payload.len());
        m.push(self as u8);
        m.extend_from_slice(&(payload.len() as u32).to_be_bytes());
        m.extend_from_slice(payload);
        m
    }
}

/// The byte a [`Request::ScreenStream`] client writes to ask for a keyframe
pub const SCREEN_KEYFRAME: u8 = b'K';

/// Next to the control socket while the daemon is stopping, and left there when it has stopped (the next one removes
/// it when it is up): a client that can't connect just then waits for the daemon to be back ([`connect`])
pub fn stopping_marker(socket: &std::path::Path) -> std::path::PathBuf {
    socket.with_extension("stopping")
}

/// A daemon that stopped this recently is taken to be restarting
const RESTART_RECENT: std::time::Duration = std::time::Duration::from_secs(20);
/// How long a client waits for a restarting daemon
const RESTART_WAIT: std::time::Duration = std::time::Duration::from_secs(10);

/// Connects to the daemon. While it restarts (`systemctl restart`, an update) the socket is briefly gone or refuses:
/// then this waits for it, up to [`RESTART_WAIT`]. Nothing was sent yet, so trying again does nothing twice. A daemon
/// that isn't running (stopped a while ago, or never started) is reported at once
pub async fn connect(socket: &std::path::Path) -> std::io::Result<tokio::net::UnixStream> {
    let started = std::time::Instant::now();
    loop {
        match tokio::net::UnixStream::connect(socket).await {
            Err(e)
                if matches!(e.kind(), std::io::ErrorKind::NotFound | std::io::ErrorKind::ConnectionRefused)
                    && started.elapsed() < RESTART_WAIT
                    && restarting(socket) =>
            {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            }
            r => return r,
        }
    }
}

fn restarting(socket: &std::path::Path) -> bool {
    std::fs::metadata(stopping_marker(socket))
        .and_then(|m| m.modified())
        .is_ok_and(|t| t.elapsed().is_ok_and(|e| e < RESTART_RECENT))
}

/// Opens a [`Request::ScreenStream`]: `Ok(Ok(..))` with the reading half (buffered: it may already hold the first
/// messages) and the writing half, `Ok(Err(why))` when the daemon can't stream, `Err` when it can't be reached
pub async fn open_screen_stream(
    socket: &std::path::Path,
    expect_udid: Option<&str>,
    low_bandwidth: bool,
) -> std::io::Result<
    Result<(tokio::io::BufReader<tokio::net::unix::OwnedReadHalf>, tokio::net::unix::OwnedWriteHalf), String>,
> {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    let stream = connect(socket).await?;
    let (r, mut w) = stream.into_split();
    let mut v = serde_json::to_value(Request::ScreenStream)?;
    if let Some(o) = v.as_object_mut() {
        o.insert(LANG.into(), ictl_l10n::lang().code().into());
        if let Some(u) = expect_udid {
            o.insert(EXPECT_UDID.into(), u.into());
        }
        if low_bandwidth {
            o.insert(LOW_BANDWIDTH.into(), true.into());
        }
    }
    let mut line = v.to_string();
    line.push('\n');
    w.write_all(line.as_bytes()).await?;
    let mut r = BufReader::with_capacity(256 * 1024, r);
    let mut first = String::new();
    tokio::time::timeout(std::time::Duration::from_secs(30), r.read_line(&mut first))
        .await
        .map_err(|_| std::io::Error::new(std::io::ErrorKind::TimedOut, "screen stream: no answer"))??;
    Ok(match serde_json::from_str::<Response>(&first).map_err(std::io::Error::other)? {
        Response::Error { message, .. } => Err(message),
        _ => Ok((r, w)),
    })
}

/// 工具结果里报告系统弹窗挡着屏幕的固定写法(后面接「弹窗文字」)。MCP 端据此判断结果里是否已经报过弹窗,
/// 不能只找「系统弹窗」几个字:按名字点到的元素文字里也可能有
pub const ALERT_BLOCKING: &str = "屏幕上有系统弹窗";

/// `iphone_tap_element` 没点到时结果开头的几种写法(不算出错,单独调用时是普通结果;批量里要停下)
pub const TAP_MISSED: [&str; 3] = ["没找到", "只匹配到", "被挡住"];

/// The version from which clients and daemons no longer read results by their wording, only by [`Outcome`]
/// (every one since 0.1.5 sends it): remove the text fallbacks then
pub const TEXT_FALLBACK_UNTIL: (u64, u64) = (0, 3);

/// Did `iphone_tap_element` tap nothing? Daemons that send [`Outcome`] say so there; older ones only write
/// Chinese text, recognised by its [`TAP_MISSED`] prefix. [`Outcome`] came with 0.1.5: the text fallback (here,
/// in [`alert_reported`], and the fixed Chinese prefixes the daemon keeps for older MCP servers) goes in 0.3.0
/// ([`TEXT_FALLBACK_UNTIL`])
pub fn tapped_nothing(text: &str, outcome: Option<Outcome>) -> bool {
    match outcome {
        Some(o) => o.tapped_nothing,
        None => TAP_MISSED.iter().any(|p| text.starts_with(p)),
    }
}

/// Does the result already report a blocking system alert? As [`tapped_nothing`], with [`ALERT_BLOCKING`]
pub fn alert_reported(text: &str, outcome: Option<Outcome>) -> bool {
    match outcome {
        Some(o) => o.alert_reported,
        None => text.contains(ALERT_BLOCKING),
    }
}

/// 请求里带上这个键(值为 true):客户端发完请求不半关闭连接,守护进程据此把「读到 EOF」当成调用方已经走了,
/// 还在排队等设备的请求就不再执行。旧版本的客户端发完就半关闭、不带这个键,守护进程对它们照旧处理完。
/// 放在请求对象里而不是改协议格式:旧版本守护进程会忽略这个多出来的键
pub const CANCEL_ON_CLOSE: &str = "cancel_on_close";

/// 请求里带上这个键(值为 UDID):守护进程正在控制的不是这台就不执行。`iphone_batch` 用它防止中途换了设备
/// (自动选设备时拔掉正在控制的那台,会改控另一台)后,剩下的步骤落到另一台上
pub const EXPECT_UDID: &str = "expect_udid";
/// The live view whose geometry was used to calculate a coordinate.
pub const EXPECT_VIEW: &str = "expect_view";

/// Request key carrying a task's token (docs/task-control.md): the request is part of that task. While a task is
/// active the daemon only lets requests with its token act on the phone (and holds them while the task is paused or
/// taken over); a token of a task that ended refuses everything. Set it with [`join_task`] or [`as_task`]
pub const TASK: &str = "task";
/// Request key with the step (from [`Request::TaskStep`]) a request belongs to; see [`as_task`]
pub const TASK_STEP: &str = "task_step";
/// The environment variable that hands a task's token to `iphone-control mcp` (and anything else using this crate)
pub const TASK_TOKEN_ENV: &str = env::TASK_TOKEN;
/// The HTTP header that does the same for MCP over HTTP (the viewer's `/mcp`), per request
pub const TASK_TOKEN_HEADER: &str = "iphone-control-task-token";

static PROCESS_TASK: std::sync::OnceLock<String> = std::sync::OnceLock::new();

tokio::task_local! {
    static CALL_TASK: (Option<String>, Option<u64>);
}

/// Every request this process sends from now on belongs to the task with `token` (the MCP server of an agent
/// started for a task). Only the first call counts
pub fn join_task(token: &str) {
    let _ = PROCESS_TASK.set(token.to_owned());
}

/// The task this process joined ([`join_task`]), if any
pub fn process_task() -> Option<&'static str> {
    PROCESS_TASK.get().map(String::as_str)
}

/// Runs `f` with its requests sent as part of a task: `token` overrides the process's ([`join_task`]), `step` says
/// which step they belong to
pub async fn as_task<F: std::future::Future>(token: Option<String>, step: Option<u64>, f: F) -> F::Output {
    let token = token.or_else(|| current_task().0);
    CALL_TASK.scope((token, step), f).await
}

/// Whether the requests sent from here belong to a task ([`as_task`] or [`join_task`])
pub fn in_task() -> bool {
    current_task().0.is_some()
}

fn current_task() -> (Option<String>, Option<u64>) {
    let (token, step) = CALL_TASK.try_with(Clone::clone).unwrap_or_default();
    (token.or_else(|| process_task().map(str::to_owned)), step)
}

/// Request key carrying the client's language (`zh` / `en`). The daemon answers in it; requests without it
/// (older clients) get Chinese, as before. Older daemons ignore the extra key
pub const LANG: &str = "lang";
/// Request key: a poll the caller repeats anyway (the control page's status and alert checks). The daemon skips it
/// when an action waits for the phone, and drops it when one comes while it runs, answering [`Data::Busy`]: such a
/// check took the phone for up to a second (WDA's alert lookup on the home screen) while a tap waited behind it
pub const BACKGROUND: &str = "background";
/// Request key: someone watches the screen (the control page), so the daemon doesn't look before acting for what they
/// see anyway. Pressing Home skips the alert lookup that comes first (0.8 s of a 1.3 s press on the home screen) and
/// looks only when the press didn't get to the home screen; a swipe through WDA doesn't wait for the list to stop (up
/// to 2.5 s of reading the element tree again and again, 3.1 s for a swipe on an iPad mini 5)
pub const WATCHED: &str = "watched";
/// Request key of a [`Request::ScreenStream`]: the page is in low-bandwidth mode, so the phone's video is capped at a
/// lower bitrate while it watches (for every page watching that phone: there is one stream). Older daemons ignore it
pub const LOW_BANDWIDTH: &str = "low_bandwidth";

/// 发一个请求、读一个响应(客户端用)。`timeout` 为 None 时一直等(例如 `sign` 要等签名装完)。
/// 返回之前被丢掉(超时、调用方取消)时连接随之关闭,守护进程会放弃还在排队的这个请求
pub async fn call(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
) -> std::io::Result<Response> {
    call_expecting(socket, req, timeout, None).await
}

/// A [`BACKGROUND`] request: answered with [`Data::Busy`] instead of keeping an action waiting
pub async fn call_background(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
) -> std::io::Result<Response> {
    exchange_on(socket, req, timeout, None, None, Some(BACKGROUND)).await.map_err(|e| e.source)
}

/// 同 [`call`];`expect_udid` 见 [`EXPECT_UDID`]
pub async fn call_expecting(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
    expect_udid: Option<&str>,
) -> std::io::Result<Response> {
    exchange_on(socket, req, timeout, expect_udid, None, None).await.map_err(|e| e.source)
}

/// [`call_expecting`] for someone watching the screen ([`WATCHED`])
pub async fn call_watched(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
    expect_udid: Option<&str>,
) -> std::io::Result<Response> {
    exchange_on(socket, req, timeout, expect_udid, None, Some(WATCHED)).await.map_err(|e| e.source)
}

/// Send a coordinate action only if this device and its measured geometry still match. Only the control page sends
/// these, so they say someone watches the screen ([`WATCHED`])
pub async fn call_in_view(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
    udid: &str,
    view: &str,
) -> std::io::Result<Response> {
    exchange_on(socket, req, timeout, Some(udid), Some(view), Some(WATCHED)).await.map_err(|e| e.source)
}

/// Transport failure with the phase of this single exchange. Once writing begins, a missing
/// reply cannot tell the caller whether the daemon acted.
#[derive(Debug)]
pub struct ExchangeError {
    pub source: std::io::Error,
    pub failure: ActionFailure,
}

/// Like the convenience call functions above, retaining transport phase for action results.
pub async fn exchange_on(
    socket: &std::path::Path,
    req: &Request,
    timeout: Option<std::time::Duration>,
    expect_udid: Option<&str>,
    expect_view: Option<&str>,
    flag: Option<&str>,
) -> Result<Response, ExchangeError> {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    let mut stage = FailureStage::Connect;
    let exchange = async {
        let stream = connect(socket).await?;
        let (r, mut w) = stream.into_split();
        stage = FailureStage::Preflight;
        let mut v = serde_json::to_value(req)?;
        if let Some(o) = v.as_object_mut() {
            o.insert(CANCEL_ON_CLOSE.into(), true.into());
            o.insert(LANG.into(), ictl_l10n::lang().code().into());
            if let Some(view) = expect_view {
                o.insert(EXPECT_VIEW.into(), view.into());
            }
            if let Some(u) = expect_udid {
                o.insert(EXPECT_UDID.into(), u.into());
            }
            // [`BACKGROUND`] or [`WATCHED`]
            if let Some(f) = flag {
                o.insert(f.into(), true.into());
            }
            add_task_keys(o);
        }
        let mut line = v.to_string();
        line.push('\n');
        stage = FailureStage::Send;
        w.write_all(line.as_bytes()).await?;
        stage = FailureStage::Reply;
        let mut resp = String::new();
        BufReader::new(r).read_line(&mut resp).await?;
        serde_json::from_str(&resp).map_err(std::io::Error::other)
    };
    let result = match timeout {
        None => exchange.await,
        Some(t) => tokio::time::timeout(t, exchange).await.unwrap_or_else(|_| {
            Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                ictl_l10n::trf!("守护进程 {} 秒内没有应答(可能正忙,例如正在启动 WDA 或重签)", "The daemon didn't answer within {} seconds (it may be busy, for example starting WDA or re-signing)", t.as_secs()),
            ))
        }),
    };
    result.map_err(|source| {
        let delivery = match stage {
            FailureStage::Connect | FailureStage::Preflight => Delivery::NotSent,
            _ => Delivery::Unknown,
        };
        ExchangeError { source, failure: ActionFailure { delivery, stage } }
    })
}

fn add_task_keys(o: &mut serde_json::Map<String, serde_json::Value>) {
    let (token, step) = current_task();
    if let Some(t) = token {
        o.insert(TASK.into(), t.into());
    }
    if let Some(s) = step {
        o.insert(TASK_STEP.into(), s.into());
    }
}

/// What [`open_task`] got back
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskOpened {
    pub id: String,
    pub token: String,
}

/// A task's events as the daemon streams them ([`Request::TaskOpen`], [`Request::TaskEvents`]). For an opened task
/// this is also its lease: dropping it ends the task as abandoned unless it ended before
pub struct TaskStream {
    lines: tokio::io::Lines<tokio::io::BufReader<tokio::net::unix::OwnedReadHalf>>,
    _w: tokio::net::unix::OwnedWriteHalf,
}

impl TaskStream {
    /// The next event; `None` when the task ended (after its `done`) or the daemon went away
    pub async fn next(&mut self) -> Option<TaskEvent> {
        loop {
            let line = self.lines.next_line().await.ok()??;
            if let Ok(e) = serde_json::from_str(&line) {
                return Some(e);
            }
        }
    }
}

async fn open_stream(socket: &std::path::Path, req: &Request) -> std::io::Result<(Response, TaskStream)> {
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    let stream = connect(socket).await?;
    let (r, mut w) = stream.into_split();
    let mut v = serde_json::to_value(req)?;
    if let Some(o) = v.as_object_mut() {
        o.insert(LANG.into(), ictl_l10n::lang().code().into());
        add_task_keys(o);
    }
    let mut line = v.to_string();
    line.push('\n');
    w.write_all(line.as_bytes()).await?;
    let mut lines = BufReader::new(r).lines();
    let first = lines.next_line().await?.ok_or_else(|| std::io::Error::other("the daemon closed the connection"))?;
    let resp = serde_json::from_str(&first).map_err(std::io::Error::other)?;
    Ok((resp, TaskStream { lines, _w: w }))
}

/// Opens a task bound to the device the daemon controls now ([`Request::TaskOpen`]). `Err` is a message
/// `apps`: the task's scope (see [`Request::TaskOpen`]), empty for none
pub async fn open_task(
    socket: &std::path::Path,
    title: &str,
    owner: &str,
    apps: &[String],
) -> Result<(TaskOpened, TaskStream), String> {
    let req = Request::TaskOpen { title: title.to_owned(), owner: owner.to_owned(), apps: apps.to_vec() };
    match open_stream(socket, &req).await.map_err(|e| e.to_string())? {
        (Response::Text { text, .. }, s) => Ok((serde_json::from_str(&text).map_err(|e| e.to_string())?, s)),
        (Response::Error { message, .. }, _) => Err(message),
        (Response::Image { caption, .. }, _) => Err(caption),
    }
}

/// Follows a task's events ([`Request::TaskEvents`]). `Err` is a message
pub async fn task_events(socket: &std::path::Path, id: &str) -> Result<TaskStream, String> {
    match open_stream(socket, &Request::TaskEvents { id: id.to_owned() }).await.map_err(|e| e.to_string())? {
        (Response::Text { .. }, s) => Ok(s),
        (Response::Error { message, .. }, _) => Err(message),
        (Response::Image { caption, .. }, _) => Err(caption),
    }
}

/// The id of the active task with `token` ([`Request::TaskCheck`]); `Err` is why not (ended, unknown, no daemon)
pub async fn task_check(socket: &std::path::Path, token: &str) -> Result<String, String> {
    let call = call(socket, &Request::TaskCheck, Some(std::time::Duration::from_secs(10)));
    match as_task(Some(token.to_owned()), None, call).await.map_err(|e| e.to_string())? {
        Response::Text { text, .. } => Ok(text),
        Response::Error { message, .. } => Err(message),
        Response::Image { caption, .. } => Err(caption),
    }
}

/// Recent tasks ([`Request::TaskList`])
pub async fn task_list(socket: &std::path::Path) -> Result<Vec<TaskInfo>, String> {
    match call(socket, &Request::TaskList, Some(std::time::Duration::from_secs(10))).await.map_err(|e| e.to_string())? {
        Response::Text { text, .. } => serde_json::from_str(&text).map_err(|e| e.to_string()),
        Response::Error { message, .. } => Err(message),
        Response::Image { caption, .. } => Err(caption),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty_input_only_sends_a_key_with_submit() {
        let reply = Response::text("ok");
        for (submit, delivery) in [(false, Delivery::NotSent), (true, Delivery::Sent)] {
            assert_eq!(reply.delivery(&Request::TypeText { text: String::new(), submit }), delivery);
        }
    }

    /// 不带 count 的点击(旧版本客户端)按 1 下;count 为 1 时不写进请求,旧版本守护进程收到的和以前一样
    #[test]
    fn a_tap_without_count_is_one_tap_on_the_wire() {
        let old: Request = serde_json::from_str(r#"{"op":"tap","x":1.0,"y":2.0}"#).unwrap();
        assert_eq!(old, Request::Tap { x: 1.0, y: 2.0, count: 1 });
        assert_eq!(serde_json::to_string(&old).unwrap(), r#"{"op":"tap","x":1.0,"y":2.0}"#);
        let five = Request::Tap { x: 1.0, y: 2.0, count: 5 };
        assert_eq!(serde_json::to_string(&five).unwrap(), r#"{"op":"tap","x":1.0,"y":2.0,"count":5}"#);
    }

    /// A daemon that is restarting (it left the marker just now) is waited for; one that isn't running is reported
    /// at once
    #[tokio::test]
    async fn clients_wait_for_a_restarting_daemon_only() {
        let dir = std::env::temp_dir().join(format!("ictl-connect-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let socket = dir.join("ctl.sock");
        let t = std::time::Instant::now();
        assert_eq!(connect(&socket).await.unwrap_err().kind(), std::io::ErrorKind::NotFound);
        assert!(t.elapsed() < std::time::Duration::from_millis(100), "not running: at once");

        std::fs::write(stopping_marker(&socket), b"").unwrap();
        let up = {
            let socket = socket.clone();
            tokio::spawn(async move {
                tokio::time::sleep(std::time::Duration::from_millis(700)).await;
                let l = tokio::net::UnixListener::bind(&socket).unwrap();
                let _ = std::fs::remove_file(stopping_marker(&socket));
                l.accept().await.unwrap()
            })
        };
        let t = std::time::Instant::now();
        connect(&socket).await.expect("connects once it is back");
        assert!(t.elapsed() >= std::time::Duration::from_millis(600));
        up.await.unwrap();
        std::fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn requests_round_trip_as_tagged_json() {
        let r = Request::TapElement { text: "x".into(), index: 0, max_age_ms: 0, scroll: None };
        // without a scroll search the request is what it always was on the wire
        assert_eq!(serde_json::to_string(&r).unwrap(), r#"{"op":"tap_element","text":"x","index":0,"max_age_ms":0}"#);
        let r = Request::TapElement {
            text: "x".into(),
            index: 0,
            max_age_ms: 0,
            scroll: Some(ScrollSearch { down: true, max: 8 }),
        };
        assert_eq!(serde_json::from_str::<Request>(&serde_json::to_string(&r).unwrap()).unwrap(), r);
        let r = Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.3, hold: 0.0 };
        let s = serde_json::to_string(&r).unwrap();
        // a plain swipe is what it always was on the wire: older daemons and clients see no change
        assert_eq!(s, r#"{"op":"swipe","from":[1.0,2.0],"to":[3.0,4.0],"seconds":0.3}"#);
        assert_eq!(serde_json::from_str::<Request>(&s).unwrap(), r);
        let r = Request::Swipe { from: (1.0, 2.0), to: (3.0, 4.0), seconds: 0.7, hold: 0.6 };
        let s = serde_json::to_string(&r).unwrap();
        assert!(s.ends_with(r#""hold":0.6}"#), "{s}");
        assert_eq!(serde_json::from_str::<Request>(&s).unwrap(), r);
        let r = Request::TapSequence { points: vec![(60.0, 548.0), (23.0, 548.0)] };
        let s = serde_json::to_string(&r).unwrap();
        assert_eq!(s, r#"{"op":"tap_sequence","points":[[60.0,548.0],[23.0,548.0]]}"#);
        assert_eq!(serde_json::from_str::<Request>(&s).unwrap(), r);
        let e: Response = serde_json::from_str(r#"{"kind":"error","message":"x"}"#).unwrap();
        assert_eq!(e, Response::error("x"));
    }

    /// Outcome travels as fields; flags that are off and a missing outcome (older daemons) stay off the wire,
    /// and older clients that don't know the field ignore it
    #[test]
    fn outcome_is_optional_on_the_wire() {
        let s = serde_json::to_string(&Response::text("x")).unwrap();
        assert_eq!(s, r#"{"kind":"text","text":"x"}"#);
        let s = serde_json::to_string(&Response::text_with("x", Outcome::default())).unwrap();
        assert_eq!(s, r#"{"kind":"text","text":"x","outcome":{}}"#);
        let missed = Response::text_with("Not found", Outcome { tapped_nothing: true, ..Default::default() });
        let s = serde_json::to_string(&missed).unwrap();
        assert_eq!(serde_json::from_str::<Response>(&s).unwrap(), missed);

        #[derive(Deserialize)]
        #[serde(tag = "kind", rename_all = "snake_case")]
        enum OldResponse {
            Text { text: String },
        }
        let OldResponse::Text { text } = serde_json::from_str(&s).unwrap();
        assert_eq!(text, "Not found");
    }

    /// Data travels tagged by its type, and only when there is some
    #[test]
    fn data_travels_with_the_text() {
        let status = Data::Status(StatusData {
            wda_ready: true,
            locked: Some(false),
            foreground: Some("com.apple.springboard".into()),
            width: Some(430.0),
            height: Some(932.0),
            ..Default::default()
        });
        let r = Response::text_data("WDA ready: True", Outcome::default(), Some(status));
        let s = serde_json::to_string(&r).unwrap();
        assert!(s.contains(r#""data":{"type":"status","wda_ready":true"#), "{s}");
        assert_eq!(serde_json::from_str::<Response>(&s).unwrap(), r);
        let el = ElementData { kind: "Button".into(), label: "OK".into(), x: 1.0, y: 2.0, width: 3.0, height: 4.0 };
        let v =
            serde_json::to_value(Data::Elements { elements: vec![el], matched_count: Some(1), truncated: Some(false) })
                .unwrap();
        assert_eq!(v["elements"][0]["type"], "Button", "the page reads `type`");
        assert!(!serde_json::to_string(&Response::text_with("x", Outcome::default())).unwrap().contains("data"));
    }

    /// A reminder that fails on time: from [`TEXT_FALLBACK_UNTIL`] on, delete the text fallbacks (`tapped_nothing`,
    /// `alert_reported`, the daemon's fixed Chinese prefixes) and this test
    #[test]
    fn the_text_fallback_is_removed_in_time() {
        let v: Vec<u64> = env!("CARGO_PKG_VERSION").split('.').map(|n| n.parse().unwrap()).collect();
        assert!((v[0], v[1]) < TEXT_FALLBACK_UNTIL, "remove the text fallbacks: every supported version sends Outcome");
    }

    /// The field decides; without it (older daemon) the Chinese text is read
    #[test]
    fn outcome_wins_over_text() {
        let missed = Outcome { tapped_nothing: true, ..Default::default() };
        assert!(tapped_nothing("Not found: no element contains \"x\"", Some(missed)));
        assert!(!tapped_nothing("没找到包含 \"x\" 的元素", Some(Outcome::default())));
        assert!(tapped_nothing("没找到包含 \"x\" 的元素", None));
        assert!(tapped_nothing("被挡住:Button \"x\" 整个被「浮层」盖住,没有点", None));
        assert!(!tapped_nothing("Not found: no element contains \"x\"", None));
        let alert = Outcome { alert_reported: true, ..Default::default() };
        assert!(alert_reported("Launched x, but a system alert is on screen: “Allow?”", Some(alert)));
        assert!(alert_reported("已启动 x,但屏幕上有系统弹窗「允许?」挡着", None));
        assert!(!alert_reported("Launched x", Some(Outcome::default())));
    }

    /// 旧版本守护进程直接反序列化请求:多出来的 cancel_on_close、lang 键不能让它报错(含没有字段的请求)
    #[test]
    fn extra_cancel_key_is_ignored_when_parsing() {
        for s in [
            r#"{"op":"status","cancel_on_close":true}"#,
            r#"{"op":"tap","x":1.0,"y":2.0,"cancel_on_close":true}"#,
            r#"{"op":"status","cancel_on_close":true,"lang":"en"}"#,
        ] {
            serde_json::from_str::<Request>(s).unwrap_or_else(|e| panic!("{s}: {e}"));
        }
    }
}