aboutsummaryrefslogtreecommitdiff
path: root/tests/rendering.rs
blob: 79c47f5f218a6b17bf0b577b1337b90596104959 (plain) (blame)
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
//! Rendering and routing tests (MJB-HLR-012, MJB-HLR-019).
//!
//! These use ratatui's `TestBackend` to render into an in-memory cell grid and
//! assert on what a user would actually see, rather than declaring the
//! presentation layer "verified manually".

use mojibake::{
    components::{Component, buffer::BufferComponent},
    config::Config,
};
use ratatui::{Terminal, backend::TestBackend};

fn default_config() -> Config {
    toml::from_str(include_str!("../.config/config.toml")).expect("built-in config must parse")
}

/// Render `text` into a `w`×`h` grid and return the rows as strings.
fn render(text: &str, w: u16, h: u16) -> (Vec<String>, tempfile::TempDir) {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("f.txt");
    std::fs::write(&path, text).unwrap();

    let mut component = BufferComponent::new(default_config(), Some(path)).unwrap();
    let mut terminal = Terminal::new(TestBackend::new(w, h)).unwrap();
    terminal
        .draw(|frame| component.draw(frame, frame.area()).unwrap())
        .unwrap();

    let buffer = terminal.backend().buffer().clone();
    let rows = (0..h)
        .map(|y| {
            (0..w)
                .map(|x| buffer.cell((x, y)).map(|c| c.symbol()).unwrap_or(" "))
                .collect::<String>()
                .trim_end()
                .to_owned()
        })
        .collect();
    (rows, dir)
}

#[test]
fn mjb_llr_200_renders_the_file_contents() {
    let (rows, _d) = render("hello world\nsecond line\n", 40, 8);
    let joined = rows.join("\n");
    assert!(
        joined.contains("hello world"),
        "file contents must be on screen, got:\n{joined}"
    );
    assert!(joined.contains("second line"));
}

/// MJB-LLR-200, MJB-HLR-012: only lines intersecting the viewport are drawn.
/// A 1000-line file in an 8-row terminal must not show line 500.
#[test]
fn mjb_llr_200_renders_only_the_visible_window() {
    let text: String = (0..1000).map(|i| format!("line{i}\n")).collect();
    let (rows, _d) = render(&text, 40, 8);
    let joined = rows.join("\n");

    assert!(joined.contains("line0"), "the top of the file is visible");
    assert!(
        !joined.contains("line500"),
        "a line far outside the viewport must not be rendered"
    );
    assert!(
        !joined.contains("line999"),
        "nor the last line of the file"
    );
}

#[test]
fn mjb_llr_200_line_numbers_are_shown_in_the_gutter() {
    let (rows, _d) = render("alpha\nbeta\n", 40, 8);
    assert!(rows[0].starts_with('1'), "row 0 gutter, got {:?}", rows[0]);
    assert!(rows[1].starts_with('2'), "row 1 gutter, got {:?}", rows[1]);
}

/// MJB-LLR-201: the block cursor is drawn with a distinct style.
#[test]
fn mjb_llr_201_cursor_is_styled_distinctly() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("f.txt");
    std::fs::write(&path, "abc\n").unwrap();

    let mut component = BufferComponent::new(default_config(), Some(path)).unwrap();
    let mut terminal = Terminal::new(TestBackend::new(20, 5)).unwrap();
    terminal
        .draw(|frame| component.draw(frame, frame.area()).unwrap())
        .unwrap();

    let buffer = terminal.backend().buffer().clone();
    // The gutter is two cells wide for a 1-line file ("1" + space), so the
    // first text cell holds the cursor.
    let gutter = 2u16;
    let cursor_cell = buffer.cell((gutter, 0)).unwrap();
    let plain_cell = buffer.cell((gutter + 1, 0)).unwrap();

    assert_eq!(cursor_cell.symbol(), "a");
    assert_ne!(
        (cursor_cell.fg, cursor_cell.bg),
        (plain_cell.fg, plain_cell.bg),
        "the cursor cell must be styled differently from ordinary text"
    );
}

/// MJB-LLR-202: the status line reports mode, file name and position.
#[test]
fn mjb_llr_202_status_line_shows_mode_and_path() {
    let (rows, _d) = render("abc\n", 60, 6);
    let status = &rows[rows.len() - 2];

    assert!(status.contains("NOR"), "mode indicator, got {status:?}");
    assert!(status.contains("f.txt"), "file name, got {status:?}");
    assert!(status.contains("1:1"), "cursor position, got {status:?}");
}

#[test]
fn mjb_llr_202_status_line_marks_an_unmodified_file() {
    let (rows, _d) = render("abc\n", 60, 6);
    let status = &rows[rows.len() - 2];
    assert!(
        !status.contains("[+]"),
        "a freshly opened file is not modified, got {status:?}"
    );
}

#[test]
fn mjb_llr_202_scratch_buffer_is_labelled() {
    let mut component = BufferComponent::new(default_config(), None).unwrap();
    let mut terminal = Terminal::new(TestBackend::new(40, 5)).unwrap();
    terminal
        .draw(|frame| component.draw(frame, frame.area()).unwrap())
        .unwrap();

    let buffer = terminal.backend().buffer().clone();
    let status: String = (0..40)
        .map(|x| buffer.cell((x, 3)).map(|c| c.symbol()).unwrap_or(" "))
        .collect();
    assert!(
        status.contains("[scratch]"),
        "a buffer with no path must say so, got {status:?}"
    );
}

/// MJB-HLR-019: no trace of the removed template widgets.
#[test]
fn mjb_llr_204_no_fps_counter_or_hello_world_is_rendered() {
    let (rows, _d) = render("some content\n", 80, 12);
    let joined = rows.join("\n").to_lowercase();

    assert!(!joined.contains("hello world"), "placeholder widget removed");
    assert!(!joined.contains("fps"), "frame-rate counter removed");
    assert!(
        !joined.contains("ticks/sec"),
        "frame-rate counter removed"
    );
}

/// MJB-LLR-203: a key matching the `Global` keymap is consumed by `App` and
/// never reaches the buffer.
///
/// Verified at the component boundary: `Ctrl-c` is bound globally to `Quit`,
/// and the buffer must not treat it as text even in insert mode. If routing
/// regressed and the key were forwarded, insert mode would type a `c`.
#[test]
fn mjb_llr_203_globally_bound_key_is_not_typed_as_text() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("f.txt");
    std::fs::write(&path, "").unwrap();

    let mut component = BufferComponent::new(default_config(), Some(path)).unwrap();
    component
        .handle_key_event(KeyEvent::new(KeyCode::Char('i'), KeyModifiers::empty()))
        .unwrap();
    component
        .handle_key_event(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL))
        .unwrap();

    assert_eq!(
        component.buffer().document.text().to_string(),
        "",
        "ctrl-c must never insert a literal 'c'"
    );
}

/// MJB-LLR-205: no per-tick key state remains, so a chord cannot be broken by
/// the passage of time or by intervening frames.
#[test]
fn mjb_llr_205_pending_chord_survives_redraws() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("f.txt");
    std::fs::write(&path, "aaa\nbbb\nccc\n").unwrap();

    let mut component = BufferComponent::new(default_config(), Some(path)).unwrap();
    let mut terminal = Terminal::new(TestBackend::new(40, 8)).unwrap();

    let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::empty());

    component.handle_key_event(k('j')).unwrap();
    component.handle_key_event(k('j')).unwrap();
    // Begin the `gg` chord...
    component.handle_key_event(k('g')).unwrap();

    // ...then redraw repeatedly, which is what the tick used to interrupt.
    for _ in 0..10 {
        terminal
            .draw(|frame| component.draw(frame, frame.area()).unwrap())
            .unwrap();
    }

    component.handle_key_event(k('g')).unwrap();

    let buf = component.buffer();
    assert_eq!(
        buf.document.range().cursor(buf.document.slice()),
        0,
        "gg must still resolve after many redraws"
    );
}

/// MJB-LLR-098: a viewport too small to hold the status rows must not panic.
#[test]
fn mjb_llr_098_tiny_viewport_renders_without_panicking() {
    for (w, h) in [(1u16, 1u16), (2, 2), (1, 3), (80, 2)] {
        let _ = render("content\nmore content\n", w, h);
    }
}

#[test]
fn mjb_llr_200_empty_file_renders_without_panicking() {
    let (rows, _d) = render("", 40, 6);
    assert!(!rows.is_empty());
}

#[test]
fn mjb_llr_200_wide_characters_render() {
    let (rows, _d) = render("文字化け\n", 40, 6);
    let joined = rows.join("\n");
    // A wide character occupies two terminal cells; ratatui fills the second
    // with a continuation space, so the glyphs are not contiguous in the
    // reconstructed row. Check for each one.
    for c in "文字化け".chars() {
        assert!(
            joined.contains(c),
            "wide character {c:?} must render, got {rows:?}"
        );
    }
}

/// MJB-LLR-025: a wide character must advance the reported column by two, not
/// by one character or by three bytes.
#[test]
fn mjb_llr_025_wide_characters_advance_two_columns() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("f.txt");
    std::fs::write(&path, "文字\n").unwrap();

    let mut component = BufferComponent::new(default_config(), Some(path)).unwrap();
    // Move right one grapheme, then read the reported column off the status line.
    component
        .handle_key_event(crossterm::event::KeyEvent::new(
            crossterm::event::KeyCode::Char('l'),
            crossterm::event::KeyModifiers::empty(),
        ))
        .unwrap();

    let mut terminal = Terminal::new(TestBackend::new(60, 6)).unwrap();
    terminal
        .draw(|frame| component.draw(frame, frame.area()).unwrap())
        .unwrap();

    let buffer = terminal.backend().buffer().clone();
    let status: String = (0..60)
        .map(|x| buffer.cell((x, 4)).map(|c| c.symbol()).unwrap_or(" "))
        .collect();
    assert!(
        status.contains("1:3"),
        "one wide character past the start is column 3, got {status:?}"
    );
}