pywatt_sdk 0.5.3

Standardized SDK for building PyWatt modules in Rust
Documentation
#[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 helper for checking that the router builder works as expected
    #[test]
    fn test_router_builder() {
        // Create a simple router builder
        let router_builder = |_state: AppState<String>| {
            Router::<()>::new().route("/hello", get(|| async { "Hello, World!" }))
        };

        // Simple state builder
        let _state_builder =
            |_init: &OrchestratorInit, _secrets: Vec<SecretString>| "test_state".to_string();

        // Create a dummy state with a properly constructed SecretClient
        let app_state = AppState::new(
            "test_module".to_string(),
            "http://localhost:8000".to_string(),
            Arc::new(SecretClient::new_dummy()),
            "test_state".to_string(),
        );

        // Build the router
        let _router = router_builder(app_state);

        // In a unit test, we can't do much more than verify that the router
        // builder doesn't panic when called
    }

    // Test helper to verify that endpoints are correctly structured
    #[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 the OrchestratorInit listen_to_string method
    #[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());

        // The implementation in ext.rs returns just the address string, not with the protocol
        assert_eq!(init.listen_to_string(), "127.0.0.1:8080");

        // The implementation returns just the path, not with the protocol
        assert_eq!(init_unix.listen_to_string(), "/tmp/test.sock");
    }
}