use serde::{Deserialize, Serialize};
use crate::{DeploymentConfig, DeploymentState, ReleaseInfo};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncRequest {
pub deployment_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub current_state: Option<DeploymentState>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncResponse {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub current_state: Option<DeploymentState>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target: Option<TargetDeployment>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub commands_url: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TargetDeployment {
pub release_info: ReleaseInfo,
pub config: DeploymentConfig,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sync_request_serialization() {
let req = SyncRequest {
deployment_id: "dep_abc123".to_string(),
current_state: None,
};
let json = serde_json::to_value(&req).unwrap();
assert_eq!(json["deploymentId"], "dep_abc123");
assert!(json.get("currentState").is_none());
}
#[test]
fn test_sync_request_deserialization() {
let json = r#"{"deploymentId": "dep_xyz"}"#;
let req: SyncRequest = serde_json::from_str(json).unwrap();
assert_eq!(req.deployment_id, "dep_xyz");
assert!(req.current_state.is_none());
}
#[test]
fn test_sync_response_empty() {
let resp = SyncResponse {
current_state: None,
target: None,
commands_url: None,
};
let json = serde_json::to_value(&resp).unwrap();
assert!(json.get("target").is_none());
assert!(json.get("currentState").is_none());
}
#[test]
fn test_sync_response_roundtrip_no_target() {
let resp = SyncResponse {
current_state: None,
target: None,
commands_url: None,
};
let serialized = serde_json::to_string(&resp).unwrap();
let deserialized: SyncResponse = serde_json::from_str(&serialized).unwrap();
assert!(deserialized.target.is_none());
assert!(deserialized.current_state.is_none());
}
#[test]
fn test_sync_request_with_camel_case() {
let json = r#"{"deploymentId": "dep_1", "currentState": null}"#;
let req: SyncRequest = serde_json::from_str(json).unwrap();
assert_eq!(req.deployment_id, "dep_1");
assert!(req.current_state.is_none());
let json = r#"{"deployment_id": "dep_1"}"#;
assert!(serde_json::from_str::<SyncRequest>(json).is_err());
}
}