astrs-cli 0.1.0

The astrs command line: run, build, monitor, migrate and bridge robotic dataflows.
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
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
//! `astrs param get/set/list/delete` (blueprint §17's Graph-ops row).
//!
//! ```text
//!   astrs param set <ref> <key> 30     ─► SetParam{scope,key,Integer(30)} ─► Ok
//!   astrs param get <ref> <key>        ─► GetParam{scope,key,inherited}   ─► ParamValue
//!   astrs param list <ref> [--node n]  ─► GetParams{scope,prefix}         ─► ParamList
//!   astrs param delete <ref> <key>     ─► DeleteParam{scope,key}          ─► Ok
//! ```
//!
//! # Three scopes, one positional word
//!
//! [`astrs_wire::ParamScope`] has three levels — global, one dataflow, one
//! node — and the CLI spells all three with the argument a user already
//! types: the dataflow reference, plus an optional `--node`. The literal
//! word `global` in the dataflow position selects
//! [`astrs_wire::ParamScope::Global`], which is how a cluster-wide default
//! is written without inventing a flag for it.
//!
//! # Values are plain JSON, not the wire's tagged form
//!
//! [`astrs_wire::Parameter`] serializes as an externally tagged enum
//! (`{"integer": 30}`), which is the right thing on the wire and the wrong
//! thing to make a human type. `astrs param set flow camera.fps 30` takes
//! plain JSON — `30`, `1.5`, `true`, `"left"`, `[1,2,3]` — and infers the
//! variant, exactly as a user expects. The tagged spelling is still
//! accepted for the one case inference cannot serve: pinning a whole number
//! as a float (`'{"float": 30}'`).

use std::io::Write;
use std::time::Duration;

use astrs_wire::{ControlReply, ControlRequest, NodeId, ParamKey, ParamScope, Parameter};

use crate::command::client::{Client, DataflowRef, Endpoint, reply_name, runtime};
use crate::command::signals::Signals;
use crate::error::CliError;

/// The word that selects [`ParamScope::Global`] in the dataflow position.
pub const GLOBAL_SCOPE_WORD: &str = "global";

/// Where a parameter lives, before the dataflow reference is resolved.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeRef {
    /// Cluster-wide defaults.
    Global,
    /// One dataflow's parameters.
    Dataflow(DataflowRef),
    /// One node's parameters.
    Node(DataflowRef, NodeId),
}

impl ScopeRef {
    /// Classifies the `<dataflow> [--node <id>]` pair a user typed.
    ///
    /// # Errors
    ///
    /// [`CliError::BadArgument`] if `--node` is not a usable node id, or if
    /// a node scope was asked for inside the global scope (there is no such
    /// thing: a node belongs to a dataflow).
    pub fn parse(dataflow: &str, node: Option<&str>) -> Result<Self, CliError> {
        let node = match node {
            Some(text) => Some(NodeId::new(text).map_err(|error| CliError::BadArgument {
                flag: "node",
                value: text.to_owned(),
                reason: error.to_string(),
            })?),
            None => None,
        };
        if dataflow.trim().eq_ignore_ascii_case(GLOBAL_SCOPE_WORD) {
            return match node {
                None => Ok(Self::Global),
                Some(node) => Err(CliError::BadArgument {
                    flag: "node",
                    value: node.as_str().to_owned(),
                    reason: "the global scope has no nodes; name a dataflow instead".to_owned(),
                }),
            };
        }
        let reference = DataflowRef::parse(dataflow);
        Ok(match node {
            Some(node) => Self::Node(reference, node),
            None => Self::Dataflow(reference),
        })
    }

    /// Resolves this scope against a connected coordinator.
    ///
    /// # Errors
    ///
    /// As [`Client::resolve`].
    pub async fn resolve(&self, client: &mut Client) -> Result<ParamScope, CliError> {
        Ok(match self {
            Self::Global => ParamScope::Global,
            Self::Dataflow(reference) => ParamScope::Dataflow {
                dataflow: client.resolve(reference).await?,
            },
            Self::Node(reference, node) => ParamScope::Node {
                dataflow: client.resolve(reference).await?,
                node: node.clone(),
            },
        })
    }
}

/// A human name for a resolved scope, for output.
#[must_use]
pub fn scope_name(scope: &ParamScope) -> String {
    match scope {
        ParamScope::Global => GLOBAL_SCOPE_WORD.to_owned(),
        ParamScope::Dataflow { dataflow } => dataflow.to_string(),
        ParamScope::Node { dataflow, node } => format!("{dataflow}/{}", node.as_str()),
        // `ParamScope` is `#[non_exhaustive]`: a scope added at the tail
        // still has to be nameable.
        _ => "an unknown scope".to_owned(),
    }
}

/// Arguments shared by every parameter verb.
#[derive(Debug, Clone)]
pub struct ScopeArgs {
    /// The dataflow (or the word `global`).
    pub dataflow: String,
    /// The node, for a node-scoped parameter.
    pub node: Option<String>,
    /// Emit JSON rather than a human line.
    pub json: bool,
}

/// Reads one parameter.
///
/// Inherited by default: a node that does not set `camera.fps` should see
/// its dataflow's value, and a dataflow that does not set it should see the
/// cluster's — which is the whole point of having three scopes. `--exact`
/// turns the fallback off for the one caller that must know *where* a value
/// lives rather than what it is.
///
/// # Errors
///
/// - [`CliError::BadArgument`] if the key or `--node` is unusable.
/// - [`CliError::UnknownDataflow`] if the reference resolves to nothing.
/// - As [`Client::request`] otherwise.
pub fn get(
    out: &mut dyn Write,
    endpoint: &Endpoint,
    args: &ScopeArgs,
    key: &str,
    inherited: bool,
) -> Result<Option<Parameter>, CliError> {
    let scope_ref = ScopeRef::parse(&args.dataflow, args.node.as_deref())?;
    let key = parse_key(key)?;
    let runtime = runtime()?;
    let (value, scope) = runtime.block_on(async {
        let mut client = Client::connect(endpoint).await?;
        let scope = scope_ref.resolve(&mut client).await?;
        match client
            .request(
                "param get",
                &ControlRequest::GetParam {
                    scope: scope.clone(),
                    key: key.clone(),
                    inherited,
                },
            )
            .await?
        {
            ControlReply::ParamValue { value, scope, .. } => Ok((value, scope)),
            other => Err(CliError::UnexpectedReply {
                request: "param get",
                reply: reply_name(&other),
            }),
        }
    })?;

    if args.json {
        emit_json(
            out,
            &serde_json::json!({
                "key": key.as_str(),
                "scope": scope_name(&scope),
                "found": value.is_some(),
                "value": value.as_ref().map(to_plain_json),
                "type": value.as_ref().map(|value| value.type_name()),
            }),
        );
    } else {
        match &value {
            Some(value) => {
                let _ = writeln!(out, "{value}");
            }
            None => {
                let _ = writeln!(out, "{} is not set in {}", key.as_str(), scope_name(&scope));
            }
        }
        let _ = out.flush();
    }
    Ok(value)
}

/// How often `watch` polls [`ControlRequest::GetParam`].
///
/// There is no `ParamSubscribe` verb (§7.3's frozen family has `Log`/`Topic`
/// subscriptions, not a third one for parameters) — see this crate's final
/// report — so `--watch` polls rather than opens a push subscription like
/// `astrs logs -f`/`astrs topic echo` do. Every transition is still seen;
/// what polling gives up is *when within a poll interval* it was written,
/// and a revision that lands and is immediately overwritten between two
/// polls, neither of which this flag's own `--help` overstates.
pub const WATCH_POLL_INTERVAL: Duration = Duration::from_millis(500);

/// Watches one parameter, printing every value it transitions to.
///
/// Prints the value already set (if any) once at the start, then again
/// only when a poll observes a different value than the last one printed
/// — two consecutive polls seeing the same value print nothing, which is
/// what makes this a *change* feed rather than a heartbeat. Runs until
/// `count` distinct values have been observed (the initial read counts as
/// one), or, when `count` is `None`, until interrupted (`Ctrl-C`/`SIGTERM`).
///
/// `count` is what makes this function callable from a test: unbounded,
/// the only way to end it is a signal, which a deadline-polled assertion
/// cannot wait on without racing the harness's own signal handling. A
/// caller that wants the production "stream forever" behaviour passes
/// `None`, exactly as `--watch` does without `--count`.
///
/// # Errors
///
/// As [`get`] for the first read; a transport failure on a later poll ends
/// the watch with that error rather than retrying forever, so a coordinator
/// that goes away is visible rather than silently watched forever.
pub fn watch(
    out: &mut dyn Write,
    endpoint: &Endpoint,
    args: &ScopeArgs,
    key: &str,
    inherited: bool,
    count: Option<u32>,
) -> Result<Vec<Option<Parameter>>, CliError> {
    let scope_ref = ScopeRef::parse(&args.dataflow, args.node.as_deref())?;
    let key = parse_key(key)?;
    let runtime = runtime()?;
    runtime.block_on(async move {
        let mut client = Client::connect(endpoint).await?;
        let scope = scope_ref.resolve(&mut client).await?;
        let mut last: Option<Option<Parameter>> = None;
        let mut seen = Vec::new();
        let mut signals = Signals::install();
        loop {
            let value = match client
                .request(
                    "param watch",
                    &ControlRequest::GetParam {
                        scope: scope.clone(),
                        key: key.clone(),
                        inherited,
                    },
                )
                .await?
            {
                ControlReply::ParamValue { value, .. } => value,
                other => {
                    return Err(CliError::UnexpectedReply {
                        request: "param watch",
                        reply: reply_name(&other),
                    });
                }
            };
            if last.as_ref() != Some(&value) {
                print_watch_line(out, &key, &scope, &value, args.json);
                seen.push(value.clone());
                last = Some(value);
                if count.is_some_and(|limit| seen.len() >= limit as usize) {
                    break;
                }
            }
            tokio::select! {
                () = tokio::time::sleep(WATCH_POLL_INTERVAL) => {}
                () = signals.next() => break,
            }
        }
        Ok(seen)
    })
}

/// Prints one `--watch` transition.
fn print_watch_line(
    out: &mut dyn Write,
    key: &ParamKey,
    scope: &ParamScope,
    value: &Option<Parameter>,
    json: bool,
) {
    if json {
        let _ = writeln!(
            out,
            "{}",
            serde_json::json!({
                "key": key.as_str(),
                "scope": scope_name(scope),
                "value": value.as_ref().map(to_plain_json),
                "type": value.as_ref().map(Parameter::type_name),
            })
        );
    } else {
        match value {
            Some(value) => {
                let _ = writeln!(out, "{} = {value} ({})", key.as_str(), value.type_name());
            }
            None => {
                let _ = writeln!(out, "{} is unset", key.as_str());
            }
        }
    }
    let _ = out.flush();
}

/// Writes one parameter.
///
/// # Errors
///
/// - [`CliError::BadArgument`] if the key, `--node` or the value is
///   unusable.
/// - [`CliError::Refused`] if `--create-only` was given and the key exists.
/// - As [`Client::request`] otherwise.
pub fn set(
    out: &mut dyn Write,
    endpoint: &Endpoint,
    args: &ScopeArgs,
    key: &str,
    value: &str,
    create_only: bool,
) -> Result<Parameter, CliError> {
    let scope_ref = ScopeRef::parse(&args.dataflow, args.node.as_deref())?;
    let key = parse_key(key)?;
    let parameter = parse_value(value)?;
    let runtime = runtime()?;
    let scope = runtime.block_on(async {
        let mut client = Client::connect(endpoint).await?;
        let scope = scope_ref.resolve(&mut client).await?;
        client
            .request_ok(
                "param set",
                &ControlRequest::SetParam {
                    scope: scope.clone(),
                    key: key.clone(),
                    value: parameter.clone(),
                    create_only,
                },
            )
            .await?;
        Ok::<ParamScope, CliError>(scope)
    })?;

    if args.json {
        emit_json(
            out,
            &serde_json::json!({
                "key": key.as_str(),
                "scope": scope_name(&scope),
                "value": to_plain_json(&parameter),
                "type": parameter.type_name(),
            }),
        );
    } else {
        let _ = writeln!(
            out,
            "{} = {parameter} ({}) in {}",
            key.as_str(),
            parameter.type_name(),
            scope_name(&scope)
        );
        let _ = out.flush();
    }
    Ok(parameter)
}

/// Lists a scope's parameters.
///
/// # Errors
///
/// As [`get`], plus [`CliError::UnexpectedReply`] for a non-`ParamList`
/// answer.
pub fn list(
    out: &mut dyn Write,
    endpoint: &Endpoint,
    args: &ScopeArgs,
    prefix: Option<&str>,
    inherited: bool,
) -> Result<Vec<(ParamKey, Parameter)>, CliError> {
    let scope_ref = ScopeRef::parse(&args.dataflow, args.node.as_deref())?;
    let runtime = runtime()?;
    let (params, scope) = runtime.block_on(async {
        let mut client = Client::connect(endpoint).await?;
        let scope = scope_ref.resolve(&mut client).await?;
        match client
            .request(
                "param list",
                &ControlRequest::GetParams {
                    scope: scope.clone(),
                    prefix: prefix.map(ToOwned::to_owned),
                    inherited,
                },
            )
            .await?
        {
            ControlReply::ParamList { params, scope } => Ok((params, scope)),
            other => Err(CliError::UnexpectedReply {
                request: "param list",
                reply: reply_name(&other),
            }),
        }
    })?;

    if args.json {
        emit_json(
            out,
            &serde_json::json!({
                "scope": scope_name(&scope),
                "count": params.len(),
                "params": params
                    .iter()
                    .map(|(key, value)| serde_json::json!({
                        "key": key.as_str(),
                        "value": to_plain_json(value),
                        "type": value.type_name(),
                    }))
                    .collect::<Vec<_>>(),
            }),
        );
    } else {
        if params.is_empty() {
            let _ = writeln!(out, "no parameters in {}", scope_name(&scope));
        }
        for (key, value) in &params {
            let _ = writeln!(out, "{:<32} {value}", key.as_str());
        }
        let _ = out.flush();
    }
    Ok(params)
}

/// Deletes one parameter.
///
/// # Errors
///
/// As [`get`].
pub fn delete(
    out: &mut dyn Write,
    endpoint: &Endpoint,
    args: &ScopeArgs,
    key: &str,
) -> Result<(), CliError> {
    let scope_ref = ScopeRef::parse(&args.dataflow, args.node.as_deref())?;
    let key = parse_key(key)?;
    let runtime = runtime()?;
    let scope = runtime.block_on(async {
        let mut client = Client::connect(endpoint).await?;
        let scope = scope_ref.resolve(&mut client).await?;
        client
            .request_ok(
                "param delete",
                &ControlRequest::DeleteParam {
                    scope: scope.clone(),
                    key: key.clone(),
                },
            )
            .await?;
        Ok::<ParamScope, CliError>(scope)
    })?;

    if args.json {
        emit_json(
            out,
            &serde_json::json!({
                "key": key.as_str(),
                "scope": scope_name(&scope),
                "deleted": true,
            }),
        );
    } else {
        let _ = writeln!(out, "deleted {} from {}", key.as_str(), scope_name(&scope));
        let _ = out.flush();
    }
    Ok(())
}

/// Validates a parameter key.
///
/// # Errors
///
/// [`CliError::BadArgument`] naming what a key may contain.
fn parse_key(key: &str) -> Result<ParamKey, CliError> {
    ParamKey::new(key).map_err(|error| CliError::BadArgument {
        flag: "key",
        value: key.to_owned(),
        reason: error.to_string(),
    })
}

/// Turns the JSON a user typed into a [`Parameter`].
///
/// # Errors
///
/// [`CliError::BadArgument`] when the text is not JSON at all, is a shape
/// the protocol has no variant for (`null`, a nested object), or is a
/// mixed-type array.
pub fn parse_value(text: &str) -> Result<Parameter, CliError> {
    let refuse = |reason: String| CliError::BadArgument {
        flag: "value",
        value: text.to_owned(),
        reason,
    };
    let value: serde_json::Value = serde_json::from_str(text.trim()).map_err(|error| {
        refuse(format!(
            "expected JSON (a number, `true`, a quoted string, or an array): {error}"
        ))
    })?;
    from_plain_json(&value).ok_or_else(|| {
        // The tagged spelling is the escape hatch for the one thing
        // inference cannot do: `{"float": 30}` for a whole number that must
        // stay a float.
        if value.is_object()
            && let Ok(parameter) = serde_json::from_value::<Parameter>(value.clone())
        {
            return CliError::BadArgument {
                flag: "value",
                value: text.to_owned(),
                reason: format!("(unreachable: {parameter} decoded)"),
            };
        }
        refuse(
            "no parameter type matches that value: use a number, a boolean, a string, a \
             same-typed array, or the tagged form like `{\"float\": 30}`"
                .to_owned(),
        )
    })
}

/// The inference [`parse_value`] documents, without the error wrapping.
fn from_plain_json(value: &serde_json::Value) -> Option<Parameter> {
    match value {
        serde_json::Value::Bool(flag) => Some(Parameter::Bool(*flag)),
        serde_json::Value::Number(number) => number.as_i64().map_or_else(
            || number.as_f64().map(Parameter::Float),
            |integer| Some(Parameter::Integer(integer)),
        ),
        serde_json::Value::String(text) => Some(Parameter::String(text.clone())),
        serde_json::Value::Array(items) => from_array(items),
        // An object may still be the tagged wire form.
        serde_json::Value::Object(_) => serde_json::from_value::<Parameter>(value.clone()).ok(),
        serde_json::Value::Null => None,
    }
}

/// A JSON array as one of the three list parameters, or `None` when its
/// elements do not share a type the protocol has a list for.
///
/// An *empty* array is a `list_string`: some variant has to be chosen, and
/// a list of strings is the one that can later hold anything a user meant.
fn from_array(items: &[serde_json::Value]) -> Option<Parameter> {
    if items.is_empty() {
        return Some(Parameter::ListString(Vec::new()));
    }
    if items.iter().all(|item| item.is_i64()) {
        return Some(Parameter::ListInt(
            items.iter().filter_map(serde_json::Value::as_i64).collect(),
        ));
    }
    if items.iter().all(serde_json::Value::is_number) {
        return Some(Parameter::ListFloat(
            items.iter().filter_map(serde_json::Value::as_f64).collect(),
        ));
    }
    if items.iter().all(serde_json::Value::is_string) {
        return Some(Parameter::ListString(
            items
                .iter()
                .filter_map(|item| item.as_str().map(ToOwned::to_owned))
                .collect(),
        ));
    }
    None
}

/// A [`Parameter`] as the plain JSON [`parse_value`] accepts, so
/// `astrs param get --json | astrs param set` round-trips.
#[must_use]
pub fn to_plain_json(value: &Parameter) -> serde_json::Value {
    match value {
        Parameter::Bool(flag) => serde_json::json!(flag),
        Parameter::Integer(integer) => serde_json::json!(integer),
        Parameter::Float(float) => serde_json::json!(float),
        Parameter::String(text) => serde_json::json!(text),
        Parameter::ListInt(items) => serde_json::json!(items),
        Parameter::ListFloat(items) => serde_json::json!(items),
        Parameter::ListString(items) => serde_json::json!(items),
        Parameter::Timestamp(stamp) => serde_json::json!(stamp.to_string()),
        // A tail-appended variant (§3) has no plain spelling yet; the
        // tagged form is always correct, so fall back to it rather than
        // guessing.
        other => serde_json::to_value(other).unwrap_or(serde_json::Value::Null),
    }
}

/// Writes one JSON object.
fn emit_json(out: &mut dyn Write, value: &serde_json::Value) {
    let _ = writeln!(
        out,
        "{}",
        serde_json::to_string_pretty(value).unwrap_or_else(|_| "{}".to_owned())
    );
    let _ = out.flush();
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use super::*;
    use astrs_wire::{AuthToken, DataflowId};

    fn endpoint() -> Endpoint {
        Endpoint::new(
            std::net::SocketAddr::from(([127, 0, 0, 1], 1)),
            AuthToken::ZERO,
        )
    }

    fn args(dataflow: &str, node: Option<&str>) -> ScopeArgs {
        ScopeArgs {
            dataflow: dataflow.to_owned(),
            node: node.map(ToOwned::to_owned),
            json: false,
        }
    }

    #[test]
    fn the_word_global_selects_the_global_scope() {
        assert_eq!(ScopeRef::parse("global", None).unwrap(), ScopeRef::Global);
        assert_eq!(ScopeRef::parse("GLOBAL", None).unwrap(), ScopeRef::Global);
    }

    #[test]
    fn a_node_inside_the_global_scope_is_refused() {
        let error = ScopeRef::parse("global", Some("cam")).unwrap_err();
        match error {
            CliError::BadArgument { flag, reason, .. } => {
                assert_eq!(flag, "node");
                assert!(reason.contains("no nodes"), "{reason}");
            }
            other => panic!("expected BadArgument, got {other}"),
        }
    }

    #[test]
    fn a_dataflow_and_a_node_scope_are_told_apart() {
        assert!(matches!(
            ScopeRef::parse("perception", None).unwrap(),
            ScopeRef::Dataflow(DataflowRef::Name(_))
        ));
        let id = DataflowId::generate();
        assert!(matches!(
            ScopeRef::parse(&id.to_string(), Some("cam")).unwrap(),
            ScopeRef::Node(DataflowRef::Id(_), _)
        ));
    }

    #[test]
    fn an_unusable_node_id_is_refused_before_anything_is_dialled() {
        let error = ScopeRef::parse("flow", Some("not a node")).unwrap_err();
        assert!(matches!(error, CliError::BadArgument { .. }), "{error}");
    }

    #[test]
    fn a_resolved_scope_has_a_readable_name() {
        assert_eq!(scope_name(&ParamScope::Global), "global");
        let id = DataflowId::from_u128(9);
        assert_eq!(
            scope_name(&ParamScope::Dataflow { dataflow: id }),
            id.to_string()
        );
        assert_eq!(
            scope_name(&ParamScope::node(id, NodeId::new("cam").unwrap())),
            format!("{id}/cam")
        );
    }

    #[test]
    fn plain_json_scalars_infer_their_parameter_type() {
        assert_eq!(parse_value("30").unwrap(), Parameter::Integer(30));
        assert_eq!(parse_value("-1").unwrap(), Parameter::Integer(-1));
        assert_eq!(parse_value("1.5").unwrap(), Parameter::Float(1.5));
        assert_eq!(parse_value("true").unwrap(), Parameter::Bool(true));
        assert_eq!(
            parse_value("\"left\"").unwrap(),
            Parameter::String("left".to_owned())
        );
    }

    #[test]
    fn arrays_infer_the_matching_list_type() {
        assert_eq!(
            parse_value("[1,2,3]").unwrap(),
            Parameter::ListInt(vec![1, 2, 3])
        );
        assert_eq!(
            parse_value("[1.5,2]").unwrap(),
            Parameter::ListFloat(vec![1.5, 2.0])
        );
        assert_eq!(
            parse_value("[\"a\",\"b\"]").unwrap(),
            Parameter::ListString(vec!["a".to_owned(), "b".to_owned()])
        );
        assert_eq!(
            parse_value("[]").unwrap(),
            Parameter::ListString(Vec::new())
        );
    }

    #[test]
    fn the_tagged_wire_form_is_accepted_for_the_case_inference_cannot_serve() {
        assert_eq!(
            parse_value("{\"float\": 30}").unwrap(),
            Parameter::Float(30.0)
        );
    }

    #[test]
    fn a_value_with_no_parameter_type_is_refused_with_the_text_that_was_typed() {
        for text in ["null", "{\"nested\": {}}", "[1, \"two\"]", "not json"] {
            let error = parse_value(text).unwrap_err();
            match error {
                CliError::BadArgument { flag, value, .. } => {
                    assert_eq!(flag, "value");
                    assert_eq!(value, text);
                }
                other => panic!("expected BadArgument for `{text}`, got {other}"),
            }
        }
    }

    #[test]
    fn every_parameter_round_trips_through_its_plain_json_form() {
        for parameter in [
            Parameter::Bool(false),
            Parameter::Integer(-7),
            Parameter::Float(0.25),
            Parameter::String("hi".to_owned()),
            Parameter::ListInt(vec![1, 2]),
            Parameter::ListFloat(vec![0.5, 1.5]),
            Parameter::ListString(vec!["a".to_owned()]),
        ] {
            let text = to_plain_json(&parameter).to_string();
            assert_eq!(parse_value(&text).unwrap(), parameter, "{text}");
        }
    }

    #[test]
    fn a_key_must_be_a_usable_parameter_key() {
        assert_eq!(parse_key("camera.fps").unwrap().as_str(), "camera.fps");
        let error = parse_key("not a key").unwrap_err();
        match error {
            CliError::BadArgument { flag, .. } => assert_eq!(flag, "key"),
            other => panic!("expected BadArgument, got {other}"),
        }
    }

    #[test]
    fn every_param_verb_reports_a_dead_cluster_rather_than_hanging() {
        let endpoint = endpoint();
        let args = args("flow", None);
        let get_error = get(&mut Vec::new(), &endpoint, &args, "k", true).unwrap_err();
        assert!(
            matches!(get_error, CliError::NoCluster { .. }),
            "{get_error}"
        );

        let set_error = set(&mut Vec::new(), &endpoint, &args, "k", "1", false).unwrap_err();
        assert!(
            matches!(set_error, CliError::NoCluster { .. }),
            "{set_error}"
        );

        let list_error = list(&mut Vec::new(), &endpoint, &args, None, false).unwrap_err();
        assert!(
            matches!(list_error, CliError::NoCluster { .. }),
            "{list_error}"
        );

        let delete_error = delete(&mut Vec::new(), &endpoint, &args, "k").unwrap_err();
        assert!(
            matches!(delete_error, CliError::NoCluster { .. }),
            "{delete_error}"
        );

        let watch_error = watch(&mut Vec::new(), &endpoint, &args, "k", true, None).unwrap_err();
        assert!(
            matches!(watch_error, CliError::NoCluster { .. }),
            "{watch_error}"
        );
    }

    #[test]
    fn a_bad_value_is_refused_before_the_dial() {
        let error = set(
            &mut Vec::new(),
            &endpoint(),
            &args("flow", None),
            "k",
            "null",
            false,
        )
        .unwrap_err();
        assert!(matches!(error, CliError::BadArgument { .. }), "{error}");
    }
}