Skip to main content

made_core/events/
task_completed.rs

1//! [`TaskCompletedEvent`] — an agent finished its work on a task.
2
3use serde::{Deserialize, Serialize};
4
5use crate::events::envelope::EventEnvelope;
6use crate::value_objects::{AgentId, DurationMs, Specialty, TaskId};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct TaskCompletedEvent {
10    #[serde(flatten)]
11    envelope: EventEnvelope,
12    task_id: TaskId,
13    specialty: Specialty,
14    agent_id: Option<AgentId>,
15    duration: DurationMs,
16}
17
18impl TaskCompletedEvent {
19    #[must_use]
20    pub fn new(
21        envelope: EventEnvelope,
22        task_id: TaskId,
23        specialty: Specialty,
24        agent_id: Option<AgentId>,
25        duration: DurationMs,
26    ) -> Self {
27        Self {
28            envelope,
29            task_id,
30            specialty,
31            agent_id,
32            duration,
33        }
34    }
35
36    #[must_use]
37    pub fn envelope(&self) -> &EventEnvelope {
38        &self.envelope
39    }
40    #[must_use]
41    pub fn task_id(&self) -> &TaskId {
42        &self.task_id
43    }
44    #[must_use]
45    pub fn specialty(&self) -> &Specialty {
46        &self.specialty
47    }
48    #[must_use]
49    pub fn agent_id(&self) -> Option<&AgentId> {
50        self.agent_id.as_ref()
51    }
52    #[must_use]
53    pub fn duration(&self) -> DurationMs {
54        self.duration
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::value_objects::EventId;
62    use time::macros::datetime;
63
64    #[test]
65    fn accessors_return_fields() {
66        let env = EventEnvelope::new(
67            EventId::new("e").unwrap(),
68            datetime!(2026-04-15 12:00:00 UTC),
69            "s",
70            None,
71        )
72        .unwrap();
73        let ev = TaskCompletedEvent::new(
74            env,
75            TaskId::new("t").unwrap(),
76            Specialty::new("triage").unwrap(),
77            Some(AgentId::new("a").unwrap()),
78            DurationMs::from_millis(250),
79        );
80        assert_eq!(ev.duration().get(), 250);
81        assert_eq!(ev.agent_id().unwrap().as_str(), "a");
82    }
83}