- document_contract: negative path coverage through the real CLI (selection, shape errors, formula unsupported/invalid, missing/external links, cycles, --unit, upstream static failure, root escape) with exit-code assertions - e2e: algebra, vector composition (declared == used), program model scope, refutation, export+replay, cache reuse, tampered payload rejection - report fixes: declared/used locator consistency, diagnostic-only exit status, unsupported vs invalid formula outcomes - clippy -D warnings and cargo fmt clean; docs/STATUS records pinned identities, executed commands, measured backend limits and known gaps
239 lines
6.9 KiB
Rust
239 lines
6.9 KiB
Rust
//! Negative/contract tests through the real CLI without requiring the Lean
|
|
//! backend: selection, document shape, formulas and link closure.
|
|
//!
|
|
//! The tests skip (and say so) when `MARQDO_BIN` is not set. Each assertion is
|
|
//! about a reported diagnostic code and the process exit code, never about
|
|
//! presentation text.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::Command;
|
|
|
|
fn cli() -> Option<PathBuf> {
|
|
std::env::var_os("MARQDO_BIN").map(PathBuf::from)
|
|
}
|
|
|
|
fn fixtures() -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures")
|
|
}
|
|
|
|
fn run(entry: &str, extra: &[&str]) -> Option<(i32, serde_json::Value)> {
|
|
let marqdo = cli()?;
|
|
let binary = env!("CARGO_BIN_EXE_marqdo-prove");
|
|
let work = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join(".tmp")
|
|
.join("tests/document_contract");
|
|
std::fs::create_dir_all(&work).expect("work dir");
|
|
let output = Command::new(binary)
|
|
.arg("check")
|
|
.arg(fixtures().join(entry))
|
|
.args(extra)
|
|
.arg("--json")
|
|
.env("MARQDO_BIN", &marqdo)
|
|
.env(
|
|
"MARQDO_PROVE_LEAN_ROOT",
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lean"),
|
|
)
|
|
.output()
|
|
.expect("run marqdo-prove");
|
|
let code = output.status.code().unwrap_or(-1);
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
let parsed: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap_or_else(|error| {
|
|
panic!(
|
|
"expected one JSON object on stdout, got `{stdout}` ({error}); stderr: {}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
)
|
|
});
|
|
Some((code, parsed))
|
|
}
|
|
|
|
fn codes(report: &serde_json::Value) -> Vec<String> {
|
|
let mut out = Vec::new();
|
|
if let Some(targets) = report["targets"].as_array() {
|
|
for target in targets {
|
|
for diagnostic in target["diagnostics"]
|
|
.as_array()
|
|
.cloned()
|
|
.unwrap_or_default()
|
|
{
|
|
if let Some(code) = diagnostic["code"].as_str() {
|
|
out.push(code.to_string());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for diagnostic in report["diagnostics"]
|
|
.as_array()
|
|
.cloned()
|
|
.unwrap_or_default()
|
|
{
|
|
if let Some(code) = diagnostic["code"].as_str() {
|
|
out.push(code.to_string());
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
#[test]
|
|
fn no_target_document_succeeds_without_targets() {
|
|
let Some((code, report)) = run("no-target.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 0, "{report}");
|
|
assert_eq!(report["targets"].as_array().map(Vec::len), Some(0));
|
|
}
|
|
|
|
#[test]
|
|
fn two_goals_are_invalid() {
|
|
let Some((code, report)) = run("two-goals.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"document.goal_count".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn dependency_list_shape_is_rejected() {
|
|
let Some((code, report)) = run("bad-dependencies.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"document.dependencies_shape".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unsupported_domain_is_unsupported_not_invalid() {
|
|
let Some((code, report)) = run("unsupported-domain.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert_eq!(report["targets"][0]["outcome"], "unsupported");
|
|
assert!(
|
|
codes(&report).contains(&"formula.unsupported".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn free_variable_is_invalid() {
|
|
let Some((code, report)) = run("free-variable.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"formula.free_variable".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn missing_link_is_reported() {
|
|
let Some((code, report)) = run("missing-link.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"link.missing".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn external_link_is_rejected() {
|
|
let Some((code, report)) = run("external-link.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"link.invalid".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn dependency_cycle_is_rejected_with_a_chain() {
|
|
let Some((code, report)) = run("cycle-a.mq.md", &[]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"link.cycle".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_unit_selection_fails() {
|
|
let Some((code, report)) = run("no-target.mq.md", &["--unit", "不存在的标题"]) else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
assert_eq!(code, 1, "{report}");
|
|
assert!(
|
|
codes(&report).contains(&"document.unit".to_string()),
|
|
"{report}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn upstream_static_failure_is_a_contract_failure() {
|
|
let Some(marqdo) = cli() else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
let binary = env!("CARGO_BIN_EXE_marqdo-prove");
|
|
let output = Command::new(binary)
|
|
.arg("check")
|
|
.arg(fixtures().join("static-error.mq.md"))
|
|
.arg("--json")
|
|
.env("MARQDO_BIN", marqdo)
|
|
.output()
|
|
.expect("run marqdo-prove");
|
|
assert_eq!(
|
|
output.status.code(),
|
|
Some(2),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
let report: serde_json::Value =
|
|
serde_json::from_str(String::from_utf8_lossy(&output.stdout).trim()).expect("json");
|
|
assert_eq!(report["status"], "failure");
|
|
assert_eq!(report["diagnostics"][0]["code"], "upstream.contract");
|
|
}
|
|
|
|
#[test]
|
|
fn root_must_contain_the_entry() {
|
|
let Some(marqdo) = cli() else {
|
|
eprintln!("skipping: MARQDO_BIN is not set");
|
|
return;
|
|
};
|
|
let binary = env!("CARGO_BIN_EXE_marqdo-prove");
|
|
let outside = Path::new("/tmp");
|
|
let output = Command::new(binary)
|
|
.arg("check")
|
|
.arg(fixtures().join("no-target.mq.md"))
|
|
.arg("--root")
|
|
.arg(outside)
|
|
.arg("--json")
|
|
.env("MARQDO_BIN", marqdo)
|
|
.output()
|
|
.expect("run marqdo-prove");
|
|
assert_eq!(output.status.code(), Some(2));
|
|
let report: serde_json::Value =
|
|
serde_json::from_str(String::from_utf8_lossy(&output.stdout).trim()).expect("json");
|
|
assert_eq!(report["diagnostics"][0]["code"], "link.outside_root");
|
|
}
|