tij 0.4.16

Text-mode interface for Jujutsu - a TUI for jj version control
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
//! Status View
//!
//! Displays the current working copy status with changed files.

mod input;
mod render;

use crate::model::{FileState, Status};
use crate::ui::navigation;

/// Input mode for Status View
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StatusInputMode {
    /// Normal navigation mode
    #[default]
    Normal,
    /// Commit message input mode
    CommitInput,
}

/// Action returned from StatusView key handling
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusAction {
    /// Show diff for selected file (opens DiffView, jumps to file)
    ShowFileDiff {
        /// Working copy change ID
        change_id: String,
        /// File path to jump to
        file_path: String,
    },
    /// Show blame/annotation for selected file
    OpenBlame {
        /// File path to annotate
        file_path: String,
    },
    /// Commit with message
    Commit { message: String },
    /// Jump to first conflict file
    JumpToConflict,
    /// Restore a single file (jj restore <file>)
    RestoreFile { file_path: String },
    /// Restore all files (jj restore)
    RestoreAll,
    /// Open diffedit for selected file (jj diffedit -r @ <file>)
    DiffEdit { file_path: String },
    /// No action
    None,
}

/// Status View state
#[derive(Debug)]
pub struct StatusView {
    /// Current status (None if not loaded)
    pub(super) status: Option<Status>,

    /// Selected file index
    pub(super) selected_index: usize,

    /// Scroll offset for display
    pub(super) scroll_offset: usize,

    /// Current input mode
    pub input_mode: StatusInputMode,

    /// Input buffer for commit message
    pub input_buffer: String,
}

impl Default for StatusView {
    fn default() -> Self {
        Self::new()
    }
}

impl StatusView {
    /// Default visible count for scroll calculations
    pub(super) const DEFAULT_VISIBLE_COUNT: usize = 20;

    /// Create a new StatusView
    pub fn new() -> Self {
        Self {
            status: None,
            selected_index: 0,
            scroll_offset: 0,
            input_mode: StatusInputMode::Normal,
            input_buffer: String::new(),
        }
    }

    /// Start commit input mode
    pub fn start_commit_input(&mut self) {
        self.input_mode = StatusInputMode::CommitInput;
        self.input_buffer.clear();
    }

    /// Cancel input mode
    pub fn cancel_input(&mut self) {
        self.input_mode = StatusInputMode::Normal;
        self.input_buffer.clear();
    }

    /// Set the status data
    pub fn set_status(&mut self, status: Status) {
        self.status = Some(status);
        // Reset selection and scroll if out of bounds
        if let Some(ref s) = self.status {
            if self.selected_index >= s.files.len() {
                self.selected_index = 0;
                self.scroll_offset = 0;
            }
            // Also reset scroll if it would show empty area
            if self.scroll_offset >= s.files.len() {
                self.scroll_offset = 0;
            }
        }
    }

    /// Get the selected file path
    pub fn selected_file_path(&self) -> Option<&str> {
        self.status
            .as_ref()
            .and_then(|s| s.files.get(self.selected_index))
            .map(|f| f.path.as_str())
    }

    /// Get the working copy change ID
    pub fn working_copy_id(&self) -> Option<&str> {
        self.status
            .as_ref()
            .map(|s| s.working_copy_change_id.as_str())
    }

    /// Check if there are any conflicts in the current status
    #[allow(dead_code)] // Phase 9: conflict resolution
    pub fn has_conflicts(&self) -> bool {
        self.status.as_ref().is_some_and(|s| s.has_conflicts)
    }

    /// Jump to the first conflicted file in the list
    ///
    /// Returns true if a conflict file was found and selection moved.
    fn jump_to_first_conflict(&mut self) -> bool {
        if let Some(ref status) = self.status
            && let Some(idx) = status
                .files
                .iter()
                .position(|f| matches!(f.state, FileState::Conflicted))
        {
            self.selected_index = idx;
            self.scroll_offset = navigation::adjust_scroll(
                self.selected_index,
                self.scroll_offset,
                Self::DEFAULT_VISIBLE_COUNT,
            );
            return true;
        }
        false
    }

    /// Move selection down
    fn move_down(&mut self, visible_count: usize) {
        if let Some(ref status) = self.status {
            let max = status.files.len().saturating_sub(1);
            self.selected_index = navigation::select_next(self.selected_index, max);
            self.scroll_offset =
                navigation::adjust_scroll(self.selected_index, self.scroll_offset, visible_count);
        }
    }

    /// Move selection up
    fn move_up(&mut self, visible_count: usize) {
        self.selected_index = navigation::select_prev(self.selected_index);
        self.scroll_offset =
            navigation::adjust_scroll(self.selected_index, self.scroll_offset, visible_count);
    }

    /// Jump to top
    fn jump_to_top(&mut self) {
        self.selected_index = 0;
        self.scroll_offset = 0;
    }

    /// Jump to bottom
    fn jump_to_bottom(&mut self, visible_count: usize) {
        if let Some(ref status) = self.status
            && !status.files.is_empty()
        {
            self.selected_index = status.files.len() - 1;
            self.scroll_offset =
                navigation::adjust_scroll(self.selected_index, self.scroll_offset, visible_count);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{ChangeId, FileStatus};
    use crossterm::event::{KeyCode, KeyEvent};

    fn sample_status() -> Status {
        Status {
            files: vec![
                FileStatus {
                    path: "src/main.rs".to_string(),
                    state: FileState::Modified,
                },
                FileStatus {
                    path: "src/new.rs".to_string(),
                    state: FileState::Added,
                },
                FileStatus {
                    path: "old.rs".to_string(),
                    state: FileState::Deleted,
                },
            ],
            has_conflicts: false,
            working_copy_change_id: ChangeId::new("abc12345".to_string()),
            parent_change_id: ChangeId::new("xyz98765".to_string()),
        }
    }

    #[test]
    fn test_new_status_view() {
        let view = StatusView::new();
        assert!(view.status.is_none());
        assert_eq!(view.selected_index, 0);
    }

    #[test]
    fn test_set_status() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        assert!(view.status.is_some());
        assert_eq!(view.status.as_ref().unwrap().files.len(), 3);
    }

    #[test]
    fn test_move_down() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        assert_eq!(view.selected_index, 0);
        view.move_down(20);
        assert_eq!(view.selected_index, 1);
        view.move_down(20);
        assert_eq!(view.selected_index, 2);
        view.move_down(20); // Should not go beyond last item
        assert_eq!(view.selected_index, 2);
    }

    #[test]
    fn test_move_up() {
        let mut view = StatusView::new();
        view.set_status(sample_status());
        view.selected_index = 2;

        view.move_up(20);
        assert_eq!(view.selected_index, 1);
        view.move_up(20);
        assert_eq!(view.selected_index, 0);
        view.move_up(20); // Should not go below 0
        assert_eq!(view.selected_index, 0);
    }

    #[test]
    fn test_jump_to_top_bottom() {
        let mut view = StatusView::new();
        view.set_status(sample_status());
        view.selected_index = 1;

        view.jump_to_bottom(20);
        assert_eq!(view.selected_index, 2);

        view.jump_to_top();
        assert_eq!(view.selected_index, 0);
    }

    #[test]
    fn test_selected_file_path() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        assert_eq!(view.selected_file_path(), Some("src/main.rs"));
        view.selected_index = 1;
        assert_eq!(view.selected_file_path(), Some("src/new.rs"));
    }

    #[test]
    fn test_working_copy_id() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        assert_eq!(view.working_copy_id(), Some("abc12345"));
    }

    #[test]
    fn test_handle_key_navigation() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('j')));
        assert_eq!(action, StatusAction::None);
        assert_eq!(view.selected_index, 1);

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('k')));
        assert_eq!(action, StatusAction::None);
        assert_eq!(view.selected_index, 0);
    }

    #[test]
    fn test_handle_key_open_diff() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Enter));
        match action {
            StatusAction::ShowFileDiff {
                change_id,
                file_path,
            } => {
                assert_eq!(change_id, "abc12345");
                assert_eq!(file_path, "src/main.rs");
            }
            _ => panic!("Expected ShowFileDiff action"),
        }
    }

    // Note: QUIT and TAB are handled by global key handler in input.rs,
    // not by StatusView.handle_key(), so no tests here for those keys.

    #[test]
    fn test_empty_status() {
        let mut view = StatusView::new();
        let empty_status = Status {
            files: vec![],
            has_conflicts: false,
            working_copy_change_id: ChangeId::new("abc".to_string()),
            parent_change_id: ChangeId::new("xyz".to_string()),
        };
        view.set_status(empty_status);

        assert!(view.status.as_ref().unwrap().is_clean());
    }

    #[test]
    fn test_has_conflicts() {
        let mut view = StatusView::new();

        // No status set - no conflicts
        assert!(!view.has_conflicts());

        // Status without conflicts
        let no_conflict_status = Status {
            files: vec![],
            has_conflicts: false,
            working_copy_change_id: ChangeId::new("abc".to_string()),
            parent_change_id: ChangeId::new("xyz".to_string()),
        };
        view.set_status(no_conflict_status);
        assert!(!view.has_conflicts());

        // Status with conflicts
        let conflict_status = Status {
            files: vec![],
            has_conflicts: true,
            working_copy_change_id: ChangeId::new("abc".to_string()),
            parent_change_id: ChangeId::new("xyz".to_string()),
        };
        view.set_status(conflict_status);
        assert!(view.has_conflicts());
    }

    fn status_with_conflicts() -> Status {
        Status {
            files: vec![
                FileStatus {
                    path: "src/main.rs".to_string(),
                    state: FileState::Modified,
                },
                FileStatus {
                    path: "src/conflict.rs".to_string(),
                    state: FileState::Conflicted,
                },
                FileStatus {
                    path: "src/other.rs".to_string(),
                    state: FileState::Added,
                },
            ],
            has_conflicts: true,
            working_copy_change_id: ChangeId::new("abc12345".to_string()),
            parent_change_id: ChangeId::new("xyz98765".to_string()),
        }
    }

    #[test]
    fn test_jump_to_first_conflict() {
        let mut view = StatusView::new();
        view.set_status(status_with_conflicts());

        assert_eq!(view.selected_index, 0);
        assert!(view.jump_to_first_conflict());
        assert_eq!(view.selected_index, 1); // conflict.rs is at index 1
    }

    #[test]
    fn test_jump_to_first_conflict_no_conflicts() {
        let mut view = StatusView::new();
        view.set_status(sample_status()); // no conflicted files

        assert_eq!(view.selected_index, 0);
        assert!(!view.jump_to_first_conflict());
        assert_eq!(view.selected_index, 0); // unchanged
    }

    #[test]
    fn test_f_key_with_conflicts() {
        let mut view = StatusView::new();
        view.set_status(status_with_conflicts());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('f')));
        assert_eq!(action, StatusAction::JumpToConflict);
        assert_eq!(view.selected_index, 1);
    }

    // =============================================================================
    // Restore tests (r/R keys)
    // =============================================================================

    #[test]
    fn test_r_key_returns_restore_file() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('r')));
        match action {
            StatusAction::RestoreFile { file_path } => {
                assert_eq!(file_path, "src/main.rs");
            }
            _ => panic!("Expected RestoreFile action, got {:?}", action),
        }
    }

    #[test]
    fn test_r_uppercase_returns_restore_all() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('R')));
        assert_eq!(action, StatusAction::RestoreAll);
    }

    #[test]
    fn test_r_key_different_from_r_uppercase() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        // Lowercase r = file restore
        let action_file = view.handle_key(KeyEvent::from(KeyCode::Char('r')));
        assert!(matches!(action_file, StatusAction::RestoreFile { .. }));

        // Uppercase R = all restore
        let action_all = view.handle_key(KeyEvent::from(KeyCode::Char('R')));
        assert_eq!(action_all, StatusAction::RestoreAll);
    }

    #[test]
    fn test_r_key_no_file_selected() {
        let mut view = StatusView::new();
        // No status set
        let action = view.handle_key(KeyEvent::from(KeyCode::Char('r')));
        assert_eq!(action, StatusAction::None);
    }

    #[test]
    fn test_r_key_empty_status() {
        let mut view = StatusView::new();
        let empty_status = Status {
            files: vec![],
            has_conflicts: false,
            working_copy_change_id: ChangeId::new("abc".to_string()),
            parent_change_id: ChangeId::new("xyz".to_string()),
        };
        view.set_status(empty_status);

        // r with no files → None
        let action = view.handle_key(KeyEvent::from(KeyCode::Char('r')));
        assert_eq!(action, StatusAction::None);
    }

    #[test]
    fn test_restore_all_empty_status() {
        let mut view = StatusView::new();
        let empty_status = Status {
            files: vec![],
            has_conflicts: false,
            working_copy_change_id: ChangeId::new("abc".to_string()),
            parent_change_id: ChangeId::new("xyz".to_string()),
        };
        view.set_status(empty_status);

        // R with no files → None (guarded)
        let action = view.handle_key(KeyEvent::from(KeyCode::Char('R')));
        assert_eq!(action, StatusAction::None);
    }

    #[test]
    fn test_restore_keys_ignored_in_commit_input_mode() {
        let mut view = StatusView::new();
        view.set_status(sample_status());
        view.start_commit_input();

        // r should be treated as text input, not restore
        let action = view.handle_key(KeyEvent::from(KeyCode::Char('r')));
        assert_eq!(action, StatusAction::None);
        assert_eq!(view.input_buffer, "r");
    }

    // =============================================================================
    // DiffEdit tests (E key)
    // =============================================================================

    #[test]
    fn test_e_uppercase_returns_diffedit() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('E')));
        match action {
            StatusAction::DiffEdit { file_path } => {
                assert_eq!(file_path, "src/main.rs");
            }
            _ => panic!("Expected DiffEdit action, got {:?}", action),
        }
    }

    #[test]
    fn test_f_key_without_conflicts() {
        let mut view = StatusView::new();
        view.set_status(sample_status());

        let action = view.handle_key(KeyEvent::from(KeyCode::Char('f')));
        assert_eq!(action, StatusAction::None);
        assert_eq!(view.selected_index, 0);
    }
}