Skip to main content

jellytau_lib/commands/
connectivity.rs

1//! Server-reachability / connectivity commands.
2//!
3//! TRACES: UR-043 | IR-027 | DR-055
4
5use crate::connectivity::{ConnectivityMonitor, ConnectivityStatus};
6use std::sync::Arc;
7use tauri::State;
8
9/// Wrapper for ConnectivityMonitor managed state
10pub struct ConnectivityMonitorWrapper(pub Arc<tokio::sync::Mutex<ConnectivityMonitor>>);
11
12/// Check if the server is currently reachable
13#[tauri::command]
14#[specta::specta]
15pub async fn connectivity_check_server(
16    state: State<'_, ConnectivityMonitorWrapper>,
17) -> Result<bool, String> {
18    let monitor = state.0.lock().await;
19    Ok(monitor.check_reachability().await)
20}
21
22/// Set the server URL and trigger an immediate check
23#[tauri::command]
24#[specta::specta]
25pub async fn connectivity_set_server_url(
26    url: String,
27    state: State<'_, ConnectivityMonitorWrapper>,
28) -> Result<(), String> {
29    let monitor = state.0.lock().await;
30    monitor.set_server_url(url).await;
31    Ok(())
32}
33
34/// Get the current connectivity status
35#[tauri::command]
36#[specta::specta]
37pub async fn connectivity_get_status(
38    state: State<'_, ConnectivityMonitorWrapper>,
39) -> Result<ConnectivityStatus, String> {
40    let monitor = state.0.lock().await;
41    Ok(monitor.get_status().await)
42}
43
44/// Start monitoring connectivity with adaptive polling
45#[tauri::command]
46#[specta::specta]
47pub async fn connectivity_start_monitoring(
48    state: State<'_, ConnectivityMonitorWrapper>,
49) -> Result<(), String> {
50    let monitor = state.0.lock().await;
51    monitor.start_monitoring().await;
52    Ok(())
53}
54
55/// Stop monitoring connectivity
56#[tauri::command]
57#[specta::specta]
58pub async fn connectivity_stop_monitoring(
59    state: State<'_, ConnectivityMonitorWrapper>,
60) -> Result<(), String> {
61    let monitor = state.0.lock().await;
62    monitor.stop_monitoring();
63    Ok(())
64}
65
66/// Mark the server as reachable (called after successful API calls)
67#[tauri::command]
68#[specta::specta]
69pub async fn connectivity_mark_reachable(
70    state: State<'_, ConnectivityMonitorWrapper>,
71) -> Result<(), String> {
72    let monitor = state.0.lock().await;
73    monitor.mark_reachable().await;
74    Ok(())
75}
76
77/// Mark the server as unreachable (called after failed API calls)
78#[tauri::command]
79#[specta::specta]
80pub async fn connectivity_mark_unreachable(
81    error: Option<String>,
82    state: State<'_, ConnectivityMonitorWrapper>,
83) -> Result<(), String> {
84    let monitor = state.0.lock().await;
85    monitor.mark_unreachable(error).await;
86    Ok(())
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_connectivity_monitor_wrapper_structure() {
95        // Test that wrapper can be created and holds Arc
96        // We can't instantiate ConnectivityMonitor directly in tests
97        // due to its dependencies, so we just test the wrapper type structure
98
99        // This verifies the wrapper type exists and can hold Arc<Mutex>
100        assert!(std::mem::size_of::<ConnectivityMonitorWrapper>() > 0);
101    }
102}