shep 0.25.2

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
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
//! Enabling, disabling, adopting and rehoming a dog, plus taking the legacy
//! `[dog.<name>]` sections a pre-migration file still carries.

use super::*;

/// fails if the writer round-trips through a plain `toml::Table`,
/// losing comments and key order.
#[test]
fn enabling_a_dog_leaves_the_rest_of_the_file_exactly_as_it_was() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, PRESERVATION_SEED).unwrap();

    ShepToml::try_edit(&path, |doc| doc.enable_dog("metrics")).unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert_preserves_comments_and_key_order(&written);

    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert_eq!(cfg.daemon.enabled_dogs, vec!["metrics"]);
    assert!(
        cfg.dog.is_empty(),
        "enable writes no dog section at all; the next boot refuses a \
             name held in both files: {written}"
    );
}
#[test]
fn enable_is_idempotent_and_disable_keeps_the_config_it_did_not_write() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, "[dog.bark]\ndebounce = \"30s\"\n").unwrap();

    ShepToml::edit(&path, |doc| {
        doc.enable_dog("bark").unwrap();
        doc.enable_dog("bark").unwrap();
    })
    .unwrap();
    let cfg =
        DaemonConfig::load(Some(&std::fs::read_to_string(&path).unwrap()), &|_| None).unwrap();
    assert_eq!(cfg.daemon.enabled_dogs, vec!["bark"]);

    ShepToml::edit(&path, |doc| doc.disable_dog("bark")).unwrap();
    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert!(cfg.daemon.enabled_dogs.is_empty());
    assert!(
        written.contains("30s"),
        "disable stops a dog; it never touches what the operator wrote"
    );
}
/// Two places have to be empty afterwards, `[daemon] adopted_dogs` and
/// `enabled_dogs`, and one has to be untouched: a `[dog.<name>]` an
/// un-migrated file still carries.
#[test]
fn rehoming_a_dog_forgets_its_adoption_and_keeps_its_settings() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    // Seeded by hand: no writer here creates a `[dog.<name>]` any more,
    // but an un-migrated file carries one.
    std::fs::write(&path, "[dog.otel]\ndebounce = \"30s\"\n").unwrap();
    ShepToml::edit(&path, |doc| {
        doc.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"), false)
            .unwrap();
    })
    .unwrap();
    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert_eq!(cfg.daemon.enabled_dogs, vec!["otel"]);
    assert_eq!(
        cfg.daemon
            .adopted_dogs
            .get("otel")
            .map(|entry| entry.path.as_path()),
        Some(Path::new("/usr/local/bin/shep-otel"))
    );
    assert!(cfg.dog.contains_key("otel"));

    ShepToml::edit(&path, |doc| doc.rehome_dog("otel")).unwrap();
    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert!(cfg.daemon.enabled_dogs.is_empty());
    assert!(!cfg.daemon.adopted_dogs.contains_key("otel"));
    assert!(
        cfg.dog.contains_key("otel"),
        "the settings an operator wrote survive a rehome: {written}"
    );
}
#[test]
fn adopted_dog_path_reads_what_adopt_dog_wrote_and_nothing_else() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    ShepToml::edit(&path, |doc| {
        doc.enable_dog("metrics").unwrap(); // built-in: no `adopted_dogs` entry at all
        doc.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"), false)
            .unwrap();

        assert_eq!(
            doc.adopted_dog_path("otel"),
            Some(PathBuf::from("/usr/local/bin/shep-otel"))
        );
        assert_eq!(doc.adopted_dog_path("metrics"), None);
        assert_eq!(doc.adopted_dog_path("ghost"), None);
    })
    .unwrap();
}
/// `enabled_dog_names` is `adopted_dog_names`'s sibling and was left
/// untouched by the brief's own six tests. A dog can be adopted and not
/// enabled, or (for a built-in) enabled without ever being adopted, so
/// this reads `[daemon] enabled_dogs` specifically, in file order.
#[test]
fn enabled_dog_names_reads_the_array_in_file_order() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, "[daemon]\nenabled_dogs = [\"metrics\", \"bark\"]\n").unwrap();

    let cfg = ShepToml::read_only(&path).unwrap();
    assert_eq!(cfg.enabled_dog_names(), vec!["metrics", "bark"]);
}
/// A document with no `[daemon] enabled_dogs` at all reads as empty,
/// never a panic or a default entry invented on its behalf.
#[test]
fn enabled_dog_names_is_empty_when_the_document_never_wrote_it() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, "[daemon]\nlog_level = \"debug\"\n").unwrap();

    let cfg = ShepToml::read_only(&path).unwrap();
    assert_eq!(cfg.enabled_dog_names(), Vec::<String>::new());
}
#[test]
fn taking_dog_sections_returns_them_keyed_by_name() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("shep.toml");
    std::fs::write(
            &path,
            "[daemon]\nenabled_dogs = [\"metrics\"]\n\n[dog.metrics]\nbind = \"127.0.0.1:9615\"\n\n[dog.bark.sinks]\noncall = { kind = \"discord\" }\n",
        )
        .expect("write");

    let taken = ShepToml::edit(&path, ShepToml::take_dog_sections).expect("edit");

    assert_eq!(taken.keys().collect::<Vec<_>>(), vec!["bark", "metrics"]);
    assert_eq!(taken["metrics"]["bind"].as_str(), Some("127.0.0.1:9615"));
}
#[test]
fn taking_dog_sections_leaves_every_other_section_alone() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("shep.toml");
    std::fs::write(
            &path,
            "# keep me\n[daemon]\nenabled_dogs = [\"metrics\"]\n\n[dog.metrics]\nbind = \"127.0.0.1:9615\"\n\n[style]\nlevel = \"full\"\n",
        )
        .expect("write");

    ShepToml::edit(&path, ShepToml::take_dog_sections).expect("edit");

    // Exact string: a `toml::Table` round-trip would drop the comment
    // or reorder the key.
    assert_eq!(
        std::fs::read_to_string(&path).expect("read"),
        "# keep me\n[daemon]\nenabled_dogs = [\"metrics\"]\n\n[style]\nlevel = \"full\"\n"
    );
}
#[test]
fn taking_from_a_file_with_no_dog_sections_changes_nothing() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("shep.toml");
    let before = "[daemon]\nlog_level = \"info\"\n";
    std::fs::write(&path, before).expect("write");

    let taken = ShepToml::edit(&path, ShepToml::take_dog_sections).expect("edit");

    assert!(taken.is_empty());
    // Content identity, not proof that nothing was written: `edit` always
    // stages and renames, so the file has a new inode either way. Not
    // writing at all is the migration's job, and its own early return is
    // where that is tested.
    assert_eq!(std::fs::read_to_string(&path).expect("read"), before);
}
#[test]
fn taking_dog_sections_keeps_nested_tables_and_arrays_of_tables() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("shep.toml");
    std::fs::write(
            &path,
            "[dog.bark.sinks]\noncall = { kind = \"discord\", url = \"https://discord.com/api/webhooks/x\" }\n\n[[dog.bark.rules]]\non = \"gave_up\"\nsinks = [\"oncall\"]\n",
        )
        .expect("write");

    let taken = ShepToml::edit(&path, ShepToml::take_dog_sections).expect("edit");

    let bark = &taken["bark"];
    assert_eq!(
        bark["sinks"]["oncall"]["url"].as_str(),
        Some("https://discord.com/api/webhooks/x"),
        "a nested sub-table's own values must survive the take"
    );
    // `as_array_of_tables`, not `as_array`: `[[dog.bark.rules]]` is a
    // `toml_edit::ArrayOfTables`, a document construct, where `sinks =
    // ["oncall"]` below is a `Value::Array`.
    let rules = bark["rules"]
        .as_array_of_tables()
        .expect("rules is an array of tables");
    assert_eq!(rules.len(), 1);
    let rule = rules.get(0).expect("one rule");
    assert_eq!(rule["on"].as_str(), Some("gave_up"));
    assert_eq!(
        rule["sinks"]
            .as_array()
            .and_then(|sinks| sinks.get(0))
            .and_then(toml_edit::Value::as_str),
        Some("oncall"),
        "the array-of-tables entry keeps its own array field"
    );
}
#[test]
fn taking_dog_sections_keeps_an_inline_table_dog() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, "[dog]\nmetrics = { bind = \"127.0.0.1:9615\" }\n").expect("write");

    let taken = ShepToml::edit(&path, ShepToml::take_dog_sections).expect("edit");

    assert_eq!(
        taken["metrics"]["bind"].as_str(),
        Some("127.0.0.1:9615"),
        "an inline-table dog under [dog] must not be dropped"
    );
}

/// Three keys under `[daemon]` are written by the dog verbs and hand-edited
/// by operators, and all three refuse a shape they did not write instead of
/// `.expect()` on it.
///
/// One case each, and each asserts the file is left alone.
#[test]
fn a_hand_edited_daemon_key_is_refused_rather_than_panicked_on() {
    let cases = [
        ("daemon = \"loud\"\n", "daemon", "a table"),
        (
            "[daemon]\nenabled_dogs = \"metrics\"\n",
            "enabled_dogs",
            "an array",
        ),
        (
            "[daemon]\nadopted_dogs = \"nope\"\n",
            "adopted_dogs",
            "a table",
        ),
    ];
    for (original, expected_key, expected_shape) in cases {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        std::fs::write(&path, original).unwrap();

        // `adopt_dog` reaches all three keys: `[daemon]`, then
        // `adopted_dogs`, then `enabled_dogs` through `enable_dog`.
        let refusal = ShepToml::try_edit(&path, |cfg| {
            cfg.adopt_dog("metrics", Path::new("/usr/local/bin/shep-metrics"), false)
        });

        match refusal {
            Err(ref err @ ShepTomlError::WrongShape { key, found, .. }) => {
                assert_eq!(key, expected_key, "names the key an operator has to fix");
                assert!(!found.is_empty(), "says what it found instead");
                // `enabled_dogs` is an array and the other two are tables, so
                // a fixed word here would tell an operator to write the wrong
                // thing before refusing the edit.
                assert!(
                    err.to_string().contains(expected_shape),
                    "{key} must be {expected_shape}: {err}"
                );
            }
            other => panic!("expected WrongShape on {original:?}, got {other:?}"),
        }
        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            original,
            "a refusal writes nothing at all"
        );
    }
}

/// The refusal is the unhappy path, so pin that the happy one still writes.
/// A `section_table_mut` that refused everything would pass the test above.
#[test]
fn a_daemon_table_shep_wrote_itself_still_takes_a_dog() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(&path, "[daemon]\nlog_level = \"info\"\n").unwrap();

    ShepToml::try_edit(&path, |cfg| {
        cfg.adopt_dog("metrics", Path::new("/usr/local/bin/shep-metrics"), false)
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert_eq!(cfg.daemon.enabled_dogs, vec!["metrics"]);
    assert!(
        written.contains("shep-metrics"),
        "the binary landed: {written}"
    );
}

/// A re-adopt reads the binary as it is now, so a dog whose new build stops
/// asking must lose the channel rather than keep a stale grant.
#[test]
fn a_channel_ask_is_recorded_and_a_readopt_that_does_not_ask_forgets_it() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    let binary = Path::new("/usr/local/bin/shep-otel");

    ShepToml::try_edit(&path, |cfg| cfg.adopt_dog("otel", binary, true)).unwrap();
    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert_eq!(cfg.daemon.channel_dogs, vec!["otel"]);
    assert!(
        ShepToml::read_only(&path)
            .unwrap()
            .asked_for_channel("otel")
    );

    ShepToml::try_edit(&path, |cfg| cfg.adopt_dog("otel", binary, false)).unwrap();
    let written = std::fs::read_to_string(&path).unwrap();
    assert!(
        !ShepToml::read_only(&path)
            .unwrap()
            .asked_for_channel("otel")
    );
    assert!(
        !written.contains("channel_dogs"),
        "an emptied key goes, so an older shep still loads the file: {written}"
    );
}

/// The ordinary adopt writes no key an older shep would refuse to load.
#[test]
fn adopting_a_dog_that_did_not_ask_writes_no_channel_key() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");

    ShepToml::try_edit(&path, |cfg| {
        cfg.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"), false)
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(!written.contains("channel_dogs"), "{written}");
}

#[test]
fn rehoming_a_dog_forgets_its_channel_ask_and_keeps_another_dogs() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    ShepToml::try_edit(&path, |cfg| {
        cfg.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"), true)?;
        cfg.adopt_dog("jobs", Path::new("/usr/local/bin/shep-jobs"), true)
    })
    .unwrap();

    ShepToml::edit(&path, |cfg| cfg.rehome_dog("otel")).unwrap();

    let cfg = ShepToml::read_only(&path).unwrap();
    assert!(!cfg.asked_for_channel("otel"));
    assert!(cfg.asked_for_channel("jobs"));
}

#[test]
fn a_hand_edited_channel_dogs_is_refused_rather_than_overwritten() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    let original = "[daemon]\nchannel_dogs = \"otel\"\n";
    std::fs::write(&path, original).unwrap();

    let refusal = ShepToml::try_edit(&path, |cfg| {
        cfg.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"), true)
    });

    assert!(
        matches!(
            refusal,
            Err(ShepTomlError::WrongShape {
                key: "channel_dogs",
                ..
            })
        ),
        "{refusal:?}"
    );
    assert_eq!(std::fs::read_to_string(&path).unwrap(), original);
}

/// The entry is the operator's, and the path is the one `shep adopt` wrote.
/// A bare path string gains its `env` half rather than losing the path, and
/// a re-adopt keeps the `env` the operator set.
#[test]
fn setting_a_dogs_env_rewrites_its_entry_and_keeps_its_path() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "[daemon]\nenabled_dogs = [\"otel\"]\n\n[daemon.adopted_dogs]\notel = \"/usr/local/bin/shep-otel\"\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "PADDOCK_KEY", Some("{{secret:paddock/key}}"))
            .unwrap();
        cfg.set_adopted_dog_env("otel", "RUST_LOG", Some("warn"))
            .unwrap();
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    let cfg = ShepToml::read_only(&path).unwrap();
    assert_eq!(
        cfg.adopted_dog_path("otel"),
        Some(PathBuf::from("/usr/local/bin/shep-otel")),
        "{written}"
    );
    assert_eq!(
        cfg.adopted_dog_env("otel"),
        BTreeMap::from([
            (
                "PADDOCK_KEY".to_owned(),
                "{{secret:paddock/key}}".to_owned()
            ),
            ("RUST_LOG".to_owned(), "warn".to_owned()),
        ]),
        "{written}"
    );
    DaemonConfig::load(Some(&written), &|_| None)
        .expect("the file the writer produced is one the daemon reads");

    ShepToml::edit(&path, |cfg| {
        cfg.adopt_dog("otel", Path::new("/opt/new/shep-otel"), false)
            .unwrap();
    })
    .unwrap();
    let cfg = ShepToml::read_only(&path).unwrap();
    assert_eq!(
        (
            cfg.adopted_dog_path("otel"),
            cfg.adopted_dog_env("otel")
                .keys()
                .map(String::as_str)
                .collect::<Vec<_>>(),
        ),
        (
            Some(PathBuf::from("/opt/new/shep-otel")),
            vec!["PADDOCK_KEY", "RUST_LOG"],
        ),
        "a re-adopt replaces the binary, not what the operator set beside it"
    );
}

/// Removing the last key leaves no `env` behind, so the entry goes back to
/// the shape every `shep.toml` had before this key existed.
#[test]
fn removing_a_dogs_last_env_key_leaves_no_empty_env_behind_it() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "[daemon.adopted_dogs.otel]\npath = \"/usr/local/bin/shep-otel\"\nenv = { RUST_LOG = \"warn\" }\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "RUST_LOG", None).unwrap();
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(!written.contains("env"), "{written}");
    assert_eq!(
        ShepToml::read_only(&path).unwrap().adopted_dog_env("otel"),
        BTreeMap::new(),
    );
}

/// A built-in dog has no entry to put an `env` key in, and the refusal says
/// which dog it means rather than naming a path the operator never wrote.
#[test]
fn setting_an_env_on_a_built_in_dog_refuses_and_writes_nothing() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    let original = "[daemon]\nenabled_dogs = [\"metrics\"]\n";
    std::fs::write(&path, original).unwrap();

    let refusal = ShepToml::try_edit(&path, |cfg| {
        cfg.set_adopted_dog_env("metrics", "RUST_LOG", Some("warn"))
    });

    assert!(
        matches!(
            refusal,
            Err(ShepTomlError::NoDogEntry {
                dog: _,
                found: "nothing",
                ..
            })
        ),
        "{refusal:?}"
    );
    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        original,
        "a refused write leaves the file as it was"
    );
}

/// `otel = { path = "...", env = { ... } }` is the same entry
/// [`DaemonConfig::load`] reads and [`ShepToml::adopted_dog_env`] reads, so a
/// writer that refused it would leave a valid `shep.toml` uneditable through
/// the door that edits it. It also stays inline: the shape is the operator's.
#[test]
fn setting_an_env_key_on_an_inline_dog_entry_keeps_it_inline() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "[daemon.adopted_dogs]\notel = { path = \"/usr/local/bin/shep-otel\", env = { RUST_LOG = \"warn\" } }\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "PADDOCK_KEY", Some("{{secret:paddock/key}}"))
            .unwrap();
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(
        written.contains("otel = {"),
        "the entry stays inline: {written}"
    );
    assert_eq!(
        ShepToml::read_only(&path).unwrap().adopted_dog_path("otel"),
        Some(PathBuf::from("/usr/local/bin/shep-otel")),
        "and it keeps its path: {written}"
    );
    let keys: Vec<String> = ShepToml::read_only(&path)
        .unwrap()
        .adopted_dog_env("otel")
        .keys()
        .cloned()
        .collect();
    assert_eq!(keys, vec!["PADDOCK_KEY", "RUST_LOG"], "{written}");
    let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
    assert_eq!(
        cfg.daemon.adopted_dogs.get("otel").unwrap().env["PADDOCK_KEY"],
        "{{secret:paddock/key}}",
        "the reference is stored as written, as a sheep's `env` stores one"
    );
}

/// A bare path entry gains the `env` half and becomes `{ path, env }`, and
/// everything the operator wrote around it stays where they put it: a comment
/// above the entry, one trailing it, and the entries either side in their order.
#[test]
fn adding_an_env_key_to_a_bare_path_entry_keeps_the_comments_and_the_order_around_it() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "# which dogs run\n[daemon]\nenabled_dogs = [\"otel\"]\n\n[daemon.adopted_dogs]\n\
         # first, as the fleet's collector\nalpha = \"/opt/alpha\"\n\
         # the exporter\notel = \"/usr/local/bin/shep-otel\" # built from main\n\
         zulu = \"/opt/zulu\"\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "PADDOCK_KEY", Some("x"))
            .unwrap();
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert_eq!(
        written,
        "# which dogs run\n[daemon]\nenabled_dogs = [\"otel\"]\n\n[daemon.adopted_dogs]\n\
         # first, as the fleet's collector\nalpha = \"/opt/alpha\"\n\
         # the exporter\notel = { path = \"/usr/local/bin/shep-otel\", env = { PADDOCK_KEY = \"x\" } } # built from main\n\
         zulu = \"/opt/zulu\"\n"
    );
    DaemonConfig::load(Some(&written), &|_| None)
        .expect("the file the writer produced is one the daemon reads");
}

/// The same entry written as a table keeps the comments inside it.
#[test]
fn removing_the_last_env_key_keeps_the_comments_inside_the_entry() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "# the exporter\n[daemon.adopted_dogs.otel]\n# its binary\npath = \"/usr/local/bin/shep-otel\"\n\
         env = { RUST_LOG = \"warn\" }\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "RUST_LOG", None).unwrap();
    })
    .unwrap();

    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        "# the exporter\n[daemon.adopted_dogs.otel]\n# its binary\npath = \"/usr/local/bin/shep-otel\"\n"
    );
}

/// Keys already in an entry's `env` keep the order the operator wrote them in,
/// and a changed one changes where it is.
#[test]
fn an_env_edit_keeps_the_order_of_the_keys_already_there() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("shep.toml");
    std::fs::write(
        &path,
        "[daemon.adopted_dogs]\notel = { path = \"/opt/otel\", env = { ZED = \"1\", ALPHA = \"2\" } }\n",
    )
    .unwrap();

    ShepToml::edit(&path, |cfg| {
        cfg.set_adopted_dog_env("otel", "MID", Some("3")).unwrap();
        cfg.set_adopted_dog_env("otel", "ZED", Some("9")).unwrap();
    })
    .unwrap();

    let written = std::fs::read_to_string(&path).unwrap();
    assert!(
        written.contains("env = { ZED = \"9\", ALPHA = \"2\", MID = \"3\" }"),
        "{written}"
    );
}