Skip to main content

#
jellytau_lib/commands/
sessions.rs

    1//! TRACES: UR-010 | JA-021 | DR-037
    2
    3use crate::jellyfin::client::SessionInfo;
    4use crate::session_poller::{PollingHint, SessionPollerManager};
    5use std::sync::Arc;
    6use tauri::State;
    7
    8/// Tauri state wrapper for SessionPollerManager
    9pub struct SessionPollerWrapper(pub Arc<SessionPollerManager>);
    10
    11/// Set polling frequency hint based on UI state
    12#[tauri::command]
    13#[specta::specta]
    14pub fn sessions_set_polling_hint(
    15    poller: State<'_, SessionPollerWrapper>,
    16    hint: String,
    17) -> Result<(), String> {
    18    let parsed_hint = match hint.as_str() {
    19        "cast_active" => PollingHint::CastActive,
    20        "cast_discovery" => PollingHint::CastDiscovery,
    21        "normal" => PollingHint::Normal,
    22        _ => return Err(format!("Invalid polling hint: {}", hint)),
    23    };
    24
    25    poller.0.set_polling_hint(parsed_hint);
    26    Ok(())
    27}
    28
    29/// Manually trigger a session poll (for refresh button)
    30#[tauri::command]
    31#[specta::specta]
    32pub async fn sessions_poll_now(
    33    poller: State<'_, SessionPollerWrapper>,
    34) -> Result<Vec<SessionInfo>, String> {
    35    poller.0.poll_now().await
    36}
    37
    38#[cfg(test)]
    39mod tests {
    40    use super::*;
    41
    42    #[test]
    43    fn test_polling_hint_parsing() {
    44        // Test valid hints
    45        assert_eq!(
    46            match "cast_active" {
    47                "cast_active" => Some(PollingHint::CastActive),
    48                "cast_discovery" => Some(PollingHint::CastDiscovery),
    49                "normal" => Some(PollingHint::Normal),
    50                _ => None,
    51            },
    52            Some(PollingHint::CastActive)
    53        );
    54
    55        assert_eq!(
    56            match "cast_discovery" {
    57                "cast_active" => Some(PollingHint::CastActive),
    58                "cast_discovery" => Some(PollingHint::CastDiscovery),
    59                "normal" => Some(PollingHint::Normal),
    60                _ => None,
    61            },
    62            Some(PollingHint::CastDiscovery)
    63        );
    64
    65        assert_eq!(
    66            match "normal" {
    67                "cast_active" => Some(PollingHint::CastActive),
    68                "cast_discovery" => Some(PollingHint::CastDiscovery),
    69                "normal" => Some(PollingHint::Normal),
    70                _ => None,
    71            },
    72            Some(PollingHint::Normal)
    73        );
    74    }
    75
    76    #[test]
    77    fn test_invalid_polling_hint() {
    78        // Test invalid hint
    79        let result = match "invalid" {
    80            "cast_active" => Ok(PollingHint::CastActive),
    81            "cast_discovery" => Ok(PollingHint::CastDiscovery),
    82            "normal" => Ok(PollingHint::Normal),
    83            _ => Err("Invalid polling hint"),
    84        };
    85
    86        assert!(result.is_err());
    87    }
    88
    89    #[test]
    90    fn test_session_poller_wrapper_structure() {
    91        // Test that wrapper type structure is correct
    92        assert!(std::mem::size_of::<SessionPollerWrapper>() > 0);
    93    }
    94
    95    #[test]
    96    fn test_polling_hints_exist() {
    97        // Verify polling hint variants exist
    98        let _ = PollingHint::CastActive;
    99        let _ = PollingHint::CastDiscovery;
    100        let _ = PollingHint::Normal;
    101    }
    102}