#[cfg(test)]
mod tests {
use pywatt_sdk::{
AnnouncedEndpoint, OrchestratorInit, ext::OrchestratorInitExt, communication::ipc_types::ListenAddress,
secret_client::SecretClient, state::AppState,
};
use axum::{Router, routing::get};
use secrecy::SecretString;
use std::sync::Arc;
use std::{collections::HashMap, path::PathBuf};
#[test]
fn test_router_builder() {
let router_builder = |_state: AppState<String>| {
Router::<()>::new().route("/hello", get(|| async { "Hello, World!" }))
};
let _state_builder =
|_init: &OrchestratorInit, _secrets: Vec<SecretString>| "test_state".to_string();
let app_state = AppState::new(
"test_module".to_string(),
"http://localhost:8000".to_string(),
Arc::new(SecretClient::new_dummy()),
"test_state".to_string(),
);
let _router = router_builder(app_state);
}
#[test]
fn test_announced_endpoints() {
let endpoints = [
AnnouncedEndpoint {
path: "/api/test".to_string(),
methods: vec!["GET".to_string(), "POST".to_string()],
auth: None,
},
AnnouncedEndpoint {
path: "/api/protected".to_string(),
methods: vec!["GET".to_string()],
auth: Some("jwt".to_string()),
},
];
assert_eq!(endpoints[0].path, "/api/test");
assert_eq!(endpoints[0].methods, vec!["GET", "POST"]);
assert_eq!(endpoints[0].auth, None);
assert_eq!(endpoints[1].path, "/api/protected");
assert_eq!(endpoints[1].methods, vec!["GET"]);
assert_eq!(endpoints[1].auth, Some("jwt".to_string()));
}
#[test]
fn test_listen_to_string() {
let init = OrchestratorInit::new(
"http://localhost:8000".to_string(),
"test_module".to_string(),
ListenAddress::Tcp("127.0.0.1:8080".parse().unwrap()),
)
.with_env(HashMap::new());
let init_unix = OrchestratorInit::new(
"http://localhost:8000".to_string(),
"test_module".to_string(),
ListenAddress::Unix(PathBuf::from("/tmp/test.sock")),
)
.with_env(HashMap::new());
assert_eq!(init.listen_to_string(), "127.0.0.1:8080");
assert_eq!(init_unix.listen_to_string(), "/tmp/test.sock");
}
}