pitboard-core 0.20.0

The engine behind Pitboard: switches Claude Code, Codex and Claude Desktop between accounts by parking and restoring their logins. Its API serves Pitboard's own front ends.
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
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
//! Pitboard's jobs on macOS: LaunchAgents, which run inside the login session so the keychain
//! is unlocked.

use super::super::unix::service::{self, Control};
use crate::context::Context;
use crate::error::{Error, Result};
use crate::host::Scheduler;
use crate::permit::Permit;
use crate::schedule::{EVERY_SECONDS, Job, Location, RESTART_AFTER_SECONDS};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// What launchd calls each job, so a person can find it without Pitboard telling them.
const LABEL: &str = "com.datlechin.pitboard.renew";
const WATCH_LABEL: &str = "com.datlechin.pitboard.watch";

const LAUNCHCTL: &str = "/bin/launchctl";
const LOG: &str = "watch.log";

#[derive(Debug)]
pub(super) struct Launchd {
    control: Arc<dyn Control>,
}

impl Launchd {
    pub(super) fn new(control: Arc<dyn Control>) -> Launchd {
        Launchd { control }
    }

    fn label(job: Job) -> &'static str {
        match job {
            Job::Renew => LABEL,
            Job::Watch => WATCH_LABEL,
        }
    }

    fn agent(ctx: &Context, job: Job) -> PathBuf {
        ctx.home()
            .join("Library/LaunchAgents")
            .join(format!("{}.plist", Self::label(job)))
    }

    fn log_folder(home: &Path) -> PathBuf {
        home.join("Library/Logs/Pitboard")
    }

    /// launchd's session for this user, which `bootstrap` and `bootout` name.
    fn domain() -> String {
        // SAFETY: `getuid` cannot fail and touches no memory of this process.
        format!("gui/{}", unsafe { libc::getuid() })
    }
}

impl Scheduler for Launchd {
    fn location(&self, ctx: &Context, job: Job) -> Location {
        Location::File(Self::agent(ctx, job))
    }

    fn installed(&self, ctx: &Context, job: Job) -> bool {
        Self::agent(ctx, job).is_file()
    }

    fn program(&self, ctx: &Context, job: Job) -> Option<PathBuf> {
        let body = std::fs::read_to_string(Self::agent(ctx, job)).ok()?;
        arguments(&body).into_iter().next().map(PathBuf::from)
    }

    fn options(&self, ctx: &Context, job: Job) -> Vec<String> {
        let Ok(body) = std::fs::read_to_string(Self::agent(ctx, job)) else {
            return Vec::new();
        };
        let arguments = arguments(&body);
        let marked = runs(job);
        match arguments.get(1..=marked.len()) {
            Some(words) if words == marked => arguments[1 + marked.len()..].to_vec(),
            _ => Vec::new(),
        }
    }

    fn environment(&self, ctx: &Context, job: Job) -> Vec<(String, String)> {
        if !self.installed(ctx, job) {
            return Vec::new();
        }
        std::fs::read_to_string(Self::agent(ctx, job))
            .map(|body| given(&body))
            .unwrap_or_default()
    }

    fn put(
        &self,
        ctx: &Context,
        permit: Permit,
        job: Job,
        program: &Path,
        options: &[String],
        environment: &[(String, String)],
    ) -> Result<()> {
        if let Some(refused) = unwritable(program, environment) {
            return Err(refused);
        }
        let path = Self::agent(ctx, job);
        let before = std::fs::read_to_string(&path).ok();
        if job == Job::Watch {
            let folder = Self::log_folder(ctx.home());
            crate::host::fs::create_private_dir(permit, &folder).map_err(|source| {
                Error::HomeUnwritable {
                    path: folder,
                    source,
                }
            })?;
        }
        service::write(
            permit,
            &path,
            &plist(job, program, options, environment, ctx.home()),
        )?;
        // `bootstrap` is launchd's own word for this, and replaces the deprecated `load`.
        let domain = Self::domain();
        let target = path.to_string_lossy();
        let _ = self
            .control
            .run(permit, LAUNCHCTL, &["bootout", &domain, &target]);
        if let Err(refused) =
            self.control
                .start(permit, LAUNCHCTL, &["bootstrap", &domain, &target])
        {
            service::restore(permit, &path, before.as_deref());
            if before.is_some() {
                let _ = self
                    .control
                    .start(permit, LAUNCHCTL, &["bootstrap", &domain, &target]);
            }
            return Err(refused);
        }
        Ok(())
    }

    fn remove(&self, ctx: &Context, permit: Permit, job: Job) -> Result<bool> {
        let path = Self::agent(ctx, job);
        if !path.is_file() {
            return Ok(false);
        }
        let _ = self.control.run(
            permit,
            LAUNCHCTL,
            &["bootout", &Self::domain(), &path.to_string_lossy()],
        );
        service::remove(permit, &path)?;
        if job == Job::Watch {
            let folder = Self::log_folder(ctx.home());
            service::remove(permit, &folder.join(LOG))?;
            // Kept where something else is in it.
            let _ = crate::host::fs::remove_dir(permit, &folder);
        }
        Ok(true)
    }

    /// launchd puts the label of the job it starts in `XPC_SERVICE_NAME`.
    #[allow(
        clippy::disallowed_methods,
        reason = "what launchd started this process as, which no front end passes on"
    )]
    fn started_this_process(&self, job: Job, said: Option<&str>) -> bool {
        said.map(str::to_owned)
            .or_else(|| std::env::var("XPC_SERVICE_NAME").ok())
            .as_deref()
            == Some(Self::label(job))
    }
}

/// What a job's agent runs Pitboard with before its options. launchd names the job it starts,
/// so renewal's runs plain `renew`; the watch service carries its marker, as on every system.
fn runs(job: Job) -> &'static [&'static str] {
    match job {
        Job::Renew => &["renew"],
        Job::Watch => &crate::schedule::WATCH_RUN,
    }
}

/// launchd's own format. Renewal's `RunAtLoad` is off: installing it is not a reason to talk to
/// Anthropic that second, and the first run comes at the first interval. The watch service
/// runs at load, which is at each login too, and launchd starts it again a minute after it
/// fails or crashes: a panic exits 101, and a run an update left behind exits on a state file
/// the new build wrote. Its lines go to a log in `home`'s `Library/Logs`, where Console finds a
/// user agent's.
///
/// The job runs with launchd's environment, not the person's shell, so a variable they set
/// would not reach it. `environment` is what it is given of the installing Pitboard's,
/// `PITBOARD_NO_ARGV` and the proxy variables, under `EnvironmentVariables`, which is left out
/// where there is nothing to give.
fn plist(
    job: Job,
    program: &Path,
    options: &[String],
    environment: &[(String, String)],
    home: &Path,
) -> String {
    let environment = if environment.is_empty() {
        String::new()
    } else {
        format!(
            "  <key>EnvironmentVariables</key>\n  <dict>\n{}  </dict>\n",
            environment
                .iter()
                .map(|(name, value)| format!(
                    "    <key>{}</key><string>{}</string>\n",
                    escape(name),
                    escape(value)
                ))
                .collect::<String>()
        )
    };
    let arguments = std::iter::once(program.to_string_lossy().into_owned())
        .chain(runs(job).iter().map(|word| (*word).to_owned()))
        .chain(options.iter().cloned())
        .map(|argument| format!("    <string>{}</string>\n", escape(&argument)))
        .collect::<String>();
    let when = match job {
        Job::Renew => format!(
            "  <key>StartInterval</key><integer>{EVERY_SECONDS}</integer>\n  \
             <key>RunAtLoad</key><false/>\n  \
             <key>LowPriorityIO</key><true/>\n  \
             <key>ProcessType</key><string>Background</string>\n"
        ),
        Job::Watch => {
            let log = escape(&Launchd::log_folder(home).join(LOG).to_string_lossy());
            format!(
                "  <key>RunAtLoad</key><true/>\n  \
                 <key>KeepAlive</key>\n  <dict>\n    \
                 <key>SuccessfulExit</key><false/>\n    <key>Crashed</key><true/>\n  </dict>\n  \
                 <key>ThrottleInterval</key><integer>{RESTART_AFTER_SECONDS}</integer>\n  \
                 <key>StandardOutPath</key><string>{log}</string>\n  \
                 <key>StandardErrorPath</key><string>{log}</string>\n"
            )
        }
    };
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>{}</string>
  <key>ProgramArguments</key>
  <array>
{arguments}  </array>
{when}{environment}</dict>
</plist>
"#,
        Launchd::label(job)
    )
}

/// The strings of the `ProgramArguments` array a plist [`plist`] wrote, the program first.
fn arguments(body: &str) -> Vec<String> {
    let Some(array) = body
        .split_once("<key>ProgramArguments</key>")
        .and_then(|(_, after)| after.split_once("</array>"))
        .map(|(array, _)| array)
    else {
        return Vec::new();
    };
    array
        .split("<string>")
        .skip(1)
        .filter_map(|after| after.split_once("</string>"))
        .map(|(argument, _)| unescape(argument))
        .collect()
}

/// The variables a plist [`plist`] wrote gives its job, in the order written. Only what
/// `plist` writes is read: each key with the string right after it, in the dictionary after
/// `EnvironmentVariables`. Neither can hold a `<` of its own, which [`escape`] writes as
/// `&lt;`, so each ends where its closing tag is.
fn given(body: &str) -> Vec<(String, String)> {
    let Some(mut rest) = body
        .split_once("<key>EnvironmentVariables</key>")
        .and_then(|(_, after)| after.split_once("<dict>"))
        .and_then(|(_, after)| after.split_once("</dict>"))
        .map(|(dict, _)| dict)
    else {
        return Vec::new();
    };
    let mut pairs = Vec::new();
    while let Some((_, after)) = rest.split_once("<key>") {
        let Some((name, after)) = after.split_once("</key>") else {
            break;
        };
        let Some((value, after)) = after
            .strip_prefix("<string>")
            .and_then(|after| after.split_once("</string>"))
        else {
            break;
        };
        pairs.push((unescape(name), unescape(value)));
        rest = after;
    }
    pairs
}

/// A path, a name or a value as XML text. An app can be kept in a folder whose name has an
/// ampersand in it, a proxy's password can hold one, and launchd refuses a plist that is not
/// well formed. A carriage return is written as a reference, since XML reads one written as
/// it is as a line feed.
fn escape(text: &str) -> String {
    text.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('\r', "&#13;")
}

/// What [`escape`] wrote, read back.
fn unescape(text: &str) -> String {
    text.replace("&lt;", "<")
        .replace("&gt;", ">")
        .replace("&#13;", "\r")
        .replace("&amp;", "&")
}

/// Whether XML 1.0 can hold `c` in a plist, as itself or as a reference: every character but
/// the control characters other than a tab, a line feed and a carriage return, and U+FFFE
/// and U+FFFF.
fn xml_holds(c: char) -> bool {
    !matches!(
        c,
        '\u{0}'..='\u{8}' | '\u{B}' | '\u{C}' | '\u{E}'..='\u{1F}' | '\u{FFFE}' | '\u{FFFF}'
    )
}

/// The refusal for a program or a variable [`plist`] could not write, naming which, where
/// one holds a character XML 1.0 cannot hold. launchd would refuse the plist, and saying
/// which holds it is what lets a person take it out. systemd's unit writes such a character
/// as an escape instead.
fn unwritable(program: &Path, environment: &[(String, String)]) -> Option<Error> {
    let program = program.to_string_lossy();
    std::iter::once((
        "the path of the program it runs".to_string(),
        program.as_ref(),
    ))
    .chain(environment.iter().flat_map(|(name, value)| {
        [
            (format!("the name {name}"), name.as_str()),
            (name.clone(), value.as_str()),
        ]
    }))
    .find_map(|(what, text)| {
        text.chars()
            .find(|c| !xml_holds(*c))
            .map(|c| Error::ScheduleRefused {
                detail: format!(
                    "a LaunchAgent cannot hold the character U+{:04X} that {what} holds, so \
                     nothing was scheduled. Take it out, then install the schedule again.",
                    u32::from(c)
                ),
            })
    })
}

#[cfg(test)]
#[allow(
    clippy::disallowed_methods,
    reason = "a test sets up its scratch files as a person or a tool would"
)]
mod tests {
    use super::*;
    use crate::host::memory::MemoryHost;
    use crate::schedule::{install, installed_program, repair};
    use std::os::unix::fs::PermissionsExt;

    /// launchd stops a job's own process when it unloads the job, and a repair unloads the
    /// schedule before loading it again. Made from inside the schedule's own job, as by an
    /// app the old schedule started, it would leave nothing loaded, so it is left to the app
    /// once it is opened any other way.
    #[test]
    fn a_schedule_is_not_repaired_from_inside_its_own_job() {
        let scratch = tempfile::tempdir().expect("a scratch folder");
        let home = scratch.path().join("home");
        std::fs::create_dir_all(home.join(".pitboard")).expect("a Pitboard home");
        let app = home.join("Applications/Pitboard.app/Contents/MacOS/Pitboard");
        let bundled = home.join("Applications/Pitboard.app/Contents/Helpers/pitboard");
        for program in [&app, &bundled] {
            std::fs::create_dir_all(program.parent().unwrap()).expect("its directory");
            std::fs::write(program, "").expect("a program");
        }
        let ctx = Context::new(home.clone()).with_memory_stores(MemoryHost::new());
        install(
            &ctx.clone().with_schedule_program(app.clone()),
            Permit::for_a_test(),
        )
        .expect("0.3.0's schedule");
        let the_app = ctx.clone().with_schedule_program(bundled.clone());

        assert!(
            !repair(
                &the_app.clone().with_scheduled_job(LABEL.into()),
                Permit::for_a_test()
            )
            .expect("nothing to do"),
            "started by the schedule"
        );
        assert_eq!(installed_program(&ctx), Some(app));
        assert_eq!(
            crate::audit::read(&ctx, 1)
                .iter()
                .map(|e| e.subject.as_str())
                .collect::<Vec<_>>(),
            ["install"],
            "and nothing recorded"
        );

        let opened = the_app.with_scheduled_job("application.com.datlechin.pitboard.1.2".into());
        assert!(
            repair(&opened, Permit::for_a_test()).expect("repaired"),
            "opened from Finder"
        );
        assert_eq!(installed_program(&ctx), Some(bundled));
    }

    /// The path is written as XML text and read back as the path it was.
    #[test]
    fn a_path_launchd_has_to_escape_reads_back_as_it_was() {
        let scratch = tempfile::tempdir().expect("a scratch folder");
        let home = scratch.path().join("home");
        let bundled = home.join("Tools&Apps/Pitboard.app/Contents/Helpers/pitboard");
        std::fs::create_dir_all(bundled.parent().unwrap()).expect("its directory");
        std::fs::write(&bundled, "").expect("a program");
        let ctx = Context::new(home.clone())
            .with_memory_stores(MemoryHost::new())
            .with_schedule_program(bundled.clone());
        install(&ctx, Permit::for_a_test()).expect("installed");
        let written = std::fs::read_to_string(Launchd::agent(&ctx, Job::Renew)).expect("the agent");
        assert!(written.contains("/Tools&amp;Apps/"), "{written}");
        assert!(!written.contains("/Tools&Apps/"), "{written}");
        assert_eq!(installed_program(&ctx), Some(bundled));
    }

    /// What it runs is one verb with no arguments, and what it does not do is as much the
    /// point as what it does.
    #[test]
    fn the_agent_runs_one_verb_and_does_not_run_at_load() {
        let body = plist(
            Job::Renew,
            Path::new("/usr/local/bin/pitboard"),
            &[],
            &[],
            Path::new("/Users/x"),
        );
        assert!(body.contains("<string>/usr/local/bin/pitboard</string>"));
        assert!(body.contains("<string>renew</string>"));
        assert!(!body.contains("status"), "it never asks for usage");
        assert!(!body.contains("use"), "and it never switches");
        assert!(
            body.contains("<key>RunAtLoad</key><false/>"),
            "installing it is not a reason to talk to Anthropic that second"
        );
        assert!(body.contains(&format!("<integer>{EVERY_SECONDS}</integer>")));
    }

    // At load, which is at each login too, and again a minute after it fails or crashes, which a
    // panic's exit and an older run meeting a newer state file both are.
    #[test]
    fn the_watch_agent_runs_at_load_and_again_a_minute_after_it_fails() {
        let at = ["--at".to_string(), "90".to_string()];
        let body = plist(
            Job::Watch,
            Path::new("/usr/local/bin/pitboard"),
            &at,
            &[],
            Path::new("/Users/x & y"),
        );
        assert!(
            body.contains(
                "  <key>Label</key><string>com.datlechin.pitboard.watch</string>\n  \
                 <key>ProgramArguments</key>\n  <array>\n    \
                 <string>/usr/local/bin/pitboard</string>\n    <string>watch</string>\n    \
                 <string>--scheduled</string>\n    <string>--at</string>\n    \
                 <string>90</string>\n  </array>\n  \
                 <key>RunAtLoad</key><true/>\n  \
                 <key>KeepAlive</key>\n  <dict>\n    \
                 <key>SuccessfulExit</key><false/>\n    <key>Crashed</key><true/>\n  </dict>\n  \
                 <key>ThrottleInterval</key><integer>60</integer>\n  \
                 <key>StandardOutPath</key>\
                 <string>/Users/x &amp; y/Library/Logs/Pitboard/watch.log</string>\n  \
                 <key>StandardErrorPath</key>\
                 <string>/Users/x &amp; y/Library/Logs/Pitboard/watch.log</string>\n\
                 </dict>\n"
            ),
            "{body}"
        );
        assert!(!body.contains("StartInterval"), "{body}");
        assert_eq!(
            arguments(&body),
            [
                "/usr/local/bin/pitboard",
                "watch",
                "--scheduled",
                "--at",
                "90"
            ]
        );
        let renewal = plist(
            Job::Renew,
            Path::new("/usr/local/bin/pitboard"),
            &[],
            &[],
            Path::new("/Users/x"),
        );
        assert!(!renewal.contains("Standard"), "{renewal}");
    }

    // launchd opens the log but makes no folder for it, and the log goes with the agent.
    #[test]
    fn the_watch_agent_keeps_its_lines_in_a_private_log_that_goes_with_it() {
        let scratch = tempfile::tempdir().expect("a scratch folder");
        let home = scratch.path().to_path_buf();
        let ctx = Context::new(home.clone()).with_memory_stores(MemoryHost::new());
        let launchd = Launchd::new(Arc::new(service::Pretend {
            refuse_start: Arc::default(),
        }));
        let log = home.join("Library/Logs/Pitboard/watch.log");
        let at = ["--at".to_string(), "90".to_string()];
        launchd
            .put(
                &ctx,
                Permit::for_a_test(),
                Job::Watch,
                Path::new("/usr/local/bin/pitboard"),
                &at,
                &[],
            )
            .expect("installed");
        let folder = log.parent().expect("its folder");
        assert_eq!(
            crate::host::fs::access(folder).map(|access| access.described),
            Some("mode 700".to_string())
        );
        std::fs::write(&log, "Watching Claude Code.\n").expect("launchd's");

        assert!(
            launchd
                .remove(&ctx, Permit::for_a_test(), Job::Watch)
                .expect("taken away")
        );
        assert!(!folder.exists());
    }

    /// launchd refuses a plist that is not well formed, and a folder's name can hold any of
    /// the characters XML gives a meaning to.
    #[test]
    fn the_agent_writes_a_path_as_xml_text() {
        let body = plist(
            Job::Renew,
            Path::new("/Users/x/A&B <old>/Pitboard.app"),
            &[],
            &[],
            Path::new("/Users/x"),
        );
        assert!(
            body.contains("<string>/Users/x/A&amp;B &lt;old&gt;/Pitboard.app</string>"),
            "{body}"
        );
    }

    /// The pairs a job is given, as `install` hands them over.
    fn pairs(given: &[(&str, &str)]) -> Vec<(String, String)> {
        given
            .iter()
            .map(|(name, value)| ((*name).to_string(), (*value).to_string()))
            .collect()
    }

    /// The job runs with launchd's environment, not the shell's, so what it is given of the
    /// installing Pitboard's, `PITBOARD_NO_ARGV` and the proxy variables, is written into it
    /// as XML text, and nothing is written where there is nothing to give. What was written
    /// reads back as it was, and the program is still read back too.
    #[test]
    fn the_agent_gives_its_job_the_variables_it_is_handed_and_reads_them_back() {
        let program = Path::new("/usr/local/bin/pitboard");
        let handed = pairs(&[
            ("PITBOARD_NO_ARGV", "1"),
            ("ALL_PROXY", "socks5h://alice:p&ss<w>rd@127.0.0.1:1080"),
            ("HTTPS_PROXY", "http://proxy.example.com:3128"),
            ("no_proxy", ""),
            ("NO_PROXY", ".anthropic.com,localhost"),
        ]);
        let body = plist(Job::Renew, program, &[], &handed, Path::new("/Users/x"));
        assert!(
            body.contains(
                "  <key>EnvironmentVariables</key>\n  <dict>\n    \
                 <key>PITBOARD_NO_ARGV</key><string>1</string>\n    \
                 <key>ALL_PROXY</key><string>socks5h://alice:p&amp;ss&lt;w&gt;rd@127.0.0.1:1080\
                 </string>\n    \
                 <key>HTTPS_PROXY</key><string>http://proxy.example.com:3128</string>\n    \
                 <key>no_proxy</key><string></string>\n    \
                 <key>NO_PROXY</key><string>.anthropic.com,localhost</string>\n  </dict>\n\
                 </dict>\n"
            ),
            "{body}"
        );
        assert_eq!(given(&body), handed);
        assert!(
            !plist(Job::Renew, program, &[], &[], Path::new("/Users/x"))
                .contains("EnvironmentVariables")
        );
        assert_eq!(
            given(&plist(Job::Renew, program, &[], &[], Path::new("/Users/x"))),
            Vec::new()
        );
        let (_, after) = body
            .split_once("<key>ProgramArguments</key>")
            .expect("its arguments");
        assert!(
            after
                .trim_start()
                .starts_with("<array>\n    <string>/usr/local/bin/pitboard")
        );
    }

    /// A variable holding a character XML 1.0 cannot hold, which launchd would refuse the
    /// whole plist for, is refused before anything is written, naming the variable. A
    /// carriage return, which XML would read back as a line feed, is written as a reference
    /// and reads back as itself.
    #[test]
    fn a_variable_a_plist_cannot_hold_is_refused_by_name() {
        let scratch = tempfile::tempdir().expect("a scratch folder");
        let home = scratch.path().join("home");
        let program = home.join("bin/pitboard");
        std::fs::create_dir_all(program.parent().expect("its directory")).expect("made");
        std::fs::write(&program, "").expect("a program");
        let home_text = home.to_string_lossy().into_owned();
        let env: crate::context::Environment = [
            ("HOME", home_text.as_str()),
            ("HTTPS_PROXY", "http://proxy.example.com:3128"),
            ("ALL_PROXY", "socks5h://alice:s3\u{1}cret@127.0.0.1:1080"),
        ]
        .into_iter()
        .collect();
        let ctx = Context::for_command_line(&env)
            .with_memory_stores(MemoryHost::new())
            .with_schedule_program(program);
        let refused = install(&ctx, Permit::for_a_test()).expect_err("refused");
        assert_eq!(refused.code(), "schedule_refused");
        let said = refused.to_string();
        assert!(
            said.contains("U+0001 that ALL_PROXY holds") && !said.contains("s3"),
            "{said}"
        );
        assert!(
            !Launchd::agent(&ctx, Job::Renew).exists(),
            "nothing was written"
        );

        let handed = pairs(&[("NO_PROXY", "a\rb")]);
        let body = plist(
            Job::Renew,
            Path::new("/usr/local/bin/pitboard"),
            &[],
            &handed,
            Path::new("/Users/x"),
        );
        assert!(body.contains("<string>a&#13;b</string>"), "{body}");
        assert_eq!(given(&body), handed);
        assert_eq!(
            unwritable(Path::new("/usr/local/bin/pitboard"), &handed).map(|e| e.to_string()),
            None
        );
    }

    /// `pitboard schedule install` writes the proxy variables it was started with into the
    /// agent, and the agent only the person can read, mode 600, since a proxy's address can
    /// hold a password: over an agent that was open to others too, which kept its mode when
    /// Pitboard wrote it before.
    #[test]
    fn an_installed_agent_carries_the_proxy_and_only_its_owner_can_read_it() {
        let scratch = tempfile::tempdir().expect("a scratch folder");
        let home = scratch.path().join("home");
        let program = home.join("bin/pitboard");
        std::fs::create_dir_all(program.parent().expect("its directory")).expect("made");
        std::fs::write(&program, "").expect("a program");
        let home_text = home.to_string_lossy().into_owned();
        let env: crate::context::Environment = [
            ("HOME", home_text.as_str()),
            ("ALL_PROXY", "socks5h://alice:s3cret@127.0.0.1:1080"),
            ("no_proxy", "localhost"),
        ]
        .into_iter()
        .collect();
        let ctx = Context::for_command_line(&env)
            .with_memory_stores(MemoryHost::new())
            .with_schedule_program(program.clone());
        let agent = Launchd::agent(&ctx, Job::Renew);
        std::fs::create_dir_all(agent.parent().expect("its directory")).expect("made");
        std::fs::write(&agent, "an agent open to others\n").expect("written");
        std::fs::set_permissions(&agent, std::fs::Permissions::from_mode(0o644))
            .expect("opened to others");

        install(&ctx, Permit::for_a_test()).expect("installed");
        let written = std::fs::read_to_string(&agent).expect("the agent");
        assert!(
            written.contains(
                "<key>ALL_PROXY</key><string>socks5h://alice:s3cret@127.0.0.1:1080</string>\n    \
                 <key>no_proxy</key><string>localhost</string>\n"
            ),
            "{written}"
        );
        assert_eq!(
            crate::host::fs::access(&agent).map(|access| access.described),
            Some("mode 600".to_string())
        );
        assert_eq!(installed_program(&ctx), Some(program));
    }
}