plannotator-tui 0.14.0

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
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
//! Notes drawn in the diff (`[review] notes = "inline"` or `"both"`): a box under each note's
//! last line, over its side of a split diff, stacked in the order made; the file note under
//! its heading; what the cursor, the keys and the mouse do on a box; and the default,
//! `panel`, drawing exactly as before. On the long four-file fixture at 160×45.

use plannotator_tui_review::{CodeAnnotation, Side};
use ratatui::crossterm::event::{KeyCode, MouseButton, MouseEventKind};
use ratatui::style::Modifier;

use super::compose_near::{COLS, ROWS, draw, long, report, the_box};
use super::inline_support::{box_saying, cells, check_boxes, comment, row_of_spot, spot};
use super::whitespace_flow;
use super::*;
use crate::config::review::Notes;
use crate::review::{Draft, Mode};
use crate::theme::palette;

pub(super) const SIZE: (u16, u16) = (COLS, ROWS);

/// The long fixture opened with `[review]` as `config` says, the cursor on new line 30 of
/// file0.py, near the bottom: new lines 8 to 30 are on screen.
pub(super) fn opened(tag: &str, config: &str) -> (Fixture, Screen) {
    let fixture = long(tag, false);
    fixture.write_config(&format!("[review]\n{config}\n"));
    let mut screen = fixture.open_configured();
    draw(&mut screen, SIZE);
    screen.diff.reveal("file0.py", Side::New, 30).expect("line 30");
    draw(&mut screen, SIZE);
    (fixture, screen)
}

/// The Notes setting to `notes`, by the settings menu.
pub(super) fn set_notes(screen: &mut Screen, notes: Notes) {
    let row = crate::review::settings::ROWS
        .iter()
        .position(|row| *row == crate::review::settings::Row::Notes)
        .expect("a Notes row");
    keys(screen, ",");
    for _ in 0..row {
        keys(screen, "j");
    }
    while screen.settings.now.notes != notes {
        press(screen, KeyCode::Right);
    }
    press(screen, KeyCode::Esc);
}

/// The diff pane's cells in `buffer`.
fn pane(screen: &Screen, buffer: &ratatui::buffer::Buffer) -> Vec<String> {
    let pane = screen.geometry.diff;
    (pane.top()..pane.bottom()).map(|y| cells(buffer, pane.x, y, pane.width)).collect()
}

pub(super) fn all(buffer: &ratatui::buffer::Buffer) -> String {
    (0..buffer.area.height).map(|y| cells(buffer, 0, y, buffer.area.width)).collect::<Vec<_>>().join("\n")
}

#[test]
fn the_panel_setting_keeps_no_rows_for_notes_and_draws_as_before() {
    let (_fixture, mut screen) = opened("inline-panel", "layout = \"split\"");
    let before = draw(&mut screen, SIZE);
    comment(&mut screen, "file0.py", Side::New, (9, 10), "a panel note");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "an old one");
    let noted = draw(&mut screen, SIZE);
    assert!(screen.diff.boxes_set().is_empty(), "no rows kept");
    assert!(screen.geometry.boxes.is_empty(), "none drawn");
    assert!(!all(&noted).contains("comment \u{b7} L"), "no box:\n{}", all(&noted));
    // Inline and back: the diff is the one panel drew.
    set_notes(&mut screen, Notes::Inline);
    assert!(!screen.diff.boxes_set().is_empty(), "inline keeps rows");
    draw(&mut screen, SIZE);
    set_notes(&mut screen, Notes::Panel);
    let back = draw(&mut screen, SIZE);
    assert!(screen.diff.boxes_set().is_empty(), "panel again keeps none");
    assert_eq!(pane(&screen, &back), pane(&screen, &noted), "panel again draws what it drew");
    assert_ne!(pane(&screen, &before), pane(&screen, &noted), "the notes' marks");
}

#[test]
fn boxes_hang_under_their_notes_last_line_on_its_side_in_the_order_made() {
    let (_fixture, mut screen) = opened("inline-under", "layout = \"unified\"\nnotes = \"inline\"");
    comment(&mut screen, "file0.py", Side::New, (10, 12), "first range");
    comment(&mut screen, "file0.py", Side::New, (12, 12), "second on twelve");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "old twelve");
    comment(&mut screen, "file1.py", Side::New, (12, 12), "other file twelve");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "unified");
    let first = box_saying(&screen, "first range").expect("drawn");
    let second = box_saying(&screen, "second on twelve").expect("drawn");
    assert_eq!(second.y, first.bottom(), "stacked under the earlier one");
    assert!(cells(&buffer, first.x, first.y, first.width).contains("comment \u{b7} L10\u{2013}12"));
    assert!(box_saying(&screen, "old twelve").is_none(), "the removed lines are further up");
    // Unified, a box starts at the code's first column, as wide as the code or 84.
    let area = screen.diff.box_areas().first().map(|a| a.area).expect("an area");
    assert_eq!((first.x, first.width), (area.x, area.width.min(84)));
    // The border is the notes' colour, the title bold.
    let corner = buffer.cell((first.x, first.y)).expect("corner");
    assert_eq!(corner.fg, palette().comment);
    // The old line's box is under the removed line 12, with the old side named.
    screen.diff.reveal("file0.py", Side::Old, 12).expect("old line 12");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "old");
    let old = box_saying(&screen, "old twelve").expect("drawn");
    assert!(cells(&buffer, old.x, old.y, old.width).contains("comment \u{b7} L12 old"));
    // The other file's box is under its own line 12.
    screen.diff.reveal("file1.py", Side::New, 16).expect("file1 line 16");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "file1");
    assert!(box_saying(&screen, "other file twelve").is_some());
    assert!(box_saying(&screen, "first range").is_none(), "file0 is off screen");
}

#[test]
fn split_boxes_sit_side_by_side_over_their_sides_or_across_when_a_side_is_narrow() {
    let (_fixture, mut screen) = opened("inline-split", "layout = \"split\"\nnotes = \"inline\"");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "old side note");
    comment(&mut screen, "file0.py", Side::New, (12, 12), "new side note");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "second old note");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "split 50");
    let line = screen.diff.split_line().expect("split");
    let old = box_saying(&screen, "old side note").expect("old");
    let new = box_saying(&screen, "new side note").expect("new");
    let second = box_saying(&screen, "second old note").expect("second");
    assert!(old.right() <= line, "the old box over the old side: {old:?} {line}");
    assert!(new.x > line, "the new box over the new side: {new:?} {line}");
    assert_eq!(new.y, old.y, "side by side, from the same row");
    assert_eq!((second.x, second.y), (old.x, old.bottom()), "the next old one under it");
    let pane = screen.geometry.diff;
    let under = (pane.top()..pane.bottom())
        .find(|&y| screen.diff.row_at(pane.right() - 2, y).is_some_and(|row| row.new_line == Some(13)))
        .expect("line 13");
    assert_eq!(under, second.bottom(), "no rows besides the block's");
    // 30:70 leaves the old side under 48 columns: its boxes run across, each its own block.
    keys(&mut screen, "((((");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "split 30");
    let line = screen.diff.split_line().expect("split");
    let old = box_saying(&screen, "old side note").expect("old");
    let new = box_saying(&screen, "new side note").expect("new");
    let second = box_saying(&screen, "second old note").expect("second");
    assert!(old.x < line && old.right() > line, "across from the old code: {old:?} {line}");
    assert!(new.x > line && new.y == old.bottom(), "the new one over its side, below: {new:?}");
    assert_eq!(second.y, new.bottom(), "in the order made");
}

#[test]
fn the_cursor_stops_on_side_boxes_left_column_first_and_takes_their_side() {
    let (_fixture, mut screen) = opened("inline-stops", "layout = \"split\"\nnotes = \"inline\"");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "first old");
    comment(&mut screen, "file0.py", Side::New, (12, 12), "the new one");
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "second old");
    screen.diff.reveal("file0.py", Side::New, 12).expect("line 12");
    draw(&mut screen, SIZE);
    let side = |screen: &Screen| screen.diff.cursor_row().map(|row| row.side);
    let on = |screen: &Screen| screen.cursor_note().and_then(|n| n.text.clone());
    let mut stops = Vec::new();
    for _ in 0..4 {
        press(&mut screen, KeyCode::Char('j'));
        draw(&mut screen, SIZE);
        stops.push((on(&screen), side(&screen)));
    }
    let old = Some(diffs_rs::engine::Side::Old);
    let new = Some(diffs_rs::engine::Side::New);
    assert_eq!(
        stops,
        [
            (Some("first old".to_owned()), old),
            (Some("second old".to_owned()), old),
            (Some("the new one".to_owned()), new),
            (None, new),
        ],
        "left column top to bottom, then the right; leaving it on the new side"
    );
    assert_eq!(screen.diff.cursor_row().and_then(|row| row.new_line), Some(13));
    // Back up onto the block, then across to the box beside.
    keys(&mut screen, "kkk");
    assert_eq!(on(&screen).as_deref(), Some("first old"));
    press(&mut screen, KeyCode::Char('l'));
    assert_eq!((on(&screen).as_deref(), side(&screen)), (Some("the new one"), new), "the box beside");
}

#[test]
fn a_click_on_either_of_two_boxes_side_by_side_picks_its_own_note() {
    let (_fixture, mut screen) = opened("inline-click-sides", "layout = \"split\"\nnotes = \"both\"");
    open_notes(&mut screen);
    comment(&mut screen, "file0.py", Side::Old, (12, 12), "left note");
    comment(&mut screen, "file0.py", Side::New, (12, 12), "right note");
    draw(&mut screen, SIZE);
    for text in ["right note", "left note", "right note"] {
        let boxed = box_saying(&screen, text).expect("drawn");
        let at = (boxed.x + 3, boxed.y + 1);
        report(&mut screen, MouseEventKind::Down(MouseButton::Left), at);
        report(&mut screen, MouseEventKind::Up(MouseButton::Left), at);
        draw(&mut screen, SIZE);
        assert_eq!(screen.cursor_note().and_then(|n| n.text.as_deref()), Some(text));
        assert_eq!(screen.selected_note().and_then(|n| n.text.as_deref()), Some(text), "the panel");
        assert_eq!(screen.diff.selection(), None);
    }
}

#[test]
fn a_new_comment_lines_up_with_the_boxes() {
    for layout in ["unified", "split"] {
        let (_fixture, mut screen) =
            opened("inline-compose-x", &format!("layout = \"{layout}\"\nnotes = \"inline\""));
        comment(&mut screen, "file0.py", Side::New, (12, 12), "a new-side box");
        comment(&mut screen, "file0.py", Side::Old, (14, 14), "an old-side box");
        draw(&mut screen, SIZE);
        // Two lines under each box's line, on its side.
        for (side, line, text) in [(Side::New, 12, "a new-side box"), (Side::Old, 14, "an old-side box")] {
            screen.diff.reveal("file0.py", side, line).expect("the line");
            keys(&mut screen, "jjj");
            draw(&mut screen, SIZE);
            let boxed = box_saying(&screen, text).expect("drawn");
            press(&mut screen, KeyCode::Char('c'));
            let buffer = draw(&mut screen, SIZE);
            let compose = the_box(&buffer, "comment on");
            assert_eq!((compose.x, compose.width), (boxed.x, boxed.width), "{layout} {side:?}");
            press(&mut screen, KeyCode::Esc);
            draw(&mut screen, SIZE);
        }
    }
}

#[test]
fn a_file_note_sits_under_its_heading_and_notes_with_no_box_are_counted() {
    let (_fixture, mut screen) = opened("inline-file", "layout = \"unified\"\nnotes = \"inline\"");
    keys(&mut screen, "C");
    type_and_save(&mut screen, "whole file thoughts");
    keys(&mut screen, "M");
    type_and_save(&mut screen, "about everything");
    screen.diff.show_heading(0).expect("heading");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "file note");
    let boxed = box_saying(&screen, "whole file thoughts").expect("drawn");
    assert!(cells(&buffer, boxed.x, boxed.y, boxed.width).contains("file comment"));
    let footer = all(&buffer);
    assert!(footer.contains("1 note not shown inline \u{b7} z"), "{footer}");
    open_notes(&mut screen);
    let buffer = draw(&mut screen, SIZE);
    assert!(all(&buffer).contains("1 note not shown inline") && !all(&buffer).contains("inline \u{b7} z"));
}

#[test]
fn inline_starts_without_the_panel_whatever_the_notes_and_both_keeps_its_rule() {
    let (fixture, mut screen) = opened("inline-rail", "notes = \"inline\"");
    comment(&mut screen, "file0.py", Side::New, (9, 9), "a note");
    let mut again = fixture.open_configured();
    draw(&mut again, SIZE);
    assert!(!again.rail_shown(), "inline: no panel at the start, notes or not");
    press(&mut again, KeyCode::Char('z'));
    assert!(again.rail_shown(), "z opens it");
    press(&mut again, KeyCode::Char('z'));
    assert!(!again.rail_shown());
    press(&mut again, KeyCode::Tab);
    assert!(again.rail_shown() && again.focus == crate::review::Focus::Notes, "Tab goes there");
    fixture.write_config("[review]\nnotes = \"both\"\n");
    let both = fixture.open_configured();
    assert!(both.rail_shown(), "both: the panel's rule, shown with notes");
    // Switching to inline puts the panel away; back to both brings it.
    let mut screen = both;
    draw(&mut screen, SIZE);
    set_notes(&mut screen, Notes::Inline);
    assert!(!screen.rail_shown());
    set_notes(&mut screen, Notes::Both);
    assert!(screen.rail_shown());
}

#[test]
fn on_a_box_enter_edits_it_over_the_box_delete_removes_it_and_x_stays_viewed() {
    let (_fixture, mut screen) = opened("inline-keys", "layout = \"split\"\nnotes = \"both\"");
    open_notes(&mut screen);
    comment(&mut screen, "file0.py", Side::New, (9, 9), "fix this");
    comment(&mut screen, "file0.py", Side::New, (20, 20), "other one");
    screen.diff.reveal("file0.py", Side::New, 9).expect("line 9");
    press(&mut screen, KeyCode::Char('j'));
    assert!(screen.cursor_note().is_some_and(|n| n.text.as_deref() == Some("fix this")), "on the box");
    let buffer = draw(&mut screen, SIZE);
    let boxed = box_saying(&screen, "fix this").expect("drawn");
    let corner = buffer.cell((boxed.x, boxed.y)).expect("corner");
    assert_eq!(corner.fg, palette().accent, "the cursor's box is lit");
    assert!(corner.modifier.contains(Modifier::BOLD));
    let text = all(&buffer);
    assert!(text.contains("enter/e edit") && text.contains("delete remove"), "{text}");
    // `x` is still viewed: the note stays.
    press(&mut screen, KeyCode::Char('x'));
    assert_eq!(screen.store.annotations().len(), 2);
    assert!(screen.status.as_deref().is_some_and(|s| s.contains("viewed")), "{:?}", screen.status);
    // Enter: the comment box over the note's box, with its text.
    press(&mut screen, KeyCode::Enter);
    assert!(matches!(&screen.mode, Mode::Compose(Draft::Edit(_))));
    assert_eq!(screen.compose.value(), "fix this");
    let buffer = draw(&mut screen, SIZE);
    // (`x` ticked the file in the list, which got wider: the box with it.)
    let boxed = box_saying(&screen, "fix this").expect("still drawn");
    let corner = cells(&buffer, boxed.x, boxed.y, 7);
    assert_eq!(corner, "\u{256d} edit ", "the comment box over the note's box");
    press(&mut screen, KeyCode::End);
    type_and_save(&mut screen, " now, and say why at some length so the box needs a second line of text");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "edited");
    assert!(screen.cursor_note().is_some(), "the cursor stays on the box");
    // `e` edits too; Esc keeps it.
    press(&mut screen, KeyCode::Char('e'));
    assert!(matches!(&screen.mode, Mode::Compose(Draft::Edit(_))));
    press(&mut screen, KeyCode::Esc);
    assert_eq!(screen.mode, Mode::Browse);
    // Delete removes it, and the cursor is back on its line.
    press(&mut screen, KeyCode::Delete);
    assert_eq!(screen.store.annotations().len(), 1);
    assert_eq!(screen.status.as_deref(), Some("note removed \u{b7} U undo"));
    assert_eq!(screen.diff.line_on(Side::New).expect("line"), Some(("file0.py", 9)));
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "removed");
    assert!(box_saying(&screen, "fix this").is_none());
}

#[test]
fn a_click_on_a_box_selects_its_note_and_a_drag_never_starts_or_lands_on_one() {
    let (_fixture, mut screen) = opened("inline-mouse", "layout = \"unified\"\nnotes = \"both\"");
    open_notes(&mut screen);
    comment(&mut screen, "file0.py", Side::New, (12, 12), "first");
    comment(&mut screen, "file0.py", Side::New, (20, 20), "second");
    draw(&mut screen, SIZE);
    let boxed = box_saying(&screen, "second").expect("drawn");
    let at = (boxed.x + 4, boxed.y + 1);
    report(&mut screen, MouseEventKind::Down(MouseButton::Left), at);
    assert!(screen.drag.is_none(), "no selection starts on a box");
    report(&mut screen, MouseEventKind::Up(MouseButton::Left), at);
    assert!(screen.cursor_note().is_some_and(|n| n.text.as_deref() == Some("second")));
    assert_eq!(screen.selected_note().and_then(|n| n.text.as_deref()), Some("second"), "the panel follows");
    assert_eq!(screen.diff.selection(), None);
    // A drag from line 11 down over line 12's box to 14 selects the lines only.
    let first = box_saying(&screen, "first").expect("drawn");
    let x = first.x + 10;
    let from = first.y - 2;
    report(&mut screen, MouseEventKind::Down(MouseButton::Left), (x, from));
    for y in from + 1..first.bottom() + 3 {
        report(&mut screen, MouseEventKind::Drag(MouseButton::Left), (x, y));
        draw(&mut screen, SIZE);
    }
    report(&mut screen, MouseEventKind::Up(MouseButton::Left), (x, first.bottom() + 2));
    let selected = screen.diff.selection().expect("a selection");
    assert_eq!((selected.side, selected.first), (Side::New, 11), "{selected:?}");
    assert!(selected.last >= 13, "past the box: {selected:?}");
}

#[test]
fn a_new_comment_opens_below_the_boxes_under_its_line_and_its_own_box_follows() {
    let (_fixture, mut screen) = opened("inline-compose", "layout = \"unified\"\nnotes = \"inline\"");
    comment(&mut screen, "file0.py", Side::New, (12, 12), "already here");
    screen.diff.reveal("file0.py", Side::New, 12).expect("line 12");
    let buffer = draw(&mut screen, SIZE);
    let existing = box_saying(&screen, "already here").expect("drawn");
    let before = spot(&screen);
    let row = row_of_spot(&screen, &before).expect("on screen");
    press(&mut screen, KeyCode::Char('c'));
    let buffer_open = draw(&mut screen, SIZE);
    let compose = the_box(&buffer_open, "comment on");
    assert_eq!(compose.y, existing.bottom(), "below the box, covering no code");
    drop(buffer);
    type_and_save(&mut screen, "one more");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "saved");
    let added = box_saying(&screen, "one more").expect("its box");
    assert_eq!(added.y, existing.bottom(), "under the earlier box");
    assert_eq!(row_of_spot(&screen, &before), Some(row), "the line kept its row");
}

#[test]
fn a_review_opened_with_notes_starts_at_the_top_with_the_first_files_box() {
    let (fixture, mut screen) = opened("inline-open", "notes = \"inline\"\nlayout = \"unified\"");
    keys(&mut screen, "C");
    type_and_save(&mut screen, "about file zero");
    comment(&mut screen, "file0.py", Side::Old, (1, 1), "on line one");
    let mut again = fixture.open_configured();
    let buffer = draw(&mut again, SIZE);
    check_boxes(&again, &buffer, "opened");
    let pane = again.geometry.diff;
    let top = again.diff.row_at(pane.right() - 2, pane.y).expect("the top row");
    assert_eq!(top.kind, diffs_rs::engine::DiffRowKind::Header, "the first heading on top");
    let file = box_saying(&again, "about file zero").expect("the file's box shows");
    assert_eq!(file.y, pane.y + 1, "right under it");
    // The cursor where a review opens without boxes: on the first heading.
    let cursor = again.diff.cursor_row().expect("a cursor");
    assert_eq!((cursor.file, cursor.kind), (0, diffs_rs::engine::DiffRowKind::Header));
    assert!(box_saying(&again, "on line one").is_some());
}

#[test]
fn a_note_from_the_agent_gets_its_box_credited_and_suggestions_show_their_code() {
    let (fixture, mut screen) = opened("inline-pi", "notes = \"inline\"\nlayout = \"unified\"");
    let file = fixture.repo().join("file0.py");
    let (note, _answer) = crate::pi::link::Note::for_test(file, None, Some(9), "check this");
    screen.take_pi_note(&note);
    let key = screen.capture.patch_key.clone();
    let patch = screen.file("file0.py").expect("file");
    let suggestion = CodeAnnotation::suggestion(patch, 11, 11, "f0_line_11 = 11 * 3".into(), None, &key);
    screen.add_note(suggestion).expect("saved");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "agent");
    let credited = box_saying(&screen, "check this").expect("drawn");
    assert!(
        cells(&buffer, credited.x, credited.y, credited.width)
            .contains("comment \u{b7} L9 \u{b7} pi \u{b7} test")
    );
    let text = all(&buffer);
    assert!(text.contains("suggestion \u{b7} L11"), "{text}");
    assert!(
        text.contains("- f0_line_11 = 11 * 2  # changed") && text.contains("+ f0_line_11 = 11 * 3"),
        "{text}"
    );
}

#[test]
fn a_box_in_a_hunk_hidden_with_whitespace_goes_is_counted_and_comes_back() {
    let fixture = whitespace_flow::fixture("inline-ws");
    fixture.write_config("[review]\nnotes = \"inline\"\nlayout = \"unified\"\n");
    let mut screen = fixture.open_configured();
    draw(&mut screen, SIZE);
    comment(&mut screen, "ws.go", Side::New, (21, 21), "trailing spaces here");
    comment(&mut screen, "ws.go", Side::New, (6, 6), "doubled");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "shown");
    assert!(box_saying(&screen, "trailing spaces here").is_some());
    assert!(!all(&buffer).contains("not shown inline"));
    keys(&mut screen, "W");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "whitespace hidden");
    assert!(box_saying(&screen, "trailing spaces here").is_none(), "its hunk is a heading now");
    assert!(box_saying(&screen, "doubled").is_some(), "the other stays");
    assert!(all(&buffer).contains("1 note not shown inline \u{b7} z"), "{}", all(&buffer));
    keys(&mut screen, "W");
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "whitespace shown");
    assert!(box_saying(&screen, "trailing spaces here").is_some(), "back");
    assert!(!all(&buffer).contains("not shown inline"));
}

#[test]
fn three_hundred_notes_lay_out_once_and_draw_only_what_shows() {
    let (_fixture, mut screen) = opened("inline-many", "layout = \"split\"\nnotes = \"inline\"");
    for i in 0..300u32 {
        let file = format!("file{}.py", i % 4);
        let line = 1 + (i / 4) % 89;
        let side = if i % 3 == 0 { Side::Old } else { Side::New };
        comment(&mut screen, &file, side, (line, line), &format!("note {i} with a few words to wrap"));
    }
    draw(&mut screen, SIZE);
    assert_eq!(screen.diff.boxes_set().len(), 300);
    let started = Instant::now();
    for _ in 0..40 {
        press(&mut screen, KeyCode::Char('j'));
        let buffer = draw(&mut screen, SIZE);
        assert!(screen.geometry.boxes.len() <= usize::from(ROWS), "only the boxes on screen");
        drop(buffer);
    }
    let buffer = draw(&mut screen, SIZE);
    check_boxes(&screen, &buffer, "300 notes");
    // Debug build, 41 frames with 300 boxes set: well under a second each.
    assert!(started.elapsed() < Duration::from_secs(20), "{:?}", started.elapsed());
}

#[test]
fn the_notes_setting_comes_from_the_config_file_is_remembered_and_reset() {
    let (fixture, mut screen) = opened("inline-setting", "notes = \"both\"");
    assert_eq!(screen.settings.now.notes, Notes::Both, "the config file's");
    set_notes(&mut screen, Notes::Inline);
    let mut next = fixture.open_configured();
    draw(&mut next, SIZE);
    assert_eq!(next.settings.now.notes, Notes::Inline, "remembered over the config file");
    keys(&mut next, ",");
    for _ in 0..crate::review::settings::ROWS.len() - 1 {
        keys(&mut next, "j");
    }
    press(&mut next, KeyCode::Enter);
    assert_eq!(next.settings.now.notes, Notes::Both, "Reset: the config file's again");
    fixture.write_config("[review]\nnotes = \"sideways\"\n");
    let review = crate::config::Config::load_from(&fixture.config());
    assert!(review.is_err_and(|e| format!("{e:#}").contains("sideways")), "a bad value is named");
}