upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/sync/rejected_index.rs
blob: 4d3190104323009fe1034a91672937af42be1f4a (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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//! Two-tier rejected events index for efficient re-processing
//!
//! This module provides a two-tier storage system for rejected repository announcements:
//!
//! 1. **Hot Cache (Tier 1)**: Stores full event objects for 2 minutes
//!    - Enables immediate re-processing when dependencies resolve
//!    - Auto-expires to prevent memory growth
//!    - Typical memory: ~200 KB, worst case: ~20 MB
//!
//! 2. **Cold Index (Tier 2)**: Stores metadata only for 7 days
//!    - Prevents repeated downloads of rejected events
//!    - Enables invalidation when dependencies change
//!    - Typical memory: ~1 MB
//!
//! # Problem Solved
//!
//! Without this system, maintainer announcements face a timing gap:
//!
//! ```text
//! 00:00 - Maintainer announcement rejected → Event discarded
//! 00:02 - Owner announcement accepted (lists maintainer) → Want to re-process
//! 00:02 - ❌ Maintainer announcement GONE → Must wait 24h for next sync
//! ```
//!
//! With the two-tier system:
//!
//! ```text
//! 00:00 - Maintainer announcement rejected → Stored in hot cache + cold index
//! 00:02 - Owner announcement accepted → Invalidate + get from hot cache
//! 00:02 - ✅ Re-process immediately → Accepted in <1 second
//! ```
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Tier 1: Hot Cache (2 minutes)                               │
//! │ - Stores FULL EVENT objects                                 │
//! │ - Enables IMMEDIATE re-processing                           │
//! │ - Auto-expires after 2 minutes                              │
//! │ - Memory: ~200 KB typical, ~20 MB worst case                │
//! └─────────────────────────────────────────────────────────────┘
//!                         │
//!                         │ After 2 minutes
//!                         ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Tier 2: Cold Index (7 days)                                 │
//! │ - Stores METADATA only (event_id, pubkey, identifier)       │
//! │ - Prevents repeated downloads                               │
//! │ - Enables invalidation                                      │
//! │ - Memory: ~1 MB typical                                     │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Usage
//!
//! ```rust,ignore
//! use ngit_grasp::sync::rejected_index::{RejectedEventsIndex, RejectionReason, EventType};
//! use nostr_sdk::{Event, PublicKey};
//! use std::time::Duration;
//!
//! let index = RejectedEventsIndex::new(
//!     Duration::from_secs(120),  // hot cache: 2 minutes
//!     Duration::from_secs(604800), // cold index: 7 days
//! );
//!
//! // Add rejected announcement (event is a nostr_sdk::Event)
//! index.add_announcement(
//!     event.clone(),
//!     event.pubkey,
//!     "my-repo".to_string(),
//!     RejectionReason::DoesNotListService,
//! );
//!
//! // Later, when owner announcement accepted...
//! let (removed, hot_events) = index.invalidate_and_get(
//!     &maintainer_pubkey,
//!     "my-repo",
//!     Some(EventType::Announcement),
//! );
//!
//! // Re-process events from hot cache immediately
//! for event in hot_events {
//!     process_event(&event).await;
//! }
//! ```

use nostr_sdk::{Event, EventId, PublicKey};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Type of event stored in the rejected events index
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventType {
    /// Repository announcement (kind 30617)
    Announcement,
    /// Repository state event (kind 30618)
    State,
}

impl std::fmt::Display for EventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Announcement => write!(f, "announcement"),
            Self::State => write!(f, "state"),
        }
    }
}

/// Reason why a repository announcement was rejected
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RejectionReason {
    /// Announcement doesn't list this service in clone/web URLs
    DoesNotListService,
    /// Maintainer announcement rejected (owner not yet accepted)
    MaintainerNotYetValid,
    /// Other validation failure
    Other,
}

impl std::fmt::Display for RejectionReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::DoesNotListService => write!(f, "does_not_list_service"),
            Self::MaintainerNotYetValid => write!(f, "maintainer_not_yet_valid"),
            Self::Other => write!(f, "other"),
        }
    }
}

/// Entry in the hot cache (full event)
#[derive(Debug, Clone)]
struct HotCacheEntry {
    event: Event,
    pubkey: PublicKey,
    identifier: String,
    event_type: EventType,
    #[allow(dead_code)] // Used for metrics/debugging in future
    reason: RejectionReason,
    cached_at: Instant,
}

/// Entry in the cold index (metadata only)
///
/// Note: event_id is stored as the HashMap key, not in this struct
#[derive(Debug, Clone)]
struct ColdIndexEntry {
    pubkey: PublicKey,
    identifier: String,
    event_type: EventType,
    #[allow(dead_code)] // Used for metrics/debugging in future
    reason: RejectionReason,
    rejected_at: Instant,
}

/// Hot cache: Stores full events for immediate re-processing
///
/// Events are stored for a short duration (default: 2 minutes) to enable
/// immediate re-processing when dependencies resolve. After expiry, events
/// are dropped from the hot cache but remain in the cold index.
#[derive(Debug, Clone)]
struct HotCache {
    /// Map of event_id -> full event entry
    entries: Arc<RwLock<HashMap<EventId, HotCacheEntry>>>,
    /// Duration before entries expire
    expiry_duration: Duration,
}

impl HotCache {
    fn new(expiry_duration: Duration) -> Self {
        Self {
            entries: Arc::new(RwLock::new(HashMap::new())),
            expiry_duration,
        }
    }

    /// Add event to hot cache
    fn add(
        &self,
        event: Event,
        pubkey: PublicKey,
        identifier: String,
        event_type: EventType,
        reason: RejectionReason,
    ) {
        let entry = HotCacheEntry {
            event,
            pubkey,
            identifier,
            event_type,
            reason,
            cached_at: Instant::now(),
        };

        self.entries.write().unwrap().insert(entry.event.id, entry);
    }

    /// Get events for a specific maintainer/identifier from hot cache
    ///
    /// If `event_type` is `Some`, only returns events of that type.
    /// If `event_type` is `None`, returns all event types.
    fn get_maintainer_events(
        &self,
        pubkey: &PublicKey,
        identifier: &str,
        event_type: Option<EventType>,
    ) -> Vec<Event> {
        let entries = self.entries.read().unwrap();
        let now = Instant::now();

        entries
            .values()
            .filter(|entry| {
                // Check if entry matches and hasn't expired
                let matches_type = event_type.is_none_or(|et| entry.event_type == et);
                entry.pubkey == *pubkey
                    && entry.identifier == identifier
                    && matches_type
                    && now.duration_since(entry.cached_at) < self.expiry_duration
            })
            .map(|entry| entry.event.clone())
            .collect()
    }

    /// Remove expired entries from hot cache
    fn cleanup_expired(&self) -> usize {
        let mut entries = self.entries.write().unwrap();
        let now = Instant::now();
        let initial_count = entries.len();

        entries.retain(|_, entry| now.duration_since(entry.cached_at) < self.expiry_duration);

        initial_count - entries.len()
    }

    /// Get current number of entries in hot cache
    fn len(&self) -> usize {
        self.entries.read().unwrap().len()
    }

    /// Check if event is in hot cache
    fn contains(&self, event_id: &EventId) -> bool {
        self.entries.read().unwrap().contains_key(event_id)
    }
}

/// Cold index: Stores metadata only for long-term deduplication
///
/// Events are stored for a long duration (default: 7 days) to prevent
/// repeated downloads of rejected events. Only metadata is stored to
/// minimize memory usage.
#[derive(Debug, Clone)]
struct ColdIndex {
    /// Map of event_id -> metadata entry
    entries: Arc<RwLock<HashMap<EventId, ColdIndexEntry>>>,
    /// Duration before entries expire
    expiry_duration: Duration,
}

impl ColdIndex {
    fn new(expiry_duration: Duration) -> Self {
        Self {
            entries: Arc::new(RwLock::new(HashMap::new())),
            expiry_duration,
        }
    }

    /// Add metadata to cold index
    fn add(
        &self,
        event_id: EventId,
        pubkey: PublicKey,
        identifier: String,
        event_type: EventType,
        reason: RejectionReason,
    ) {
        let entry = ColdIndexEntry {
            pubkey,
            identifier,
            event_type,
            reason,
            rejected_at: Instant::now(),
        };

        self.entries.write().unwrap().insert(event_id, entry);
    }

    /// Check if event is in cold index
    fn contains(&self, event_id: &EventId) -> bool {
        let entries = self.entries.read().unwrap();
        if let Some(entry) = entries.get(event_id) {
            let now = Instant::now();
            now.duration_since(entry.rejected_at) < self.expiry_duration
        } else {
            false
        }
    }

    /// Invalidate (remove) entries from cold index
    ///
    /// Called when an owner announcement is accepted that lists this maintainer.
    /// Removes the cold index entries so they can be re-fetched on next sync.
    ///
    /// If `event_type` is `Some`, only removes entries of that type.
    /// If `event_type` is `None`, removes all event types matching pubkey/identifier.
    fn invalidate_maintainer_announcements(
        &self,
        maintainer_pubkey: &PublicKey,
        identifier: &str,
        event_type: Option<EventType>,
    ) -> usize {
        let mut entries = self.entries.write().unwrap();
        let initial_count = entries.len();

        entries.retain(|_, entry| {
            // Keep entries that DON'T match the maintainer/identifier/type
            let matches_type = event_type.is_none_or(|et| entry.event_type == et);
            !(entry.pubkey == *maintainer_pubkey && entry.identifier == identifier && matches_type)
        });

        initial_count - entries.len()
    }

    /// Remove expired entries from cold index
    fn cleanup_expired(&self) -> usize {
        let mut entries = self.entries.write().unwrap();
        let now = Instant::now();
        let initial_count = entries.len();

        entries.retain(|_, entry| now.duration_since(entry.rejected_at) < self.expiry_duration);

        initial_count - entries.len()
    }

    /// Get current number of entries in cold index
    fn len(&self) -> usize {
        self.entries.read().unwrap().len()
    }
}

/// Two-tier rejected events index
///
/// Combines hot cache (full events, short duration) with cold index
/// (metadata only, long duration) for efficient re-processing and deduplication.
#[derive(Clone)]
pub struct RejectedEventsIndex {
    hot_cache: HotCache,
    cold_index: ColdIndex,
    metrics: Option<super::metrics::SyncMetrics>,
}

// Manual Debug impl to avoid requiring Debug on SyncMetrics
impl std::fmt::Debug for RejectedEventsIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RejectedEventsIndex")
            .field("hot_cache", &self.hot_cache)
            .field("cold_index", &self.cold_index)
            .field("metrics", &self.metrics.is_some())
            .finish()
    }
}

impl RejectedEventsIndex {
    /// Create new rejected events index
    ///
    /// # Arguments
    ///
    /// * `hot_cache_duration` - How long to keep full events in hot cache (default: 2 minutes)
    /// * `cold_index_duration` - How long to keep metadata in cold index (default: 7 days)
    pub fn new(hot_cache_duration: Duration, cold_index_duration: Duration) -> Self {
        Self {
            hot_cache: HotCache::new(hot_cache_duration),
            cold_index: ColdIndex::new(cold_index_duration),
            metrics: None,
        }
    }

    /// Create new rejected events index with metrics
    ///
    /// # Arguments
    ///
    /// * `hot_cache_duration` - How long to keep full events in hot cache (default: 2 minutes)
    /// * `cold_index_duration` - How long to keep metadata in cold index (default: 7 days)
    /// * `metrics` - Prometheus metrics for tracking index operations
    pub fn with_metrics(
        hot_cache_duration: Duration,
        cold_index_duration: Duration,
        metrics: super::metrics::SyncMetrics,
    ) -> Self {
        let index = Self {
            hot_cache: HotCache::new(hot_cache_duration),
            cold_index: ColdIndex::new(cold_index_duration),
            metrics: Some(metrics),
        };

        // Initialize metrics with current sizes for both event types
        index.update_metrics_for_type("announcement");
        index.update_metrics_for_type("state");
        index
    }

    /// Update metrics with current sizes for a specific event type
    ///
    /// # Arguments
    ///
    /// * `event_type` - The event type label ("announcement" or "state")
    fn update_metrics_for_type(&self, event_type: &str) {
        if let Some(ref metrics) = self.metrics {
            metrics.update_rejected_hot_cache_size(event_type, self.hot_cache.len());
            metrics.update_rejected_cold_index_size(event_type, self.cold_index.len());
        }
    }

    /// Add rejected announcement to both tiers
    ///
    /// # Arguments
    ///
    /// * `event` - Full event object (stored in hot cache)
    /// * `pubkey` - Author's public key
    /// * `identifier` - Repository identifier (d tag)
    /// * `reason` - Why the announcement was rejected
    pub fn add_announcement(
        &self,
        event: Event,
        pubkey: PublicKey,
        identifier: String,
        reason: RejectionReason,
    ) {
        // Add to hot cache (full event)
        self.hot_cache.add(
            event.clone(),
            pubkey,
            identifier.clone(),
            EventType::Announcement,
            reason,
        );

        // Add to cold index (metadata only)
        self.cold_index.add(
            event.id,
            pubkey,
            identifier,
            EventType::Announcement,
            reason,
        );

        // Update metrics
        self.update_metrics_for_type("announcement");
    }

    /// Add rejected state event to both tiers
    ///
    /// # Arguments
    ///
    /// * `event` - Full event object (stored in hot cache)
    /// * `pubkey` - Author's public key
    /// * `identifier` - Repository identifier (d tag)
    /// * `reason` - Why the state event was rejected
    pub fn add_state(
        &self,
        event: Event,
        pubkey: PublicKey,
        identifier: String,
        reason: RejectionReason,
    ) {
        // Add to hot cache (full event)
        self.hot_cache.add(
            event.clone(),
            pubkey,
            identifier.clone(),
            EventType::State,
            reason,
        );

        // Add to cold index (metadata only)
        self.cold_index
            .add(event.id, pubkey, identifier, EventType::State, reason);

        // Update metrics
        self.update_metrics_for_type("state");
    }

    /// Check if event is already rejected (in either tier)
    pub fn contains(&self, event_id: &EventId) -> bool {
        self.hot_cache.contains(event_id) || self.cold_index.contains(event_id)
    }

    /// Invalidate events and get them for immediate re-processing (unified method)
    ///
    /// This is called when a dependency is satisfied (e.g., owner announcement accepted,
    /// or announcement accepted for state events). It removes the cold index entries
    /// (so they can be re-fetched on next sync) and returns any events still in the
    /// hot cache for immediate re-processing.
    ///
    /// # Arguments
    ///
    /// * `pubkey` - Public key to match (maintainer for announcements, author for states)
    /// * `identifier` - Repository identifier (d tag)
    /// * `event_type` - If `Some`, filter to that event type; if `None`, return all types
    ///
    /// # Returns
    ///
    /// Tuple of (number of cold index entries removed, events from hot cache)
    pub fn invalidate_and_get(
        &self,
        pubkey: &PublicKey,
        identifier: &str,
        event_type: Option<EventType>,
    ) -> (usize, Vec<Event>) {
        // Remove from cold index
        let removed = self
            .cold_index
            .invalidate_maintainer_announcements(pubkey, identifier, event_type);

        // Get from hot cache (for immediate re-processing)
        let events = self
            .hot_cache
            .get_maintainer_events(pubkey, identifier, event_type);

        // Track metrics based on event type
        if let Some(ref metrics) = self.metrics {
            let type_label = match event_type {
                Some(EventType::State) => "state",
                Some(EventType::Announcement) | None => "announcement",
            };

            if removed > 0 {
                metrics.record_rejected_invalidation(type_label, removed);
            }
            if events.is_empty() {
                metrics.record_rejected_hot_cache_miss(type_label);
            } else {
                for _ in &events {
                    metrics.record_rejected_hot_cache_hit(type_label);
                }
            }
        }

        // Update size metrics based on event type
        let type_label = match event_type {
            Some(EventType::State) => "state",
            Some(EventType::Announcement) | None => "announcement",
        };
        self.update_metrics_for_type(type_label);

        (removed, events)
    }

    /// Clean up expired entries from both tiers
    ///
    /// # Arguments
    ///
    /// * `event_type` - The event type label for metrics ("announcement" or "state")
    ///
    /// # Returns
    ///
    /// Tuple of (hot cache expired, cold index expired)
    pub fn cleanup_expired_for_type(&self, event_type: &str) -> (usize, usize) {
        let hot_expired = self.hot_cache.cleanup_expired();
        let cold_expired = self.cold_index.cleanup_expired();

        // Track metrics
        if let Some(ref metrics) = self.metrics {
            if hot_expired > 0 {
                metrics.record_rejected_hot_cache_expired(event_type, hot_expired);
            }
            if cold_expired > 0 {
                metrics.record_rejected_cold_index_expired(event_type, cold_expired);
            }
        }

        // Update size metrics
        self.update_metrics_for_type(event_type);

        (hot_expired, cold_expired)
    }

    /// Get current number of entries in hot cache
    pub fn hot_cache_len(&self) -> usize {
        self.hot_cache.len()
    }

    /// Get current number of entries in cold index
    pub fn cold_index_len(&self) -> usize {
        self.cold_index.len()
    }

    /// Get all rejected event IDs (from both hot cache and cold index)
    ///
    /// Used for excluding rejected events from negentropy sync.
    /// Note: This creates a snapshot - events may be added/removed concurrently.
    pub fn get_all_event_ids(&self) -> HashSet<EventId> {
        let mut ids = HashSet::new();

        // Add from hot cache
        let hot_entries = self.hot_cache.entries.read().unwrap();
        ids.extend(hot_entries.keys().cloned());

        // Add from cold index
        let cold_entries = self.cold_index.entries.read().unwrap();
        ids.extend(cold_entries.keys().cloned());

        ids
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nostr_sdk::{Keys, NostrSigner};

    async fn create_test_event() -> Event {
        let keys = Keys::generate();
        let unsigned = nostr_sdk::EventBuilder::text_note("test").build(keys.public_key());
        keys.sign_event(unsigned).await.unwrap()
    }

    #[tokio::test]
    async fn test_hot_cache_stores_and_retrieves_events() {
        let cache = HotCache::new(Duration::from_secs(120));
        let event = create_test_event().await;
        let pubkey = event.pubkey;
        let identifier = "test-repo".to_string();

        cache.add(
            event.clone(),
            pubkey,
            identifier.clone(),
            EventType::Announcement,
            RejectionReason::DoesNotListService,
        );

        assert!(cache.contains(&event.id));

        let retrieved = cache.get_maintainer_events(&pubkey, &identifier, None);
        assert_eq!(retrieved.len(), 1);
        assert_eq!(retrieved[0].id, event.id);
    }

    #[tokio::test]
    async fn test_hot_cache_expires_after_duration() {
        let cache = HotCache::new(Duration::from_millis(50));
        let event = create_test_event().await;

        cache.add(
            event.clone(),
            event.pubkey,
            "test-repo".to_string(),
            EventType::Announcement,
            RejectionReason::DoesNotListService,
        );

        assert!(cache.contains(&event.id));

        // Wait for expiry
        std::thread::sleep(Duration::from_millis(60));

        let expired = cache.cleanup_expired();
        assert_eq!(expired, 1);
        assert!(!cache.contains(&event.id));
    }

    #[tokio::test]
    async fn test_cold_index_tracks_metadata() {
        let index = ColdIndex::new(Duration::from_secs(604800));
        let event = create_test_event().await;

        index.add(
            event.id,
            event.pubkey,
            "test-repo".to_string(),
            EventType::Announcement,
            RejectionReason::DoesNotListService,
        );

        assert!(index.contains(&event.id));
        assert_eq!(index.len(), 1);
    }

    #[tokio::test]
    async fn test_cold_index_invalidation() {
        let index = ColdIndex::new(Duration::from_secs(604800));
        let event = create_test_event().await;
        let pubkey = event.pubkey;
        let identifier = "test-repo".to_string();

        index.add(
            event.id,
            pubkey,
            identifier.clone(),
            EventType::Announcement,
            RejectionReason::MaintainerNotYetValid,
        );

        assert!(index.contains(&event.id));

        let removed = index.invalidate_maintainer_announcements(
            &pubkey,
            &identifier,
            Some(EventType::Announcement),
        );
        assert_eq!(removed, 1);
        assert!(!index.contains(&event.id));
    }

    #[tokio::test]
    async fn test_two_tier_index_add_and_contains() {
        let index = RejectedEventsIndex::new(Duration::from_secs(120), Duration::from_secs(604800));
        let event = create_test_event().await;

        index.add_announcement(
            event.clone(),
            event.pubkey,
            "test-repo".to_string(),
            RejectionReason::DoesNotListService,
        );

        assert!(index.contains(&event.id));
        assert_eq!(index.hot_cache_len(), 1);
        assert_eq!(index.cold_index_len(), 1);
    }

    #[tokio::test]
    async fn test_invalidate_and_get_announcements() {
        let index = RejectedEventsIndex::new(Duration::from_secs(120), Duration::from_secs(604800));
        let event = create_test_event().await;
        let pubkey = event.pubkey;
        let identifier = "test-repo".to_string();

        index.add_announcement(
            event.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::MaintainerNotYetValid,
        );

        let (removed, hot_events) =
            index.invalidate_and_get(&pubkey, &identifier, Some(EventType::Announcement));

        assert_eq!(removed, 1); // Removed from cold index
        assert_eq!(hot_events.len(), 1); // Retrieved from hot cache
        assert_eq!(hot_events[0].id, event.id);

        // Cold index entry removed, hot cache still has it
        assert_eq!(index.cold_index_len(), 0);
        assert_eq!(index.hot_cache_len(), 1);
    }

    #[tokio::test]
    async fn test_cleanup_expired_both_tiers() {
        let index = RejectedEventsIndex::new(
            Duration::from_millis(50),  // Hot cache expires quickly
            Duration::from_millis(100), // Cold index expires slower
        );
        let event = create_test_event().await;

        index.add_announcement(
            event.clone(),
            event.pubkey,
            "test-repo".to_string(),
            RejectionReason::DoesNotListService,
        );

        // Wait for hot cache to expire
        std::thread::sleep(Duration::from_millis(60));

        let (hot_expired, cold_expired) = index.cleanup_expired_for_type("announcement");
        assert_eq!(hot_expired, 1);
        assert_eq!(cold_expired, 0); // Not expired yet

        // Wait for cold index to expire
        std::thread::sleep(Duration::from_millis(50));

        let (hot_expired, cold_expired) = index.cleanup_expired_for_type("announcement");
        assert_eq!(hot_expired, 0); // Already cleaned up
        assert_eq!(cold_expired, 1);
    }

    #[tokio::test]
    async fn test_hot_cache_miss_after_expiry() {
        let index =
            RejectedEventsIndex::new(Duration::from_millis(50), Duration::from_secs(604800));
        let event = create_test_event().await;
        let pubkey = event.pubkey;
        let identifier = "test-repo".to_string();

        index.add_announcement(
            event.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::MaintainerNotYetValid,
        );

        // Wait for hot cache to expire
        std::thread::sleep(Duration::from_millis(60));

        let (removed, hot_events) =
            index.invalidate_and_get(&pubkey, &identifier, Some(EventType::Announcement));

        assert_eq!(removed, 1); // Removed from cold index
        assert_eq!(hot_events.len(), 0); // Hot cache expired - miss!

        // This is expected: events arrive >2 minutes apart, must wait for next sync
    }

    #[tokio::test]
    async fn test_multiple_maintainer_repos() {
        let index = RejectedEventsIndex::new(Duration::from_secs(120), Duration::from_secs(604800));

        let keys1 = Keys::generate();
        let keys2 = Keys::generate();

        let unsigned1 = nostr_sdk::EventBuilder::text_note("test1").build(keys1.public_key());
        let event1 = keys1.sign_event(unsigned1).await.unwrap();

        let unsigned2 = nostr_sdk::EventBuilder::text_note("test2").build(keys2.public_key());
        let event2 = keys2.sign_event(unsigned2).await.unwrap();

        // Add two different maintainer repos
        index.add_announcement(
            event1.clone(),
            event1.pubkey,
            "repo1".to_string(),
            RejectionReason::MaintainerNotYetValid,
        );

        index.add_announcement(
            event2.clone(),
            event2.pubkey,
            "repo2".to_string(),
            RejectionReason::MaintainerNotYetValid,
        );

        assert_eq!(index.hot_cache_len(), 2);
        assert_eq!(index.cold_index_len(), 2);

        // Invalidate only first maintainer
        let (removed, hot_events) =
            index.invalidate_and_get(&event1.pubkey, "repo1", Some(EventType::Announcement));

        assert_eq!(removed, 1);
        assert_eq!(hot_events.len(), 1);
        assert_eq!(hot_events[0].id, event1.id);

        // Second maintainer still in index
        assert_eq!(index.cold_index_len(), 1);
        assert!(index.contains(&event2.id));
    }

    #[tokio::test]
    async fn test_invalidate_and_get_unified_with_event_type_filter() {
        let index = RejectedEventsIndex::new(Duration::from_secs(120), Duration::from_secs(604800));
        let keys = Keys::generate();

        // Create an announcement event
        let unsigned_ann =
            nostr_sdk::EventBuilder::text_note("announcement").build(keys.public_key());
        let event_ann = keys.sign_event(unsigned_ann).await.unwrap();

        // Create a state event
        let unsigned_state = nostr_sdk::EventBuilder::text_note("state").build(keys.public_key());
        let event_state = keys.sign_event(unsigned_state).await.unwrap();

        let pubkey = event_ann.pubkey;
        let identifier = "test-repo".to_string();

        // Add announcement and state for same pubkey/identifier
        index.add_announcement(
            event_ann.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::MaintainerNotYetValid,
        );

        index.add_state(
            event_state.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::Other,
        );

        assert_eq!(index.hot_cache_len(), 2);
        assert_eq!(index.cold_index_len(), 2);

        // Invalidate only announcements
        let (removed, hot_events) =
            index.invalidate_and_get(&pubkey, &identifier, Some(EventType::Announcement));

        assert_eq!(removed, 1); // Only announcement removed from cold index
        assert_eq!(hot_events.len(), 1);
        assert_eq!(hot_events[0].id, event_ann.id);

        // State is still in cold index
        assert_eq!(index.cold_index_len(), 1);
        assert!(index.contains(&event_state.id));

        // Now invalidate states
        let (removed, hot_events) =
            index.invalidate_and_get(&pubkey, &identifier, Some(EventType::State));

        assert_eq!(removed, 1);
        assert_eq!(hot_events.len(), 1);
        assert_eq!(hot_events[0].id, event_state.id);

        // Cold index now empty
        assert_eq!(index.cold_index_len(), 0);
    }

    #[tokio::test]
    async fn test_invalidate_and_get_unified_without_filter() {
        let index = RejectedEventsIndex::new(Duration::from_secs(120), Duration::from_secs(604800));
        let keys = Keys::generate();

        // Create an announcement event
        let unsigned_ann =
            nostr_sdk::EventBuilder::text_note("announcement").build(keys.public_key());
        let event_ann = keys.sign_event(unsigned_ann).await.unwrap();

        // Create a state event
        let unsigned_state = nostr_sdk::EventBuilder::text_note("state").build(keys.public_key());
        let event_state = keys.sign_event(unsigned_state).await.unwrap();

        let pubkey = event_ann.pubkey;
        let identifier = "test-repo".to_string();

        // Add announcement and state for same pubkey/identifier
        index.add_announcement(
            event_ann.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::MaintainerNotYetValid,
        );

        index.add_state(
            event_state.clone(),
            pubkey,
            identifier.clone(),
            RejectionReason::Other,
        );

        assert_eq!(index.hot_cache_len(), 2);
        assert_eq!(index.cold_index_len(), 2);

        // Invalidate all types (None filter)
        let (removed, hot_events) = index.invalidate_and_get(&pubkey, &identifier, None);

        assert_eq!(removed, 2); // Both removed from cold index
        assert_eq!(hot_events.len(), 2); // Both returned from hot cache

        // Both should be in the results
        let event_ids: Vec<_> = hot_events.iter().map(|e| e.id).collect();
        assert!(event_ids.contains(&event_ann.id));
        assert!(event_ids.contains(&event_state.id));

        // Cold index now empty
        assert_eq!(index.cold_index_len(), 0);
    }
}