From b2ba6904c856990bf99cceacf52576a07989f2b7 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Wed, 11 Mar 2026 08:40:20 +0100 Subject: [PATCH] test(appd): add dispatch integration tests 5 async tests covering the dispatch function end-to-end: - dispatch_launch_returns_ack: LaunchApp returns LaunchAck with a positive session ID. - dispatch_terminate_known_returns_stopped: launch then terminate returns AppState::Stopped. - dispatch_terminate_unknown_returns_error: unknown session ID returns Error response. - dispatch_query_running_lists_active_sessions: after two launches, QueryRunning returns two session IDs. - dispatch_query_app_state_returns_starting: newly launched session reports AppStateKind::Starting. --- crates/weft-appd/src/main.rs | 104 +++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 8650807..19bf54f 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -161,6 +161,110 @@ mod tests { use super::*; use ipc::AppStateKind; + fn make_registry() -> Registry { + Arc::new(Mutex::new(SessionRegistry::default())) + } + + #[tokio::test] + async fn dispatch_launch_returns_ack() { + let reg = make_registry(); + let resp = dispatch( + Request::LaunchApp { + app_id: "com.test.app".into(), + surface_id: 0, + }, + ®, + ) + .await; + match resp { + Response::LaunchAck { session_id } => assert!(session_id > 0), + _ => panic!("expected LaunchAck"), + } + } + + #[tokio::test] + async fn dispatch_terminate_known_returns_stopped() { + let reg = make_registry(); + let ack = dispatch( + Request::LaunchApp { + app_id: "app".into(), + surface_id: 0, + }, + ®, + ) + .await; + let session_id = match ack { + Response::LaunchAck { session_id } => session_id, + _ => panic!("expected LaunchAck"), + }; + let resp = dispatch(Request::TerminateApp { session_id }, ®).await; + assert!(matches!( + resp, + Response::AppState { + state: AppStateKind::Stopped, + .. + } + )); + } + + #[tokio::test] + async fn dispatch_terminate_unknown_returns_error() { + let reg = make_registry(); + let resp = dispatch(Request::TerminateApp { session_id: 999 }, ®).await; + assert!(matches!(resp, Response::Error { .. })); + } + + #[tokio::test] + async fn dispatch_query_running_lists_active_sessions() { + let reg = make_registry(); + dispatch( + Request::LaunchApp { + app_id: "a".into(), + surface_id: 0, + }, + ®, + ) + .await; + dispatch( + Request::LaunchApp { + app_id: "b".into(), + surface_id: 0, + }, + ®, + ) + .await; + let resp = dispatch(Request::QueryRunning, ®).await; + match resp { + Response::RunningApps { session_ids } => assert_eq!(session_ids.len(), 2), + _ => panic!("expected RunningApps"), + } + } + + #[tokio::test] + async fn dispatch_query_app_state_returns_starting() { + let reg = make_registry(); + let ack = dispatch( + Request::LaunchApp { + app_id: "app".into(), + surface_id: 0, + }, + ®, + ) + .await; + let session_id = match ack { + Response::LaunchAck { session_id } => session_id, + _ => panic!(), + }; + let resp = dispatch(Request::QueryAppState { session_id }, ®).await; + assert!(matches!( + resp, + Response::AppState { + state: AppStateKind::Starting, + .. + } + )); + } + #[test] fn registry_launch_increments_id() { let mut reg = SessionRegistry::default();