Watch
0
0
Fork
You've already forked hyperhive
0

hive-agent: compact tests set the harness dir like the other Bus tests

The two report_compact tests build a Bus, which resolves the events db under
HYPERHIVE_HARNESS_DIR and panics when it is unset, as in the nix test
sandbox. Add a test-only paths::with_harness_dir that points the variable at
a temp dir under a lock, and use it in both the new tests and the existing
swarm_inference Bus test, which set the variable unguarded.
This commit is contained in:
atlas 2026-10-11 02:17:16 +02:00 • committed by mara
commit 3c03d82858
3 changed files with 69 additions and 37 deletions

View file

@ -131,3 +131,38 @@ pub fn claude_dir() -> PathBuf {
} }
PathBuf::from("/root/.claude") PathBuf::from("/root/.claude")
} }
/// Run `f` with `HYPERHIVE_HARNESS_DIR` pointing at a fresh temp dir, then
/// restore the previous value. Tests that build a `Bus` or read the harness
/// state files go through this: the variable is process-wide, so the calls
/// are serialised on a lock.
#[cfg(test)]
pub fn with_harness_dir<T>(f: impl FnOnce() -> T) -> T {
use std::ffi::OsString;
use std::sync::Mutex;
static LOCK: Mutex<()> = Mutex::new(());
struct Restore(Option<OsString>);
impl Drop for Restore {
fn drop(&mut self) {
// SAFETY: still holding `LOCK`, the only writer of this variable.
unsafe {
match self.0.take() {
Some(v) => std::env::set_var("HYPERHIVE_HARNESS_DIR", v),
None => std::env::remove_var("HYPERHIVE_HARNESS_DIR"),
}
}
}
}
let _lock = LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let dir = tempfile::tempdir().unwrap();
// Declared after `_lock` so the variable is restored before the lock drops.
let _restore = Restore(std::env::var_os("HYPERHIVE_HARNESS_DIR"));
// SAFETY: serialised by `LOCK`.
unsafe { std::env::set_var("HYPERHIVE_HARNESS_DIR", dir.path()) };
f()
}

View file

@ -320,10 +320,8 @@ mod tests {
/// persisted files, so clearing it, or a restart, finds the local value. /// persisted files, so clearing it, or a restart, finds the local value.
#[test] #[test]
fn the_swarm_value_is_not_persisted_over_the_local_one() { fn the_swarm_value_is_not_persisted_over_the_local_one() {
let dir = tempfile::tempdir().unwrap(); let (swarm, swarm_sources, files, cleared, cleared_sources) =
let prev = std::env::var_os("HYPERHIVE_HARNESS_DIR"); crate::paths::with_harness_dir(|| {
// SAFETY: no other test in this crate reads or sets this variable.
unsafe { std::env::set_var("HYPERHIVE_HARNESS_DIR", dir.path()) };
let bus = Bus::new(); let bus = Bus::new();
bus.set_model("sonnet"); bus.set_model("sonnet");
bus.set_effort("low"); bus.set_effort("low");
@ -343,11 +341,8 @@ mod tests {
o.apply(&AgentInference::default(), &target); o.apply(&AgentInference::default(), &target);
let cleared = (bus.model(), bus.effort()); let cleared = (bus.model(), bus.effort());
let cleared_sources = (bus.model_source(), bus.effort_source()); let cleared_sources = (bus.model_source(), bus.effort_source());
match prev { (swarm, swarm_sources, files, cleared, cleared_sources)
// SAFETY: as above. });
Some(v) => unsafe { std::env::set_var("HYPERHIVE_HARNESS_DIR", v) },
None => unsafe { std::env::remove_var("HYPERHIVE_HARNESS_DIR") },
}
assert_eq!(swarm, pair("opus", "max")); assert_eq!(swarm, pair("opus", "max"));
assert_eq!(files, (Some("sonnet".to_owned()), Some("low".to_owned()))); assert_eq!(files, (Some("sonnet".to_owned()), Some("low".to_owned())));
assert_eq!(cleared, pair("sonnet", "low")); assert_eq!(cleared, pair("sonnet", "low"));

View file

@ -983,6 +983,7 @@ mod tests {
} }
fn compact_report(result: Result<(), &str>) -> (bool, Vec<String>) { fn compact_report(result: Result<(), &str>) -> (bool, Vec<String>) {
crate::paths::with_harness_dir(|| {
let bus = Bus::new(); let bus = Bus::new();
let mut rx = bus.subscribe(); let mut rx = bus.subscribe();
let ok = report_compact(&bus, result); let ok = report_compact(&bus, result);
@ -993,6 +994,7 @@ mod tests {
} }
} }
(ok, notes) (ok, notes)
})
} }
#[test] #[test]