Skip to main content

phytrace_sdk/core/
builder.rs

1//! Event builder for constructing UDM events fluently.
2//!
3//! The EventBuilder provides a fluent interface for constructing events
4//! with automatic configuration injection and validation.
5
6use crate::core::config::PhyTraceConfig;
7use crate::error::{BuilderError, PhyTraceResult};
8use crate::models::domains::location::LocalCoordinates;
9use crate::models::domains::*;
10use crate::models::enums::{EventType, SourceType};
11use crate::models::event::UdmEvent;
12
13/// Fluent builder for UDM events.
14///
15/// # Example
16///
17/// ```rust
18/// use phytrace_sdk::core::{EventBuilder, PhyTraceConfig};
19/// use phytrace_sdk::{EventType, SourceType};
20///
21/// let config = PhyTraceConfig::new("robot-001")
22///     .with_source_type(SourceType::Amr);
23///
24/// let event = EventBuilder::new(&config)
25///     .event_type(EventType::TelemetryPeriodic)
26///     .position_local(10.0, 20.0, 0.0)
27///     .heading_deg(90.0)
28///     .battery_soc(85.0)
29///     .build()
30///     .unwrap();
31/// ```
32pub struct EventBuilder {
33    event: UdmEvent,
34    config: PhyTraceConfig,
35}
36
37impl Clone for EventBuilder {
38    fn clone(&self) -> Self {
39        Self {
40            event: self.event.clone(),
41            config: self.config.clone(),
42        }
43    }
44}
45
46impl EventBuilder {
47    /// Create a new event builder with configuration.
48    pub fn new(config: &PhyTraceConfig) -> Self {
49        let mut event = UdmEvent::new(config.source.source_type);
50
51        // Set top-level source_id for PhyCloud compatibility
52        event.source_id = config.source.source_id.clone();
53
54        // Auto-populate identity from config
55        event.identity = Some(IdentityDomain {
56            source_id: Some(config.source.source_id.clone()),
57            fleet_id: config.source.fleet_id.clone(),
58            site_id: config.source.site_id.clone(),
59            organization_id: config.source.organization_id.clone(),
60            tags: if config.source.tags.is_empty() {
61                None
62            } else {
63                Some(config.source.tags.clone())
64            },
65            ..Default::default()
66        });
67
68        Self {
69            event,
70            config: config.clone(),
71        }
72    }
73
74    /// Set the event type.
75    pub fn event_type(mut self, event_type: EventType) -> Self {
76        self.event.event_type = event_type;
77        self
78    }
79
80    /// Override the source type.
81    pub fn source_type(mut self, source_type: SourceType) -> Self {
82        self.event.source_type = source_type;
83        self
84    }
85
86    // =========================================================================
87    // Identity Helpers
88    // =========================================================================
89
90    /// Set the source ID (robot/system identifier).
91    /// This sets both the top-level source_id (for PhyCloud) and the identity domain.
92    pub fn source_id(mut self, source_id: impl Into<String>) -> Self {
93        let sid = source_id.into();
94        // Set top-level source_id for PhyCloud compatibility
95        self.event.source_id = sid.clone();
96        // Also set in identity domain for UDM completeness
97        let identity = self.event.identity.get_or_insert_with(Default::default);
98        identity.source_id = Some(sid);
99        self
100    }
101
102    /// Set model/firmware information.
103    pub fn model_info(
104        mut self,
105        _manufacturer: impl Into<String>,
106        model: impl Into<String>,
107        firmware: impl Into<String>,
108    ) -> Self {
109        let identity = self.event.identity.get_or_insert_with(Default::default);
110        identity.model = Some(model.into());
111        identity.firmware_version = Some(firmware.into());
112        self
113    }
114
115    /// Add a tag.
116    pub fn tag(mut self, tag: impl Into<String>) -> Self {
117        let identity = self.event.identity.get_or_insert_with(Default::default);
118        let tags = identity.tags.get_or_insert_with(Vec::new);
119        tags.push(tag.into());
120        self
121    }
122
123    // =========================================================================
124    // Location Helpers
125    // =========================================================================
126
127    /// Set local position (x, y, z in meters).
128    pub fn position_local(mut self, x: f64, y: f64, z: f64) -> Self {
129        let location = self.event.location.get_or_insert_with(Default::default);
130        location.local = Some(LocalCoordinates {
131            x_m: Some(x),
132            y_m: Some(y),
133            z_m: Some(z),
134            ..Default::default()
135        });
136        self
137    }
138
139    /// Set 2D position (convenience for x, y only).
140    pub fn position_2d(mut self, x: f64, y: f64) -> Self {
141        let location = self.event.location.get_or_insert_with(Default::default);
142        location.local = Some(LocalCoordinates {
143            x_m: Some(x),
144            y_m: Some(y),
145            z_m: Some(0.0),
146            ..Default::default()
147        });
148        self
149    }
150
151    /// Set 3D position (convenience alias).
152    pub fn position_3d(self, x: f64, y: f64, z: f64) -> Self {
153        self.position_local(x, y, z)
154    }
155
156    /// Set heading in degrees (0-360).
157    pub fn heading_deg(mut self, heading: f64) -> Self {
158        let location = self.event.location.get_or_insert_with(Default::default);
159        location.heading_deg = Some(heading);
160        self
161    }
162
163    /// Set heading in radians (converted to degrees).
164    pub fn heading(self, heading_rad: f64) -> Self {
165        let heading_deg = heading_rad.to_degrees();
166        self.heading_deg(heading_deg)
167    }
168
169    /// Set GPS coordinates.
170    pub fn gps(mut self, latitude: f64, longitude: f64) -> Self {
171        let location = self.event.location.get_or_insert_with(Default::default);
172        location.latitude = Some(latitude);
173        location.longitude = Some(longitude);
174        self
175    }
176
177    /// Set floor number.
178    pub fn floor(mut self, floor: i32) -> Self {
179        let location = self.event.location.get_or_insert_with(Default::default);
180        location.floor = Some(floor);
181        self
182    }
183
184    /// Set map ID and frame.
185    pub fn map(mut self, map_id: impl Into<String>, frame_id: impl Into<String>) -> Self {
186        let location = self.event.location.get_or_insert_with(Default::default);
187        location.map_id = Some(map_id.into());
188        location.frame_id = Some(frame_id.into());
189        self
190    }
191
192    // =========================================================================
193    // Motion Helpers
194    // =========================================================================
195
196    /// Set linear velocity (m/s).
197    pub fn linear_velocity(mut self, vx: f64, vy: f64, vz: f64) -> Self {
198        let motion = self.event.motion.get_or_insert_with(Default::default);
199        motion.linear_velocity = Some(motion::LinearVelocity {
200            x_mps: Some(vx),
201            y_mps: Some(vy),
202            z_mps: Some(vz),
203        });
204        self
205    }
206
207    /// Set angular velocity (rad/s) - converts to degrees/s internally.
208    pub fn angular_velocity(mut self, roll_rps: f64, pitch_rps: f64, yaw_rps: f64) -> Self {
209        let motion = self.event.motion.get_or_insert_with(Default::default);
210        motion.angular_velocity = Some(motion::AngularVelocity {
211            roll_dps: Some(roll_rps.to_degrees()),
212            pitch_dps: Some(pitch_rps.to_degrees()),
213            yaw_dps: Some(yaw_rps.to_degrees()),
214        });
215        self
216    }
217
218    /// Set speed (m/s) - for 2D motion.
219    pub fn speed(mut self, speed_mps: f64) -> Self {
220        let motion = self.event.motion.get_or_insert_with(Default::default);
221        motion.speed_mps = Some(speed_mps);
222        self
223    }
224
225    // =========================================================================
226    // Power Helpers
227    // =========================================================================
228
229    /// Set battery state of charge (0-100%).
230    pub fn battery_soc(mut self, soc_percent: f64) -> Self {
231        let power = self.event.power.get_or_insert_with(Default::default);
232        let battery = power.battery.get_or_insert_with(Default::default);
233        battery.state_of_charge_pct = Some(soc_percent);
234        self
235    }
236
237    /// Set battery voltage.
238    pub fn battery_voltage(mut self, voltage_v: f64) -> Self {
239        let power = self.event.power.get_or_insert_with(Default::default);
240        let battery = power.battery.get_or_insert_with(Default::default);
241        battery.voltage_v = Some(voltage_v);
242        self
243    }
244
245    /// Set battery health (0-100%).
246    pub fn battery_health(mut self, health_percent: f64) -> Self {
247        let power = self.event.power.get_or_insert_with(Default::default);
248        let battery = power.battery.get_or_insert_with(Default::default);
249        battery.state_of_health_pct = Some(health_percent);
250        self
251    }
252
253    /// Set charging status.
254    pub fn charging(mut self, is_charging: bool) -> Self {
255        use crate::models::enums::ChargingState;
256        let power = self.event.power.get_or_insert_with(Default::default);
257        let charging = power.charging.get_or_insert_with(Default::default);
258        charging.state = Some(if is_charging {
259            ChargingState::Charging
260        } else {
261            ChargingState::NotCharging
262        });
263        self
264    }
265
266    // =========================================================================
267    // Operational Helpers
268    // =========================================================================
269
270    /// Set operational mode.
271    pub fn operational_mode(mut self, mode: crate::models::enums::OperationalMode) -> Self {
272        let operational = self.event.operational.get_or_insert_with(Default::default);
273        operational.mode = Some(mode);
274        self
275    }
276
277    /// Set operational state.
278    pub fn operational_state(mut self, state: crate::models::enums::OperationalState) -> Self {
279        let operational = self.event.operational.get_or_insert_with(Default::default);
280        operational.state = Some(state);
281        self
282    }
283
284    /// Set current task.
285    pub fn task(mut self, task_id: impl Into<String>, task_type: impl Into<String>) -> Self {
286        let operational = self.event.operational.get_or_insert_with(Default::default);
287        operational.task = Some(operational::Task {
288            task_id: Some(task_id.into()),
289            task_type: Some(task_type.into()),
290            ..Default::default()
291        });
292        self
293    }
294
295    // =========================================================================
296    // Safety Helpers
297    // =========================================================================
298
299    /// Set safety state.
300    pub fn safety_state(mut self, state: crate::models::enums::SafetyState) -> Self {
301        let safety = self.event.safety.get_or_insert_with(Default::default);
302        safety.safety_state = Some(state);
303        self
304    }
305
306    /// Set e-stop status.
307    pub fn estop_active(mut self, active: bool) -> Self {
308        let safety = self.event.safety.get_or_insert_with(Default::default);
309        let estop = safety.e_stop.get_or_insert_with(Default::default);
310        estop.is_active = Some(active);
311        self
312    }
313
314    // =========================================================================
315    // Navigation Helpers
316    // =========================================================================
317
318    /// Set localization quality.
319    pub fn localization_quality(
320        mut self,
321        quality: crate::models::enums::LocalizationQuality,
322    ) -> Self {
323        let nav = self.event.navigation.get_or_insert_with(Default::default);
324        let loc = nav.localization.get_or_insert_with(Default::default);
325        loc.quality = Some(quality);
326        self
327    }
328
329    /// Set path state.
330    pub fn path_state(mut self, state: crate::models::enums::PathState) -> Self {
331        let nav = self.event.navigation.get_or_insert_with(Default::default);
332        let path = nav.path.get_or_insert_with(Default::default);
333        path.state = Some(state);
334        self
335    }
336
337    // =========================================================================
338    // Domain Setters
339    // =========================================================================
340
341    /// Set the full identity domain.
342    pub fn identity(mut self, identity: IdentityDomain) -> Self {
343        self.event.identity = Some(identity);
344        self
345    }
346
347    /// Set the full location domain.
348    pub fn location(mut self, location: LocationDomain) -> Self {
349        self.event.location = Some(location);
350        self
351    }
352
353    /// Set the full motion domain.
354    pub fn motion(mut self, motion: MotionDomain) -> Self {
355        self.event.motion = Some(motion);
356        self
357    }
358
359    /// Set the full power domain.
360    pub fn power(mut self, power: PowerDomain) -> Self {
361        self.event.power = Some(power);
362        self
363    }
364
365    /// Set the full operational domain.
366    pub fn operational(mut self, operational: OperationalDomain) -> Self {
367        self.event.operational = Some(operational);
368        self
369    }
370
371    /// Set the full navigation domain.
372    pub fn navigation(mut self, navigation: NavigationDomain) -> Self {
373        self.event.navigation = Some(navigation);
374        self
375    }
376
377    /// Set the full perception domain.
378    pub fn perception(mut self, perception: PerceptionDomain) -> Self {
379        self.event.perception = Some(perception);
380        self
381    }
382
383    /// Set the full safety domain.
384    pub fn safety(mut self, safety: SafetyDomain) -> Self {
385        self.event.safety = Some(safety);
386        self
387    }
388
389    /// Set the full actuators domain.
390    pub fn actuators(mut self, actuators: ActuatorsDomain) -> Self {
391        self.event.actuators = Some(actuators);
392        self
393    }
394
395    /// Set the full communication domain.
396    pub fn communication(mut self, communication: CommunicationDomain) -> Self {
397        self.event.communication = Some(communication);
398        self
399    }
400
401    /// Set the full compute domain.
402    pub fn compute(mut self, compute: ComputeDomain) -> Self {
403        self.event.compute = Some(compute);
404        self
405    }
406
407    /// Set the full AI domain.
408    pub fn ai(mut self, ai: AiDomain) -> Self {
409        self.event.ai = Some(ai);
410        self
411    }
412
413    /// Set the full maintenance domain.
414    pub fn maintenance(mut self, maintenance: MaintenanceDomain) -> Self {
415        self.event.maintenance = Some(maintenance);
416        self
417    }
418
419    /// Set the full context domain.
420    pub fn context(mut self, context: ContextDomain) -> Self {
421        self.event.context = Some(context);
422        self
423    }
424
425    /// Set the full payload domain.
426    pub fn payload(mut self, payload: PayloadDomain) -> Self {
427        self.event.payload = Some(payload);
428        self
429    }
430
431    /// Set the full manipulation domain.
432    pub fn manipulation(mut self, manipulation: ManipulationDomain) -> Self {
433        self.event.manipulation = Some(manipulation);
434        self
435    }
436
437    /// Set the full HRI domain.
438    pub fn hri(mut self, hri: HriDomain) -> Self {
439        self.event.hri = Some(hri);
440        self
441    }
442
443    /// Set the full coordination domain.
444    pub fn coordination(mut self, coordination: CoordinationDomain) -> Self {
445        self.event.coordination = Some(coordination);
446        self
447    }
448
449    /// Set the full simulation domain.
450    pub fn simulation(mut self, simulation: SimulationDomain) -> Self {
451        self.event.simulation = Some(simulation);
452        self
453    }
454
455    /// Set the full thermal domain.
456    pub fn thermal(mut self, thermal: ThermalDomain) -> Self {
457        self.event.thermal = Some(thermal);
458        self
459    }
460
461    /// Set the full audio domain.
462    pub fn audio(mut self, audio: AudioDomain) -> Self {
463        self.event.audio = Some(audio);
464        self
465    }
466
467    /// Set the full environment interaction domain.
468    pub fn environment_interaction(
469        mut self,
470        environment_interaction: EnvironmentInteractionDomain,
471    ) -> Self {
472        self.event.environment_interaction = Some(environment_interaction);
473        self
474    }
475
476    /// Set the full compliance domain.
477    pub fn compliance(mut self, compliance: ComplianceDomain) -> Self {
478        self.event.compliance = Some(compliance);
479        self
480    }
481
482    /// Set custom extensions.
483    pub fn extensions(mut self, extensions: serde_json::Value) -> Self {
484        self.event.extensions = Some(extensions);
485        self
486    }
487
488    /// Set the heartbeat block of a `system_heartbeat` event.
489    pub fn heartbeat(mut self, heartbeat: serde_json::Value) -> Self {
490        self.event.heartbeat = Some(heartbeat);
491        self
492    }
493
494    // =========================================================================
495    // Build
496    // =========================================================================
497
498    /// Build the event, validating and optionally signing.
499    pub fn build(self) -> PhyTraceResult<UdmEvent> {
500        // Validate required fields
501        if !self.event.has_domain_data() {
502            return Err(BuilderError::MissingField("at least one domain".to_string()).into());
503        }
504
505        Ok(self.event)
506    }
507
508    /// Build without validation.
509    pub fn build_unchecked(self) -> UdmEvent {
510        self.event
511    }
512
513    /// Get a reference to the event being built.
514    pub fn peek(&self) -> &UdmEvent {
515        &self.event
516    }
517}
518
519#[cfg(test)]
520mod tests {
521    use super::*;
522    use crate::models::domains::{
523        ActuatorsDomain, AiDomain, AudioDomain, CommunicationDomain, ComplianceDomain,
524        ComputeDomain, ContextDomain, CoordinationDomain, EnvironmentInteractionDomain, HriDomain,
525        MaintenanceDomain, ManipulationDomain, MotionDomain, NavigationDomain, PayloadDomain,
526        PerceptionDomain, SafetyDomain, SimulationDomain, ThermalDomain,
527    };
528    use crate::models::enums::{
529        ChargingState, LocalizationQuality, OperationalMode, OperationalState, PathState,
530        SafetyState,
531    };
532
533    fn test_config() -> PhyTraceConfig {
534        PhyTraceConfig::new("robot-001").with_source_type(SourceType::Amr)
535    }
536
537    #[test]
538    fn test_builder_basic() {
539        let event = EventBuilder::new(&test_config())
540            .position_2d(10.0, 20.0)
541            .build()
542            .unwrap();
543
544        assert_eq!(
545            event.identity.as_ref().unwrap().source_id,
546            Some("robot-001".to_string())
547        );
548        assert!(event.location.is_some());
549    }
550
551    #[test]
552    fn test_builder_all_helpers() {
553        let event = EventBuilder::new(&test_config())
554            .event_type(EventType::TelemetryPeriodic)
555            .position_2d(10.0, 20.0)
556            .heading_deg(90.0)
557            .speed(2.5)
558            .battery_soc(85.0)
559            .operational_mode(OperationalMode::Autonomous)
560            .safety_state(SafetyState::Normal)
561            .build()
562            .unwrap();
563
564        assert!(event.location.is_some());
565        assert!(event.motion.is_some());
566        assert!(event.power.is_some());
567        assert!(event.operational.is_some());
568        assert!(event.safety.is_some());
569    }
570
571    #[test]
572    fn test_builder_with_full_domain() {
573        let location = LocationDomain {
574            heading_deg: Some(180.0),
575            ..Default::default()
576        };
577
578        let event = EventBuilder::new(&test_config())
579            .location(location)
580            .build()
581            .unwrap();
582
583        let loc = event.location.unwrap();
584        assert_eq!(loc.heading_deg, Some(180.0));
585    }
586
587    #[test]
588    fn test_builder_empty_fails() {
589        let config = PhyTraceConfig::new("robot-001");
590        let mut builder = EventBuilder::new(&config);
591        // Clear the auto-populated identity
592        builder.event.identity = None;
593
594        let result = builder.build();
595        result.unwrap_err();
596    }
597
598    // ==========================================================================
599    // Additional Builder Tests for Coverage
600    // ==========================================================================
601
602    #[test]
603    fn test_builder_clone() {
604        let builder = EventBuilder::new(&test_config()).position_2d(10.0, 20.0);
605        let cloned = builder.clone();
606
607        let event1 = builder.build().unwrap();
608        let event2 = cloned.build().unwrap();
609
610        assert_eq!(
611            event1
612                .location
613                .as_ref()
614                .unwrap()
615                .local
616                .as_ref()
617                .unwrap()
618                .x_m,
619            event2
620                .location
621                .as_ref()
622                .unwrap()
623                .local
624                .as_ref()
625                .unwrap()
626                .x_m
627        );
628    }
629
630    #[test]
631    fn test_builder_source_type_override() {
632        let event = EventBuilder::new(&test_config())
633            .source_type(SourceType::Drone)
634            .position_2d(0.0, 0.0)
635            .build()
636            .unwrap();
637
638        assert_eq!(event.source_type, SourceType::Drone);
639    }
640
641    #[test]
642    fn test_builder_model_info() {
643        let event = EventBuilder::new(&test_config())
644            .model_info("Manufacturer", "Model X", "1.2.3")
645            .position_2d(0.0, 0.0)
646            .build()
647            .unwrap();
648
649        let identity = event.identity.unwrap();
650        assert_eq!(identity.model, Some("Model X".to_string()));
651        assert_eq!(identity.firmware_version, Some("1.2.3".to_string()));
652    }
653
654    #[test]
655    fn test_builder_tag() {
656        let event = EventBuilder::new(&test_config())
657            .tag("warehouse")
658            .tag("zone-a")
659            .position_2d(0.0, 0.0)
660            .build()
661            .unwrap();
662
663        let identity = event.identity.unwrap();
664        let tags = identity.tags.unwrap();
665        assert!(tags.contains(&"warehouse".to_string()));
666        assert!(tags.contains(&"zone-a".to_string()));
667    }
668
669    #[test]
670    fn test_builder_position_local() {
671        let event = EventBuilder::new(&test_config())
672            .position_local(1.0, 2.0, 3.0)
673            .build()
674            .unwrap();
675
676        let local = event.location.unwrap().local.unwrap();
677        assert_eq!(local.x_m, Some(1.0));
678        assert_eq!(local.y_m, Some(2.0));
679        assert_eq!(local.z_m, Some(3.0));
680    }
681
682    #[test]
683    fn test_builder_position_3d() {
684        let event = EventBuilder::new(&test_config())
685            .position_3d(5.0, 6.0, 7.0)
686            .build()
687            .unwrap();
688
689        let local = event.location.unwrap().local.unwrap();
690        assert_eq!(local.x_m, Some(5.0));
691        assert_eq!(local.y_m, Some(6.0));
692        assert_eq!(local.z_m, Some(7.0));
693    }
694
695    #[test]
696    fn test_builder_heading() {
697        // Test heading in radians (should convert to degrees)
698        let event = EventBuilder::new(&test_config())
699            .heading(std::f64::consts::PI) // 180 degrees
700            .position_2d(0.0, 0.0)
701            .build()
702            .unwrap();
703
704        let heading = event.location.unwrap().heading_deg.unwrap();
705        assert!((heading - 180.0).abs() < 0.001);
706    }
707
708    #[test]
709    fn test_builder_gps() {
710        let event = EventBuilder::new(&test_config())
711            .gps(41.8781, -87.6298)
712            .build()
713            .unwrap();
714
715        let location = event.location.unwrap();
716        assert_eq!(location.latitude, Some(41.8781));
717        assert_eq!(location.longitude, Some(-87.6298));
718    }
719
720    #[test]
721    fn test_builder_floor() {
722        let event = EventBuilder::new(&test_config())
723            .floor(3)
724            .position_2d(0.0, 0.0)
725            .build()
726            .unwrap();
727
728        assert_eq!(event.location.unwrap().floor, Some(3));
729    }
730
731    #[test]
732    fn test_builder_map() {
733        let event = EventBuilder::new(&test_config())
734            .map("map_001", "base_link")
735            .position_2d(0.0, 0.0)
736            .build()
737            .unwrap();
738
739        let location = event.location.unwrap();
740        assert_eq!(location.map_id, Some("map_001".to_string()));
741        assert_eq!(location.frame_id, Some("base_link".to_string()));
742    }
743
744    #[test]
745    fn test_builder_linear_velocity() {
746        let event = EventBuilder::new(&test_config())
747            .linear_velocity(1.0, 0.5, 0.0)
748            .build()
749            .unwrap();
750
751        let lv = event.motion.unwrap().linear_velocity.unwrap();
752        assert_eq!(lv.x_mps, Some(1.0));
753        assert_eq!(lv.y_mps, Some(0.5));
754        assert_eq!(lv.z_mps, Some(0.0));
755    }
756
757    #[test]
758    fn test_builder_angular_velocity() {
759        let event = EventBuilder::new(&test_config())
760            .angular_velocity(0.0, 0.0, 1.0) // rad/s, should convert to deg/s
761            .build()
762            .unwrap();
763
764        let av = event.motion.unwrap().angular_velocity.unwrap();
765        assert!((av.yaw_dps.unwrap() - 57.2958).abs() < 0.1); // ~1 rad/s in deg/s
766    }
767
768    #[test]
769    fn test_builder_battery_voltage() {
770        let event = EventBuilder::new(&test_config())
771            .battery_voltage(24.5)
772            .build()
773            .unwrap();
774
775        let battery = event.power.unwrap().battery.unwrap();
776        assert_eq!(battery.voltage_v, Some(24.5));
777    }
778
779    #[test]
780    fn test_builder_battery_health() {
781        let event = EventBuilder::new(&test_config())
782            .battery_health(95.0)
783            .build()
784            .unwrap();
785
786        let battery = event.power.unwrap().battery.unwrap();
787        assert_eq!(battery.state_of_health_pct, Some(95.0));
788    }
789
790    #[test]
791    fn test_builder_charging() {
792        let event_charging = EventBuilder::new(&test_config())
793            .charging(true)
794            .build()
795            .unwrap();
796
797        let charging = event_charging.power.unwrap().charging.unwrap();
798        assert_eq!(charging.state, Some(ChargingState::Charging));
799
800        let event_not_charging = EventBuilder::new(&test_config())
801            .charging(false)
802            .build()
803            .unwrap();
804
805        let charging = event_not_charging.power.unwrap().charging.unwrap();
806        assert_eq!(charging.state, Some(ChargingState::NotCharging));
807    }
808
809    #[test]
810    fn test_builder_operational_state() {
811        let event = EventBuilder::new(&test_config())
812            .operational_state(OperationalState::ExecutingTask)
813            .build()
814            .unwrap();
815
816        assert_eq!(
817            event.operational.unwrap().state,
818            Some(OperationalState::ExecutingTask)
819        );
820    }
821
822    #[test]
823    fn test_builder_task() {
824        let event = EventBuilder::new(&test_config())
825            .task("task-001", "delivery")
826            .build()
827            .unwrap();
828
829        let task = event.operational.unwrap().task.unwrap();
830        assert_eq!(task.task_id, Some("task-001".to_string()));
831        assert_eq!(task.task_type, Some("delivery".to_string()));
832    }
833
834    #[test]
835    fn test_builder_estop_active() {
836        let event = EventBuilder::new(&test_config())
837            .estop_active(true)
838            .build()
839            .unwrap();
840
841        let estop = event.safety.unwrap().e_stop.unwrap();
842        assert_eq!(estop.is_active, Some(true));
843    }
844
845    #[test]
846    fn test_builder_localization_quality() {
847        let event = EventBuilder::new(&test_config())
848            .localization_quality(LocalizationQuality::Good)
849            .build()
850            .unwrap();
851
852        let loc = event.navigation.unwrap().localization.unwrap();
853        assert_eq!(loc.quality, Some(LocalizationQuality::Good));
854    }
855
856    #[test]
857    fn test_builder_path_state() {
858        let event = EventBuilder::new(&test_config())
859            .path_state(PathState::Executing)
860            .build()
861            .unwrap();
862
863        let path = event.navigation.unwrap().path.unwrap();
864        assert_eq!(path.state, Some(PathState::Executing));
865    }
866
867    #[test]
868    fn test_builder_all_domain_setters() {
869        let event = EventBuilder::new(&test_config())
870            .identity(IdentityDomain::default())
871            .location(LocationDomain::default())
872            .motion(MotionDomain::default())
873            .power(PowerDomain::default())
874            .operational(OperationalDomain::default())
875            .navigation(NavigationDomain::default())
876            .perception(PerceptionDomain::default())
877            .safety(SafetyDomain::default())
878            .actuators(ActuatorsDomain::default())
879            .communication(CommunicationDomain::default())
880            .compute(ComputeDomain::default())
881            .ai(AiDomain::default())
882            .maintenance(MaintenanceDomain::default())
883            .context(ContextDomain::default())
884            .payload(PayloadDomain::default())
885            .manipulation(ManipulationDomain::default())
886            .hri(HriDomain::default())
887            .coordination(CoordinationDomain::default())
888            .simulation(SimulationDomain::default())
889            .thermal(ThermalDomain::default())
890            .audio(AudioDomain::default())
891            .environment_interaction(EnvironmentInteractionDomain::default())
892            .compliance(ComplianceDomain::default())
893            .build()
894            .unwrap();
895
896        assert!(event.identity.is_some());
897        assert!(event.location.is_some());
898        assert!(event.motion.is_some());
899        assert!(event.power.is_some());
900        assert!(event.operational.is_some());
901        assert!(event.navigation.is_some());
902        assert!(event.perception.is_some());
903        assert!(event.safety.is_some());
904        assert!(event.actuators.is_some());
905        assert!(event.communication.is_some());
906        assert!(event.compute.is_some());
907        assert!(event.ai.is_some());
908        assert!(event.maintenance.is_some());
909        assert!(event.context.is_some());
910        assert!(event.payload.is_some());
911        assert!(event.manipulation.is_some());
912        assert!(event.hri.is_some());
913        assert!(event.coordination.is_some());
914        assert!(event.simulation.is_some());
915        assert!(event.thermal.is_some());
916        assert!(event.audio.is_some());
917        assert!(event.environment_interaction.is_some());
918        assert!(event.compliance.is_some());
919    }
920
921    #[test]
922    fn test_builder_extensions() {
923        let extensions = serde_json::json!({
924            "custom_field": "custom_value",
925            "nested": { "key": 123 }
926        });
927
928        let event = EventBuilder::new(&test_config())
929            .extensions(extensions.clone())
930            .position_2d(0.0, 0.0)
931            .build()
932            .unwrap();
933
934        assert_eq!(event.extensions, Some(extensions));
935    }
936
937    #[test]
938    fn test_builder_build_unchecked() {
939        let config = PhyTraceConfig::new("robot-001");
940        let mut builder = EventBuilder::new(&config);
941        builder.event.identity = None;
942
943        // build_unchecked should succeed even without domain data
944        let event = builder.build_unchecked();
945        assert!(event.identity.is_none());
946    }
947
948    #[test]
949    fn test_builder_peek() {
950        let builder = EventBuilder::new(&test_config()).position_2d(10.0, 20.0);
951
952        let event = builder.peek();
953        assert!(event.location.is_some());
954    }
955
956    #[test]
957    fn test_builder_config_with_tags() {
958        // Test that tags from config are passed through
959        let config = PhyTraceConfig::new("robot-001").with_source_type(SourceType::Amr);
960
961        let event = EventBuilder::new(&config)
962            .tag("test-tag")
963            .position_2d(0.0, 0.0)
964            .build()
965            .unwrap();
966
967        let identity = event.identity.unwrap();
968        assert!(identity.tags.is_some());
969        assert!(identity.tags.unwrap().contains(&"test-tag".to_string()));
970    }
971}