Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(storage): adjust default backend logic for testing

Steven Enamakelb74df2e574a5
diff --git a/crates/openhuman-core/src/storage/local.rs b/crates/openhuman-core/src/storage/local.rs
index 24a597cf56..70e3407f4e 100644
--- a/crates/openhuman-core/src/storage/local.rs
+++ b/crates/openhuman-core/src/storage/local.rs
@@ -20,9 +20,9 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex, PoisonError};
-use anyhow::{anyhow, Context, Result};
+use anyhow::{Context, Result, anyhow};
-use super::{current_scope, installed, Scope, Storage, StorageBackend};
+use super::{Scope, Storage, StorageBackend, current_scope, installed};
/// A backend and the scope a store's records live under in it.
#[derive(Clone)]
@@ -53,6 +53,21 @@ type WorkspaceBackend = Arc<tokio::sync::OnceCell<Arc<dyn StorageBackend>>>;
static WORKSPACES: LazyLock<Mutex<HashMap<PathBuf, WorkspaceBackend>>> =
LazyLock::new(Mutex::default);
+#[cfg(test)]
+static TEST_WORKSPACE_URLS: LazyLock<Mutex<HashMap<PathBuf, String>>> =
+ LazyLock::new(Mutex::default);
+
+#[cfg(test)]
+pub(super) fn use_persistent_default_for_test(workspace_dir: &Path) {
+ TEST_WORKSPACE_URLS
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .insert(
+ workspace_dir.to_path_buf(),
+ super::config::default_url(workspace_dir),
+ );
+}
+
#[cfg(test)]
struct BackendOpenTestGate {
started: tokio::sync::oneshot::Sender<()>,
@@ -120,21 +135,49 @@ async fn open_default_backend(workspace_dir: PathBuf) -> Result<Arc<dyn StorageB
#[cfg(test)]
await_test_backend_open_gate().await;
- let url = super::config::default_url(&workspace_dir);
- super::config::prepare_default_dir(&workspace_dir).with_context(|| {
- format!(
- "create the storage directory for {}",
- workspace_dir.display()
- )
- })?;
- tracing::debug!(
- target: "openhuman::storage",
- workspace = %workspace_dir.display(),
- "[storage::local] opening the workspace default backend (none installed)"
- );
- super::open(&url)
- .await
- .with_context(|| format!("open the default storage for {}", workspace_dir.display()))
+ #[cfg(test)]
+ {
+ let persistent_url = TEST_WORKSPACE_URLS
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner)
+ .get(&workspace_dir)
+ .cloned();
+ if let Some(url) = persistent_url {
+ super::config::prepare_default_dir(&workspace_dir).with_context(|| {
+ format!(
+ "create the storage directory for {}",
+ workspace_dir.display()
+ )
+ })?;
+ return super::open(&url).await.with_context(|| {
+ format!("open the default storage for {}", workspace_dir.display())
+ });
+ }
+
+ let backend: Arc<dyn StorageBackend> =
+ Arc::new(tinystoragedrivers_testkit::MemoryBackend::new());
+ super::boot::prepare_and_migrate(&backend).await?;
+ return Ok(super::fenced_backend::wrap(backend));
+ }
+
+ #[cfg(not(test))]
+ {
+ let url = super::config::default_url(&workspace_dir);
+ super::config::prepare_default_dir(&workspace_dir).with_context(|| {
+ format!(
+ "create the storage directory for {}",
+ workspace_dir.display()
+ )
+ })?;
+ tracing::debug!(
+ target: "openhuman::storage",
+ workspace = %workspace_dir.display(),
+ "[storage::local] opening the workspace default backend (none installed)"
+ );
+ super::open(&url)
+ .await
+ .with_context(|| format!("open the default storage for {}", workspace_dir.display()))
+ }
}
async fn initialized_backend(
diff --git a/crates/openhuman-core/src/storage/local_tests.rs b/crates/openhuman-core/src/storage/local_tests.rs
index 118dcfefc5..9db02aa429 100644
--- a/crates/openhuman-core/src/storage/local_tests.rs
+++ b/crates/openhuman-core/src/storage/local_tests.rs
@@ -25,16 +25,15 @@ fn get_thing(opened: &Opened, id: &'static str) -> Option<i64> {
}
#[test]
-// Persistence test: this pins the host's real default driver and on-disk
-// workspace layout, so it intentionally opens the SQLite backend.
+// Ordinary unit test: the cfg(test) default must use the real driver testkit,
+// not create a workspace database as a side effect.
fn an_uninstalled_call_opens_the_default_for_its_workspace() {
let dir = tempfile::tempdir().unwrap();
let opened = open("things", dir.path()).unwrap();
- assert_eq!(opened.backend.driver(), "sqlite");
put_thing(&opened, "a", 1);
assert!(
- dir.path().join("storage").is_dir(),
- "the default lives in <workspace>/storage"
+ !dir.path().join("storage").exists(),
+ "ordinary unit tests use an in-memory testkit backend"
);
// The same workspace reuses the one backend.
let again = open("things", dir.path()).unwrap();
@@ -43,7 +42,7 @@ fn an_uninstalled_call_opens_the_default_for_its_workspace() {
}
#[test]
-// Persistence test: separate workspace defaults must remain isolated on disk.
+// Ordinary unit test: separate workspace defaults must remain isolated.
fn workspaces_do_not_share_a_default() {
let one = tempfile::tempdir().unwrap();
let two = tempfile::tempdir().unwrap();
@@ -58,6 +57,7 @@ fn workspaces_do_not_share_a_default() {
// reopen the same durable workspace data.
fn forgetting_a_workspace_reopens_the_same_data() {
let dir = tempfile::tempdir().unwrap();
+ super::use_persistent_default_for_test(dir.path());
let first = open("things", dir.path()).unwrap();
put_thing(&first, "kept", 3);
forget(dir.path());