use std::path::{Path, PathBuf};
use std::process::{Command, Output};
fn grids() -> PathBuf {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../benchmark-grids");
assert!(
dir.join("matpower").is_dir(),
"benchmark-grids is a submodule: git submodule update --init benchmark-grids"
);
dir
}
fn out(name: &str) -> PathBuf {
let dir = Path::new(env!("CARGO_TARGET_TMPDIR"))
.join("convert")
.join(name);
let _ = std::fs::remove_dir_all(&dir);
dir
}
fn cimox(args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_cimox"))
.args(args)
.output()
.expect("run cimox")
}
fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
#[test]
fn a_matpower_case_becomes_its_profiles() {
let case = grids().join("matpower/case14.m");
let dir = out("matpower");
let run = cimox(&[
"convert",
case.to_str().unwrap(),
"--out",
dir.to_str().unwrap(),
]);
assert!(run.status.success(), "{}", text(&run.stderr));
let written: Vec<String> = text(&run.stdout).lines().map(str::to_string).collect();
let names: Vec<String> = ["EQ", "SSH", "TP", "SV"]
.iter()
.map(|p| dir.join(format!("case14_{p}.xml")).display().to_string())
.collect();
assert_eq!(written, names, "the written paths, on stdout");
for path in &names {
assert!(Path::new(path).is_file(), "{path}");
}
let stderr = text(&run.stderr);
assert!(
stderr.contains("note: mpc.gencost not converted"),
"{stderr}"
);
assert!(stderr.contains("converted from MATPOWER"), "{stderr}");
}
#[test]
fn a_ucte_file_becomes_one_zip() {
let file = grids().join("powsybl-open-rao/files/cases/common/TestCase12Nodes.uct");
let dir = out("ucte");
let run = cimox(&[
"convert",
file.to_str().unwrap(),
"--out",
dir.to_str().unwrap(),
"--zip",
]);
assert!(run.status.success(), "{}", text(&run.stderr));
let zip = dir.join("TestCase12Nodes.zip");
assert_eq!(text(&run.stdout).trim(), zip.display().to_string());
let mut archive = zip::ZipArchive::new(std::fs::File::open(&zip).unwrap()).unwrap();
let mut names: Vec<String> = (0..archive.len())
.map(|i| archive.by_index(i).unwrap().name().to_string())
.collect();
names.sort();
assert_eq!(
names,
[
"TestCase12Nodes_EQ.xml",
"TestCase12Nodes_SSH.xml",
"TestCase12Nodes_TP.xml"
],
"no SV: UCTE holds no state"
);
assert!(text(&run.stderr).contains("converted from UCTE-DEF"));
}
#[test]
fn the_options_reach_the_converter() {
let case = grids().join("matpower/case14.m");
let (with, without) = (out("breakers"), out("no-breakers"));
for (dir, extra) in [(&with, None), (&without, Some("--no-breakers"))] {
let mut args = vec![
"convert",
case.to_str().unwrap(),
"--out",
dir.to_str().unwrap(),
];
args.extend(extra);
args.extend(["--time", "2026-01-01T00:00:00Z"]);
assert!(cimox(&args).status.success());
}
let eq = |dir: &Path| std::fs::read_to_string(dir.join("case14_EQ.xml")).unwrap();
assert!(eq(&with).contains("cim:Breaker "));
assert!(!eq(&without).contains("cim:Breaker "));
assert!(eq(&with).contains("2026-01-01T00:00:00Z"));
}
#[test]
fn what_cannot_be_converted_is_refused() {
let case = grids().join("matpower/case14.m");
let case = case.to_str().unwrap();
let refused = |args: &[&str], says: &str| {
let run = cimox(args);
assert!(!run.status.success(), "{args:?}");
assert!(
text(&run.stderr).contains(says),
"{args:?}: {}",
text(&run.stderr)
);
};
refused(&["convert", case, "--to", "json"], "convert to CGMES");
refused(&["convert", case, "Cargo.toml.xml"], "on their own");
refused(
&["convert", "--to", "json", "a.xml", "--zip"],
"for MATPOWER and UCTE-DEF",
);
let bad = out("bad");
std::fs::create_dir_all(&bad).unwrap();
let file = bad.join("broken.uct");
std::fs::write(&file, "not a UCTE file\n").unwrap();
refused(&["convert", file.to_str().unwrap()], "error converting");
}