Skip to content

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

test(storage): add sqlite test support helper for scope move tests

Steven Enamakela25c7758246e
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index 48b9b15258..c2fe9aa64a 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -50,6 +50,8 @@ pub mod scope_move;
pub mod secrets;
pub mod sessions;
pub mod state_kv;
+#[cfg(test)]
+pub mod test_support;
pub use scope::{
ParsedScope, Principal, Scope, Tenant, scope_for_agent, scope_for_module, scope_for_profile,
diff --git a/crates/openhuman-core/src/storage/scope_move_tests.rs b/crates/openhuman-core/src/storage/scope_move_tests.rs
index 080b2eac27..0ffe7f5521 100644
--- a/crates/openhuman-core/src/storage/scope_move_tests.rs
+++ b/crates/openhuman-core/src/storage/scope_move_tests.rs
@@ -226,11 +226,16 @@ fn boot_import_moves_profile_v1_scope_and_keeps_source_unchanged() {
let dir = tempfile::tempdir().unwrap();
let target = Target::new(dir.path(), "alice");
let config_path = target.profile_dir.join("config.toml");
+ let destination_url = format!("sqlite:{}", target.storage_db().display());
std::fs::create_dir_all(config_path.parent().unwrap()).unwrap();
std::fs::write(&config_path, "# profile fixture\n").unwrap();
let config = crate::config::Config {
config_path,
workspace_dir: target.files(),
+ storage: crate::config::StorageConfig {
+ url: Some(destination_url.clone()),
+ ..Default::default()
+ },
..crate::config::Config::default()
};
@@ -256,9 +261,10 @@ fn boot_import_moves_profile_v1_scope_and_keeps_source_unchanged() {
let source_db = legacy::v1_db(&target);
let before = hash_file(&source_db);
- let destination_url = format!("sqlite:{}", target.storage_db().display());
- let backend = storage::block_on(async move { storage::open(&destination_url).await }).unwrap();
- let first = crate::storage::import::run_all(&backend, &config);
+ let destination = crate::storage::test_support::sqlite_backend_in(dir, destination_url)
+ .unwrap();
+ let backend = &destination.backend;
+ let first = crate::storage::import::run_all(backend, &config);
let moved = first
.iter()
.find(|report| report.domain == IMPORT_DOMAIN)
@@ -297,7 +303,7 @@ fn boot_import_moves_profile_v1_scope_and_keeps_source_unchanged() {
.await
})
.unwrap();
- let second = crate::storage::import::run_all(&backend, &config);
+ let second = crate::storage::import::run_all(backend, &config);
let outcome = second
.iter()
.find(|report| report.domain == IMPORT_DOMAIN)
diff --git a/crates/openhuman-core/src/storage/test_support.rs b/crates/openhuman-core/src/storage/test_support.rs
new file mode 100644
index 0000000000..5d29c16a93
--- /dev/null
+++ b/crates/openhuman-core/src/storage/test_support.rs
@@ -0,0 +1,52 @@
+//! Test-only storage backends backed by the real TSD testkit and drivers.
+
+use std::sync::Arc;
+
+use crate::storage::{StorageBackend, block_on_anyhow};
+
+/// A real TSD in-memory backend for tests that do not require conditional
+/// writes or persistence across backend instances.
+pub fn memory_backend() -> Arc<dyn StorageBackend> {
+ Arc::new(tinystoragedrivers_testkit::MemoryBackend::new())
+}
+
+/// An on-disk SQLite backend whose temporary directory remains alive as long
+/// as the fixture is retained. Use it for persistence, conditional-write,
+/// fence, and import-claim tests; the TSD memory backend does not emulate those
+/// capabilities.
+#[cfg(feature = "storage-sqlite")]
+pub struct SqliteBackend {
+ _directory: tempfile::TempDir,
+ /// The actual SQLite backend opened from [`Self::url`].
+ pub backend: Arc<dyn StorageBackend>,
+ /// URL suitable for configuring the same isolated SQLite database.
+ pub url: String,
+}
+
+/// Opens an isolated on-disk SQLite backend for tests that need its real
+/// cross-process conditions or durable state.
+#[cfg(feature = "storage-sqlite")]
+pub fn sqlite_backend() -> anyhow::Result<SqliteBackend> {
+ let directory = tempfile::tempdir()?;
+ let url = format!("sqlite:{}", directory.path().join("storage").display());
+ sqlite_backend_in(directory, url)
+}
+
+/// Opens a SQLite backend at `url`, retaining `directory` for the lifetime of
+/// the returned backend fixture.
+#[cfg(feature = "storage-sqlite")]
+pub fn sqlite_backend_in(
+ directory: tempfile::TempDir,
+ url: impl Into<String>,
+) -> anyhow::Result<SqliteBackend> {
+ let url = url.into();
+ let backend = block_on_anyhow({
+ let url = url.clone();
+ async move { crate::storage::open(&url).await }
+ })?;
+ Ok(SqliteBackend {
+ _directory: directory,
+ backend,
+ url,
+ })
+}