peat-protocol 0.9.0-rc.10

Peat Coordination Protocol — hierarchical capability composition over CRDTs for heterogeneous mesh networks
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
//! Simultaneous Iroh + BLE transport proof
//!
//! Proves that a **real** IrohMeshTransport (with QUIC accept loop) runs
//! alongside a mock BLE transport in the same TransportManager. This is the
//! M4 dual-active integration proof: real async QUIC endpoint + mock BLE
//! coexist, route correctly, and PACE fallback works when Iroh stops.
//!
//! Previous tests proved:
//! - `dual_active_transport_e2e.rs`: all-mock routing logic
//! - `canned_message_sync.rs`: CannedMessage over encrypted BLE
//! - Pi-to-Pi functional test: real BLE sync (277 ms)
//!
//! This test adds: real QUIC transport lifecycle in the same manager as BLE.

#![cfg(feature = "automerge-backend")]

use std::sync::Arc;
use std::time::Instant;

use async_trait::async_trait;
use tokio::sync::mpsc;

use peat_protocol::network::iroh_transport::IrohTransport;
use peat_protocol::network::peer_config::PeerConfig;
use peat_protocol::transport::iroh::IrohMeshTransport;
use peat_protocol::transport::{
    CollectionRouteConfig, CollectionRouteTable, CollectionTransportRoute, MeshConnection,
    MeshTransport, MessagePriority, MessageRequirements, NodeId, PeerEventReceiver, RouteDecision,
    Transport, TransportCapabilities, TransportInstance, TransportManager, TransportManagerConfig,
    TransportPolicy, TransportType,
};

// =============================================================================
// Mock BLE Transport (self-contained, Transport trait only)
// =============================================================================

struct MockBleTransport {
    caps: TransportCapabilities,
    reachable_peers: Vec<NodeId>,
}

impl MockBleTransport {
    fn new(peers: Vec<NodeId>) -> Self {
        Self {
            caps: TransportCapabilities::bluetooth_le(),
            reachable_peers: peers,
        }
    }
}

struct MockBleConnection {
    peer_id: NodeId,
    connected_at: Instant,
}

impl MeshConnection for MockBleConnection {
    fn peer_id(&self) -> &NodeId {
        &self.peer_id
    }
    fn is_alive(&self) -> bool {
        true
    }
    fn connected_at(&self) -> Instant {
        self.connected_at
    }
}

#[async_trait]
impl MeshTransport for MockBleTransport {
    async fn start(&self) -> peat_protocol::transport::Result<()> {
        Ok(())
    }
    async fn stop(&self) -> peat_protocol::transport::Result<()> {
        Ok(())
    }
    async fn connect(
        &self,
        peer_id: &NodeId,
    ) -> peat_protocol::transport::Result<Box<dyn MeshConnection>> {
        Ok(Box::new(MockBleConnection {
            peer_id: peer_id.clone(),
            connected_at: Instant::now(),
        }))
    }
    async fn disconnect(&self, _peer_id: &NodeId) -> peat_protocol::transport::Result<()> {
        Ok(())
    }
    fn get_connection(&self, _peer_id: &NodeId) -> Option<Box<dyn MeshConnection>> {
        None
    }
    fn peer_count(&self) -> usize {
        0
    }
    fn connected_peers(&self) -> Vec<NodeId> {
        vec![]
    }
    fn subscribe_peer_events(&self) -> PeerEventReceiver {
        let (_tx, rx) = mpsc::channel(1);
        rx
    }
}

impl Transport for MockBleTransport {
    fn capabilities(&self) -> &TransportCapabilities {
        &self.caps
    }

    fn is_available(&self) -> bool {
        true
    }

    fn signal_quality(&self) -> Option<u8> {
        Some(80)
    }

    fn can_reach(&self, peer_id: &NodeId) -> bool {
        self.reachable_peers.contains(peer_id)
    }
}

// =============================================================================
// Helpers
// =============================================================================

/// Build the standard dual-active config: Iroh primary, BLE alternate,
/// with collection routes for documents (QUIC), canned_msgs (BLE),
/// beacons (PACE).
fn simultaneous_config() -> TransportManagerConfig {
    let policy = TransportPolicy::new("tactical")
        .primary(vec!["iroh-primary"])
        .alternate(vec!["ble-primary"]);

    let routes = CollectionRouteTable::new()
        .with_collection(CollectionRouteConfig {
            collection: "documents".to_string(),
            route: CollectionTransportRoute::Fixed {
                transport_type: TransportType::Quic,
            },
            priority: MessagePriority::High,
        })
        .with_collection(CollectionRouteConfig {
            collection: "canned_msgs".to_string(),
            route: CollectionTransportRoute::Fixed {
                transport_type: TransportType::BluetoothLE,
            },
            priority: MessagePriority::Normal,
        })
        .with_collection(CollectionRouteConfig {
            collection: "beacons".to_string(),
            route: CollectionTransportRoute::Pace {
                policy_override: None,
            },
            priority: MessagePriority::Normal,
        });

    TransportManagerConfig {
        default_policy: Some(policy),
        collection_routes: routes,
        ..Default::default()
    }
}

// =============================================================================
// Tests
// =============================================================================

/// Main proof: real Iroh QUIC + mock BLE both active simultaneously
/// in the same TransportManager with correct routing.
#[tokio::test]
async fn test_iroh_and_ble_simultaneously_active() {
    let peer = NodeId::new("peer-1".to_string());
    let config = simultaneous_config();
    let mut manager = TransportManager::new(config);

    // --- Real Iroh transport ---
    let iroh_transport = Arc::new(IrohTransport::new().await.unwrap());
    let iroh_mesh = Arc::new(IrohMeshTransport::new(
        Arc::clone(&iroh_transport),
        PeerConfig::empty(),
    ));
    // Register the peer so can_reach() returns true
    iroh_mesh.register_peer(peer.clone(), iroh_transport.endpoint_id());

    // --- Mock BLE transport ---
    let mock_ble = Arc::new(MockBleTransport::new(vec![peer.clone()]));

    // Register legacy transports (for Fixed routes)
    manager.register(Arc::clone(&iroh_mesh) as Arc<dyn Transport>);
    manager.register(Arc::clone(&mock_ble) as Arc<dyn Transport>);

    // Register PACE instances
    manager.register_instance(
        TransportInstance::new(
            "iroh-primary",
            TransportType::Quic,
            TransportCapabilities::quic(),
        ),
        Arc::clone(&iroh_mesh) as Arc<dyn Transport>,
    );
    manager.register_instance(
        TransportInstance::new(
            "ble-primary",
            TransportType::BluetoothLE,
            TransportCapabilities::bluetooth_le(),
        ),
        Arc::clone(&mock_ble) as Arc<dyn Transport>,
    );

    // Start real Iroh transport (spawns QUIC accept loop)
    iroh_mesh.start().await.unwrap();

    // ---- Assert both available simultaneously ----
    let available = manager.available_instance_ids();
    assert_eq!(
        available.len(),
        2,
        "Expected 2 available instances, got {:?}",
        available
    );
    assert!(available.contains("iroh-primary"));
    assert!(available.contains("ble-primary"));

    // Assert real QUIC accept loop is running
    assert!(
        iroh_transport.is_accept_loop_running(),
        "Iroh QUIC accept loop should be running"
    );

    // ---- Assert routing ----
    let reqs = MessageRequirements::default();

    // documents → Fixed QUIC
    assert_eq!(
        manager.route_collection("documents", &peer, &reqs),
        RouteDecision::Transport(TransportType::Quic),
    );

    // canned_msgs → Fixed BLE
    assert_eq!(
        manager.route_collection("canned_msgs", &peer, &reqs),
        RouteDecision::Transport(TransportType::BluetoothLE),
    );

    // beacons → PACE selects iroh-primary (primary)
    assert_eq!(
        manager.route_collection("beacons", &peer, &reqs),
        RouteDecision::TransportInstance("iroh-primary".to_string()),
    );

    // ---- Stop Iroh, verify PACE fallback ----
    iroh_mesh.stop().await.unwrap();
    assert!(
        !iroh_transport.is_accept_loop_running(),
        "Iroh accept loop should be stopped"
    );

    // Iroh is_available() returns false → PACE falls back to BLE
    assert_eq!(
        manager.route_collection("beacons", &peer, &reqs),
        RouteDecision::TransportInstance("ble-primary".to_string()),
        "PACE should fall back to BLE when Iroh is stopped"
    );

    // BLE is still available
    assert!(mock_ble.is_available());
}

/// Iroh lifecycle (start/stop/restart) does not interfere with BLE availability.
#[tokio::test]
async fn test_iroh_lifecycle_doesnt_affect_ble() {
    let peer = NodeId::new("peer-1".to_string());
    let config = simultaneous_config();
    let manager = TransportManager::new(config);

    // Real Iroh
    let iroh_transport = Arc::new(IrohTransport::new().await.unwrap());
    let iroh_mesh = Arc::new(IrohMeshTransport::new(
        Arc::clone(&iroh_transport),
        PeerConfig::empty(),
    ));
    iroh_mesh.register_peer(peer.clone(), iroh_transport.endpoint_id());

    // Mock BLE
    let mock_ble = Arc::new(MockBleTransport::new(vec![peer.clone()]));

    // Register PACE instances
    manager.register_instance(
        TransportInstance::new(
            "iroh-primary",
            TransportType::Quic,
            TransportCapabilities::quic(),
        ),
        Arc::clone(&iroh_mesh) as Arc<dyn Transport>,
    );
    manager.register_instance(
        TransportInstance::new(
            "ble-primary",
            TransportType::BluetoothLE,
            TransportCapabilities::bluetooth_le(),
        ),
        Arc::clone(&mock_ble) as Arc<dyn Transport>,
    );

    // Phase 1: Start Iroh → both available
    iroh_mesh.start().await.unwrap();
    assert_eq!(manager.available_instance_ids().len(), 2);
    assert!(mock_ble.is_available(), "BLE should be available");

    // Phase 2: Stop Iroh → only BLE available
    iroh_mesh.stop().await.unwrap();
    let available = manager.available_instance_ids();
    assert_eq!(
        available.len(),
        1,
        "Only BLE should be available after Iroh stops"
    );
    assert!(available.contains("ble-primary"));
    assert!(
        mock_ble.is_available(),
        "BLE must remain available after Iroh stops"
    );

    // Phase 3: Restart Iroh → both available again
    iroh_mesh.start().await.unwrap();
    assert_eq!(
        manager.available_instance_ids().len(),
        2,
        "Both should be available after Iroh restarts"
    );
    assert!(
        mock_ble.is_available(),
        "BLE still available after Iroh restarts"
    );

    // Cleanup
    iroh_mesh.stop().await.unwrap();
}

/// Routing decisions for multiple collections in rapid succession all resolve
/// to the correct transport — proves no interference between routes.
#[tokio::test]
async fn test_simultaneous_routing_decisions() {
    let peer = NodeId::new("peer-1".to_string());
    let config = simultaneous_config();
    let mut manager = TransportManager::new(config);

    // Real Iroh
    let iroh_transport = Arc::new(IrohTransport::new().await.unwrap());
    let iroh_mesh = Arc::new(IrohMeshTransport::new(
        Arc::clone(&iroh_transport),
        PeerConfig::empty(),
    ));
    iroh_mesh.register_peer(peer.clone(), iroh_transport.endpoint_id());

    // Mock BLE
    let mock_ble = Arc::new(MockBleTransport::new(vec![peer.clone()]));

    // Register legacy + PACE
    manager.register(Arc::clone(&iroh_mesh) as Arc<dyn Transport>);
    manager.register(Arc::clone(&mock_ble) as Arc<dyn Transport>);
    manager.register_instance(
        TransportInstance::new(
            "iroh-primary",
            TransportType::Quic,
            TransportCapabilities::quic(),
        ),
        Arc::clone(&iroh_mesh) as Arc<dyn Transport>,
    );
    manager.register_instance(
        TransportInstance::new(
            "ble-primary",
            TransportType::BluetoothLE,
            TransportCapabilities::bluetooth_le(),
        ),
        Arc::clone(&mock_ble) as Arc<dyn Transport>,
    );

    // Start Iroh
    iroh_mesh.start().await.unwrap();

    let reqs = MessageRequirements::default();

    // Rapid-fire routing decisions — each should go to the right transport
    let collections_and_expected = [
        ("documents", RouteDecision::Transport(TransportType::Quic)),
        (
            "canned_msgs",
            RouteDecision::Transport(TransportType::BluetoothLE),
        ),
        (
            "beacons",
            RouteDecision::TransportInstance("iroh-primary".to_string()),
        ),
        ("documents", RouteDecision::Transport(TransportType::Quic)),
        (
            "canned_msgs",
            RouteDecision::Transport(TransportType::BluetoothLE),
        ),
        (
            "beacons",
            RouteDecision::TransportInstance("iroh-primary".to_string()),
        ),
    ];

    for (collection, expected) in &collections_and_expected {
        let decision = manager.route_collection(collection, &peer, &reqs);
        assert_eq!(
            &decision, expected,
            "Collection '{}' routed to {:?}, expected {:?}",
            collection, decision, expected
        );
    }

    // Cleanup
    iroh_mesh.stop().await.unwrap();
}