//! Integration tests for the core CLI boundary and Analysis adapter. //! //! These tests need a real `marqdo` binary. Set `MARQDO_TEST_BIN` to run them; //! without it they report a skip and do not claim evidence. The repository //! keeps real captured artefacts under `tests/fixtures/analysis/` for the //! offline contract tests. use marqdo_knowledge::document::analysis; use marqdo_knowledge::document::core_cli::CoreCli; use std::path::{Path, PathBuf}; fn core_bin() -> Option { let value = std::env::var_os("MARQDO_TEST_BIN")?; let path = PathBuf::from(value); if path.is_file() { Some(path) } else { None } } fn write_project(root: &Path) { std::fs::create_dir_all(root.join("physics")).unwrap(); std::fs::write( root.join("marqdo.yaml"), "project:\n name: demo\n version: 0.1.0\n marqdo: \">=3.0.0-beta.1,<4.0.0\"\ndependencies: {}\n", ) .unwrap(); std::fs::write( root.join("physics/mechanics.mq.md"), "---\ntype: Theory\ntitle: 经典力学\n---\n\n# 经典力学\n\n## @knowledge 牛顿第二定律\n\n合外力等于质量与加速度的乘积。\n\n计算入口:[计算力](#计算力)。\n\n## @exec 计算力\n\n`params(m, a)`\n\n依据[牛顿第二定律](#牛顿第二定律)。\n\n`return m * a`\n", ) .unwrap(); std::fs::write( root.join("bad.mq.md"), "# bad\n\n## @exec broken\n\n`return this is not valid ))`\n", ) .unwrap(); } #[test] fn real_core_cli_emits_a_valid_analysis_artifact() { let Some(bin) = core_bin() else { eprintln!("skip: MARQDO_TEST_BIN is not set"); return; }; let temp = tempfile::tempdir().unwrap(); write_project(temp.path()); let core = CoreCli::locate(Some(&bin)).unwrap(); assert!(core.identity.version.starts_with('3')); let emitted = core .emit_analysis(temp.path(), "physics/mechanics.mq.md") .unwrap(); let artifact = analysis::parse_and_validate(&emitted.bytes).unwrap(); assert_eq!(artifact.schema_version, 3); assert!(artifact .modules .iter() .any(|module| module.relative_path == "physics/mechanics.mq.md")); let batch = analysis::analyze_core(temp.path(), &["physics/mechanics.mq.md".to_string()], &core); assert!(!batch.diagnostics.has_errors(), "{:?}", batch.diagnostics); let module = batch .module_for_path("physics/mechanics.mq.md") .expect("valid module facts"); assert!(module .facts .functions .iter() .any(|function| function.local_name == "计算力")); } #[test] fn invalid_marqdo_document_fails_analysis_without_an_artifact() { let Some(bin) = core_bin() else { eprintln!("skip: MARQDO_TEST_BIN is not set"); return; }; let temp = tempfile::tempdir().unwrap(); write_project(temp.path()); let core = CoreCli::locate(Some(&bin)).unwrap(); let batch = analysis::analyze_core(temp.path(), &["bad.mq.md".to_string()], &core); assert!(batch.diagnostics.has_errors()); assert_eq!(batch.modules.len(), 0); assert_eq!(batch.failed_entries, vec!["bad.mq.md".to_string()]); } #[test] fn analysis_core_cli_identity_is_recorded() { let Some(bin) = core_bin() else { eprintln!("skip: MARQDO_TEST_BIN is not set"); return; }; let core = CoreCli::locate(Some(&bin)).unwrap(); assert!(core.identity.version.starts_with('3')); assert!(core.identity.binary_digest.starts_with("sha256:")); assert_eq!(core.identity.binary_digest.len(), 71); }