use std::path::Path;
use super::DataDir;
#[derive(Debug, Clone)]
pub struct MockDataDir {
pub(crate) temp_dir: std::sync::Arc<tempfile::TempDir>,
pub config_toml: super::ConfigToml,
pub keypair: pubky_common::crypto::Keypair,
}
impl MockDataDir {
pub fn new(
config_toml: super::ConfigToml,
keypair: Option<pubky_common::crypto::Keypair>,
) -> anyhow::Result<Self> {
let keypair = keypair.unwrap_or_else(pubky_common::crypto::Keypair::random);
Ok(Self {
temp_dir: std::sync::Arc::new(tempfile::TempDir::new()?),
config_toml,
keypair,
})
}
#[cfg(any(test, feature = "testing"))]
pub fn test() -> Self {
let config = super::ConfigToml::default_test_config();
let keypair = pubky_common::crypto::Keypair::from_secret(&[0; 32]);
Self::new(config, Some(keypair)).expect("failed to create MockDataDir")
}
}
impl Default for MockDataDir {
fn default() -> Self {
Self::test()
}
}
impl DataDir for MockDataDir {
fn path(&self) -> &Path {
self.temp_dir.path()
}
fn ensure_data_dir_exists_and_is_writable(&self) -> anyhow::Result<()> {
Ok(()) }
fn read_or_create_config_file(&self) -> anyhow::Result<super::ConfigToml> {
Ok(self.config_toml.clone())
}
fn read_or_create_keypair(&self) -> anyhow::Result<pubky_common::crypto::Keypair> {
Ok(self.keypair.clone())
}
}