iphone-cli 0.1.1

Let AI agents read and operate any app on a real iPhone through the accessibility tree. No XCUITest, WebDriverAgent or Xcode signing.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! `iph doctor`: check, in order, everything the phone needs for iph to work,
//! and say in one line what the user must do for each thing that is missing.
//!
//! Which checks are truly required was established on an iPhone 15 Pro Max
//! with iOS 27.0.1; see docs/onboarding.md.

use std::time::Duration;

use idevice::{
    IdeviceService, amfi::AmfiClient, lockdown::LockdownClient,
    mobile_image_mounter::ImageMounter, provider::IdeviceProvider, usbmuxd::UsbmuxdConnection,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::{
    actions::{LaunchArgs, ScrollArgs},
    ax::tree::BuildOptions,
    device::{
        ax, conn,
        session::{Fresh, Session},
    },
    fields,
    i18n::t,
    reply::Res,
};

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct DoctorArgs {
    /// Skip the check that sends a real touch (it brings Settings to the front
    /// and scrolls its list down and back).
    pub no_touch_test: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct Check {
    pub check: &'static str,
    /// "ok" | "fail" | "warn" | "skipped" | "info"
    pub status: &'static str,
    pub required: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_action: Option<String>,
    pub auto_fixed: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

impl Check {
    fn new(check: &'static str, required: bool) -> Self {
        Self {
            check,
            status: "ok",
            required,
            user_action: None,
            auto_fixed: false,
            detail: None,
        }
    }

    fn status(mut self, status: &'static str) -> Self {
        self.status = status;
        self
    }

    fn action(mut self, ko: &str, en: &str) -> Self {
        self.user_action = Some(t(ko, en).to_string());
        self
    }

    fn detail(mut self, detail: impl Into<String>) -> Self {
        self.detail = Some(detail.into());
        self
    }
}

const SETTINGS: &str = "com.apple.Preferences";
const NEEDED_SERVICES: &[&str] = &[
    ax::SERVICE,
    "com.apple.coredevice.displayservice",
    "com.apple.coredevice.hid.universalhidservice",
    "com.apple.coredevice.appservice",
];

async fn with_timeout<T>(fut: impl std::future::Future<Output = T>) -> Option<T> {
    tokio::time::timeout(Duration::from_secs(15), fut).await.ok()
}

struct LockdownFacts {
    developer_mode: Option<bool>,
    /// `PasswordProtected`: true while the phone is locked with a passcode.
    locked: Option<bool>,
}

/// What lockdown says about Developer Mode and the lock. Read-only.
async fn lockdown_facts(provider: &dyn IdeviceProvider) -> LockdownFacts {
    let mut facts = LockdownFacts { developer_mode: None, locked: None };
    // The image mounter answers the Developer Mode question most reliably;
    // the AMFI service closed the connection on iOS 27.0.1 over Wi-Fi.
    if let Some(Ok(mut mounter)) = with_timeout(ImageMounter::connect(provider)).await {
        facts.developer_mode = with_timeout(mounter.query_developer_mode_status())
            .await
            .and_then(|r| r.ok());
    }
    if let Some(Ok(mut lockdown)) = with_timeout(LockdownClient::connect(provider)).await {
        if let Some(Ok(pairing)) = with_timeout(provider.get_pairing_file()).await {
            let _ = with_timeout(lockdown.start_session(&pairing)).await;
        }
        if facts.developer_mode.is_none() {
            facts.developer_mode = with_timeout(
                lockdown.get_value(Some("DeveloperModeStatus"), Some("com.apple.security.mac.amfi")),
            )
            .await
            .and_then(|r| r.ok())
            .and_then(|v| v.as_boolean());
        }
    }
    facts.locked = conn::passcode_locked(provider).await;
    facts
}

/// Developer services the tunnel does not offer. `Err` when the tunnel itself
/// cannot be opened.
async fn missing_services(s: &mut Session) -> Result<Vec<&'static str>, String> {
    match s.ensure_tunnel().await {
        Ok(tunnel) => Ok(NEEDED_SERVICES
            .iter()
            .filter(|name| !tunnel.rsd.services.contains_key(**name))
            .copied()
            .collect()),
        Err(f) => Err(f.message),
    }
}

fn hex_field(v: Option<&plist::Value>) -> Option<u64> {
    match v? {
        plist::Value::String(s) => u64::from_str_radix(s.trim_start_matches("0x"), 16).ok(),
        plist::Value::Integer(i) => i.as_unsigned(),
        _ => None,
    }
}

/// Mount the personalized developer disk image that Xcode keeps on this Mac.
///
/// Written from the documented flow (match the build identity to the phone's
/// board and chip, have Apple sign it, upload, mount). It could not be tried
/// on the test phone, whose image was already mounted.
async fn mount_ddi(provider: &dyn IdeviceProvider) -> anyhow::Result<()> {
    use anyhow::{Context, anyhow};
    let restore = crate::ddi::xcode_restore()
        .ok_or_else(|| anyhow!("no Xcode developer disk image on this computer"))?;

    let manifest_bytes = std::fs::read(restore.join("BuildManifest.plist"))?;
    let manifest: plist::Value = plist::from_bytes(&manifest_bytes).context("BuildManifest.plist")?;

    let mut lockdown = LockdownClient::connect(provider)
        .await
        .map_err(|e| anyhow!("lockdown: {e:?}"))?;
    if let Ok(pairing) = provider.get_pairing_file().await {
        let _ = lockdown.start_session(&pairing).await;
    }
    let number = |v: plist::Value| v.as_unsigned_integer();
    let board = lockdown.get_value(Some("BoardId"), None).await.ok().and_then(number);
    let chip = lockdown.get_value(Some("ChipID"), None).await.ok().and_then(number);
    let ecid = lockdown
        .get_value(Some("UniqueChipID"), None)
        .await
        .ok()
        .and_then(number)
        .ok_or_else(|| anyhow!("the phone did not report its chip id"))?;

    let identities = manifest
        .as_dictionary()
        .and_then(|d| d.get("BuildIdentities"))
        .and_then(|v| v.as_array())
        .ok_or_else(|| anyhow!("BuildManifest has no BuildIdentities"))?;
    let identity = identities
        .iter()
        .filter_map(|i| i.as_dictionary())
        .find(|i| hex_field(i.get("ApBoardID")) == board && hex_field(i.get("ApChipID")) == chip)
        .ok_or_else(|| anyhow!("no Xcode developer disk image on this computer matches this phone model"))?;
    let path_of = |component: &str| -> anyhow::Result<std::path::PathBuf> {
        identity
            .get("Manifest")
            .and_then(|m| m.as_dictionary())
            .and_then(|m| m.get(component))
            .and_then(|c| c.as_dictionary())
            .and_then(|c| c.get("Info"))
            .and_then(|i| i.as_dictionary())
            .and_then(|i| i.get("Path"))
            .and_then(|p| p.as_string())
            .map(|p| restore.join(p))
            .ok_or_else(|| anyhow!("BuildManifest has no path for {component}"))
    };
    let image = std::fs::read(path_of("PersonalizedDMG")?)?;
    let trust_cache = std::fs::read(path_of("LoadableTrustCache")?)?;

    let mut mounter = ImageMounter::connect(provider)
        .await
        .map_err(|e| anyhow!("image mounter: {e:?}"))?;
    mounter
        .mount_personalized(provider, image, trust_cache, &manifest_bytes, None, ecid)
        .await
        .map_err(|e| anyhow!("mount: {e:?}"))
}

/// The checks for "the phone is unlocked and Settings is in front", and
/// whether Settings is in front.
///
/// `locked`: lockdown's word. `launched`: whether the launch of Settings was
/// seen in front, or why it failed; `None` when not tried. `read`: when it was
/// not seen, a reading taken after: whether Settings is in front on it, or why
/// it failed.
fn front_checks(
    locked: Option<bool>,
    launched: Option<Result<bool, String>>,
    read: Option<Result<bool, String>>,
) -> (Vec<Check>, bool) {
    let mut out = Vec::new();
    if locked == Some(true) {
        out.push(
            Check::new("screen_unlocked", true)
                .status("fail")
                .detail("the phone reports that it is locked")
                .action("폰 잠금을 풀어 주세요.", "Unlock the phone."),
        );
        return (out, false);
    }
    let unlocked = match locked {
        Some(false) => Check::new("screen_unlocked", true).detail("lockdown: not locked"),
        _ => Check::new("screen_unlocked", true).detail("lockdown did not say; Settings came to the front"),
    };
    if launched == Some(Ok(true)) || read == Some(Ok(true)) {
        out.push(unlocked);
        return (out, true);
    }
    match (&launched, &read) {
        (_, Some(Err(why))) => {
            // The screen cannot be read: that is what to report, lock or not.
            out.push(match locked {
                Some(false) => Check::new("screen_unlocked", true).detail("lockdown: not locked"),
                _ => Check::new("screen_unlocked", true)
                    .status("skipped")
                    .detail("lockdown did not say, and the screen cannot be read"),
            });
            out.push(
                Check::new("accessibility_tree", true)
                    .status("fail")
                    .detail(why.clone())
                    .action(
                        "화면 읽기 응답이 없습니다(폰의 화면 읽기 서비스 axauditd). iph가 그 서비스를 다시 띄웠거나 2분 안에 다시 띄웁니다. 잠시 뒤 `iph doctor`를 다시 실행하고, 계속되면 폰을 재시동하세요.",
                        "No answer from the phone's screen reading (its axauditd service). iph has restarted it or will within 2 minutes. Run `iph doctor` again shortly; if it keeps failing, restart the phone.",
                    ),
            );
        }
        (Some(Err(why)), _) => {
            out.push(unlocked);
            out.push(
                Check::new("settings_in_front", true)
                    .status("fail")
                    .detail(why.clone())
                    .action("설정 앱을 열지 못했습니다. 폰 화면을 확인하고 다시 시도하세요.", "Settings could not be opened. Check the phone's screen and try again."),
            );
        }
        _ => {
            // The screen reads, and shows something else.
            out.push(unlocked);
            out.push(
                Check::new("settings_in_front", true)
                    .status("fail")
                    .detail("Settings was launched but another screen is in front")
                    .action(
                        "설정 앱이 앞에 나오지 않았습니다. 다른 창이나 알림이 가리고 있는지 폰 화면을 확인하세요.",
                        "Settings did not come to the front. Check the phone's screen for a window or alert in the way.",
                    ),
            );
        }
    }
    (out, false)
}

pub async fn run(s: &mut Session, args: DoctorArgs) -> Res {
    let mut checks: Vec<Check> = Vec::new();
    let skip = |name: &'static str, required: bool, why: &str| {
        Check::new(name, required).status("skipped").detail(why.to_string())
    };

    // 1. Pairing: the Mac holds a pairing record for this phone.
    let paired = match UsbmuxdConnection::default().await {
        Ok(mut mux) => with_timeout(mux.get_pair_record(&s.udid)).await.is_some_and(|r| r.is_ok()),
        Err(_) => false,
    };
    checks.push(if paired {
        Check::new("pairing", true)
    } else {
        Check::new("pairing", true).status("fail").action(
            "USB로 연결하고 폰에서 '이 컴퓨터를 신뢰'를 누르세요.",
            "Connect the phone by USB and tap \"Trust This Computer\" on it.",
        )
    });

    // 2. Reachable at all, and how.
    let route = conn::route(&s.udid).await;
    let connection = route.as_ref().ok().map(|r| r.connection.clone());
    checks.push(match &route {
        Ok(r) => Check::new("connection", true).detail(r.connection.clone()),
        Err(e) => Check::new("connection", true)
            .status("fail")
            .detail(format!("{e:#}"))
            .action(
                "폰을 USB로 연결하거나, 컴퓨터와 같은 Wi-Fi에 두고 화면을 켜 주세요.",
                "Plug the phone in, or put it on the computer's Wi-Fi and wake its screen.",
            ),
    });

    // 3. Developer Mode. If it is off, make the switch appear in Settings.
    let facts = match &route {
        Ok(r) => Some(lockdown_facts(&*r.provider).await),
        Err(_) => None,
    };
    let dev_mode = facts.as_ref().and_then(|f| f.developer_mode);
    checks.push(match dev_mode {
        Some(true) => Check::new("developer_mode", true),
        Some(false) => {
            let mut revealed = false;
            if let Ok(r) = &route
                && let Some(Ok(mut amfi)) = with_timeout(AmfiClient::connect(&*r.provider)).await {
                    revealed = with_timeout(amfi.reveal_developer_mode_option_in_ui())
                        .await
                        .is_some_and(|r| r.is_ok());
                }
            let mut c = Check::new("developer_mode", true).status("fail").action(
                "설정 → 개인정보 보호 및 보안 → 개발자 모드 → 켜기 → 재시동 후 비밀번호를 입력하세요.",
                "Settings → Privacy & Security → Developer Mode → turn on → restart, then enter the passcode.",
            );
            c.auto_fixed = revealed;
            if revealed {
                c = c.detail("the Developer Mode switch was made visible in Settings");
            }
            c
        }
        None if route.is_ok() => Check::new("developer_mode", true)
            .status("warn")
            .detail("could not ask the phone; judged by the services below"),
        None => skip("developer_mode", true, "phone not reachable"),
    });

    // 4. Developer disk image: its services must be on offer over the tunnel.
    let mut services_ok = false;
    if route.is_ok() && dev_mode != Some(false) {
        let mut missing = missing_services(s).await;
        let mut mounted_from = None;
        let mut mount_note = None;
        if matches!(&missing, Ok(m) if !m.is_empty()) {
            // Install the image through cryptexd (Xcode's copy, else one
            // fetched once), then look again. Phones before iOS 17.4 take it
            // through the image mounter instead, from Xcode's copy only.
            let cryptex = match s.ensure_tunnel().await {
                Ok(tunnel) => match tokio::time::timeout(Duration::from_secs(300), crate::ddi::install(tunnel)).await {
                    Ok(r) => r,
                    Err(_) => Err(anyhow::anyhow!("timed out")),
                },
                Err(f) => Err(anyhow::anyhow!(f.message)),
            };
            match cryptex {
                Ok(source) => mounted_from = Some(source),
                Err(e) => {
                    let mut note = format!("cryptex install failed: {e:#}");
                    if let Ok(r) = &route {
                        match tokio::time::timeout(Duration::from_secs(180), mount_ddi(&*r.provider)).await {
                            Ok(Ok(())) => mounted_from = Some("Xcode".to_string()),
                            Ok(Err(e)) => note.push_str(&format!("; image mounter: {e:#}")),
                            Err(_) => note.push_str("; image mounter timed out"),
                        }
                    }
                    if mounted_from.is_none() {
                        mount_note = Some(note);
                    }
                }
            }
            if mounted_from.is_some() {
                s.reset();
                tokio::time::sleep(Duration::from_secs(2)).await;
                missing = missing_services(s).await;
            }
        }
        let mounted_now = mounted_from.is_some();
        match missing {
            Ok(m) if m.is_empty() => {
                services_ok = true;
                let mut c = Check::new("developer_disk_image", true);
                c.auto_fixed = mounted_now;
                if let Some(from) = &mounted_from {
                    c = c.detail(format!("installed the developer disk image ({from})"));
                }
                checks.push(c);
            }
            Ok(m) => {
                let mut detail = format!("missing services: {}", m.join(", "));
                if let Some(note) = mount_note {
                    detail.push_str("; ");
                    detail.push_str(&note);
                }
                checks.push(Check::new("developer_disk_image", true).status("fail").detail(detail).action(
                    "개발자 디스크 이미지를 연결하지 못했습니다. 인터넷 연결(애플 서명 서버, GitHub)을 확인하고 다시 `iph doctor`를 실행하세요. 맥이라면 Xcode의 창 → 기기 및 시뮬레이터에서 이 폰을 한 번 골라도 됩니다.",
                    "Could not install the developer disk image. Check the internet connection (Apple's signing server, GitHub) and run `iph doctor` again. On a Mac, selecting this phone once in Xcode → Window → Devices and Simulators also works.",
                ));
            }
            Err(message) => checks.push(
                Check::new("developer_disk_image", true)
                    .status("fail")
                    .detail(message)
                    .action(
                        "폰 잠금을 풀고 다시 시도하세요. 계속 실패하면 USB로 연결하세요.",
                        "Unlock the phone and try again. If it keeps failing, connect by USB.",
                    ),
            ),
        }
    } else {
        checks.push(skip(
            "developer_disk_image",
            true,
            if route.is_ok() { "Developer Mode is off" } else { "phone not reachable" },
        ));
    }

    // 5. Wi-Fi (recommended): works without the cable.
    checks.push(match connection.as_deref() {
        Some("wifi") => Check::new("wifi", false),
        Some(_) => {
            let also_wifi = conn::list_raw().await.ok().is_some_and(|all| {
                all.iter().any(|d| {
                    d.udid == s.udid && matches!(d.connection_type, idevice::usbmuxd::Connection::Network(_))
                })
            });
            if also_wifi {
                Check::new("wifi", false).detail("reachable over Wi-Fi as well as USB")
            } else {
                Check::new("wifi", false).status("warn").action(
                    "케이블 없이 쓰려면 폰과 컴퓨터를 같은 Wi-Fi에 두세요. (맥은 Finder, 윈도는 Apple 기기 앱에서 폰을 고르고 'Wi-Fi에 있을 때 이 iPhone 보기'를 켜야 할 수 있습니다.)",
                    "To work without the cable, put the phone and the computer on the same Wi-Fi. (You may need \"Show this iPhone when on Wi-Fi\" in Finder on a Mac, or in the Apple Devices app on Windows.)",
                )
            }
        }
        None => skip("wifi", false, "phone not reachable"),
    });

    // 6. Unlocked, and Settings comes to the front. Only lockdown's word
    // makes this a lock: a launch that cannot be seen in front can as well
    // be a screen that cannot be read (2026-11-11: 25 minutes of "unlock the
    // phone" on an unlocked phone whose accessibility daemon answered only
    // with empty readings).
    let locked = facts.as_ref().and_then(|f| f.locked);
    let mut front_ok = false;
    let mut tree_reported = false;
    if locked == Some(true) || services_ok {
        let launched = if locked == Some(true) {
            None
        } else {
            Some(match s.cmd_launch(LaunchArgs { app: SETTINGS.into(), restart: false }).await {
                Ok(r) => Ok((
                    r.get("foreground").and_then(|v| v.as_bool()) == Some(true),
                    r.get("pid").and_then(|v| v.as_i64()),
                )),
                Err(f) => Err(f.message),
            })
        };
        // Not seen in front: is the screen readable at all, and what is on it?
        let read = match &launched {
            None | Some(Ok((true, _))) => None,
            Some(other) => {
                let pid = other.as_ref().ok().and_then(|(_, pid)| *pid);
                Some(match s.screen(Fresh::Force, false, BuildOptions::default()).await {
                    Ok((_, screen)) => Ok(pid.is_some_and(|p| screen.apps.iter().any(|a| a.pid == p))),
                    Err(f) => Err(f.message),
                })
            }
        };
        let launched = launched.map(|l| l.map(|(front, _)| front));
        let (found, front) = front_checks(locked, launched, read);
        tree_reported = found.iter().any(|c| c.check == "accessibility_tree");
        front_ok = front;
        checks.extend(found);
    } else {
        checks.push(skip("screen_unlocked", true, "developer services unavailable"));
    }

    // 7. The accessibility tree arrives.
    let mut tree_ok = false;
    if front_ok {
        match s.screen(Fresh::Force, false, BuildOptions::default()).await {
            Ok((_, screen)) if screen.elements.len() > 1 => {
                tree_ok = true;
                checks.push(
                    Check::new("accessibility_tree", true)
                        .detail(format!("{} elements", screen.elements.len())),
                );
            }
            Ok(_) => checks.push(Check::new("accessibility_tree", true).status("fail").action(
                "폰 화면이 켜져 있는지 확인하고 다시 시도하세요.",
                "Make sure the phone's screen is on and try again.",
            )),
            Err(f) => checks.push(
                Check::new("accessibility_tree", true)
                    .status("fail")
                    .detail(f.message)
                    .action(
                        "폰 화면이 켜져 있는지 확인하고 다시 시도하세요.",
                        "Make sure the phone's screen is on and try again.",
                    ),
            ),
        }
    } else if !tree_reported {
        checks.push(skip("accessibility_tree", true, "Settings is not in front"));
    }

    // 8. Touch input really lands: scroll Settings down one step and back.
    if args.no_touch_test {
        checks.push(skip("touch_input", true, "--no-touch-test"));
    } else if tree_ok {
        // Down and back; if down does nothing (the list may be at its end, or
        // the first drag of a new session was lost), up and back.
        let mut verdict: Option<Check> = None;
        let mut last_detail = String::new();
        for (there, back) in [("down", "up"), ("up", "down")] {
            let moved = s
                .cmd_scroll(ScrollArgs { direction: there.into(), until: None, max: None })
                .await;
            match &moved {
                Ok(r) if r.get("screen_changed").and_then(|v| v.as_bool()) == Some(true) => {
                    // Put the list back where it was.
                    let _ = s
                        .cmd_scroll(ScrollArgs { direction: back.into(), until: None, max: None })
                        .await;
                    let mut check = Check::new("touch_input", true);
                    if there == "up" {
                        check = check.detail("the first drag did not move the screen; the second did");
                    }
                    verdict = Some(check);
                    break;
                }
                Ok(_) => last_detail = "the drags were sent but the screen did not move".to_string(),
                Err(f) => last_detail = f.message.clone(),
            }
        }
        checks.push(verdict.unwrap_or_else(|| {
            Check::new("touch_input", true).status("fail").detail(last_detail).action(
                "설정 → 개발자 → 'UI 자동화 활성화'를 켜고 다시 시도하세요. 그래도 안 되면 폰을 재시동하세요.",
                "Turn on Settings → Developer → Enable UI Automation and try again. If it still fails, restart the phone.",
            )
        }));
    } else {
        checks.push(skip("touch_input", true, "no accessibility tree"));
    }

    // 9. Auto-lock (recommended). Not read from the phone: just advice.
    checks.push(Check::new("auto_lock", false).status("info").action(
        "오래 쓸 때는 설정 → 디스플레이 및 밝기 → 자동 잠금 → '안 함'을 권합니다. 폰이 잠기면 읽기와 입력이 멈춥니다.",
        "For long sessions set Settings → Display & Brightness → Auto-Lock → Never. Reading and input stop when the phone locks.",
    ));

    // 10. Do Not Disturb that `iph dnd on` left on. The phone's Focus is not
    // touched here.
    if let Some(left) = &s.dnd.record {
        checks.push(
            Check::new("do_not_disturb", false)
                .status("info")
                .detail(format!("on since {}, set by iph", left.at))
                .action(
                    "`iph dnd on`으로 켠 방해금지 모드가 켜져 있습니다. `iph dnd off`로 원래대로 되돌릴 수 있습니다.",
                    "Do Not Disturb from `iph dnd on` is on. `iph dnd off` puts the earlier Focus back.",
                ),
        );
    }

    let ok = checks
        .iter()
        .all(|c| !c.required || c.status == "ok");
    let failing: Vec<Value> = checks
        .iter()
        .filter(|c| c.required && c.status != "ok")
        .map(|c| json!(c.check))
        .collect();
    Ok(fields! {
        "ready": ok,
        "udid": s.udid.clone(),
        "connection": connection,
        "failing": failing,
        "checks": checks,
    })
}

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

    fn says(checks: &[Check]) -> String {
        checks
            .iter()
            .map(|c| format!("{} {} {}", c.check, c.status, c.user_action.as_deref().unwrap_or("")))
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn an_unreadable_screen_on_an_unlocked_phone_is_not_called_a_lock() {
        // 2026-11-11: lockdown said unlocked, the launch could not be seen in
        // front because no reading came, and doctor said "unlock the phone".
        let (checks, front) = front_checks(
            Some(false),
            Some(Ok(false)),
            Some(Err("no accessibility tree within 14000 ms (3 empty, 0 not understood)".into())),
        );
        let text = says(&checks);
        assert!(!front);
        assert!(!text.contains("잠금을 풀") && !text.contains("Unlock"), "{text}");
        assert!(text.contains("screen_unlocked ok"), "{text}");
        assert!(text.contains("accessibility_tree fail"), "{text}");
        // Lockdown not answering is no reason to call it a lock either.
        let (checks, _) = front_checks(None, Some(Ok(false)), Some(Err("ax_timeout".into())));
        let text = says(&checks);
        assert!(!text.contains("잠금을 풀") && !text.contains("Unlock"), "{text}");
        assert!(text.contains("accessibility_tree fail"), "{text}");
    }

    #[test]
    fn only_lockdown_makes_it_a_lock() {
        let (checks, front) = front_checks(Some(true), None, None);
        assert!(!front);
        assert_eq!(checks.len(), 1);
        assert_eq!(checks[0].check, "screen_unlocked");
        assert_eq!(checks[0].status, "fail");
        assert!(checks[0].user_action.is_some());
    }

    #[test]
    fn another_screen_in_front_is_said_as_such() {
        let (checks, front) = front_checks(Some(false), Some(Ok(false)), Some(Ok(false)));
        let text = says(&checks);
        assert!(!front);
        assert!(text.contains("settings_in_front fail"), "{text}");
        assert!(!text.contains("잠금을 풀") && !text.contains("Unlock"), "{text}");
    }

    #[test]
    fn settings_in_front_passes() {
        assert!(front_checks(Some(false), Some(Ok(true)), None).1);
        // Seen only on the reading after the launch.
        assert!(front_checks(None, Some(Ok(false)), Some(Ok(true))).1);
    }
}