use edifact_mapper::{DataDir, Mapper};
use std::path::PathBuf;
fn write_bundle(dir: &std::path::Path, fv: &str, built_by: Option<&str>) {
std::fs::create_dir_all(dir).unwrap();
let mut bundle = serde_json::json!({
"format_version": fv,
"bundle_version": 2,
"variants": {},
"bo4e_catalog": { "types": {} },
});
if let Some(v) = built_by {
bundle["built_by"] = serde_json::Value::String(v.to_string());
}
std::fs::write(
dir.join(format!("edifact-data-{fv}.bin")),
serde_json::to_vec(&bundle).unwrap(),
)
.unwrap();
}
fn tmp(name: &str) -> PathBuf {
let dir =
std::env::temp_dir().join(format!("edifact-provenance-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
dir
}
#[test]
fn a_bundle_from_another_release_is_refused_by_name() {
let dir = tmp("mismatch");
write_bundle(&dir, "FV2604", Some("0.0.1-from-another-era"));
let text = match Mapper::from_data_dir(DataDir::path(&dir).eager(&["FV2604"])) {
Err(e) => e.to_string(),
Ok(_) => panic!("a bundle from another release must not load silently"),
};
assert!(
text.contains("0.0.1-from-another-era"),
"the error does not say which release produced the bundle: {text}"
);
assert!(
text.contains(env!("CARGO_PKG_VERSION")),
"the error does not say which release the crate expects: {text}"
);
}
#[test]
fn a_bundle_without_provenance_is_refused() {
let dir = tmp("unstamped");
write_bundle(&dir, "FV2604", None);
let text = match Mapper::from_data_dir(DataDir::path(&dir).eager(&["FV2604"])) {
Err(e) => e.to_string(),
Ok(_) => panic!("an unstamped bundle must not load silently"),
};
assert!(
text.to_lowercase().contains("bundle"),
"the error does not mention the bundle: {text}"
);
}
#[test]
fn a_mismatched_bundle_loads_when_the_caller_opts_in() {
let dir = tmp("optin");
write_bundle(&dir, "FV2604", Some("0.0.1-from-another-era"));
assert!(
Mapper::from_data_dir(
DataDir::path(&dir)
.allow_bundle_from_other_release(true)
.eager(&["FV2604"]),
)
.is_ok(),
"an explicit opt-in should load the bundle"
);
}
#[test]
fn the_committed_bundles_match_this_crate() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.unwrap()
.to_path_buf();
if !root.join("dist/edifact-data-FV2604.bin").exists() {
eprintln!("skipping: build the bundles first");
return;
}
assert!(
Mapper::from_data_dir(DataDir::path(root.join("dist")).eager(&["FV2604"])).is_ok(),
"the committed bundle should be from this release"
);
}