Files
codex/codex-rs/tui/src/app/tests/stream_animation_tests.rs
T
Tamir Duberstein a95391160a Drive TUI stream animation from the foreground loop (#40644)
## Why

Stream animation ticks generated on a background thread can accumulate while the
TUI is busy, causing stale ticks to run after the animation stops or the chat
widget is replaced.

## What changed

- Replace the animation thread and `CommitTick` app event with a Tokio interval
  owned by the foreground event loop.
- Delay missed ticks instead of replaying a burst, and let pending app events run
  before animation work.
- Drop the interval when animation stops or the chat widget is replaced, while
  preserving `CommitTick` session-log entries.

## Testing

Add a paused-time test covering stable pacing, missed ticks, restart behavior,
and chat-widget replacement.

GitOrigin-RevId: d0561d3f8962b5e05a728463e76aa8ec83cd5a31
2026-08-25 17:17:01 +00:00

52 lines
2.2 KiB
Rust

use super::*;
use futures::FutureExt;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn stream_animation_preserves_pacing_and_discards_stopped_ticks() -> Result<()> {
let mut app = make_test_app().await;
let mut app_server = start_config_write_test_app_server(&app).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
let (replacement, _tx, _rx, _ops) = make_chatwidget_manual_with_sender().await;
time::pause();
let started_at = time::Instant::now();
app.handle_event(&mut tui, &mut app_server, AppEvent::StartCommitAnimation)
.await?;
time::advance(COMMIT_ANIMATION_TICK / 2).await;
app.handle_event(&mut tui, &mut app_server, AppEvent::StartCommitAnimation)
.await?;
time::advance(COMMIT_ANIMATION_TICK).await;
let interval = app.commit_animation.as_mut().expect("animation started");
assert_eq!(
interval.tick().now_or_never(),
Some(started_at + COMMIT_ANIMATION_TICK)
);
// A blocked UI consumes one overdue tick, not a burst of obsolete ticks.
time::advance(COMMIT_ANIMATION_TICK * 4).await;
assert!(interval.tick().now_or_never().is_some());
assert!(interval.tick().now_or_never().is_none());
let next_tick = time::Instant::now() + COMMIT_ANIMATION_TICK;
time::advance(COMMIT_ANIMATION_TICK + Duration::from_millis(/*millis*/ 1)).await;
assert_eq!(interval.tick().now_or_never(), Some(next_tick));
// Restart while the previous timer is overdue; only the new deadline survives.
time::advance(COMMIT_ANIMATION_TICK * 3).await;
app.handle_event(&mut tui, &mut app_server, AppEvent::StopCommitAnimation)
.await?;
assert!(app.commit_animation.is_none());
app.handle_event(&mut tui, &mut app_server, AppEvent::StartCommitAnimation)
.await?;
let interval = app.commit_animation.as_mut().expect("animation restarted");
assert!(interval.tick().now_or_never().is_none());
let next_tick = time::Instant::now() + COMMIT_ANIMATION_TICK;
time::advance(COMMIT_ANIMATION_TICK + Duration::from_millis(/*millis*/ 1)).await;
assert_eq!(interval.tick().now_or_never(), Some(next_tick));
app.replace_chat_widget(replacement);
assert!(app.commit_animation.is_none());
time::resume();
Ok(())
}