use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tempfile::TempDir;
use macrame::util::clock::FakeClock;
use macrame::Database;
pub struct TestHarness {
#[allow(dead_code)]
pub temp_dir: TempDir,
pub db_path: std::path::PathBuf,
pub clock: Arc<FakeClock>,
}
impl Default for TestHarness {
fn default() -> Self {
Self::new()
}
}
impl TestHarness {
pub fn new() -> Self {
Self::starting_at(SystemTime::UNIX_EPOCH)
}
pub fn starting_at(initial: SystemTime) -> Self {
let temp_dir = TempDir::new().expect("failed to create temp dir");
let db_path = temp_dir.path().join("test_macrame.db");
let clock = Arc::new(FakeClock::new(initial));
Self {
temp_dir,
db_path,
clock,
}
}
#[allow(dead_code)] pub async fn db_with_fake_clock(&self) -> Database {
Database::open_with_clock(&self.db_path, None, Arc::clone(&self.clock) as _)
.await
.expect("failed to open database with the fake clock")
}
#[allow(dead_code)]
pub fn advance(&self, d: Duration) {
self.clock.advance(d);
}
}