#![cfg(test)]
use crate::board::{BoardStore, Ticket, TicketPhase};
use std::path::PathBuf;
use std::sync::OnceLock;
static TEST_ROOT: OnceLock<PathBuf> = OnceLock::new();
fn test_root() -> &'static PathBuf {
TEST_ROOT.get_or_init(|| {
let tmp = tempfile::TempDir::new().expect("failed to create test temp dir");
let path = tmp.path().to_path_buf();
std::mem::forget(tmp);
path
})
}
pub async fn expect_ticket(store: &BoardStore, id: &str) -> Ticket {
store
.get_ticket(id)
.await
.expect("BoardStore::get_ticket query failed")
.expect("expected ticket to exist")
}
pub async fn expect_ticket_status(store: &BoardStore, id: &str) -> TicketPhase {
store
.get_ticket_status(id)
.await
.expect("BoardStore::get_ticket_status query failed")
.expect("expected ticket status to exist")
}
pub async fn init_test_stores() {
use crate::session::SessionStorage;
static INIT: tokio::sync::OnceCell<()> = tokio::sync::OnceCell::const_new();
INIT.get_or_init(|| async {
let _ = crate::config::CONFIG.try_set_storage_root(test_root().clone());
crate::session::SESSIONS
.set(
SessionStorage::new_global(test_root())
.await
.expect("failed to create SessionStorage for tests"),
)
.expect("SESSIONS already initialized by another path");
crate::board::BOARD
.set(
BoardStore::open(test_root())
.await
.expect("failed to create BoardStore for tests"),
)
.expect("BOARD already initialized by another path");
})
.await;
}