Skip to content

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

test(storage): add unit tests for layout db, mod, plan, and state

Steven Enamakeld6f4aef0279d
diff --git a/crates/openhuman-core/src/storage/layout/db_tests.rs b/crates/openhuman-core/src/storage/layout/db_tests.rs
index 4563e55b7b..f8ca3097aa 100644
--- a/crates/openhuman-core/src/storage/layout/db_tests.rs
+++ b/crates/openhuman-core/src/storage/layout/db_tests.rs
@@ -1 +1,54 @@
use super::*;
+use crate::storage::layout::test_fixtures::{fixture_docs, v1_profile};
+use crate::storage::layout::{legacy, Target};
+use tempfile::TempDir;
+
+#[test]
+fn the_copy_has_the_sources_counts_and_leaves_the_source_alone() {
+ let fx = v1_profile();
+ let source = legacy::v1_db(&fx.target);
+ checkpoint(&source).unwrap();
+ let before = std::fs::read(&source).unwrap();
+ let dest = fx.target.storage_db();
+ let copied = copy(&source, &dest).unwrap();
+ assert!(!copied.reused);
+ assert_eq!(copied.counts, table_counts(&source).unwrap());
+ assert_eq!(copied.counts, table_counts(&dest).unwrap());
+ assert!(!dest.with_file_name("storage.db.tmp").exists());
+ assert_eq!(std::fs::read(&source).unwrap(), before, "the source is read-only to the copy");
+ assert_eq!(fixture_docs(&dest).len(), 3);
+}
+
+#[test]
+fn a_matching_copy_is_kept_and_a_stale_one_replaced() {
+ let fx = v1_profile();
+ let source = legacy::v1_db(&fx.target);
+ let dest = fx.target.storage_db();
+ copy(&source, &dest).unwrap();
+ assert!(copy(&source, &dest).unwrap().reused);
+ // a leftover that does not match the source is rebuilt
+ std::fs::remove_file(&dest).unwrap();
+ let stale = Connection::open(&dest).unwrap();
+ stale.execute_batch("CREATE TABLE junk (x)").unwrap();
+ drop(stale);
+ let again = copy(&source, &dest).unwrap();
+ assert!(!again.reused);
+ assert_eq!(table_counts(&dest).unwrap(), table_counts(&source).unwrap());
+}
+
+#[test]
+fn a_count_mismatch_names_the_table() {
+ let a: Counts = [("docs".to_owned(), 3), ("blobs".to_owned(), 1)].into();
+ let b: Counts = [("docs".to_owned(), 2), ("blobs".to_owned(), 1)].into();
+ let diff = mismatch(&a, &b).unwrap();
+ assert!(diff.contains("docs"), "{diff}");
+ assert!(!diff.contains("blobs"), "{diff}");
+ assert!(mismatch(&a, &a).is_none());
+}
+
+#[test]
+fn a_missing_source_is_an_error() {
+ let dir = TempDir::new().unwrap();
+ let target = Target::new(dir.path(), "u1");
+ assert!(copy(&dir.path().join("nope.db"), &target.storage_db()).is_err());
+}
diff --git a/crates/openhuman-core/src/storage/layout/mod_tests.rs b/crates/openhuman-core/src/storage/layout/mod_tests.rs
index 4563e55b7b..21ba42a3bd 100644
--- a/crates/openhuman-core/src/storage/layout/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/layout/mod_tests.rs
@@ -1 +1,26 @@
use super::*;
+use crate::config::Config;
+
+#[test]
+fn a_profile_config_yields_its_target() {
+ let root = std::path::Path::new("/r/.openhuman");
+ let config = Config {
+ config_path: root.join("users").join("abc").join("config.toml"),
+ ..Config::default()
+ };
+ let target = Target::from_config(&config).unwrap();
+ assert_eq!(target, Target::new(root, "abc"));
+ assert_eq!(target.workspace(), root.join("users/abc/workspace"));
+ assert_eq!(target.files(), root.join("users/abc/files"));
+ assert_eq!(target.storage_db(), root.join("users/abc/storage.db"));
+ assert_eq!(target.backups(), root.join("backups"));
+}
+
+#[test]
+fn a_config_outside_users_has_no_layout() {
+ let config = Config {
+ config_path: std::path::PathBuf::from("/somewhere/else/config.toml"),
+ ..Config::default()
+ };
+ assert!(Target::from_config(&config).is_none());
+}
diff --git a/crates/openhuman-core/src/storage/layout/plan_tests.rs b/crates/openhuman-core/src/storage/layout/plan_tests.rs
index 4563e55b7b..db0e4b004d 100644
--- a/crates/openhuman-core/src/storage/layout/plan_tests.rs
+++ b/crates/openhuman-core/src/storage/layout/plan_tests.rs
@@ -1 +1,78 @@
use super::*;
+use crate::storage::layout::test_fixtures::{v1_profile, write};
+
+#[test]
+fn user_files_move_and_imported_state_is_deleted() {
+ assert_eq!(classify_top("skills").map(|c| c.0), Some(Action::Move));
+ assert_eq!(classify_top("attachments").map(|c| c.0), Some(Action::Move));
+ assert_eq!(classify_top("artifacts").map(|c| c.0), Some(Action::Move));
+ assert_eq!(classify_top("STYLE.md").map(|c| c.0), Some(Action::Move));
+ assert_eq!(classify_top("storage").map(|c| c.0), Some(Action::Delete));
+ assert_eq!(classify_top("session_raw").map(|c| c.0), Some(Action::Delete));
+ assert_eq!(classify_top("who-knows"), None);
+}
+
+#[test]
+fn memory_is_split_one_level_down() {
+ assert_eq!(classify_memory_child("jobs.json", false).0, Action::Delete);
+ assert_eq!(classify_memory_child("conversations", true).0, Action::Delete);
+ assert_eq!(classify_memory_child("legacy_claims", true).0, Action::Move);
+ // a directory that merely looks like a state file is not one
+ assert_eq!(classify_memory_child("x.json", true).0, Action::Move);
+}
+
+#[test]
+fn scan_classifies_a_v1_workspace_and_totals_it() {
+ let fx = v1_profile();
+ let plan = scan(&fx.workspace()).unwrap();
+ let by = |p: &str| plan.entries.iter().find(|e| e.path == p).unwrap_or_else(|| panic!("no {p}"));
+ assert_eq!(by("skills").action, Action::Move);
+ assert_eq!(by("state").action, Action::Move);
+ assert_eq!(by("memory/legacy_claims").action, Action::Move);
+ assert_eq!(by("memory/jobs.json").action, Action::Delete);
+ assert_eq!(by("memory/conversations").action, Action::Delete);
+ assert_eq!(by("storage").action, Action::Delete);
+ assert_eq!(by("mystery").action, Action::Unknown);
+ assert_eq!(by("notes.tmp").action, Action::Unknown);
+ assert!(plan.entries.iter().all(|e| e.path != "memory"), "memory is classified by child");
+ let unknown = plan.totals(Action::Unknown);
+ assert_eq!((unknown.entries, unknown.files), (2, 2));
+ assert_eq!(unknown.bytes, "???".len() as u64 + "scratch".len() as u64);
+ let archive = plan.archive_totals();
+ assert_eq!(
+ archive.files,
+ plan.totals(Action::Move).files + plan.totals(Action::Delete).files
+ );
+ // sorted by path
+ let paths: Vec<_> = plan.entries.iter().map(|e| e.path.clone()).collect();
+ let mut sorted = paths.clone();
+ sorted.sort();
+ assert_eq!(paths, sorted);
+}
+
+#[test]
+fn a_missing_workspace_is_an_empty_plan() {
+ let dir = tempfile::TempDir::new().unwrap();
+ assert!(scan(&dir.path().join("workspace")).unwrap().entries.is_empty());
+}
+
+#[test]
+fn measure_counts_files_and_bytes_without_following_symlinks() {
+ let dir = tempfile::TempDir::new().unwrap();
+ write(&dir.path().join("a/one.txt"), "12345");
+ write(&dir.path().join("a/b/two.txt"), "123");
+ #[cfg(unix)]
+ std::os::unix::fs::symlink(dir.path().join("a"), dir.path().join("a/loop")).unwrap();
+ let (files, bytes) = measure(&dir.path().join("a"));
+ #[cfg(unix)]
+ assert_eq!(files, 3, "two files and the symlink itself");
+ #[cfg(not(unix))]
+ assert_eq!(files, 2);
+ assert!(bytes >= 8);
+}
+
+#[test]
+fn join_splits_the_relative_path() {
+ let base = std::path::Path::new("/x/ws");
+ assert_eq!(join(base, "memory/jobs.json"), base.join("memory").join("jobs.json"));
+}
diff --git a/crates/openhuman-core/src/storage/layout/state_tests.rs b/crates/openhuman-core/src/storage/layout/state_tests.rs
index 4563e55b7b..f29e3f7242 100644
--- a/crates/openhuman-core/src/storage/layout/state_tests.rs
+++ b/crates/openhuman-core/src/storage/layout/state_tests.rs
@@ -1 +1,43 @@
use super::*;
+use tempfile::TempDir;
+
+#[test]
+fn a_second_migrator_is_refused_while_the_first_holds_the_lock() {
+ let dir = TempDir::new().unwrap();
+ let first = Lock::acquire(dir.path()).unwrap();
+ let second = Lock::acquire(dir.path());
+ assert!(matches!(second, Err(LayoutError::Locked(_))), "{second:?}");
+ drop(first);
+ Lock::acquire(dir.path()).expect("the lock is free once the holder drops it");
+}
+
+#[test]
+fn state_round_trips_and_records_each_step_once() {
+ let dir = TempDir::new().unwrap();
+ assert!(LayoutState::load(dir.path()).unwrap().is_none());
+ let mut state = LayoutState::new("u/1 weird", 1_700_000_000_000);
+ assert!(state.archive.starts_with("u_1_weird-2023"), "{}", state.archive);
+ assert!(state.archive.ends_with(".tar.zst"));
+ state.finish(Step::Import, dir.path()).unwrap();
+ state.finish(Step::Import, dir.path()).unwrap();
+ let loaded = LayoutState::load(dir.path()).unwrap().unwrap();
+ assert_eq!(loaded.done, vec![Step::Import]);
+ assert!(loaded.has(Step::Import) && !loaded.has(Step::Migrate));
+ LayoutState::clear(dir.path()).unwrap();
+ LayoutState::clear(dir.path()).unwrap();
+ assert!(LayoutState::load(dir.path()).unwrap().is_none());
+}
+
+#[test]
+fn a_damaged_state_file_is_an_error_not_a_fresh_start() {
+ let dir = TempDir::new().unwrap();
+ std::fs::write(dir.path().join(STATE_FILE), "{ nope").unwrap();
+ assert!(LayoutState::load(dir.path()).is_err());
+}
+
+#[test]
+fn steps_are_ordered() {
+ let mut sorted = Step::ALL;
+ sorted.sort();
+ assert_eq!(sorted, Step::ALL);
+}