Skip to content

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

chore(tinymemes): add docs module tests

Steven Enamakel0b5d1182cf5f
diff --git a/crates/openhuman-core/src/tinymemes/docs.rs b/crates/openhuman-core/src/tinymemes/docs.rs
index 40da050cb1..869c0d48b0 100644
--- a/crates/openhuman-core/src/tinymemes/docs.rs
+++ b/crates/openhuman-core/src/tinymemes/docs.rs
@@ -63,8 +63,13 @@ pub(super) fn load(config: &Config, kind: IndexKind) -> Result<Option<String>> {
return Ok(text(&stored.doc, FIELD).map(str::to_owned));
}
let path = legacy_path(&config.workspace_dir, kind);
- let Ok(legacy) = std::fs::read_to_string(&path) else {
- return Ok(None);
+ // Only an absent file means "nothing to adopt". Any other failure
+ // (permissions, bad UTF-8) is an error: answering `None` would let the
+ // module start empty and save over state the old file still holds.
+ let legacy = match std::fs::read_to_string(&path) {
+ Ok(legacy) => legacy,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(e) => return Err(anyhow!("read {}: {e}", path.display())),
};
// `Absent`: a concurrent save wins over the old file's content.
let adopted = legacy.clone();
@@ -91,10 +96,14 @@ pub(super) fn load(config: &Config, kind: IndexKind) -> Result<Option<String>> {
}
Ok(Some(legacy))
}
- // Somebody stored a document in between; theirs is the truth.
- Err(_) => {
+ // Somebody stored a document in between; theirs is the truth. When no
+ // document exists the put failed for a real reason, which is reported.
+ Err(error) => {
let stored = repo.run(move |docs| async move { docs.get(COLLECTION, id).await })?;
- Ok(stored.and_then(|s| text(&s.doc, FIELD).map(str::to_owned)))
+ match stored {
+ Some(stored) => Ok(text(&stored.doc, FIELD).map(str::to_owned)),
+ None => Err(error),
+ }
}
}
}
diff --git a/crates/openhuman-core/src/tinymemes/docs_tests.rs b/crates/openhuman-core/src/tinymemes/docs_tests.rs
index 5e2713eb87..d8187b737f 100644
--- a/crates/openhuman-core/src/tinymemes/docs_tests.rs
+++ b/crates/openhuman-core/src/tinymemes/docs_tests.rs
@@ -84,3 +84,36 @@ fn a_stored_document_wins_over_a_legacy_file() {
);
assert!(legacy_path(&config.workspace_dir, IndexKind::Memes).exists());
}
+
+#[cfg(unix)]
+#[test]
+fn an_unreadable_legacy_file_is_an_error_not_an_empty_start() {
+ use std::os::unix::fs::PermissionsExt;
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ write_legacy(&config, IndexKind::Remixed, "[\"keep\"]");
+ let path = legacy_path(&config.workspace_dir, IndexKind::Remixed);
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
+ if std::fs::read_to_string(&path).is_ok() {
+ // Running as a user who can read anything (root): nothing to assert.
+ return;
+ }
+ assert!(load(&config, IndexKind::Remixed).is_err());
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
+ assert_eq!(
+ load(&config, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"keep\"]"),
+ "the state is still there once the file can be read"
+ );
+}
+
+#[test]
+fn an_invalid_utf8_legacy_file_is_an_error() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ let path = legacy_path(&config.workspace_dir, IndexKind::Slang);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(&path, [0xff, 0xfe, 0x00]).unwrap();
+ assert!(load(&config, IndexKind::Slang).is_err());
+ assert!(path.exists(), "an unreadable file is left where it is");
+}