From ee495478ff52bb3bec1c9f2af573e6acf00f1117 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Fri, 27 Dec 2024 15:50:08 -0500 Subject: [PATCH] start working on test for server address --- src-tauri/Cargo.toml | 2 +- src-tauri/src/srv/mod.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 1537c58..1836bc3 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -30,7 +30,7 @@ tauri-build = { version = "2.0.0-beta", features = [] } [dependencies] creddy_cli = { path = "./creddy_cli" } -tauri = { version = "2.0.0-beta", features = ["tray-icon"] } +tauri = { version = "2.0.0-beta", features = ["tray-icon", "test"] } sodiumoxide = "0.2.7" sysinfo = "0.26.8" aws-config = "1.5.3" diff --git a/src-tauri/src/srv/mod.rs b/src-tauri/src/srv/mod.rs index 096d2c2..d04f104 100644 --- a/src-tauri/src/srv/mod.rs +++ b/src-tauri/src/srv/mod.rs @@ -223,3 +223,33 @@ mod platform { Ok((stream, pid)) } } + + +#[cfg(test)] +mod tests { + use super::*; + use tokio::io::AsyncWriteExt; + + #[tokio::test] + async fn test_server_connect() { + let app = tauri::test::mock_app(); + serve("creddy_server_test", app.app_handle().clone(), |stream, _handle, _pid| { + async move { + let buf = serde_json::to_vec(&CliResponse::Empty).unwrap(); + stream.write_all(&buf).await.unwrap(); + Ok(()) + } + }); + + let addr = creddy_cli::server_addr("creddy_server_test"); + let mut stream = creddy_cli::connect(Some(addr)).await.unwrap(); + let req = CliRequest::InvokeShortcut{ action: ShortcutAction::ShowWindow }; + let req_bytes = serde_json::to_vec(&req).unwrap(); + stream.write_all(&req_bytes).await.unwrap(); + + let mut buf = Vec::new(); + stream.read_to_end(&mut buf).await.unwrap(); + let resp: CliResponse = serde_json::from_slice(&buf).unwrap(); + assert!(matches!(resp, CliResponse::Empty)) + } +}