Skip to content

Commit 576e964

Browse files
committed
test(ui): add unit tests for state transitions and overlay rendering
1 parent cc8de91 commit 576e964

1 file changed

Lines changed: 189 additions & 0 deletions

File tree

‎src/ui.rs‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,3 +721,192 @@ impl<'a> UI<'a> {
721721
}
722722
}
723723
}
724+
725+
#[cfg(test)]
726+
mod tests {
727+
use super::*;
728+
use chrono::{DateTime, Utc};
729+
use ratatui::{backend::TestBackend, buffer::Buffer, Terminal};
730+
731+
fn metadata(hash: &str, message: &str) -> CommitMetadata {
732+
CommitMetadata {
733+
hash: hash.to_string(),
734+
author: "Author".to_string(),
735+
date: DateTime::from_timestamp(1_704_067_200, 0)
736+
.unwrap()
737+
.with_timezone(&Utc),
738+
message: message.to_string(),
739+
changes: vec![],
740+
}
741+
}
742+
743+
fn test_ui() -> UI<'static> {
744+
UI {
745+
state: UIState::Playing,
746+
speed_ms: 16,
747+
file_tree: FileTreePane::new(),
748+
editor: EditorPane,
749+
terminal: TerminalPane,
750+
status_bar: StatusBarPane,
751+
engine: AnimationEngine::new(16),
752+
repo: None,
753+
should_exit: Arc::new(AtomicBool::new(false)),
754+
theme: Theme::default(),
755+
order: PlaybackOrder::Asc,
756+
loop_playback: false,
757+
commit_spec: None,
758+
is_range_mode: false,
759+
diff_mode: None,
760+
playback_state: PlaybackState::Playing,
761+
history: Vec::new(),
762+
history_index: None,
763+
menu_index: 0,
764+
prev_state: None,
765+
}
766+
}
767+
768+
fn render_buffer(ui: &mut UI<'static>, width: u16, height: u16) -> Buffer {
769+
let backend = TestBackend::new(width, height);
770+
let mut terminal = Terminal::new(backend).unwrap();
771+
terminal.draw(|f| ui.render(f)).unwrap();
772+
terminal.backend().buffer().clone()
773+
}
774+
775+
fn buffer_text(buffer: &Buffer) -> String {
776+
(0..buffer.area.height)
777+
.map(|y| {
778+
(0..buffer.area.width)
779+
.map(|x| buffer[(x, y)].symbol())
780+
.collect::<Vec<_>>()
781+
.join("")
782+
})
783+
.collect::<Vec<_>>()
784+
.join("\n")
785+
}
786+
787+
#[test]
788+
fn load_commit_tracks_history_navigation_and_truncates_future_entries() {
789+
let mut ui = test_ui();
790+
let first = metadata("1111111", "first");
791+
let second = metadata("2222222", "second");
792+
let third = metadata("3333333", "third");
793+
794+
ui.load_commit(first.clone());
795+
ui.load_commit(second.clone());
796+
797+
assert_eq!(ui.state, UIState::Playing);
798+
assert_eq!(ui.history_index, Some(1));
799+
assert_eq!(
800+
ui.history
801+
.iter()
802+
.map(|item| item.hash.as_str())
803+
.collect::<Vec<_>>(),
804+
vec!["1111111", "2222222"]
805+
);
806+
807+
ui.handle_prev();
808+
assert_eq!(ui.history_index, Some(0));
809+
810+
ui.handle_next();
811+
assert_eq!(ui.history_index, Some(1));
812+
813+
ui.handle_prev();
814+
ui.load_commit(third);
815+
assert_eq!(ui.history_index, Some(1));
816+
assert_eq!(
817+
ui.history
818+
.iter()
819+
.map(|item| item.hash.as_str())
820+
.collect::<Vec<_>>(),
821+
vec!["1111111", "3333333"]
822+
);
823+
}
824+
825+
#[test]
826+
fn menu_round_trip_restores_playing_state_from_waiting() {
827+
let mut ui = test_ui();
828+
ui.state = UIState::WaitingForNext {
829+
resume_at: Instant::now() + Duration::from_secs(1),
830+
};
831+
832+
ui.open_menu();
833+
assert_eq!(ui.state, UIState::Menu);
834+
assert_eq!(ui.menu_index, 0);
835+
assert!(matches!(
836+
ui.prev_state.as_deref(),
837+
Some(UIState::WaitingForNext { .. })
838+
));
839+
840+
ui.close_menu();
841+
assert_eq!(ui.state, UIState::Playing);
842+
assert!(ui.prev_state.is_none());
843+
}
844+
845+
#[test]
846+
fn toggle_pause_and_manual_pause_update_playback_state() {
847+
let mut ui = test_ui();
848+
849+
ui.toggle_pause();
850+
assert_eq!(ui.playback_state, PlaybackState::Paused);
851+
852+
ui.ensure_manual_pause();
853+
assert_eq!(ui.playback_state, PlaybackState::Paused);
854+
855+
ui.toggle_pause();
856+
assert_eq!(ui.playback_state, PlaybackState::Playing);
857+
}
858+
859+
#[test]
860+
fn advance_to_next_commit_without_repo_finishes_ui() {
861+
let mut default_ui = test_ui();
862+
assert!(!default_ui.advance_to_next_commit());
863+
assert_eq!(default_ui.state, UIState::Finished);
864+
865+
let mut diff_ui = test_ui();
866+
diff_ui.set_diff_mode(Some(DiffMode::Staged));
867+
assert!(!diff_ui.advance_to_next_commit());
868+
assert_eq!(diff_ui.state, UIState::Finished);
869+
}
870+
871+
#[test]
872+
fn render_menu_shows_dialog_and_selected_item() {
873+
let mut ui = test_ui();
874+
ui.state = UIState::Menu;
875+
ui.menu_index = 1;
876+
ui.engine.dialog_title = Some("Open File...".to_string());
877+
ui.engine.dialog_typing_text = "src/ui.rs".to_string();
878+
879+
let text = buffer_text(&render_buffer(&mut ui, 80, 24));
880+
881+
assert!(text.contains("Menu (Esc to close)"));
882+
assert!(text.contains("> About"));
883+
assert!(text.contains("Open File"));
884+
assert!(text.contains("src/ui.rs"));
885+
}
886+
887+
#[test]
888+
fn render_keybindings_and_about_overlays_include_expected_copy() {
889+
let mut keybindings_ui = test_ui();
890+
keybindings_ui.state = UIState::KeyBindings;
891+
let keybindings = buffer_text(&render_buffer(&mut keybindings_ui, 80, 24));
892+
893+
assert!(keybindings.contains("Key Bindings (Esc to close)"));
894+
assert!(keybindings.contains("h / l Step line back / forward"));
895+
assert!(keybindings.contains("p / n Previous / Next commit"));
896+
897+
let mut about_ui = test_ui();
898+
about_ui.state = UIState::About;
899+
let about = buffer_text(&render_buffer(&mut about_ui, 80, 24));
900+
901+
assert!(about.contains("About (Esc to close)"));
902+
assert!(about.contains("Version 0.8.0"));
903+
assert!(about.contains("https://github.com/unhappychoice/gitlogue"));
904+
}
905+
906+
#[test]
907+
fn centered_rect_clamps_requested_size_to_outer_area() {
908+
let rect = UI::centered_rect(Rect::new(4, 2, 20, 10), 40, 12);
909+
910+
assert_eq!(rect, Rect::new(4, 2, 20, 10));
911+
}
912+
}

0 commit comments

Comments
 (0)