communitas-core 0.1.22

Core business logic for Communitas - PQC collaboration with virtual disks
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
 * Enhanced TestHarness for Communitas Integration Tests
 *
 * Provides a controlled testing environment with real QUIC transport,
 * multi-node scenarios, network chaos simulation, and full networking stack.
 *
 * Capabilities:
 * - Real QUIC connections over ephemeral UDP ports
 * - Multi-node mesh/line/star topologies
 * - Network chaos: partition, latency, jitter, packet loss
 * - Presence, FOAF, gossip, and sync integration
 */
use crate::core_context::CoreContext;
use anyhow::{Context as _, Result};
use saorsa_gossip_groups::GroupContext;
use saorsa_gossip_presence::PresenceManager;
use saorsa_gossip_transport::{QuicTransport, TransportConfig};
use saorsa_gossip_types::{PeerId, TopicId};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tempfile::TempDir;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// Network link policy for chaos engineering
#[derive(Debug, Clone)]
pub struct LinkPolicy {
    /// Whether nodes can communicate
    pub connected: bool,
    /// Base latency added to communication
    pub latency: Duration,
    /// Random jitter added (0 to jitter)
    pub jitter: Duration,
    /// Packet loss probability (0.0 to 1.0)
    pub loss: f32,
}

impl Default for LinkPolicy {
    fn default() -> Self {
        Self {
            connected: true,
            latency: Duration::ZERO,
            jitter: Duration::ZERO,
            loss: 0.0,
        }
    }
}

impl LinkPolicy {
    /// Perfect link (no latency, no loss)
    pub fn perfect() -> Self {
        Self::default()
    }

    /// Disconnected link
    pub fn disconnected() -> Self {
        Self {
            connected: false,
            ..Default::default()
        }
    }

    /// Lossy link with specified packet loss rate
    pub fn lossy(loss: f32) -> Self {
        Self {
            loss: loss.clamp(0.0, 1.0),
            ..Default::default()
        }
    }

    /// High-latency link
    pub fn slow(latency_ms: u64) -> Self {
        Self {
            latency: Duration::from_millis(latency_ms),
            jitter: Duration::from_millis(latency_ms / 10),
            ..Default::default()
        }
    }

    /// Should this packet be dropped?
    pub fn should_drop(&self) -> bool {
        !self.connected || (self.loss > 0.0 && rand::random::<f32>() < self.loss)
    }

    /// Get effective delay for this packet
    pub fn effective_delay(&self) -> Duration {
        if self.jitter > Duration::ZERO {
            let jitter_ms = rand::random::<u64>() % self.jitter.as_millis() as u64;
            self.latency + Duration::from_millis(jitter_ms)
        } else {
            self.latency
        }
    }
}

/// Test node with full networking stack
pub struct TestNode {
    pub id: usize,
    pub four_words: String,
    pub peer_id: PeerId,
    pub port: u16,
    pub addr: SocketAddr,
    pub temp_dir: TempDir,
    pub core: Option<Arc<CoreContext>>,
    pub presence: Option<Arc<RwLock<PresenceManager>>>,
    pub groups: Arc<RwLock<HashMap<TopicId, GroupContext>>>,
    pub transport: Arc<QuicTransport>,
}

impl TestNode {
    /// Create a new test node with unique identity and ephemeral port
    pub async fn new(id: usize) -> Result<Self> {
        let temp_dir = TempDir::new().context("failed to create temp dir")?;

        // Generate test identity
        let four_words = format!("test-node-{:04x}-peer", id);
        let peer_id = PeerId::new([(id % 256) as u8; 32]);

        // Create transport with default config
        let config = TransportConfig::default();
        let transport = Arc::new(QuicTransport::new(config));

        // Use ephemeral port (assigned by OS)
        // Note: Actual port discovery will need to be implemented in QuicTransport
        let port = 10000 + (id as u16); // Temp: Use deterministic port for testing
        let addr: SocketAddr = format!("127.0.0.1:{}", port)
            .parse()
            .context("failed to parse address")?;

        debug!("TestNode {} created on port {}", id, port);

        Ok(TestNode {
            id,
            four_words,
            peer_id,
            port,
            addr,
            temp_dir,
            core: None,
            presence: None,
            groups: Arc::new(RwLock::new(HashMap::new())),
            transport,
        })
    }

    /// Initialize core context (full stack)
    pub async fn initialize_core(&mut self) -> Result<()> {
        let groups_map = self.groups.clone();
        let presence_mgr =
            PresenceManager::new(self.peer_id, self.transport.clone(), groups_map.clone());
        self.presence = Some(Arc::new(RwLock::new(presence_mgr)));

        // TODO: Initialize CoreContext when we have the constructor ready
        // For now, mark as initialized
        info!("TestNode {} core initialized", self.id);
        Ok(())
    }

    /// Join a topic/group
    pub async fn join_group(&self, topic_id: TopicId, group_name: &str) -> Result<()> {
        let group_ctx = GroupContext::from_entity(group_name);

        let mut groups = self.groups.write().await;
        groups.insert(topic_id, group_ctx);

        info!("TestNode {} joined group {}", self.id, group_name);
        Ok(())
    }

    /// Get local address for bootstrap
    pub fn bootstrap_addr(&self) -> String {
        format!("127.0.0.1:{}", self.port)
    }

    /// Shutdown node
    pub async fn shutdown(self) -> Result<()> {
        info!("TestNode {} shutting down", self.id);
        // Cleanup happens automatically via Drop
        Ok(())
    }
}

/// Network simulator with chaos control
pub struct NetworkSimulator {
    pub nodes: HashMap<usize, Arc<RwLock<TestNode>>>,
    pub policies: Arc<RwLock<HashMap<(usize, usize), LinkPolicy>>>,
}

impl Default for NetworkSimulator {
    fn default() -> Self {
        Self::new()
    }
}

impl NetworkSimulator {
    pub fn new() -> Self {
        NetworkSimulator {
            nodes: HashMap::new(),
            policies: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub fn add_node(&mut self, node: TestNode) -> usize {
        let id = node.id;
        self.nodes.insert(id, Arc::new(RwLock::new(node)));
        id
    }

    /// Set link policy between two nodes
    pub async fn set_policy(&self, a: usize, b: usize, policy: LinkPolicy) {
        let mut policies = self.policies.write().await;
        policies.insert((a.min(b), a.max(b)), policy.clone());
        debug!("Link {}->{} policy: {:?}", a, b, policy);
    }

    /// Get link policy between two nodes
    pub async fn get_policy(&self, a: usize, b: usize) -> LinkPolicy {
        let policies = self.policies.read().await;
        policies
            .get(&(a.min(b), a.max(b)))
            .cloned()
            .unwrap_or_default()
    }

    /// Check if nodes are connected
    pub async fn are_connected(&self, a: usize, b: usize) -> bool {
        self.get_policy(a, b).await.connected
    }

    /// Get node by ID
    pub async fn get_node(&self, id: usize) -> Option<Arc<RwLock<TestNode>>> {
        self.nodes.get(&id).cloned()
    }

    /// Connect two nodes (set policy to perfect)
    pub async fn connect(&self, a: usize, b: usize) {
        self.set_policy(a, b, LinkPolicy::perfect()).await;
    }

    /// Disconnect two nodes
    pub async fn disconnect(&self, a: usize, b: usize) {
        self.set_policy(a, b, LinkPolicy::disconnected()).await;
    }

    /// Set latency between nodes
    pub async fn set_latency(&self, a: usize, b: usize, latency_ms: u64) {
        self.set_policy(a, b, LinkPolicy::slow(latency_ms)).await;
    }

    /// Set packet loss between nodes
    pub async fn set_loss(&self, a: usize, b: usize, loss: f32) {
        self.set_policy(a, b, LinkPolicy::lossy(loss)).await;
    }
}

/// Main test harness for integration testing
pub struct TestHarness {
    pub network: Arc<RwLock<NetworkSimulator>>,
    pub temp_dir: TempDir,
}

impl TestHarness {
    /// Create test harness with N nodes
    pub async fn new(node_count: usize) -> Result<Self> {
        let temp_dir = TempDir::new().context("failed to create harness temp dir")?;
        let network = Arc::new(RwLock::new(NetworkSimulator::new()));

        let harness = TestHarness { network, temp_dir };

        // Create nodes
        for i in 0..node_count {
            let mut node = TestNode::new(i).await?;
            node.initialize_core().await?;
            harness.network.write().await.add_node(node);
        }

        info!("TestHarness created with {} nodes", node_count);
        Ok(harness)
    }

    /// Setup mesh topology (all nodes connected to all)
    pub async fn mesh(&self) -> Result<()> {
        let node_count = self.network.read().await.nodes.len();
        for i in 0..node_count {
            for j in (i + 1)..node_count {
                self.network.read().await.connect(i, j).await;
            }
        }
        info!("Mesh topology configured");
        Ok(())
    }

    /// Setup line topology (0-1-2-3-...)
    pub async fn line(&self) -> Result<()> {
        let node_count = self.network.read().await.nodes.len();

        // First disconnect all nodes from each other
        for i in 0..node_count {
            for j in (i + 1)..node_count {
                self.network.read().await.disconnect(i, j).await;
            }
        }

        // Then connect only adjacent nodes
        for i in 0..node_count.saturating_sub(1) {
            self.network.read().await.connect(i, i + 1).await;
        }
        info!("Line topology configured");
        Ok(())
    }

    /// Setup star topology (hub connected to all)
    pub async fn star(&self, hub: usize) -> Result<()> {
        let node_count = self.network.read().await.nodes.len();

        // First disconnect all nodes from each other
        for i in 0..node_count {
            for j in (i + 1)..node_count {
                self.network.read().await.disconnect(i, j).await;
            }
        }

        // Then connect hub to all spokes
        for i in 0..node_count {
            if i != hub {
                self.network.read().await.connect(hub, i).await;
            }
        }
        info!("Star topology configured with hub {}", hub);
        Ok(())
    }

    /// Partition network into two groups
    pub async fn partition(&self, group_a: &[usize], group_b: &[usize]) -> Result<()> {
        for &a in group_a {
            for &b in group_b {
                self.network.read().await.disconnect(a, b).await;
            }
        }
        info!("Network partitioned: {:?} | {:?}", group_a, group_b);
        Ok(())
    }

    /// Heal partition (reconnect all)
    pub async fn heal(&self) -> Result<()> {
        let node_count = self.network.read().await.nodes.len();
        for i in 0..node_count {
            for j in (i + 1)..node_count {
                self.network.read().await.connect(i, j).await;
            }
        }
        info!("Network healed");
        Ok(())
    }

    /// Set latency between two nodes
    pub async fn set_latency(&self, a: usize, b: usize, latency_ms: u64) {
        self.network
            .read()
            .await
            .set_latency(a, b, latency_ms)
            .await;
    }

    /// Set packet loss between two nodes
    pub async fn set_loss(&self, a: usize, b: usize, loss: f32) {
        self.network.read().await.set_loss(a, b, loss).await;
    }

    /// Wait until at least N nodes are connected
    pub async fn wait_until_connected(&self, count: usize, timeout: Duration) -> Result<()> {
        let start = std::time::Instant::now();

        loop {
            let network = self.network.read().await;
            let node_count = network.nodes.len();

            // Count connected pairs
            let mut connected = 0;
            for i in 0..node_count {
                for j in (i + 1)..node_count {
                    if network.are_connected(i, j).await {
                        connected += 1;
                    }
                }
            }

            if connected >= count {
                info!("Connected threshold reached: {}/{}", connected, count);
                return Ok(());
            }

            if start.elapsed() > timeout {
                warn!("Timeout waiting for connections: {}/{}", connected, count);
                return Err(anyhow::anyhow!(
                    "Timeout waiting for {} connections (got {})",
                    count,
                    connected
                ));
            }

            tokio::time::sleep(Duration::from_millis(100)).await;
        }
    }

    /// Get node by ID
    pub async fn get_node(&self, id: usize) -> Option<Arc<RwLock<TestNode>>> {
        self.network.read().await.get_node(id).await
    }

    /// Get bootstrap addresses for all nodes
    pub async fn get_bootstrap_addrs(&self) -> Vec<String> {
        let network = self.network.read().await;
        let mut addrs = Vec::new();
        for node_lock in network.nodes.values() {
            let node = node_lock.read().await;
            addrs.push(node.bootstrap_addr());
        }
        addrs
    }

    /// Cleanup harness
    pub async fn cleanup(self) -> Result<()> {
        info!("TestHarness cleanup started");

        // Shutdown all nodes
        let network = Arc::try_unwrap(self.network)
            .map_err(|_| anyhow::anyhow!("Failed to unwrap network"))?
            .into_inner();

        for (id, node_lock) in network.nodes {
            let node = Arc::try_unwrap(node_lock)
                .map_err(|_| anyhow::anyhow!("Failed to unwrap node {}", id))?
                .into_inner();
            node.shutdown().await?;
        }

        info!("TestHarness cleanup complete");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_harness_creation() {
        let harness = TestHarness::new(3).await.expect("harness creation failed");
        let network = harness.network.read().await;
        assert_eq!(network.nodes.len(), 3);
        drop(network);
        harness.cleanup().await.expect("cleanup failed");
    }

    #[tokio::test]
    async fn test_mesh_topology() {
        let harness = TestHarness::new(4).await.expect("harness creation failed");
        harness.mesh().await.expect("mesh setup failed");

        let network = harness.network.read().await;
        // In a 4-node mesh, we should have 6 connections (n*(n-1)/2)
        for i in 0..4 {
            for j in (i + 1)..4 {
                assert!(
                    network.are_connected(i, j).await,
                    "Nodes {} and {} should be connected",
                    i,
                    j
                );
            }
        }
    }

    #[tokio::test]
    async fn test_partition_and_heal() {
        let harness = TestHarness::new(4).await.expect("harness creation failed");
        harness.mesh().await.expect("mesh failed");

        // Partition into [0,1] and [2,3]
        harness
            .partition(&[0, 1], &[2, 3])
            .await
            .expect("partition failed");

        let network = harness.network.read().await;
        // Within partitions should be connected
        assert!(network.are_connected(0, 1).await);
        assert!(network.are_connected(2, 3).await);

        // Across partitions should be disconnected
        assert!(!network.are_connected(0, 2).await);
        assert!(!network.are_connected(0, 3).await);
        assert!(!network.are_connected(1, 2).await);
        assert!(!network.are_connected(1, 3).await);
        drop(network);

        // Heal
        harness.heal().await.expect("heal failed");

        let network = harness.network.read().await;
        // All should be connected again
        for i in 0..4 {
            for j in (i + 1)..4 {
                assert!(network.are_connected(i, j).await);
            }
        }
    }

    #[tokio::test]
    async fn test_link_policies() {
        let harness = TestHarness::new(2).await.expect("harness creation failed");

        // Test latency
        harness.set_latency(0, 1, 100).await;
        let policy = harness.network.read().await.get_policy(0, 1).await;
        assert_eq!(policy.latency, Duration::from_millis(100));

        // Test packet loss
        harness.set_loss(0, 1, 0.3).await;
        let policy = harness.network.read().await.get_policy(0, 1).await;
        assert_eq!(policy.loss, 0.3);
    }

    #[tokio::test]
    async fn test_star_topology() {
        let harness = TestHarness::new(5).await.expect("harness creation failed");
        harness.star(0).await.expect("star failed");

        let network = harness.network.read().await;
        // Hub (0) connected to all
        for i in 1..5 {
            assert!(network.are_connected(0, i).await);
        }

        // Spokes not connected to each other
        assert!(!network.are_connected(1, 2).await);
        assert!(!network.are_connected(2, 3).await);
    }

    #[tokio::test]
    async fn test_node_initialization() {
        let node = TestNode::new(42).await.expect("node creation failed");
        assert_eq!(node.id, 42);
        assert!(node.port > 0, "Should have ephemeral port");
        assert!(node.four_words.contains("test-node"));

        node.shutdown().await.expect("shutdown failed");
    }
}