Skip to content

Commit deb44e9

Browse files
committed
test(ui): extract playback state transitions into helpers and add regression tests
1 parent 74ae357 commit deb44e9

1 file changed

Lines changed: 122 additions & 50 deletions

File tree

‎src/ui.rs‎

Lines changed: 122 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,31 @@ impl<'a> UI<'a> {
8080
) -> Self {
8181
let should_exit = Arc::new(AtomicBool::new(false));
8282
Self::setup_signal_handler(should_exit.clone());
83+
Self::build(
84+
speed_ms,
85+
repo,
86+
theme,
87+
order,
88+
loop_playback,
89+
commit_spec,
90+
is_range_mode,
91+
speed_rules,
92+
should_exit,
93+
)
94+
}
8395

96+
#[allow(clippy::too_many_arguments)]
97+
fn build(
98+
speed_ms: u64,
99+
repo: Option<&'a GitRepository>,
100+
theme: Theme,
101+
order: PlaybackOrder,
102+
loop_playback: bool,
103+
commit_spec: Option<String>,
104+
is_range_mode: bool,
105+
speed_rules: Vec<SpeedRule>,
106+
should_exit: Arc<AtomicBool>,
107+
) -> Self {
84108
let mut engine = AnimationEngine::new(speed_ms);
85109
engine.set_speed_rules(speed_rules);
86110

@@ -399,6 +423,29 @@ impl<'a> UI<'a> {
399423
matches!(key.code, KeyCode::Char('q')) || Self::is_ctrl_c(key)
400424
}
401425

426+
fn advance_state_after_tick(&mut self, now: Instant) -> bool {
427+
match self.state {
428+
UIState::Playing if self.engine.is_finished() => {
429+
self.state = if self.repo.is_some() {
430+
UIState::WaitingForNext {
431+
resume_at: now + Duration::from_millis(self.speed_ms * 100),
432+
}
433+
} else {
434+
UIState::Finished
435+
};
436+
}
437+
UIState::WaitingForNext { resume_at }
438+
if now >= resume_at && self.playback_state != PlaybackState::Paused =>
439+
{
440+
self.advance_to_next_commit();
441+
}
442+
UIState::Finished => return false,
443+
_ => {}
444+
}
445+
446+
true
447+
}
448+
402449
/// Runs the main UI event loop.
403450
pub fn run(&mut self) -> Result<()> {
404451
enable_raw_mode()?;
@@ -455,35 +502,8 @@ impl<'a> UI<'a> {
455502
}
456503
}
457504

458-
// State machine
459-
match self.state {
460-
UIState::Playing => {
461-
if self.engine.is_finished() {
462-
if self.repo.is_some() {
463-
self.state = UIState::WaitingForNext {
464-
resume_at: Instant::now()
465-
+ Duration::from_millis(self.speed_ms * 100),
466-
};
467-
} else {
468-
self.state = UIState::Finished;
469-
}
470-
}
471-
}
472-
UIState::WaitingForNext { resume_at } => {
473-
if Instant::now() >= resume_at {
474-
if matches!(self.playback_state, PlaybackState::Paused) {
475-
continue;
476-
}
477-
478-
self.advance_to_next_commit();
479-
}
480-
}
481-
UIState::Menu | UIState::KeyBindings | UIState::About => {
482-
// Paused while in menu/dialog
483-
}
484-
UIState::Finished => {
485-
break;
486-
}
505+
if !self.advance_state_after_tick(Instant::now()) {
506+
break;
487507
}
488508
}
489509

@@ -859,28 +879,17 @@ mod tests {
859879
}
860880

861881
fn test_ui_with_repo<'a>(repo: Option<&'a GitRepository>) -> UI<'a> {
862-
UI {
863-
state: UIState::Playing,
864-
speed_ms: 16,
865-
file_tree: FileTreePane::new(),
866-
editor: EditorPane,
867-
terminal: TerminalPane,
868-
status_bar: StatusBarPane,
869-
engine: AnimationEngine::new(16),
882+
UI::build(
883+
16,
870884
repo,
871-
should_exit: Arc::new(AtomicBool::new(false)),
872-
theme: Theme::default(),
873-
order: PlaybackOrder::Asc,
874-
loop_playback: false,
875-
commit_spec: None,
876-
is_range_mode: false,
877-
diff_mode: None,
878-
playback_state: PlaybackState::Playing,
879-
history: Vec::new(),
880-
history_index: None,
881-
menu_index: 0,
882-
prev_state: None,
883-
}
885+
Theme::default(),
886+
PlaybackOrder::Asc,
887+
false,
888+
None,
889+
false,
890+
Vec::new(),
891+
Arc::new(AtomicBool::new(false)),
892+
)
884893
}
885894

886895
fn test_ui() -> UI<'static> {
@@ -1273,4 +1282,67 @@ mod tests {
12731282
assert!(text.contains("hash: 4444444"));
12741283
assert!(text.contains("render commit"));
12751284
}
1285+
1286+
#[test]
1287+
fn advance_state_after_tick_transitions_finished_playback_to_waiting_or_finished() -> Result<()>
1288+
{
1289+
let now = Instant::now();
1290+
1291+
let standalone_ui = {
1292+
let mut ui = test_ui();
1293+
ui.engine.state = crate::animation::AnimationState::Finished;
1294+
ui
1295+
};
1296+
let mut standalone_ui = standalone_ui;
1297+
assert!(standalone_ui.advance_state_after_tick(now));
1298+
assert_eq!(standalone_ui.state, UIState::Finished);
1299+
1300+
let test_repo = TestRepo::new();
1301+
test_repo.commit_file("src/lib.rs", "fn keep() {}\n", "keep", 1);
1302+
let repo = GitRepository::open(&test_repo.path)?;
1303+
let mut repo_ui = test_ui_with_repo(Some(&repo));
1304+
repo_ui.engine.state = crate::animation::AnimationState::Finished;
1305+
1306+
assert!(repo_ui.advance_state_after_tick(now));
1307+
assert!(matches!(
1308+
repo_ui.state,
1309+
UIState::WaitingForNext { resume_at }
1310+
if resume_at == now + Duration::from_millis(repo_ui.speed_ms * 100)
1311+
));
1312+
1313+
Ok(())
1314+
}
1315+
1316+
#[test]
1317+
fn advance_state_after_tick_waits_while_paused_and_advances_when_resumed() -> Result<()> {
1318+
let test_repo = TestRepo::new();
1319+
let first = test_repo.commit_file("src/lib.rs", "fn first() {}\n", "first", 1);
1320+
let repo = GitRepository::open(&test_repo.path)?;
1321+
let resume_at = Instant::now() - Duration::from_millis(1);
1322+
let mut ui = test_ui_with_repo(Some(&repo));
1323+
ui.state = UIState::WaitingForNext { resume_at };
1324+
ui.playback_state = PlaybackState::Paused;
1325+
1326+
assert!(ui.advance_state_after_tick(Instant::now()));
1327+
assert!(matches!(ui.state, UIState::WaitingForNext { .. }));
1328+
assert!(ui.history.is_empty());
1329+
1330+
ui.playback_state = PlaybackState::Playing;
1331+
assert!(ui.advance_state_after_tick(Instant::now()));
1332+
assert_eq!(ui.state, UIState::Playing);
1333+
assert_eq!(
1334+
ui.history.last().map(|item| item.hash.as_str()),
1335+
Some(first.as_str())
1336+
);
1337+
1338+
Ok(())
1339+
}
1340+
1341+
#[test]
1342+
fn advance_state_after_tick_stops_loop_once_ui_is_finished() {
1343+
let mut ui = test_ui();
1344+
ui.state = UIState::Finished;
1345+
1346+
assert!(!ui.advance_state_after_tick(Instant::now()));
1347+
}
12761348
}

0 commit comments

Comments
 (0)