thing2 cut 2 fix batch: fixture load never writes, bless writer tested, MemState edges pinned

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 21:01:21 +01:00
commit f8c7af593e
6 changed files with 102 additions and 3 deletions

View file

@ -96,7 +96,8 @@ pub trait StatePort {
fn unsubscribe(&mut self, id: SubscriptionId);
/// Every change since the last drain that some live interest matches, in mutation order,
/// Every change since the last drain that some interest matched when it happened, in mutation
/// order (unsubscribing does not recall changes already queued),
/// each once.
fn drain(&mut self) -> Vec<Change>;
}

View file

@ -83,8 +83,17 @@ impl Fixture {
/// The committed store, decoded through the contract.
pub fn load(self) -> FixtureSet {
let cache = store::open(&Self::dir().join(self.file_name()));
FixtureSet::new(cache.get_all().unwrap(), cache.get_all().unwrap(), cache.get_all().unwrap())
// Opening a store creates a lock file beside it, so open a copy: the fixtures directory
// is never written by a load.
let work = std::env::temp_dir().join(format!("thing-testkit-load-{}-{}", std::process::id(), self.file_name()));
std::fs::create_dir_all(&work).unwrap();
let copy = work.join(self.file_name());
std::fs::copy(Self::dir().join(self.file_name()), &copy).unwrap();
let cache = store::open(&copy);
let set = FixtureSet::new(cache.get_all().unwrap(), cache.get_all().unwrap(), cache.get_all().unwrap());
drop(cache);
let _ = std::fs::remove_dir_all(&work);
set
}
}

View file

@ -208,6 +208,8 @@ impl StatePort for MemState {
Some(value) => Lookup::Present(Entry { address: address.clone(), record: value.into_record() }),
None => Lookup::Absent,
}
// A schema outside the four contract arms reads Absent. Cut 4 replaces this with the provider-state
// answer once thing2:question:provider-state-value is ruled.
}, else Lookup::Absent)
}

View file

@ -161,3 +161,25 @@ fn a_state_holds_every_fixture_document_under_its_publisher() {
};
assert!(matches!(state.read(&at), Lookup::Present(_)));
}
#[test]
fn loading_a_fixture_writes_nothing_in_the_fixtures_directory() {
let listing = || {
let mut names: Vec<_> = std::fs::read_dir(Fixture::dir())
.unwrap()
.map(|entry| entry.unwrap().file_name().into_string().unwrap())
.collect();
names.sort();
names
};
let before = listing();
let mut permissions = std::fs::metadata(Fixture::dir()).unwrap().permissions();
let writable = permissions.clone();
permissions.set_readonly(true);
std::fs::set_permissions(Fixture::dir(), permissions).unwrap();
for fixture in Fixture::ALL {
load(fixture);
}
std::fs::set_permissions(Fixture::dir(), writable).unwrap();
assert_eq!(listing(), before);
}

View file

@ -57,3 +57,30 @@ fn read_returns_the_committed_bytes() {
std::fs::write(dir.join("r.bin"), b"abc").unwrap();
assert_eq!(golden::read(&dir, "r.bin"), b"abc");
}
/// Re-runs this binary with THING_BLESS=1 so the writer is observed without touching this
/// process's environment.
#[test]
fn blessing_writes_the_file_and_the_text() {
let dir = scratch("bless");
let status = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "bless_child", "--test-threads=1"])
.env("THING_BLESS", "1")
.env("THING_BLESS_CHILD_DIR", &dir)
.status()
.unwrap();
assert!(status.success());
assert_eq!(std::fs::read(dir.join("b.bin")).unwrap(), b"blessed");
assert_eq!(std::fs::read(dir.join("t.bin")).unwrap(), b"tb");
assert_eq!(std::fs::read(dir.join("t.txt")).unwrap(), b"tt");
}
/// The child half of the test above; does nothing in a normal run.
#[test]
fn bless_child() {
if let Ok(dir) = std::env::var("THING_BLESS_CHILD_DIR") {
let dir = PathBuf::from(dir);
golden::check(&dir, "b.bin", b"blessed");
golden::check_with_text(&dir, "t", b"tb", "tt");
}
}

View file

@ -265,3 +265,41 @@ fn unsubscribing_ends_only_that_interest() {
state.put(&provider_a, &panel);
assert!(state.drain().is_empty());
}
#[test]
fn unsubscribing_keeps_changes_already_queued() {
let one = surface("provider.a", "one");
let mut state = MemState::new();
let id = state.subscribe(Interest::Record(surface_at("provider.a", &one)));
state.put(&provider("provider.a"), &one);
state.unsubscribe(id);
assert_eq!(state.drain().len(), 1, "a change queued while the interest was live is still drained");
}
#[test]
fn a_list_mixes_refused_and_present_in_key_order() {
let a = surface("provider.a", "a");
let c = surface("provider.a", "c");
let b_at = address("provider.a", SURFACE, "thing:surface:provider.a.b");
let mut state = MemState::new();
state.put(&provider("provider.a"), &c);
state.put_raw(b_at.clone(), unknown_arm());
state.put(&provider("provider.a"), &a);
let kinds: Vec<(&str, String)> = state
.list(&prefix("provider.a", SURFACE, "thing:"))
.into_iter()
.map(|lookup| match lookup {
Lookup::Present(entry) => ("present", entry.address.key.as_str().to_string()),
Lookup::Refused(refusal) => ("refused", refusal.address.key.as_str().to_string()),
Lookup::Absent => panic!("a list holds no absence"),
})
.collect();
assert_eq!(
kinds,
[
("present", "thing:surface:provider.a.a".to_string()),
("refused", b_at.key.as_str().to_string()),
("present", "thing:surface:provider.a.c".to_string()),
]
);
}