//! Lean-backed end-to-end acceptance tests. //! //! These tests exercise the real CLI, the pinned Lean + Mathlib backend, the //! independent kernel replay and the certificate path. They are `#[ignore]`d //! because they require the fixed backend and take ~20-30s per target; CI runs //! them explicitly: //! //! ```sh //! MARQDO_BIN=/abs/marqdo cargo test --test e2e -- --ignored --test-threads=1 //! ``` use std::path::{Path, PathBuf}; use std::process::Command; use serde_json::Value; fn marqdo() -> Option { std::env::var_os("MARQDO_BIN").map(PathBuf::from) } fn repo() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) } fn work(tag: &str) -> PathBuf { let directory = repo().join(".tmp").join("e2e").join(tag); let _ = std::fs::remove_dir_all(&directory); std::fs::create_dir_all(&directory).expect("work dir"); directory } fn run(arguments: &[&str]) -> (i32, Value) { let output = Command::new(env!("CARGO_BIN_EXE_marqdo-prove")) .args(arguments) .env("MARQDO_BIN", marqdo().expect("MARQDO_BIN")) .env("MARQDO_PROVE_LEAN_ROOT", repo().join("lean")) .output() .expect("run marqdo-prove"); let code = output.status.code().unwrap_or(-1); let stdout = String::from_utf8_lossy(&output.stdout); let report: Value = serde_json::from_str(stdout.trim()).unwrap_or_else(|error| { panic!( "expected one JSON object, got `{stdout}` ({error}); stderr: {}", String::from_utf8_lossy(&output.stderr) ) }); (code, report) } fn check(entry: &Path, extra: &[&str]) -> (i32, Value) { let mut arguments = vec!["check", entry.to_str().expect("utf8 path"), "--json"]; arguments.extend_from_slice(extra); run(&arguments) } #[test] #[ignore] fn algebra_fixture_verifies_and_publishes_certificates() { let (code, report) = check(&repo().join("examples/algebra.mq.md"), &[]); assert_eq!(code, 0, "{report}"); let targets = report["targets"].as_array().expect("targets"); assert_eq!(targets.len(), 2); for target in targets { assert_eq!(target["outcome"], "verified", "{target}"); assert_eq!(target["freshness"], "current"); assert_eq!(target["scope"], "mathematical"); assert!(target["certificate"]["cache_key"].as_str().is_some()); let axioms = target["axioms"].as_array().expect("axioms"); for axiom in axioms { assert!( ["propext", "Classical.choice", "Quot.sound"] .contains(&axiom.as_str().unwrap_or("")), "{axiom}" ); } } } #[test] #[ignore] fn vector_composition_uses_both_linked_theorems() { let (code, report) = check(&repo().join("examples/vector.mq.md"), &[]); assert_eq!(code, 0, "{report}"); let target = &report["targets"][0]; assert_eq!(target["outcome"], "verified", "{target}"); let declared = target["declared_dependencies"] .as_array() .expect("declared"); let used = target["used_dependencies"].as_array().expect("used"); assert_eq!(declared.len(), 2); // Actual proof-term dependencies, not merely declared links. assert_eq!(used.len(), 2, "{target}"); for dependency in declared { assert!( used.iter().any(|used| used == &dependency["locator"]), "declared {} was not used: {target}", dependency["locator"] ); } } #[test] #[ignore] fn program_model_bridge_is_exact_integer_scope() { let (code, report) = check(&repo().join("examples/arithmetic.mq.md"), &[]); assert_eq!(code, 0, "{report}"); let target = &report["targets"][0]; assert_eq!(target["outcome"], "verified", "{target}"); assert_eq!(target["scope"], "program_model"); let declared = target["declared_dependencies"] .as_array() .expect("declared"); assert!(declared .iter() .any(|dependency| dependency["kind"] == "model")); assert!(declared .iter() .any(|dependency| dependency["kind"] == "theorem")); } #[test] #[ignore] fn false_propositions_are_refuted_by_proven_negations() { let (code, report) = check(&repo().join("tests/fixtures/false.mq.md"), &[]); assert_eq!(code, 1, "{report}"); let targets = report["targets"].as_array().expect("targets"); assert_eq!(targets.len(), 2); for target in targets { assert_eq!(target["outcome"], "refuted", "{target}"); } } #[test] #[ignore] fn exported_bundle_replays_without_search() { let directory = work("replay"); let bundle = directory.join("vector-proof"); let (code, report) = check( &repo().join("examples/vector.mq.md"), &["--out", bundle.to_str().expect("utf8")], ); assert_eq!(code, 0, "{report}"); assert!(bundle.join("manifest.json").is_file()); assert!(bundle.join("proof.ndjson").is_file()); let (code, replayed) = run(&[ "replay", repo().join("examples/vector.mq.md").to_str().expect("utf8"), bundle.to_str().expect("utf8"), "--json", ]); assert_eq!(code, 0, "{replayed}"); assert_eq!(replayed["targets"][0]["outcome"], "verified", "{replayed}"); assert_eq!(replayed["targets"][0]["freshness"], "current"); } #[test] #[ignore] fn repeated_check_reuses_and_replays_the_certificate() { let entry = repo().join("examples/algebra.mq.md"); let (_, first) = check(&entry, &[]); let key = first["targets"][0]["certificate"]["cache_key"] .as_str() .expect("cache key") .to_string(); let (code, second) = check(&entry, &[]); assert_eq!(code, 0, "{second}"); assert_eq!(second["targets"][0]["outcome"], "verified"); assert_eq!(second["targets"][0]["freshness"], "current"); assert_eq!( second["targets"][0]["certificate"]["cache_key"], Value::String(key), "a cache hit must keep the same content address" ); } #[test] #[ignore] fn tampered_payload_is_rejected() { let directory = work("tamper"); let bundle = directory.join("algebra-proof"); let (code, report) = check( &repo().join("examples/algebra.mq.md"), &[ "--unit", "整数平方非负性", "--out", bundle.to_str().expect("utf8"), ], ); assert_eq!(code, 0, "{report}"); let payload = bundle.join("proof.ndjson"); let mut bytes = std::fs::read(&payload).expect("payload"); let last = bytes.len() - 2; bytes[last] ^= 0x01; std::fs::write(&payload, &bytes).expect("tamper"); let output = Command::new(env!("CARGO_BIN_EXE_marqdo-prove")) .args([ "replay", repo() .join("examples/algebra.mq.md") .to_str() .expect("utf8"), bundle.to_str().expect("utf8"), "--json", ]) .env("MARQDO_BIN", marqdo().expect("MARQDO_BIN")) .env("MARQDO_PROVE_LEAN_ROOT", repo().join("lean")) .output() .expect("run replay"); assert_ne!(output.status.code(), Some(0)); let report: Value = serde_json::from_str(String::from_utf8_lossy(&output.stdout).trim()).expect("json"); assert_eq!(report["status"], "failure", "{report}"); } #[test] #[ignore] fn goals_outside_the_fixed_strategy_report_unknown() { let (code, report) = check(&repo().join("tests/fixtures/unknown.mq.md"), &[]); assert_eq!(code, 1, "{report}"); assert_eq!(report["targets"][0]["outcome"], "unknown", "{report}"); } #[test] #[ignore] fn stale_bundle_is_rejected_after_a_source_change() { let directory = work("stale"); let project = directory.join("project"); std::fs::create_dir_all(&project).expect("project dir"); for file in ["algebra.mq.md", "vector.mq.md"] { std::fs::copy(repo().join("examples").join(file), project.join(file)) .expect("copy fixture"); } let entry = project.join("vector.mq.md"); let bundle = directory.join("bundle"); let (code, report) = check(&entry, &["--out", bundle.to_str().expect("utf8")]); assert_eq!(code, 0, "{report}"); // A prose-only edit still changes the bound source bytes; the old bundle // must not report `current`. let algebra = std::fs::read_to_string(project.join("algebra.mq.md")).expect("read"); std::fs::write( project.join("algebra.mq.md"), algebra.replace( "任意整数的平方均非负。", "任意整数的平方均非负(解释性修改)。", ), ) .expect("edit"); let output = Command::new(env!("CARGO_BIN_EXE_marqdo-prove")) .args([ "replay", entry.to_str().expect("utf8"), bundle.to_str().expect("utf8"), "--json", ]) .env("MARQDO_BIN", marqdo().expect("MARQDO_BIN")) .env("MARQDO_PROVE_LEAN_ROOT", repo().join("lean")) .output() .expect("run replay"); assert_eq!( output.status.code(), Some(1), "{}", String::from_utf8_lossy(&output.stderr) ); let report: Value = serde_json::from_str(String::from_utf8_lossy(&output.stdout).trim()).expect("json"); let diagnostics = report["diagnostics"].as_array().expect("diagnostics"); assert!( diagnostics .iter() .any(|diagnostic| diagnostic["code"] == "certificate.stale"), "{report}" ); } #[test] #[ignore] fn mutated_program_model_is_refuted_not_verified() { let directory = work("model-mutation"); let project = directory.join("project"); std::fs::create_dir_all(&project).expect("project dir"); for file in ["algebra.mq.md", "arithmetic.mq.md"] { std::fs::copy(repo().join("examples").join(file), project.join(file)).expect("copy"); } let entry = project.join("arithmetic.mq.md"); let source = std::fs::read_to_string(&entry).expect("read"); std::fs::write( &entry, source.replace("`return x * x`", "`return x * x - 1`"), ) .expect("mutate return"); let (code, report) = check(&entry, &[]); assert_eq!(code, 1, "{report}"); assert_eq!(report["targets"][0]["outcome"], "refuted", "{report}"); assert_eq!(report["targets"][0]["scope"], "program_model"); } #[test] #[ignore] fn bundle_replays_from_a_different_checkout_directory() { let first = work("clean-replay-a"); let second = work("clean-replay-b"); let bundle = first.join("bundle"); for file in ["algebra.mq.md", "vector.mq.md"] { std::fs::copy(repo().join("examples").join(file), first.join(file)).expect("copy"); } let (code, report) = check( &first.join("vector.mq.md"), &["--out", bundle.to_str().expect("utf8")], ); assert_eq!(code, 0, "{report}"); // A different absolute checkout path with identical source bytes. for file in ["algebra.mq.md", "vector.mq.md"] { std::fs::copy(repo().join("examples").join(file), second.join(file)).expect("copy"); } let second_bundle = second.join("bundle"); std::fs::create_dir_all(&second_bundle).expect("bundle dir"); for file in ["manifest.json", "proof.ndjson"] { std::fs::copy(bundle.join(file), second_bundle.join(file)).expect("copy bundle"); } let (code, replayed) = run(&[ "replay", second.join("vector.mq.md").to_str().expect("utf8"), second_bundle.to_str().expect("utf8"), "--json", ]); assert_eq!(code, 0, "{replayed}"); assert_eq!(replayed["targets"][0]["outcome"], "verified", "{replayed}"); assert_eq!(replayed["targets"][0]["freshness"], "current"); }