isothrum-client 3.0.2

The client of the Isothrum runner service: connect to (or start) a home's service, send it commands, follow its event stream.
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! The service's wire protocol, version [`PROTOCOL`]: newline-delimited JSON (one object per
//! line, UTF-8, at most [`MAX_LINE`] bytes) over the home's local endpoint.
//!
//! The client writes [`Request`]s, each with an id of its choosing; the service answers each with
//! one [`Reply`] carrying that id. The first request of a connection is [`Command::Hello`]; the
//! service refuses any other before it. An operation command (`up`, `down`, `park`, `resume`,
//! `update`, `attach`, `detach`, `retire`, `provision`, `check`) is answered at once with
//! [`Reply::Accepted`], or refused (`locked`) while another of its run is queued or running: one
//! operation per run, but for one asked once the running one was cancelled, queued behind it; the operation's events, ending with its `result` or
//! `error` and an `operation` event of phase `ended`, arrive on a subscription: after `subscribe`,
//! the connection carries [`Reply::Event`] frames until it closes or `unsubscribe`.
//!
//! Everything else an embedder asks of a home goes through the service too, never through the
//! home's files: a run's record (`record`), forgetting it (`forget`), the runs past their lifetime
//! (`expired`), the way into a machine (`console_command`), its screen (`graphical_console`, whose
//! tunnel, when it has one, stays open as long as the connection that asked) or its hypervisor's
//! app (`native_console`), where its published services are reached (`service_endpoints`), what its machines use
//! (`usage`), the
//! runner's SSH key (`public_key`), and what the runs' own downloads fetched (`downloads`) and their
//! removal (`remove_downloads`).
//!
//! # What every protocol keeps
//!
//! A client of one release meets services of others: an app that bundles Isothrum finds the
//! service an older (or newer) release started. So that it can always tell which service runs and
//! replace it, every protocol, this one and every later one, keeps these two frames as they are:
//!
//! - `{"id":N,"cmd":"hello","protocol":P}` is answered `{"reply":"hello","id":N,"protocol":Q,
//!   "version":"…"}` whatever `P` is: `Q` is the protocol the service speaks, `version` its
//!   release. When `P` isn't `Q`, the connection then takes only `shutdown_if_idle` (any other
//!   request is refused).
//! - `{"id":N,"cmd":"shutdown_if_idle"}` is answered `{"reply":"ok","id":N}` (the service stops,
//!   and takes no operation meanwhile) or `{"reply":"busy","id":N,"operations":[…]}` (it has
//!   operations queued or running, and goes on).

use isothrum_runner::{
    Download, DownloadRemoval, DownloadToAdopt, DownloadsToRemove, InputVariable, MachineUsage, NativeConsole, Op, Park, RunName, ServiceEndpoint,
    ShellCommand, Status,
};
use serde::{Deserialize, Serialize};

use crate::v1::{Event, EventCursor, OperationId, OperationRow, RunRow};

mod console;
mod target;
pub use console::{ConsoleEndpoint, ReachedConsole};
pub use target::{OpOptions, Target};

/// The protocol's version: a service refuses a `hello` with another.
pub const PROTOCOL: u32 = 1;

/// The longest line either side accepts, in bytes (a longer one ends the connection).
pub const MAX_LINE: usize = 16 * 1024 * 1024;

/// One request: its id and what it asks.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Request {
    /// The id its reply carries.
    pub id: u64,
    /// What it asks.
    #[serde(flatten)]
    pub command: Command,
}

/// What a request asks of the service.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "cmd", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Command {
    /// Opens the conversation (must come first).
    Hello {
        /// The protocol the client speaks ([`PROTOCOL`]).
        protocol: u32,
    },
    /// Start the run (a parked run is resumed).
    Up {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// How long a run that stops itself may live, in seconds.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        lifetime_s: Option<u64>,
    },
    /// Remove everything the run created.
    Down {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
    },
    /// Stop the run, keeping what it created.
    Park {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// Paused or shut down.
        mode: Park,
    },
    /// Start a parked run again.
    Resume {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
    },
    /// Bring the running run up to date with its project.
    Update {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
    },
    /// Bring a machine of the project that isn't running into the running run.
    Attach {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// The machine.
        machine: String,
    },
    /// Take a machine out of the running run.
    Retire {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// The machine.
        machine: String,
    },
    /// Save a machine of the running run as a box file (Vagrant's `package`).
    Package {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// The machine.
        machine: String,
        /// The box file written.
        output: std::path::PathBuf,
    },
    /// Run the set-up again on the running machines.
    Provision {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// Only this machine.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        machine: Option<String>,
        /// Only the provisioner of this name (Vagrant's `--provision-with`).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        with: Option<String>,
    },
    /// Runs a command in a machine of the run, with no terminal; its result says how it ended.
    Exec {
        /// The run.
        target: Target,
        /// The values it runs with.
        #[serde(default)]
        options: OpOptions,
        /// The machine.
        machine: String,
        /// The command and its arguments.
        argv: Vec<String>,
    },
    /// The run's status (not queued: it reads). `target` is needed for a run not recorded yet;
    /// without it the run is reopened as recorded.
    Status {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The values and secrets its read gives the tools, beside the recorded ones (a
        /// server's API token, for its machines' states there).
        #[serde(default, skip_serializing_if = "OpOptions::is_empty")]
        options: OpOptions,
    },
    /// The input variables a Terraform folder declares (not queued: it reads the folder's
    /// files, and runs nothing).
    Variables {
        /// The folder (its `name` is unused).
        target: Target,
    },
    /// Every recorded run.
    List,
    /// One run's record (not queued: it reads the store); none for a run never recorded.
    Record {
        /// The run.
        run: RunName,
    },
    /// Forgets a run's record: what the run created is left as it is. Refused while an operation
    /// of the run is queued or running.
    Forget {
        /// The run.
        run: RunName,
    },
    /// The recorded runs whose lifetime passed and that may still hold something: what a reaper
    /// takes down (a `down` each, given the values and secrets the run needs).
    Expired,
    /// The command that opens an interactive console into a machine of a run (not queued:
    /// nothing runs), for the client to run in its own terminal.
    ConsoleCommand {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The machine.
        machine: String,
        /// The account to open it as.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        user: Option<String>,
    },
    /// A running machine's graphical console (not queued: it reads). Reached through an SSH
    /// tunnel, the tunnel stays open for as long as the connection that asked: a client asks on a
    /// connection of its own, closed when the viewer is done; the service writes
    /// [`Reply::ConsoleClosed`] and closes it when the tunnel ends first.
    GraphicalConsole {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The values and secrets the hypervisor is asked with (a server's address and token).
        #[serde(default, skip_serializing_if = "OpOptions::is_empty")]
        options: OpOptions,
        /// The machine.
        machine: String,
    },
    /// What opens a running machine in its hypervisor's own app (not queued: it reads).
    NativeConsole {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The values and secrets the hypervisor is asked with.
        #[serde(default, skip_serializing_if = "OpOptions::is_empty")]
        options: OpOptions,
        /// The machine.
        machine: String,
    },
    /// The service endpoints a run publishes, each where this computer reaches it: as the run
    /// gives it, or at a local forward through the run's jump host, which the service keeps open
    /// (not queued: it reads).
    ServiceEndpoints {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The values and secrets its outputs are read with.
        #[serde(default, skip_serializing_if = "OpOptions::is_empty")]
        options: OpOptions,
    },
    /// What each running machine of a run uses now (CPU and memory), as its engine samples it
    /// once; none for a format whose tool reports no per-machine figure (not queued: it reads).
    Usage {
        /// The run.
        run: RunName,
        /// Where it is opened from (default: where its record says).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        target: Option<Target>,
        /// The values and secrets its tools are run with.
        #[serde(default, skip_serializing_if = "OpOptions::is_empty")]
        options: OpOptions,
    },
    /// The public half of the runner's SSH key, one line (the key is made when there is none):
    /// what the client authorizes where a run's SSH goes.
    PublicKey,
    /// What the runs' own downloads fetched and the home's store records (the images a Compose
    /// start's pull pulled, the boxes a Vagrant `up` added), each with its size when it can be read
    /// (not queued: it reads).
    Downloads,
    /// Removes the recorded downloads `which` names, each on its own: one its tool refuses (in
    /// use) is said in its outcome and kept, the others go on. Nothing the store doesn't record is
    /// removed (not queued).
    RemoveDownloads {
        /// All, or those of the given ids.
        which: DownloadsToRemove,
    },
    /// Adopts downloads fetched outside the runs (by an earlier program's own record) into the
    /// home's record, each one on this computer now as it was fetched: they are listed and
    /// removed like the runs' own (not queued).
    AdoptDownloads {
        /// The downloads to adopt.
        adopted: Vec<DownloadToAdopt>,
    },
    /// The IPv4 address blocks the engine's networks hold, but those of a run's own networks:
    /// what a deployment's networks must stay clear of (not queued).
    AddressBlocksInUse {
        /// The run whose own networks don't count.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        except: Option<RunName>,
        /// The Docker context of the engine asked; the current one when none.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        docker_context: Option<String>,
    },
    /// The run's latest operations, newest first.
    Operations {
        /// The run.
        run: RunName,
        /// At most this many.
        limit: u32,
    },
    /// Cancels an operation: a queued one never begins, a running one is cut short (its tools
    /// stopped).
    Cancel {
        /// The operation.
        operation: OperationId,
    },
    /// Turns the connection into a subscription: the events after `after` (every one the
    /// service keeps when `None`), then the live ones, of one run or every run.
    Subscribe {
        /// Only this run's.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        run: Option<RunName>,
        /// Resumes after this cursor (a reconnecting client's last).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        after: Option<EventCursor>,
    },
    /// Ends the connection's subscription.
    Unsubscribe,
    /// Stops the service: queued operations are dropped, running ones cancelled and their tools
    /// stopped, then it exits.
    Shutdown,
    /// Stops the service only when no operation is queued or running ([`Reply::Ok`]: it takes no
    /// new one, and exits); else refused with [`Reply::Busy`], and nothing changes. Every protocol
    /// takes it, from a client of any protocol (see the module's documentation).
    ShutdownIfIdle,
}

impl Command {
    /// The operation it queues, when it queues one.
    #[must_use]
    pub fn op(&self) -> Option<Op> {
        Some(match self {
            Command::Up { .. } => Op::Up,
            Command::Down { .. } => Op::Down,
            Command::Park { .. } => Op::Park,
            Command::Resume { .. } => Op::Resume,
            Command::Update { .. } => Op::Update,
            Command::Attach { .. } => Op::Attach,
            Command::Retire { .. } => Op::Retire,
            Command::Package { .. } => Op::Package,
            Command::Provision { .. } => Op::Provision,
            Command::Exec { .. } => Op::Check,
            // Not an operation: a question about the home, or the conversation's own.
            _ => return None,
        })
    }

    /// Whether it asks the service about the home, running no operation: a run's status or
    /// record, forgetting it, the expired runs, a way into a machine, its endpoints, the key, the
    /// downloads and their removal.
    #[must_use]
    pub fn is_question(&self) -> bool {
        matches!(
            self,
            Command::Status { .. }
                | Command::Record { .. }
                | Command::Forget { .. }
                | Command::Expired
                | Command::ConsoleCommand { .. }
                | Command::GraphicalConsole { .. }
                | Command::NativeConsole { .. }
                | Command::PublicKey
                | Command::ServiceEndpoints { .. }
                | Command::Usage { .. }
                | Command::Downloads
                | Command::RemoveDownloads { .. }
                | Command::AdoptDownloads { .. }
                | Command::AddressBlocksInUse { .. }
        )
    }

    /// The run and the options of an operation command.
    #[must_use]
    pub fn target(&self) -> Option<(&Target, &OpOptions)> {
        match self {
            Command::Up { target, options, .. }
            | Command::Down { target, options }
            | Command::Park { target, options, .. }
            | Command::Resume { target, options }
            | Command::Update { target, options }
            | Command::Attach { target, options, .. }
            | Command::Retire { target, options, .. }
            | Command::Package { target, options, .. }
            | Command::Provision { target, options, .. }
            | Command::Exec { target, options, .. } => Some((target, options)),
            _ => None,
        }
    }
}

/// What the service writes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "reply", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Reply {
    /// To `hello`.
    Hello {
        /// The request's id.
        id: u64,
        /// The protocol it speaks.
        protocol: u32,
        /// Its version.
        version: String,
    },
    /// An operation command was accepted: its id, and its place on its run (0: it begins now; 1:
    /// it waits for the run's cancelled operation to end).
    Accepted {
        /// The request's id.
        id: u64,
        /// The operation's id.
        operation: OperationId,
        /// Its run.
        run: RunName,
        /// How many of the run's operations are ahead of it.
        position: u32,
    },
    /// To `status`.
    Status {
        /// The request's id.
        id: u64,
        /// The status.
        status: Box<Status>,
    },
    /// To `variables`.
    Variables {
        /// The request's id.
        id: u64,
        /// The folder's input variables, by name.
        variables: Vec<InputVariable>,
    },
    /// To `list` (every recorded run) and `expired` (those past their lifetime).
    Runs {
        /// The request's id.
        id: u64,
        /// The runs.
        runs: Vec<RunRow>,
    },
    /// To `record`.
    Run {
        /// The request's id.
        id: u64,
        /// The run's record; none for a run never recorded.
        run: Option<Box<RunRow>>,
    },
    /// To `console_command`.
    Shell {
        /// The request's id.
        id: u64,
        /// The command that opens the console.
        command: ShellCommand,
    },
    /// To `graphical_console`.
    Console {
        /// The request's id.
        id: u64,
        /// Where the console answers, and how to sign in.
        console: Box<ReachedConsole>,
    },
    /// After a `graphical_console` reached through a tunnel, once the tunnel ended on its own
    /// (its `ssh` exited): the service closes the connection after it.
    ConsoleClosed {
        /// The `graphical_console` request's id.
        id: u64,
        /// How the tunnel ended.
        reason: String,
    },
    /// To `native_console`.
    Native {
        /// The request's id.
        id: u64,
        /// What opens the machine in its hypervisor's app.
        console: NativeConsole,
    },
    /// To `service_endpoints`.
    Endpoints {
        /// The request's id.
        id: u64,
        /// The endpoints, by name.
        endpoints: Vec<ServiceEndpoint>,
    },
    /// To `usage`.
    Usage {
        /// The request's id.
        id: u64,
        /// Each running machine's use, by name.
        machines: Vec<MachineUsage>,
    },
    /// To `public_key`.
    PublicKey {
        /// The request's id.
        id: u64,
        /// The key's public half, one line.
        key: String,
    },
    /// To `downloads`.
    Downloads {
        /// The request's id.
        id: u64,
        /// The recorded downloads, oldest first.
        downloads: Vec<Download>,
    },
    /// To `remove_downloads`: how each removal went, in the order asked.
    DownloadsRemoved {
        /// The request's id.
        id: u64,
        /// Each removal.
        removed: Vec<DownloadRemoval>,
    },
    /// To `adopt_downloads`: the downloads recorded for those asked, new or not.
    DownloadsAdopted {
        /// The request's id.
        id: u64,
        /// The recorded downloads.
        adopted: Vec<Download>,
    },
    /// To `address_blocks_in_use`: the blocks, in CIDR form.
    AddressBlocks {
        /// The request's id.
        id: u64,
        /// The blocks.
        blocks: Vec<String>,
    },
    /// To `operations`.
    Operations {
        /// The request's id.
        id: u64,
        /// The operations, newest first.
        operations: Vec<OperationRow>,
    },
    /// Done (`subscribe`, `unsubscribe`, `cancel`, `shutdown`, `shutdown_if_idle`).
    Ok {
        /// The request's id.
        id: u64,
    },
    /// Refused, or failed.
    Error {
        /// The request's id (0 for a line that couldn't be read as a request).
        id: u64,
        /// Why: its code, message and typed fields, as the runner reports a failure.
        #[serde(flatten)]
        report: isothrum_runner::ErrorReport,
    },
    /// To `shutdown_if_idle`, refused: the operations queued or running, which the service goes on
    /// with.
    Busy {
        /// The request's id.
        id: u64,
        /// The operations, oldest first.
        operations: Vec<OperationId>,
    },
    /// A subscription's frame.
    Event {
        /// Its place in the stream.
        cursor: EventCursor,
        /// The event.
        event: Box<Event>,
    },
}

impl Reply {
    /// The request it answers; `None` for a subscription's frame.
    #[must_use]
    pub fn id(&self) -> Option<u64> {
        match self {
            Reply::Hello { id, .. }
            | Reply::Accepted { id, .. }
            | Reply::Status { id, .. }
            | Reply::Runs { id, .. }
            | Reply::Run { id, .. }
            | Reply::Shell { id, .. }
            | Reply::Console { id, .. }
            | Reply::ConsoleClosed { id, .. }
            | Reply::Native { id, .. }
            | Reply::Endpoints { id, .. }
            | Reply::Usage { id, .. }
            | Reply::PublicKey { id, .. }
            | Reply::Variables { id, .. }
            | Reply::Operations { id, .. }
            | Reply::Downloads { id, .. }
            | Reply::DownloadsRemoved { id, .. }
            | Reply::DownloadsAdopted { id, .. }
            | Reply::AddressBlocks { id, .. }
            | Reply::Ok { id }
            | Reply::Busy { id, .. }
            | Reply::Error { id, .. } => Some(*id),
            Reply::Event { .. } => None,
        }
    }
}

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

    use std::fmt;
    use std::net::SocketAddr;

    use isothrum_runner::{ConsoleProtocol, Format};
    use serde_json::json;

    /// `value` written as one line, read back as itself: the line, as JSON.
    fn round_trip<T: Serialize + serde::de::DeserializeOwned + PartialEq + fmt::Debug>(value: &T) -> serde_json::Value {
        let line = serde_json::to_string(value).unwrap();
        assert!(!line.contains('\n'), "one line: {line}");
        assert_eq!(&serde_json::from_str::<T>(&line).unwrap(), value, "{line}");
        serde_json::from_str(&line).unwrap()
    }

    #[test]
    fn a_request_is_one_flat_json_object() {
        let up = Request {
            id: 7,
            command: Command::Up {
                target: Target::new("/stacks/web", Some(RunName::new("web").unwrap())).with_format(Format::Compose),
                options: OpOptions::default(),
                lifetime_s: Some(3600),
            },
        };
        assert_eq!(
            round_trip(&up),
            json!({"id": 7, "cmd": "up", "target": {"path": "/stacks/web", "name": "web", "format": "compose"}, "options": {}, "lifetime_s": 3600})
        );
        let park = round_trip(&Request {
            id: 8,
            command: Command::Park {
                target: Target::new("/stacks/web", None),
                options: OpOptions::default(),
                mode: Park::Pause,
            },
        });
        assert_eq!((&park["cmd"], &park["mode"]), (&json!("park"), &json!("pause")), "{park}");
        round_trip(&Request { id: 9, command: Command::List });
        assert_eq!(
            round_trip(&Request {
                id: 10,
                command: Command::Subscribe {
                    run: None,
                    after: Some(EventCursor(41)),
                },
            }),
            json!({"id": 10, "cmd": "subscribe", "after": 41})
        );
    }

    #[test]
    fn a_secret_is_never_in_the_debug_form() {
        let options = OpOptions {
            secrets: [("TOKEN".to_owned(), "hunter2".to_owned())].into(),
            ..OpOptions::default()
        };
        let shown = format!("{options:?}");
        assert!(shown.contains("TOKEN") && !shown.contains("hunter2"), "{shown}");
    }

    #[test]
    fn reveal_is_off_unless_asked_and_travels_only_when_on() {
        assert_eq!(serde_json::to_string(&OpOptions::default()).unwrap(), "{}");
        let on = OpOptions {
            reveal: true,
            ..OpOptions::default()
        };
        let json = serde_json::to_string(&on).unwrap();
        assert_eq!(json, r#"{"reveal":true}"#);
        assert!(serde_json::from_str::<OpOptions>(&json).unwrap().reveal);
        assert!(!serde_json::from_str::<OpOptions>("{}").unwrap().reveal, "off when not said");
    }

    #[test]
    fn replies_round_trip() {
        let accepted = round_trip(&Reply::Accepted {
            id: 1,
            operation: OperationId(4),
            run: RunName::new("web").unwrap(),
            position: 1,
        });
        assert_eq!(accepted, json!({"reply": "accepted", "id": 1, "operation": 4, "run": "web", "position": 1}));
        let refused = round_trip(&Reply::Error {
            id: 2,
            report: isothrum_runner::ErrorReport::new(isothrum_runner::ErrorCode::Usage, "no").unchanged(),
        });
        assert_eq!(refused, json!({"reply": "error", "id": 2, "code": "usage", "message": "no", "changed": false}));
        let run = RunName::new("web").unwrap();
        let event = crate::v1::phase_event(&run, (Op::Down, OperationId(4)), crate::v1::OperationPhase::Started, None, None);
        let frame = round_trip(&Reply::Event {
            cursor: EventCursor(12),
            event: Box::new(event),
        });
        assert_eq!(
            (&frame["reply"], &frame["cursor"], &frame["event"]["v"]),
            (&json!("event"), &json!(12), &json!(1)),
            "{frame}"
        );
    }

    #[test]
    fn the_home_s_other_questions_are_flat_objects_too() {
        let run = RunName::new("web").unwrap();
        assert_eq!(
            round_trip(&Request {
                id: 4,
                command: Command::Forget { run: run.clone() },
            }),
            json!({"id": 4, "cmd": "forget", "run": "web"})
        );
        assert_eq!(
            round_trip(&Request {
                id: 5,
                command: Command::Expired
            }),
            json!({"id": 5, "cmd": "expired"})
        );
        assert_eq!(
            round_trip(&Request {
                id: 6,
                command: Command::PublicKey
            }),
            json!({"id": 6, "cmd": "public_key"})
        );
        let console = round_trip(&Request {
            id: 7,
            command: Command::GraphicalConsole {
                run: run.clone(),
                target: None,
                options: OpOptions::default(),
                machine: "desktop".into(),
            },
        });
        assert_eq!(console, json!({"id": 7, "cmd": "graphical_console", "run": "web", "machine": "desktop"}));
        round_trip(&Request {
            id: 8,
            command: Command::ConsoleCommand {
                run,
                target: Some(Target::new("/stacks/web", None)),
                machine: "web".into(),
                user: Some("root".into()),
            },
        });
        for command in [
            Command::Record {
                run: RunName::new("web").unwrap(),
            },
            Command::Expired,
            Command::PublicKey,
        ] {
            assert_eq!(command.op(), None, "{command:?} queues nothing");
            assert!(command.is_question(), "{command:?} is a question");
        }
    }

    #[test]
    fn the_downloads_and_their_removal_are_flat_objects_too() {
        assert_eq!(
            round_trip(&Request {
                id: 3,
                command: Command::Downloads
            }),
            json!({"id": 3, "cmd": "downloads"})
        );
        let remove = |which| Request {
            id: 4,
            command: Command::RemoveDownloads { which },
        };
        assert_eq!(
            round_trip(&remove(DownloadsToRemove::All)),
            json!({"id": 4, "cmd": "remove_downloads", "which": "all"})
        );
        assert_eq!(
            round_trip(&remove(DownloadsToRemove::Only(vec![isothrum_runner::DownloadId(7)]))),
            json!({"id": 4, "cmd": "remove_downloads", "which": {"only": [7]}})
        );
        assert!(Command::Downloads.is_question() && Command::Downloads.op().is_none());
        assert!(remove(DownloadsToRemove::All).command.is_question());

        let image = json!({"id": 7, "kind": "image", "reference": "nginx:1.27", "image_id": "sha256:a", "docker_context": "orbstack",
            "run": "web", "fetched_at": "2026-11-17T10:00:00Z", "size_bytes": 187_654_321});
        let listed = json!({"reply": "downloads", "id": 5, "downloads": [image.clone()]});
        let downloads: Reply = serde_json::from_value(listed.clone()).unwrap();
        assert_eq!(round_trip(&downloads), listed);
        assert_eq!(downloads.id(), Some(5));
        let removed = json!({"reply": "downloads_removed", "id": 6, "removed": [
            {"id": 7, "outcome": "failed", "reason": "in use", "download": image},
            {"id": 8, "outcome": "not_recorded"}
        ]});
        let outcomes: Reply = serde_json::from_value(removed.clone()).unwrap();
        assert_eq!(round_trip(&outcomes), removed);
    }

    #[test]
    fn a_reached_console_s_secrets_travel_but_never_show() {
        let reached = ReachedConsole {
            machine: "pve".into(),
            protocol: ConsoleProtocol::Vnc,
            endpoint: ConsoleEndpoint::ProxmoxVnc {
                websocket: "wss://server/vnc?ticket=t0p".into(),
                port: 5900,
                ticket: "t0p".into(),
                header: "Authorization".into(),
                authorization: "PVEAPIToken=hunter2".into(),
                insecure: false,
            },
            user: None,
            password: Some("t0p".into()),
            note: None,
            tunnelled: false,
        };
        let frame = round_trip(&Reply::Console {
            id: 9,
            console: Box::new(reached.clone()),
        });
        assert_eq!(
            (&frame["console"]["endpoint"]["at"], &frame["console"]["password"]),
            (&json!("proxmox_vnc"), &json!("t0p"))
        );
        let shown = format!("{reached:?}");
        assert!(!shown.contains("t0p") && !shown.contains("hunter2"), "{shown}");
        let loopback = ConsoleEndpoint::Loopback {
            address: SocketAddr::from(([127, 0, 0, 1], 5901)),
        };
        assert_eq!(serde_json::to_value(&loopback).unwrap(), json!({"at": "loopback", "address": "127.0.0.1:5901"}));
    }

    #[test]
    fn the_frames_every_protocol_keeps_read_as_written() {
        let hello = r#"{"id":0,"cmd":"hello","protocol":7}"#;
        assert_eq!(serde_json::from_str::<Request>(hello).unwrap().command, Command::Hello { protocol: 7 });
        assert_eq!(
            round_trip(&Request {
                id: 3,
                command: Command::ShutdownIfIdle,
            }),
            json!({"id": 3, "cmd": "shutdown_if_idle"})
        );
        let hello_reply = round_trip(&Reply::Hello {
            id: 0,
            protocol: 7,
            version: "9.0.0".into(),
        });
        assert_eq!(hello_reply, json!({"reply": "hello", "id": 0, "protocol": 7, "version": "9.0.0"}));
        let busy = round_trip(&Reply::Busy {
            id: 3,
            operations: vec![OperationId(4), OperationId(9)],
        });
        assert_eq!(busy, json!({"reply": "busy", "id": 3, "operations": [4, 9]}));
        assert_eq!(round_trip(&Reply::Ok { id: 3 }), json!({"reply": "ok", "id": 3}));
    }
}