Huginn/crates/mind-body/src/control/tests.rs
Meta Vens 6df3255330 Delete the run opener, the work queue and the breaker (C2 follow-up, Soul F3)
Map section 7 keeps no dormant switch in main: Self-run launching is parked
at parked/self-run-launch. open_and_launch was the only code that opened a
SelfRun run and nothing called it in production.

launch.rs keeps the systemd door (Launcher, SystemdLauncher, unit names,
the brake's stop) and the two reads self_run uses, holders and closure.
Deleted: open_and_launch, Ports, LaunchRequest, LaunchOutcome, Declined,
slot, START_GRACE_S, run_label, the dead-run closer and the breaker
question, with their tests.

queue.rs keeps MindPort, HuginnMind, the paged reads and on_hold, which
self_run's inbox reads. Deleted: queue, breaker, Breaker, CONSECUTIVE,
WINDOW, EMPTY_SHARE, live_claims, claims_of, with their tests.

control: Grant::for_test and the launch-gate tests went with the gate.
testkit: the helpers only those tests used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 22:30:50 +01:00

192 lines
7.9 KiB
Rust

//! The reader's tests, in the crate because they move the reader's root with
//! the `#[cfg(test)]` seam: no caller outside the crate can. Every test works in
//! a temporary directory through the same file door the units use; nothing
//! touches `/etc`. They run as root in the verify container like the writer's.
use std::path::{Path, PathBuf};
use chrono::{DateTime, TimeZone, Utc};
use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore};
use eureka_pipeline::Slug;
use super::*;
fn at(seconds: i64) -> DateTime<Utc> {
Utc.timestamp_opt(1_800_000_000 + seconds, 0).unwrap()
}
/// The store of the instance `eureka` under the root `dir`.
fn store_path(dir: &Path) -> PathBuf {
dir.join("eureka").join("control.cc")
}
fn held(reason: HeldReason) -> Effective {
Effective::Held { reason }
}
/// The real reader's answer for the store at `<root>/<instance>/control.cc`,
/// with the reader's root moved to `<root>` on this thread.
fn effective(path: &Path) -> Effective {
let directory = path.parent().unwrap();
let instance = Slug(directory.file_name().unwrap().to_str().unwrap().into());
with_root(directory.parent().unwrap(), || read_effective(&instance))
}
#[test]
fn an_absent_store_reads_as_held() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(effective(&store_path(dir.path())), held(HeldReason::BrakeAbsent));
assert!(!store_path(dir.path()).exists(), "reading must not create the store");
}
#[test]
fn a_path_that_cannot_be_read_as_a_file_reads_as_held() {
let dir = tempfile::tempdir().unwrap();
// A directory sits where the store should be: it exists and cannot be read as a file.
let path = store_path(dir.path());
std::fs::create_dir_all(&path).unwrap();
assert_eq!(effective(&path), held(HeldReason::Undecodable));
}
#[test]
fn a_brake_payload_that_does_not_decode_makes_the_store_undecodable() {
// Same type id as the brake, a different shape: the payload cannot decode as a Brake.
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "eureka.control.brake.v1", schema = "ControlBrake")]
struct WrongBrake {
#[cultcache(key = 0)]
released: String,
}
let dir = tempfile::tempdir().unwrap();
let path = store_path(dir.path());
let mut cache = CultCache::new();
cache.register_entry_type::<WrongBrake>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.put(BRAKE_KEY, &WrongBrake { released: "yes".into() }).unwrap();
assert_eq!(effective(&path), held(HeldReason::Undecodable));
}
/// The swarm store with its brake released, and nothing else in it.
fn release_swarm(root: &Path) {
SwarmWriter::open(swarm_path(root)).unwrap().set_brake(true, at(0), "op").unwrap();
}
/// A written instance store with its brake released, and a released swarm.
fn released_store(dir: &Path) -> PathBuf {
let path = store_path(dir);
ControlWriter::open(&path).unwrap().set_brake(true, at(0), "op").unwrap();
release_swarm(dir);
path
}
#[test]
fn both_brakes_released_is_a_grant_for_the_instance_and_nothing_else() {
let dir = tempfile::tempdir().unwrap();
let path = released_store(dir.path());
let Effective::Released(grant) = effective(&path) else { panic!("held") };
assert_eq!(grant.instance(), &Slug("eureka".into()));
}
#[test]
fn either_held_brake_or_an_absent_one_holds_with_its_own_reason() {
let dir = tempfile::tempdir().unwrap();
let path = released_store(dir.path());
SwarmWriter::open(swarm_path(dir.path())).unwrap().set_brake(false, at(1), "op").unwrap();
assert_eq!(effective(&path), held(HeldReason::BrakeHeld), "a held swarm brake stops every instance");
release_swarm(dir.path());
ControlWriter::open(&path).unwrap().set_brake(false, at(2), "op").unwrap();
assert_eq!(effective(&path), held(HeldReason::BrakeHeld), "a held instance brake stops her");
let other = tempfile::tempdir().unwrap();
ControlWriter::open(store_path(other.path())).unwrap().set_brake(true, at(0), "op").unwrap();
assert_eq!(effective(&store_path(other.path())), held(HeldReason::BrakeAbsent), "no swarm store is no swarm brake");
}
/// A document of the dollar-metering days, standing in for what an old `swarm.cc` holds.
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "eureka.control.swarm_dial.v1", schema = "OldSwarmDial")]
struct OldDial {
#[cultcache(key = 0)]
heat: String,
}
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "eureka.control.share.v1", schema = "OldShare")]
struct OldShare {
#[cultcache(key = 0)]
weight: String,
}
#[test]
fn an_old_swarm_store_holding_a_dial_or_a_share_reads_as_held_undecodable_and_is_never_migrated() {
for old in ["dial", "share"] {
let dir = tempfile::tempdir().unwrap();
let path = released_store(dir.path());
let mut cache = CultCache::new();
cache.register_entry_type::<Brake>().unwrap();
cache.register_entry_type::<OldDial>().unwrap();
cache.register_entry_type::<OldShare>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(swarm_path(dir.path()))).unwrap();
cache.pull_all_backing_stores().unwrap();
match old {
"dial" => {
cache.put("dial", &OldDial { heat: "1".into() }).unwrap();
}
_ => {
cache.put("eureka", &OldShare { weight: "1".into() }).unwrap();
}
}
assert_eq!(effective(&path), held(HeldReason::Undecodable), "{old}: the brake in the same file does not rescue it");
assert_eq!(read_swarm_for_test(dir.path()), SwarmEffective::Held(HeldReason::Undecodable));
}
}
fn read_swarm_for_test(root: &Path) -> SwarmEffective {
with_root(root, read_envelope)
}
#[test]
fn the_stores_are_written_world_readable_and_root_only_writable() {
let dir = tempfile::tempdir().unwrap();
let path = released_store(dir.path());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
for file in [path.as_path(), swarm_path(dir.path()).as_path()] {
assert_eq!(std::fs::metadata(file).unwrap().permissions().mode() & 0o777, 0o644, "{}", file.display());
}
}
#[cfg(not(unix))]
let _ = path;
}
#[test]
fn a_writer_refuses_to_open_over_a_store_holding_a_record_it_does_not_read() {
let dir = tempfile::tempdir().unwrap();
let path = store_path(dir.path());
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut cache = CultCache::new();
cache.register_entry_type::<OldDial>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.put("dial", &OldDial { heat: "1".into() }).unwrap();
let before = std::fs::read(&path).unwrap();
let refusal = ControlWriter::open(&path).err().expect("a foreign record refuses the open").to_string();
assert!(refusal.contains("does not read"), "{refusal}");
assert_eq!(std::fs::read(&path).unwrap(), before, "the refused open leaves the store bytes alone");
}
#[test]
fn a_swarm_writer_refuses_to_open_over_a_store_holding_a_record_it_does_not_read() {
let dir = tempfile::tempdir().unwrap();
let path = swarm_path(dir.path());
let mut cache = CultCache::new();
cache.register_entry_type::<OldDial>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.put("dial", &OldDial { heat: "1".into() }).unwrap();
let before = std::fs::read(&path).unwrap();
let refusal = SwarmWriter::open(&path).err().expect("a foreign record refuses the open").to_string();
assert!(refusal.contains("does not read"), "{refusal}");
assert_eq!(std::fs::read(&path).unwrap(), before, "the refused open leaves the store bytes alone");
}