shep 0.25.0

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
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
//! The community dog index: fetching it, validating it, and treating every
//! string in it as hostile input.
//!
//! `shep dogs --available` prints this document to a terminal, so every
//! surviving string passes through [`sanitise`]. Response headers are
//! sanitised a layer below, in [`crate::fetch`].
//!
//! A malformed entry is skipped and counted, never fatal. Only
//! [`IndexError::InsecureUrl`] names the URL. [`parse`] owns the document
//! grammar; this file owns the types, the URL policy and the fetch.
use core::fmt;
use std::collections::BTreeMap;
use std::time::Duration;

use serde::Serialize;
use serde_json::Value;

use crate::fetch::{self, FetchError};
use crate::terminal_safe::sanitise;

mod cache;
mod parse;
mod prebuilt;

#[cfg(test)]
pub(crate) use cache::write_cache;
pub use cache::{
    LoadedIndex, MAX_AGE, Origin, cached_index, cached_only, cached_with_age, describe_age,
};
pub use parse::parse_index;
pub use prebuilt::{BUILD_TARGET, Prebuilt};

/// Where the index lives when nothing overrides it. An exact file path, so
/// it answers 200 rather than redirecting: [`crate::fetch::get`] refuses
/// redirects.
pub const DEFAULT_INDEX_URL: &str = "https://shep-pm.com/dogs.json";

/// The environment variable that overrides [`DEFAULT_INDEX_URL`], for
/// self-hosting and for the integration tests. Trusted input: whoever can
/// set it can already run `shep`.
pub const INDEX_URL_ENV: &str = "SHEP_DOG_INDEX";

/// Where an entry naming a category this build does not know is filed.
///
/// A category is a grouping heading, so an unknown one means the index is
/// ahead of this binary rather than that the entry is bad. Filing it keeps
/// the dog listed and adoptable. Itself one of [`CATEGORIES`].
const UNKNOWN_CATEGORY_FALLBACK: &str = "other";

/// The seven categories a dog can be filed under, in the docs site's order.
///
/// `the_categories_match_the_docs_site_list` and
/// `the_schema_agrees_with_the_categories_and_source_kinds` hold this equal
/// to `web/src/data/dogs.ts` and `web/public/dogs.schema.json`. This list is
/// compiled in while the index is served live, so an installed binary can be
/// older than the index it reads: see [`UNKNOWN_CATEGORY_FALLBACK`].
pub(crate) const CATEGORIES: [&str; 7] = [
    "logs",
    "metrics",
    "alerts",
    "health",
    "deploy",
    "interactive",
    UNKNOWN_CATEGORY_FALLBACK,
];

/// The only `version` this build's [`parse_index`] accepts. Bump this and
/// the published `dogs.json` together when the wrapper's shape changes. An
/// entry's own shape may grow independently: a bad entry is skipped, not
/// refused.
const SUPPORTED_INDEX_VERSION: u64 = 1;

/// The response cap. A megabyte is roughly two thousand entries at the size
/// the live index's own entries run.
const SIZE_LIMIT: usize = 1 << 20;

/// End-to-end budget for the fetch, connect and TLS handshake included.
const TIMEOUT: Duration = Duration::from_secs(10);

/// Hosts that may serve the index over plain `http://`. See
/// [`require_secure_url`].
const LOOPBACK_HOSTS: [&str; 4] = ["localhost", "127.0.0.1", "::1", "[::1]"];

/// One dog an operator could adopt, with every string already sanitised.
///
/// `Debug` needs no redaction: every field came out of a public JSON
/// document.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct AvailableDog {
    /// The dog's own name, displayed rather than typed.
    pub name: String,
    /// The crate or repository name: the dog's real identity.
    pub package: String,
    /// The name this dog expects to be adopted under. A dog is given no
    /// argv and cannot learn its own adopted name, so `shep adopt <path>
    /// --name <name>` with the wrong `<name>` silently discards its entire
    /// `[dog.<name>]` section. Build an adopt line from this field, never
    /// from [`Self::name`] or [`Self::package`].
    pub adopt_as: String,
    /// One line describing what the dog does.
    pub description: String,
    /// HTTPS URL of the dog's repository.
    pub repo: String,
    /// SPDX license string.
    pub license: String,
    /// Always one of [`CATEGORIES`]: an entry naming anything else is
    /// filed under [`UNKNOWN_CATEGORY_FALLBACK`] rather than dropped.
    pub category: String,
    /// How the dog is built.
    pub source: DogSourceKind,
    /// The permissions the approved version declares, as the index lists
    /// them: `None` when the row lists none, `Some(vec![])` when the version
    /// declares none.
    ///
    /// What the index claims, not what the binary asks for: `shep install`
    /// reads the manifest off the binary and prompts from that.
    pub permissions: Option<Vec<IndexedPermission>>,
    /// The needs this dog meets, each once, in the order the row lists
    /// them. A name this build does not know is not here.
    ///
    /// Not serialised: no listing shows it yet.
    #[serde(skip)]
    pub needs: Vec<Need>,
}

/// A need shep writes a hint for, from the fixed list shep owns.
///
/// The index maps a dog to the needs it meets. A name in the index that is
/// not here is ignored, so the index can add a need before this build shows it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Need {
    /// Reading and keeping a sheep's logs.
    Logs,
    /// Being told when a sheep needs attention.
    Alerts,
}

impl Need {
    /// Every need shep knows, in the order it lists them.
    pub const ALL: [Self; 2] = [Self::Logs, Self::Alerts];

    /// The name the index spells this need as.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Logs => "logs",
            Self::Alerts => "alerts",
        }
    }

    /// The need an index names, or `None` for a name this build does not
    /// know. Exact match: no case folding, no trimming.
    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|need| need.as_str() == name)
    }
}

/// One permission a `cargo` row says its approved version asks for.
///
/// The name and whether the dog can run without it, and nothing else: the
/// reason a dog gives for asking a permission stays in the manifest, which
/// is what the grant prompt prints. `Debug` needs no redaction, for
/// [`AvailableDog`]'s reason.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct IndexedPermission {
    /// The permission's name, such as `flock.read` or `events:sheep.`,
    /// sanitised like every other string the index carries.
    pub name: String,
    /// Whether the dog cannot run without it.
    pub required: bool,
}

/// How a dog is installed, tagged by `kind` exactly as the index tags it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum DogSourceKind {
    /// Installable with `cargo install <package>` from crates.io. The
    /// package name is [`AvailableDog::package`], not a field here.
    Cargo {
        /// The exact version the index approves. A row without one is
        /// listed but cannot be installed.
        version: Option<String>,
        /// Hex sha256 of the prebuilt binary, keyed by exact target
        /// triple. Empty when the index hashes none.
        sha256: BTreeMap<String, String>,
    },
    /// Installable with `cargo install --git <url>`, for a dog that is not
    /// on crates.io.
    CargoGit {
        /// The repository to install from, always `https://`.
        url: String,
    },
    /// Installable with `go install <module>@latest`.
    GoInstall {
        /// The Go module path. Not a URL, and not checked as one.
        module: String,
    },
    /// No one-line installer; `instructions` is prose, never a command to
    /// run.
    Manual {
        /// What the entry says to do instead, sanitised like every other
        /// string here.
        instructions: String,
    },
}

/// A parsed index, with the two counts a caller prints.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Index {
    /// The entries that validated, in the order the document listed them.
    pub dogs: Vec<AvailableDog>,
    /// How many entries were dropped for failing validation.
    pub skipped: usize,
    /// How many surviving entries had something stripped. Once per entry,
    /// never once per field, and never for a skipped entry.
    pub sanitised: usize,
    /// The latest released shep the document names, or `None` when it names
    /// none or one that is not a semver version.
    pub shep: Option<semver::Version>,
}

/// Why reading the index failed outright, as opposed to the per-entry
/// problems [`Index::skipped`] counts.
///
/// Not `#[non_exhaustive]`: every module in this crate is private, so no
/// out-of-tree consumer can match on it. `Debug` needs no redaction, a dog
/// index URL being a public location rather than a credential.
#[derive(Debug)]
pub enum IndexError {
    /// The index URL was not `https://` and its host was not a loopback
    /// literal. Carries the URL, which is public by construction.
    InsecureUrl(String),
    /// The request itself failed, was refused, or came back malformed.
    Fetch(FetchError),
    /// The bytes were not JSON at all. Carries the parser's complaint,
    /// never the offending bytes.
    Malformed(String),
    /// The bytes were JSON, but the top level was not an object. Distinct
    /// from [`Self::Malformed`]: a bare array is a wrong document, not a
    /// broken one, and an empty listing would be the wrong answer for it.
    NotAnObject,
    /// The document's `version` was missing, or named a version this build
    /// does not understand. `found` is the value exactly as the document
    /// had it, `None` when the key was absent.
    UnsupportedVersion {
        /// The `version` value the document carried, if any.
        found: Option<Value>,
    },
    /// The document named a `version` this build understands, but its
    /// `dogs` field was missing or was not itself an array.
    MissingDogsArray,
}

impl fmt::Display for IndexError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InsecureUrl(url) => write!(
                f,
                "dog index url {url} is not https://; the index is read over TLS \
                 unless it is served from loopback"
            ),
            Self::Fetch(source) => write!(f, "{source}"),
            Self::Malformed(reason) => write!(f, "the dog index was not valid json: {reason}"),
            Self::NotAnObject => write!(
                f,
                "the dog index was not a json object -- a bare array is the shape shep 0.1.0 \
                 read, and is no longer accepted"
            ),
            Self::UnsupportedVersion { found } => {
                let found = found
                    .as_ref()
                    .map_or_else(|| "unspecified".to_string(), Value::to_string);
                write!(
                    f,
                    "the dog index is version {found}, which this shep does not understand \
                     (this build reads version {SUPPORTED_INDEX_VERSION}); upgrade shep to read it"
                )
            }
            Self::MissingDogsArray => write!(f, "the dog index has no \"dogs\" array"),
        }
    }
}

impl core::error::Error for IndexError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Fetch(source) => Some(source),
            Self::InsecureUrl(_)
            | Self::Malformed(_)
            | Self::NotAnObject
            | Self::UnsupportedVersion { .. }
            | Self::MissingDogsArray => None,
        }
    }
}

impl From<FetchError> for IndexError {
    fn from(source: FetchError) -> Self {
        Self::Fetch(source)
    }
}

/// Where to read the index from: `SHEP_DOG_INDEX` when it is set,
/// [`DEFAULT_INDEX_URL`] when it is not.
///
/// Set but empty comes back as the empty string rather than falling back,
/// so a script whose variable failed to expand gets a refusal.
pub fn index_url() -> String {
    std::env::var(INDEX_URL_ENV).unwrap_or_else(|_err| DEFAULT_INDEX_URL.to_owned())
}

/// Fetches the index document at `url`, unparsed.
///
/// The `https://` policy lives here rather than in [`crate::fetch::get`],
/// which speaks either scheme so a test can bind a plain-HTTP port. The
/// refusal happens before a packet is sent. Callers go through
/// [`cached_index`], which parses what this returns.
///
/// # Errors
/// - [`IndexError::InsecureUrl`] if `url` is `http://` and not loopback.
/// - [`IndexError::Fetch`] if the request failed; see [`crate::fetch`].
async fn fetch_index_document(url: &str) -> Result<Vec<u8>, IndexError> {
    let target = fetch::parse_url(url)?;
    require_secure_url(url, &target)?;
    Ok(fetch::get(&target, SIZE_LIMIT, TIMEOUT).await?)
}

/// Refuses a plaintext index URL, unless it points at this machine.
///
/// HTTPS only, with a carve-out for a loopback literal: `SHEP_DOG_INDEX` is
/// an override for testing and self-hosting, and there is no wire between
/// two processes on one host. Exact equality against [`LOOPBACK_HOSTS`], so
/// `http://127.0.0.1.example.com/` cannot talk its way through a prefix or
/// suffix match. The sibling case, `http://evil.com@127.0.0.1/`, no longer
/// reaches here at all: [`fetch::parse_url`] runs first and refuses a
/// `user@` authority outright.
///
/// # Errors
/// - [`IndexError::InsecureUrl`] if `target` is `http://` and its host is
///   not one of [`LOOPBACK_HOSTS`].
fn require_secure_url(url: &str, target: &fetch::Target) -> Result<(), IndexError> {
    if target.https || LOOPBACK_HOSTS.contains(&target.host.as_str()) {
        Ok(())
    } else {
        Err(IndexError::InsecureUrl(url.to_owned()))
    }
}

/// Whether `url` is one this module will print as a link an operator might
/// copy.
fn is_https(url: &str) -> bool {
    url.starts_with("https://")
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    use super::*;
    use crate::dog_index::parse::tests::{
        CARGO_SOURCE_KEYS, SOURCE_KINDS, one_entry_with_description,
    };

    /// `web/`, three directories above this file, only when it actually
    /// exists.
    ///
    /// It is absent once this crate is extracted on its own, so the drift
    /// guards below skip rather than fail there.
    fn workspace_web_dir() -> Option<PathBuf> {
        let dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../../web"));
        dir.is_dir().then(|| dir.to_path_buf())
    }

    /// Reads `web/{relative}`, or `None` outside the workspace checkout
    /// (see [`workspace_web_dir`]).
    ///
    /// # Panics
    /// Inside the workspace, if `relative` cannot be read: that is real
    /// drift the guard exists to catch.
    #[track_caller]
    fn read_workspace_web_file(relative: &str) -> Option<String> {
        let dir = workspace_web_dir()?;
        Some(
            std::fs::read_to_string(dir.join(relative)).unwrap_or_else(|err| {
                panic!("web/{relative} exists in the workspace but could not be read: {err}")
            }),
        )
    }

    /// The string literals of the array `declaration` names in `dogs.ts`.
    ///
    /// Stops at the array's closing `]` rather than at `];`, since a
    /// declaration may close with `] as const;`. Splits past the `=` first.
    ///
    /// # Panics
    /// If `declaration` is absent, has no initialiser, or never closes.
    #[track_caller]
    fn docs_site_array<'a>(dogs_ts: &'a str, declaration: &str) -> Vec<&'a str> {
        let after = dogs_ts
            .split_once(declaration)
            .unwrap_or_else(|| panic!("web/src/data/dogs.ts declares {declaration}"))
            .1
            .split_once('=')
            .unwrap_or_else(|| panic!("the {declaration} declaration has an initialiser"))
            .1;
        let literal = after
            .split_once(']')
            .unwrap_or_else(|| panic!("the {declaration} array is closed"))
            .0;
        literal.split('"').skip(1).step_by(2).collect()
    }

    /// Serves `body` once as a 200 on an ephemeral loopback port, and
    /// returns the URL to read it from.
    async fn serve_index(body: String) -> String {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let response = format!(
            "HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n{body}",
            body.len()
        );
        tokio::spawn(async move {
            let (mut stream, _peer) = listener.accept().await.unwrap();
            // Drains the request so the client's write never stalls on a
            // full socket buffer.
            let mut buf = [0u8; 1024];
            let _ = stream.read(&mut buf).await;
            let _ = stream.write_all(response.as_bytes()).await;
            let _ = stream.shutdown().await;
        });
        format!("http://127.0.0.1:{}/dogs.json", addr.port())
    }

    #[test]
    fn the_unknown_category_fallback_is_itself_a_known_category() {
        assert!(
            CATEGORIES.contains(&UNKNOWN_CATEGORY_FALLBACK),
            "the fallback must be a category the index groups under"
        );
    }

    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_source_kinds_match_the_docs_site_list() {
        let Some(dogs_ts) = read_workspace_web_file("src/data/dogs.ts") else {
            return;
        };

        let site = docs_site_array(&dogs_ts, "SOURCE_KINDS");

        let ours: Vec<&str> = SOURCE_KINDS.iter().map(|(kind, _)| *kind).collect();
        assert_eq!(
            site, ours,
            "web/src/data/dogs.ts and dog_index.rs disagree about the source kinds"
        );
    }

    /// Two independent lists in two languages, and nothing but
    /// this test holds them equal. Only the runtime array is read: the
    /// `DogCategory` union above it is what the array is typed against, so
    /// TypeScript fails the site's own build if those two disagree.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_categories_match_the_docs_site_list() {
        let Some(dogs_ts) = read_workspace_web_file("src/data/dogs.ts") else {
            return;
        };

        let site = docs_site_array(&dogs_ts, "CATEGORIES");

        assert_eq!(
            site,
            CATEGORIES.to_vec(),
            "web/src/data/dogs.ts and dog_index.rs disagree about the categories"
        );
    }

    /// The checked-in editor schema (`web/public/dogs.schema.json`)
    /// against both lists this file enforces.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_schema_agrees_with_the_categories_and_source_kinds() {
        let Some(schema) = read_workspace_web_file("public/dogs.schema.json") else {
            return;
        };
        let schema: Value = serde_json::from_str(&schema).expect("dogs.schema.json is valid json");
        let entry_schema = &schema["properties"]["dogs"]["items"];

        let schema_categories: Vec<&str> = entry_schema["properties"]["category"]["enum"]
            .as_array()
            .expect("category.enum is an array")
            .iter()
            .map(|v| v.as_str().expect("each category is a string"))
            .collect();
        assert_eq!(
            schema_categories,
            CATEGORIES.to_vec(),
            "dogs.schema.json and dog_index.rs disagree about the categories"
        );

        let schema_kinds: Vec<&str> = entry_schema["properties"]["source"]["oneOf"]
            .as_array()
            .expect("source.oneOf is an array")
            .iter()
            .map(|variant| {
                variant["properties"]["kind"]["const"]
                    .as_str()
                    .expect("each source variant names a const kind")
            })
            .collect();
        let ours: Vec<&str> = SOURCE_KINDS.iter().map(|(kind, _)| *kind).collect();
        assert_eq!(
            schema_kinds, ours,
            "dogs.schema.json and dog_index.rs disagree about the source kinds"
        );
    }

    /// The published schema's `cargo` variant: the same keys
    /// [`parse::validate_source`] reads off one, and a `permissions` item
    /// shaped the way [`parse::validate_permissions`] requires.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_schema_declares_the_cargo_source_keys() {
        let Some(schema) = read_workspace_web_file("public/dogs.schema.json") else {
            return;
        };
        let schema: Value = serde_json::from_str(&schema).expect("dogs.schema.json is valid json");
        let cargo = &schema["properties"]["dogs"]["items"]["properties"]["source"]["oneOf"][0];

        let keys: Vec<&str> = cargo["properties"]
            .as_object()
            .expect("the cargo variant has properties")
            .keys()
            .map(String::as_str)
            .collect();
        assert_eq!(
            keys,
            CARGO_SOURCE_KEYS.to_vec(),
            "dogs.schema.json and validate_source disagree about a cargo row's keys"
        );

        let permissions = &cargo["properties"]["permissions"];
        assert_eq!(permissions["type"], "array", "{permissions}");
        let item = &permissions["items"];
        assert_eq!(item["type"], "object", "{item}");
        assert_eq!(item["additionalProperties"], false, "{item}");
        let item_keys: Vec<&str> = item["required"]
            .as_array()
            .expect("a permission item requires its keys")
            .iter()
            .map(|v| v.as_str().expect("each key is a string"))
            .collect();
        assert_eq!(item_keys, ["name", "required"], "{item}");
        assert_eq!(item["properties"]["name"]["type"], "string", "{item}");
        assert_eq!(item["properties"]["required"]["type"], "boolean", "{item}");
    }

    /// The published schema's `needs`: optional, a list of unique words.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_schema_describes_needs_and_does_not_require_them() {
        let Some(schema) = read_workspace_web_file("public/dogs.schema.json") else {
            return;
        };
        let schema: Value = serde_json::from_str(&schema).expect("dogs.schema.json is valid json");
        let entry_schema = &schema["properties"]["dogs"]["items"];

        let needs = &entry_schema["properties"]["needs"];
        assert_eq!(needs["type"], "array", "{needs}");
        assert_eq!(needs["uniqueItems"], true, "{needs}");
        assert_eq!(needs["items"]["type"], "string", "{needs}");
        assert_eq!(needs["items"]["pattern"], "^[a-z][a-z0-9-]*$", "{needs}");

        let required = entry_schema["required"]
            .as_array()
            .expect("required is a list");
        assert!(!required.iter().any(|key| key == "needs"), "{required:?}");
    }

    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_live_index_maps_log_rotate_to_logs_and_discord_to_alerts() {
        let Some(live) = read_workspace_web_file("public/dogs.json") else {
            return;
        };
        let index = parse_index(live.as_bytes()).expect("the live index parses");
        let needs_of = |adopt_as: &str| {
            index
                .dogs
                .iter()
                .find(|dog| dog.adopt_as == adopt_as)
                .unwrap_or_else(|| panic!("the live index lists {adopt_as}"))
                .needs
                .clone()
        };

        assert_eq!(needs_of("log-rotate"), [Need::Logs]);
        assert_eq!(needs_of("discord"), [Need::Alerts]);
        assert_eq!(needs_of("deploy"), []);
    }

    /// The published schema's top-level `shep`: optional, a semver string.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_schema_declares_the_top_level_shep() {
        let Some(schema) = read_workspace_web_file("public/dogs.schema.json") else {
            return;
        };
        let schema: Value = serde_json::from_str(&schema).expect("dogs.schema.json is valid json");

        let shep = &schema["properties"]["shep"];
        assert_eq!(shep["type"], "string", "{shep}");
        assert!(shep["pattern"].is_string(), "{shep}");

        let required = schema["required"].as_array().expect("required is a list");
        assert!(!required.iter().any(|key| key == "shep"), "{required:?}");
    }

    /// The release pull request writes the workspace version into the
    /// index, so a checkout names the version it builds.
    ///
    /// Skips outside the workspace checkout, see
    /// [`read_workspace_web_file`].
    #[test]
    fn the_live_index_names_the_version_this_build_is() {
        let Some(live) = read_workspace_web_file("public/dogs.json") else {
            return;
        };
        let index = parse_index(live.as_bytes()).expect("the live index parses");

        assert_eq!(
            index.shep,
            Some(env!("CARGO_PKG_VERSION").parse().expect("a semver")),
        );
    }

    /// It is the one URL an operator never types, so nothing else would
    /// notice.
    #[test]
    fn the_default_index_url_is_https() {
        assert!(
            DEFAULT_INDEX_URL.starts_with("https://"),
            "{DEFAULT_INDEX_URL}"
        );
    }

    /// The refusal lands before any connection is attempted, so this can
    /// name a host it never reaches.
    #[tokio::test]
    async fn a_plain_http_index_url_is_refused_before_it_connects() {
        let err = fetch_index_document("http://example.com/dogs.json")
            .await
            .expect_err("refused");
        let IndexError::InsecureUrl(url) = err else {
            panic!("wrong variant: {err:?}")
        };
        assert_eq!(url, "http://example.com/dogs.json");
    }

    #[tokio::test]
    async fn a_host_that_merely_contains_a_loopback_literal_is_still_refused() {
        for url in [
            "http://127.0.0.1.example.com/dogs.json",
            "http://localhost.example.com/dogs.json",
        ] {
            assert!(
                matches!(
                    fetch_index_document(url).await,
                    Err(IndexError::InsecureUrl(_))
                ),
                "{url} was not refused"
            );
        }
    }

    /// The third URL that used to sit in the loop above. It is still
    /// refused, but a step earlier and by a different rule:
    /// [`fetch::parse_url`] refuses a `user@` authority before
    /// [`require_secure_url`] ever sees a host to compare.
    #[tokio::test]
    async fn a_loopback_literal_behind_a_userinfo_prefix_is_refused_at_parse_time() {
        let err = fetch_index_document("http://evil.com@127.0.0.1/dogs.json")
            .await
            .expect_err("refused");
        assert!(
            matches!(err, IndexError::Fetch(FetchError::Url(_))),
            "wrong variant: {err:?}"
        );
    }

    /// The await's forcing mechanism is `fetch_index_document`'s own ten second
    /// budget: a server that never answers fails this test rather than
    /// hanging it.
    #[tokio::test]
    async fn a_loopback_http_index_is_read_because_that_is_how_a_local_one_is_served() {
        let url = serve_index(one_entry_with_description("clean\u{1b}[2Jhere")).await;
        let bytes = fetch_index_document(&url).await.expect("read");
        let index = parse_index(&bytes).expect("parses");
        assert_eq!(index.dogs.len(), 1);
        assert_eq!(index.sanitised, 1);
        assert!(!index.dogs[0].description.contains('\u{1b}'));
    }
}