oyo 0.1.41

CLI TUI for oyo - Step-through diff viewer
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
use std::path::PathBuf;

use crate::app::{AnimationPhase, App, ViewMode};
use crate::config::{
    DiffForegroundMode, DiffHighlightMode, EvoSyntaxMode, ModifiedStepMode, SyntaxMode,
};
use crate::test_utils::TestApp;
use crate::views::{
    extent_marker_text, render_blame, render_diff_scrollbar, render_evolution, render_split,
    render_unified_pane, show_extent_marker,
};
use oyo_core::{AnimationFrame, LineKind, MultiFileDiff, ViewLine};
use ratatui::{backend::TestBackend, buffer::Buffer, style::Color, Terminal};

fn make_app(old: &str, new: &str, view_mode: ViewMode) -> TestApp {
    TestApp::new_default(|| {
        let diff = MultiFileDiff::from_file_pair(
            PathBuf::from("old.txt"),
            PathBuf::from("new.txt"),
            old.to_string(),
            new.to_string(),
        );
        let mut app = App::new(diff, view_mode, 200, false, None);
        app.animation_enabled = false;
        app.animation_phase = AnimationPhase::Idle;
        app.syntax_mode = SyntaxMode::Off;
        app.diff_bg = false;
        app.diff_fg = DiffForegroundMode::Theme;
        app.diff_highlight = DiffHighlightMode::Text;
        app
    })
}

fn render_buffer(app: &mut App, width: u16, height: u16) -> Buffer {
    let backend = TestBackend::new(width, height);
    let mut terminal = Terminal::new(backend).expect("terminal");
    terminal
        .draw(|frame| {
            let area = frame.area();
            match app.view_mode {
                ViewMode::UnifiedPane => render_unified_pane(frame, app, area),
                ViewMode::Split => render_split(frame, app, area),
                ViewMode::Evolution => render_evolution(frame, app, area),
                ViewMode::Blame => render_blame(frame, app, area),
                ViewMode::Preview => {}
            }
        })
        .expect("draw");
    terminal.backend().buffer().clone()
}

#[test]
fn diff_scrollbar_track_uses_background_without_thumb() {
    let mut app = make_app("same\n", "same\n", ViewMode::UnifiedPane);
    app.theme.background = Some(Color::Blue);
    let backend = TestBackend::new(1, 4);
    let mut terminal = Terminal::new(backend).expect("terminal");

    terminal
        .draw(|frame| render_diff_scrollbar(frame, &mut app, frame.area(), 4, 4, 0))
        .expect("draw");

    assert!(terminal
        .backend()
        .buffer()
        .content
        .iter()
        .all(|cell| cell.style().bg == Some(Color::Blue)));
    assert!(app.diff_scrollbar.is_none());
}

fn render_unified_buffer(app: &mut App, width: u16, height: u16) -> Buffer {
    let backend = TestBackend::new(width, height);
    let mut terminal = Terminal::new(backend).expect("terminal");
    terminal
        .draw(|frame| {
            let area = frame.area();
            render_unified_pane(frame, app, area);
        })
        .expect("draw");
    terminal.backend().buffer().clone()
}

fn buffer_text(buf: &Buffer) -> Vec<String> {
    let mut lines = Vec::new();
    for y in 0..buf.area.height {
        let mut line = String::new();
        for x in 0..buf.area.width {
            line.push_str(buf[(x, y)].symbol());
        }
        lines.push(line);
    }
    lines
}

fn count_occurrences(haystack: &str, needle: &str) -> usize {
    haystack.match_indices(needle).count()
}

#[test]
fn binary_image_empty_state_points_to_preview() {
    let diff = MultiFileDiff::from_file_pair_bytes(
        PathBuf::from("image.png"),
        vec![0xff, 0x00],
        vec![0xff, 0x01],
    );
    let mut app = App::new(diff, ViewMode::UnifiedPane, 200, false, None);
    let text = buffer_text(&render_buffer(&mut app, 40, 5)).join("\n");

    assert!(text.contains("ctrl-p preview"), "empty state: {text}");
    assert!(!text.contains("preview disabled"), "empty state: {text}");
    app.enable_review_mode();
    app.diff_view_area = Some((0, 0, 40, 5));
    app.set_diff_selection_cells(vec![vec!["x".to_string(); 40]; 5]);
    assert_eq!(app.review_line_add_hover_at(38, 2), (None, false));
    let (x, y, width, _) = app.binary_preview_hit.expect("preview action hitbox");
    assert!(app.handle_binary_preview_click(x + width / 2, y));
    assert_eq!(app.view_mode, ViewMode::Preview);
}

fn column_contains(buf: &Buffer, x: u16, needle: &str) -> bool {
    for y in 0..buf.area.height {
        if buf[(x, y)].symbol() == needle {
            return true;
        }
    }
    false
}

fn marker_view_line(kind: LineKind, has_changes: bool) -> ViewLine {
    ViewLine {
        content: String::new(),
        spans: vec![],
        kind,
        old_line: Some(1),
        new_line: Some(1),
        is_active: false,
        is_active_change: false,
        is_primary_active: false,
        show_hunk_extent: false,
        change_id: 0,
        hunk_index: Some(0),
        has_changes,
    }
}

#[test]
fn deleted_extent_marker_uses_dashed_bar() {
    let deleted = marker_view_line(LineKind::Deleted, true);
    let inserted = marker_view_line(LineKind::Inserted, true);

    assert_eq!(extent_marker_text("▌", "D", &deleted), "D");
    assert_eq!(extent_marker_text("▌", "D", &inserted), "▌");
}

#[test]
fn no_step_extent_marker_shows_changed_hunk_lines() {
    let mut app = make_app("old\n", "new\n", ViewMode::UnifiedPane);
    app.stepping = false;

    let changed = marker_view_line(LineKind::Modified, true);
    assert!(show_extent_marker(&app, &changed));

    let context = marker_view_line(LineKind::Context, false);
    assert!(!show_extent_marker(&app, &context));
    app.diff_extent_marker_context = true;
    assert!(show_extent_marker(&app, &context));
}

#[test]
fn test_unified_modified_lifecycle_render() {
    let old = "line1\nOLDSIDE\nline3\n";
    let new = "line1\nNEWSIDE\nline3\n";
    let mut app = make_app(old, new, ViewMode::UnifiedPane);

    let before = buffer_text(&render_buffer(&mut app, 80, 20)).join("\n");
    assert!(before.contains("OLDSIDE"));
    assert!(!before.contains("NEWSIDE"));

    app.next_step();
    let on_step_lines = buffer_text(&render_buffer(&mut app, 80, 20));
    assert!(
        on_step_lines
            .iter()
            .any(|line| line.contains("OLDSIDE") && line.contains("NEWSIDE")),
        "active modified line should show old + new"
    );

    app.multi_diff.current_navigator().clear_active_change();
    app.animation_phase = AnimationPhase::Idle;
    let after = buffer_text(&render_buffer(&mut app, 80, 20)).join("\n");
    assert!(!after.contains("OLDSIDE"));
    assert!(after.contains("NEWSIDE"));
}

#[test]
fn test_unified_peek_change_updates_render() {
    let old = "line1\nOLDTOKEN\nline3\n";
    let new = "line1\nNEWTOKEN\nline3\n";
    let mut app = make_app(old, new, ViewMode::UnifiedPane);
    app.unified_modified_step_mode = ModifiedStepMode::Mixed;

    app.next_step();
    let before = buffer_text(&render_buffer(&mut app, 80, 20)).join("\n");
    assert!(before.contains("OLDTOKEN"));
    assert!(before.contains("NEWTOKEN"));

    app.toggle_peek_old_change();
    let after = buffer_text(&render_buffer(&mut app, 80, 20)).join("\n");
    assert!(!after.contains("OLDTOKEN"));
    assert!(after.contains("NEWTOKEN"));
}

#[test]
fn test_split_modified_lifecycle_render() {
    let old = "line1\nOLDSPLIT\nline3\n";
    let new = "line1\nNEWSPLIT\nline3\n";
    let mut app = make_app(old, new, ViewMode::Split);

    let before = buffer_text(&render_buffer(&mut app, 100, 20)).join("\n");
    assert_eq!(count_occurrences(&before, "OLDSPLIT"), 2);
    assert!(!before.contains("NEWSPLIT"));

    app.next_step();
    app.multi_diff.current_navigator().clear_active_change();
    let after = buffer_text(&render_buffer(&mut app, 100, 20)).join("\n");
    assert_eq!(count_occurrences(&after, "OLDSPLIT"), 1);
    assert_eq!(count_occurrences(&after, "NEWSPLIT"), 1);
}

#[test]
fn test_evolution_full_preview_no_duplicate_modified_line() {
    let old = "line1\nOLDEVO\nline3\n";
    let new = "line1\nNEWEVO\nline3\n";
    let mut app = make_app(old, new, ViewMode::Evolution);
    app.syntax_mode = SyntaxMode::On;
    app.evo_syntax = EvoSyntaxMode::Full;

    app.next_hunk();
    app.next_hunk();
    let rendered = buffer_text(&render_buffer(&mut app, 80, 20)).join("\n");
    assert!(rendered.contains("NEWEVO"));
    assert!(!rendered.contains("OLDEVO"));
}

#[test]
fn test_evolution_deleted_active_fallback_marker() {
    let old = "line1\nDEL\nline3\n";
    let new = "line1\nline3\n";
    let mut app = make_app(old, new, ViewMode::Evolution);
    app.next_step(); // apply deletion
    app.animation_phase = AnimationPhase::Idle;

    let rendered = buffer_text(&render_buffer(&mut app, 60, 10)).join("\n");
    assert!(
        rendered.contains("â–¶"),
        "cursor marker should remain visible when deleted line is hidden"
    );
}

#[test]
fn test_evolution_window_cache_scroll_offset() {
    let old = (0..600).map(|i| format!("line {i}\n")).collect::<String>();
    let new = (0..600)
        .filter(|i| *i >= 50)
        .map(|i| format!("line {i} new\n"))
        .collect::<String>();
    let mut app = make_app(&old, &new, ViewMode::Evolution);
    app.last_viewport_height = 10;
    app.auto_center = false;
    app.needs_scroll_to_active = false;
    app.stepping = false;
    app.scroll_offset = 400;
    let span = app.last_viewport_height.max(20).saturating_mul(4).max(200);
    let scroll_offset = app.scroll_offset;
    let (_window_start, display_start) = {
        let nav = app.multi_diff.current_navigator();
        let window_start = scroll_offset.min(nav.diff().changes.len().saturating_sub(1).max(span));
        let display_start = nav
            .evolution_display_index_for_change_index(window_start)
            .unwrap_or(0);
        assert!(
            display_start < window_start,
            "expected evolution display start to differ from raw change index"
        );
        (window_start, display_start)
    };

    let _ = app.current_view_with_frame(AnimationFrame::Idle);
    let start_first = app.view_window_start();
    assert_eq!(
        start_first, display_start,
        "window start should use evolution display index"
    );

    app.scroll_offset = start_first + 5;
    let view = app.current_view_with_frame(AnimationFrame::Idle);
    let start_second = app.view_window_start();
    let render_scroll = app.render_scroll_offset();

    assert_eq!(
        start_first, start_second,
        "cached view should preserve window start"
    );
    assert!(
        render_scroll < view.len(),
        "render scroll should stay inside windowed view"
    );
}

#[test]
fn test_unified_wrap_hunk_hint_overflow_places_above() {
    let long = "LONGINSERT_LONGINSERT_LONGINSERT_LONGINSERT";
    let old = "";
    let new = format!("{long}\nshort\n");
    let mut app = make_app(old, &new, ViewMode::UnifiedPane);
    app.line_wrap = true;

    for _ in 0..5 {
        if app.last_step_hint_text().is_some() {
            break;
        }
        app.next_step();
    }
    assert!(
        app.last_step_hint_text().is_some(),
        "should reach last-step hint state"
    );

    let lines = buffer_text(&render_buffer(&mut app, 20, 4));
    let hint_idx = lines
        .iter()
        .position(|line| line.contains("Last step"))
        .expect("virtual hint should render");
    let long_idx = lines
        .iter()
        .position(|line| line.contains("LONGINSERT"))
        .expect("insert line should render");
    assert!(
        hint_idx < long_idx,
        "wrapped overflow should place hint above the hunk"
    );
}

#[test]
fn test_unified_wrap_end_scroll_no_bounce() {
    let long = "LONGINSERT_LONGINSERT_LONGINSERT_LONGINSERT";
    let mut new = String::new();
    for idx in 0..40 {
        new.push_str(&format!("{long} {idx}\n"));
    }
    let mut app = make_app("", &new, ViewMode::UnifiedPane);
    app.line_wrap = true;
    app.auto_center = false;
    app.needs_scroll_to_active = false;
    app.no_step_auto_jump_on_enter = false;
    app.stepping = false;
    app.enter_no_step_mode();
    app.last_viewport_height = 4;
    app.scroll_offset = usize::MAX;

    let first = buffer_text(&render_buffer(&mut app, 20, 4));
    let max_scroll = app.scroll_offset;
    assert!(max_scroll > 0, "expected content to be scrollable");

    app.scroll_down();
    let second = buffer_text(&render_buffer(&mut app, 20, 4));

    assert_eq!(
        app.scroll_offset, max_scroll,
        "scroll offset should clamp at end"
    );
    assert_eq!(first, second, "render should not bounce at end");
}

#[test]
fn test_blame_end_scroll_no_bounce() {
    let long = "LONGINSERT_LONGINSERT_LONGINSERT_LONGINSERT";
    let mut new = String::new();
    for idx in 0..40 {
        new.push_str(&format!("{long} {idx}\n"));
    }
    let mut app = make_app("", &new, ViewMode::Blame);
    app.blame_enabled = true;
    app.line_wrap = false;
    app.auto_center = false;
    app.needs_scroll_to_active = false;
    app.no_step_auto_jump_on_enter = false;
    app.stepping = false;
    app.enter_no_step_mode();
    app.last_viewport_height = 4;
    app.scroll_offset = usize::MAX;

    let first = buffer_text(&render_buffer(&mut app, 30, 4));
    let max_scroll = app.scroll_offset;
    assert!(max_scroll > 0, "expected content to be scrollable");

    app.scroll_down();
    let second = buffer_text(&render_buffer(&mut app, 30, 4));

    assert_eq!(
        app.scroll_offset, max_scroll,
        "scroll offset should clamp at end"
    );
    assert_eq!(first, second, "render should not bounce at end");
}

#[test]
fn test_blame_large_file_end_scroll_no_empty_state() {
    let long = "LONGINSERT_LONGINSERT_LONGINSERT_LONGINSERT";
    let mut new = String::new();
    for idx in 0..200 {
        new.push_str(&format!("{long} {idx}\n"));
    }
    let mut app = TestApp::new_with_guard(32, || {
        let diff = MultiFileDiff::from_file_pair(
            PathBuf::from("old.txt"),
            PathBuf::from("new.txt"),
            String::new(),
            new,
        );
        let mut app = App::new(diff, ViewMode::Blame, 200, false, None);
        app.animation_enabled = false;
        app.animation_phase = AnimationPhase::Idle;
        app.syntax_mode = SyntaxMode::Off;
        app.diff_bg = false;
        app.diff_fg = DiffForegroundMode::Theme;
        app.diff_highlight = DiffHighlightMode::Text;
        app
    });
    app.blame_enabled = true;
    app.line_wrap = false;
    app.auto_center = false;
    app.needs_scroll_to_active = false;
    app.no_step_auto_jump_on_enter = false;
    app.stepping = false;
    app.enter_no_step_mode();
    app.last_viewport_height = 4;
    app.scroll_offset = usize::MAX;

    let view = app.current_view_with_frame(AnimationFrame::Idle);
    let mut extra_rows = vec![0; view.len()];
    if let Some(last) = extra_rows.last_mut() {
        *last = 2;
    }
    app.blame_extra_rows = Some(extra_rows);

    let first_buf = render_unified_buffer(&mut app, 30, 4);
    let first_text = buffer_text(&first_buf).join("\n");
    assert!(
        !first_text.contains("No content at this step"),
        "expected content at end of blame view"
    );
    let max_scroll = app.scroll_offset;

    app.scroll_down();
    let second_buf = render_unified_buffer(&mut app, 30, 4);
    let second_text = buffer_text(&second_buf).join("\n");

    assert_eq!(
        app.scroll_offset, max_scroll,
        "scroll offset should clamp at end"
    );
    assert_eq!(first_text, second_text, "render should not bounce at end");
}

#[test]
fn test_split_wrap_hunk_hint_overflow_places_above() {
    let long = "LONGINSERT".repeat(12);
    let old = "";
    let new = format!("{long}\nshort\n");
    let mut app = make_app(old, &new, ViewMode::Split);
    app.line_wrap = true;
    app.split_align_lines = true;

    for _ in 0..5 {
        if app.last_step_hint_text().is_some() {
            break;
        }
        app.next_step();
    }
    assert!(
        app.last_step_hint_text().is_some(),
        "should reach last-step hint state"
    );
    app.multi_diff.current_navigator().clear_active_change();

    let lines = buffer_text(&render_buffer(&mut app, 60, 4));
    let hint_idx = lines
        .iter()
        .position(|line| line.contains("Last step"))
        .expect("virtual hint should render");
    let long_idx = lines
        .iter()
        .position(|line| line.contains("LONGINSERT"))
        .expect("insert line should render");
    assert!(
        hint_idx < long_idx,
        "wrapped overflow should place hint above the hunk"
    );
}

#[test]
fn test_extent_markers_clear_at_start() {
    let old = "line1\nOLD_A\nOLD_B\nline4\n";
    let new = "line1\nNEW_A\nNEW_B\nline4\n";
    let mut app = make_app(old, new, ViewMode::UnifiedPane);
    app.extent_marker = "E".to_string();

    let before_buf = render_buffer(&mut app, 80, 10);
    assert!(
        !column_contains(&before_buf, 0, "E"),
        "extent markers should be hidden at step 0"
    );

    app.next_hunk();
    let in_hunk_buf = render_buffer(&mut app, 80, 10);
    assert!(
        column_contains(&in_hunk_buf, 0, "E"),
        "extent markers should show inside a hunk"
    );

    app.prev_step();
    app.multi_diff.current_navigator().clear_active_change();
    app.animation_phase = AnimationPhase::Idle;
    let after_buf = render_buffer(&mut app, 80, 10);
    assert!(
        !column_contains(&after_buf, 0, "E"),
        "extent markers should clear after hunk-out"
    );
}

#[test]
fn test_extent_markers_skip_context_by_default() {
    let old = "CTX\nOLD1\nOLD2\n";
    let new = "CTX\nNEW1\nNEW2\n";
    let mut app = make_app(old, new, ViewMode::UnifiedPane);
    app.extent_marker = "E".to_string();

    app.next_hunk();
    let buf = render_buffer(&mut app, 40, 8);
    assert!(
        column_contains(&buf, 0, "E"),
        "extent markers should show for changed lines"
    );

    let lines = buffer_text(&buf);
    let ctx_row = lines
        .iter()
        .position(|line| line.contains("CTX"))
        .expect("context line should render");
    assert_ne!(
        buf[(0, ctx_row as u16)].symbol(),
        "E",
        "context lines should not show extent markers by default"
    );
}