shep 0.19.0

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS, Linux and Windows, with logs, watch and cron restarts, and webhook alerts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
//! The dogs tab under a sheep pane: the listing that fills it, a dog's row
//! opening to its table, the keys on both, and the table write the tab's
//! removal sends.
//!
//! An open dog's table is a pane of its own, held by the tab. A key that
//! lands inside one runs against that pane through [`App::in_table`], which
//! seats it as the body for the length of the handler, so every handler
//! written for a pane edits a table inline as it did on a pane of its own.

use super::*;

impl App {
    /// The ask for the dogs listing, the first time the `dogs` tab is up.
    /// [`Effect::None`] on any other tab, and once it has been asked.
    pub(super) fn dogs_tab_effect(&mut self) -> Effect {
        let Some(pane) = self.config_pane() else {
            return Effect::None;
        };
        if !pane.shows_dogs() || !pane.dogs_tab().needs_listing() {
            return Effect::None;
        }
        let sheep = pane.target().name().to_owned();
        if self.refuse_over_host() {
            return Effect::None;
        }
        if let Some(pane) = self.config_pane_mut() {
            pane.dogs_tab_mut().start_listing();
        }
        self.sheep_dogs_ask += 1;
        Effect::ListSheepDogs {
            sheep,
            ask: self.sheep_dogs_ask,
        }
    }

    /// One [`Msg::SheepDogNames`]: lists the dogs on the tab that asked, every
    /// row closed.
    ///
    /// Guarded on the pane's own sheep the way [`Self::on_dog_section`] is
    /// guarded on its dog, and on the ask: closing the pane moves it on, so
    /// an answer outlives neither the pane nor a newer ask.
    pub(super) fn on_sheep_dog_names(
        &mut self,
        sheep: &str,
        ask: u64,
        dogs: Vec<ListedDog>,
    ) -> Effect {
        let latest = ask == self.sheep_dogs_ask;
        let mut listed = false;
        if let Some(pane) = self.config_pane_mut()
            && latest
            && matches!(pane.target(), PaneTarget::Sheep { name } if name == sheep)
        {
            pane.list_dogs(dogs);
            listed = true;
        }
        if !listed {
            return Effect::None;
        }
        self.open_focused_dog(sheep)
    }

    /// The dog `shep lookout <sheep> dogs <dog>` asked for, opened as soon as
    /// the tab's listing names it, so the operator sees its table without a
    /// keystroke between the two reads.
    ///
    /// A dog the listing does not name is reported and the tab stays open as
    /// it is: the ask is spent either way, the same rule
    /// [`App::resolve_focus`]'s sheep follows.
    fn open_focused_dog(&mut self, sheep: &str) -> Effect {
        let Some(dog) = self.take_focus_dog() else {
            return Effect::None;
        };
        let Some(index) = self.config_pane().and_then(|pane| {
            pane.dogs_tab()
                .rows()
                .iter()
                .position(|row| row.name() == dog)
        }) else {
            self.notice = Some(Notice::grave(format!("{sheep}: no dog named {dog}")));
            return Effect::None;
        };
        self.toggle_dog_row(index, Some(true))
    }

    /// One [`Msg::SheepDog`]: the dog's schema has come, so its row, if it
    /// was waiting, opens. A dog that publishes no sheep schema opens
    /// nothing and says so.
    pub(super) fn on_sheep_dog(&mut self, sheep: &str, entry: SheepDogEntry) -> Effect {
        let Some(pane) = self.config_pane_mut() else {
            return Effect::None;
        };
        if !matches!(pane.target(), PaneTarget::Sheep { name } if name == sheep) {
            return Effect::None;
        }
        if let Some(DogToggle::NoSchema(dog)) = pane.answer_dog(entry) {
            self.notice = Some(no_schema_notice(&dog));
        }
        Effect::None
    }

    /// The `dogs` tab's own keys, ahead of the pane's: an armed removal, a
    /// dog's row, and a key landing inside an open table. [`None`] for every
    /// key the pane's keymap answers the same way here as on any tab.
    ///
    /// `d` arms a removal, which `Enter` confirms and every other key but a
    /// quit cancels, the secrets pane's rule.
    pub(super) fn on_dogs_tab_key(&mut self, key: KeyPress) -> Option<Effect> {
        let pane = self.config_pane()?;
        if !pane.shows_dogs() {
            return None;
        }
        let armed = pane.dogs_tab().armed().is_some();
        if armed && !matches!(key, KeyPress::Confirm | KeyPress::Quit) {
            self.config_pane_mut()?.dogs_tab_mut().disarm();
            if key == KeyPress::Escape {
                return Some(Effect::None);
            }
        }
        let pane = self.config_pane()?;
        // An open list owns the keyboard, movement and `Escape` included.
        let list_open = pane.focus().list().is_some();
        match pane.cursor()? {
            PaneRow::Dog(dog) => self.on_dog_row_key(key, dog),
            PaneRow::Under { .. } if list_open || lands_in_table(key) => Some(
                self.in_table(|app| app.on_pane_key(key))
                    .unwrap_or(Effect::None),
            ),
            _ => None,
        }
    }

    /// A key with the cursor on `dog`'s row.
    fn on_dog_row_key(&mut self, key: KeyPress, dog: usize) -> Option<Effect> {
        let armed = self.config_pane()?.dogs_tab().armed().is_some();
        let effect = match key {
            KeyPress::Confirm if armed => self.confirm_table_removal(),
            KeyPress::Confirm | KeyPress::Edit => self.toggle_dog_row(dog, None),
            KeyPress::TabNext => self.toggle_dog_row(dog, Some(true)),
            KeyPress::TabPrev => self.toggle_dog_row(dog, Some(false)),
            KeyPress::Remove => self.arm_table_removal(dog),
            // `space` files nothing on a row that holds no value.
            KeyPress::Cycle => Effect::None,
            _ => return None,
        };
        Some(effect)
    }

    /// Opens the row at `dog` if it is closed, else closes it; with `want`
    /// set, only toward that state. An adopted dog's first opening sends its
    /// probe.
    fn toggle_dog_row(&mut self, dog: usize, want: Option<bool>) -> Effect {
        let Some(pane) = self.config_pane() else {
            return Effect::None;
        };
        let Some(row) = pane.dogs_tab().rows().get(dog) else {
            return Effect::None;
        };
        if want.is_some_and(|open| open == row.is_open()) {
            return Effect::None;
        }
        let probes = !row.is_open() && row.needs_probe();
        let sheep = pane.target().name().to_owned();
        // An adopted dog's binary is on the shepherd's machine.
        if probes && self.refuse_over_host() {
            return Effect::None;
        }
        let Some(pane) = self.config_pane_mut() else {
            return Effect::None;
        };
        match pane.toggle_dog(dog) {
            DogToggle::Ask { dog, adopted_path } => Effect::LoadSheepDog {
                sheep,
                dog,
                adopted_path,
            },
            DogToggle::NoSchema(dog) => {
                self.notice = Some(no_schema_notice(&dog));
                Effect::None
            }
            DogToggle::Closed | DogToggle::Opened | DogToggle::Waiting => Effect::None,
        }
    }

    /// `d` on `dog`'s row: arms the removal of its table. A row with no
    /// table arms nothing and says nothing; a closed gate refuses in its own
    /// words.
    fn arm_table_removal(&mut self, dog: usize) -> Effect {
        let has_table = self
            .config_pane()
            .and_then(|pane| pane.dogs_tab().rows().get(dog))
            .is_some_and(DogRow::has_table);
        if !has_table || self.authorize_write().is_none() {
            return Effect::None;
        }
        if let Some(pane) = self.config_pane_mut() {
            pane.dogs_tab_mut().arm_removal(dog);
        }
        Effect::None
    }

    /// `Enter` with a removal armed: sends it and disarms.
    fn confirm_table_removal(&mut self) -> Effect {
        let Some(pane) = self.config_pane_mut() else {
            return Effect::None;
        };
        let PaneTarget::Sheep { name } = pane.target().clone() else {
            return Effect::None;
        };
        let tab = pane.dogs_tab_mut();
        let Some(dog) = tab.armed().map(str::to_owned) else {
            return Effect::None;
        };
        tab.disarm();
        let Some(authority) = self.authorize_write() else {
            return Effect::None;
        };
        let ticket = self.take_write_ticket();
        Effect::Send(Sent::SetSheepDogTable {
            name,
            dog,
            ticket,
            table: None,
            authority,
        })
    }

    /// Runs `run` with the open table the cursor is inside seated as the
    /// body, then puts it back and moves the sheep pane's cursor to the row
    /// the table's own is on. [`None`] when the cursor is not in a table.
    pub(super) fn in_table<R>(&mut self, run: impl FnOnce(&mut Self) -> R) -> Option<R> {
        let dog = self.config_pane()?.focused_dog()?;
        self.in_dog_table(dog, run)
    }

    /// [`Self::in_table`] for the table of the dog at `dog`.
    pub(super) fn in_dog_table<R>(
        &mut self,
        dog: usize,
        run: impl FnOnce(&mut Self) -> R,
    ) -> Option<R> {
        let table = self.config_pane_mut()?.dogs_tab_mut().take_table(dog)?;
        let sheep = core::mem::replace(&mut self.body, Body::ConfigPane(table));
        let out = run(self);
        if let Body::ConfigPane(table) = core::mem::replace(&mut self.body, sheep)
            && let Some(pane) = self.config_pane_mut()
        {
            pane.dogs_tab_mut().seat_table(dog, table);
            pane.pull_focus(dog);
        }
        Some(out)
    }

    /// One `Request::SetSheepDogSettings` reply, for a removal or for a
    /// table's write on close.
    ///
    /// A success re-reads the sheep, so an open tab shows the table
    /// gone; [`Self::on_sheep_config`] drops the answer when no pane is
    /// waiting for it. Neither arm can print a value: the reply carries
    /// none, and `removed` comes off the request.
    pub(super) fn on_table_set(
        &mut self,
        name: &str,
        dog: &str,
        removed: bool,
        result: Result<Response, RequestError>,
    ) -> Effect {
        let why = match result {
            Ok(Response::SheepDogSettingsSet { .. }) => {
                let verb = if removed { "removed" } else { "written" };
                self.notice = Some(Notice::info(format!(
                    "{name}: its {dog} table is {verb}, and {dog} is told"
                )));
                return Effect::Send(Sent::SheepConfig {
                    name: name.to_owned(),
                });
            }
            other => failed_reply(other),
        };
        self.notice = Some(Notice::grave(format!("{name}: {dog}: {why}")));
        Effect::None
    }

    /// A table write the link task never took.
    pub(super) fn on_table_unsent(&mut self, name: &str, dog: &str) -> Effect {
        self.notice = Some(Notice::grave(format!(
            "{name}: its {dog} table was not sent"
        )));
        Effect::None
    }
}

/// Whether a key with the cursor inside an open table is the table's to
/// answer. Movement, `Escape`, `r`, the help overlay and the tab keys are the
/// sheep pane's, because the table is only part of what they walk.
fn lands_in_table(key: KeyPress) -> bool {
    !matches!(
        key,
        KeyPress::Quit
            | KeyPress::Escape
            | KeyPress::Refresh
            | KeyPress::Help
            | KeyPress::SelectUp
            | KeyPress::SelectDown
            | KeyPress::SelectFirst
            | KeyPress::SelectLast
            | KeyPress::NextGroup
            | KeyPress::PrevGroup
            | KeyPress::Group(_)
    )
}

fn no_schema_notice(dog: &str) -> Notice {
    Notice::grave(format!(
        "{dog} publishes no sheep schema, so its table is not edited here"
    ))
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use shep_core::protocol::{RpcError, RpcErrorCode};

    use super::*;
    use crate::lookout::app::testing::*;
    use crate::lookout::pane::{DogTableState, ListRow};

    fn dog_names(app: &App) -> Vec<&str> {
        app.config_pane()
            .expect("a pane")
            .dogs_tab()
            .rows()
            .iter()
            .map(DogRow::name)
            .collect()
    }

    fn row<'a>(app: &'a App, dog: &str) -> &'a DogRow {
        app.config_pane()
            .expect("a pane")
            .dogs_tab()
            .rows()
            .iter()
            .find(|row| row.name() == dog)
            .unwrap_or_else(|| panic!("{dog} is not listed"))
    }

    fn cursor_dog(app: &App) -> Option<&str> {
        let pane = app.config_pane()?;
        match pane.cursor()? {
            PaneRow::Dog(index) => pane.dogs_tab().rows().get(index).map(DogRow::name),
            _ => None,
        }
    }

    #[test]
    fn the_dogs_tab_opens_with_every_dog_collapsed_and_no_probe_sent() {
        let mut app = app_in_web(Control::Allowed);
        let ask = ask_dogs(&mut app);
        let effect = app.update(Msg::SheepDogNames {
            sheep: "web".into(),
            ask,
            dogs: listing(),
        });
        assert_eq!(effect, Effect::None, "the listing probes no dog");
        assert_eq!(dog_names(&app), ["deploy", "jobs", "legacy"]);
        for dog in ["deploy", "jobs", "legacy"] {
            assert!(!row(&app, dog).is_open(), "{dog} starts collapsed");
        }
        assert!(
            app.config_pane()
                .expect("a pane")
                .rows()
                .iter()
                .all(|row| matches!(row, PaneRow::Dog(_))),
            "no table is drawn yet"
        );
    }

    #[test]
    fn opening_a_dog_sends_one_probe_and_its_table_draws_inline() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        assert_eq!(
            app.update(Msg::Key(KeyPress::Confirm)),
            Effect::LoadSheepDog {
                sheep: "web".into(),
                dog: "jobs".into(),
                adopted_path: Some("/opt/jobs".into()),
            }
        );
        assert_eq!(
            app.update(Msg::SheepDog {
                sheep: "web".into(),
                entry: jobs_entry(),
            }),
            Effect::None
        );
        assert!(row(&app, "jobs").is_open());
        let pane = app.config_pane().expect("a pane");
        assert!(
            pane.rows()
                .iter()
                .any(|row| matches!(row, PaneRow::Under { .. })),
            "the table's rows sit under the dog's"
        );
        assert_eq!(
            row(&app, "jobs")
                .table()
                .expect("built")
                .value("concurrency"),
            "2"
        );

        // Closed and opened again, the answer is kept: no second probe.
        assert_eq!(app.update(Msg::Key(KeyPress::Confirm)), Effect::None);
        assert!(!row(&app, "jobs").is_open());
        assert_eq!(app.update(Msg::Key(KeyPress::Confirm)), Effect::None);
        assert!(row(&app, "jobs").is_open());
    }

    /// `SetSheepDogSettings` replaces the table, so the one write carries
    /// the secret the operator never touched exactly as it was.
    #[test]
    fn an_edit_inside_an_inline_table_writes_the_whole_table_once() {
        let mut app = app_in_jobs_table();
        type_concurrency(&mut app, "4");
        let effect = close_jobs_table(&mut app);
        assert!(!format!("{effect:?}").contains(TOKEN), "{effect:?}");
        let batch = wire_batch(effect);
        let [
            Sent::SetSheepDogTable {
                name,
                dog,
                table: Some(table),
                ..
            },
        ] = batch.as_slice()
        else {
            panic!("one table write: {batch:?}");
        };
        assert_eq!((name.as_str(), dog.as_str()), ("web", "jobs"));
        assert_eq!(
            serde_json::Value::Object(table.as_map().clone()),
            json!({ "concurrency": 4, "token": TOKEN })
        );
        assert!(
            matches!(app.body(), Body::FlockTable),
            "esc lands on the dashboard"
        );
    }

    #[test]
    fn closing_with_a_table_open_and_no_edits_sends_nothing() {
        let mut app = app_in_jobs_table();
        assert_eq!(app.update(Msg::Key(KeyPress::Escape)), Effect::None);
        assert!(matches!(app.body(), Body::FlockTable));
    }

    #[test]
    fn a_table_closed_after_an_edit_still_writes_it() {
        let mut app = app_in_jobs_table();
        type_concurrency(&mut app, "4");
        cursor_on(&mut app, "jobs");
        let _ = app.update(Msg::Key(KeyPress::Confirm));
        assert!(!row(&app, "jobs").is_open());
        let batch = wire_batch(close_jobs_table(&mut app));
        let [Sent::SetSheepDogTable { dog, .. }] = batch.as_slice() else {
            panic!("one table write: {batch:?}");
        };
        assert_eq!(dog, "jobs");
    }

    /// The list a table's field opens to owns every key, movement and
    /// `Escape` included, the same as on a pane of its own.
    #[test]
    fn a_list_inside_an_inline_table_walks_adds_and_backs_out_one_level() {
        let mut schema = jobs_schema();
        schema["properties"]["tags"] = json!({ "type": "array", "items": { "type": "string" } });
        let mut app = app_in_jobs_table_over(SheepDogEntry {
            schema: Some(schema),
            ..jobs_entry()
        });
        pane_to(&mut app, "tags");
        let _ = app.update(Msg::Key(KeyPress::Edit));
        let list = |app: &App| {
            app.config_pane()
                .expect("a pane")
                .focus()
                .list()
                .map(|list| (list.elements().to_vec(), list.cursor()))
        };
        assert_eq!(list(&app), Some((vec![], Some(ListRow::New))));

        for typed in ['a', 'b'] {
            let _ = app.update(Msg::Key(KeyPress::SelectLast));
            let _ = app.update(Msg::Key(KeyPress::Confirm));
            let _ = app.update(Msg::Key(KeyPress::TextChar(typed)));
            let _ = app.update(Msg::Key(KeyPress::TextApply));
        }
        let _ = app.update(Msg::Key(KeyPress::SelectFirst));
        let _ = app.update(Msg::Key(KeyPress::SelectDown));
        assert_eq!(
            list(&app),
            Some((vec!["a".into(), "b".into()], Some(ListRow::Item(1))))
        );

        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert_eq!(list(&app), None, "esc closes the list");
        assert!(
            matches!(app.body(), Body::ConfigPane(_)),
            "and leaves the sheep pane up"
        );
    }

    #[test]
    fn a_landed_table_write_says_so_and_names_no_value() {
        let mut app = app_in_jobs_table();
        type_concurrency(&mut app, "4");
        let mut batch = wire_batch(close_jobs_table(&mut app));
        let effect = app.update(Msg::Replied {
            sent: batch.remove(0),
            result: Ok(Response::SheepDogSettingsSet {
                name: "web".into(),
                dog: "jobs".into(),
            }),
        });
        assert_eq!(
            effect,
            Effect::Send(Sent::SheepConfig { name: "web".into() })
        );
        let notice = app.notice().expect("reported").to_string();
        assert_eq!(notice, "web: its jobs table is written, and jobs is told");
    }

    #[test]
    fn a_dog_with_no_sheep_schema_opens_nothing_and_says_why() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "legacy");
        assert_eq!(app.update(Msg::Key(KeyPress::Edit)), Effect::None);
        assert!(!row(&app, "legacy").is_open());
        let notice = app.notice().expect("a refusal").to_string();
        assert!(notice.contains("no sheep schema"), "{notice}");
        assert!(!notice.contains(PASSWORD), "{notice}");
    }

    #[test]
    fn a_probe_that_finds_no_schema_leaves_the_row_closed_and_says_why() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "deploy");
        let _ = app.update(Msg::Key(KeyPress::Confirm));
        let _ = app.update(Msg::SheepDog {
            sheep: "web".into(),
            entry: SheepDogEntry {
                name: "deploy".into(),
                adopted_path: Some("/opt/deploy".into()),
                schema: None,
            },
        });
        assert!(!row(&app, "deploy").is_open());
        let notice = app.notice().expect("a refusal").to_string();
        assert!(
            notice.contains("deploy publishes no sheep schema"),
            "{notice}"
        );
    }

    #[test]
    fn a_probe_answer_for_another_sheep_opens_nothing() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        let _ = app.update(Msg::Key(KeyPress::Confirm));
        let _ = app.update(Msg::SheepDog {
            sheep: "api".into(),
            entry: jobs_entry(),
        });
        assert!(row(&app, "jobs").table().is_none());
    }

    #[test]
    fn the_dogs_tab_lists_once_per_pane() {
        let mut app = app_in_web(Control::Allowed);
        let _ = ask_dogs(&mut app);
        let _ = app.update(Msg::Key(KeyPress::Group(1)));
        assert_eq!(app.update(Msg::Key(KeyPress::Group(9))), Effect::None);
    }

    #[test]
    fn an_answer_for_a_sheep_the_pane_has_left_lists_nothing() {
        let mut app = app_in_web(Control::Allowed);
        let ask = ask_dogs(&mut app);
        answer(&mut app, "api", ask, listing());
        assert!(!app.config_pane().unwrap().dogs_tab().is_listed());

        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert!(app.config_pane().is_none());
        answer(&mut app, "web", ask, listing());
        assert!(app.config_pane().is_none(), "a late answer opens nothing");
        assert!(app.notice().is_none(), "silently: nothing went wrong");
    }

    #[test]
    fn an_answer_for_a_closed_pane_lists_nothing_on_its_reopening() {
        let mut app = app_in_web(Control::Allowed);
        let ask = ask_dogs(&mut app);
        let _ = app.update(Msg::Key(KeyPress::Escape));
        let _ = app.update(Msg::Key(KeyPress::Edit));
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig { name: "web".into() },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        assert!(app.config_pane().is_some(), "reopened");
        answer(&mut app, "web", ask, listing());
        assert!(!app.config_pane().unwrap().dogs_tab().is_listed());
    }

    /// `d` there would file `dogs = null`, and the close would then drop
    /// every table the sheep carries in one write; `space` files nothing on
    /// a row that holds no value.
    #[test]
    fn space_on_a_dog_row_files_nothing() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        assert_eq!(app.update(Msg::Key(KeyPress::Cycle)), Effect::None);
        assert!(app.config_pane().unwrap().edits().is_empty());
        assert!(app.notice().is_none(), "{:?}", app.notice());
    }

    #[test]
    fn d_arms_and_enter_sends_the_removal() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        assert_eq!(app.update(Msg::Key(KeyPress::Remove)), Effect::None);
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), Some("jobs"));
        let sent = app.update(Msg::Key(KeyPress::Confirm));
        let Effect::Send(Sent::SetSheepDogTable {
            ref name,
            ref dog,
            table: None,
            ..
        }) = sent
        else {
            panic!("the confirm sends the removal: {sent:?}");
        };
        assert_eq!((name.as_str(), dog.as_str()), ("web", "jobs"));
        assert_eq!(
            wire(sent),
            Request::SetSheepDogSettings {
                name: "web".into(),
                dog: "jobs".into(),
                table: None,
            }
        );
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), None);
    }

    #[test]
    fn any_other_key_disarms_and_the_first_esc_is_eaten() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        let _ = app.update(Msg::Key(KeyPress::Remove));
        let _ = app.update(Msg::Key(KeyPress::SelectDown));
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), None);
        assert_eq!(cursor_dog(&app), Some("legacy"));

        let _ = app.update(Msg::Key(KeyPress::Remove));
        assert_eq!(
            app.config_pane().unwrap().dogs_tab().armed(),
            Some("legacy")
        );
        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), None);
        assert!(app.config_pane().is_some(), "the pane stays up");
    }

    #[test]
    fn a_row_with_no_table_arms_nothing() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "deploy");
        let _ = app.update(Msg::Key(KeyPress::Remove));
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), None);
        assert!(app.notice().is_none(), "{:?}", app.notice());
    }

    #[test]
    fn a_closed_gate_refuses_the_removal_in_its_own_words() {
        let mut app = app_in_dogs(Control::ReadOnly);
        cursor_on(&mut app, "jobs");
        assert_eq!(app.update(Msg::Key(KeyPress::Remove)), Effect::None);
        assert_eq!(app.config_pane().unwrap().dogs_tab().armed(), None);
        assert_eq!(
            app.notice().map(ToString::to_string),
            Some(READ_ONLY_REFUSAL.to_string())
        );
    }

    #[test]
    fn a_landed_removal_notices_and_the_re_read_refreshes_the_tab() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        let _ = app.update(Msg::Key(KeyPress::Remove));
        let Effect::Send(sent) = app.update(Msg::Key(KeyPress::Confirm)) else {
            panic!("the removal goes out");
        };
        let effect = app.update(Msg::Replied {
            sent,
            result: Ok(Response::SheepDogSettingsSet {
                name: "web".into(),
                dog: "jobs".into(),
            }),
        });
        assert_eq!(
            effect,
            Effect::Send(Sent::SheepConfig { name: "web".into() })
        );
        let notice = app.notice().expect("reported").to_string();
        assert_eq!(notice, "web: its jobs table is removed, and jobs is told");

        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig { name: "web".into() },
            result: Ok(Response::SheepConfig(Box::new(web_view(false)))),
        });
        assert_eq!(row(&app, "jobs").state(), DogTableState::Unset);
        assert_eq!(cursor_dog(&app), Some("jobs"));
    }

    #[test]
    fn a_refused_write_lands_its_message_and_no_value() {
        let mut app = app_in_dogs(Control::Allowed);
        let table: serde_json::Map<String, serde_json::Value> =
            json!({ "token": TOKEN }).as_object().cloned().unwrap();
        let sent = Sent::SetSheepDogTable {
            name: "web".into(),
            dog: "jobs".into(),
            ticket: 7,
            table: Some(table.into()),
            authority: WriteAuthority::granted(&app).expect("the gate is open"),
        };
        assert!(!format!("{sent:?}").contains(TOKEN), "{sent:?}");
        let effect = app.update(Msg::Replied {
            sent: sent.clone(),
            result: Err(RequestError::Rpc(RpcError {
                code: RpcErrorCode::NotFound,
                message: "no sheep named web".into(),
                daemon_version: None,
            })),
        });
        assert_eq!(effect, Effect::None, "a refusal does not re-read");
        let notice = app.notice().expect("reported");
        assert!(notice.is_grave());
        assert_eq!(notice.to_string(), "web: jobs: no sheep named web");

        let _ = app.update(Msg::Unsent { sent });
        let notice = app.notice().expect("reported").to_string();
        assert_eq!(notice, "web: its jobs table was not sent");
    }

    /// `Msg` derives `Debug`, and a listing or a probe answer carries each
    /// dog's schema, defaults included (IR-41).
    #[test]
    fn the_listing_and_probe_answers_debug_name_no_schema() {
        let entry = SheepDogEntry {
            name: "jobs".into(),
            adopted_path: None,
            schema: Some(json!({ "default": TOKEN })),
        };
        let listed = Msg::SheepDogNames {
            sheep: "web".into(),
            ask: 1,
            dogs: vec![ListedDog {
                entry: entry.clone(),
                asked: true,
            }],
        };
        assert!(!format!("{listed:?}").contains(TOKEN), "{listed:?}");
        let probed = Msg::SheepDog {
            sheep: "web".into(),
            entry,
        };
        assert!(!format!("{probed:?}").contains(TOKEN), "{probed:?}");
    }

    /// The sheep is the table's owner, so `r` re-reads the sheep and the
    /// open table takes its values from the answer, cursor and edits kept.
    #[test]
    fn r_re_reads_the_sheep_and_rebuilds_the_open_table_keeping_the_cursor() {
        let mut app = app_in_jobs_table();
        type_concurrency(&mut app, "4");
        pane_to(&mut app, "token");
        let cursor = app.config_pane().unwrap().view().cursor();
        assert_eq!(
            app.update(Msg::Key(KeyPress::Refresh)),
            Effect::Send(Sent::SheepConfig { name: "web".into() })
        );
        let mut view = web_view(true);
        let moved = json!({ "concurrency": 9, "token": "rotated" });
        view.config.dogs.insert(
            "jobs".into(),
            moved.as_object().cloned().expect("a table").into(),
        );
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig { name: "web".into() },
            result: Ok(Response::SheepConfig(Box::new(view))),
        });
        assert!(row(&app, "jobs").is_open());
        assert_eq!(
            row(&app, "jobs")
                .table()
                .expect("built")
                .value("concurrency"),
            "9"
        );
        assert_eq!(app.config_pane().unwrap().view().cursor(), cursor);
        assert_eq!(table_edits(&app), 1, "the operator's edit survives");
    }

    /// A table another writer added above `jobs` shifts every row. The
    /// highlighted row and the row the table acts on must stay one row.
    #[test]
    fn a_re_read_that_shifts_the_rows_keeps_the_cursor_on_its_field() {
        let mut app = app_in_jobs_table();
        pane_to(&mut app, "token");
        let _ = app.update(Msg::Key(KeyPress::Refresh));
        let mut view = web_view(true);
        let alpha = json!({ "on": true });
        view.config.dogs.insert(
            "alpha".into(),
            alpha.as_object().cloned().expect("a table").into(),
        );
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig { name: "web".into() },
            result: Ok(Response::SheepConfig(Box::new(view))),
        });
        assert_eq!(dog_names(&app)[0], "alpha", "the new table sorts first");
        let pane = app.config_pane().expect("a pane");
        let Some(PaneRow::Under { dog, at }) = pane.cursor() else {
            panic!("the cursor stays inside jobs's table: {:?}", pane.cursor());
        };
        let jobs = &pane.dogs_tab().rows()[dog];
        assert_eq!(jobs.name(), "jobs");
        let table = jobs.table().expect("built");
        assert_eq!(table.view().cursor(), at, "the table acts on the shown row");
        assert_eq!(
            table.cursor_field().map(|field| field.key.as_str()),
            Some("token")
        );
    }

    /// The probe answer lands after the cursor moved to another dog; the
    /// table it draws shifts the rows below it.
    #[test]
    fn a_probe_answer_after_the_cursor_moved_keeps_it_on_its_dog() {
        let mut app = app_in_dogs(Control::Allowed);
        cursor_on(&mut app, "jobs");
        assert!(matches!(
            app.update(Msg::Key(KeyPress::Confirm)),
            Effect::LoadSheepDog { .. }
        ));
        let _ = app.update(Msg::Key(KeyPress::SelectDown));
        assert_eq!(cursor_dog(&app), Some("legacy"));
        let _ = app.update(Msg::SheepDog {
            sheep: "web".into(),
            entry: jobs_entry(),
        });
        assert!(row(&app, "jobs").is_open());
        assert_eq!(cursor_dog(&app), Some("legacy"));
    }

    #[test]
    fn a_re_read_of_another_sheep_leaves_the_open_table_alone() {
        let mut app = app_in_jobs_table();
        let mut view = web_view(true);
        view.name = "api".into();
        let other = json!({ "concurrency": 64 });
        view.config.dogs.insert(
            "jobs".into(),
            other.as_object().cloned().expect("a table").into(),
        );
        let _ = app.update(Msg::Replied {
            sent: Sent::SheepConfig { name: "api".into() },
            result: Ok(Response::SheepConfig(Box::new(view))),
        });
        assert_eq!(
            row(&app, "jobs")
                .table()
                .expect("built")
                .value("concurrency"),
            "2"
        );
    }

    #[test]
    fn a_reply_of_the_wrong_kind_is_reported_and_re_reads_nothing() {
        let mut app = app_in_dogs(Control::Allowed);
        let effect = app.update(Msg::Replied {
            sent: Sent::SetSheepDogTable {
                name: "web".into(),
                dog: "jobs".into(),
                ticket: 7,
                table: None,
                authority: WriteAuthority::granted(&app).expect("the gate is open"),
            },
            result: Ok(Response::SheepConfig(Box::new(web_view(true)))),
        });
        assert_eq!(effect, Effect::None);
        let notice = app.notice().expect("reported");
        assert!(notice.is_grave());
        assert_eq!(
            notice.to_string(),
            "web: jobs: the shepherd answered something this lookout does not understand"
        );
    }
}