vox 0.4.0

Core Vox library crate
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
//! Schema compatibility tests: two versions of a service talk to each other.
//!
//! Each test pair defines a "v1" and "v2" module with the same trait name
//! and method names but different field types. Because method IDs are
//! name-only (not type-dependent), the two versions route to the same
//! handler. Schema exchange sends type metadata before payloads, and
//! translation plans handle the schema differences.

use vox_core::{BareConduit, MemoryLink, acceptor_conduit, initiator_conduit, memory_link_pair};
use vox_types::{
    ConnectionSettings, HandshakeResult, MessageFamily, MetadataEntry, Parity, SessionRole,
    VoxError,
};

type MessageConduit = BareConduit<MessageFamily, MemoryLink>;

fn conduit_pair() -> (MessageConduit, MessageConduit) {
    let (a, b) = memory_link_pair(64);
    (BareConduit::new(a), BareConduit::new(b))
}

fn test_acceptor_handshake(service: &'static str) -> HandshakeResult {
    HandshakeResult {
        role: SessionRole::Acceptor,
        our_settings: ConnectionSettings {
            parity: Parity::Even,
            max_concurrent_requests: 64,
        },
        peer_settings: ConnectionSettings {
            parity: Parity::Odd,
            max_concurrent_requests: 64,
        },
        peer_supports_retry: true,
        session_resume_key: None,
        peer_resume_key: None,
        our_schema: vec![],
        peer_schema: vec![],
        peer_metadata: vec![MetadataEntry::str("vox-service", service)],
    }
}

fn test_initiator_handshake(service: &'static str) -> HandshakeResult {
    HandshakeResult {
        role: SessionRole::Initiator,
        our_settings: ConnectionSettings {
            parity: Parity::Odd,
            max_concurrent_requests: 64,
        },
        peer_settings: ConnectionSettings {
            parity: Parity::Even,
            max_concurrent_requests: 64,
        },
        peer_supports_retry: true,
        session_resume_key: None,
        peer_resume_key: None,
        our_schema: vec![],
        peer_schema: vec![],
        peer_metadata: vec![MetadataEntry::str("vox-service", service)],
    }
}

// ============================================================================
// Compatible: added field with default
// ============================================================================

/// V1: the original schema — two fields.
mod point_v1 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Point {
        pub x: f64,
        pub y: f64,
    }

    #[vox::service]
    pub trait Geometry {
        async fn transform(&self, p: Point) -> Point;
    }
}

/// V2: added a field with a default — compatible in both directions.
mod point_v2 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Point {
        pub x: f64,
        pub y: f64,
        #[facet(default)]
        pub z: f64,
    }

    #[vox::service]
    pub trait Geometry {
        async fn transform(&self, p: Point) -> Point;
    }
}

// V1 server implementation
#[derive(Clone)]
struct V1GeometryService;

impl point_v1::Geometry for V1GeometryService {
    async fn transform(&self, p: point_v1::Point) -> point_v1::Point {
        point_v1::Point {
            x: p.x * 2.0,
            y: p.y * 2.0,
        }
    }
}

// V2 server implementation
#[derive(Clone)]
struct V2GeometryService;

impl point_v2::Geometry for V2GeometryService {
    async fn transform(&self, p: point_v2::Point) -> point_v2::Point {
        point_v2::Point {
            x: p.x + 100.0,
            y: p.y + 100.0,
            z: p.z + 1.0,
        }
    }
}

// r[verify schema.translation.fill-defaults]
// r[verify schema.interaction.channels]
#[tokio::test]
async fn v1_client_v2_server_fills_default() {
    let (client_conduit, server_conduit) = conduit_pair();

    let server_task = tokio::task::spawn(async move {
        let _server_caller = acceptor_conduit(server_conduit, test_acceptor_handshake("Geometry"))
            .on_connection(point_v2::GeometryDispatcher::new(V2GeometryService))
            .establish::<point_v2::GeometryClient>()
            .await
            .expect("server handshake failed");
        std::future::pending::<()>().await;
    });

    let client = initiator_conduit(client_conduit, test_initiator_handshake("Geometry"))
        .establish::<point_v1::GeometryClient>()
        .await
        .expect("client handshake failed");

    // Client sends V1 Point {x, y}. Server receives as V2 Point {x, y, z=0.0}.
    let result = client
        .transform(point_v1::Point { x: 1.0, y: 2.0 })
        .await
        .expect("call should succeed");

    // Server adds 100 to x and y, adds 1.0 to z (which was 0.0).
    // Response is V2 Point. Client reads it as V1 Point (drops z).
    assert_eq!(result.x, 101.0);
    assert_eq!(result.y, 102.0);

    server_task.abort();
}

// r[verify schema.translation.skip-unknown]
#[tokio::test]
async fn v2_client_v1_server_skips_unknown_field() {
    let (client_conduit, server_conduit) = conduit_pair();

    let server_task = tokio::task::spawn(async move {
        let _server_caller = acceptor_conduit(server_conduit, test_acceptor_handshake("Geometry"))
            .on_connection(point_v1::GeometryDispatcher::new(V1GeometryService))
            .establish::<point_v1::GeometryClient>()
            .await
            .expect("server handshake failed");
        std::future::pending::<()>().await;
    });

    let client = initiator_conduit(client_conduit, test_initiator_handshake("Geometry"))
        .establish::<point_v2::GeometryClient>()
        .await
        .expect("client handshake failed");

    // Client sends V2 Point {x, y, z}. Server receives as V1 Point {x, y} — z is skipped.
    let result = client
        .transform(point_v2::Point {
            x: 3.0,
            y: 4.0,
            z: 99.0,
        })
        .await
        .expect("call should succeed");

    // Server doubles x and y. Response is V1 Point.
    // Client reads it as V2 Point (z fills default 0.0).
    assert_eq!(result.x, 6.0);
    assert_eq!(result.y, 8.0);
    assert_eq!(result.z, 0.0);

    server_task.abort();
}

// ============================================================================
// Compatible: field reorder
// ============================================================================

mod reordered_v1 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Pair {
        pub first: String,
        pub second: u32,
    }

    #[vox::service]
    pub trait PairService {
        async fn echo(&self, p: Pair) -> Pair;
    }
}

mod reordered_v2 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Pair {
        pub second: u32,
        pub first: String,
    }

    #[vox::service]
    pub trait PairService {
        async fn echo(&self, p: Pair) -> Pair;
    }
}

#[derive(Clone)]
struct PairEchoV1;

impl reordered_v1::PairService for PairEchoV1 {
    async fn echo(&self, p: reordered_v1::Pair) -> reordered_v1::Pair {
        p
    }
}

// r[verify schema.translation.reorder]
#[tokio::test]
async fn reordered_fields_are_matched_by_name() {
    let (client_conduit, server_conduit) = conduit_pair();

    let server_task = tokio::task::spawn(async move {
        let _server_caller =
            acceptor_conduit(server_conduit, test_acceptor_handshake("PairService"))
                .on_connection(reordered_v1::PairServiceDispatcher::new(PairEchoV1))
                .establish::<reordered_v1::PairServiceClient>()
                .await
                .expect("server handshake failed");
        std::future::pending::<()>().await;
    });

    let client = initiator_conduit(client_conduit, test_initiator_handshake("PairService"))
        .establish::<reordered_v2::PairServiceClient>()
        .await
        .expect("client handshake failed");

    // Client sends V2 Pair {second, first}. Server receives as V1 Pair {first, second}.
    let result = client
        .echo(reordered_v2::Pair {
            second: 42,
            first: "hello".into(),
        })
        .await
        .expect("call should succeed");

    assert_eq!(result.second, 42);
    assert_eq!(result.first, "hello");

    server_task.abort();
}

// ============================================================================
// Compatible: combined add + remove + reorder
// ============================================================================

mod evolved_v1 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Config {
        pub name: String,
        pub timeout_ms: u64,
        pub retries: u32,
    }

    #[vox::service]
    pub trait ConfigService {
        async fn get(&self) -> Config;
    }
}

mod evolved_v2 {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct Config {
        pub retries: u32,
        pub name: String,
        // timeout_ms removed, priority added with default
        #[facet(default)]
        pub priority: u32,
    }

    #[vox::service]
    pub trait ConfigService {
        async fn get(&self) -> Config;
    }
}

#[derive(Clone)]
struct ConfigServiceV1;

impl evolved_v1::ConfigService for ConfigServiceV1 {
    async fn get(&self) -> evolved_v1::Config {
        evolved_v1::Config {
            name: "prod".into(),
            timeout_ms: 5000,
            retries: 3,
        }
    }
}

// r[verify schema.translation.fill-defaults]
// r[verify schema.translation.skip-unknown]
// r[verify schema.translation.reorder]
#[tokio::test]
async fn evolved_schema_combined_changes() {
    let (client_conduit, server_conduit) = conduit_pair();

    let server_task = tokio::task::spawn(async move {
        let _server_caller =
            acceptor_conduit(server_conduit, test_acceptor_handshake("ConfigService"))
                .on_connection(evolved_v1::ConfigServiceDispatcher::new(ConfigServiceV1))
                .establish::<evolved_v1::ConfigServiceClient>()
                .await
                .expect("server handshake failed");
        std::future::pending::<()>().await;
    });

    let client = initiator_conduit(client_conduit, test_initiator_handshake("ConfigService"))
        .establish::<evolved_v2::ConfigServiceClient>()
        .await
        .expect("client handshake failed");

    // V1 server returns {name, timeout_ms, retries}.
    // V2 client reads as {retries, name, priority} — reordered, timeout_ms skipped, priority defaults.
    let result = client.get().await.expect("call should succeed");

    assert_eq!(result.name, "prod");
    assert_eq!(result.retries, 3);
    assert_eq!(result.priority, 0); // default

    server_task.abort();
}

// ============================================================================
// Incompatible: missing required field without default
// ============================================================================

/// Old daemon: only has basic fields.
mod status_old {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct DaemonStatus {
        pub uptime_ms: u64,
        pub listen: String,
    }

    #[vox::service]
    pub trait Daemon {
        async fn status(&self) -> DaemonStatus;
    }
}

/// New client: expects additional required fields that the old daemon doesn't have.
mod status_new {
    #[derive(Debug, Clone, PartialEq, facet::Facet)]
    pub struct DaemonStatus {
        pub uptime_ms: u64,
        pub listen: String,
        pub pid: u32,                // required, no default — incompatible
        pub executable_path: String, // required, no default — incompatible
    }

    #[vox::service]
    pub trait Daemon {
        async fn status(&self) -> DaemonStatus;
    }
}

#[derive(Clone)]
struct OldDaemonService;

impl status_old::Daemon for OldDaemonService {
    async fn status(&self) -> status_old::DaemonStatus {
        status_old::DaemonStatus {
            uptime_ms: 12345,
            listen: "local:///tmp/daemon.vox".into(),
        }
    }
}

// r[verify schema.errors.missing-required]
// r[verify schema.errors.non-retryable]
// r[verify rpc.fallible.vox-error.retryable]
#[tokio::test]
async fn missing_required_field_is_non_retryable() {
    let (client_conduit, server_conduit) = conduit_pair();

    let server_task = tokio::task::spawn(async move {
        let _server_caller = acceptor_conduit(server_conduit, test_acceptor_handshake("Daemon"))
            .on_connection(status_old::DaemonDispatcher::new(OldDaemonService))
            .establish::<status_old::DaemonClient>()
            .await
            .expect("server handshake failed");
        std::future::pending::<()>().await;
    });

    let client = initiator_conduit(client_conduit, test_initiator_handshake("Daemon"))
        .establish::<status_new::DaemonClient>()
        .await
        .expect("client handshake failed");

    // New client calls old daemon. The response has DaemonStatus with only
    // {uptime_ms, listen}, but the client expects {uptime_ms, listen, pid,
    // executable_path}. Translation plan fails on the missing required fields.
    let err = client.status().await.expect_err("call should fail");

    // The error must be InvalidPayload (translation plan failure).
    assert!(
        matches!(&err, VoxError::InvalidPayload(msg) if msg.contains("translation plan failed")),
        "expected InvalidPayload with translation plan failure, got: {err:?}"
    );

    // And it must be non-retryable.
    assert!(
        !err.is_retryable(),
        "schema incompatibility must be non-retryable"
    );

    server_task.abort();
}