use super::*;
use crate::core::config::{alef_config_schema, write_alef_config_schema};
fn vendor_schema(base_dir: &Path, version: &str) -> PathBuf {
let path = base_dir.join(DEFAULT_SCHEMA_PATH);
write_alef_config_schema(&path, version).expect("schema writes");
path
}
#[test]
fn tree_without_a_vendored_schema_reports_nothing() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(find_stale_vendored_schema(dir.path(), "1.2.3").is_none());
}
#[test]
fn current_vendored_schema_reports_nothing() {
let dir = tempfile::tempdir().expect("tempdir");
vendor_schema(dir.path(), "1.2.3");
assert!(find_stale_vendored_schema(dir.path(), "1.2.3").is_none());
}
#[test]
fn schema_describing_a_different_surface_is_reported_and_gates_the_exit_code() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join(DEFAULT_SCHEMA_PATH);
std::fs::create_dir_all(path.parent().expect("schema has a parent")).expect("mkdir");
let mut schema = alef_config_schema("1.2.3").expect("schema generation succeeds");
schema
.as_object_mut()
.expect("schema root is an object")
.insert("properties".to_string(), serde_json::json!({}));
std::fs::write(&path, serde_json::to_string_pretty(&schema).expect("renders")).expect("writes");
let finding = find_stale_vendored_schema(dir.path(), "1.2.3").expect("shape drift is reported");
assert!(finding.describes_a_different_surface());
let lines = finding.report_lines();
assert_eq!(lines.len(), 2, "expected a heading and one path line, got: {lines:?}");
assert!(
lines[0].contains("describes a different alef.toml surface"),
"heading must name the actual problem, got: {}",
lines[0]
);
assert!(
lines[1].contains(DEFAULT_SCHEMA_PATH) && lines[1].contains("alef schema --output"),
"detail line must name the path and the remedy command, got: {}",
lines[1]
);
}
#[test]
fn unparseable_vendored_schema_gates_the_exit_code() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join(DEFAULT_SCHEMA_PATH);
std::fs::create_dir_all(path.parent().expect("schema has a parent")).expect("mkdir");
std::fs::write(&path, "{ truncated").expect("writes");
let finding = find_stale_vendored_schema(dir.path(), "1.2.3").expect("unparseable copy is reported");
assert!(finding.describes_a_different_surface());
}
#[test]
fn version_stamp_drift_is_reported_without_gating_the_exit_code() {
let dir = tempfile::tempdir().expect("tempdir");
vendor_schema(dir.path(), "1.2.3");
let finding = find_stale_vendored_schema(dir.path(), "1.2.4").expect("version stamp drift is reported");
assert!(!finding.describes_a_different_surface());
let lines = finding.report_lines();
assert_eq!(lines.len(), 2, "expected a heading and one path line, got: {lines:?}");
assert!(
lines[0].contains("informational"),
"heading must mark itself non-blocking, got: {}",
lines[0]
);
assert!(
lines[1].contains("stamped 1.2.3") && lines[1].contains("running alef 1.2.4"),
"detail line must name both versions, got: {}",
lines[1]
);
assert!(
lines[1].contains("alef schema --output"),
"detail line must name the remedy command, got: {}",
lines[1]
);
}