1use 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
13pub 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 pub fn new(config: &PhyTraceConfig) -> Self {
49 let mut event = UdmEvent::new(config.source.source_type);
50
51 event.source_id = config.source.source_id.clone();
53
54 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 pub fn event_type(mut self, event_type: EventType) -> Self {
76 self.event.event_type = event_type;
77 self
78 }
79
80 pub fn source_type(mut self, source_type: SourceType) -> Self {
82 self.event.source_type = source_type;
83 self
84 }
85
86 pub fn source_id(mut self, source_id: impl Into<String>) -> Self {
93 let sid = source_id.into();
94 self.event.source_id = sid.clone();
96 let identity = self.event.identity.get_or_insert_with(Default::default);
98 identity.source_id = Some(sid);
99 self
100 }
101
102 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 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 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 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 pub fn position_3d(self, x: f64, y: f64, z: f64) -> Self {
153 self.position_local(x, y, z)
154 }
155
156 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 pub fn identity(mut self, identity: IdentityDomain) -> Self {
343 self.event.identity = Some(identity);
344 self
345 }
346
347 pub fn location(mut self, location: LocationDomain) -> Self {
349 self.event.location = Some(location);
350 self
351 }
352
353 pub fn motion(mut self, motion: MotionDomain) -> Self {
355 self.event.motion = Some(motion);
356 self
357 }
358
359 pub fn power(mut self, power: PowerDomain) -> Self {
361 self.event.power = Some(power);
362 self
363 }
364
365 pub fn operational(mut self, operational: OperationalDomain) -> Self {
367 self.event.operational = Some(operational);
368 self
369 }
370
371 pub fn navigation(mut self, navigation: NavigationDomain) -> Self {
373 self.event.navigation = Some(navigation);
374 self
375 }
376
377 pub fn perception(mut self, perception: PerceptionDomain) -> Self {
379 self.event.perception = Some(perception);
380 self
381 }
382
383 pub fn safety(mut self, safety: SafetyDomain) -> Self {
385 self.event.safety = Some(safety);
386 self
387 }
388
389 pub fn actuators(mut self, actuators: ActuatorsDomain) -> Self {
391 self.event.actuators = Some(actuators);
392 self
393 }
394
395 pub fn communication(mut self, communication: CommunicationDomain) -> Self {
397 self.event.communication = Some(communication);
398 self
399 }
400
401 pub fn compute(mut self, compute: ComputeDomain) -> Self {
403 self.event.compute = Some(compute);
404 self
405 }
406
407 pub fn ai(mut self, ai: AiDomain) -> Self {
409 self.event.ai = Some(ai);
410 self
411 }
412
413 pub fn maintenance(mut self, maintenance: MaintenanceDomain) -> Self {
415 self.event.maintenance = Some(maintenance);
416 self
417 }
418
419 pub fn context(mut self, context: ContextDomain) -> Self {
421 self.event.context = Some(context);
422 self
423 }
424
425 pub fn payload(mut self, payload: PayloadDomain) -> Self {
427 self.event.payload = Some(payload);
428 self
429 }
430
431 pub fn manipulation(mut self, manipulation: ManipulationDomain) -> Self {
433 self.event.manipulation = Some(manipulation);
434 self
435 }
436
437 pub fn hri(mut self, hri: HriDomain) -> Self {
439 self.event.hri = Some(hri);
440 self
441 }
442
443 pub fn coordination(mut self, coordination: CoordinationDomain) -> Self {
445 self.event.coordination = Some(coordination);
446 self
447 }
448
449 pub fn simulation(mut self, simulation: SimulationDomain) -> Self {
451 self.event.simulation = Some(simulation);
452 self
453 }
454
455 pub fn thermal(mut self, thermal: ThermalDomain) -> Self {
457 self.event.thermal = Some(thermal);
458 self
459 }
460
461 pub fn audio(mut self, audio: AudioDomain) -> Self {
463 self.event.audio = Some(audio);
464 self
465 }
466
467 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 pub fn compliance(mut self, compliance: ComplianceDomain) -> Self {
478 self.event.compliance = Some(compliance);
479 self
480 }
481
482 pub fn extensions(mut self, extensions: serde_json::Value) -> Self {
484 self.event.extensions = Some(extensions);
485 self
486 }
487
488 pub fn heartbeat(mut self, heartbeat: serde_json::Value) -> Self {
490 self.event.heartbeat = Some(heartbeat);
491 self
492 }
493
494 pub fn build(self) -> PhyTraceResult<UdmEvent> {
500 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 pub fn build_unchecked(self) -> UdmEvent {
510 self.event
511 }
512
513 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 builder.event.identity = None;
593
594 let result = builder.build();
595 result.unwrap_err();
596 }
597
598 #[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 let event = EventBuilder::new(&test_config())
699 .heading(std::f64::consts::PI) .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) .build()
762 .unwrap();
763
764 let av = event.motion.unwrap().angular_velocity.unwrap();
765 assert!((av.yaw_dps.unwrap() - 57.2958).abs() < 0.1); }
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 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 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}