// Common test utilities. use std::path::PathBuf; use nutrition_mcp::db::{self, DbPool}; /// A temporary SQLite database for testing. /// Creates a unique DB file in /tmp that is cleaned up on drop. pub struct TestDb { pub pool: DbPool, _path: PathBuf, } impl TestDb { pub async fn new() -> Self { let path = std::env::temp_dir().join(format!( "nutrition-test-{}-{}.db", std::process::id(), uuid::Uuid::new_v4() )); let pool = db::init_database(&path).await.expect("failed to init test DB"); Self { pool, _path: path, } } } impl Drop for TestDb { fn drop(&mut self) { // Best-effort cleanup — ignore errors let _ = std::fs::remove_file(&self._path); let wal = self._path.with_extension("db-wal"); let shm = self._path.with_extension("db-shm"); let _ = std::fs::remove_file(&wal); let _ = std::fs::remove_file(&shm); } }