shep 0.16.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
//! The bleats pane's own keymap, and the scroll back through the tail.
//!
//! `o`, `m` and `/` reach this keymap only through the dispatcher in
//! [`super::super::feed_keys`], which the sheep pane's embedded feed runs as
//! well, so neither screen can answer one of them on its own.

use super::super::*;

impl App {
    /// The bleats pane's own keymap, in force while [`Self::bleats_pane`] is
    /// `Some`. `Escape` drops the newest filter chip first, one axis at a
    /// time, and only closes the pane once none are left. `j`/`k` scroll a
    /// line, `ctrl-d`/`ctrl-u` a page, `G` jumps to the tail and resumes
    /// following, `f` toggles following explicitly, `w` toggles wrapping,
    /// and `n`/`N` step toward the newest or oldest matching line.
    pub(in crate::lookout::app) fn on_bleats_key(&mut self, key: KeyPress) -> Effect {
        // `o`, `m` and `/`: claimed ahead of this keymap by [`Self::on_feed_key`],
        // the dispatcher the sheep pane's embedded feed calls as well, so the
        // two screens cannot answer one of them differently.
        if let Some(effect) = self.on_feed_key(key, Self::bleats_pane_mut) {
            return effect;
        }
        match key {
            KeyPress::Quit => Effect::Quit,
            KeyPress::Escape => {
                let dropped_a_chip = self
                    .bleats_pane_mut()
                    .is_some_and(BleatsPane::drop_newest_chip);
                if !dropped_a_chip {
                    self.close_pane();
                }
                Effect::None
            }
            // Named here only to keep this keymap exhaustive: the guard above
            // already answered all three. A wildcard would let a new
            // `KeyPress` variant fall through unnoticed, which is what the
            // long inert arms below exist to prevent; `config_pane.rs` names
            // the `Quit` its own guard took for the same reason.
            KeyPress::FilterStart | KeyPress::StreamCycle | KeyPress::LevelCycle => Effect::None,
            // `k`/`Up`: one line toward older lines.
            KeyPress::SelectUp => {
                self.scroll_bleats_back(1);
                Effect::None
            }
            // `j`/`Down`: one line toward the newest.
            KeyPress::SelectDown => {
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.scroll_down(1);
                }
                Effect::None
            }
            // `G`/`End`: the tail, and following resumes — that is what an
            // operator means by "go to the end".
            KeyPress::SelectLast => {
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.jump_to_end();
                }
                Effect::None
            }
            // `ctrl-u`: a page toward older lines. Sized through
            // `page_amount_up` rather than `pane.body_rows()` directly: see
            // that function's own doc for why a page is a line count under
            // wrap, not a raw row count.
            KeyPress::PageUp => {
                let amount = self
                    .bleats_pane()
                    .map_or(1, |pane| crate::lookout::view::bleats_full::page_amount_up(self, pane));
                self.scroll_bleats_back(amount);
                Effect::None
            }
            // `ctrl-d`: toward the newest line, and sized by its own
            // function. The backward count `ctrl-u` uses drops lines when
            // applied forward; see `page_amount_up`'s doc.
            KeyPress::PageDown => {
                let amount = self
                    .bleats_pane()
                    .map_or(1, |pane| crate::lookout::view::bleats_full::page_amount_down(self, pane));
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.page_down(amount);
                }
                Effect::None
            }
            // `f`: toggles following explicitly.
            KeyPress::FollowToggle => {
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.toggle_follow();
                }
                Effect::None
            }
            // `w`: toggles whether a long line wraps or truncates.
            KeyPress::WrapToggle => {
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.toggle_wrap();
                }
                Effect::None
            }
            // `n`: one match toward the newest line. A no-op with no match
            // axis set: see `BleatsPane::match_next`.
            KeyPress::MatchNext => {
                if let Some(pane) = self.bleats_pane_mut() {
                    pane.match_next();
                }
                Effect::None
            }
            // `N`: the same, toward the oldest matching line, through
            // `scroll_bleats_back` so it takes the ceiling `k` and `ctrl-u`
            // take. Calling `match_prev` directly would climb past the
            // oldest surviving match and then `n`, `j` and `ctrl-d` would
            // all stop appearing to work until the offset drained. The
            // matcher guard is read here because the scroll happens here.
            KeyPress::MatchPrev => {
                let stepping = self
                    .bleats_pane()
                    .is_some_and(|pane| pane.filters().match_text().is_some());
                if stepping {
                    self.scroll_bleats_back(1);
                }
                Effect::None
            }
            // There is nothing on this screen `g`/`Home` can move to: the
            // window has no fixed start, only a tail. Left unbound rather
            // than aliased to `ctrl-u`'s page, which would give one key two
            // different meanings depending on how far a page happens to be.
            KeyPress::Help => self.open_keymap(),
            KeyPress::SelectFirst
            | KeyPress::Refresh
            | KeyPress::Action(_)
            | KeyPress::Confirm
            // Reach here only from text mode, already branched above in
            // `on_key`; listed so a new `KeyPress` variant cannot fall
            // silently into an arm that ignores it.
            | KeyPress::TextChar(_)
            | KeyPress::TextBackspace
            | KeyPress::TextEdit(_)
            | KeyPress::TextApply
            | KeyPress::TextAbandon
            | KeyPress::Settings
            | KeyPress::Cycle
            | KeyPress::Edit
            | KeyPress::Remove
            | KeyPress::StepUp
            | KeyPress::StepDown
            // `F` and `z` belong to the flock table. Regrouping a table the
            // operator cannot see, while a log pane owns the screen, is a
            // change they would meet on closing it.
            | KeyPress::FoldView
            | KeyPress::Collapse
            | KeyPress::Bleats
            // The secrets pane's own keys; nothing to move or reload while
            // the bleats pane owns the screen instead.
            | KeyPress::Secrets
            | KeyPress::Reveal
            | KeyPress::Copy
            | KeyPress::TabPrev
            | KeyPress::TabNext
            | KeyPress::SecretDelete => Effect::None,
            // The group keys, `Undo` and `Continue` belong to the config pane:
            // no other screen has groups, a filed edit set or a close dialog.
            | KeyPress::NextGroup
            | KeyPress::PrevGroup
            | KeyPress::Group(_)
            | KeyPress::Undo
            | KeyPress::Continue => Effect::None,
        }
    }

    /// Scrolls the bleats pane back by `amount`, then holds the offset at the
    /// last value that changes the frame.
    ///
    /// The ceiling has to live here rather than on the pane: it depends on
    /// the surviving lines and their wrapped heights, which the pane cannot
    /// see. Without it `scroll_up` saturating-adds forever and `j` stops
    /// appearing to work, since the render clamps while the stored value
    /// keeps climbing.
    fn scroll_bleats_back(&mut self, amount: usize) {
        let ceiling = self.bleats_pane().map_or(0, |pane| {
            crate::lookout::view::bleats_full::max_scroll_offset(self, pane)
        });
        if let Some(pane) = self.bleats_pane_mut() {
            pane.scroll_up(amount);
            pane.clamp_scroll(ceiling);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lookout::view::fixtures;

    /// `Escape` with a chip set drops the chip and leaves the pane open; only
    /// the next one closes it. Both halves in one test, because the guard in
    /// `on_bleats_key` is invisible to a test that never sets a chip: with a
    /// freshly opened pane `drop_newest_chip` returns `false` either way, so
    /// removing the guard entirely still passes every other `esc` test in the
    /// pane. Measured, not assumed.
    #[test]
    fn esc_drops_a_chip_before_it_closes_the_pane() {
        let mut app =
            fixtures::with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
        let _ = app.update(Msg::Key(KeyPress::Bleats));
        app.bleats_pane_mut()
            .expect("the pane is open")
            .set_min_level(Some(crate::lookout::level::Level::Warn));

        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert!(
            matches!(app.body(), Body::Bleats(_)),
            "the first Escape spends the chip and keeps the pane"
        );
        assert!(
            app.bleats_pane()
                .expect("still open")
                .filters()
                .min_level
                .is_none(),
            "the chip it spent was the level one"
        );

        let _ = app.update(Msg::Key(KeyPress::Escape));
        assert!(
            matches!(app.body(), Body::FlockTable),
            "with no chips left the next Escape closes"
        );
    }

    /// `o` cycles the stream axis through its three states and back. Three
    /// presses return to where it began, which is what makes it a cycle
    /// rather than a toggle that strands the operator on `err`.
    #[test]
    fn o_cycles_the_stream_axis_and_returns_to_both() {
        let mut app =
            fixtures::with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
        let _ = app.update(Msg::Key(KeyPress::Bleats));
        assert!(app.bleats_pane().expect("open").filters().stream.is_none());
        let _ = app.update(Msg::Key(KeyPress::StreamCycle));
        let first = app.bleats_pane().expect("open").filters().stream;
        assert!(first.is_some(), "one press sets an axis");
        let _ = app.update(Msg::Key(KeyPress::StreamCycle));
        let _ = app.update(Msg::Key(KeyPress::StreamCycle));
        assert!(
            app.bleats_pane().expect("open").filters().stream.is_none(),
            "three presses land back on both"
        );
    }

    /// `m` raises the minimum level and eventually clears it. The unset state
    /// has to be reachable by key, or an operator who sets a minimum can never
    /// see unlevelled output again without closing the pane.
    #[test]
    fn m_cycles_the_level_axis_back_to_unset() {
        let mut app =
            fixtures::with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
        let _ = app.update(Msg::Key(KeyPress::Bleats));
        let mut seen_some = false;
        for _ in 0..8 {
            let _ = app.update(Msg::Key(KeyPress::LevelCycle));
            if app
                .bleats_pane()
                .expect("open")
                .filters()
                .min_level
                .is_some()
            {
                seen_some = true;
            }
        }
        assert!(seen_some, "the cycle passes through a set minimum");
        // Whatever the cycle length, it must return to unset within one lap.
        let mut cleared = false;
        for _ in 0..8 {
            let _ = app.update(Msg::Key(KeyPress::LevelCycle));
            if app
                .bleats_pane()
                .expect("open")
                .filters()
                .min_level
                .is_none()
            {
                cleared = true;
                break;
            }
        }
        assert!(cleared, "the cycle returns to unset");
    }

    /// Scrolling back stops the follow, or the next refresh undoes the
    /// operator's keypress.
    #[test]
    fn scrolling_back_stops_following() {
        let mut app = fixtures::bleats_pane_with_lines(120);
        assert!(app.bleats_pane().expect("open").following());
        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        assert!(
            !app.bleats_pane().expect("open").following(),
            "one line back is enough to mean the operator took over"
        );
    }

    /// `G` is the way back to the live tail, so it restores following as well
    /// as jumping.
    #[test]
    fn g_returns_to_the_end_and_resumes_following() {
        let mut app = fixtures::bleats_pane_with_lines(120);
        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        let _ = app.update(Msg::Key(KeyPress::SelectLast));
        let pane = app.bleats_pane().expect("open");
        assert!(pane.following(), "G resumes the follow");
        assert_eq!(pane.scroll_offset(), 0, "and lands on the newest line");
    }

    /// A filter that hides most of the window must not leave the offset
    /// pointing past the end of what survives.
    #[test]
    fn a_narrowing_filter_clamps_the_scroll_offset() {
        let mut app = fixtures::bleats_pane_with_lines(120);
        let _ = app.update(Msg::Key(KeyPress::PageUp));
        let _ = app.update(Msg::Key(KeyPress::PageUp));

        // One survivor, far fewer than the offset two pages back. Without
        // the clamp the skip underflows: it panics in debug and wraps in
        // release, and a wrapped skip yields no feed lines at all.
        app.bleats_pane_mut_for_tests()
            .expect("open")
            .set_match("line-119".to_string());
        let text = fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(
            &app, 160, 40,
        ));

        // Asserts a rendered FEED line, tagged `out`, not merely that the
        // text appears: the filter row echoes the matcher back as its own
        // `match line-119` chip, so `contains("line-119")` passes even when
        // every feed line was skipped away. `!text.is_empty()` was weaker
        // still, and passed in release with the clamp removed because the
        // title and the chip alone kept the buffer non-empty.
        assert!(
            text.contains("out  line-119"),
            "the one surviving line is drawn in the body: {text}"
        );
    }

    /// `SelectUp`/`SelectDown` actually move the window, not just the
    /// `following` flag: pins which lines are on screen before and after,
    /// so a wrong direction or an off-by-one is caught rather than passing
    /// on the presence of any text at all.
    #[test]
    fn select_up_and_down_move_which_lines_are_on_screen() {
        let mut app = fixtures::bleats_pane_with_lines(20);
        app.note_body_rows(6); // 1 title row + 5 body rows
        let before =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert!(
            before.contains("line-19"),
            "starts pinned to the tail: {before}"
        );
        assert!(
            !before.contains("line-14"),
            "one line older than the window: {before}"
        );

        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        let after =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert!(
            after.contains("line-14") && !after.contains("line-19"),
            "one line back drops the newest line and reveals the one above the old window: {after}"
        );

        let _ = app.update(Msg::Key(KeyPress::SelectDown));
        let restored =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert_eq!(
            restored, before,
            "one line forward undoes the one line back"
        );
    }

    /// `ctrl-u`/`ctrl-d` move by a body height rather than a line, and the
    /// body height is what `note_body_rows` last reported, not a hardcoded
    /// guess.
    /// Two pages back leave no line unseen, with a filter chip on screen.
    ///
    /// The chip costs a row, so the body is two rows shorter than the pane,
    /// not one. A jump sized to the pane rather than the body skips a line
    /// between consecutive pages: it belongs to neither window and `ctrl-u`
    /// alone never renders it. Paging back down is symmetric either way,
    /// which is why the round trip looked fine and the gap did not.
    ///
    /// Asserts every line across the two windows, not the offset: an
    /// operator reading history by paging silently misses the gap, so a
    /// test that only watched the number move would too.
    /// Wrapped paging leaves no line unseen either, over a feed whose older
    /// lines are far longer than its newest.
    ///
    /// Measured: with the page sized from the tail, three steps into the
    /// wrapped stretch showed `old-32..old-35` where the step before showed
    /// `new-4..new-15`, skipping eight lines no screen ever drew, and the
    /// next step skipped eight more. Sized from the current window instead,
    /// the same walk is contiguous.
    ///
    /// That is the off-by-one page size one level harder: sized from the
    /// wrong place the jump was one row too many, here it is most of a
    /// screen. Both were invisible for the same reason, that paging back
    /// cancels the error out.
    ///
    /// `note_body_width` matters as much as `note_body_rows` here. The
    /// wrap-aware path is skipped entirely while the pane's width is `0`,
    /// which is what a test that never reports one leaves it as, so a test
    /// missing that call passes against a page size that was never wrapped.
    ///
    /// Walks in one direction and asserts every line across the windows,
    /// because paging back is symmetric and would hide the gap.
    /// Over-scrolling does not make `j` stop working.
    ///
    /// `scroll_up` saturating-adds and the clamp lived only in the render, so
    /// the stored offset climbed past anything that changes the frame.
    /// Measured before the ceiling: a 20-line feed with a 5-row body left the
    /// offset at 39 after 40 `k` presses, where 15 was the most that did
    /// anything, and the operator then pressed `j` 25 times before the window
    /// moved. `G` and `f` escape that; `j` is the reflex and it did nothing.
    /// Holding `N` past the oldest match does not deaden `n` either.
    ///
    /// `k` and `ctrl-u` route through the clamp; `N` called `match_prev`
    /// directly and climbed past it. The existing `n`/`N` test presses `N`
    /// once, so it never reached the ceiling.
    #[test]
    fn over_stepping_back_through_matches_does_not_deaden_the_step_forward() {
        let mut app = fixtures::bleats_pane_with_lines(20);
        app.note_body_rows(6);
        app.note_body_width(80);
        app.bleats_pane_mut_for_tests()
            .expect("open")
            .set_match("line".to_string());

        for _ in 0..40 {
            let _ = app.update(Msg::Key(KeyPress::MatchPrev));
        }
        let parked =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        let _ = app.update(Msg::Key(KeyPress::MatchNext));
        let after_one =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert_ne!(parked, after_one, "one step forward has to move the window");
    }

    #[test]
    fn over_scrolling_back_does_not_deaden_the_scroll_forward() {
        let mut app = fixtures::bleats_pane_with_lines(20);
        app.note_body_rows(6);
        app.note_body_width(80);

        for _ in 0..40 {
            let _ = app.update(Msg::Key(KeyPress::SelectUp));
        }
        let parked =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));

        let _ = app.update(Msg::Key(KeyPress::SelectDown));
        let after_one =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert_ne!(
            parked, after_one,
            "one press forward has to move the window, however far back the \
             operator scrolled"
        );
    }

    #[test]
    fn wrapped_pages_leave_no_line_unseen() {
        let mut app = fixtures::bleats_pane_with_mixed_line_lengths();
        let _ = app.update(Msg::Key(KeyPress::WrapToggle));
        app.note_body_rows(13);
        app.note_body_width(60);

        let mut seen = String::new();
        for _ in 0..12 {
            seen.push_str(&fixtures::render_all(
                &crate::lookout::view::bleats_full::draw_lines(&app, 60, 13),
            ));
            let _ = app.update(Msg::Key(KeyPress::PageUp));
        }
        for n in 30..40 {
            assert!(
                seen.contains(&format!("old-{n} ")),
                "old-{n} fell between two wrapped pages"
            );
        }
    }

    /// The same, walking `ctrl-d` instead. Its own test because its own
    /// arithmetic: a single shared page size drops lines in one direction
    /// whichever way it is measured.
    ///
    /// This one was missing when the wrapped-page fix landed, and that is
    /// exactly why the fix was half a fix. The sibling test above presses
    /// only `PageUp`, so a `PageDown` that skipped eight lines a step passed
    /// the whole suite.
    #[test]
    fn wrapped_pages_down_leave_no_line_unseen() {
        /// The `old-N`/`new-N` ids the pane is drawing, in order.
        fn shown(app: &App) -> Vec<String> {
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(app, 60, 13))
                .split_whitespace()
                .filter(|word| word.starts_with("old-") || word.starts_with("new-"))
                .map(str::to_string)
                .collect()
        }

        let mut app = fixtures::bleats_pane_with_mixed_line_lengths();
        let _ = app.update(Msg::Key(KeyPress::WrapToggle));
        app.note_body_rows(13);
        app.note_body_width(60);

        // Into the wrapped stretch, then forward one page at a time.
        for _ in 0..6 {
            let _ = app.update(Msg::Key(KeyPress::PageUp));
        }

        // Consecutive windows, not eventual coverage. A long enough walk
        // sees every line whatever the step size, so the gap only shows in
        // the join between one window and the next.
        for step in 0..5 {
            let before = shown(&app);
            let _ = app.update(Msg::Key(KeyPress::PageDown));
            let after = shown(&app);
            let (Some(last), Some(first)) = (before.last(), after.first()) else {
                continue;
            };
            // The feed is `old-0..old-39` then `new-0..new-39`, so this is
            // each id's position in it.
            let position = |id: &str| -> usize {
                let (prefix, n) = id.split_once('-').expect("id-N");
                let n: usize = n.parse().expect("a number");
                if prefix == "old" { n } else { 40 + n }
            };
            assert!(
                position(first) <= position(last) + 1,
                "step {step} jumped from {last} to {first}, leaving a gap: \
                 {before:?} then {after:?}"
            );
            // Two-sided. The check above catches a page that steps over
            // lines; this one catches a page that barely steps at all, which
            // `page_amount_down` returning a constant 1 would do while
            // satisfying the first assertion trivially.
            assert!(
                position(first) + 1 >= position(before[0].as_str()) + before.len(),
                "step {step} moved by almost nothing, so it is not a page: \
                 {before:?} then {after:?}"
            );
        }
    }

    #[test]
    fn consecutive_pages_leave_no_line_unseen_while_a_chip_is_showing() {
        let mut app = fixtures::bleats_pane_with_lines(40);
        app.bleats_pane_mut_for_tests()
            .expect("open")
            .set_match("line".to_string());
        app.note_body_rows(6); // 1 title + 1 filter row + 4 body rows

        let first =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        let _ = app.update(Msg::Key(KeyPress::PageUp));
        let second =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        let _ = app.update(Msg::Key(KeyPress::PageUp));
        let third =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));

        let seen = format!("{first}{second}{third}");
        // The three windows are contiguous, so every line from the oldest
        // one drawn through the newest must appear in one of them.
        for n in 28..=39 {
            assert!(
                seen.contains(&format!("line-{n}")),
                "line-{n} fell between two pages: {seen}"
            );
        }
    }

    #[test]
    fn page_up_and_down_move_by_a_body_height() {
        let mut app = fixtures::bleats_pane_with_lines(20);
        app.note_body_rows(6); // 1 title row + 5 body rows
        let before =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert!(
            before.contains("line-19"),
            "starts pinned to the tail: {before}"
        );

        let _ = app.update(Msg::Key(KeyPress::PageUp));
        let after =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert!(
            after.contains("line-10") && !after.contains("line-15"),
            "a page is 5 body rows, so one page up lands on 10..=14, not 1 row back: {after}"
        );
        assert!(
            !app.bleats_pane().expect("open").following(),
            "ctrl-u is backward movement too"
        );

        let _ = app.update(Msg::Key(KeyPress::PageDown));
        let restored =
            fixtures::render_all(&crate::lookout::view::bleats_full::draw_lines(&app, 80, 6));
        assert_eq!(restored, before, "one page down undoes one page up");
    }

    /// `f` toggles following explicitly, and turning it back on snaps to the
    /// tail rather than leaving the view wherever it was scrolled to.
    #[test]
    fn f_toggles_following_and_resuming_snaps_to_the_tail() {
        let mut app = fixtures::bleats_pane_with_lines(120);
        let _ = app.update(Msg::Key(KeyPress::SelectUp));
        assert!(!app.bleats_pane().expect("open").following());

        let _ = app.update(Msg::Key(KeyPress::FollowToggle));
        let pane = app.bleats_pane().expect("open");
        assert!(pane.following(), "f turned it back on");
        assert_eq!(pane.scroll_offset(), 0, "and snapped to the tail");

        let _ = app.update(Msg::Key(KeyPress::FollowToggle));
        assert!(
            !app.bleats_pane().expect("open").following(),
            "f is a toggle, not a one-way switch"
        );
    }

    /// `n` with no match axis set does nothing, rather than quietly becoming
    /// a line-movement key.
    /// `n` and `N` step between matches, in opposite directions, and both
    /// stop the follow.
    ///
    /// The no-matcher case below covers only the inert branch, so a swapped
    /// direction, a wrong step, or a `following` regression would all have
    /// shipped unseen.
    /// A wrapped line's height is measured in display columns, not `char`s.
    ///
    /// Sixty full-width characters occupy 120 columns, so at this width they
    /// wrap to about twice the rows sixty ASCII characters would. Every other
    /// wrap test here is ASCII, where the two counts agree and a regression
    /// to `chars().count()` would pass unnoticed. This repo has fixed that
    /// same bug on two other branches.
    #[test]
    fn a_wide_character_line_wraps_by_columns_not_char_count() {
        let mut wide = fixtures::bleats_pane_with_a_wide_line();
        let _ = wide.update(Msg::Key(KeyPress::WrapToggle));
        wide.note_body_rows(30);
        wide.note_body_width(40);
        let wide_rows = crate::lookout::view::bleats_full::draw_lines(&wide, 40, 30).len();

        // 60 double-width characters are 120 display columns. The body is
        // 40 wide less the 5-column stream tag, so 35, and 120 columns need
        // 4 rows. Counting `char`s instead gives 60 over 35, which is 2.
        // The title takes one more row, so 5 total by columns and 3 by
        // `char`s: the assertion separates the two.
        assert!(
            wide_rows >= 5,
            "wrapped by columns that is 4 body rows plus a title; by \
             `char`s it would be 2. Got {wide_rows}"
        );
    }

    /// Wrapping a long line makes it occupy more rows than one, which is the
    /// whole point, and the pane must still draw inside its area.
    #[test]
    fn a_wrapped_line_occupies_more_rows_and_stays_in_the_area() {
        let mut app = fixtures::bleats_pane_with_long_line();
        let unwrapped = fixtures::draw_lines(&app, 80, 20).len();
        let _ = app.update(Msg::Key(KeyPress::WrapToggle));
        let wrapped = fixtures::draw_lines(&app, 80, 20);
        assert!(wrapped.len() <= 20, "never draws past its own height");
        assert!(
            wrapped.iter().filter(|line| !line.spans.is_empty()).count() >= unwrapped,
            "wrapping uses at least as many rows as not wrapping"
        );
    }

    /// The brief's own test above (`a_wrapped_line_occupies_more_rows_and_stays_in_the_area`)
    /// only asserts `>=`, which a `w` that did nothing at all would still
    /// satisfy: the unwrapped and "wrapped" renders would be identical, and
    /// identical passes `>=` too. This pins the number changing, not merely
    /// never shrinking.
    #[test]
    fn toggling_wrap_actually_changes_how_many_rows_a_long_line_draws() {
        let mut app = fixtures::bleats_pane_with_long_line();
        let unwrapped = fixtures::draw_lines(&app, 80, 20).len();
        let _ = app.update(Msg::Key(KeyPress::WrapToggle));
        let wrapped = fixtures::draw_lines(&app, 80, 20).len();
        assert!(
            wrapped > unwrapped,
            "wrap must add rows for a line too long for one, not just permit them: \
             {unwrapped} unwrapped rows, {wrapped} wrapped: got the same window"
        );
    }

    /// `o` and `m` are global bindings, so the dashboard sees them too; with
    /// no bleats pane open there is no stream or level axis to cycle, and
    /// the reducer says so explicitly rather than falling through to a
    /// wildcard, the same way it already does for `KeyPress::Bleats` here.
    #[test]
    fn stream_and_level_cycle_are_inert_on_the_dashboard() {
        let mut app =
            fixtures::with_selection(ProcessInfo::builder(9, "web", ProcStatus::Online).build());
        assert_eq!(app.update(Msg::Key(KeyPress::StreamCycle)), Effect::None);
        assert_eq!(app.update(Msg::Key(KeyPress::LevelCycle)), Effect::None);
        assert!(matches!(app.body(), Body::FlockTable));
    }
}