From 4530a09389b5919644d754769ead774bf6b31907 Mon Sep 17 00:00:00 2001 From: Lahfir Date: Tue, 19 May 2026 12:32:07 -0700 Subject: [PATCH] test: make focus-window confirmation deterministic Avoid wall-clock scheduler dependence in the focus confirmation hysteresis test while keeping the production polling timeout unchanged. --- crates/core/src/commands/focus_window.rs | 38 ++++++++++++++---------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/crates/core/src/commands/focus_window.rs b/crates/core/src/commands/focus_window.rs index 4fd801d9..cce53f6d 100644 --- a/crates/core/src/commands/focus_window.rs +++ b/crates/core/src/commands/focus_window.rs @@ -6,10 +6,8 @@ use crate::{ use serde_json::{Value, json}; use std::time::{Duration, Instant}; -#[cfg(not(test))] const FOCUS_SETTLE_TIMEOUT_MS: u64 = 750; -#[cfg(test)] -const FOCUS_SETTLE_TIMEOUT_MS: u64 = 250; +const FOCUS_POLL_INTERVAL_MS: u64 = 50; const FOCUS_CONFIRMATIONS: u8 = 2; pub struct FocusWindowArgs { @@ -61,11 +59,25 @@ fn wait_for_focused_window( adapter: &dyn PlatformAdapter, window_id: &str, app: Option, +) -> Result { + wait_for_focused_window_with_poll_interval( + adapter, + window_id, + app.as_deref(), + Duration::from_millis(FOCUS_POLL_INTERVAL_MS), + ) +} + +fn wait_for_focused_window_with_poll_interval( + adapter: &dyn PlatformAdapter, + window_id: &str, + app: Option<&str>, + poll_interval: Duration, ) -> Result { let deadline = Instant::now() + Duration::from_millis(FOCUS_SETTLE_TIMEOUT_MS); let mut confirmations = 0; loop { - match observed_focused_window(adapter, app.as_deref())? { + match observed_focused_window(adapter, app)? { Some(window) if window.id == window_id => { confirmations += 1; if confirmations >= FOCUS_CONFIRMATIONS { @@ -87,7 +99,9 @@ fn wait_for_focused_window( )); } - std::thread::sleep(Duration::from_millis(50)); + if !poll_interval.is_zero() { + std::thread::sleep(poll_interval); + } } } @@ -245,17 +259,11 @@ mod tests { focused_window_supported: true, }; - let value = execute( - FocusWindowArgs { - window_id: Some(target.id), - app: None, - title: None, - }, - &adapter, - ) - .unwrap(); + let value = + wait_for_focused_window_with_poll_interval(&adapter, &target.id, None, Duration::ZERO) + .unwrap(); - assert_eq!(value["focused"]["id"], "w1"); + assert_eq!(value.id, "w1"); assert_eq!(*adapter.focused_window_calls.lock().unwrap(), 4); } }