@@ -726,7 +726,103 @@ impl<'a> UI<'a> {
726726mod tests {
727727 use super :: * ;
728728 use chrono:: { DateTime , Utc } ;
729+ use git2:: { Repository , Signature , Time } ;
729730 use ratatui:: { backend:: TestBackend , buffer:: Buffer , Terminal } ;
731+ use std:: fs;
732+ use std:: path:: { Path , PathBuf } ;
733+ use std:: sync:: atomic:: { AtomicU64 , Ordering as CounterOrdering } ;
734+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
735+
736+ struct TestRepo {
737+ path : PathBuf ,
738+ repo : Repository ,
739+ }
740+
741+ impl TestRepo {
742+ fn new ( ) -> Self {
743+ static COUNTER : AtomicU64 = AtomicU64 :: new ( 0 ) ;
744+
745+ let unique_id = format ! (
746+ "{}_{}_{}" ,
747+ std:: process:: id( ) ,
748+ SystemTime :: now( )
749+ . duration_since( UNIX_EPOCH )
750+ . unwrap( )
751+ . as_nanos( ) ,
752+ COUNTER . fetch_add( 1 , CounterOrdering :: SeqCst )
753+ ) ;
754+ let path = std:: env:: temp_dir ( ) . join ( format ! ( "gitlogue_ui_test_{unique_id}" ) ) ;
755+ fs:: create_dir_all ( & path) . unwrap ( ) ;
756+
757+ let repo = Repository :: init ( & path) . unwrap ( ) ;
758+ let mut config = repo. config ( ) . unwrap ( ) ;
759+ config. set_str ( "user.name" , "Test User" ) . unwrap ( ) ;
760+ config
. set_str ( "user.email" , "[email protected] " ) . unwrap ( ) ; 761+
762+ Self { path, repo }
763+ }
764+
765+ fn write_file ( & self , relative_path : & str , content : & str ) {
766+ let file_path = self . path . join ( relative_path) ;
767+ file_path
768+ . parent ( )
769+ . map ( fs:: create_dir_all)
770+ . transpose ( )
771+ . unwrap ( ) ;
772+ fs:: write ( file_path, content) . unwrap ( ) ;
773+ }
774+
775+ fn stage_file ( & self , relative_path : & str ) {
776+ let mut index = self . repo . index ( ) . unwrap ( ) ;
777+ index. add_path ( Path :: new ( relative_path) ) . unwrap ( ) ;
778+ index. write ( ) . unwrap ( ) ;
779+ }
780+
781+ fn commit_file (
782+ & self ,
783+ relative_path : & str ,
784+ content : & str ,
785+ message : & str ,
786+ timestamp : i64 ,
787+ ) -> String {
788+ self . write_file ( relative_path, content) ;
789+ self . stage_file ( relative_path) ;
790+
791+ let mut index = self . repo . index ( ) . unwrap ( ) ;
792+ let tree_id = index. write_tree ( ) . unwrap ( ) ;
793+ let tree = self . repo . find_tree ( tree_id) . unwrap ( ) ;
794+ let signature =
795+ Signature :: new ( "Test User" , "[email protected] " , & Time :: new ( timestamp
, 0 ) ) . unwrap ( ) ; 796+ let parent = self
797+ . repo
798+ . head ( )
799+ . ok ( )
800+ . and_then ( |head| head. peel_to_commit ( ) . ok ( ) ) ;
801+
802+ let oid = match parent. as_ref ( ) {
803+ Some ( parent_commit) => self . repo . commit (
804+ Some ( "HEAD" ) ,
805+ & signature,
806+ & signature,
807+ message,
808+ & tree,
809+ & [ parent_commit] ,
810+ ) ,
811+ None => self
812+ . repo
813+ . commit ( Some ( "HEAD" ) , & signature, & signature, message, & tree, & [ ] ) ,
814+ }
815+ . unwrap ( ) ;
816+
817+ oid. to_string ( )
818+ }
819+ }
820+
821+ impl Drop for TestRepo {
822+ fn drop ( & mut self ) {
823+ let _ = fs:: remove_dir_all ( & self . path ) ;
824+ }
825+ }
730826
731827 fn metadata ( hash : & str , message : & str ) -> CommitMetadata {
732828 CommitMetadata {
@@ -740,7 +836,7 @@ mod tests {
740836 }
741837 }
742838
743- fn test_ui ( ) -> UI < ' static > {
839+ fn test_ui_with_repo < ' a > ( repo : Option < & ' a GitRepository > ) -> UI < ' a > {
744840 UI {
745841 state : UIState :: Playing ,
746842 speed_ms : 16 ,
@@ -749,7 +845,7 @@ mod tests {
749845 terminal : TerminalPane ,
750846 status_bar : StatusBarPane ,
751847 engine : AnimationEngine :: new ( 16 ) ,
752- repo : None ,
848+ repo,
753849 should_exit : Arc :: new ( AtomicBool :: new ( false ) ) ,
754850 theme : Theme :: default ( ) ,
755851 order : PlaybackOrder :: Asc ,
@@ -765,6 +861,16 @@ mod tests {
765861 }
766862 }
767863
864+ fn test_ui ( ) -> UI < ' static > {
865+ test_ui_with_repo ( None )
866+ }
867+
868+ fn apply_until_metadata_is_visible ( ui : & mut UI < ' _ > ) {
869+ while ui. engine . current_metadata ( ) . is_none ( ) {
870+ assert ! ( ui. engine. manual_step( StepMode :: Change ) ) ;
871+ }
872+ }
873+
768874 fn render_buffer ( ui : & mut UI < ' static > , width : u16 , height : u16 ) -> Buffer {
769875 let backend = TestBackend :: new ( width, height) ;
770876 let mut terminal = Terminal :: new ( backend) . unwrap ( ) ;
@@ -856,6 +962,19 @@ mod tests {
856962 assert_eq ! ( ui. playback_state, PlaybackState :: Playing ) ;
857963 }
858964
965+ #[ test]
966+ fn step_helpers_pause_playback_without_needing_checkpoints ( ) {
967+ let mut ui = test_ui ( ) ;
968+
969+ ui. step_line ( ) ;
970+ ui. step_change ( ) ;
971+ ui. step_line_back ( ) ;
972+ ui. step_change_back ( ) ;
973+
974+ assert_eq ! ( ui. playback_state, PlaybackState :: Paused ) ;
975+ assert_eq ! ( ui. state, UIState :: Playing ) ;
976+ }
977+
859978 #[ test]
860979 fn advance_to_next_commit_without_repo_finishes_ui ( ) {
861980 let mut default_ui = test_ui ( ) ;
@@ -868,6 +987,139 @@ mod tests {
868987 assert_eq ! ( diff_ui. state, UIState :: Finished ) ;
869988 }
870989
990+ #[ test]
991+ fn history_navigation_bounds_are_noops_without_targets ( ) {
992+ let mut ui = test_ui ( ) ;
993+
994+ assert ! ( !ui. play_history_commit( 0 ) ) ;
995+
996+ ui. load_commit ( metadata ( "1111111" , "only" ) ) ;
997+ ui. handle_prev ( ) ;
998+ ui. handle_next ( ) ;
999+
1000+ assert_eq ! ( ui. history_index, Some ( 0 ) ) ;
1001+ assert_eq ! ( ui. state, UIState :: Playing ) ;
1002+ }
1003+
1004+ #[ test]
1005+ fn advance_to_next_commit_uses_repo_order_and_finishes_after_last_commit ( ) -> Result < ( ) > {
1006+ let test_repo = TestRepo :: new ( ) ;
1007+ let first = test_repo. commit_file ( "src/lib.rs" , "fn first() {}\n " , "first" , 1 ) ;
1008+ let second = test_repo. commit_file ( "src/lib.rs" , "fn second() {}\n " , "second" , 2 ) ;
1009+ let repo = GitRepository :: open ( & test_repo. path ) ?;
1010+ let mut ui = test_ui_with_repo ( Some ( & repo) ) ;
1011+ ui. order = PlaybackOrder :: Asc ;
1012+
1013+ assert ! ( ui. advance_to_next_commit( ) ) ;
1014+ assert_eq ! (
1015+ ui. history. last( ) . map( |item| item. hash. as_str( ) ) ,
1016+ Some ( first. as_str( ) )
1017+ ) ;
1018+
1019+ assert ! ( ui. advance_to_next_commit( ) ) ;
1020+ assert_eq ! (
1021+ ui. history. last( ) . map( |item| item. hash. as_str( ) ) ,
1022+ Some ( second. as_str( ) )
1023+ ) ;
1024+
1025+ assert ! ( !ui. advance_to_next_commit( ) ) ;
1026+ assert_eq ! ( ui. state, UIState :: Finished ) ;
1027+
1028+ Ok ( ( ) )
1029+ }
1030+
1031+ #[ test]
1032+ fn advance_to_next_commit_restarts_from_beginning_when_looping ( ) -> Result < ( ) > {
1033+ let test_repo = TestRepo :: new ( ) ;
1034+ let only = test_repo. commit_file ( "src/lib.rs" , "fn only() {}\n " , "only" , 1 ) ;
1035+ let repo = GitRepository :: open ( & test_repo. path ) ?;
1036+ let mut ui = test_ui_with_repo ( Some ( & repo) ) ;
1037+ ui. order = PlaybackOrder :: Desc ;
1038+ ui. loop_playback = true ;
1039+
1040+ assert ! ( ui. advance_to_next_commit( ) ) ;
1041+ assert ! ( ui. advance_to_next_commit( ) ) ;
1042+
1043+ let hashes = ui
1044+ . history
1045+ . iter ( )
1046+ . map ( |item| item. hash . as_str ( ) )
1047+ . collect :: < Vec < _ > > ( ) ;
1048+
1049+ assert_eq ! ( hashes, vec![ only. as_str( ) , only. as_str( ) ] ) ;
1050+ assert_eq ! ( ui. state, UIState :: Playing ) ;
1051+
1052+ Ok ( ( ) )
1053+ }
1054+
1055+ #[ test]
1056+ fn fetch_repo_commit_supports_random_commit_specs_and_ranges ( ) -> Result < ( ) > {
1057+ let test_repo = TestRepo :: new ( ) ;
1058+ let first = test_repo. commit_file ( "src/lib.rs" , "fn first() {}\n " , "first" , 1 ) ;
1059+ let second = test_repo. commit_file ( "src/lib.rs" , "fn second() {}\n " , "second" , 2 ) ;
1060+ let third = test_repo. commit_file ( "src/lib.rs" , "fn third() {}\n " , "third" , 3 ) ;
1061+ let repo = GitRepository :: open ( & test_repo. path ) ?;
1062+
1063+ let mut random_ui = test_ui_with_repo ( Some ( & repo) ) ;
1064+ random_ui. order = PlaybackOrder :: Random ;
1065+ let random_hash = random_ui. fetch_repo_commit ( & repo) ?. hash ;
1066+ assert ! ( [ first. as_str( ) , second. as_str( ) , third. as_str( ) ] . contains( & random_hash. as_str( ) ) ) ;
1067+
1068+ let mut commit_ui = test_ui_with_repo ( Some ( & repo) ) ;
1069+ commit_ui. commit_spec = Some ( second. clone ( ) ) ;
1070+ assert_eq ! ( commit_ui. fetch_repo_commit( & repo) ?. hash, second) ;
1071+
1072+ let range = format ! ( "{first}..{third}" ) ;
1073+ repo. set_commit_range ( & range) ?;
1074+
1075+ let mut range_random_ui = test_ui_with_repo ( Some ( & repo) ) ;
1076+ range_random_ui. is_range_mode = true ;
1077+ range_random_ui. order = PlaybackOrder :: Random ;
1078+ let random_hash = range_random_ui. fetch_repo_commit ( & repo) ?. hash ;
1079+ assert ! ( [ second. as_str( ) , third. as_str( ) ] . contains( & random_hash. as_str( ) ) ) ;
1080+
1081+ repo. set_commit_range ( & range) ?;
1082+
1083+ let mut range_desc_ui = test_ui_with_repo ( Some ( & repo) ) ;
1084+ range_desc_ui. is_range_mode = true ;
1085+ range_desc_ui. order = PlaybackOrder :: Desc ;
1086+ assert_eq ! ( range_desc_ui. fetch_repo_commit( & repo) ?. hash, third) ;
1087+
1088+ Ok ( ( ) )
1089+ }
1090+
1091+ #[ test]
1092+ fn diff_mode_refreshes_working_tree_and_finishes_when_clean ( ) -> Result < ( ) > {
1093+ let clean_repo = TestRepo :: new ( ) ;
1094+ clean_repo. commit_file ( "src/lib.rs" , "fn clean() {}\n " , "clean" , 1 ) ;
1095+ let clean_git_repo = GitRepository :: open ( & clean_repo. path ) ?;
1096+ let mut clean_ui = test_ui_with_repo ( Some ( & clean_git_repo) ) ;
1097+ clean_ui. set_diff_mode ( Some ( DiffMode :: Staged ) ) ;
1098+
1099+ assert ! ( !clean_ui. advance_to_next_commit( ) ) ;
1100+ assert_eq ! ( clean_ui. state, UIState :: Finished ) ;
1101+
1102+ let dirty_repo = TestRepo :: new ( ) ;
1103+ dirty_repo. commit_file ( "src/lib.rs" , "fn clean() {}\n " , "clean" , 1 ) ;
1104+ dirty_repo. write_file ( "src/lib.rs" , "fn dirty() {\n println!(\" hi\" );\n }\n " ) ;
1105+ dirty_repo. stage_file ( "src/lib.rs" ) ;
1106+
1107+ let dirty_git_repo = GitRepository :: open ( & dirty_repo. path ) ?;
1108+ let mut dirty_ui = test_ui_with_repo ( Some ( & dirty_git_repo) ) ;
1109+ dirty_ui. set_diff_mode ( Some ( DiffMode :: Staged ) ) ;
1110+
1111+ assert ! ( dirty_ui. advance_to_next_commit( ) ) ;
1112+ assert_eq ! (
1113+ dirty_ui
1114+ . history
1115+ . last( )
1116+ . map( |item| ( item. hash. as_str( ) , item. message. as_str( ) ) ) ,
1117+ Some ( ( "working-tree" , "Staged changes" ) )
1118+ ) ;
1119+
1120+ Ok ( ( ) )
1121+ }
1122+
8711123 #[ test]
8721124 fn render_menu_shows_dialog_and_selected_item ( ) {
8731125 let mut ui = test_ui ( ) ;
@@ -909,4 +1161,16 @@ mod tests {
9091161
9101162 assert_eq ! ( rect, Rect :: new( 4 , 2 , 20 , 10 ) ) ;
9111163 }
1164+
1165+ #[ test]
1166+ fn render_playing_state_uses_loaded_metadata_for_file_tree_and_status_bar ( ) {
1167+ let mut ui = test_ui ( ) ;
1168+ ui. load_commit ( metadata ( "4444444" , "render commit" ) ) ;
1169+ apply_until_metadata_is_visible ( & mut ui) ;
1170+
1171+ let text = buffer_text ( & render_buffer ( & mut ui, 80 , 40 ) ) ;
1172+
1173+ assert ! ( text. contains( "hash: 4444444" ) ) ;
1174+ assert ! ( text. contains( "render commit" ) ) ;
1175+ }
9121176}
0 commit comments