#![allow(clippy::expect_used, clippy::panic)]
use docs_compiler::{compile, BuildContext};
use std::path::PathBuf;
const TOOLCHAIN: &str = "leanprover/lean4:v4.35.0";
fn root_with(lean_file: &str) -> (tempfile::TempDir, PathBuf, String) {
let dir = tempfile::tempdir().expect("tempdir");
let root: PathBuf = dir.path().to_path_buf();
let fixtures = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-fixtures/lean");
std::fs::copy(fixtures.join(lean_file), root.join("theo.lean")).expect("copy fixture");
std::fs::copy(fixtures.join("lean-toolchain"), root.join("lean-toolchain"))
.expect("copy toolchain pin");
let doc = format!(
"```lean-check file=\"theo.lean\" theorem=\"{theorem}\" toolchain=\"{TOOLCHAIN}\"\n```\n",
theorem = theorem_name(lean_file)
);
(dir, root, doc)
}
fn theorem_name(lean_file: &str) -> &'static str {
match lean_file {
"no_arbitrage.lean" => "no_arbitrage_round_trip",
"bad_claim.lean" => "bad_claim",
"uses_sorry.lean" => "uses_sorry",
other => panic!("unknown fixture {other}"),
}
}
#[test]
#[ignore = "requires lean on PATH; runs in the lean-oracle CI job"]
fn four_kind_end_to_end_with_real_lean() {
let dir = tempfile::tempdir().expect("tempdir");
let root: PathBuf = dir.path().to_path_buf();
let fixtures = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-fixtures/lean");
for name in ["no_arbitrage.lean", "lean-toolchain"] {
std::fs::copy(fixtures.join(name), root.join(name)).expect("copy lean fixture");
}
std::fs::write(root.join("sample.rs"), "pub fn answer() -> u32 { 42 }\n").expect("sample.rs");
let wat = r#"
(module
(import "wasi_snapshot_preview1" "fd_write"
(func $fd_write (param i32 i32 i32 i32) (result i32)))
(memory (export "memory") 1)
(data (i32.const 0) "\10\00\00\00\02\00\00\00")
(data (i32.const 16) "ok")
(func (export "_start")
i32.const 1
i32.const 0
i32.const 1
i32.const 48
call $fd_write
drop))
"#;
std::fs::write(root.join("ok.wasm"), wat::parse_str(wat).expect("wat")).expect("wasm");
let doc = r#"# Four kinds
```island nav-search client=visible
{"count": 4}
```
```include-symbol file="sample.rs" symbol="answer"
```
```witness wasm="ok.wasm" assert_stdout="ok" assert_exit=0
```
```lean-check file="no_arbitrage.lean" theorem="no_arbitrage_round_trip" toolchain="leanprover/lean4:v4.35.0"
```
"#;
let html = compile(doc, &BuildContext::new(&root)).expect("builds");
assert!(html.contains("<docs-island"));
assert!(html.contains("docs-symbol") && html.contains("pub fn answer"));
assert!(html.contains("docs-witness") && html.contains("ok"));
assert!(html.contains("docs-proof") && html.contains("data-proof-certificate"));
}
#[test]
#[ignore = "requires lean on PATH; runs in the lean-oracle CI job"]
fn positive_theorem_produces_a_real_certificate() {
let (_guard, root, doc) = root_with("no_arbitrage.lean");
let html = compile(&doc, &BuildContext::new(&root)).expect("lean verifies the theorem");
assert!(html.contains("docs-proof"), "badge emitted");
assert!(
html.contains("\"kernel_result\":\"verified\""),
"certificate records the kernel verdict: {html}"
);
assert!(
html.contains(&docs_proof::source_hash(
&std::fs::read_to_string(root.join("theo.lean")).expect("fixture")
)),
"certificate binds to the exact source bytes"
);
}
#[test]
#[ignore = "requires lean on PATH; runs in the lean-oracle CI job"]
fn kernel_rejection_fails_the_build() {
let (_guard, root, doc) = root_with("bad_claim.lean");
match compile(&doc, &BuildContext::new(&root)) {
Err(docs_compiler::CompilerError::Lean { theorem, detail }) => {
assert_eq!(theorem, "bad_claim");
assert!(!detail.is_empty());
}
other => panic!("expected Lean failure, got {other:?}"),
}
}
#[test]
#[ignore = "requires lean on PATH; runs in the lean-oracle CI job"]
fn sorry_is_not_a_verified_proof() {
let (_guard, root, doc) = root_with("uses_sorry.lean");
match compile(&doc, &BuildContext::new(&root)) {
Err(docs_compiler::CompilerError::Lean { theorem, detail }) => {
assert_eq!(theorem, "uses_sorry");
assert!(detail.contains("sorry"), "detail={detail}");
}
other => panic!("expected Lean sorry failure, got {other:?}"),
}
}