Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

test: add unit tests for shutdown wiring and spawn+channel pattern

Louis Beaumont5b765bacedeb
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
index 91a23ae48..3d0c02a90 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/embedded_server.rs
@@ -406,3 +406,50 @@ pub async fn start_embedded_server(
Ok(EmbeddedServerHandle { shutdown_tx, ui_recorder_handle })
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::sync::atomic::{AtomicBool, Ordering};
+ use std::sync::Arc;
+
+ #[test]
+ fn test_shutdown_sets_ui_recorder_stop_flag() {
+ let flag = Arc::new(AtomicBool::new(false));
+ let flag_clone = flag.clone();
+
+ let (shutdown_tx, _) = broadcast::channel::<()>(1);
+ let handle = EmbeddedServerHandle {
+ shutdown_tx,
+ ui_recorder_handle: Some(screenpipe_server::UiRecorderHandle::new_for_test(flag)),
+ };
+
+ assert!(!flag_clone.load(Ordering::Relaxed));
+ handle.shutdown();
+ assert!(flag_clone.load(Ordering::Relaxed), "shutdown must set stop_flag");
+ }
+
+ #[test]
+ fn test_shutdown_sends_broadcast() {
+ let (shutdown_tx, mut rx) = broadcast::channel::<()>(1);
+ let handle = EmbeddedServerHandle {
+ shutdown_tx,
+ ui_recorder_handle: None,
+ };
+
+ handle.shutdown();
+ assert!(rx.try_recv().is_ok(), "shutdown must send on broadcast channel");
+ }
+
+ #[test]
+ fn test_shutdown_without_ui_recorder() {
+ let (shutdown_tx, _) = broadcast::channel::<()>(1);
+ let handle = EmbeddedServerHandle {
+ shutdown_tx,
+ ui_recorder_handle: None,
+ };
+
+ // Should not panic when ui_recorder_handle is None
+ handle.shutdown();
+ }
+}
diff --git a/crates/screenpipe-server/src/ui_recorder.rs b/crates/screenpipe-server/src/ui_recorder.rs
index 185a9ae17..f22e7ddbb 100644
--- a/crates/screenpipe-server/src/ui_recorder.rs
+++ b/crates/screenpipe-server/src/ui_recorder.rs
@@ -185,6 +185,16 @@ impl UiRecorderHandle {
let _ = handle.await;
}
}
+
+ /// Create a handle with only a stop flag (for testing shutdown wiring)
+ #[doc(hidden)]
+ pub fn new_for_test(stop_flag: Arc<AtomicBool>) -> Self {
+ Self {
+ stop_flag,
+ task_handle: None,
+ tree_walker_handle: None,
+ }
+ }
}
/// Start UI event recording.
@@ -706,3 +716,103 @@ async fn flush_batch(
}
batch.clear();
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_stop_flag_sets_on_stop() {
+ let handle = UiRecorderHandle {
+ stop_flag: Arc::new(AtomicBool::new(false)),
+ task_handle: None,
+ tree_walker_handle: None,
+ };
+
+ assert!(handle.is_running());
+ handle.stop();
+ assert!(!handle.is_running());
+ }
+
+ #[test]
+ fn test_stop_flag_propagates_to_shared_clone() {
+ // Simulates the real scenario: stop_flag is shared between
+ // UiRecorderHandle and the tree walker thread via Arc.
+ let flag = Arc::new(AtomicBool::new(false));
+ let flag_clone = flag.clone();
+
+ let handle = UiRecorderHandle {
+ stop_flag: flag,
+ task_handle: None,
+ tree_walker_handle: None,
+ };
+
+ // The tree walker checks the cloned flag
+ assert!(!flag_clone.load(Ordering::Relaxed));
+ handle.stop();
+ assert!(flag_clone.load(Ordering::Relaxed));
+ }
+
+ #[tokio::test]
+ async fn test_spawn_channel_pattern_clean_shutdown() {
+ // Verifies the spawn+channel pattern doesn't panic when the
+ // spawned task is cancelled (simulating runtime shutdown).
+ let (result_tx, result_rx) = std::sync::mpsc::sync_channel::<Result<i64, String>>(1);
+
+ // Drop the sender without sending — simulates runtime dropping the task
+ drop(result_tx);
+
+ // recv_timeout should return Err, not panic
+ let result = result_rx.recv_timeout(std::time::Duration::from_millis(100));
+ assert!(result.is_err());
+ }
+
+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+ async fn test_spawn_channel_pattern_success() {
+ let rt_handle = tokio::runtime::Handle::current();
+ let (result_tx, result_rx) = std::sync::mpsc::sync_channel::<Result<i64, String>>(1);
+
+ rt_handle.spawn(async move {
+ let _ = result_tx.send(Ok(42i64));
+ });
+
+ // recv_timeout blocks the thread, so we need multi_thread runtime
+ // to let the spawned task run on another worker
+ let result = result_rx.recv_timeout(std::time::Duration::from_secs(5));
+ assert!(result.is_ok());
+ assert_eq!(result.unwrap().unwrap(), 42);
+ }
+
+ #[tokio::test]
+ async fn test_spawn_channel_runtime_shutdown_no_panic() {
+ // Creates a separate runtime, spawns a slow task, then drops the
+ // runtime. The recv should return Err cleanly — no panic.
+ let (result_tx, result_rx) = std::sync::mpsc::sync_channel::<Result<i64, String>>(1);
+
+ let rt = tokio::runtime::Builder::new_current_thread()
+ .enable_time()
+ .build()
+ .unwrap();
+
+ let handle = rt.handle().clone();
+ handle.spawn(async move {
+ // Simulate a slow DB write that won't complete before shutdown
+ tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
+ let _ = result_tx.send(Ok(1));
+ });
+
+ // Shutdown the runtime — the task is cancelled, sender is dropped
+ rt.shutdown_background();
+
+ let result = result_rx.recv_timeout(std::time::Duration::from_millis(500));
+ assert!(result.is_err(), "should get Err when runtime shuts down");
+ }
+
+ #[test]
+ fn test_handle_is_send() {
+ // EmbeddedServerHandle stores UiRecorderHandle and crosses async
+ // boundaries — it must be Send.
+ fn assert_send<T: Send>() {}
+ assert_send::<UiRecorderHandle>();
+ }
+}