use crate::hotkey_service::HotkeyService;
use crate::audio_state::RecordingState;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use anyhow::Result;
#[tokio::test]
async fn test_hotkey_service_shutdown() -> Result<()> {
let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
let recording_state = Arc::new(RecordingState::new());
let service = HotkeyService::new(recording_state, shutdown_rx)?;
service.register_hotkey("Alt+T")?;
let service_handle = tokio::spawn(async move {
service.run().await;
});
tokio::time::sleep(Duration::from_millis(100)).await;
shutdown_tx.send(())?;
match tokio::time::timeout(Duration::from_secs(3), service_handle).await {
Ok(result) => {
result?;
Ok(())
}
Err(_) => {
panic!("Hotkey service did not shut down within timeout");
}
}
}
#[tokio::test]
async fn test_hotkey_toggle_state() -> Result<()> {
let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
let recording_state = Arc::new(RecordingState::new());
let service = HotkeyService::new(recording_state.clone(), shutdown_rx)?;
service.register_hotkey("Alt+R")?;
let service_handle = tokio::spawn(async move {
service.run().await;
});
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(!recording_state.is_active());
shutdown_tx.send(())?;
let _ = tokio::time::timeout(Duration::from_secs(3), service_handle).await;
Ok(())
}
#[test]
fn test_hotkey_registration() -> Result<()> {
let (_, shutdown_rx) = broadcast::channel(1);
let recording_state = Arc::new(RecordingState::new());
let mut service = HotkeyService::new(recording_state, shutdown_rx)?;
assert!(service.register_hotkey("Alt+A").is_ok());
assert!(service.register_hotkey("Ctrl+Shift+B").is_ok());
assert!(service.register_hotkey("Invalid").is_err());
Ok(())
}