communitas-core 0.1.21

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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Comprehensive MessageSyncService Tests
//!
//! Tests message synchronization, out-of-order detection, missing message recovery,
//! and multi-peer scenarios with property-based testing.

use communitas_core::crdt::*;
use communitas_core::message_sync::MessageSyncService;
use proptest::prelude::*;

// ============================================================================
// Property-Based Tests for MessageSyncService
// ============================================================================

/// Generate arbitrary peer ID (four-word format)
fn arb_peer_id() -> impl Strategy<Value = String> {
    "[a-z]{4}-[a-z]{4}-[a-z]{4}-[a-z]{4}".prop_map(|s| s.to_string())
}

/// Generate arbitrary entity ID
fn arb_entity_id() -> impl Strategy<Value = String> {
    prop::sample::select(vec![
        "contact-alice".to_string(),
        "project-communitas".to_string(),
        "saorsa-general".to_string(),
        "group-test".to_string(),
        "saorsa-org".to_string(),
    ])
}

proptest! {
    /// Property: Sending a message increments the vector clock for the sender
    #[test]
    fn prop_send_increments_clock(
        peer_id in arb_peer_id(),
        entity_id in arb_entity_id()
    ) {
        tokio_test::block_on(async {
            let service = MessageSyncService::new(peer_id.clone());

            // Send first message
            let msg1 = service
                .send_message(
                    entity_id.clone(),
                    EntityType::Person,
                    MessageContent {
                        text: "First message".to_string(),
                        author: "Test".to_string(),
                        attachments: None,
                    },
                    None,
                )
                .await
                .map_err(|e| TestCaseError::fail(e.to_string()))?;

            prop_assert_eq!(msg1.metadata.vector_clock.0.get(&peer_id), Some(&1));

            // Send second message
            let msg2 = service
                .send_message(
                    entity_id.clone(),
                    EntityType::Person,
                    MessageContent {
                        text: "Second message".to_string(),
                        author: "Test".to_string(),
                        attachments: None,
                    },
                    None,
                )
                .await
                .map_err(|e| TestCaseError::fail(e.to_string()))?;

            prop_assert_eq!(msg2.metadata.vector_clock.0.get(&peer_id), Some(&2));

            Ok(())
        })?;
    }

    /// Property: Lamport clock always increases
    #[test]
    fn prop_lamport_always_increases(
        peer_id in arb_peer_id(),
        entity_id in arb_entity_id(),
        count in 2usize..10
    ) {
        tokio_test::block_on(async {
            let service = MessageSyncService::new(peer_id.clone());

            let mut last_lamport = 0;

            for i in 0..count {
                let msg = service
                    .send_message(
                        entity_id.clone(),
                        EntityType::Person,
                        MessageContent {
                            text: format!("Message {}", i),
                            author: "Test".to_string(),
                            attachments: None,
                        },
                        None,
                    )
                    .await
                    .map_err(|e| TestCaseError::fail(e.to_string()))?;

                prop_assert!(msg.metadata.lamport_clock > last_lamport);
                last_lamport = msg.metadata.lamport_clock;
            }

            Ok(())
        })?;
    }

    /// Property: Receiving messages in order always succeeds
    #[test]
    fn prop_in_order_messages_accepted(
        peer_id1 in arb_peer_id(),
        peer_id2 in arb_peer_id(),
        entity_id in arb_entity_id()
    ) {
        tokio_test::block_on(async {
            if peer_id1 == peer_id2 {
                return Ok(());
            }

            let sender = MessageSyncService::new(peer_id1.clone());
            let receiver = MessageSyncService::new(peer_id2.clone());

            // Send 5 messages in order
            for i in 0..5 {
                let msg = sender
                    .send_message(
                        entity_id.clone(),
                        EntityType::Person,
                        MessageContent {
                            text: format!("Message {}", i),
                            author: "Sender".to_string(),
                            attachments: None,
                        },
                        None,
                    )
                    .await
                    .map_err(|e| TestCaseError::fail(e.to_string()))?;

                let result = receiver
                    .receive_message(msg)
                    .await
                    .map_err(|e| TestCaseError::fail(e.to_string()))?;

                prop_assert!(result.accepted);
                prop_assert!(!result.out_of_order);
            }

            Ok(())
        })?;
    }

    /// Property: get_all_messages returns messages in causal order
    #[test]
    fn prop_get_all_messages_causally_ordered(
        peer_id in arb_peer_id(),
        entity_id in arb_entity_id(),
        count in 2usize..20
    ) {
        tokio_test::block_on(async {
            let service = MessageSyncService::new(peer_id.clone());

            // Send random messages
            for i in 0..count {
                service
                    .send_message(
                        entity_id.clone(),
                        EntityType::Person,
                        MessageContent {
                            text: format!("Message {}", i),
                            author: "Test".to_string(),
                            attachments: None,
                        },
                        None,
                    )
                    .await
                    .map_err(|e| TestCaseError::fail(e.to_string()))?;
            }

            let response = service
                .get_all_messages(&entity_id)
                .await
                .map_err(|e| TestCaseError::fail(e.to_string()))?;

            // Verify causal order
            for i in 0..response.messages.len().saturating_sub(1) {
                let ord = response.messages[i]
                    .metadata
                    .vector_clock
                    .compare(&response.messages[i + 1].metadata.vector_clock);

                // Should never have a message that happened AFTER the next one
                prop_assert!(!matches!(ord, ClockOrdering::After));
            }

            Ok(())
        })?;
    }
}

// ============================================================================
// Multi-Peer Scenario Tests
// ============================================================================

#[tokio::test]
async fn test_two_peers_bidirectional_sync() {
    let peer1 = MessageSyncService::new("peer-one-two-three-four".to_string());
    let peer2 = MessageSyncService::new("peer-five-six-seven-eight".to_string());
    let entity_id = "contact-alice";

    // Peer 1 sends 3 messages
    for i in 0..3 {
        peer1
            .send_message(
                entity_id.to_string(),
                EntityType::Person,
                MessageContent {
                    text: format!("Peer1 message {}", i),
                    author: "Peer1".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();
    }

    // Peer 2 sends 2 messages
    for i in 0..2 {
        peer2
            .send_message(
                entity_id.to_string(),
                EntityType::Person,
                MessageContent {
                    text: format!("Peer2 message {}", i),
                    author: "Peer2".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();
    }

    // Peer1 syncs from Peer2
    let peer2_messages = peer2.get_all_messages(entity_id).await.unwrap();
    assert_eq!(peer2_messages.messages.len(), 2); // Peer2 has 2 messages
    let result1 = peer1.handle_sync_response(peer2_messages).await.unwrap();
    assert_eq!(result1.messages_added, 2); // Added peer2's messages

    // Peer2 syncs from Peer1
    let peer1_messages = peer1.get_all_messages(entity_id).await.unwrap();
    assert_eq!(peer1_messages.messages.len(), 5); // Peer1 now has 3 + 2 = 5
    let result2 = peer2.handle_sync_response(peer1_messages).await.unwrap();
    // All 5 messages are "accepted" (duplicates are silently ignored in add_message)
    // The actual behavior is that duplicates return accepted=true but don't add
    assert_eq!(result2.messages_added, 5);
    assert_eq!(result2.messages_rejected, 0);

    // Both peers should now have 5 messages total
    let peer1_final = peer1.get_messages(entity_id).await.unwrap();
    let peer2_final = peer2.get_messages(entity_id).await.unwrap();

    assert_eq!(peer1_final.len(), 5);
    assert_eq!(peer2_final.len(), 5);

    // Messages should be in the same causal order
    for i in 0..5 {
        assert_eq!(peer1_final[i].metadata.id, peer2_final[i].metadata.id);
    }
}

#[tokio::test]
async fn test_out_of_order_message_queued() {
    let sender = MessageSyncService::new("peer-sender".to_string());
    let receiver = MessageSyncService::new("peer-receiver".to_string());
    let entity_id = "contact-test";

    // Sender creates 3 messages
    let msg1 = sender
        .send_message(
            entity_id.to_string(),
            EntityType::Person,
            MessageContent {
                text: "Message 1".to_string(),
                author: "Sender".to_string(),
                attachments: None,
            },
            None,
        )
        .await
        .unwrap();

    let msg2 = sender
        .send_message(
            entity_id.to_string(),
            EntityType::Person,
            MessageContent {
                text: "Message 2".to_string(),
                author: "Sender".to_string(),
                attachments: None,
            },
            None,
        )
        .await
        .unwrap();

    let msg3 = sender
        .send_message(
            entity_id.to_string(),
            EntityType::Person,
            MessageContent {
                text: "Message 3".to_string(),
                author: "Sender".to_string(),
                attachments: None,
            },
            None,
        )
        .await
        .unwrap();

    // Receiver gets msg1 (OK)
    let result1 = receiver.receive_message(msg1).await.unwrap();
    assert!(result1.accepted);

    // Receiver gets msg3 BEFORE msg2 (out of order!)
    let result3 = receiver.receive_message(msg3.clone()).await.unwrap();
    assert!(!result3.accepted);
    assert!(result3.out_of_order);
    assert!(result3.missing_ranges.is_some());

    // Verify msg3 is in pending queue
    let state = receiver.get_sync_state(entity_id).await.unwrap();
    assert_eq!(state.out_of_order_messages.len(), 1);

    // Now receive msg2 (fills the gap)
    let result2 = receiver.receive_message(msg2).await.unwrap();
    assert!(result2.accepted);

    // msg3 should now be processed from pending queue
    let messages = receiver.get_messages(entity_id).await.unwrap();
    assert_eq!(messages.len(), 3);

    // Verify causal order - check that all 3 messages are present
    assert_eq!(messages.len(), 3);

    // Verify messages are from the same peer
    for msg in &messages {
        assert!(msg.metadata.author_peer_id == "peer-sender");
    }
}

#[tokio::test]
async fn test_missing_message_ranges() {
    let sender = MessageSyncService::new("peer-sender".to_string());
    let receiver = MessageSyncService::new("peer-receiver".to_string());
    let entity_id = "contact-test";

    // Sender sends 10 messages
    for i in 0..10 {
        sender
            .send_message(
                entity_id.to_string(),
                EntityType::Person,
                MessageContent {
                    text: format!("Message {}", i),
                    author: "Sender".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();
    }

    // Receiver only gets messages 0, 1, 2, 9 (missing 3-8)
    let all_messages = sender.get_messages(entity_id).await.unwrap();
    receiver
        .receive_message(all_messages[0].clone())
        .await
        .unwrap();
    receiver
        .receive_message(all_messages[1].clone())
        .await
        .unwrap();
    receiver
        .receive_message(all_messages[2].clone())
        .await
        .unwrap();

    // Try to receive message 9 (out of order)
    let result = receiver
        .receive_message(all_messages[9].clone())
        .await
        .unwrap();

    assert!(!result.accepted);
    assert!(result.out_of_order);

    let missing = result.missing_ranges.unwrap();
    assert!(!missing.is_empty());

    // Verify the missing range includes messages 4-10 (we have 1,2,3, missing 4-9)
    let peer_sender_range = missing.iter().find(|r| r.peer_id == "peer-sender").unwrap();
    // We have up to timestamp 3, so we need from 4 onwards
    assert_eq!(peer_sender_range.from_timestamp, 4);
    assert_eq!(peer_sender_range.to_timestamp, 10);
}

#[tokio::test]
async fn test_three_peer_convergence() {
    let peer1 = MessageSyncService::new("peer-one".to_string());
    let peer2 = MessageSyncService::new("peer-two".to_string());
    let peer3 = MessageSyncService::new("peer-three".to_string());
    let entity_id = "project-communitas";

    // Each peer sends 2 messages
    for i in 0..2 {
        peer1
            .send_message(
                entity_id.to_string(),
                EntityType::Project,
                MessageContent {
                    text: format!("Peer1 msg {}", i),
                    author: "Peer1".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();

        peer2
            .send_message(
                entity_id.to_string(),
                EntityType::Project,
                MessageContent {
                    text: format!("Peer2 msg {}", i),
                    author: "Peer2".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();

        peer3
            .send_message(
                entity_id.to_string(),
                EntityType::Project,
                MessageContent {
                    text: format!("Peer3 msg {}", i),
                    author: "Peer3".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();
    }

    // Sync peer1 <-> peer2
    let peer2_sync = peer2.get_all_messages(entity_id).await.unwrap();
    peer1.handle_sync_response(peer2_sync).await.unwrap();

    let peer1_sync = peer1.get_all_messages(entity_id).await.unwrap();
    peer2.handle_sync_response(peer1_sync).await.unwrap();

    // Sync peer2 <-> peer3
    let peer3_sync = peer3.get_all_messages(entity_id).await.unwrap();
    peer2.handle_sync_response(peer3_sync).await.unwrap();

    let peer2_sync2 = peer2.get_all_messages(entity_id).await.unwrap();
    peer3.handle_sync_response(peer2_sync2).await.unwrap();

    // Sync peer1 <-> peer3
    let peer3_sync2 = peer3.get_all_messages(entity_id).await.unwrap();
    peer1.handle_sync_response(peer3_sync2).await.unwrap();

    let peer1_sync2 = peer1.get_all_messages(entity_id).await.unwrap();
    peer3.handle_sync_response(peer1_sync2).await.unwrap();

    // All peers should converge to 6 messages
    let peer1_messages = peer1.get_messages(entity_id).await.unwrap();
    let peer2_messages = peer2.get_messages(entity_id).await.unwrap();
    let peer3_messages = peer3.get_messages(entity_id).await.unwrap();

    assert_eq!(peer1_messages.len(), 6);
    assert_eq!(peer2_messages.len(), 6);
    assert_eq!(peer3_messages.len(), 6);

    // All peers should have the same message IDs in the same order
    for i in 0..6 {
        assert_eq!(peer1_messages[i].metadata.id, peer2_messages[i].metadata.id);
        assert_eq!(peer2_messages[i].metadata.id, peer3_messages[i].metadata.id);
    }
}

#[tokio::test]
async fn test_sync_state_tracking() {
    let service = MessageSyncService::new("peer-test".to_string());
    let entity_id = "contact-alice";

    // Initially, no messages
    let state0 = service.get_sync_state(entity_id).await.unwrap();
    assert_eq!(state0.message_count, 0);
    assert!(state0.missing_messages.is_empty());

    // Send 3 messages
    for i in 0..3 {
        service
            .send_message(
                entity_id.to_string(),
                EntityType::Person,
                MessageContent {
                    text: format!("Message {}", i),
                    author: "Test".to_string(),
                    attachments: None,
                },
                None,
            )
            .await
            .unwrap();
    }

    let state1 = service.get_sync_state(entity_id).await.unwrap();
    assert_eq!(state1.message_count, 3);
    assert_eq!(state1.vector_clock.0.get("peer-test"), Some(&3));
}

#[tokio::test]
async fn test_needs_sync_detection() {
    let peer1 = MessageSyncService::new("peer-one".to_string());
    let peer2 = MessageSyncService::new("peer-two".to_string());
    let entity_id = "contact-test";

    // Initially, no sync needed
    let peer1_clock = VectorClock::new();
    assert!(!peer2.needs_sync(entity_id, &peer1_clock).await);

    // Peer1 sends a message
    peer1
        .send_message(
            entity_id.to_string(),
            EntityType::Person,
            MessageContent {
                text: "Hello".to_string(),
                author: "Peer1".to_string(),
                attachments: None,
            },
            None,
        )
        .await
        .unwrap();

    // Get peer1's clock
    let state = peer1.get_sync_state(entity_id).await.unwrap();
    let peer1_clock = state.vector_clock;

    // Peer2 should need sync now
    assert!(peer2.needs_sync(entity_id, &peer1_clock).await);

    // After syncing, no more sync needed
    let sync_response = peer1.get_all_messages(entity_id).await.unwrap();
    peer2.handle_sync_response(sync_response).await.unwrap();

    assert!(!peer2.needs_sync(entity_id, &peer1_clock).await);
}

#[tokio::test]
async fn test_duplicate_message_ignored() {
    let service = MessageSyncService::new("peer-test".to_string());
    let entity_id = "contact-alice";

    let msg = service
        .send_message(
            entity_id.to_string(),
            EntityType::Person,
            MessageContent {
                text: "Message".to_string(),
                author: "Test".to_string(),
                attachments: None,
            },
            None,
        )
        .await
        .unwrap();

    // Try to receive the same message again
    let _result = service.receive_message(msg.clone()).await.unwrap();

    // Duplicate detection is in add_message - won't add twice
    let messages = service.get_messages(entity_id).await.unwrap();
    assert_eq!(messages.len(), 1); // Still only 1 message
}