use std::path::{Path, PathBuf};
use std::process::Command;
fn bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_cargo-antigen"))
}
fn workspace_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("cargo-antigen crate dir has a parent (the workspace root)")
.to_path_buf()
}
fn run_in(cwd: &Path, args: &[&str]) -> (i32, String, String) {
let out = Command::new(bin())
.args(args)
.current_dir(cwd)
.output()
.expect("failed to run cargo-antigen");
(
out.status.code().unwrap_or(-1),
String::from_utf8_lossy(&out.stdout).into_owned(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
}
fn staged_workspace() -> (tempfile::TempDir, PathBuf) {
let src_root = workspace_root();
let example_src = src_root.join("antigen/examples/substrate_witness.rs");
let doc_src = src_root.join("docs/disciplines/ieee754-odd-functions.md");
assert!(
example_src.exists(),
"the substrate_witness example must exist in the real tree: {}",
example_src.display()
);
assert!(
doc_src.exists(),
"the ratified discipline doc must exist in the real tree (this is the \
doc-gap fix that unblocked the example's workflow): {}",
doc_src.display()
);
let tmp = tempfile::tempdir().expect("create temp workspace");
let root = tmp.path().to_path_buf();
let example_dst = root.join("antigen/examples/substrate_witness.rs");
std::fs::create_dir_all(example_dst.parent().unwrap()).unwrap();
std::fs::copy(&example_src, &example_dst).expect("copy example into temp workspace");
let doc_dst = root.join("docs/disciplines/ieee754-odd-functions.md");
std::fs::create_dir_all(doc_dst.parent().unwrap()).unwrap();
std::fs::copy(&doc_src, &doc_dst).expect("copy discipline doc into temp workspace");
(tmp, root)
}
fn fingerprint_from_scan(cwd: &Path) -> String {
let (code, stdout, stderr) = run_in(
cwd,
&[
"antigen",
"scan",
"--root",
"antigen/examples",
"--format",
"json",
],
);
assert_eq!(code, 0, "scan --format json must exit 0: {stderr}");
let doc: serde_json::Value = serde_json::from_str(&stdout)
.unwrap_or_else(|e| panic!("scan --format json must emit valid JSON ({e}):\n{stdout}"));
let presentations = doc["report"]["presentations"]
.as_array()
.unwrap_or_else(|| panic!("scan JSON must have report.presentations array:\n{stdout}"));
let presentation = presentations
.iter()
.find(|p| p["antigen_type"] == "SignedZeroDiscipline" && !p["requires_predicate"].is_null())
.unwrap_or_else(|| {
panic!(
"scan must capture a SignedZeroDiscipline presentation with \
requires_predicate; presentations: {:?}",
presentations
)
});
let fp = presentation["structural_fingerprint"]
.as_str()
.unwrap_or_else(|| {
panic!(
"the SignedZeroDiscipline presentation must carry an obtainable \
structural_fingerprint (F6 producer); presentation = {presentation}"
)
});
assert!(
fp.contains(':'),
"structural_fingerprint should be `<algo>:<hex>`, got `{fp}`"
);
fp.to_string()
}
fn audit_verdict_for_signed_zero(audit_stdout: &str) -> String {
for line in audit_stdout.lines() {
if line.contains("SignedZeroDiscipline") {
if line.contains("defended at Execution") {
return "Execution".to_string();
} else if line.contains("substrate-gap") {
return "substrate-gap".to_string();
} else if line.contains("undefended") {
return "undefended".to_string();
} else if line.contains("defended at") {
let tier = line
.split("defended at ")
.nth(1)
.unwrap_or("")
.trim()
.to_string();
return tier;
}
}
}
panic!(
"audit output did not contain a SignedZeroDiscipline presentation-verdict line.\n\
Full audit output:\n{audit_stdout}"
);
}
#[test]
fn substrate_witness_example_workflow_reaches_execution_tier() {
let (_tmp, root) = staged_workspace();
let fingerprint = fingerprint_from_scan(&root);
let sidecar = root.join("antigen/examples/.attest/SignedZeroDiscipline.json");
let (code, _out, stderr) = run_in(
&root,
&[
"antigen",
"attest",
"scaffold",
"--antigen",
"SignedZeroDiscipline",
"--source-file",
"antigen/examples/substrate_witness.rs",
"--item-path",
"signed_zero_preserving_sinh",
"--fingerprint",
&fingerprint,
],
);
assert_eq!(code, 0, "scaffold must succeed: {stderr}");
assert!(sidecar.exists(), "scaffold must create the sidecar file");
let (code, _out, stderr) = run_in(
&root,
&[
"antigen",
"attest",
"sign",
"--sidecar",
sidecar.to_str().unwrap(),
"--item-path",
"signed_zero_preserving_sinh",
"--signer",
"alice",
"--role",
"math-researcher",
"--fingerprint",
&fingerprint,
"--reasoning",
"reviewed sinh body: explicit x == 0.0 short-circuit preserves the sign bit",
],
);
assert_eq!(code, 0, "sign must succeed: {stderr}");
let (code, stdout, stderr) = run_in(&root, &["antigen", "audit", "--root", "antigen/examples"]);
assert_eq!(code, 0, "audit must exit 0: {stderr}");
let verdict = audit_verdict_for_signed_zero(&stdout);
assert_eq!(
verdict, "Execution",
"the substrate_witness example documents that its signed sidecar climbs to \
the Execution tier. If this is not `Execution`, the example's own workflow \
no longer reaches the tier it claims — a doc↔impl drift the example can no \
longer be trusted to demonstrate. Full audit:\n{stdout}"
);
}
#[test]
fn substrate_witness_example_is_not_execution_tier_without_signing() {
let (_tmp, root) = staged_workspace();
let (code, stdout, stderr) = run_in(&root, &["antigen", "audit", "--root", "antigen/examples"]);
assert_eq!(code, 0, "audit must exit 0 even with no sidecar: {stderr}");
let verdict = audit_verdict_for_signed_zero(&stdout);
assert_ne!(
verdict, "Execution",
"without a signed sidecar the substrate-witness immunity must NOT report \
Execution tier — that would mean the tier is unconditional and the \
positive test proves nothing. Audit:\n{stdout}"
);
assert_eq!(
verdict, "substrate-gap",
"without a signed sidecar the substrate-witness site must report \
substrate-gap (requires= predicate declared but not met). \
ADR-029 Amendment 1: a failing requires= produces SubstrateGap. \
Audit:\n{stdout}"
);
}