zc2 0.0.52

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
//! The machine check shared by `zc doctor`, `zc setup` and the desktop app's
//! first-run "Check this machine" screen (zc#509, plan snapshot S2).
//!
//! Every check is read-only and bounded in time: nothing here installs,
//! starts or signs in. `zc doctor --json` prints [`Report`], which the app
//! reads through its bundled zc.

use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
    Ok,
    /// Works, but something is worth knowing (shown, never blocks setup).
    Warn,
    /// Sharing cannot work until this is fixed.
    Fail,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Check {
    /// Stable id: `profile`, `signed_in`, `hub`, `cpu`, `memory`, `disk`,
    /// `runtime`, `node_key`, `action_key`.
    pub id: String,
    pub label: String,
    pub status: Status,
    pub detail: String,
    /// What `zc doctor --fix <id>` can do about it (`reset_hub`), when the
    /// app can fix this check for the owner with one click.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fix: Option<String>,
    /// The technical reason behind `detail` (probe errors, commands,
    /// variable names): `zc doctor` prints it, the app shows it only under
    /// Details. `detail` stays one plain sentence.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub diagnostics: Option<String>,
}

/// `zc doctor --fix reset_hub`: forget a hub address that is not the
/// environment's own hub (see [`custom_hub`]).
pub const FIX_RESET_HUB: &str = "reset_hub";

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Report {
    /// `lab` | `staging` | `prod`.
    pub env: String,
    pub api_url: String,
    pub os: String,
    pub arch: String,
    pub checks: Vec<Check>,
}

impl Report {
    /// No check failed.
    pub fn ok(&self) -> bool {
        self.checks.iter().all(|c| c.status != Status::Fail)
    }
}

fn check(id: &str, label: impl Into<String>, status: Status, detail: impl Into<String>) -> Check {
    Check {
        id: id.into(),
        label: label.into(),
        status,
        detail: detail.into(),
        fix: None,
        diagnostics: None,
    }
}

impl Check {
    fn with_fix(mut self, fix: &str) -> Self {
        self.fix = Some(fix.to_string());
        self
    }

    fn with_diagnostics(mut self, d: impl Into<String>) -> Self {
        let d = d.into();
        self.diagnostics = (!d.is_empty()).then_some(d);
        self
    }
}

/// Total memory in bytes.
pub fn total_memory() -> Option<u64> {
    #[cfg(target_os = "linux")]
    {
        let meminfo = std::fs::read_to_string("/proc/meminfo").ok()?;
        let read = |p: &str| std::fs::read_to_string(p).ok();
        let own = read("/proc/self/cgroup").unwrap_or_default();
        Some(effective_memory(
            meminfo_total(&meminfo)?,
            cgroup_memory_limit(&own, &read),
        ))
    }
    #[cfg(target_os = "macos")]
    {
        let out = std::process::Command::new("/usr/sbin/sysctl")
            .args(["-n", "hw.memsize"])
            .output()
            .ok()?;
        String::from_utf8_lossy(&out.stdout).trim().parse().ok()
    }
    #[cfg(not(any(target_os = "linux", target_os = "macos")))]
    {
        None
    }
}

/// `MemTotal` of `/proc/meminfo`, in bytes.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn meminfo_total(meminfo: &str) -> Option<u64> {
    let kb: u64 = meminfo
        .lines()
        .find(|l| l.starts_with("MemTotal:"))?
        .split_whitespace()
        .nth(1)?
        .parse()
        .ok()?;
    Some(kb * 1024)
}

/// The memory limit this process runs under, when one is visible: the lowest
/// cgroup v2 `memory.max` from its own cgroup (named by `/proc/self/cgroup`) up
/// to the namespace root, where an LXC's limit shows; else cgroup v1
/// `memory.limit_in_bytes`. "max" and unreadable files are no limit. Inside a
/// container `/proc/meminfo` usually shows the HOST's RAM, so a percentage
/// budget must be taken of this limit instead (zc#604). A process whose limit
/// sits on an ancestor it cannot see (a pod inside an LXC) still reads the
/// host's RAM: there, set an absolute `ZAKURO_BUDGET_MEMORY`.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn cgroup_memory_limit(
    self_cgroup: &str,
    read: &dyn Fn(&str) -> Option<String>,
) -> Option<u64> {
    let parse = |v: Option<String>| -> Option<u64> {
        let v = v?;
        let v = v.trim();
        if v == "max" {
            return None;
        }
        v.parse::<u64>().ok()
    };
    if let Some(path) = self_cgroup.lines().find_map(|l| l.strip_prefix("0::")) {
        let mut dir = path.trim().trim_end_matches('/').to_string();
        let mut lowest: Option<u64> = None;
        loop {
            if let Some(v) = parse(read(&format!("/sys/fs/cgroup{dir}/memory.max"))) {
                lowest = Some(lowest.map_or(v, |l| l.min(v)));
            }
            if dir.is_empty() {
                break;
            }
            dir = match dir.rfind('/') {
                Some(i) => dir[..i].to_string(),
                None => String::new(),
            };
        }
        if lowest.is_some() {
            return lowest;
        }
    }
    parse(read("/sys/fs/cgroup/memory/memory.limit_in_bytes"))
}

/// The memory this machine can use: its RAM, or a lower container limit.
/// cgroup v1 reports "no limit" as a huge number, which the `min` ignores.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn effective_memory(meminfo_total: u64, cgroup_limit: Option<u64>) -> u64 {
    match cgroup_limit {
        Some(l) if l > 0 => meminfo_total.min(l),
        _ => meminfo_total,
    }
}

/// Free bytes on the file system holding `path`.
pub fn free_disk(path: &Path) -> Option<u64> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
        let mut st: libc::statvfs = unsafe { std::mem::zeroed() };
        // SAFETY: `c` is a valid NUL-terminated path and `st` a writable statvfs.
        if unsafe { libc::statvfs(c.as_ptr(), &mut st) } != 0 {
            return None;
        }
        #[allow(clippy::unnecessary_cast)]
        Some(st.f_bavail as u64 * st.f_frsize as u64)
    }
    #[cfg(not(unix))]
    {
        let _ = path;
        None
    }
}

const GB: f64 = 1024.0 * 1024.0 * 1024.0;

/// What a machine needs to lend anything useful: below these, a warning.
const MIN_FREE_DISK_GB: f64 = 20.0;
const MIN_MEMORY_GB: f64 = 4.0;

pub fn cpu_check(cpus: usize) -> Check {
    let lend = (cpus / 2).max(1);
    check(
        "cpu",
        format!("{cpus} CPUs"),
        if cpus >= 2 { Status::Ok } else { Status::Warn },
        format!("lends up to {lend} by default (half, at least one)"),
    )
}

pub fn memory_check(bytes: Option<u64>) -> Check {
    match bytes {
        Some(b) => {
            let gb = b as f64 / GB;
            check(
                "memory",
                format!("{gb:.0} GB memory"),
                if gb >= MIN_MEMORY_GB {
                    Status::Ok
                } else {
                    Status::Warn
                },
                if gb >= MIN_MEMORY_GB {
                    "enough for public jobs".to_string()
                } else {
                    format!("under {MIN_MEMORY_GB:.0} GB: few jobs will fit")
                },
            )
        }
        None => check("memory", "Memory", Status::Warn, "could not read the total"),
    }
}

pub fn disk_check(bytes: Option<u64>) -> Check {
    match bytes {
        Some(b) => {
            let gb = b as f64 / GB;
            check(
                "disk",
                format!("{gb:.0} GB free disk"),
                if gb >= MIN_FREE_DISK_GB {
                    Status::Ok
                } else {
                    Status::Warn
                },
                if gb >= MIN_FREE_DISK_GB {
                    "room for job images".to_string()
                } else {
                    format!("under {MIN_FREE_DISK_GB:.0} GB: job images may not fit")
                },
            )
        }
        None => check(
            "disk",
            "Free disk",
            Status::Warn,
            "could not read free space",
        ),
    }
}

/// A hub address that is not its environment's own hub, and where it was set.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomHub {
    /// `~/.zakuro/credentials`, `~/.zakuro/env` or `ZAKURO_API_URL`.
    pub origin: &'static str,
    /// Set in a file `zc doctor --fix reset_hub` can rewrite (not the
    /// process environment).
    pub fixable: bool,
}

impl CustomHub {
    /// Where it was set, in words: a file path, or "the environment Zakuro
    /// was started with" for the variable (its name is a diagnostic).
    pub fn place(&self) -> &'static str {
        match self.origin {
            "ZAKURO_API_URL" => "the environment Zakuro was started with",
            file => file,
        }
    }
}

fn same_url(a: &str, b: &str) -> bool {
    a.trim().trim_end_matches('/') == b.trim().trim_end_matches('/')
}

/// `api_url` when it is not `env`'s own hub: a leftover `api_url=` from a
/// development or lab session silently overrode the profile, so the check
/// said "prod" while calling localhost. The staging and prod hubs name their
/// own environment ([`crate::deeplink::env_for_api_url`]), so a
/// `zc login --staging` is never reported.
pub fn custom_hub(
    api_url: &str,
    env: crate::profile::Env,
    credentials: Option<&str>,
    env_file: Option<&str>,
) -> Option<CustomHub> {
    if same_url(api_url, env.profile().api_url) {
        return None;
    }
    let stored = |text: Option<&str>, key: &str| {
        text.and_then(|t| crate::credentials::parse(t).remove(key))
            .map(|u| crate::credentials::migrate_legacy_api_url(&u))
            .is_some_and(|u| same_url(&u, api_url))
    };
    Some(if stored(credentials, "api_url") {
        CustomHub {
            origin: "~/.zakuro/credentials",
            fixable: true,
        }
    } else if stored(env_file, "ZAKURO_API_URL") {
        CustomHub {
            origin: "~/.zakuro/env",
            fixable: true,
        }
    } else {
        CustomHub {
            origin: "ZAKURO_API_URL",
            fixable: false,
        }
    })
}

fn is_loopback(host: &str) -> bool {
    let h = host.rsplit_once(':').map_or(host, |(h, port)| {
        if port.chars().all(|c| c.is_ascii_digit()) {
            h
        } else {
            host
        }
    });
    let h = h.trim_start_matches('[').trim_end_matches(']');
    h.eq_ignore_ascii_case("localhost")
        || h == "::1"
        || h == "0.0.0.0"
        || h.parse::<std::net::Ipv4Addr>()
            .is_ok_and(|a| a.is_loopback())
}

/// Attempts before the hub check fails, and the pause before each retry: a
/// hub restart or a network that is still coming up should not fail setup.
const HUB_ATTEMPTS: usize = 3;
const HUB_BACKOFF: [Duration; 2] = [Duration::from_millis(500), Duration::from_millis(1500)];

/// `GET <hub>/api/health`, timed, retried on transport errors.
fn hub_check(api_url: &str, env: crate::profile::Env, custom: Option<&CustomHub>) -> Check {
    let url = format!("{}/api/health", api_url.trim_end_matches('/'));
    let agent = ureq::Agent::new_with_config(
        ureq::Agent::config_builder()
            .http_status_as_error(false)
            .timeout_global(Some(Duration::from_secs(5)))
            .build(),
    );
    hub_check_with(
        api_url,
        env,
        custom,
        || {
            let t = Instant::now();
            agent
                .get(&url)
                .call()
                .map(|r| (r.status().as_u16(), t.elapsed()))
                .map_err(|e| e.to_string())
        },
        std::thread::sleep,
    )
}

/// [`hub_check`] with the request and the sleep injected. `get` returns the
/// HTTP status and the round trip, or a transport error.
fn hub_check_with(
    api_url: &str,
    env: crate::profile::Env,
    custom: Option<&CustomHub>,
    mut get: impl FnMut() -> Result<(u16, Duration), String>,
    mut sleep: impl FnMut(Duration),
) -> Check {
    let host = host_of(api_url);
    let mut last = String::new();
    for attempt in 0..HUB_ATTEMPTS {
        if attempt > 0 {
            sleep(HUB_BACKOFF[attempt - 1]);
        }
        match get() {
            Ok((s, t)) if (200..300).contains(&s) => {
                return check(
                    "hub",
                    "Hub reachable",
                    Status::Ok,
                    format!("{host} · {} ms", t.as_millis()),
                )
            }
            Ok((s, _)) => {
                return check(
                    "hub",
                    "Hub reachable",
                    Status::Fail,
                    format!("{host} is having trouble right now; try again in a minute"),
                )
                .with_diagnostics(format!("GET {host}/api/health: HTTP {s}"))
            }
            Err(e) => last = e,
        }
    }
    match custom {
        Some(c) if is_loopback(host) => {
            let detail = format!(
                "nothing is answering at {host}, a development hub set in {}; \
                 this machine's {} hub is {}",
                c.place(),
                env.name(),
                host_of(env.profile().api_url)
            );
            let fail = check("hub", "Hub not reachable", Status::Fail, detail)
                .with_diagnostics(format!("GET {host}/api/health: {last}"));
            if c.fixable {
                fail.with_fix(FIX_RESET_HUB)
            } else {
                fail
            }
        }
        _ => check(
            "hub",
            "Hub not reachable",
            Status::Fail,
            format!("can't reach {host}; check this machine's internet connection"),
        )
        .with_diagnostics(format!("GET {host}/api/health: {last}")),
    }
}

/// `text` without the `keys` lines (`export` prefixes included), other lines
/// and comments kept as they were.
fn without_keys(text: &str, keys: &[&str]) -> String {
    text.lines()
        .filter(|l| {
            let l = l.trim();
            let l = l.strip_prefix("export ").unwrap_or(l);
            !l.split_once('=')
                .is_some_and(|(k, _)| keys.contains(&k.trim()))
        })
        .map(|l| format!("{l}\n"))
        .collect()
}

/// `zc doctor --fix reset_hub`: drop the custom hub from
/// `~/.zakuro/credentials` and `~/.zakuro/env`, so the profile's own hub is
/// used again. The API key goes too: it was issued by that other hub and
/// means nothing to this one, so the owner signs in again.
pub fn reset_hub(state_dir: &Path) -> std::io::Result<()> {
    for (name, keys) in [
        ("credentials", &["api_url", "api_key"][..]),
        ("env", &["ZAKURO_API_URL", "ZAKURO_API_KEY"][..]),
    ] {
        let p = state_dir.join(name);
        let text = match std::fs::read_to_string(&p) {
            Ok(t) => t,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => return Err(e),
        };
        std::fs::write(&p, without_keys(&text, keys))?;
    }
    Ok(())
}

/// Run one `fix` a check offered. Errors are worded for the owner.
pub fn run_fix(id: &str) -> Result<(), String> {
    match id {
        FIX_RESET_HUB => {
            let dir = crate::credentials::dir().ok_or("no home directory")?;
            reset_hub(&dir).map_err(|e| format!("could not reset the hub address: {e}"))
        }
        other => Err(format!("unknown fix {other:?}")),
    }
}

fn host_of(url: &str) -> &str {
    let s = url.split("://").nth(1).unwrap_or(url);
    s.split('/').next().unwrap_or(s)
}

/// The `runtime` check: the isolating backend zc runs jobs in, from the same
/// preference-ordered probe as `zc doctor`'s sandbox line. A backend chosen
/// after a stronger one was skipped says why (e.g. "lima: limactl 1.0.6 is too
/// old"), so the owner can get the better isolation.
pub fn sandbox_check(sel: &crate::sandbox::Selection, macos: bool) -> Check {
    use crate::sandbox::RuntimeKind;
    let name = |k: RuntimeKind| match k {
        RuntimeKind::Lima => "Lima VM",
        RuntimeKind::Colima => "Colima VM",
        RuntimeKind::Lxc => "LXC container",
        RuntimeKind::Docker => "Docker",
        RuntimeKind::Host => "host",
    };
    let skipped: Vec<String> = sel
        .tried
        .iter()
        .take_while(|p| Some(p.kind) != sel.chosen)
        .filter(|p| !p.available)
        .map(|p| format!("{}: {}", p.kind.as_str(), p.detail))
        .collect();
    match sel.chosen {
        Some(k) if k != RuntimeKind::Host => check(
            "runtime",
            format!("Sandbox: {}", name(k)),
            Status::Ok,
            "jobs run isolated",
        )
        .with_diagnostics(if skipped.is_empty() {
            String::new()
        } else {
            format!("stronger isolation skipped ({})", skipped.join("; "))
        }),
        _ => {
            // One plain sentence by default; the install commands and every
            // probe's reason are diagnostics (zc#630), per OS: a Mac never
            // reads apt or systemctl, Linux never reads brew.
            let too_old = sel
                .tried
                .iter()
                .any(|p| p.detail.contains("is too old for Lima's vz"));
            let (detail, hint) = if macos && too_old {
                (
                    "this version of macOS can't run Zakuro's sandbox (macOS 13.5 or newer is needed), so jobs that need isolation can't run on this Mac",
                    "update macOS",
                )
            } else if macos {
                (
                    "Zakuro's sandbox is not set up yet, so jobs that need isolation can't run on this Mac",
                    "install Lima 2 (`brew install lima`) or Colima (`brew install colima`)",
                )
            } else {
                (
                    "Incus is not set up, so jobs that need isolation can't run on this machine",
                    "install Incus (`apt install incus && incus admin init --minimal`) or Docker",
                )
            };
            let mut diag = format!("host workers only: {hint}");
            if !skipped.is_empty() {
                diag.push_str(&format!(" ({})", skipped.join("; ")));
            }
            check("runtime", "No isolating sandbox", Status::Warn, detail).with_diagnostics(diag)
        }
    }
}

/// Signed in = an API key in `~/.zakuro/credentials` (or `ZAKURO_API_KEY`).
pub fn signed_in_check(file_key: bool, env_key: bool) -> Check {
    if file_key || env_key {
        check(
            "signed_in",
            "Signed in",
            Status::Ok,
            if file_key {
                "key in ~/.zakuro/credentials"
            } else {
                "key from the environment Zakuro was started with"
            },
        )
        .with_diagnostics(if file_key { "" } else { "ZAKURO_API_KEY" })
    } else {
        check(
            "signed_in",
            "Not signed in",
            Status::Fail,
            "sign in to continue",
        )
        .with_diagnostics("zc login")
    }
}

/// The `profile` check: the environment and the hub it talks to. A hub that
/// is not the environment's own warns, and says where it was set.
pub fn profile_check(
    env: crate::profile::Env,
    api_url: &str,
    unknown_env: Option<&str>,
    custom: Option<&CustomHub>,
) -> Check {
    if let Some(v) = unknown_env {
        return check(
            "profile",
            format!("Profile {}", env.name()),
            Status::Warn,
            "unknown environment setting; using prod",
        )
        .with_diagnostics(format!("ZAKURO_ENV={v:?} is not lab|staging|prod"));
    }
    match custom {
        Some(c) => {
            let warn = check(
                "profile",
                format!("Custom hub, not the {} hub", env.name()),
                Status::Warn,
                format!(
                    "{} set in {}; the {} hub is {}",
                    host_of(api_url),
                    c.place(),
                    env.name(),
                    host_of(env.profile().api_url)
                ),
            )
            .with_diagnostics(format!("{api_url} from {}", c.origin));
            if c.fixable {
                warn.with_fix(FIX_RESET_HUB)
            } else {
                warn
            }
        }
        None => check(
            "profile",
            format!("Profile {}", env.name()),
            Status::Ok,
            host_of(api_url).to_string(),
        ),
    }
}

/// The `kid`s of the keys the hub signs links with
/// (`GET <hub>/api/broker/action/pubkey`), computed here from the public keys
/// rather than taken from the hub's own `kid` field. `None` when the hub did
/// not answer usably.
fn hub_action_kids(api_url: &str) -> Option<Vec<String>> {
    let url = format!("{}/api/broker/action/pubkey", api_url.trim_end_matches('/'));
    let agent = ureq::Agent::new_with_config(
        ureq::Agent::config_builder()
            .timeout_global(Some(Duration::from_secs(5)))
            .build(),
    );
    let body = agent
        .get(&url)
        .call()
        .ok()?
        .body_mut()
        .read_to_string()
        .ok()?;
    parse_action_kids(&body)
}

fn parse_action_kids(body: &str) -> Option<Vec<String>> {
    let v: serde_json::Value = serde_json::from_str(body).ok()?;
    v.get("keys")?
        .as_array()?
        .iter()
        .map(|k| {
            let b64 = k.get("pubkey").or_else(|| k.get("x"))?.as_str()?;
            crate::deeplink::TrustedKey::from_b64(b64)
                .ok()
                .map(|t| t.kid)
        })
        .collect()
}

/// The `action_key` check: can this machine open the hub's signed
/// `zakuro://` links? Only a real gap warns: a hub that signs with a key this
/// zc does not trust. A hub that signs no links (prod until it provisions
/// its key, ADR ZC-0020) has nothing to open, so that is not a warning.
pub fn action_key_check(
    env: crate::profile::Env,
    local: Result<Vec<crate::deeplink::TrustedKey>, String>,
    hub_kids: Option<&[String]>,
) -> Check {
    let local = match local {
        Ok(k) => k,
        Err(e) => {
            return check(
                "action_key",
                "Open in Zakuro links",
                Status::Fail,
                "the links key setting is not valid",
            )
            .with_diagnostics(e)
        }
    };
    let hub_signs = hub_kids.filter(|k| !k.is_empty());
    let trusted = |kid: &String| local.iter().any(|t| &t.kid == kid);
    match hub_signs {
        Some(kids) if !kids.iter().any(trusted) => check(
            "action_key",
            "Open in Zakuro links off",
            Status::Warn,
            format!(
                "the {} hub signs links with key {}, which this version of Zakuro \
                 does not know yet; update Zakuro",
                env.name(),
                kids[0]
            ),
        ),
        Some(kids) => check(
            "action_key",
            "Open in Zakuro links",
            Status::Ok,
            format!("hub key {}", kids.iter().find(|k| trusted(k)).unwrap()),
        ),
        None if hub_kids.is_some() => check(
            "action_key",
            "Open in Zakuro links",
            Status::Ok,
            format!("the {} hub does not sign links yet", env.name()),
        ),
        // The hub did not say (offline, or no network check).
        None => match local.first() {
            Some(k) => check(
                "action_key",
                "Open in Zakuro links",
                Status::Ok,
                format!("hub key {}", k.kid),
            ),
            None => check(
                "action_key",
                "Open in Zakuro links",
                Status::Ok,
                format!("no {} hub key in this version", env.name()),
            ),
        },
    }
}

/// Run every check. `with_network` adds the hub round trip.
pub fn machine_report(with_network: bool) -> Report {
    let resolved = crate::profile::resolve();
    let api_url = resolved.api_url.0.clone();
    // The hub this machine talks to names the environment: a LaunchAgent or
    // an app started from Finder has no ZAKURO_ENV, so the profile alone says
    // prod while `zc login --staging` pointed the credentials at staging.
    let env = crate::deeplink::env_for_api_url(Some(&api_url));
    let state_dir = crate::credentials::dir();
    let file_key = crate::credentials::path()
        .and_then(|p| std::fs::read_to_string(p).ok())
        .map(|t| crate::credentials::parse(&t))
        .and_then(|m| m.get("api_key").cloned())
        .is_some_and(|k| !k.trim().is_empty());
    let env_key = std::env::var("ZAKURO_API_KEY").is_ok_and(|k| !k.trim().is_empty());
    let read = |name: &str| {
        state_dir
            .as_ref()
            .and_then(|d| std::fs::read_to_string(d.join(name)).ok())
    };
    let custom = custom_hub(
        &api_url,
        env,
        read("credentials").as_deref(),
        read("env").as_deref(),
    );

    let mut checks = vec![profile_check(
        env,
        &api_url,
        resolved.unknown_env.as_deref(),
        custom.as_ref(),
    )];
    checks.push(signed_in_check(file_key, env_key));
    if with_network {
        checks.push(hub_check(&api_url, env, custom.as_ref()));
    }
    let cpus = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);
    checks.push(cpu_check(cpus));
    checks.push(memory_check(total_memory()));
    let home = state_dir
        .clone()
        .or_else(|| std::env::var_os("HOME").map(Into::into))
        .unwrap_or_else(|| "/".into());
    let probe_dir = if home.exists() { home } else { "/".into() };
    checks.push(disk_check(free_disk(&probe_dir)));

    // The engine jobs actually run in (Lima, then Colima, then Docker on macOS;
    // LXC, then Docker on Linux), not a bare Docker probe: with Colima or Lima
    // up and no Docker socket, this used to warn "No container runtime".
    let active = crate::sandbox::engine::active();
    checks.push(sandbox_check(&active.selection, cfg!(target_os = "macos")));

    let node_key = crate::broker::node_identity::NodeKey::load_in(state_dir);
    checks.push(match node_key {
        Some(k) => check(
            "node_key",
            "Node identity",
            Status::Ok,
            format!("zc://node-{}", k.fingerprint()),
        ),
        // A fresh install has none yet, and needs nothing from the owner for
        // one (zc#637): it is made when sharing starts.
        None => check(
            "node_key",
            "Node identity",
            Status::Ok,
            "created when sharing starts",
        ),
    });

    let hub_kids = if with_network {
        hub_action_kids(&api_url)
    } else {
        None
    };
    checks.push(action_key_check(
        env,
        crate::deeplink::keys_for(env),
        hub_kids.as_deref(),
    ));

    Report {
        env: env.name().to_string(),
        api_url,
        os: std::env::consts::OS.to_string(),
        arch: std::env::consts::ARCH.to_string(),
        checks,
    }
}

/// One line per check, as `zc doctor` and `zc setup` print them.
pub fn format_check(c: &Check) -> String {
    let mark = match c.status {
        Status::Ok => "✓",
        Status::Warn => "!",
        Status::Fail => "✗",
    };
    match &c.diagnostics {
        Some(d) => format!("  {mark} {:<30} {} ({d})", c.label, c.detail),
        None => format!("  {mark} {:<30} {}", c.label, c.detail),
    }
}

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

    #[test]
    fn thresholds_warn_but_never_fail_on_hardware() {
        assert_eq!(cpu_check(12).status, Status::Ok);
        assert!(cpu_check(12).detail.contains("up to 6"));
        assert_eq!(cpu_check(1).status, Status::Warn);
        assert_eq!(
            memory_check(Some(36 * 1024 * 1024 * 1024)).label,
            "36 GB memory"
        );
        assert_eq!(
            memory_check(Some(2 * 1024 * 1024 * 1024)).status,
            Status::Warn
        );
        assert_eq!(
            disk_check(Some(10 * 1024 * 1024 * 1024)).status,
            Status::Warn
        );
        assert_eq!(disk_check(None).status, Status::Warn);
        assert_eq!(signed_in_check(false, false).status, Status::Fail);
        assert_eq!(
            signed_in_check(false, true).diagnostics.as_deref(),
            Some("ZAKURO_API_KEY")
        );
    }

    /// zc#628: profile prod with `api_url=http://localhost:8000` left in the
    /// credentials said "Profile prod" and "localhost:8000: Connection refused".
    #[test]
    fn a_leftover_dev_hub_is_named_and_offered_a_reset() {
        use crate::profile::Env;
        let creds = "api_key=zk_dev\napi_url=http://localhost:8000\n";
        let c = custom_hub("http://localhost:8000", Env::Prod, Some(creds), None).unwrap();
        assert_eq!(c.origin, "~/.zakuro/credentials");
        assert!(c.fixable);

        let p = profile_check(Env::Prod, "http://localhost:8000", None, Some(&c));
        assert_eq!(p.status, Status::Warn);
        assert_eq!(p.label, "Custom hub, not the prod hub");
        assert!(p
            .detail
            .contains("localhost:8000 set in ~/.zakuro/credentials"));
        assert!(p.detail.contains("hub.zakuro-ai.com"));
        assert_eq!(p.fix.as_deref(), Some(FIX_RESET_HUB));

        let mut slept = vec![];
        let h = hub_check_with(
            "http://localhost:8000",
            Env::Prod,
            Some(&c),
            || Err("io: Connection refused".into()),
            |d| slept.push(d),
        );
        assert_eq!(h.status, Status::Fail);
        assert!(h
            .detail
            .starts_with("nothing is answering at localhost:8000"));
        assert_eq!(h.fix.as_deref(), Some(FIX_RESET_HUB));
        assert_eq!(slept, HUB_BACKOFF.to_vec(), "3 attempts, backed off");
    }

    #[test]
    fn the_environments_own_hubs_are_not_custom() {
        use crate::profile::Env;
        let prod = crate::credentials::PROD_API_URL;
        let stg = crate::credentials::STAGING_API_URL;
        assert_eq!(custom_hub(prod, Env::Prod, None, None), None);
        assert_eq!(custom_hub(&format!("{prod}/"), Env::Prod, None, None), None);
        // `zc login --staging` stores the staging hub: env_for_api_url names
        // it staging, whose own hub it is.
        let creds = format!("api_url={stg}\n");
        assert_eq!(custom_hub(stg, Env::Staging, Some(&creds), None), None);
        let p = profile_check(Env::Prod, prod, None, None);
        assert_eq!((p.status, p.fix), (Status::Ok, None));
        // Set only in the process environment: named, but not fixable here.
        let c = custom_hub("http://127.0.0.1:9", Env::Prod, None, None).unwrap();
        assert_eq!((c.origin, c.fixable), ("ZAKURO_API_URL", false));
        let p = profile_check(Env::Prod, "http://127.0.0.1:9", None, Some(&c));
        assert_eq!(p.fix, None);
        // Legacy my.zakuro-ai.com in the env file migrates to prod: not custom.
        let env_file = "ZAKURO_API_URL=https://my.zakuro-ai.com\n";
        let c = custom_hub("http://10.0.0.5:8000", Env::Prod, None, Some(env_file));
        assert_eq!(c.unwrap().origin, "ZAKURO_API_URL");
    }

    #[test]
    fn the_hub_check_rides_out_a_blip_and_names_http_errors() {
        use crate::profile::Env;
        let mut calls = 0;
        let h = hub_check_with(
            "https://hub.zakuro-ai.com",
            Env::Prod,
            None,
            || {
                calls += 1;
                if calls < 3 {
                    Err("timeout".into())
                } else {
                    Ok((200, Duration::from_millis(42)))
                }
            },
            |_| {},
        );
        assert_eq!(h.status, Status::Ok);
        assert_eq!(h.detail, "hub.zakuro-ai.com · 42 ms");
        // An HTTP error is an answer: no retry.
        let mut calls = 0;
        let h = hub_check_with(
            "https://hub.zakuro-ai.com",
            Env::Prod,
            None,
            || {
                calls += 1;
                Ok((502, Duration::ZERO))
            },
            |_| {},
        );
        assert_eq!((h.status, calls), (Status::Fail, 1));
        assert_eq!(
            h.detail,
            "hub.zakuro-ai.com is having trouble right now; try again in a minute"
        );
        assert_eq!(
            h.diagnostics.as_deref(),
            Some("GET hub.zakuro-ai.com/api/health: HTTP 502")
        );
        // A remote hub that never answers keeps the transport error, no fix.
        let h = hub_check_with(
            "https://hub.zakuro-ai.com",
            Env::Prod,
            None,
            || Err("dns failed".into()),
            |_| {},
        );
        assert_eq!(
            h.detail,
            "can't reach hub.zakuro-ai.com; check this machine's internet connection"
        );
        assert_eq!(
            h.diagnostics.as_deref(),
            Some("GET hub.zakuro-ai.com/api/health: dns failed")
        );
        assert_eq!(h.fix, None);
    }

    #[test]
    fn loopback_hosts() {
        for h in [
            "localhost:8000",
            "127.0.0.1",
            "127.0.0.1:8000",
            "[::1]:8000",
            "0.0.0.0:80",
        ] {
            assert!(is_loopback(h), "{h}");
        }
        for h in [
            "hub.zakuro-ai.com",
            "10.13.13.1:9000",
            "localhost.example.com",
        ] {
            assert!(!is_loopback(h), "{h}");
        }
    }

    #[test]
    fn reset_hub_drops_the_custom_hub_and_its_key_only() {
        let dir = std::env::temp_dir().join(format!("zc-reset-hub-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(
            dir.join("credentials"),
            "# zc\napi_key=zk_dev\napi_url=http://localhost:8000\nenv=prod\n",
        )
        .unwrap();
        std::fs::write(
            dir.join("env"),
            "export ZAKURO_API_URL=http://localhost:8000\nZAKURO_LOG=debug\n",
        )
        .unwrap();
        reset_hub(&dir).unwrap();
        assert_eq!(
            std::fs::read_to_string(dir.join("credentials")).unwrap(),
            "# zc\nenv=prod\n"
        );
        assert_eq!(
            std::fs::read_to_string(dir.join("env")).unwrap(),
            "ZAKURO_LOG=debug\n"
        );
        // Nothing to reset is fine.
        reset_hub(&dir.join("missing")).unwrap();
        let _ = std::fs::remove_dir_all(&dir);
        assert_eq!(run_fix("nope"), Err("unknown fix \"nope\"".into()));
    }

    /// zc#629: the bodies prod and staging answered on 2026-11-16.
    const PROD_PUBKEY_BODY: &str = r#"{"alg":"EdDSA","kid":null,"pubkey":null,"keys":[]}"#;
    const STG_PUBKEY_BODY: &str = r#"{"alg":"EdDSA","kid":"bde74a18996820b2","pubkey":"dOhGMGFe0iJu4g6KvZKsG8MwK/W0i8WX4l3tXbsmL04=","keys":[{"kty":"OKP","crv":"Ed25519","alg":"EdDSA","use":"sig","kid":"bde74a18996820b2","x":"dOhGMGFe0iJu4g6KvZKsG8MwK_W0i8WX4l3tXbsmL04","pubkey":"dOhGMGFe0iJu4g6KvZKsG8MwK/W0i8WX4l3tXbsmL04="}]}"#;

    #[test]
    fn a_hub_that_signs_no_links_is_not_a_warning() {
        use crate::profile::Env;
        let kids = parse_action_kids(PROD_PUBKEY_BODY).unwrap();
        assert!(kids.is_empty());
        let c = action_key_check(Env::Prod, Ok(vec![]), Some(&kids));
        assert_eq!(c.status, Status::Ok);
        assert_eq!(c.detail, "the prod hub does not sign links yet");
        assert!(!c.detail.contains("ZAKURO_"), "{}", c.detail);
    }

    #[test]
    fn the_staging_hub_key_is_trusted_by_kid() {
        use crate::profile::Env;
        let kids = parse_action_kids(STG_PUBKEY_BODY).unwrap();
        assert_eq!(kids, vec!["bde74a18996820b2".to_string()]);
        let local = crate::deeplink::keys_for(Env::Staging);
        let c = action_key_check(Env::Staging, local, Some(&kids));
        assert_eq!(
            (c.status, c.detail.as_str()),
            (Status::Ok, "hub key bde74a18996820b2")
        );
    }

    #[test]
    fn a_hub_key_this_zc_does_not_know_asks_for_an_update() {
        use crate::profile::Env;
        // The day prod provisions its key, before a zc that pins it ships.
        let kids = parse_action_kids(STG_PUBKEY_BODY).unwrap();
        let c = action_key_check(Env::Prod, Ok(vec![]), Some(&kids));
        assert_eq!(c.status, Status::Warn);
        assert!(c.detail.contains("bde74a18996820b2"));
        assert!(c.detail.ends_with("update Zakuro"));
        // Offline: nothing known about the hub, nothing to warn about.
        let c = action_key_check(Env::Prod, Ok(vec![]), None);
        assert_eq!(c.status, Status::Ok);
        // A malformed override is still a failure.
        let c = action_key_check(Env::Prod, Err("bad key".into()), None);
        assert_eq!(c.status, Status::Fail);
        assert_eq!(parse_action_kids("not json"), None);
    }

    #[test]
    fn report_ok_means_nothing_failed() {
        let r = Report {
            env: "prod".into(),
            api_url: "https://hub.zakuro-ai.com".into(),
            os: "linux".into(),
            arch: "x86_64".into(),
            checks: vec![cpu_check(1), signed_in_check(true, false)],
        };
        assert!(r.ok());
        let v = serde_json::to_value(&r).unwrap();
        assert_eq!(v["checks"][0]["status"], "warn");
        assert_eq!(
            host_of("https://hub.zakuro-ai.com/api"),
            "hub.zakuro-ai.com"
        );
    }

    #[test]
    fn a_mac_too_old_for_the_vm_says_so() {
        use crate::sandbox::{Probe, RuntimeKind, Selection};
        let sel = Selection {
            chosen: None,
            tried: vec![Probe {
                kind: RuntimeKind::Lima,
                available: false,
                detail: crate::sandbox::provision::too_old(Some((12, 7))).unwrap(),
            }],
        };
        let c = sandbox_check(&sel, true);
        assert!(
            c.detail.starts_with("this version of macOS can't run"),
            "{}",
            c.detail
        );
        assert!(c.diagnostics.unwrap().contains("macOS 12.7 is too old"));
    }

    #[test]
    fn the_sandbox_check_reports_the_engine_jobs_use() {
        use crate::sandbox::{Probe, RuntimeKind, Selection};
        let probe = |kind, available, detail: &str| Probe {
            kind,
            available,
            detail: detail.into(),
        };
        // Colima up, Lima too old, no Docker socket: OK, not "No container runtime".
        let sel = Selection {
            chosen: Some(RuntimeKind::Colima),
            tried: vec![
                probe(RuntimeKind::Lima, false, "limactl 1.0.6 is too old"),
                probe(RuntimeKind::Colima, true, "colima 0.8.1"),
                probe(RuntimeKind::Docker, false, "no docker socket"),
            ],
        };
        let c = sandbox_check(&sel, true);
        assert_eq!(c.status, Status::Ok);
        assert_eq!(c.label, "Sandbox: Colima VM");
        assert_eq!(c.detail, "jobs run isolated");
        let diag = c.diagnostics.unwrap();
        assert!(diag.contains("lima: limactl 1.0.6 is too old"), "{diag}");
        assert!(!diag.contains("docker"), "only the stronger ones: {diag}");

        let lima = Selection {
            chosen: Some(RuntimeKind::Lima),
            tried: vec![probe(RuntimeKind::Lima, true, "ok")],
        };
        assert_eq!(sandbox_check(&lima, true).detail, "jobs run isolated");

        let none = Selection {
            chosen: None,
            tried: vec![probe(RuntimeKind::Lima, false, "limactl not found")],
        };
        let c = sandbox_check(&none, true);
        assert_eq!(c.status, Status::Warn);
        assert!(c.detail.ends_with("can't run on this Mac"), "{}", c.detail);
        let diag = c.diagnostics.unwrap();
        assert!(diag.contains("brew install lima"), "{diag}");
        assert!(
            !diag.contains("apt") && !diag.contains("systemctl"),
            "{diag}"
        );
        let linux = sandbox_check(&none, false);
        assert!(linux.detail.starts_with("Incus is not set up"));
        assert!(!linux.diagnostics.unwrap().contains("brew"));
    }

    /// zc#630: the text the app shows by default is plain: no commands, no
    /// variable names. They live in `diagnostics` (the app's Details).
    #[test]
    fn default_text_names_no_command_or_variable() {
        use crate::profile::Env;
        use crate::sandbox::{Probe, RuntimeKind, Selection};
        let none = Selection {
            chosen: None,
            tried: vec![
                Probe {
                    kind: RuntimeKind::Lima,
                    available: false,
                    detail: "limactl is not installed (brew install lima, or set ZAKURO_LIMACTL)"
                        .into(),
                },
                Probe {
                    kind: RuntimeKind::Docker,
                    available: false,
                    detail: "docker socket /var/run/docker.sock does not exist (start Docker Desktop, Colima or OrbStack, or set DOCKER_HOST)".into(),
                },
            ],
        };
        let env_only = CustomHub {
            origin: "ZAKURO_API_URL",
            fixable: false,
        };
        let checks = [
            sandbox_check(&none, true),
            sandbox_check(&none, false),
            signed_in_check(false, false),
            signed_in_check(false, true),
            profile_check(Env::Prod, "http://x:1", Some("dev"), None),
            profile_check(Env::Prod, "http://x:1", None, Some(&env_only)),
            hub_check_with(
                "http://x:1",
                Env::Prod,
                Some(&env_only),
                || Err("refused".into()),
                |_| {},
            ),
            hub_check_with(
                "https://hub.zakuro-ai.com",
                Env::Prod,
                None,
                || Err("dns".into()),
                |_| {},
            ),
            action_key_check(
                Env::Prod,
                Err("ZAKURO_ACTION_PUBKEYS: 'x' is not base64".into()),
                None,
            ),
        ];
        for c in &checks {
            for bad in [
                "ZAKURO_",
                "DOCKER_HOST",
                "brew ",
                "apt ",
                "systemctl",
                "zc login",
                "limactl",
            ] {
                assert!(
                    !c.detail.contains(bad),
                    "{}: {:?} in {:?}",
                    c.id,
                    bad,
                    c.detail
                );
            }
        }
        // The technical reason is still there for Details and `zc doctor`.
        assert!(checks[0]
            .diagnostics
            .as_deref()
            .unwrap()
            .contains("ZAKURO_LIMACTL"));
        assert!(format_check(&checks[2]).ends_with("sign in to continue (zc login)"));
    }

    #[test]
    fn docker_hints_follow_the_os() {
        use crate::broker::deploy::runtime::RuntimeError;
        let mac = crate::broker::deploy::runtime::next_step_for(RuntimeError::SocketMissing, true);
        assert!(!mac.as_deref().unwrap().contains("systemctl"));
        let linux =
            crate::broker::deploy::runtime::next_step_for(RuntimeError::SocketMissing, false);
        assert!(linux.as_deref().unwrap().contains("systemctl"));
    }

    #[test]
    fn the_real_machine_reads_without_network() {
        let r = machine_report(false);
        let ids: Vec<&str> = r.checks.iter().map(|c| c.id.as_str()).collect();
        for id in [
            "profile",
            "signed_in",
            "cpu",
            "memory",
            "disk",
            "runtime",
            "node_key",
            "action_key",
        ] {
            assert!(ids.contains(&id), "{id} in {ids:?}");
        }
        assert!(!ids.contains(&"hub"));
        #[cfg(target_os = "linux")]
        assert!(total_memory().unwrap() > 0);
        #[cfg(unix)]
        assert!(free_disk(Path::new("/")).is_some());
    }

    #[test]
    fn a_container_memory_limit_lowers_the_total() {
        let meminfo = "MemTotal:       65728352 kB\nMemFree: 1 kB\n";
        let host = meminfo_total(meminfo).unwrap();
        assert_eq!(host, 65728352 * 1024);
        // cgroup v2, own path.
        let files = |p: &str| match p {
            "/sys/fs/cgroup/kubepods/pod1/c1/memory.max" => Some("17179869184\n".to_string()),
            _ => None,
        };
        let lim = cgroup_memory_limit("0::/kubepods/pod1/c1\n", &files);
        assert_eq!(lim, Some(17179869184));
        assert_eq!(effective_memory(host, lim), 17179869184);
        // cgroup v2: own cgroup unlimited, the limit on the namespace root (an LXC).
        let lxc = |p: &str| match p {
            "/sys/fs/cgroup/user.slice/session-1.scope/memory.max" => Some("max\n".to_string()),
            "/sys/fs/cgroup/user.slice/memory.max" => Some("max\n".to_string()),
            "/sys/fs/cgroup/memory.max" => Some("8589934592\n".to_string()),
            _ => None,
        };
        assert_eq!(
            cgroup_memory_limit("0::/user.slice/session-1.scope\n", &lxc),
            Some(8589934592)
        );
        // The lowest limit on the way up wins.
        let nested = |p: &str| match p {
            "/sys/fs/cgroup/a/b/memory.max" => Some("4294967296".to_string()),
            "/sys/fs/cgroup/memory.max" => Some("8589934592".to_string()),
            _ => None,
        };
        assert_eq!(cgroup_memory_limit("0::/a/b", &nested), Some(4294967296));
        // cgroup v2 with no limit ("max"): the host's RAM.
        let unlimited = |p: &str| (p == "/sys/fs/cgroup/memory.max").then(|| "max\n".to_string());
        assert_eq!(cgroup_memory_limit("0::/\n", &unlimited), None);
        // cgroup v1: a limit, and the "unlimited" sentinel the min ignores.
        let v1 = |p: &str| {
            (p == "/sys/fs/cgroup/memory/memory.limit_in_bytes").then(|| "8589934592".to_string())
        };
        assert_eq!(
            effective_memory(host, cgroup_memory_limit("", &v1)),
            8589934592
        );
        let v1_none = |p: &str| {
            (p == "/sys/fs/cgroup/memory/memory.limit_in_bytes")
                .then(|| "9223372036854771712".to_string())
        };
        assert_eq!(
            effective_memory(host, cgroup_memory_limit("", &v1_none)),
            host
        );
        // Nothing readable: the host's RAM.
        assert_eq!(
            effective_memory(host, cgroup_memory_limit("", &|_| None)),
            host
        );
    }
}