Skip to main content

alien_core/
sync.rs

1//! Sync protocol types for agent ↔ manager communication.
2//!
3//! The agent periodically calls `POST /v1/sync` with a `SyncRequest` and
4//! receives a `SyncResponse` containing the target deployment state.
5
6use serde::{Deserialize, Serialize};
7
8use crate::{DeploymentConfig, DeploymentState, ReleaseInfo};
9
10/// Request sent by the agent to the manager during periodic sync.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct SyncRequest {
14    /// The deployment ID this agent is managing.
15    pub deployment_id: String,
16    /// Current deployment state as seen by the agent.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub current_state: Option<DeploymentState>,
19}
20
21/// Response from the manager to the agent sync request.
22#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct SyncResponse {
25    /// Target deployment the agent should converge toward.
26    /// None means no changes needed.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub target: Option<TargetDeployment>,
29}
30
31/// Target deployment state for the agent to converge toward.
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase")]
34pub struct TargetDeployment {
35    /// Release information (ID, version, stack definition).
36    pub release_info: ReleaseInfo,
37    /// Full deployment configuration (settings, env vars, etc.).
38    pub config: DeploymentConfig,
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_sync_request_serialization() {
47        let req = SyncRequest {
48            deployment_id: "dep_abc123".to_string(),
49            current_state: None,
50        };
51
52        let json = serde_json::to_value(&req).unwrap();
53        assert_eq!(json["deploymentId"], "dep_abc123");
54        // current_state is None → should be omitted
55        assert!(json.get("currentState").is_none());
56    }
57
58    #[test]
59    fn test_sync_request_deserialization() {
60        let json = r#"{"deploymentId": "dep_xyz"}"#;
61        let req: SyncRequest = serde_json::from_str(json).unwrap();
62        assert_eq!(req.deployment_id, "dep_xyz");
63        assert!(req.current_state.is_none());
64    }
65
66    #[test]
67    fn test_sync_response_empty() {
68        let resp = SyncResponse { target: None };
69        let json = serde_json::to_value(&resp).unwrap();
70        // target is None → should be omitted
71        assert!(json.get("target").is_none());
72    }
73
74    #[test]
75    fn test_sync_response_roundtrip_no_target() {
76        let resp = SyncResponse { target: None };
77        let serialized = serde_json::to_string(&resp).unwrap();
78        let deserialized: SyncResponse = serde_json::from_str(&serialized).unwrap();
79        assert!(deserialized.target.is_none());
80    }
81
82    #[test]
83    fn test_sync_request_with_camel_case() {
84        // Verify camelCase renaming works correctly
85        let json = r#"{"deploymentId": "dep_1", "currentState": null}"#;
86        let req: SyncRequest = serde_json::from_str(json).unwrap();
87        assert_eq!(req.deployment_id, "dep_1");
88        assert!(req.current_state.is_none());
89
90        // snake_case should NOT work
91        let json = r#"{"deployment_id": "dep_1"}"#;
92        assert!(serde_json::from_str::<SyncRequest>(json).is_err());
93    }
94}