plannotator-tui 0.15.1

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! The notes rail (`toggle-notes`, `[ui] notes_rail`): away while a document has no notes,
//! shown at the first, and once closed it stays closed, this session and the next. Hidden,
//! the text takes its columns and the notes keep their marks and their count.

#![allow(clippy::expect_used, clippy::panic, reason = "tests assert by panicking")]

use plannotator_tui_schema::Kind;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};

use super::review_test_support::{draw, file_app, folder_app, press, whole_list_note_app};
use super::{App, Focus};
use crate::notes_rail::{NotesRail, RailStart};

/// The fixture document with a note on "one" and one on "three".
fn noted_app(tag: &str) -> (std::path::PathBuf, App) {
    let (root, mut app, _) = file_app(tag);
    app.add_quote_annotation("one", Kind::Comment, "first note".to_owned()).expect("note");
    app.add_quote_annotation("three", Kind::Comment, "second note".to_owned()).expect("note");
    (root, app)
}

/// The rail's width at `width` columns, as the document screen sizes it.
fn rail(width: u16) -> u16 {
    (width * 3 / 10).clamp(28, 36)
}

fn screen_row<'a>(screen: &'a str, needle: &str) -> &'a str {
    screen.lines().find(|row| row.contains(needle)).unwrap_or_else(|| panic!("no {needle:?} in\n{screen}"))
}

fn add_comment_on_block_two(app: &mut App, text: &str) {
    press(app, 'g');
    press(app, 'j');
    press(app, 'j');
    press(app, 'c');
    app.handle_event(&Event::Paste(text.to_owned())).expect("paste");
    app.handle_event(&Event::Key(KeyEvent::from(KeyCode::Enter))).expect("enter");
    press(app, 'g');
}

#[test]
fn closing_the_rail_gives_the_text_its_columns_and_keeps_marks_and_counts() {
    let (root, mut app) = noted_app("rail-widths");
    app.ui.doc_width = 0;
    for width in [80, 120, 200] {
        app.notes_rail = NotesRail::new(RailStart::WhenNotes);
        let open = draw(&mut app, width, 20);
        assert!(open.contains("first note"), "at {width} the rail shows:\n{open}");
        assert_eq!(app.geometry.doc.width, width - 2 - 1 - rail(width), "at {width}");
        assert!(!open.contains("notes hidden"));

        press(&mut app, 'z');
        let hidden = draw(&mut app, width, 20);
        assert!(!hidden.contains("first note"), "at {width} the rail is gone:\n{hidden}");
        assert_eq!(app.geometry.doc.width, width - 2, "at {width} the text takes the rail's columns");
        assert_eq!(app.open.layout.width, usize::from(width - 2), "the text is laid out again");
        assert_eq!(app.geometry.bubbles.len(), 0, "no bubble is left to click");
        assert!(screen_row(&hidden, "one").starts_with('▍'), "the note's mark stays:\n{hidden}");
        assert!(screen_row(&hidden, "Send").contains("2 new"), "the header still counts:\n{hidden}");
        assert!(hidden.contains("2 annotations"));
        assert!(hidden.contains("notes hidden: z shows them"), "{hidden}");
        assert!(!hidden.contains("rail hidden: widen"));
    }
    press(&mut app, 'z');
    assert!(draw(&mut app, 120, 20).contains("first note"), "the same key shows it again");
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn the_reading_column_recentres_in_the_columns_a_closed_rail_leaves() {
    let (root, mut app) = noted_app("rail-column");
    draw(&mut app, 200, 20);
    let column_room = 200 - 2 - 1 - rail(200);
    assert_eq!((app.geometry.doc.x, app.geometry.doc.width), ((column_room - 80) / 2 + 2, 80));
    press(&mut app, 'z');
    draw(&mut app, 200, 20);
    assert_eq!((app.geometry.doc.x, app.geometry.doc.width), ((198 - 80) / 2 + 2, 80));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn a_document_without_notes_has_no_rail_until_its_first_note() {
    let (root, mut app, _) = file_app("rail-first-note");
    app.ui.doc_width = 0;
    let empty = draw(&mut app, 120, 20);
    assert_eq!(app.geometry.doc.width, 118, "no empty rail takes columns:\n{empty}");
    assert!(!empty.contains("notes hidden") && !empty.contains("rail hidden"), "{empty}");
    add_comment_on_block_two(&mut app, "the first note");
    let noted = draw(&mut app, 120, 20);
    assert!(noted.contains("the first note"), "the rail comes with the first note:\n{noted}");
    assert_eq!(app.geometry.doc.width, 120 - 2 - 1 - rail(120));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn shown_keeps_the_rail_with_no_notes_and_hidden_starts_it_closed() {
    let (root, mut app, _) = file_app("rail-start");
    app.ui.doc_width = 0;
    app.ui.notes_rail = RailStart::Shown;
    app.start_notes_rail();
    draw(&mut app, 120, 20);
    assert_eq!(app.geometry.doc.width, 120 - 2 - 1 - rail(120));

    app.ui =
        crate::config::Config::parse("[ui]\nnotes_rail = \"hidden\"\ndoc_width = 0\n").expect("config").ui;
    app.start_notes_rail();
    add_comment_on_block_two(&mut app, "a note");
    let screen = draw(&mut app, 120, 20);
    assert!(!screen.contains("a note") && screen.contains("notes hidden: z shows them"), "{screen}");
    press(&mut app, 'z');
    assert!(draw(&mut app, 120, 20).contains("a note"));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn tab_opens_a_closed_rail_and_focuses_it_and_closing_it_returns_to_the_document() {
    let (root, mut app) = noted_app("rail-tab");
    press(&mut app, 'z');
    draw(&mut app, 120, 20);
    app.handle_event(&Event::Key(KeyEvent::from(KeyCode::Tab))).expect("tab");
    assert_eq!(app.focus, Focus::Rail);
    let screen = draw(&mut app, 120, 20);
    assert!(screen.contains("first note"), "{screen}");
    assert!(screen.contains("z hide"), "the rail's hints name the key:\n{screen}");

    press(&mut app, 'z');
    assert_eq!(app.focus, Focus::Document);
    assert!(!draw(&mut app, 120, 20).contains("first note"));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn a_note_added_while_the_rail_is_closed_is_marked_and_leaves_it_closed() {
    let (root, mut app) = noted_app("rail-add");
    press(&mut app, 'z');
    add_comment_on_block_two(&mut app, "third note");
    assert_eq!(app.open.store.len(), 3);
    let screen = draw(&mut app, 200, 20);
    assert!(!screen.contains("third note"), "a wider pane does not open it either:\n{screen}");
    assert!(screen_row(&screen, "two").trim_start().starts_with('▍'), "the new note is marked:\n{screen}");
    assert!(screen_row(&screen, "Send").contains("3 new"), "{screen}");
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn a_closed_rail_stays_closed_across_a_reload_and_another_file_of_the_folder() {
    let (root, mut app, _) = folder_app("rail-session");
    std::fs::write(root.join("docs/b.md"), "# Other\n\nfour\n").expect("second file");
    app.add_quote_annotation("one", Kind::Comment, "first note".to_owned()).expect("note");
    press(&mut app, 'z');
    app.reload().expect("reload");
    assert!(!draw(&mut app, 160, 20).contains("first note"));
    app.open_file_path(&root.join("docs/b.md")).expect("open b");
    app.add_quote_annotation("four", Kind::Comment, "b note".to_owned()).expect("note");
    let screen = draw(&mut app, 160, 20);
    assert!(!screen.contains("b note") && screen.contains("notes hidden"), "{screen}");
    app.open_file_path(&root.join("docs/a.md")).expect("back to a");
    assert!(!draw(&mut app, 160, 20).contains("first note"));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn the_next_session_opens_the_rail_as_this_one_left_it() {
    let (root, mut app) = noted_app("rail-memory");
    press(&mut app, 'z');
    let state = app.data_dir.join("clients/plannotator-tui/ui-state.json");
    assert_eq!(std::fs::read_to_string(&state).expect("remembered"), r#"{"notesRail":"closed"}"#);
    app.notes_rail = NotesRail::default();
    app.start_notes_rail();
    assert!(!draw(&mut app, 120, 20).contains("first note"), "closed again in the next session");
    press(&mut app, 'z');
    app.start_notes_rail();
    assert!(draw(&mut app, 120, 20).contains("first note"), "and open once reopened");
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn a_rebound_toggle_is_the_key_the_footer_names() {
    let (root, mut app) = noted_app("rail-rebound");
    app.keys = toml::from_str("toggle-notes = [\"Z\"]\n").expect("keymap");
    press(&mut app, 'z');
    assert!(draw(&mut app, 120, 20).contains("first note"), "z is no longer the key");
    press(&mut app, 'Z');
    assert!(draw(&mut app, 120, 20).contains("notes hidden: Z shows them"));
    let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn a_rail_left_open_still_waits_for_a_documents_first_note() {
    let (root, mut app, _) = file_app("rail-left-open");
    app.ui.doc_width = 0;
    let state = app.data_dir.join("clients/plannotator-tui");
    std::fs::create_dir_all(&state).expect("state dir");
    std::fs::write(state.join("ui-state.json"), r#"{"notesRail":"open"}"#).expect("state");
    app.start_notes_rail();
    let empty = draw(&mut app, 120, 20);
    assert_eq!(app.geometry.doc.width, 118, "no empty rail takes columns:\n{empty}");
    assert!(!empty.contains("notes hidden") && !empty.contains("rail hidden"), "{empty}");
    add_comment_on_block_two(&mut app, "the first note");
    let noted = draw(&mut app, 120, 20);
    assert!(noted.contains("the first note"), "the rail comes with the first note:\n{noted}");
    assert_eq!(app.geometry.doc.width, 120 - 2 - 1 - rail(120));
    let _ = std::fs::remove_dir_all(&root);
}

/// The border colour of the one note bubble, drawn with block `block` selected.
fn border(app: &mut App, block: usize) -> ratatui::style::Color {
    app.select_block(block);
    let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 20)).expect("terminal");
    terminal.draw(|frame| app.draw(frame)).expect("draw");
    let [(rect, _)] = app.geometry.bubbles.as_slice() else { panic!("one bubble") };
    terminal.backend().buffer().cell((rect.x, rect.y)).expect("corner").fg
}

/// A note made on a whole list is on each of its items: its bubble lights up from any of
/// them, and `x` on any of them clears it.
#[test]
fn a_note_on_a_whole_list_is_lit_and_cleared_from_any_item() {
    let (root, mut app) = whole_list_note_app("rail-whole-list");
    let lit = border(&mut app, 1);
    assert_ne!(lit, ratatui::style::Color::DarkGray, "lit from the first item");
    assert_eq!(border(&mut app, 0), ratatui::style::Color::DarkGray, "not from the paragraph before");
    assert_eq!(border(&mut app, 4), ratatui::style::Color::DarkGray, "not from the paragraph after");
    for block in [2, 3] {
        assert_eq!(border(&mut app, block), lit, "lit from {:?}", app.open.doc.block_text(block));
    }

    app.select_block(2);
    press(&mut app, 'x');
    assert_eq!(app.status.as_deref(), Some("removed 1 annotation(s) on block"));
    assert!(app.open.store.placed().is_empty());
    let _ = std::fs::remove_dir_all(&root);
}

/// With `tables = "split"`, a note made on the whole table is on each of its rows: its
/// bubble lights up from any of them, the image key finds it, and `x` clears it.
#[test]
fn a_note_on_a_whole_table_is_on_each_row_when_tables_are_split() {
    let (root, mut app, _) = file_app("rail-whole-table");
    let table = "| a | b |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |";
    std::fs::write(root.join("docs/a.md"), format!("Intro\n\n{table}\n\nAfter\n")).expect("document");
    super::review_test_support::reopen(&mut app);
    app.add_quote_annotation(table, Kind::Comment, "on the table".to_owned()).expect("note");
    // Cut the same file into rows, as `[ui.blocks] tables = "split"` opens it.
    let rows = crate::doc::Blocks { tables: crate::doc::Cut::Split, ..crate::doc::Blocks::default() };
    app.open.doc = crate::doc::Document::parse_into(app.open.doc.source.clone(), rows);
    app.open.layout = crate::layout::DocLayout::build(&app.open.doc, 100);
    app.open.store.resolve_all(&app.open.doc);
    let texts: Vec<_> = (0..app.open.doc.blocks.len()).map(|b| app.open.doc.block_text(b)).collect();
    assert_eq!(texts, ["Intro", "| a | b |", "| 1 | 2 |", "| 3 | 4 |", "After"]);
    let id = app.open.store.placed().first().map(|p| p.annotation.id.clone()).expect("the note");

    let lit = border(&mut app, 1);
    assert_ne!(lit, ratatui::style::Color::DarkGray, "lit from the header");
    assert_eq!(border(&mut app, 0), ratatui::style::Color::DarkGray, "not from the paragraph before");
    assert_eq!(border(&mut app, 4), ratatui::style::Color::DarkGray, "not from the paragraph after");
    for block in [2, 3] {
        assert_eq!(border(&mut app, block), lit, "lit from {:?}", app.open.doc.block_text(block));
        press(&mut app, 'I');
        assert_eq!(app.mode, super::Mode::AttachImage(id.clone()), "image key from row {block}");
        app.handle_event(&Event::Key(KeyEvent::from(KeyCode::Esc))).expect("esc");
    }

    app.select_block(3);
    press(&mut app, 'x');
    assert_eq!(app.status.as_deref(), Some("removed 1 annotation(s) on block"));
    assert!(app.open.store.placed().is_empty());
    let _ = std::fs::remove_dir_all(&root);
}