Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools
Rust ai-agentautomationclaudeclaude-codedockeriosiphonelinuxmcpmcp-servermodel-context-protocolrustwebdriveragent
docs: required tests for every pull request (#46)
Qinghe Lie4ad77211b33
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.mdindex 40c7924..2a53830 100644--- a/.github/PULL_REQUEST_TEMPLATE.md+++ b/.github/PULL_REQUEST_TEMPLATE.md@@ -2,15 +2,44 @@<!-- What this changes and why. Link the issue if there is one. 英文或中文都可以。 -->-## How it was tested+## Tests-- [ ] `cargo test --workspace --all-features`, `cargo clippy --workspace --all-targets --all-features -- -D warnings`, `cargo fmt --check` (per package)-- [ ] On a real phone (model, iOS version, what you ran — e.g. `tests/device/run.py`):-- [ ] Not needed (docs only, or no device-facing change)+<!-- Required for every pull request, see CONTRIBUTING.md, "Required tests". 每一项都要填写,不适用的写明原因。 -->++### 1. Unit tests++- [ ] `cargo test --workspace --all-features --locked`, `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` and `cargo fmt --check` (per package) pass+- [ ] New or changed behaviour / the fixed bug has a unit test: <!-- test names, or why there can't be one -->+- [ ] No existing test removed, ignored or loosened+- [ ] `#[ignore]` tests of the changed area run by hand: <!-- names, or "none apply" -->++### 2. Real-device regression++- Device, iOS version, host (Linux / macOS): <!-- e.g. iPhone 15 Pro Max, iOS 27.0.1, Debian 12 -->+- [ ] `tests/device/run.py` default steps pass on this branch (exit code 0)+- [ ] The same steps on `main` or the latest release, same device, for comparison; no step clearly slower, or why:+- [ ] Steps exercising this change: <!-- what you ran -->+- [ ] Longer run (WDA start-up/recovery, reconnecting, unlocking, live picture, timers): <!-- duration and result, or "not affected" -->+- [ ] Not needed: the change doesn't touch code that talks to a phone — why:++<details><summary>run.py output</summary>++```text+this branch:++main / latest release:+```++</details>++### 3. Regression cases (docs/dev/regression-cases.md)++- [ ] All cases still hold; device cases in the changed area re-run: <!-- #numbers, or "none apply" -->+- [ ] A newly fixed problem is added as a row: <!-- #number, or "not applicable" -->## Checklist- [ ] MCP tool names, parameters and default outputs stay compatible (docs/decisions.md D10)- [ ] New or changed dependencies are pinned to exact versions; vendor changes recorded in THIRD_PARTY.md- [ ] User-visible changes noted in CHANGELOG.md (and CHANGELOG.zh-CN.md if you can) under *Unreleased*-- [ ] No private data in fixtures, screenshots or logs+- [ ] No private data in fixtures, screenshots, logs or the outputs abovediff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 759526b..ac6bcf5 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -8,6 +8,7 @@ Most of the code comments, design documents and commit messages in this reposito- **Bugs:** include the iPhone or iPad model, iOS version, how you installed it (install.sh, Homebrew, .deb or source; Linux or macOS), and the output of `iphone-control doctor`, with private data removed. The [issue template](.github/ISSUE_TEMPLATE/bug_report.yml) asks for these.- **Larger changes:** open an issue first. The key design decisions and their reasons are in [docs/decisions.md](docs/decisions.md); the regression cases every change must keep passing are in [docs/dev/regression-cases.md](docs/dev/regression-cases.md).+- **Tests:** every pull request must pass the tests in [Required tests](#required-tests); read it before you start, since the device part needs a phone.- **Security problems:** don't open an issue — see [SECURITY.md](SECURITY.md).## Building and testing without a phone@@ -39,12 +40,12 @@ Anything that touches the device, timing or WebDriverAgent behaviour needs a rea```bashpython3 -m venv .venv && .venv/bin/pip install -r tests/device/requirements.txt-export MCP_CMD="iphone-control mcp" # run.py still defaults to the old container command+export MCP_CMD="$PWD/target/release/iphone-control mcp" # your build; the daemon must be the same build.venv/bin/python tests/device/run.py # default steps: harmless (Home, Clock tabs, Spotlight search).venv/bin/python tests/device/run.py '[["iphone_launch_app", {"app": "设置"}], ["iphone_screenshot", {}]]'```-`run.py` prints every result with its wall time and saves screenshots to `./shots`. Use the full path of the binary in `MCP_CMD` if it isn't on your `PATH`. Please mention in the pull request what you ran on which device and iOS version.+`run.py` prints every result with its wall time, ends with a line counting calls and failures, saves screenshots to `./shots` and exits with 1 if any call failed. `MCP_CMD` defaults to `iphone-control mcp` on your `PATH`. To test your branch, run its daemon too (stop the installed service with `iphone-control service stop`, then start `target/release/iphone-control daemon` in another terminal; `service start` afterwards). What a pull request has to run on the device is in [Required tests](#required-tests).On macOS, the full checklist (setup, `debug` checks, coexistence with Finder, reconnect) is in [docs/dev/macos-verification.md](docs/dev/macos-verification.md).@@ -71,9 +72,39 @@ Notes:- **Never commit** `data/`, `~/.config/iphone-control/env` or other settings files with secrets, certificates, provisioning profiles, pairing records or anything Apple ships (Developer Disk Images, frameworks).- Comments explain *why*, especially device quirks; put measured numbers next to timeouts and thresholds.+## Required tests++Every pull request must pass the three kinds of tests below, and its description must show it: the [pull request template](.github/PULL_REQUEST_TEMPLATE.md) has a section for each. A pull request without them is asked for them before review. The rules apply to maintainers too. 每个 PR 都必须通过下面三类测试,并在描述里写明结果。++### 1. Unit tests — every pull request++- `cargo test`, clippy with `-D warnings` and `cargo fmt --check` ([commands above](#building-and-testing-without-a-phone)) pass on your machine. CI runs them on Linux and macOS, plus the MSRV check, gitleaks and the package checks; a pull request with a failing check isn't merged.+- New behaviour and every bug fix come with a unit test when the logic can run without a phone (parsing, decisions, state, arguments, protocol). For a fix, the test fails without it. Name the tests in the pull request, or say why there can't be one.+- Don't delete, `#[ignore]` or loosen an existing test to make a change pass. If a test is wrong, fix it and explain why in the pull request.+- Tests marked `#[ignore]` are run by hand when their area changes, and named in the pull request: `slow_page_stays_close_to_live` (`apps/iphone-control/src/video/`, needs ffmpeg) and `measure_recording` (`video/vt.rs`, macOS only). Run one with `cargo test --workspace --all-features --locked -- --ignored --nocapture <name>`.++### 2. Real-device regression — every pull request that touches the phone++Needed when the change touches code that talks to a phone or changes what it does or how long it takes: anything under `crates/` and `apps/` except pure text changes (comments, translations that don't change meaning). Not needed for changes limited to documentation, the website, CI, packaging or other tests; say so in the template.++- **Default steps:** build your branch, run its daemon and `tests/device/run.py` with the default steps on a real iPhone or iPad ([Testing on a real phone](#testing-on-a-real-phone)). Every call succeeds and the exit code is 0.+- **No slower, no less stable:** on the same device, run the default steps once more on `main` (or the latest release), and paste both outputs. A step that became clearly slower (more than about 20 %, or by more than 0.5 s) needs a reason. A change that could affect long-running behaviour (WDA start-up and recovery, reconnecting, unlocking, the live picture, timers) also needs a longer run: repeat the default steps for at least an hour, e.g. `for i in $(seq 12); do .venv/bin/python tests/device/run.py || break; sleep 300; done`, and report failures and how the times developed.+- **What you changed:** also run steps that exercise the change itself (pass them as a JSON list) and paste the output.+- **The right hardware:** when the change concerns iPad, landscape, an iOS version or a platform, test on that. Changes to macOS-only code go through [docs/dev/macos-verification.md](docs/dev/macos-verification.md).+- **In the pull request:** device model, iOS version, Linux or macOS, and the outputs (the `===` lines and the last line are enough; remove private data).++### 3. Regression cases — every pull request++[docs/dev/regression-cases.md](docs/dev/regression-cases.md) lists the problems this project has run into, with the right behaviour and how each is tested. Every pull request must keep all of them passing.++- Cases tested by unit tests are covered by part 1.+- Cases tested on a device (真机, headless Chrome, measured by hand) in the area you changed: run them again and list their numbers in the pull request. To find them, search the file for the module, tool or behaviour you changed; if none apply, say so.+- A pull request that fixes a problem not in the list adds a row: the problem, the right behaviour, and how it is tested (the unit test's name, or the device steps).+## Pull requests-- Keep them focused; describe what changed and how you tested it (offline and/or which device).+- Keep them focused; describe what changed and why.+- The tests above pass and are reported in the template ([Required tests](#required-tests)).- CI must pass: format, clippy with `-D warnings`, tests (on Linux and macOS), the MSRV check, gitleaks and the package checks (`.deb`, `install.sh`, the Homebrew formula).- User-visible changes get a line in [CHANGELOG.md](CHANGELOG.md) and [CHANGELOG.zh-CN.md](CHANGELOG.zh-CN.md) under *Unreleased* (a Chinese line is welcome but optional for outside contributors; maintainers add it before release).diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.mdindex 4db45d9..a403d35 100644--- a/docs/dev/regression-cases.md+++ b/docs/dev/regression-cases.md@@ -1,6 +1,6 @@# 必须覆盖的回归用例-#1~#34 来自 toolkit 和本项目早期实际踩过的坑,#35~#45 是多设备和 HID 横屏,#46~#58 是实时画面、开发者工具和英文界面,#59~#63 是 iPhone 18 Pro Max(iOS 27.2.1)真机全量测试发现的问题,#64 起是 viewer 的 AI 任务和新页面。能单测的写单测,不能单测的进真机回归清单:改了设备、时序、WDA 相关的代码,要在真机上重跑相关的用例,并在 PR 里写明跑了哪些、用什么设备和 iOS 版本。+#1~#34 来自 toolkit 和本项目早期实际踩过的坑,#35~#45 是多设备和 HID 横屏,#46~#58 是实时画面、开发者工具和英文界面,#59~#63 是 iPhone 18 Pro Max(iOS 27.2.1)真机全量测试发现的问题,#64 起是 viewer 的 AI 任务和新页面。能单测的写单测,不能单测的进真机回归清单。**每个 PR 都必须保持这里的全部用例通过**:单测的由 `cargo test` 覆盖;改到的范围里测试方式是真机、无头 Chrome 或手动测量的,要重跑并在 PR 里列出编号,写明设备和 iOS 版本;修了清单里没有的问题,要加一行。完整要求见 [CONTRIBUTING.md 的 Required tests](../../CONTRIBUTING.md#required-tests)。| # | 坑 | 正确行为 | 测试方式 ||---|---|---|---|diff --git a/tests/device/run.py b/tests/device/run.pyindex 3ed2b8e..af41c21 100644--- a/tests/device/run.py+++ b/tests/device/run.py@@ -5,9 +5,11 @@ result with its wall time, and save screenshots.python tests/device/run.py # default steps (harmless: Home, Clock tabs, Spotlight search)python tests/device/run.py '[["iphone_status", {}], ["sleep", 1], ["iphone_screenshot", {}]]'-The MCP server command comes from MCP_CMD (default: `iphone-control mcp`;-for a source install use e.g. `iphone-control mcp`). Screenshots go to SHOTS_DIR (default: ./shots).+The MCP server command comes from MCP_CMD (default: `iphone-control mcp`; for a build of your branch use its+full path, e.g. `target/release/iphone-control mcp`). Screenshots go to SHOTS_DIR (default: ./shots).A step `["sleep", seconds]` waits without calling a tool. Exit code is 1 if any tool returned an error.+The last line sums it up (calls, failures, total time): paste the output into the pull request, see+CONTRIBUTING.md, "Required tests"."""import asyncio@@ -46,7 +48,11 @@ async def main() -> int:shots = os.environ.get("SHOTS_DIR", "shots")os.makedirs(shots, exist_ok=True)failed = 0- params = StdioServerParameters(command=cmd[0], args=cmd[1:])+ # the whole environment: the MCP SDK passes only a few variables by default, and without XDG_RUNTIME_DIR the+ # server looks for the daemon's socket in the wrong place (every call fails at once)+ params = StdioServerParameters(command=cmd[0], args=cmd[1:], env=dict(os.environ))+ calls = 0+ t0 = time.time()async with stdio_client(params) as (r, w), ClientSession(r, w) as session:await session.initialize()n = 0@@ -67,9 +73,11 @@ async def main() -> int:out.append(f"<image {path}>")else:out.append(c.text)+ calls += 1failed += bool(res.is_error)flag = " ERROR" if res.is_error else ""print(f"=== {name} {json.dumps(args, ensure_ascii=False)} {dt:.2f}s{flag}\n" + "\n".join(out), flush=True)+ print(f"--- {calls} calls, {failed} failed, {time.time() - t0:.1f}s in all", flush=True)return 1 if failed else 0