use std::path::{Path, PathBuf};
use crate::assistant::grounding;
fn repo_path(relative: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../..")
.join(relative)
}
#[test]
fn every_grounding_embed_matches_its_authored_original() -> Result<(), String> {
for (name, embedded) in grounding::documents() {
let authored_path = repo_path(&format!("examples/assistant/resources/{name}"));
let authored = std::fs::read_to_string(&authored_path)
.map_err(|error| format!("cannot read {}: {error}", authored_path.display()))?;
if embedded != authored {
return Err(format!(
"the embedded grounding copy of {name} has drifted from \
examples/assistant/resources/{name}; sync it with: \
cp examples/assistant/resources/{name} \
crates/aion-server/assistant-embed/grounding/{name}"
));
}
}
Ok(())
}
#[test]
fn materialize_rewrites_every_document_owner_only() -> Result<(), String> {
let root = tempfile::tempdir().map_err(|error| error.to_string())?;
let dir = root.path().join("grounding");
std::fs::create_dir_all(&dir).map_err(|error| error.to_string())?;
std::fs::write(dir.join("AWL-REFERENCE.md"), "an older binary's truth")
.map_err(|error| error.to_string())?;
grounding::materialize(&dir)?;
for (name, content) in grounding::documents() {
let written = std::fs::read_to_string(dir.join(name))
.map_err(|error| format!("{name} was not written: {error}"))?;
if written != content {
return Err(format!("{name} on disk does not match the embed"));
}
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
let mode = std::fs::metadata(&dir)
.map_err(|error| error.to_string())?
.permissions()
.mode();
if mode & 0o077 != 0 {
return Err(format!(
"the grounding directory is not owner-only: {mode:o}"
));
}
}
Ok(())
}