use serde::Deserialize;
use super::common::{ResourceRef, UserRef};
#[derive(Debug, Clone, Deserialize)]
pub struct Event {
pub user: Option<UserRef>,
pub resource: ResourceRef,
#[serde(rename = "type")]
pub resource_type: Option<String>,
pub action: String,
pub parent: Option<ResourceRef>,
pub created_at: Option<String>,
pub change: Option<EventChange>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EventChange {
pub field: Option<String>,
pub action: Option<String>,
pub new_value: Option<serde_json::Value>,
pub added_value: Option<serde_json::Value>,
pub removed_value: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EventsResponse {
pub data: Vec<Event>,
pub sync: String,
pub has_more: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EventsSyncReset {
pub sync: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_deserialize_event() {
let json = r#"{
"user": {"gid": "123", "name": "Test User"},
"resource": {"gid": "456", "resource_type": "task", "name": "My Task"},
"type": "task",
"action": "changed",
"parent": {"gid": "789", "resource_type": "project", "name": "My Project"},
"created_at": "2024-01-01T00:00:00.000Z",
"change": {
"field": "completed",
"action": "changed",
"new_value": true
}
}"#;
let event: Event = serde_json::from_str(json).unwrap();
assert_eq!(event.action, "changed");
assert_eq!(event.resource.gid, "456");
assert_eq!(event.resource_type, Some("task".to_string()));
assert_eq!(event.user.unwrap().gid, "123");
assert_eq!(event.parent.unwrap().gid, "789");
let change = event.change.unwrap();
assert_eq!(change.field, Some("completed".to_string()));
assert_eq!(change.action, Some("changed".to_string()));
}
#[test]
fn test_deserialize_events_response() {
let json = r#"{
"data": [
{
"resource": {"gid": "1", "resource_type": "task"},
"action": "changed"
}
],
"sync": "token123",
"has_more": false
}"#;
let resp: EventsResponse = serde_json::from_str(json).unwrap();
assert_eq!(resp.data.len(), 1);
assert_eq!(resp.sync, "token123");
assert!(!resp.has_more);
}
#[test]
fn test_deserialize_events_sync_reset() {
let json = r#"{
"sync": "fresh_token",
"errors": [{"message": "Sync token invalid or too old"}]
}"#;
let reset: EventsSyncReset = serde_json::from_str(json).unwrap();
assert_eq!(reset.sync, "fresh_token");
}
#[test]
fn test_deserialize_event_minimal() {
let json = r#"{
"resource": {"gid": "1"},
"action": "added"
}"#;
let event: Event = serde_json::from_str(json).unwrap();
assert_eq!(event.action, "added");
assert!(event.user.is_none());
assert!(event.parent.is_none());
assert!(event.change.is_none());
assert!(event.resource_type.is_none());
}
}