phoxal 0.16.0

Phoxal — production-oriented autonomous robot framework (engine, model, typed bus, contracts).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Golden tests for the generated API layer: the version-local body shape (plain
//! payload, no `{"v":…}` wrapper — D62), the `ContractBody` consts, the
//! `ApiVersion` id, and the topic keys produced by the api-local builders.

use crate::api::y2026_1 as api;
use crate::api::{ApiVersion, ContractBody};

#[test]
fn api_version_id_is_the_dated_module_name() {
    assert_eq!(<api::Api as ApiVersion>::ID, "y2026_1");
}

#[test]
fn contract_body_consts_are_family_and_topic() {
    assert_eq!(<api::drive::State as ContractBody>::FAMILY, "drive::State");
    assert_eq!(<api::drive::State as ContractBody>::TOPIC, "drive/state");
    assert_eq!(
        <api::drive::Target as ContractBody>::FAMILY,
        "drive::Target"
    );
    assert_eq!(<api::drive::Target as ContractBody>::TOPIC, "drive/target");
    assert_eq!(
        <api::motor::Command as ContractBody>::TOPIC,
        "motor/command"
    );
    assert_eq!(
        <api::localize::LocalizationState as ContractBody>::TOPIC,
        "localize/state"
    );
}

#[test]
fn body_serializes_as_plain_payload_without_version_tag() {
    let target = api::drive::Target {
        linear_x_mps: 1.0,
        angular_z_radps: 0.5,
    };
    // MessagePack-as-JSON projection: the wire body is the plain struct — there is
    // no `v`/`data` envelope around it.
    let json = serde_json::to_value(&target).unwrap();
    assert_eq!(json["linear_x_mps"], 1.0);
    assert!(
        json.get("v").is_none(),
        "wire body must not carry a version tag"
    );
    assert!(json.get("data").is_none());
}

#[test]
fn body_round_trips_through_messagepack() {
    let state = api::drive::State {
        target: api::drive::Target {
            linear_x_mps: 0.3,
            angular_z_radps: -0.2,
        },
        limited_target: api::drive::Target {
            linear_x_mps: 0.3,
            angular_z_radps: -0.2,
        },
        actuator_authority: api::drive::ActuatorAuthority::Active,
        stop_reason: None,
    };
    let bytes = rmp_serde::to_vec_named(&state).unwrap();
    let decoded: api::drive::State = rmp_serde::from_slice(&bytes).unwrap();
    assert_eq!(state, decoded);
}

#[test]
fn recovered_component_capability_bodies_round_trip_through_messagepack() {
    let imu = api::component::ImuSample {
        orientation: Some([1.0, 0.0, 0.0, 0.0]),
        angular_velocity_radps: [0.1, 0.2, 0.3],
        linear_acceleration_mps2: [1.0, 2.0, 9.81],
        covariance: Some([0.0; 9]),
        noise_density: Some([0.01, 0.02, 0.03]),
        sensor_frame_id: Some("imu_link".to_string()),
        measured_at_ns: Some(42),
        health: api::component::SensorHealth::Degraded,
        bias: Some(api::component::Bias {
            angular_velocity_radps: [0.001, 0.002, 0.003],
            linear_acceleration_mps2: [0.01, 0.02, 0.03],
        }),
    };
    let bytes = rmp_serde::to_vec_named(&imu).unwrap();
    let decoded: api::component::ImuSample = rmp_serde::from_slice(&bytes).unwrap();
    assert_eq!(imu, decoded);

    let frame = api::component::CameraFrame {
        width: 640,
        height: 480,
        encoding: api::component::CameraEncoding::Rgb8,
        intrinsics: Some(api::component::CameraIntrinsics {
            fx: 500.0,
            fy: 501.0,
            cx: 320.0,
            cy: 240.0,
        }),
        distortion: Some(api::component::CameraDistortion {
            model: "plumb_bob".to_string(),
            coefficients: vec![0.1, 0.2, 0.3],
        }),
        exposure: Some(api::component::CameraExposureTiming {
            exposure_start_ns: Some(100),
            exposure_duration_ns: Some(200),
        }),
        measured_at_ns: Some(300),
        calibration: Some(api::component::CameraCalibrationIdentity {
            id: "front".to_string(),
            version: "v1".to_string(),
        }),
        data: vec![1, 2, 3, 4],
    };
    let bytes = rmp_serde::to_vec_named(&frame).unwrap();
    let decoded: api::component::CameraFrame = rmp_serde::from_slice(&bytes).unwrap();
    assert_eq!(frame, decoded);
}

#[test]
fn topic_builder_keys_match_contract_topics() {
    assert_eq!(api::topic::new().drive().state().key(), "drive/state");
    assert_eq!(api::topic::new().drive().target().key(), "drive/target");
    assert_eq!(api::topic::new().motor().command().key(), "motor/command");
    assert_eq!(
        api::topic::new().presence().heartbeat().key(),
        "presence/heartbeat"
    );
}

// ---- dynamic / parameterized topics ----------------------------------------

#[test]
fn dynamic_topic_builder_fills_the_key_template() {
    let topic = api::topic::new()
        .component()
        .motor_command("front_left_drive", "motor");
    assert_eq!(
        topic.key(),
        "component/front_left_drive/motor/motor/command"
    );
    let enc = api::topic::new()
        .component()
        .encoder_sample("front_left_drive", "encoder");
    assert_eq!(
        enc.key(),
        "component/front_left_drive/encoder/encoder/sample"
    );
}

#[test]
fn recovered_component_capability_topic_builders_fill_key_templates() {
    let component = api::topic::new().component();
    assert_eq!(
        component.accelerometer_sample("imu0", "accel").key(),
        "component/imu0/accelerometer/accel/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .gyroscope_sample("imu0", "gyro")
            .key(),
        "component/imu0/gyroscope/gyro/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .magnetometer_sample("imu0", "mag")
            .key(),
        "component/imu0/magnetometer/mag/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .imu_sample("imu0", "imu")
            .key(),
        "component/imu0/imu/imu/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .range_sample("base", "front_tof")
            .key(),
        "component/base/range/front_tof/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .gnss_sample("gps", "gnss")
            .key(),
        "component/gps/gnss/gnss/sample"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .camera_frame("head", "front")
            .key(),
        "component/head/camera/front/frame"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .depth_frame("head", "front_depth")
            .key(),
        "component/head/depth/front_depth/frame"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .lidar_scan("front_lidar", "scan")
            .key(),
        "component/front_lidar/lidar/scan/scan"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .mmwave_scan("radar", "mmwave")
            .key(),
        "component/radar/mmwave/mmwave/scan"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .microphone_frame("head", "mic")
            .key(),
        "component/head/microphone/mic/frame"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .led_command("status_panel", "status")
            .key(),
        "component/status_panel/led/status/command"
    );
    assert_eq!(
        api::topic::new()
            .component()
            .emergency_stop_state("safety_panel", "estop")
            .key(),
        "component/safety_panel/emergency_stop/estop/state"
    );
}

#[test]
fn dynamic_topic_contract_body_topic_is_the_template() {
    assert_eq!(
        <api::component::MotorCommand as ContractBody>::TOPIC,
        "component/{instance}/motor/{capability}/command"
    );
    assert_eq!(
        <api::component::MotorCommand as ContractBody>::FAMILY,
        "component::MotorCommand"
    );
    assert_eq!(
        <api::component::ImuSample as ContractBody>::TOPIC,
        "component/{instance}/imu/{capability}/sample"
    );
    assert_eq!(
        <api::component::ImuSample as ContractBody>::FAMILY,
        "component::ImuSample"
    );
}

#[test]
fn dynamic_topic_wildcard_is_subscribe_only() {
    let concrete = api::topic::new().component().motor_command("base", "motor");
    assert!(concrete.publish_key().is_ok());

    let wildcard = api::topic::new().component().motor_command("*", "motor");
    assert_eq!(wildcard.key(), "component/*/motor/motor/command");
    assert!(wildcard.publish_key().is_err());
}

// ---- API inheritance (`extends`, D61) --------------------------------------

mod extends {
    use crate::api::y2026_1 as v1;
    use crate::api::y2026_2 as v2;
    use crate::api::{ApiVersion, ContractBody};

    #[test]
    fn child_version_has_its_own_id() {
        assert_eq!(<v2::Api as ApiVersion>::ID, "y2026_2");
    }

    #[test]
    fn inherited_type_is_a_fresh_type_with_the_same_family_and_topic() {
        // Same FAMILY/TOPIC consts as the parent...
        assert_eq!(
            <v2::localize::LocalizationState as ContractBody>::FAMILY,
            <v1::localize::LocalizationState as ContractBody>::FAMILY,
        );
        assert_eq!(
            <v2::localize::LocalizationState as ContractBody>::TOPIC,
            <v1::localize::LocalizationState as ContractBody>::TOPIC,
        );
        // ...but bound to a different API version.
        assert_eq!(
            <<v2::localize::LocalizationState as ContractBody>::Api as ApiVersion>::ID,
            "y2026_2",
        );
        assert_eq!(
            <<v1::localize::LocalizationState as ContractBody>::Api as ApiVersion>::ID,
            "y2026_1",
        );
    }

    #[test]
    fn inherited_type_is_wire_identical_to_the_parent() {
        // The parent struct is re-emitted verbatim, so the same field values
        // serialize byte-for-byte identically across versions (D61).
        let v1_body = v1::localize::LocalizationState {
            x_m: 1.0,
            y_m: 2.0,
            yaw_rad: 0.5,
            confidence: 0.9,
        };
        let v2_body = v2::localize::LocalizationState {
            x_m: 1.0,
            y_m: 2.0,
            yaw_rad: 0.5,
            confidence: 0.9,
        };
        let v1_bytes = rmp_serde::to_vec_named(&v1_body).unwrap();
        let v2_bytes = rmp_serde::to_vec_named(&v2_body).unwrap();
        assert_eq!(v1_bytes, v2_bytes);

        // And a v1 payload decodes into the v2 type (wire-compatible).
        let decoded: v2::localize::LocalizationState = rmp_serde::from_slice(&v1_bytes).unwrap();
        assert_eq!(decoded, v2_body);
    }

    #[test]
    fn overridden_type_replaces_the_parent_but_keeps_family_and_topic() {
        // y2026_2 overrides drive::Target with an extra field, same family/topic.
        let target = v2::drive::Target {
            linear_x_mps: 0.3,
            angular_z_radps: 0.1,
            curvature_limit_radpm: Some(2.0),
        };
        assert_eq!(target.curvature_limit_radpm, Some(2.0));
        assert_eq!(
            <v2::drive::Target as ContractBody>::FAMILY,
            <v1::drive::Target as ContractBody>::FAMILY,
        );
        assert_eq!(
            <v2::drive::Target as ContractBody>::TOPIC,
            <v1::drive::Target as ContractBody>::TOPIC,
        );
    }

    #[test]
    fn inherited_type_reflects_an_overridden_dependency() {
        // `drive::State` embeds `drive::Target`. y2026_2 overrides `Target`, so the
        // inherited `y2026_2::drive::State` embeds the NEW Target (extra field) and
        // is NOT byte-identical to y2026_1's — correct versioning (a contract
        // changes with its dependencies).
        let v1_state = v1::drive::State {
            target: v1::drive::Target {
                linear_x_mps: 0.0,
                angular_z_radps: 0.0,
            },
            limited_target: v1::drive::Target {
                linear_x_mps: 0.0,
                angular_z_radps: 0.0,
            },
            actuator_authority: v1::drive::ActuatorAuthority::Active,
            stop_reason: None,
        };
        let v2_state = v2::drive::State {
            target: v2::drive::Target {
                linear_x_mps: 0.0,
                angular_z_radps: 0.0,
                curvature_limit_radpm: None,
            },
            limited_target: v2::drive::Target {
                linear_x_mps: 0.0,
                angular_z_radps: 0.0,
                curvature_limit_radpm: None,
            },
            actuator_authority: v2::drive::ActuatorAuthority::Active,
            stop_reason: None,
        };
        let v1_bytes = rmp_serde::to_vec_named(&v1_state).unwrap();
        let v2_bytes = rmp_serde::to_vec_named(&v2_state).unwrap();
        assert_ne!(
            v1_bytes, v2_bytes,
            "the inherited State embeds the overridden Target, so it changes with it"
        );
    }

    #[test]
    fn new_family_exists_only_in_the_child() {
        assert_eq!(
            <v2::battery::State as ContractBody>::FAMILY,
            "battery::State"
        );
        assert_eq!(<v2::battery::State as ContractBody>::TOPIC, "battery/state");
    }

    #[test]
    fn child_topic_builders_cover_inherited_overridden_and_new_families() {
        // inherited
        assert_eq!(v2::topic::new().localize().state().key(), "localize/state");
        assert_eq!(v2::topic::new().motor().command().key(), "motor/command");
        // overridden
        assert_eq!(v2::topic::new().drive().target().key(), "drive/target");
        // new
        assert_eq!(v2::topic::new().battery().state().key(), "battery/state");
    }
}

// A self-contained three-version tree exercising multi-level inheritance
// (`vc extends vb extends va`) without touching the production api tree.
mod multi_level {
    use crate::api::{ApiVersion, ContractBody};

    crate::phoxal_api_tree! {
        version va {
            sensor {
                struct Reading { value_a: f32 }
                topic reading: pubsub Reading;
            }
        }
        version vb extends va {
            beacon {
                struct Ping { seq: u64 }
                topic ping: pubsub Ping;
            }
        }
        version vc extends vb {
            sensor {
                struct Reading { value_a: f32, value_b: f32 }
                topic reading: pubsub Reading;
            }
        }
    }

    #[test]
    fn transitive_inheritance_carries_through_two_levels() {
        // `beacon` is declared in vb and inherited by vc (two levels deep).
        assert_eq!(<vc::beacon::Ping as ContractBody>::TOPIC, "beacon/ping");
        assert_eq!(<vc::beacon::Ping as ContractBody>::FAMILY, "beacon::Ping");
        assert_eq!(
            <<vc::beacon::Ping as ContractBody>::Api as ApiVersion>::ID,
            "vc"
        );

        // `sensor::Reading` is inherited unchanged into vb, then overridden in vc.
        assert_eq!(
            <vb::sensor::Reading as ContractBody>::TOPIC,
            "sensor/reading"
        );
        let _vc_reading = vc::sensor::Reading {
            value_a: 1.0,
            value_b: 2.0,
        };
        assert_eq!(vc::topic::new().beacon().ping().key(), "beacon/ping");
        assert_eq!(vc::topic::new().sensor().reading().key(), "sensor/reading");
    }
}