summaryrefslogtreecommitdiffstats
path: root/src/config/mod.rs
blob: e62b2b6886d08993fa70b2cdce222056052cb70e (plain)
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
//! Configuration: built-in defaults, then configuration files (`--config`, in order),
//! then command-line arguments. Every option can be set in each.
//!
//! An argument that replaces a value from a file is logged (info), as is a file that
//! replaces one from another file (debug; warning between two includes of one file).
//! Values can be imported instead of written out (`import`): imported values are never
//! logged, only where they come from.
//!
//! All options are described once, in [`OPTIONS`]: the command line, the file keys and
//! the conversion of text values all derive from it. The merged values are then read
//! into [`Settings`] and checked.

mod file;
mod import;

use std::collections::BTreeMap;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};
use clap::parser::ValueSource;
use clap::{Arg, ArgAction, ArgMatches, Command};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use tracing::{debug, info};

use import::{Imported, Raw};

/// How an option's value is written as text (command line, text imports).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Kind {
    /// `true`/`false`; on the command line, present or not.
    Flag,
    Text,
    Integer,
    /// Comma- or line-separated; repeatable on the command line.
    List,
}

#[derive(Debug)]
pub(crate) struct OptionSpec {
    pub(crate) name: &'static str,
    pub(crate) kind: Kind,
    pub(crate) value_name: &'static str,
    /// For `--help` only; the effective defaults are in [`Settings`].
    pub(crate) default: Option<&'static str>,
    pub(crate) help: &'static str,
    pub(crate) hidden: bool,
}

const fn option(
    name: &'static str,
    kind: Kind,
    value_name: &'static str,
    default: Option<&'static str>,
    help: &'static str,
) -> OptionSpec {
    OptionSpec {
        name,
        kind,
        value_name,
        default,
        help,
        hidden: false,
    }
}

/// Every option. Names are the long option names and the configuration file keys.
pub(crate) const OPTIONS: &[OptionSpec] = &[
    option(
        "bind-addr",
        Kind::Text,
        "HOSTNAME:PORT",
        Some("127.0.0.1:3000"),
        "Address of the HTTP API.",
    ),
    option(
        "htpasswd",
        Kind::Text,
        "PATH",
        None,
        "Users of the HTTP API (htpasswd file with Argon2 hashes). Required.",
    ),
    option(
        "debug-api",
        Kind::Flag,
        "BOOL",
        Some("false"),
        "Enable the /debug endpoints (tests only).",
    ),
    option(
        "seeds",
        Kind::List,
        "STRING,[STRING...]",
        None,
        "How to reach a node of the swarm to join: a Mycelium public key, a Mycelium IPv6 address, or a host name with an AAAA record for one. Without seeds, the node starts a new swarm (genesis).",
    ),
    option(
        "swarm-key-file",
        Kind::Text,
        "PATH",
        None,
        "Pre-shared swarm key: only nodes that know it may join (raw bytes, at least 16; e.g. `head -c 32 /dev/urandom`). Without it, anyone who knows the swarm ID can.",
    ),
    option(
        "alias",
        Kind::Text,
        "NAME",
        None,
        "A name for this node, shown next to its public key (/peers, metrics). Published in the swarm's member list; not unique, never an identity.",
    ),
    option(
        "keepalive-secs",
        Kind::Integer,
        "SECONDS",
        Some("60"),
        "Keepalive interval; dormancy, resync and probe timings derive from it.",
    ),
    option(
        "neighbours",
        Kind::Integer,
        "COUNT",
        Some("12"),
        "Neighbours each node keeps (at least 2). Swarms of up to this many + 1 nodes are a full mesh; larger ones sync over several hops.",
    ),
    option(
        "electorate-window-days",
        Kind::Integer,
        "DAYS",
        Some("7"),
        "Members that renewed their alive stamp within this many days vote on remakes (a majority of them must agree). Matters on the node that coordinates a remake.",
    ),
    option(
        "mycelium-rest-url",
        Kind::Text,
        "SCHEME://HOSTNAME:PORT",
        None,
        "Use a Mycelium daemon running elsewhere (its REST API) instead of supervising one. Excludes the other mycelium options except mycelium-peers.",
    ),
    option(
        "mycelium-key-file",
        Kind::Text,
        "PATH",
        None,
        "Private key of the supervised Mycelium daemon (its identity); a new one each start if absent.",
    ),
    option(
        "mycelium-network-key-file",
        Kind::Text,
        "PATH",
        None,
        "Key of a private Mycelium network for the supervised daemon.",
    ),
    option(
        "mycelium-network-name",
        Kind::Text,
        "NAME",
        None,
        "Name of that private Mycelium network.",
    ),
    option(
        "mycelium-tcp-listen-port",
        Kind::Integer,
        "PORT",
        None,
        "TCP port the supervised Mycelium daemon listens on for overlay peers; a free port if absent.",
    ),
    option(
        "mycelium-peers",
        Kind::List,
        "URL,[URL...]",
        Some("tls://127.0.0.1:9651"),
        "Peers of the supervised Mycelium daemon (overlay links such as `tls://host:9651`), not continuum peers.",
    ),
    OptionSpec {
        name: "debug-epoch",
        kind: Kind::Integer,
        value_name: "GENERATION",
        default: None,
        help: "Test hook: start on a non-genesis epoch of this generation (with a fixed dummy id). Needs debug-api.",
        hidden: true,
    },
];

pub(crate) fn spec(name: &str) -> Option<&'static OptionSpec> {
    OPTIONS.iter().find(|spec| spec.name == name)
}

/// The effective configuration.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub(crate) struct Settings {
    #[serde(default = "default_bind_addr")]
    pub(crate) bind_addr: SocketAddr,
    pub(crate) htpasswd: String,
    #[serde(default)]
    pub(crate) debug_api: bool,
    #[serde(default)]
    pub(crate) seeds: Vec<String>,
    pub(crate) swarm_key_file: Option<PathBuf>,
    pub(crate) alias: Option<String>,
    #[serde(default = "default_keepalive_secs")]
    pub(crate) keepalive_secs: u64,
    #[serde(default = "default_neighbours")]
    pub(crate) neighbours: usize,
    #[serde(default = "default_electorate_window_days")]
    pub(crate) electorate_window_days: u64,
    pub(crate) mycelium_rest_url: Option<String>,
    pub(crate) mycelium_key_file: Option<PathBuf>,
    pub(crate) mycelium_network_key_file: Option<PathBuf>,
    pub(crate) mycelium_network_name: Option<String>,
    pub(crate) mycelium_tcp_listen_port: Option<u16>,
    #[serde(default = "default_mycelium_peers")]
    pub(crate) mycelium_peers: Vec<String>,
    pub(crate) debug_epoch: Option<u64>,
}

fn default_bind_addr() -> SocketAddr {
    SocketAddr::from(([127, 0, 0, 1], 3000))
}
fn default_keepalive_secs() -> u64 {
    60
}
fn default_neighbours() -> usize {
    12
}
fn default_electorate_window_days() -> u64 {
    7
}
fn default_mycelium_peers() -> Vec<String> {
    vec!["tls://127.0.0.1:9651".to_owned()]
}

impl Settings {
    /// `mycelium-rest-url`, parsed (checked when loading).
    pub(crate) fn mycelium_rest_url(&self) -> Option<url::Url> {
        self.mycelium_rest_url
            .as_deref()
            .and_then(|url| url::Url::parse(url).ok())
    }

    fn check(&self) -> Result<()> {
        if let Some(url) = &self.mycelium_rest_url {
            url::Url::parse(url).with_context(|| format!("mycelium-rest-url {url:?}"))?;
        }
        if self.mycelium_rest_url.is_some() {
            let supervised_only = [
                ("mycelium-key-file", self.mycelium_key_file.is_some()),
                (
                    "mycelium-network-key-file",
                    self.mycelium_network_key_file.is_some(),
                ),
                (
                    "mycelium-network-name",
                    self.mycelium_network_name.is_some(),
                ),
                (
                    "mycelium-tcp-listen-port",
                    self.mycelium_tcp_listen_port.is_some(),
                ),
            ];
            if let Some((name, _)) = supervised_only.iter().find(|(_, set)| *set) {
                bail!(
                    "{name} is for a supervised Mycelium daemon and can't be combined with mycelium-rest-url"
                );
            }
        }
        if self.debug_epoch.is_some() && !self.debug_api {
            bail!("debug-epoch needs debug-api");
        }
        if self.neighbours < 2 {
            bail!("neighbours must be at least 2");
        }
        if self.keepalive_secs == 0 {
            bail!("keepalive-secs must be at least 1");
        }
        if self.electorate_window_days == 0 {
            bail!("electorate-window-days must be at least 1");
        }
        if let Some(alias) = &self.alias {
            check_alias(alias)?;
        }
        Ok(())
    }
}

/// Up to 64 characters, no control characters, not blank.
fn check_alias(alias: &str) -> Result<()> {
    if alias.trim().is_empty() {
        bail!("alias must not be blank");
    }
    if alias.chars().count() > 64 {
        bail!("alias must be at most 64 characters");
    }
    if alias.chars().any(char::is_control) {
        bail!("alias must not contain control characters");
    }
    Ok(())
}

// --- values and where they came from -------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Provenance {
    File(PathBuf),
    Argument,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Origin {
    pub(crate) provenance: Provenance,
    /// Importers the value (or some list elements) came from, e.g. `env:HOSTNAME`.
    pub(crate) imports: Vec<String>,
}

impl Origin {
    pub(crate) fn is_imported(&self) -> bool {
        !self.imports.is_empty()
    }
}

impl std::fmt::Display for Origin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.provenance {
            Provenance::File(path) => write!(f, "{}", path.display())?,
            Provenance::Argument => write!(f, "the command line")?,
        }
        if self.is_imported() {
            write!(f, " ({})", self.imports.join(", "))?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Entry {
    pub(crate) value: JsonValue,
    pub(crate) origin: Origin,
}

impl Entry {
    /// The value for logs: imported values are never shown.
    pub(crate) fn shown(&self) -> String {
        if self.origin.is_imported() {
            "<imported>".to_owned()
        } else {
            self.value.to_string()
        }
    }
}

/// Option name to value.
pub(crate) type Layer = BTreeMap<String, Entry>;

/// The value of a text, by the option's kind.
fn from_text(kind: Kind, text: &str) -> Result<JsonValue> {
    Ok(match kind {
        Kind::Text => JsonValue::String(text.to_owned()),
        Kind::Integer => {
            let number: u64 = text
                .trim()
                .parse()
                .map_err(|_| anyhow!("{text:?} is not a non-negative integer"))?;
            JsonValue::from(number)
        }
        Kind::Flag => match text.trim() {
            "true" | "yes" | "1" => JsonValue::Bool(true),
            "false" | "no" | "0" => JsonValue::Bool(false),
            _ => bail!("{text:?} is not true or false"),
        },
        Kind::List => JsonValue::Array(
            text.split([',', '\n'])
                .map(str::trim)
                .filter(|item| !item.is_empty())
                .map(|item| JsonValue::String(item.to_owned()))
                .collect(),
        ),
    })
}

fn from_import(kind: Kind, importer: &import::Importer) -> Result<JsonValue> {
    match importer.fetch()? {
        Imported::Text(text) => from_text(kind, &text),
        Imported::Json(value) => Ok(value),
    }
}

/// A value from a file in `dir`: importer tables are resolved, also as list elements.
fn resolve_file_value(
    spec: &OptionSpec,
    value: JsonValue,
    dir: &Path,
) -> Result<(JsonValue, Vec<String>)> {
    match value {
        JsonValue::Object(table) => match import::parse_table(&table, dir)? {
            Some(importer) => Ok((
                from_import(spec.kind, &importer)?,
                vec![importer.describe()],
            )),
            None => bail!("a table here must be an importer (env, file or command)"),
        },
        JsonValue::Array(items) if spec.kind == Kind::List => {
            let mut imports = Vec::new();
            let mut values = Vec::new();
            for item in items {
                match item {
                    JsonValue::Object(table) => {
                        let importer = import::parse_table(&table, dir)?.ok_or_else(|| {
                            anyhow!("a table here must be an importer (env, file or command)")
                        })?;
                        match from_import(Kind::List, &importer)? {
                            JsonValue::Array(more) => values.extend(more),
                            other => values.push(other),
                        }
                        imports.push(importer.describe());
                    }
                    other => values.push(other),
                }
            }
            Ok((JsonValue::Array(values), imports))
        }
        other => Ok((other, Vec::new())),
    }
}

/// The first option whose value alone doesn't fit (serde doesn't name it).
fn culprit(merged: &Layer) -> Option<(&String, &Entry)> {
    merged.iter().find(|(name, entry)| {
        let mut alone = serde_json::Map::new();
        alone.insert("htpasswd".to_owned(), JsonValue::String(String::new()));
        alone.insert((*name).clone(), entry.value.clone());
        serde_json::from_value::<Settings>(JsonValue::Object(alone)).is_err()
    })
}

// --- command line --------------------------------------------------------------

const CONFIG: &str = "config";
const PRINT_CONFIG: &str = "print-config";

fn command() -> Command {
    let mut command = Command::new("continuum")
        .version(env!("CARGO_PKG_VERSION"))
        .about("continuum: a replicated key-value store over Mycelium")
        .after_help(
            "Every option can also be set in a configuration file (--config); arguments win.\n\
             A value can be imported instead of written out: env:NAME, file:PATH or cmd:COMMAND \
             (as text), or env+json:, file+json:, cmd+json: (parsed as JSON). \
             literal:VALUE passes a value that starts with one of these prefixes.",
        )
        .arg(
            Arg::new(CONFIG)
                .long(CONFIG)
                .value_name("PATH")
                .action(ArgAction::Append)
                .help("Configuration file (.toml or .json); repeatable, later files win."),
        )
        .arg(
            Arg::new(PRINT_CONFIG)
                .long(PRINT_CONFIG)
                .action(ArgAction::SetTrue)
                .help("Print the effective configuration as JSON (with each value's source) and exit."),
        );
    for spec in OPTIONS {
        let help = match spec.default {
            Some(default) => format!("{} [default: {default}]", spec.help),
            None => spec.help.to_owned(),
        };
        let arg = Arg::new(spec.name)
            .long(spec.name)
            .help(help)
            .hide(spec.hidden);
        command = command.arg(match spec.kind {
            Kind::Flag => arg.action(ArgAction::SetTrue),
            Kind::List => arg.value_name(spec.value_name).action(ArgAction::Append),
            Kind::Text | Kind::Integer => arg.value_name(spec.value_name).action(ArgAction::Set),
        });
    }
    command
}

/// The values given as arguments.
fn arguments(matches: &ArgMatches) -> Result<Layer> {
    let mut layer = Layer::new();
    for spec in OPTIONS {
        if matches.value_source(spec.name) != Some(ValueSource::CommandLine) {
            continue;
        }
        let mut imports = Vec::new();
        let value = if spec.kind == Kind::Flag {
            JsonValue::Bool(true)
        } else {
            let mut values = Vec::new();
            for raw in matches.get_many::<String>(spec.name).into_iter().flatten() {
                let value = match import::parse_argument(raw) {
                    Raw::Literal(text) => from_text(spec.kind, text),
                    Raw::Import(importer) => {
                        imports.push(importer.describe());
                        from_import(spec.kind, &importer)
                    }
                }
                .with_context(|| format!("--{}", spec.name))?;
                values.push(value);
            }
            if spec.kind == Kind::List {
                JsonValue::Array(
                    values
                        .into_iter()
                        .flat_map(|value| match value {
                            JsonValue::Array(items) => items,
                            other => vec![other],
                        })
                        .collect(),
                )
            } else {
                values.pop().unwrap_or(JsonValue::Null)
            }
        };
        layer.insert(
            spec.name.to_owned(),
            Entry {
                value,
                origin: Origin {
                    provenance: Provenance::Argument,
                    imports,
                },
            },
        );
    }
    Ok(layer)
}

/// The configuration, and where each value set came from (absent: default).
#[derive(Debug, Clone)]
pub(crate) struct Loaded {
    pub(crate) settings: Settings,
    pub(crate) origins: BTreeMap<String, Origin>,
    pub(crate) files: Vec<PathBuf>,
    /// `--print-config`: print [`Loaded::report`] and exit.
    pub(crate) print_only: bool,
}

impl Loaded {
    /// The effective configuration with each value's source, for `--print-config` and
    /// `GET /admin/config`. Imported values are left out (they may be secret); their
    /// importers are listed instead.
    pub(crate) fn report(&self) -> JsonValue {
        let values = match serde_json::to_value(&self.settings) {
            Ok(JsonValue::Object(values)) => values,
            _ => serde_json::Map::new(),
        };
        let options: serde_json::Map<String, JsonValue> = OPTIONS
            .iter()
            .filter(|spec| !spec.hidden || self.origins.contains_key(spec.name))
            .map(|spec| {
                let mut entry = serde_json::Map::new();
                match self.origins.get(spec.name) {
                    None => {
                        entry.insert("source".into(), "default".into());
                    }
                    Some(origin) => {
                        match &origin.provenance {
                            Provenance::File(path) => {
                                entry.insert("source".into(), "file".into());
                                entry.insert("file".into(), path.display().to_string().into());
                            }
                            Provenance::Argument => {
                                entry.insert("source".into(), "argument".into());
                            }
                        }
                        if origin.is_imported() {
                            entry.insert("imported".into(), origin.imports.clone().into());
                        }
                    }
                }
                let imported = self.origins.get(spec.name).is_some_and(Origin::is_imported);
                if !imported {
                    entry.insert(
                        "value".into(),
                        values.get(spec.name).cloned().unwrap_or(JsonValue::Null),
                    );
                }
                (spec.name.to_owned(), JsonValue::Object(entry))
            })
            .collect();
        serde_json::json!({
            "files": self.files.iter().map(|path| path.display().to_string()).collect::<Vec<_>>(),
            "options": options,
        })
    }
}

/// Reads the command line and the configuration files it names. Exits on `--help`,
/// `--version` and command-line syntax errors, like any command.
pub(crate) fn load() -> Result<Loaded> {
    let matches = command().get_matches();
    let print_only = matches.get_flag(PRINT_CONFIG);
    if print_only {
        // Logs go to stdout too; the report says where each value came from anyway.
        let _ = crate::logging::set_filter("off");
    }
    let files: Vec<PathBuf> = matches
        .get_many::<String>(CONFIG)
        .into_iter()
        .flatten()
        .map(PathBuf::from)
        .collect();

    let mut merged = Layer::new();
    for path in &files {
        for (name, entry) in file::load(path)? {
            if let Some(previous) = merged.get(&name) {
                debug!(
                    option = %name,
                    from = %entry.origin,
                    overrides = %previous.origin,
                    "Later configuration file overrides an earlier one"
                );
            }
            merged.insert(name, entry);
        }
    }
    for (name, entry) in arguments(&matches)? {
        if let Some(previous) = merged.get(&name) {
            info!(
                option = %name,
                value = %entry.shown(),
                overrides = %previous.shown(),
                from = %previous.origin,
                "Command-line argument overrides the configuration file"
            );
        }
        merged.insert(name, entry);
    }
    let mut loaded = resolve(merged, files)?;
    loaded.print_only = print_only;
    Ok(loaded)
}

fn resolve(merged: Layer, files: Vec<PathBuf>) -> Result<Loaded> {
    let object: serde_json::Map<String, JsonValue> = merged
        .iter()
        .map(|(name, entry)| (name.clone(), entry.value.clone()))
        .collect();
    let mut settings: Settings =
        serde_json::from_value(JsonValue::Object(object)).map_err(|error| {
            match culprit(&merged) {
                Some((name, entry)) => anyhow!(
                    "invalid configuration: option {name} (set in {}): {error}",
                    entry.origin
                ),
                None => anyhow!("invalid configuration: {error}"),
            }
        })?;
    settings.alias = settings.alias.map(|alias| alias.trim().to_owned());
    settings.check().context("invalid configuration")?;
    Ok(Loaded {
        settings,
        origins: merged
            .into_iter()
            .map(|(name, entry)| (name, entry.origin))
            .collect(),
        files,
        print_only: false,
    })
}

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

    fn layer(values: JsonValue) -> Layer {
        values
            .as_object()
            .unwrap()
            .iter()
            .map(|(name, value)| {
                (
                    name.clone(),
                    Entry {
                        value: value.clone(),
                        origin: Origin {
                            provenance: Provenance::Argument,
                            imports: Vec::new(),
                        },
                    },
                )
            })
            .collect()
    }

    #[test]
    fn options_and_settings_agree() {
        // Every option is a Settings field, and every field an option.
        let loaded = resolve(layer(json!({ "htpasswd": "users" })), Vec::new()).unwrap();
        let JsonValue::Object(fields) = serde_json::to_value(&loaded.settings).unwrap() else {
            panic!()
        };
        let mut names: Vec<&str> = fields.keys().map(String::as_str).collect();
        let mut options: Vec<&str> = OPTIONS.iter().map(|spec| spec.name).collect();
        names.sort_unstable();
        options.sort_unstable();
        assert_eq!(names, options);

        // Help defaults match the real ones.
        for spec in OPTIONS {
            if let Some(default) = spec.default {
                assert_eq!(
                    from_text(spec.kind, default).unwrap(),
                    fields[spec.name],
                    "default of {}",
                    spec.name
                );
            }
        }
        command().debug_assert();
    }

    #[test]
    fn report_hides_imported_values() {
        let mut values = layer(json!({ "htpasswd": "users", "alias": "secret-ish" }));
        values.get_mut("alias").unwrap().origin.imports = vec!["env:HOSTNAME".into()];
        let report = resolve(values, Vec::new()).unwrap().report();
        let options = &report["options"];
        assert_eq!(
            options["htpasswd"],
            json!({ "source": "argument", "value": "users" })
        );
        assert_eq!(
            options["alias"],
            json!({ "source": "argument", "imported": ["env:HOSTNAME"] })
        );
        assert_eq!(
            options["neighbours"],
            json!({ "source": "default", "value": 12 })
        );
        assert!(options.get("debug-epoch").is_none());
    }

    #[test]
    fn text_values() {
        assert_eq!(from_text(Kind::Integer, " 30 ").unwrap(), json!(30));
        assert!(from_text(Kind::Integer, "-1").is_err());
        assert_eq!(
            from_text(Kind::List, "a, b\nc,").unwrap(),
            json!(["a", "b", "c"])
        );
        assert_eq!(from_text(Kind::Flag, "yes").unwrap(), json!(true));
    }

    #[test]
    fn checks() {
        assert!(resolve(layer(json!({})), Vec::new()).is_err()); // htpasswd is required
        assert!(
            resolve(
                layer(json!({ "htpasswd": "u", "neighbours": 1 })),
                Vec::new()
            )
            .is_err()
        );
        assert!(
            resolve(
                layer(json!({ "htpasswd": "u", "debug-epoch": 3 })),
                Vec::new()
            )
            .is_err()
        );
        assert!(
            resolve(
                layer(json!({
                    "htpasswd": "u",
                    "mycelium-rest-url": "http://[::1]:8989",
                    "mycelium-key-file": "k",
                })),
                Vec::new()
            )
            .is_err()
        );
        assert!(resolve(layer(json!({ "htpasswd": "u", "alias": " " })), Vec::new()).is_err());
        assert!(
            resolve(
                layer(json!({ "htpasswd": "u", "keepalive-secs": "x" })),
                Vec::new()
            )
            .is_err()
        );
    }

    #[test]
    fn arguments_with_importers() {
        // SAFETY: tests in this module don't read this variable concurrently.
        unsafe { std::env::set_var("CONTINUUM_TEST_SEEDS", "[\"a\", \"b\"]") };
        let matches = command()
            .try_get_matches_from([
                "continuum",
                "--seeds",
                "env+json:CONTINUUM_TEST_SEEDS",
                "--seeds",
                "c,d",
                "--alias",
                "literal:env:x",
                "--debug-api",
            ])
            .unwrap();
        let layer = arguments(&matches).unwrap();
        assert_eq!(layer["seeds"].value, json!(["a", "b", "c", "d"]));
        assert_eq!(
            layer["seeds"].origin.imports,
            ["env+json:CONTINUUM_TEST_SEEDS"]
        );
        assert_eq!(layer["alias"].value, json!("env:x"));
        assert_eq!(layer["debug-api"].value, json!(true));
        assert!(!layer.contains_key("neighbours"));
    }
}