Skip to content

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

test(modules): cover symlinked token import and register import_once tests

Steven Enamakel4182b20cdfc7
diff --git a/crates/openhuman-core/src/modules/import_once_tests.rs b/crates/openhuman-core/src/modules/import_once_tests.rs
new file mode 100644
index 0000000000..0b1c7b30f4
--- /dev/null
+++ b/crates/openhuman-core/src/modules/import_once_tests.rs
@@ -0,0 +1,68 @@
+use super::*;
+
+#[derive(Debug)]
+struct Done(bool);
+
+impl Settled for Done {
+ fn settled(&self) -> bool {
+ self.0
+ }
+}
+
+#[tokio::test]
+async fn a_concurrent_caller_waits_for_the_running_import() {
+ let dir = std::path::PathBuf::from("/import-once/concurrent");
+ let finished = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let flag = finished.clone();
+ let first = import_once("test-concurrent", MODULE_FOR_TEST, &dir, async move {
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
+ flag.store(true, std::sync::atomic::Ordering::SeqCst);
+ Ok(Done(true))
+ });
+ let flag = finished.clone();
+ let second = async {
+ tokio::time::sleep(std::time::Duration::from_millis(20)).await;
+ import_once("test-concurrent", MODULE_FOR_TEST, &dir, async {
+ // Only reached if the first import did not settle.
+ Ok(Done(true))
+ })
+ .await;
+ flag.load(std::sync::atomic::Ordering::SeqCst)
+ };
+ let (_, saw_finished) = tokio::join!(first, second);
+ assert!(saw_finished, "second caller returned before the import ended");
+}
+
+#[tokio::test]
+async fn an_unsettled_outcome_and_a_cancelled_import_are_retried() {
+ let dir = std::path::PathBuf::from("/import-once/retry");
+ let runs = std::sync::atomic::AtomicUsize::new(0);
+ let count = || runs.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
+ import_once("test-retry", MODULE_FOR_TEST, &dir, async {
+ count();
+ Ok(Done(false))
+ })
+ .await;
+ let cancelled = tokio::time::timeout(
+ std::time::Duration::from_millis(10),
+ import_once("test-retry", MODULE_FOR_TEST, &dir, async {
+ count();
+ std::future::pending::<anyhow::Result<Done>>().await
+ }),
+ )
+ .await;
+ assert!(cancelled.is_err());
+ import_once("test-retry", MODULE_FOR_TEST, &dir, async {
+ count();
+ Ok(Done(true))
+ })
+ .await;
+ import_once("test-retry", MODULE_FOR_TEST, &dir, async {
+ count();
+ Ok(Done(true))
+ })
+ .await;
+ assert_eq!(runs.load(std::sync::atomic::Ordering::SeqCst), 3);
+}
+
+const MODULE_FOR_TEST: &str = "tinyjuice";
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 9a4a1f08b7..6a0111ce1e 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -299,3 +299,7 @@ pub async fn import_legacy_tinyjuice(root: &std::path::Path, ttl: Option<std::ti
#[cfg(not(feature = "modules"))]
let _ = (root, ttl);
}
+
+#[cfg(all(test, feature = "modules"))]
+#[path = "import_once_tests.rs"]
+mod import_once_tests;
diff --git a/crates/openhuman-core/src/modules/storage_import_tinyjuice_tests.rs b/crates/openhuman-core/src/modules/storage_import_tinyjuice_tests.rs
index 05511ed528..0df458643f 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinyjuice_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinyjuice_tests.rs
@@ -179,3 +179,25 @@ fn only_hex_tokens_of_the_hash_length_are_ccr_names() {
assert!(!is_token(name), "{name}");
}
}
+
+#[cfg(unix)]
+#[tokio::test]
+async fn a_symlinked_token_is_never_followed_into_storage() {
+ let outside = tempfile::tempdir().unwrap();
+ std::fs::write(outside.path().join("secret"), "credential bytes").unwrap();
+ let dir = tempfile::tempdir().unwrap();
+ std::os::unix::fs::symlink(outside.path().join("secret"), dir.path().join(FRESH)).unwrap();
+ let scoped = scoped();
+ let outcome = import_into(&scoped, dir.path(), None, SystemTime::now())
+ .await
+ .unwrap();
+ assert!(matches!(
+ outcome,
+ Outcome::Imported {
+ imported: 0,
+ skipped: 1,
+ ..
+ }
+ ));
+ assert_eq!(read(&scoped, FRESH).await, None);
+}