use std::path::PathBuf;
use std::process::Command;
fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.expect("workspace root")
.to_path_buf()
}
fn xpile_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_xpile"))
}
fn run_diamond_json() -> String {
let root = workspace_root();
let out = Command::new(xpile_bin())
.args([
"diamond",
"--json",
"--contracts-dir",
root.join("contracts").to_str().unwrap(),
])
.output()
.expect("run xpile diamond");
assert!(
out.status.success(),
"xpile diamond failed:\n stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).to_string()
}
fn read_aggregate_field(json: &str, name: &str) -> u64 {
let key = format!("\"{name}\":");
let idx = json
.rfind(&key)
.unwrap_or_else(|| panic!("missing aggregate field {name} in:\n{json}"));
let after = &json[idx + key.len()..];
let end = after
.find([',', '}'])
.expect("delimiter after aggregate field");
after[..end].trim().parse().expect("parse aggregate field")
}
const GRANDFATHERED_DEPTH13: [&str; 13] = [
"C-PY-INT-ARITH",
"C-COMPILE-RUST-TO-PTX-MMA",
"C-BASHRS-POSIX-IDEMPOTENCE",
"C-FFI-CPYTHON-EXT",
"C-NOTATION-LATEX-MATH-TO-EQUATION",
"C-XLATE-LEAN-TO-RUST",
"C-XLATE-PY-LIST-TO-VEC",
"C-XLATE-PY-STR-TO-RUST-STRING",
"C-XLATE-RUST-FN-TO-LEAN-THM",
"C-XPILE-BACKEND-TRAIT",
"C-XPILE-CONTRACT-BACKEND-TRAIT",
"C-XPILE-CONTRACT-FRONTEND-TRAIT",
"C-XPILE-FRONTEND-TRAIT",
];
fn contract_depths(json: &str) -> std::collections::HashMap<String, u64> {
let parsed: serde_json::Value =
serde_json::from_str(json).expect("diamond --json is valid JSON");
let mut out = std::collections::HashMap::new();
for c in parsed["contracts"]
.as_array()
.expect("`contracts` array in diamond --json")
{
let id = c["id"]
.as_str()
.expect("contract id is a string")
.to_string();
let depth_str = c["depth"].as_str().expect("contract depth is a string");
let n: u64 = depth_str
.trim_start_matches("depth-")
.trim_end_matches('+')
.parse()
.unwrap_or_else(|_| panic!("unparseable depth {depth_str:?} for {id}"));
out.insert(id, n);
}
out
}
fn grandfathered_failures(json: &str, floor: u64) -> Vec<String> {
let depths = contract_depths(json);
let mut fails = Vec::new();
for id in GRANDFATHERED_DEPTH13 {
match depths.get(id) {
None => fails.push(format!("{id} (missing from report)")),
Some(&d) if d < floor => {
fails.push(format!("{id} (depth-{d} < required depth-{floor})"))
}
Some(_) => {}
}
}
fails
}
fn assert_grandfathered_floor(json: &str, floor: u64, milestone: &str) {
let fails = grandfathered_failures(json, floor);
assert!(
fails.is_empty(),
"Diamond depth-{floor} UNIVERSAL milestone ({milestone}): every grandfathered \
(pre-R6) contract must be at depth-{floor}+, but these are not: {fails:?}\n{json}"
);
}
#[test]
fn substrate_diamond_depth_1_universal() {
let json = run_diamond_json();
let contracts_total = read_aggregate_field(&json, "contracts_total");
let depth_1_plus = read_aggregate_field(&json, "depth_1_plus");
assert_eq!(
depth_1_plus, contracts_total,
"Diamond depth-1 UNIVERSAL milestone: every contract should have ≥1 Diamond equation, \
but only {depth_1_plus} of {contracts_total} do.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_2_universal() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 2, "PMAT-228..250 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_3_universal() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 3, "PMAT-336 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_4_universal() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 4, "PMAT-344 / PMAT-454");
}
#[test]
fn substrate_diamond_aggregate_total_at_least_30() {
let json = run_diamond_json();
let total_diamonds = read_aggregate_field(&json, "total_diamonds");
assert!(
total_diamonds >= 30,
"expected substrate to have ≥30 wired Diamond equations, got {total_diamonds}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_5_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 5, "PMAT-354 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_6_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 6, "PMAT-365");
}
#[test]
fn substrate_diamond_depth_7_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 7, "PMAT-376");
}
#[test]
fn substrate_diamond_depth_8_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 8, "PMAT-387");
}
#[test]
fn substrate_diamond_depth_9_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 9, "PMAT-398 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_10_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 10, "PMAT-409 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_11_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 11, "PMAT-420 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_12_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 12, "PMAT-431 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_13_opened() {
let json = run_diamond_json();
assert_grandfathered_floor(&json, 13, "PMAT-442 / PMAT-454");
}
#[test]
fn substrate_diamond_depth_14_opened() {
let json = run_diamond_json();
let depth_14_plus = read_aggregate_field(&json, "depth_14_plus");
assert!(
depth_14_plus >= 2,
"Diamond depth-14 ACROSS LAYERS milestone (PMAT-310, PMAT-311): \
expected ≥2 contracts at depth-14+ (Layer 1 + Layer 5), got {depth_14_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_15_opened() {
let json = run_diamond_json();
let depth_15_plus = read_aggregate_field(&json, "depth_15_plus");
assert!(
depth_15_plus >= 2,
"Diamond depth-15 ACROSS LAYERS milestone (PMAT-312, PMAT-313): \
expected ≥2 contracts at depth-15+ (Layer 1 + Layer 5), got {depth_15_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_16_opened() {
let json = run_diamond_json();
let depth_16_plus = read_aggregate_field(&json, "depth_16_plus");
assert!(
depth_16_plus >= 2,
"Diamond depth-16 ACROSS LAYERS milestone (PMAT-315, PMAT-316): \
expected ≥2 contracts at depth-16+ (Layer 1 + Layer 5), got {depth_16_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_17_opened() {
let json = run_diamond_json();
let depth_17_plus = read_aggregate_field(&json, "depth_17_plus");
assert!(
depth_17_plus >= 2,
"Diamond depth-17 ACROSS LAYERS milestone (PMAT-317, PMAT-318): \
expected ≥2 contracts at depth-17+ (Layer 1 + Layer 5), got {depth_17_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_18_opened() {
let json = run_diamond_json();
let depth_18_plus = read_aggregate_field(&json, "depth_18_plus");
assert!(
depth_18_plus >= 2,
"Diamond depth-18 ACROSS LAYERS milestone (PMAT-320, PMAT-321): \
expected ≥2 contracts at depth-18+ (Layer 1 + Layer 5), got {depth_18_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_19_opened() {
let json = run_diamond_json();
let depth_19_plus = read_aggregate_field(&json, "depth_19_plus");
assert!(
depth_19_plus >= 2,
"Diamond depth-19 ACROSS LAYERS milestone (PMAT-322, PMAT-323): \
expected ≥2 contracts at depth-19+ (Layer 1 + Layer 5), got {depth_19_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_20_opened() {
let json = run_diamond_json();
let depth_20_plus = read_aggregate_field(&json, "depth_20_plus");
assert!(
depth_20_plus >= 2,
"Diamond depth-20 ACROSS LAYERS milestone (PMAT-325, PMAT-326): \
expected ≥2 contracts at depth-20+ (Layer 1 + Layer 5), got {depth_20_plus}.\n{json}"
);
}
#[test]
fn substrate_diamond_depth_21_opened() {
let json = run_diamond_json();
let depth_21_plus = read_aggregate_field(&json, "depth_21_plus");
assert!(
depth_21_plus >= 1,
"Diamond depth-21 milestone (PMAT-327): expected ≥1 contract at depth-21+, \
got {depth_21_plus}.\n{json}"
);
}
fn synthetic_report(contracts: &[(&str, u64)]) -> String {
let items: Vec<String> = contracts
.iter()
.map(|(id, d)| {
format!("{{\"id\":\"{id}\",\"diamond_count\":{d},\"depth\":\"depth-{d}+\"}}")
})
.collect();
format!(
"{{\"contracts\":[{}],\"contracts_total\":{}}}",
items.join(","),
contracts.len()
)
}
#[test]
fn r6_grandfather_allows_new_contract_at_depth_1() {
let mut cs: Vec<(&str, u64)> = GRANDFATHERED_DEPTH13.iter().map(|id| (*id, 13)).collect();
cs.push(("C-C-INT-ARITH", 1)); let json = synthetic_report(&cs);
for floor in 2..=13 {
assert!(
grandfathered_failures(&json, floor).is_empty(),
"a new contract at depth-1 must not trip the grandfathered depth-{floor} gate, \
but got failures: {:?}",
grandfathered_failures(&json, floor)
);
}
}
#[test]
fn r6_grandfather_catches_regressed_grandfathered_contract() {
let mut cs: Vec<(&str, u64)> = GRANDFATHERED_DEPTH13.iter().map(|id| (*id, 13)).collect();
cs[0].1 = 12; let json = synthetic_report(&cs);
let fails = grandfathered_failures(&json, 13);
assert_eq!(
fails.len(),
1,
"exactly one regressed contract expected: {fails:?}"
);
assert!(fails[0].contains(GRANDFATHERED_DEPTH13[0]));
}
#[test]
fn r6_grandfather_catches_removed_grandfathered_contract() {
let cs: Vec<(&str, u64)> = GRANDFATHERED_DEPTH13
.iter()
.skip(1)
.map(|id| (*id, 13))
.collect();
let json = synthetic_report(&cs);
let fails = grandfathered_failures(&json, 13);
assert!(
fails.iter().any(|f| f.contains("missing")),
"a removed grandfathered contract must be reported missing: {fails:?}"
);
}
#[test]
fn r6_grandfather_live_report_meets_depth_13() {
let json = run_diamond_json();
assert!(
grandfathered_failures(&json, 13).is_empty(),
"all 13 grandfathered contracts must still be at depth-13+ in the live report"
);
}