regy 0.1.0

Private-by-default desktop agent for the Regy web interface
use serde_json::json;

use crate::domain::protocol::AgentMessage;

pub fn build_host_hello(
    host_id: impl Into<String>,
    hostname: impl Into<String>,
    username: impl Into<String>,
    home_dir: Option<String>,
) -> AgentMessage {
    AgentMessage::HostHello {
        host_id: host_id.into(),
        agent_version: env!("CARGO_PKG_VERSION").to_string(),
        hostname: hostname.into(),
        username: username.into(),
        os: std::env::consts::OS.to_string(),
        capabilities: json!({
            "pty": true,
            "resize": true,
            "shell": true,
            "fileWatch": false,
            "directoryList": true,
            "modelsList": true,
            "homeDir": home_dir,
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn host_hello_contains_capabilities() {
        let msg = build_host_hello("host-1", "machine", "user", Some("/home/u".into()));
        let value = serde_json::to_value(msg).unwrap();
        assert_eq!(value["type"], "host.hello");
        assert_eq!(value["capabilities"]["pty"], true);
        assert_eq!(value["capabilities"]["resize"], true);
        assert_eq!(value["capabilities"]["homeDir"], "/home/u");
    }

    #[test]
    fn host_hello_accepts_missing_home_dir() {
        let msg = build_host_hello("host-1", "machine", "user", None);
        let value = serde_json::to_value(msg).unwrap();
        assert!(value["capabilities"]["homeDir"].is_null());
    }
}