upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/proactive_sync_dynamic.rs
blob: 2d3232ffa4d09f4ebd4d54231bd03eae10d91af4 (plain)
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! GRASP-02 Phase 4: Dynamic Subscription Integration Tests
//!
//! Tests verify dynamic subscription management:
//! - New announcement triggers Layer 2 subscription
//! - New PR/Issue triggers Layer 3 subscription
//! - Subscription count tracking per connection
//! - Consolidation at filter count > 150
//! - No duplicate subscriptions
//!
//! # Running Tests
//!
//! ```bash
//! cargo test --test proactive_sync_dynamic
//! cargo test --test proactive_sync_dynamic -- --nocapture
//! ```

use std::collections::HashSet;

use ngit_grasp::sync::SubscriptionManager;
use nostr_sdk::prelude::*;

/// Kind 30617 - Repository Announcement (NIP-34)
const KIND_REPOSITORY_ANNOUNCEMENT: u16 = 30617;

/// Kind 30618 - Maintainer List (NIP-34)
const KIND_MAINTAINER_LIST: u16 = 30618;

/// Maximum filters before consolidation (from spec)
const CONSOLIDATION_THRESHOLD: usize = 150;

/// Helper to create a test announcement event
fn create_test_announcement(keys: &Keys, identifier: &str) -> Event {
    let tags = vec![
        Tag::identifier(identifier),
        Tag::custom(
            TagKind::custom("clone"),
            vec![format!("http://test.example.com/{}", identifier)],
        ),
        Tag::custom(
            TagKind::custom("relays"),
            vec!["ws://test.example.com".to_string()],
        ),
    ];

    EventBuilder::new(Kind::Custom(KIND_REPOSITORY_ANNOUNCEMENT), "Test repo")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

/// Helper to create a test maintainer list event
fn create_test_maintainer_list(keys: &Keys, identifier: &str) -> Event {
    let tags = vec![
        Tag::identifier(identifier),
        Tag::custom(
            TagKind::custom("relays"),
            vec!["ws://test.example.com".to_string()],
        ),
    ];

    EventBuilder::new(Kind::Custom(KIND_MAINTAINER_LIST), "Maintainer list")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

/// Helper to create a test PR event (kind 1617)
fn create_test_pr_event(keys: &Keys, repo_coord: &str) -> Event {
    let tags = vec![Tag::custom(
        TagKind::custom("a"),
        vec![repo_coord.to_string()],
    )];

    EventBuilder::new(Kind::Custom(1617), "Test patch proposal")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

/// Helper to create a test PR event (kind 1618)
fn create_test_pr_1618_event(keys: &Keys, repo_coord: &str) -> Event {
    let tags = vec![Tag::custom(
        TagKind::custom("a"),
        vec![repo_coord.to_string()],
    )];

    EventBuilder::new(Kind::Custom(1618), "Test PR")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

/// Helper to create a test Issue event (kind 1621)
fn create_test_issue_event(keys: &Keys, repo_coord: &str) -> Event {
    let tags = vec![Tag::custom(
        TagKind::custom("a"),
        vec![repo_coord.to_string()],
    )];

    EventBuilder::new(Kind::Custom(1621), "Test issue")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

/// Helper to create a test Reply event (kind 1622)
fn create_test_reply_event(keys: &Keys, event_id: &str) -> Event {
    let tags = vec![Tag::custom(
        TagKind::custom("e"),
        vec![event_id.to_string()],
    )];

    EventBuilder::new(Kind::Custom(1622), "Test reply")
        .tags(tags)
        .sign_with_keys(keys)
        .expect("Failed to sign event")
}

// ============================================================================
// Filter Count Tests
// ============================================================================

/// Test initial filter count is 1 (Layer 1 only)
#[test]
fn test_initial_filter_count() {
    // Create a minimal SubscriptionManager-like state for testing
    // We test the logic without needing a full FilterService

    // Initial state: 0 announcements, 0 events, not consolidated
    // Filter count should be: 1 (Layer 1) + 0 + 0 = 1
    let announcement_count = 0;
    let event_count = 0;
    let is_consolidated = false;

    let filter_count = if is_consolidated {
        1
    } else {
        1 + announcement_count + event_count
    };

    assert_eq!(filter_count, 1);
}

/// Test filter count increases with announcements
#[test]
fn test_filter_count_with_announcements() {
    let announcement_count = 5;
    let event_count = 0;
    let is_consolidated = false;

    let filter_count = if is_consolidated {
        1
    } else {
        1 + announcement_count + event_count
    };

    // 1 (Layer 1) + 5 (announcements) = 6
    assert_eq!(filter_count, 6);
}

/// Test filter count increases with events
#[test]
fn test_filter_count_with_events() {
    let announcement_count = 0;
    let event_count = 10;
    let is_consolidated = false;

    let filter_count = if is_consolidated {
        1
    } else {
        1 + announcement_count + event_count
    };

    // 1 (Layer 1) + 10 (events) = 11
    assert_eq!(filter_count, 11);
}

/// Test filter count with both announcements and events
#[test]
fn test_filter_count_mixed() {
    let announcement_count = 50;
    let event_count = 30;
    let is_consolidated = false;

    let filter_count = if is_consolidated {
        1
    } else {
        1 + announcement_count + event_count
    };

    // 1 + 50 + 30 = 81
    assert_eq!(filter_count, 81);
}

/// Test filter count is 1 when consolidated
#[test]
fn test_filter_count_consolidated() {
    let announcement_count = 100; // These would be cleared on consolidation
    let event_count = 100;
    let is_consolidated = true;

    let filter_count = if is_consolidated {
        1
    } else {
        1 + announcement_count + event_count
    };

    assert_eq!(filter_count, 1);
}

// ============================================================================
// Consolidation Threshold Tests
// ============================================================================

/// Test consolidation is not triggered below threshold
#[test]
fn test_should_consolidate_below_threshold() {
    let filter_count = 100;
    let is_consolidated = false;

    let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD;

    assert!(!should_consolidate);
}

/// Test consolidation is triggered at threshold
#[test]
fn test_should_consolidate_at_threshold() {
    let filter_count = 151; // > 150
    let is_consolidated = false;

    let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD;

    assert!(should_consolidate);
}

/// Test consolidation is triggered well above threshold
#[test]
fn test_should_consolidate_above_threshold() {
    let filter_count = 200;
    let is_consolidated = false;

    let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD;

    assert!(should_consolidate);
}

/// Test consolidation is not triggered if already consolidated
#[test]
fn test_should_consolidate_already_consolidated() {
    let filter_count = 200; // Would trigger, but already consolidated
    let is_consolidated = true;

    let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD;

    assert!(!should_consolidate);
}

/// Test exact threshold boundary (150 should NOT trigger, 151 should)
#[test]
fn test_consolidation_threshold_boundary() {
    let is_consolidated = false;

    // 150 should NOT trigger (> 150, not >= 150)
    let should_consolidate_at_150 = !is_consolidated && 150 > CONSOLIDATION_THRESHOLD;
    assert!(!should_consolidate_at_150);

    // 151 should trigger
    let should_consolidate_at_151 = !is_consolidated && 151 > CONSOLIDATION_THRESHOLD;
    assert!(should_consolidate_at_151);
}

// ============================================================================
// Duplicate Prevention Tests
// ============================================================================

/// Test duplicate announcement detection
#[test]
fn test_duplicate_announcement_prevention() {
    let mut subscribed_announcements: HashSet<String> = HashSet::new();

    let event_id = "abc123".to_string();

    // First add should succeed
    let is_new = !subscribed_announcements.contains(&event_id);
    assert!(is_new);
    subscribed_announcements.insert(event_id.clone());

    // Second add should fail (duplicate)
    let is_new_again = !subscribed_announcements.contains(&event_id);
    assert!(!is_new_again);
}

/// Test duplicate event detection
#[test]
fn test_duplicate_event_prevention() {
    let mut subscribed_events: HashSet<String> = HashSet::new();

    let event_id = "def456".to_string();

    // First add should succeed
    let is_new = !subscribed_events.contains(&event_id);
    assert!(is_new);
    subscribed_events.insert(event_id.clone());

    // Second add should fail (duplicate)
    let is_new_again = !subscribed_events.contains(&event_id);
    assert!(!is_new_again);
}

/// Test multiple unique items are tracked correctly
#[test]
fn test_multiple_unique_items_tracked() {
    let mut subscribed_announcements: HashSet<String> = HashSet::new();

    // Add multiple unique announcements
    for i in 0..10 {
        let id = format!("announcement_{}", i);
        assert!(!subscribed_announcements.contains(&id));
        subscribed_announcements.insert(id);
    }

    assert_eq!(subscribed_announcements.len(), 10);
}

// ============================================================================
// Event Creation and Validation Tests
// ============================================================================

/// Test announcement event has required d tag
#[test]
fn test_announcement_has_d_tag() {
    let keys = Keys::generate();
    let event = create_test_announcement(&keys, "my-repo");

    let has_d_tag = event.tags.iter().any(|tag| {
        let tag_vec = tag.clone().to_vec();
        tag_vec.len() >= 2 && tag_vec[0] == "d"
    });

    assert!(has_d_tag);
}

/// Test announcement event has correct kind
#[test]
fn test_announcement_correct_kind() {
    let keys = Keys::generate();
    let event = create_test_announcement(&keys, "my-repo");

    assert_eq!(event.kind.as_u16(), KIND_REPOSITORY_ANNOUNCEMENT);
}

/// Test maintainer list event has correct kind
#[test]
fn test_maintainer_list_correct_kind() {
    let keys = Keys::generate();
    let event = create_test_maintainer_list(&keys, "maintainers");

    assert_eq!(event.kind.as_u16(), KIND_MAINTAINER_LIST);
}

/// Test PR event has a tag
#[test]
fn test_pr_event_has_a_tag() {
    let keys = Keys::generate();
    let coord = "30617:pubkey123:my-repo";
    let event = create_test_pr_event(&keys, coord);

    let has_a_tag = event.tags.iter().any(|tag| {
        let tag_vec = tag.clone().to_vec();
        tag_vec.len() >= 2 && tag_vec[0] == "a"
    });

    assert!(has_a_tag);
}

/// Test issue event has a tag
#[test]
fn test_issue_event_has_a_tag() {
    let keys = Keys::generate();
    let coord = "30617:pubkey123:my-repo";
    let event = create_test_issue_event(&keys, coord);

    let has_a_tag = event.tags.iter().any(|tag| {
        let tag_vec = tag.clone().to_vec();
        tag_vec.len() >= 2 && tag_vec[0] == "a"
    });

    assert!(has_a_tag);
}

/// Test reply event has e tag
#[test]
fn test_reply_event_has_e_tag() {
    let keys = Keys::generate();
    let event_id = "abc123def456";
    let event = create_test_reply_event(&keys, event_id);

    let has_e_tag = event.tags.iter().any(|tag| {
        let tag_vec = tag.clone().to_vec();
        tag_vec.len() >= 2 && tag_vec[0] == "e"
    });

    assert!(has_e_tag);
}

// ============================================================================
// Subscription Lifecycle Tests
// ============================================================================

/// Test subscription lifecycle: initial -> add announcements -> add events -> consolidate
#[test]
fn test_subscription_lifecycle() {
    let mut subscribed_announcements: HashSet<String> = HashSet::new();
    let mut subscribed_events: HashSet<String> = HashSet::new();
    let mut is_consolidated = false;

    // Initial state
    let initial_count = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(initial_count, 1);

    // Add some announcements
    for i in 0..50 {
        subscribed_announcements.insert(format!("ann_{}", i));
    }

    let after_announcements = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(after_announcements, 51);

    // Add some events
    for i in 0..50 {
        subscribed_events.insert(format!("evt_{}", i));
    }

    let after_events = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(after_events, 101);

    // Add more to exceed threshold
    for i in 50..100 {
        subscribed_announcements.insert(format!("ann_{}", i));
    }

    let before_consolidation = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(before_consolidation, 151);

    // Should trigger consolidation
    let should_consolidate = !is_consolidated && before_consolidation > CONSOLIDATION_THRESHOLD;
    assert!(should_consolidate);

    // Consolidate
    subscribed_announcements.clear();
    subscribed_events.clear();
    is_consolidated = true;

    // After consolidation
    let after_consolidation = if is_consolidated {
        1
    } else {
        1 + subscribed_announcements.len() + subscribed_events.len()
    };
    assert_eq!(after_consolidation, 1);

    // Should not trigger consolidation again
    let should_consolidate_again =
        !is_consolidated && after_consolidation > CONSOLIDATION_THRESHOLD;
    assert!(!should_consolidate_again);
}

/// Test that consolidated state blocks new additions
#[test]
fn test_consolidated_blocks_additions() {
    let is_consolidated = true;

    // When consolidated, add_announcement should return None (simulated)
    // The logic is: if is_consolidated, return None
    let should_add = !is_consolidated;

    assert!(!should_add);
}

/// Test that non-consolidated state allows additions
#[test]
fn test_non_consolidated_allows_additions() {
    let is_consolidated = false;
    let mut subscribed_announcements: HashSet<String> = HashSet::new();
    let event_id = "new_announcement";

    // When not consolidated and event not in set, should add
    let should_add = !is_consolidated && !subscribed_announcements.contains(event_id);

    assert!(should_add);

    subscribed_announcements.insert(event_id.to_string());
    assert!(subscribed_announcements.contains(event_id));
}

// ============================================================================
// Filter Building Tests (coordinate format)
// ============================================================================

/// Test announcement coordinate format
#[test]
fn test_announcement_coordinate_format() {
    let keys = Keys::generate();
    let identifier = "my-repo";
    let event = create_test_announcement(&keys, identifier);

    // Extract d tag
    let d_tag = event.tags.iter().find_map(|tag| {
        let tag_vec = tag.clone().to_vec();
        if tag_vec.len() >= 2 && tag_vec[0] == "d" {
            Some(tag_vec[1].clone())
        } else {
            None
        }
    });

    assert!(d_tag.is_some());
    assert_eq!(d_tag.unwrap(), identifier);

    // Build coordinate: kind:pubkey:identifier
    let coord = format!(
        "{}:{}:{}",
        KIND_REPOSITORY_ANNOUNCEMENT,
        event.pubkey.to_hex(),
        identifier
    );

    // Verify format
    let parts: Vec<&str> = coord.split(':').collect();
    assert_eq!(parts.len(), 3);
    assert_eq!(parts[0], "30617");
    assert_eq!(parts[2], identifier);
}

/// Test multiple announcement coordinates are unique
#[test]
fn test_multiple_announcement_coordinates_unique() {
    let keys = Keys::generate();

    let identifiers = vec!["repo1", "repo2", "repo3"];
    let mut coords: HashSet<String> = HashSet::new();

    for id in identifiers {
        let event = create_test_announcement(&keys, id);
        let coord = format!(
            "{}:{}:{}",
            KIND_REPOSITORY_ANNOUNCEMENT,
            event.pubkey.to_hex(),
            id
        );
        coords.insert(coord);
    }

    assert_eq!(coords.len(), 3);
}

// ============================================================================
// Integration-style Tests
// ============================================================================

/// Test simulated workflow: announcement received, then PR received
#[test]
fn test_workflow_announcement_then_pr() {
    let keys = Keys::generate();
    let mut subscribed_announcements: HashSet<String> = HashSet::new();
    let mut subscribed_events: HashSet<String> = HashSet::new();
    let is_consolidated = false;

    // Step 1: Receive announcement
    let announcement = create_test_announcement(&keys, "my-repo");
    let ann_id = announcement.id.to_hex();

    // Should add to tracking (simulating add_announcement)
    let should_add_ann = !is_consolidated && !subscribed_announcements.contains(&ann_id);
    assert!(should_add_ann);
    subscribed_announcements.insert(ann_id.clone());

    // Filter count should increase
    let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(filter_count, 2);

    // Step 2: Receive PR for that repo
    let coord = format!(
        "{}:{}:my-repo",
        KIND_REPOSITORY_ANNOUNCEMENT,
        keys.public_key().to_hex()
    );
    let pr = create_test_pr_event(&keys, &coord);
    let pr_id = pr.id.to_hex();

    // Should add to tracking (simulating add_event)
    let should_add_pr = !is_consolidated && !subscribed_events.contains(&pr_id);
    assert!(should_add_pr);
    subscribed_events.insert(pr_id.clone());

    // Filter count should increase again
    let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len();
    assert_eq!(filter_count, 3);
}

/// Test stress: adding many items triggers consolidation
#[test]
fn test_stress_many_items_triggers_consolidation() {
    let keys = Keys::generate();
    let mut subscribed_announcements: HashSet<String> = HashSet::new();
    let mut subscribed_events: HashSet<String> = HashSet::new();
    let mut is_consolidated = false;
    let mut consolidation_triggered = false;

    // Add 100 announcements
    for i in 0..100 {
        let event = create_test_announcement(&keys, &format!("repo-{}", i));
        let event_id = event.id.to_hex();

        if !is_consolidated && !subscribed_announcements.contains(&event_id) {
            subscribed_announcements.insert(event_id);
        }

        // Check consolidation after each add
        let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len();
        if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD {
            consolidation_triggered = true;
            subscribed_announcements.clear();
            subscribed_events.clear();
            is_consolidated = true;
            break;
        }
    }

    // If we didn't consolidate yet, add events
    if !consolidation_triggered {
        for i in 0..100 {
            let coord = format!("30617:pubkey:repo-{}", i);
            let event = create_test_pr_event(&keys, &coord);
            let event_id = event.id.to_hex();

            if !is_consolidated && !subscribed_events.contains(&event_id) {
                subscribed_events.insert(event_id);
            }

            // Check consolidation after each add
            let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len();
            if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD {
                consolidation_triggered = true;
                subscribed_announcements.clear();
                subscribed_events.clear();
                is_consolidated = true;
                break;
            }
        }
    }

    // Consolidation should have been triggered
    assert!(consolidation_triggered);
    assert!(is_consolidated);

    // After consolidation, counts should be reset
    assert_eq!(subscribed_announcements.len(), 0);
    assert_eq!(subscribed_events.len(), 0);
}