use crate::types::lock::LockedSource;
use crate::types::report::Diagnostic;
use crate::types::resolve::FetchError;
use crate::types::resolve::SourceKey;
use crate::types::tree::ContentHash;
pub fn check_tree(
key: &SourceKey,
locked: &LockedSource,
fetched: &ContentHash,
lock_tree: &ContentHash,
) -> Result<(), Diagnostic> {
match locked {
LockedSource::Git { rev, .. } if fetched != lock_tree => Err(Diagnostic::new(
key.location(),
FetchError::TreeMismatch {
found: fetched.clone(),
rev: rev.clone(),
locked: lock_tree.clone(),
},
)),
LockedSource::Git { .. } | LockedSource::Path(_) => Ok(()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::name::PackName;
use crate::types::name::ToolName;
use crate::types::project::CommitId;
use crate::types::project::GitUrl;
use crate::types::project::LocalPath;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
fn git_source() -> Result<LockedSource, Box<dyn std::error::Error>> {
Ok(LockedSource::Git {
url: GitUrl::new("https://github.com/airsstack/agentstacks")?,
tag: None,
subdir: None,
rev: CommitId::new(REV)?,
})
}
fn refusal(
key: &SourceKey,
a: &ContentHash,
b: &ContentHash,
) -> Result<String, Box<dyn std::error::Error>> {
let result = check_tree(key, &git_source()?, a, b);
Ok(result.err().ok_or("expected a refusal")?.to_string())
}
#[test]
fn git_tree_mismatch_is_refused() -> TestResult {
let key = SourceKey::Pack(PackName::new("core")?);
let (a, b) = (ContentHash::of_bytes(b"a"), ContentHash::of_bytes(b"b"));
assert_eq!(
refusal(&key, &a, &b)?,
format!(
"agentstacks.toml: packs.core: tree sha256:{} at rev {REV} differs from the lock's sha256:{}",
a.hex(),
b.hex()
)
);
Ok(())
}
#[test]
fn removed_tool_tree_mismatch_is_refused() -> TestResult {
let key = SourceKey::Tool(ToolName::new("claude")?);
let (a, b) = (ContentHash::of_bytes(b"a"), ContentHash::of_bytes(b"b"));
assert_eq!(
refusal(&key, &a, &b)?,
format!(
"agentstacks.toml: tools.claude: tree sha256:{} at rev {REV} differs from the lock's sha256:{}",
a.hex(),
b.hex()
)
);
Ok(())
}
#[test]
fn git_tree_equal_passes() -> TestResult {
let key = SourceKey::Pack(PackName::new("core")?);
let hash = ContentHash::of_bytes(b"a");
assert!(check_tree(&key, &git_source()?, &hash, &hash.clone()).is_ok());
Ok(())
}
#[test]
fn path_tree_change_is_not_refused() -> TestResult {
let key = SourceKey::Pack(PackName::new("core")?);
let source = LockedSource::Path(LocalPath::new("packs/core")?);
let (a, b) = (ContentHash::of_bytes(b"a"), ContentHash::of_bytes(b"b"));
assert!(check_tree(&key, &source, &a, &b).is_ok());
Ok(())
}
}