stt-cli 0.2.1

Speech to text Cli using Groq API and OpenAI API
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<()> {
    // Setup
    let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
    let recording_state = Arc::new(RecordingState::new());
    let service = HotkeyService::new(recording_state, shutdown_rx)?;

    // Register a test hotkey
    service.register_hotkey("Alt+T")?;

    // Start the service
    let service_handle = tokio::spawn(async move {
        service.run().await;
    });

    // Wait a bit to ensure service is running
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Trigger shutdown
    shutdown_tx.send(())?;

    // Wait for service to stop with timeout
    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<()> {
    // Setup
    let (shutdown_tx, shutdown_rx) = broadcast::channel(1);
    let recording_state = Arc::new(RecordingState::new());
    let service = HotkeyService::new(recording_state.clone(), shutdown_rx)?;

    // Register test hotkey
    service.register_hotkey("Alt+R")?;

    // Start service in background
    let service_handle = tokio::spawn(async move {
        service.run().await;
    });

    // Wait for service to start
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Initial state should be inactive
    assert!(!recording_state.is_active());

    // Simulate hotkey press (note: this is a mock test since we can't actually trigger global hotkeys in tests)
    // In a real scenario, pressing Alt+R would trigger this

    // Cleanup
    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)?;

    // Test valid hotkey registration
    assert!(service.register_hotkey("Alt+A").is_ok());
    assert!(service.register_hotkey("Ctrl+Shift+B").is_ok());

    // Test invalid hotkey format
    assert!(service.register_hotkey("Invalid").is_err());

    Ok(())
}