test_utils.rs (1368B)
1 #![cfg(test)] 2 //! Test utilities for relay testing 3 //! 4 //! This module provides mock implementations and helpers for unit and integration tests. 5 6 use nostrdb::Filter; 7 8 use crate::relay::{OutboxSession, OutboxSubId, OutboxTask}; 9 use crate::Wakeup; 10 11 /// A mock Wakeup implementation that tracks how many times wake() was called. 12 /// 13 /// This is useful for unit tests to verify that wakeups are triggered correctly 14 /// without needing a real UI/event loop. 15 #[derive(Clone)] 16 pub struct MockWakeup {} 17 18 impl MockWakeup { 19 /// Create a new MockWakeup with zero wakeup count. 20 pub fn new() -> Self { 21 Self {} 22 } 23 } 24 25 impl Default for MockWakeup { 26 fn default() -> Self { 27 Self::new() 28 } 29 } 30 31 impl Wakeup for MockWakeup { 32 fn wake(&self) {} 33 } 34 35 /// Returns a task for `id`, panicking when the task is missing. 36 #[track_caller] 37 pub fn expect_task<'a>(session: &'a OutboxSession, id: OutboxSubId) -> &'a OutboxTask { 38 session 39 .tasks 40 .get(&id) 41 .unwrap_or_else(|| panic!("Expected task for {:?}", id)) 42 } 43 44 // ==================== SubRegistry tests ==================== 45 46 pub fn trivial_filter() -> Vec<Filter> { 47 vec![Filter::new().kinds(vec![1]).build()] 48 } 49 50 pub fn filters_json(filters: &[Filter]) -> Vec<String> { 51 filters 52 .iter() 53 .map(|f| f.json().expect("serialize filter to json")) 54 .collect() 55 }