Skip to main content

todoapp_core/
testing.rs

1//! Deterministic `Clock`/`IdGenerator` fixtures, store- and app-agnostic, so
2//! every crate's tests (app, conformance, adapters) can share one definition.
3
4use std::cell::RefCell;
5
6use crate::{Clock, Date, Id, IdGenerator, Timestamp};
7
8/// Sequential id generator: `t1`, `t2`, … Deterministic for tests.
9#[derive(Default)]
10pub struct SeqIds {
11    n: RefCell<u64>,
12}
13
14impl IdGenerator for SeqIds {
15    fn next_id(&self) -> Id {
16        let mut n = self.n.borrow_mut();
17        *n += 1;
18        Id::new(format!("t{n}"))
19    }
20}
21
22/// Clock pinned to a fixed instant and date — deterministic for tests.
23pub struct FixedClock {
24    pub now: Timestamp,
25    pub today: Date,
26}
27
28impl Default for FixedClock {
29    fn default() -> Self {
30        Self {
31            now: Timestamp::from_millisecond(0),
32            today: Date::parse("2026-06-22").unwrap(),
33        }
34    }
35}
36
37impl Clock for FixedClock {
38    fn now(&self) -> Timestamp {
39        self.now
40    }
41    fn today(&self) -> Date {
42        self.today
43    }
44}