use std::path::{Path, PathBuf};
use std::process::Command;
fn spec() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/conformance")
}
fn zk2(args: &[&str]) -> i32 {
Command::new(env!("CARGO_BIN_EXE_zk2"))
.args(args)
.output()
.expect("runs")
.status
.code()
.expect("exits")
}
fn p(rel: &str) -> String {
spec().join(rel).display().to_string()
}
#[test]
fn exit_codes() {
assert_eq!(
zk2(&["contract", "lint", &p("contracts/ok-minimal.toml")]),
0
);
assert_eq!(
zk2(&["contract", "lint", &p("contracts/e015-event.toml")]),
1
);
assert_eq!(
zk2(&["contract", "fingerprint", &p("contracts/ok-kinds.toml")]),
0
);
let c = |case: &str, side: &str| p(&format!("compat/contract/{case}/{side}.toml"));
assert_eq!(
zk2(&[
"contract",
"compat",
&c("optional-role-added", "old"),
&c("optional-role-added", "new")
]),
0
);
assert_eq!(
zk2(&[
"contract",
"compat",
&c("priority-changed", "old"),
&c("priority-changed", "new")
]),
1
);
assert_eq!(
zk2(&[
"contract",
"compat",
&c("resource-removed", "old"),
&c("resource-removed", "new")
]),
1
);
assert_eq!(zk2(&["contract", "check-history", &p("history/ok")]), 0);
assert_eq!(
zk2(&["contract", "check-history", &p("history/tampered")]),
1
);
assert_eq!(
zk2(&[
"contract",
"compat",
&p("bundles/valid.bundle.json"),
&p("bundles/valid.bundle.json")
]),
0
);
assert_eq!(zk2(&["contract", "nope"]), 2);
assert_eq!(zk2(&["contract", "fingerprint", "/nonexistent.toml"]), 1);
}
fn run(args: &[&str]) -> (i32, String) {
let out = Command::new(env!("CARGO_BIN_EXE_zk2"))
.args(args)
.output()
.expect("runs");
(
out.status.code().expect("exits"),
String::from_utf8(out.stdout).expect("UTF-8"),
)
}
#[test]
fn catalog_exit_codes() {
let dir = std::env::temp_dir().join(format!("zk2-catalog-cli-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let at = |name: &str| dir.join(name).display().to_string();
let modem = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../examples/zk2/zenoh-modem/modem.v3.toml")
.display()
.to_string();
let mut revisions: Vec<_> = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../spec/profiles/.history/health.v1")
.read_dir()
.unwrap()
.map(|e| e.unwrap().path())
.collect();
revisions.sort();
let health = revisions[0].display().to_string();
let (code, first) = run(&["catalog", "new", "--epoch", "1767225600", &modem]);
assert_eq!(code, 0);
std::fs::write(at("a.json"), &first).unwrap();
let (code, id_a) = run(&["catalog", "id", &at("a.json")]);
assert_eq!(code, 0);
assert_eq!(id_a.trim().len(), 8);
let (code, second) = run(&["catalog", "add", &at("a.json"), &health, &modem]);
assert_eq!(code, 0);
assert!(second.contains(id_a.trim()), "{second}");
std::fs::write(at("b.json"), &second).unwrap();
assert_eq!(
zk2(&[
"catalog",
"check",
&at("b.json"),
"--previous",
&at("a.json"),
&modem,
&health
]),
0
);
assert_eq!(
zk2(&[
"catalog",
"check",
&at("a.json"),
"--previous",
&at("b.json")
]),
1
);
assert_eq!(run(&["catalog", "add", &at("b.json"), &modem]), (0, second));
assert_eq!(zk2(&["catalog", "check", &at("a.json"), &health]), 1);
let (code, empty) = run(&["catalog", "new"]);
assert_eq!(code, 0);
assert_eq!(
empty.trim(),
r#"{"entries":[],"epoch":0,"format":"zk2-catalog/1"}"#
);
std::fs::write(
at("refused.json"),
r#"{"format": "zk2-catalog/1", "epoch": -0, "entries": []}"#,
)
.unwrap();
assert_eq!(zk2(&["catalog", "id", &at("refused.json")]), 1);
assert_eq!(zk2(&["catalog", "check", &at("refused.json")]), 1);
assert_eq!(zk2(&["catalog", "add", &at("refused.json"), &modem]), 1);
assert_eq!(zk2(&["catalog", "id", &at("missing.json")]), 2);
assert_eq!(zk2(&["catalog", "new", "--epoch", "-1"]), 2);
assert_eq!(zk2(&["catalog", "new", "--epoch", "01"]), 2);
assert_eq!(zk2(&["catalog", "new", "--epoch", "9007199254740992"]), 2);
assert_eq!(zk2(&["catalog", "new", &at("missing.toml")]), 2);
assert_eq!(zk2(&["catalog", "add", &at("a.json")]), 2);
assert_eq!(
zk2(&["catalog", "check", &at("a.json"), "--previus", "x"]),
2
);
let invalid = p("contracts/e015-event.toml");
assert_eq!(zk2(&["catalog", "new", &invalid]), 1);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn a_published_review_is_accepted() {
let profiles = Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/profiles");
let contract = profiles.join("health/health.v1.toml").display().to_string();
let history = profiles.join(".history").display().to_string();
let (code, out) = run(&["contract", "compat", "--history", &history, &contract]);
assert_eq!(code, 0, "{out}");
assert!(out.contains("same revision as a published one"), "{out}");
assert!(out.contains("review [uses_changed]"), "{out}");
assert!(out.contains("accepted: the revision is published"), "{out}");
let dir = std::env::temp_dir().join(format!("zk2-accepted-cli-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("health.v1")).unwrap();
let first = "e9dbcdcb2fc325ca3121fdcacf7badd0091f62d97ab4c7d7a5f022dd8d6671df.bundle.json";
std::fs::copy(
profiles.join(".history/health.v1").join(first),
dir.join("health.v1").join(first),
)
.unwrap();
let root = dir.display().to_string();
let (code, out) = run(&["contract", "compat", "--history", &root, &contract]);
assert_eq!(code, 1, "{out}");
assert!(out.contains("review [uses_changed]"), "{out}");
assert!(!out.contains("accepted"), "{out}");
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn config_derive_reproduces_the_sx1262s_derived_types() {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/zk2/zenoh-modem/config");
let contract = dir.join("sx1262_config.v1.toml").display().to_string();
let out = Command::new(env!("CARGO_BIN_EXE_zk2"))
.args(["config", "derive", &contract])
.output()
.expect("runs");
assert_eq!(
out.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let printed: serde_json::Value = serde_json::from_slice(&out.stdout).expect("JSON");
let packed: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(dir.join("schemas/sx1262_config.json")).expect("the packed file"),
)
.expect("JSON");
let names = |v: &serde_json::Value| {
let mut n: Vec<String> = v["types"]
.as_object()
.expect("types")
.keys()
.cloned()
.collect();
n.sort_unstable();
n
};
let derived = names(&printed);
assert_eq!(
derived,
["Control", "Error", "Record", "SetDuty", "SetRadio"],
"Record, a Set<T> per hot or reach group, Control and Error"
);
assert_eq!(
names(&packed),
[
"Control",
"Duty",
"Error",
"Modulation",
"Radio",
"Record",
"SetDuty",
"SetRadio"
],
"the three group types and the five derived ones"
);
for n in &derived {
assert_eq!(printed["types"][n], packed["types"][n], "{n}");
}
assert_eq!(zk2(&["contract", "lint", &contract]), 0);
}
#[test]
fn config_derive_writes_the_derived_types() {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../spec/profiles/config/conformance");
let contract = dir.join("contracts/rfcfg.v1.toml");
let out = Command::new(env!("CARGO_BIN_EXE_zk2"))
.args(["config", "derive", &contract.display().to_string()])
.output()
.expect("runs");
assert_eq!(
out.status.code(),
Some(0),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let printed: serde_json::Value = serde_json::from_slice(&out.stdout).expect("JSON");
let packed: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(dir.join("contracts/rfcfg.json")).expect("rfcfg.json"),
)
.expect("JSON");
let types = printed["types"].as_object().expect("types");
let names: Vec<&str> = types.keys().map(String::as_str).collect();
let mut want = vec![
"Control", "Error", "Record", "SetDuty", "SetLink", "SetQueue", "SetRadio", "SetSim",
];
want.sort_unstable();
let mut got = names.clone();
got.sort_unstable();
assert_eq!(got, want);
for n in names {
assert_eq!(printed["types"][n], packed["types"][n], "{n}");
}
assert_eq!(
zk2(&["contract", "lint", &contract.display().to_string()]),
0
);
assert_eq!(
zk2(&["config", "derive", &p("contracts/ok-minimal.toml")]),
1
);
assert_eq!(
zk2(&["config", "derive", &p("contracts/no-such-file.toml")]),
2
);
}