//! End-to-end compile/check tests over the CLI-facing API. use marqdo_knowledge::cli::{CheckArgs, CompileArgs, EmbeddingArgs}; use marqdo_knowledge::compiler; use marqdo_knowledge::storage::read::ReadStore; use std::path::{Path, PathBuf}; fn project(files: &[(&str, &str)]) -> PathBuf { let temp = tempfile::tempdir().unwrap(); let root = temp.path().to_path_buf(); for (path, content) in files { let absolute = root.join(path); if let Some(parent) = absolute.parent() { std::fs::create_dir_all(parent).unwrap(); } std::fs::write(absolute, content).unwrap(); } std::mem::forget(temp); root } fn compile_args(root: &Path) -> CompileArgs { CompileArgs { root: root.to_path_buf(), exclude: Vec::new(), rebuild: false, json: true, embedding: EmbeddingArgs::default(), } } fn check_args(root: &Path) -> CheckArgs { CheckArgs { root: root.to_path_buf(), okf: false, json: true, } } fn envelope(output: &marqdo_knowledge::cli::Output) -> serde_json::Value { serde_json::from_str(&output.stdout).expect("machine envelope is valid JSON") } fn core_bin() -> Option { let path = PathBuf::from(std::env::var_os("MARQDO_TEST_BIN")?); path.is_file().then_some(path) } #[test] fn plain_markdown_compiles_without_a_core_cli() { let root = project(&[ ( "notes.md", "# 笔记\n\n概率归一化是常见操作。\n\n参见[笔记](#笔记)。\n", ), ("dir/other.md", "# 其他\n\n内容。\n"), ]); let output = compiler::compile(&compile_args(&root), None).unwrap(); assert_eq!(output.exit_code, 0); let value = envelope(&output); assert_eq!(value["schema"], "marqdo.knowledge.result"); assert_eq!(value["command"], "compile"); assert_eq!(value["success"], true); assert_eq!(value["payload"]["documents"], 2); assert_eq!(value["payload"]["mode"], "rebuild"); assert!(value["snapshot_digest"] .as_str() .unwrap() .starts_with("sha256:")); assert!(root.join(".marqdo/knowledge.sqlite").is_file()); let store = ReadStore::open(&root.join(".marqdo/knowledge.sqlite")).unwrap(); store.integrity_check(true).unwrap(); assert_eq!(store.build_meta().unwrap().core_identity, "native-only"); } #[test] fn incremental_compile_reuses_unchanged_documents_and_increments_generation() { let root = project(&[("doc.md", "# 标题\n\n第一版内容。\n")]); let first = compiler::compile(&compile_args(&root), None).unwrap(); let first = envelope(&first); assert_eq!(first["payload"]["mode"], "rebuild"); let first_digest = first["snapshot_digest"].as_str().unwrap().to_string(); assert_eq!(first["payload"]["rebuild_generation"], 1); let second = compiler::compile(&compile_args(&root), None).unwrap(); let second = envelope(&second); assert_eq!(second["payload"]["mode"], "incremental"); assert_eq!(second["snapshot_digest"], first_digest); // A no-op compile keeps the generation counter stable. assert_eq!(second["payload"]["rebuild_generation"], 1); std::fs::write(root.join("doc.md"), "# 标题\n\n第二版内容。\n").unwrap(); let third = compiler::compile(&compile_args(&root), None).unwrap(); let third = envelope(&third); assert_ne!(third["snapshot_digest"], first_digest); assert_eq!(third["payload"]["rebuild_generation"], 2); let store = ReadStore::open(&root.join(".marqdo/knowledge.sqlite")).unwrap(); let query = marqdo_knowledge::text::fts_query("二版").unwrap(); assert!(!store.fts_search(&query, 5).unwrap().is_empty()); let old = marqdo_knowledge::text::fts_query("一版").unwrap(); assert!(store.fts_search(&old, 5).unwrap().is_empty()); } #[test] fn rebuild_replaces_the_artifact_explicitly() { let root = project(&[("doc.md", "# 标题\n\n内容。\n")]); compiler::compile(&compile_args(&root), None).unwrap(); let mut args = compile_args(&root); args.rebuild = true; let value = envelope(&compiler::compile(&args, None).unwrap()); assert_eq!(value["payload"]["mode"], "rebuild"); assert_eq!(value["payload"]["rebuild_generation"], 2); } #[test] fn check_reports_absent_then_current_database() { let root = project(&[("doc.md", "# 标题\n\n内容。\n")]); let absent = envelope(&compiler::check(&check_args(&root), None).unwrap()); assert_eq!(absent["payload"]["database"], "absent"); assert_eq!(absent["success"], true); compiler::compile(&compile_args(&root), None).unwrap(); let current = compiler::check(&check_args(&root), None).unwrap(); let value = envelope(¤t); assert_eq!(value["payload"]["database"], "current"); assert_eq!(current.exit_code, 0); std::fs::write(root.join("doc.md"), "# 标题\n\n改变。\n").unwrap(); let stale = compiler::check(&check_args(&root), None).unwrap(); let value = envelope(&stale); assert_eq!(value["payload"]["database"], "stale"); assert_eq!(stale.exit_code, 1); } #[test] fn check_fails_on_a_definite_broken_link() { let root = project(&[("doc.md", "# 标题\n\n[x](missing.md)\n")]); let output = compiler::check(&check_args(&root), None).unwrap(); assert_eq!(output.exit_code, 1); let value = envelope(&output); assert_eq!(value["success"], false); let diagnostics = value["diagnostics"].as_array().unwrap(); assert!(diagnostics .iter() .any(|diagnostic| diagnostic["code"] == "knowledge.reference_missing")); } #[test] fn mixed_project_uses_real_analysis_when_available() { let Some(core) = core_bin() else { eprintln!("skip: MARQDO_TEST_BIN is not set"); return; }; let root = project(&[ ( "marqdo.yaml", "project:\n name: demo\n version: 0.1.0\n marqdo: \">=3.0.0-beta.1,<4.0.0\"\ndependencies: {}\n", ), ( "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", ), ( "notes.md", "# 笔记\n\n概率归一化。\n\n参见[经典力学](physics/mechanics.mq.md#经典力学)。\n", ), ]); let output = compiler::compile(&compile_args(&root), Some(&core)).unwrap(); assert_eq!(output.exit_code, 0, "{}", output.stderr); let value = envelope(&output); assert_eq!(value["payload"]["documents"], 2); let store = ReadStore::open(&root.join(".marqdo/knowledge.sqlite")).unwrap(); let meta = store.build_meta().unwrap(); assert!(meta.producer_version.starts_with('3')); assert!(meta.core_identity.starts_with("marqdo@")); assert_eq!(store.contexts().unwrap().len(), 1); // The Exec function is a real callable node with an upstream FunctionId. let exact = store .nodes_by_exact_name(&marqdo_knowledge::text::normalize("计算力")) .unwrap(); let function = exact .iter() .find(|node| node.kind == marqdo_knowledge::model::NodeKind::Function) .expect("function node"); let metadata = &function.metadata; assert_eq!(metadata["callable"], true); assert!(metadata["function_id"] .as_str() .unwrap() .contains("::经典力学.计算力")); // Plain Markdown link into the Marqdo module resolves to the anchored unit. let hints = store .aliases_for(metadata["function_id"].as_str().unwrap()) .unwrap(); assert!(!hints.is_empty()); let check = compiler::check(&check_args(&root), Some(&core)).unwrap(); assert_eq!(check.exit_code, 0, "{}", check.stderr); }