bloop_server_framework/
test_utils.rs

1use crate::achievement::AchievementContext;
2use crate::bloop::{Bloop, BloopProvider};
3use crate::evaluator::registration_number::RegistrationNumberProvider;
4use crate::nfc_uid::NfcUid;
5use crate::player::{PlayerInfo, PlayerMutator};
6use crate::trigger::TriggerRegistry;
7use chrono::DateTime;
8use std::collections::HashMap;
9use std::fmt::Debug;
10use std::sync::{Arc, OnceLock, RwLock};
11use std::time::Duration;
12use tokio::time::Instant;
13use uuid::Uuid;
14
15/// A utility struct providing a custom UTC clock based on a fixed base time
16/// and a monotonic timer to ensure consistent time progression.
17pub struct Utc;
18
19static BASE_INSTANT: OnceLock<Instant> = OnceLock::new();
20static BASE_UTC: OnceLock<DateTime<chrono::Utc>> = OnceLock::new();
21
22/// Returns the current time as `DateTime<Utc>`, computed from a fixed base UTC
23/// time plus the elapsed monotonic duration since program start.
24impl Utc {
25    pub fn now() -> DateTime<chrono::Utc> {
26        let base_instant = *BASE_INSTANT.get_or_init(Instant::now);
27        let base_utc = *BASE_UTC.get_or_init(|| DateTime::from_timestamp(0, 0).unwrap());
28
29        let elapsed = Instant::now()
30            .checked_duration_since(base_instant)
31            .unwrap_or(Duration::ZERO);
32
33        base_utc + chrono::Duration::from_std(elapsed).unwrap()
34    }
35}
36
37/// Builder for creating test/mock players with configurable properties.
38#[derive(Default, Debug)]
39pub struct MockPlayerBuilder {
40    nfc_uid: NfcUid,
41    bloops_count: usize,
42    registration_number: usize,
43}
44
45impl MockPlayerBuilder {
46    /// Creates a new builder with default values.
47    pub fn new() -> Self {
48        Self {
49            nfc_uid: NfcUid::default(),
50            bloops_count: 0,
51            registration_number: 0,
52        }
53    }
54
55    /// Sets the NFC UID of the mock player.
56    pub fn nfc_uid(mut self, nfc_uid: NfcUid) -> Self {
57        self.nfc_uid = nfc_uid;
58        self
59    }
60
61    /// Sets the initial bloops count for the mock player.
62    pub fn bloops_count(mut self, bloops_count: usize) -> Self {
63        self.bloops_count = bloops_count;
64        self
65    }
66
67    /// Sets the registration number of the mock player.
68    pub fn registration_number(mut self, registration_number: usize) -> Self {
69        self.registration_number = registration_number;
70        self
71    }
72
73    /// Builds the [`MockPlayer`] wrapped in `Arc<RwLock>` along with its UUID.
74    pub fn build(self) -> (Arc<RwLock<MockPlayer>>, Uuid) {
75        let id = Uuid::new_v4();
76
77        (
78            Arc::new(RwLock::new(MockPlayer {
79                id,
80                nfc_uid: self.nfc_uid,
81                name: "test".to_string(),
82                bloops_count: self.bloops_count,
83                awarded: HashMap::new(),
84                registration_number: self.registration_number,
85            })),
86            id,
87        )
88    }
89}
90
91/// Represents a mock implementation of a player.
92///
93/// Holds player details and state useful for testing achievement logic.
94#[derive(Debug)]
95pub struct MockPlayer {
96    pub id: Uuid,
97    pub nfc_uid: NfcUid,
98    pub name: String,
99    pub bloops_count: usize,
100    pub awarded: HashMap<Uuid, DateTime<chrono::Utc>>,
101    pub registration_number: usize,
102}
103
104impl MockPlayer {
105    /// Returns a new [`MockPlayerBuilder`] to construct a mock player instance.
106    pub fn builder() -> MockPlayerBuilder {
107        MockPlayerBuilder::new()
108    }
109}
110
111impl PlayerInfo for MockPlayer {
112    fn id(&self) -> Uuid {
113        self.id
114    }
115
116    fn nfc_uid(&self) -> NfcUid {
117        self.nfc_uid
118    }
119
120    fn total_bloops(&self) -> usize {
121        self.bloops_count
122    }
123
124    fn awarded_achievements(&self) -> &HashMap<Uuid, DateTime<chrono::Utc>> {
125        &self.awarded
126    }
127}
128
129impl PlayerMutator for MockPlayer {
130    fn increment_bloops(&mut self) {
131        self.bloops_count += 1;
132    }
133
134    fn add_awarded_achievement(&mut self, achievement_id: Uuid, awarded_at: DateTime<chrono::Utc>) {
135        self.awarded.insert(achievement_id, awarded_at);
136    }
137}
138
139impl RegistrationNumberProvider for MockPlayer {
140    fn registration_number(&self) -> usize {
141        self.registration_number
142    }
143}
144
145/// Builder pattern struct for assembling an achievement test context.
146///
147/// Supports configuration of current bloop, player state, bloop provider,
148/// and trigger registry for testing achievement logic.
149#[derive(Debug)]
150pub struct TestCtxBuilder<Player, State = (), Trigger = ()> {
151    pub current_bloop: Bloop<Player>,
152    pub state: State,
153    pub bloop_provider: BloopProvider<Player>,
154    pub trigger_registry: TriggerRegistry<Trigger>,
155}
156
157impl<Player> TestCtxBuilder<Player, (), ()> {
158    /// Creates a new test context builder with a current bloop and default state.
159    pub fn new(current_bloop: Bloop<Player>) -> Self {
160        Self {
161            current_bloop,
162            state: (),
163            trigger_registry: TriggerRegistry::new(HashMap::new()),
164            bloop_provider: BloopProvider::new(Duration::from_secs(1800)),
165        }
166    }
167}
168
169impl<Player, State, Trigger> TestCtxBuilder<Player, State, Trigger> {
170    /// Sets the bloops to be used in the context's bloop provider.
171    pub fn bloops(mut self, bloops: Vec<Bloop<Player>>) -> Self {
172        self.bloop_provider = BloopProvider::with_bloops(Duration::from_secs(1800), bloops);
173        self
174    }
175
176    /// Sets the custom state for the context.
177    pub fn state<T: Default>(self, state: T) -> TestCtxBuilder<Player, T, Trigger> {
178        TestCtxBuilder {
179            current_bloop: self.current_bloop,
180            state,
181            bloop_provider: self.bloop_provider,
182            trigger_registry: self.trigger_registry,
183        }
184    }
185
186    /// Sets the trigger registry for the context.
187    pub fn trigger_registry<T>(
188        self,
189        trigger_registry: TriggerRegistry<T>,
190    ) -> TestCtxBuilder<Player, State, T> {
191        TestCtxBuilder {
192            current_bloop: self.current_bloop,
193            state: self.state,
194            bloop_provider: self.bloop_provider,
195            trigger_registry,
196        }
197    }
198
199    /// Builds the achievement context from the configured components.
200    pub fn build(&mut self) -> AchievementContext<'_, Player, State, Trigger> {
201        AchievementContext::new(
202            &self.current_bloop,
203            &self.bloop_provider,
204            &self.state,
205            &mut self.trigger_registry,
206        )
207    }
208}