use std::{fs, path::PathBuf, process::Command}; use serde_json::Value; fn lint() -> Command { Command::new(env!("CARGO_BIN_EXE_marqdo-lint")) } fn fixtures() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures") } #[test] fn rule_commands_do_not_require_the_core() { let output = lint() .env("MARQDO_BIN", "no-such-core") .args(["rules", "--format", "json"]) .output() .unwrap(); assert!(output.status.success()); let rules: Vec = serde_json::from_slice(&output.stdout).unwrap(); assert_eq!(rules.len(), 22); let output = lint() .env("MARQDO_BIN", "no-such-core") .args(["explain", "MQD-BRANCH-001", "--format", "json"]) .output() .unwrap(); assert!(output.status.success()); let rule: Value = serde_json::from_slice(&output.stdout).unwrap(); assert_eq!(rule["fix_safety"], "manual"); assert!(rule["evidence"].is_string()); } #[test] fn reports_and_exit_codes_distinguish_style_from_unavailable_analysis() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let temp = tempfile::tempdir().unwrap(); let source = temp.path().join("index.mq.md"); fs::write(&source, "## @exec small\n\n*return 1*\n").unwrap(); let report = |args: &[&str]| { let output = lint() .current_dir(temp.path()) .args(["index.mq.md", "--format", "json"]) .args(args) .output() .unwrap(); assert!(output.stderr.is_empty(), "{:?}", output); ( output.status.code().unwrap(), serde_json::from_slice::(&output.stdout).unwrap(), ) }; let (code, result) = report(&[]); assert_eq!(code, 1, "{result}"); assert_eq!(result["analysis_status"], "complete"); assert_eq!(result["diagnostics"][0]["id"], "MQD-DOC-001"); assert!(result["diagnostics"] .as_array() .unwrap() .iter() .any(|diagnostic| { diagnostic["id"] == "MQD-DOC-003" && diagnostic["severity"] == "info" })); assert_eq!(report(&["--fail-on", "error"]).0, 0); fs::write( temp.path().join("marqdo-lint.toml"), r#" [[suppress]] path = "index.mq.md" rule = "MQD-DOC-001" reason = "Fixture for a deliberately short helper" "#, ) .unwrap(); assert_eq!(report(&[]).0, 0); let config_path = temp.path().join("marqdo-lint.toml"); let config = fs::read_to_string(&config_path).unwrap(); fs::write( &config_path, format!("{config}\n[rules]\n\"MQD-DOC-003\" = \"warning\"\n"), ) .unwrap(); assert_eq!(report(&[]).0, 1); fs::copy(fixtures().join("golden/invalid-control.mq.md"), source).unwrap(); let (code, result) = report(&["--fail-on", "never"]); assert_eq!(code, 2, "{result}"); assert_eq!(result["analysis_status"], "analysis_incomplete"); assert!(result["metrics"]["narrative_ratio"].is_null()); assert!(result["core_output"] .as_str() .unwrap() .contains("sem.unclosed_control")); assert!(result["diagnostics"].as_array().unwrap().is_empty()); let invalid_signature = "## @exec x\n\n- `params(n: Int)`\n\n*return 1*"; fs::write(temp.path().join("index.mq.md"), invalid_signature).unwrap(); let (code, result) = report(&[]); assert_eq!(code, 2, "{result}"); assert!( result["core_output"].as_str().unwrap().contains("params"), "{result}" ); } #[test] fn multi_module_run_reports_each_source_once() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let output = lint() .current_dir(fixtures().join("multi")) .args([ "index.mq.md", "geometry.mq.md", ".", "--format", "json", "--fail-on", "never", ]) .output() .unwrap(); assert!(output.status.success(), "{:?}", output); let report: Value = serde_json::from_slice(&output.stdout).unwrap(); assert_eq!(report["summary"]["files"], 2); assert_eq!(report["reports"][0]["source"]["path"], "geometry.mq.md"); assert_eq!(report["reports"][1]["source"]["path"], "index.mq.md"); } #[cfg(unix)] #[test] fn t30_frozen_core_installs_and_dispatches_lint() { let Some(core) = std::env::var_os("MARQDO_BIN") else { return; }; let temp = tempfile::tempdir().unwrap(); let bundle = temp.path().join("bundle"); fs::create_dir_all(bundle.join("bin")).unwrap(); fs::copy( env!("CARGO_BIN_EXE_marqdo-lint"), bundle.join("bin/marqdo-lint"), ) .unwrap(); let manifest = bundle.join("tool.json"); fs::write(&manifest, include_bytes!("../tool.json")).unwrap(); let marqdo = || { let mut command = Command::new(&core); command .env("MARQDO_TOOL_HOME", temp.path().join("registry")) .env_remove("MARQDO_BIN"); command }; let install = marqdo() .args(["tool", "install"]) .arg(manifest) .output() .unwrap(); assert!(install.status.success(), "{:?}", install); let output = marqdo() .current_dir(fixtures().join("golden")) .args([ "lint", "branches.mq.md", "--format", "json", "--fail-on", "never", ]) .output() .unwrap(); assert!(output.status.success(), "{:?}", output); let report: Value = serde_json::from_slice(&output.stdout).unwrap(); assert_eq!(report["analysis_status"], "complete"); } #[test] fn safe_fix_previews_then_persists_verified_whitespace_in_a_manifest_project() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let temp = tempfile::tempdir().unwrap(); let source = temp.path().join("index.mq.md"); let multi = fixtures().join("multi"); fs::copy(multi.join("marqdo.yaml"), temp.path().join("marqdo.yaml")).unwrap(); fs::copy( multi.join("geometry.mq.md"), temp.path().join("geometry.mq.md"), ) .unwrap(); let clean = fs::read_to_string(multi.join("index.mq.md")).unwrap(); let original = clean.replace("## @exec main\n\n", "## @exec main\n \t\n"); assert_ne!(original, clean); fs::write(&source, &original).unwrap(); let fix = |args: &[&str]| { lint() .current_dir(temp.path()) .args([ "index.mq.md", "--fix", "safe", "--format", "json", "--fail-on", "never", ]) .args(args) .output() .unwrap() }; let preview = fix(&["--dry-run"]); assert!(preview.status.success(), "{:?}", preview); assert!(String::from_utf8_lossy(&preview.stderr).contains("@@")); assert_eq!(fs::read_to_string(&source).unwrap(), original); let applied = fix(&[]); assert!(applied.status.success(), "{:?}", applied); assert_eq!(fs::read_to_string(&source).unwrap(), clean); let repeated = fix(&[]); assert!( repeated.status.success() && repeated.stderr.is_empty(), "{:?}", repeated ); } #[test] fn safe_fix_rejects_changes_to_inert_payload_and_unavailable_analysis() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let temp = tempfile::tempdir().unwrap(); let source = temp.path().join("index.mq.md"); for (original, code) in [ ("## @exec example\n\n```text\n \t\n```\n\n*return 1*\n", 1), ("## @exec example\n\n`if true`\n\n*return 1*", 2), ] { fs::write(&source, original).unwrap(); let output = lint() .current_dir(temp.path()) .args([ "index.mq.md", "--fix", "safe", "--format", "json", "--fail-on", "never", ]) .output() .unwrap(); let report: Value = serde_json::from_slice(&output.stdout).unwrap(); assert_eq!(output.status.code(), Some(code), "{report}"); assert!( report["diagnostics"] .as_array() .unwrap() .iter() .any(|diagnostic| { diagnostic["id"] == "MQD-SAFETY-001" }), "{report}" ); assert_eq!(fs::read_to_string(&source).unwrap(), original); } } #[test] fn supplemental_rules_use_bound_calls_and_allow_rich_knowledge() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let temp = tempfile::tempdir().unwrap(); let source = temp.path().join("index.mq.md"); let original = "## @exec output\n\n`params(x: Int)`\n\n**打印(x)**\n\n### @knowledge 说明\n\n这一段说明输出用途。\n\n### @exec 结果\n\n`return x`\n"; let report = |text: &str| { fs::write(&source, text).unwrap(); let result = lint() .current_dir(temp.path()) .args(["index.mq.md", "--format", "json", "--fail-on", "never"]) .output() .unwrap(); assert!(result.status.success(), "{:?}", result); serde_json::from_slice::(&result.stdout).unwrap() }; let result = report(original); assert_eq!( result["metrics"]["functions"][0]["has_explicit_effects"], true ); let has = |result: &Value, id: &str| { result["diagnostics"] .as_array() .unwrap() .iter() .any(|diagnostic| diagnostic["id"] == id) }; for id in [ "MQD-PROSE-002", "MQD-PARAM-001", "MQD-KNOW-001", "MQD-STYLE-001", ] { assert!(has(&result, id), "{id} {result}"); } let improved = original .replace( "`params(x: Int)`", "输入 x 使用最小货币单位。\n\n`params(x: Int)`", ) .replace("**打印(x)**", "向控制台写入整数以便核对。\n\n**打印(x)**") .replace("这一段说明输出用途。", "这里的**输出**指控制台记录。") .replace("`return x`", "*return x*"); let result = report(&improved); for id in [ "MQD-PROSE-002", "MQD-PARAM-001", "MQD-KNOW-001", "MQD-STYLE-001", ] { assert!(!has(&result, id), "{id} {result}"); } } #[test] fn t16_exec_emphasis_inside_prose_is_not_a_comment() { if std::env::var_os("MARQDO_BIN").is_none() { return; } let temp = tempfile::tempdir().unwrap(); fs::write( temp.path().join("index.mq.md"), "## @exec 示例\n\n这是 **特别重要** 的处理。\n\n*return 1*\n", ) .unwrap(); let output = lint() .current_dir(temp.path()) .args(["index.mq.md", "--format", "json"]) .output() .unwrap(); let report: Value = serde_json::from_slice(&output.stdout).unwrap(); assert!(report["diagnostics"] .as_array() .unwrap() .iter() .any(|d| d["id"] == "MQD-PROSE-001")); } #[cfg(unix)] #[test] fn old_tool_manifest_is_rejected_and_new_manifest_requires_revision_one() { let Some(core) = std::env::var_os("MARQDO_BIN") else { return; }; let temp = tempfile::tempdir().unwrap(); let bundle = temp.path().join("bundle"); fs::create_dir_all(bundle.join("bin")).unwrap(); fs::copy( env!("CARGO_BIN_EXE_marqdo-lint"), bundle.join("bin/marqdo-lint"), ) .unwrap(); let manifest = bundle.join("tool.json"); fs::write(&manifest, include_bytes!("../tool.json")).unwrap(); let value: Value = serde_json::from_slice(&fs::read(&manifest).unwrap()).unwrap(); assert_eq!(value["requires"]["api_revision"], 1); assert_eq!(value["requires"]["analysis"], 3); assert!(value.get("compatibility").is_none()); let marqdo = || { let mut command = Command::new(&core); command .env("MARQDO_TOOL_HOME", temp.path().join("registry")) .env_remove("MARQDO_BIN"); command }; assert!(marqdo() .args(["tool", "install"]) .arg(&manifest) .output() .unwrap() .status .success()); assert!(marqdo() .args(["tool", "remove", "lint"]) .output() .unwrap() .status .success()); let old = value; let mut object = old.as_object().unwrap().clone(); object.insert( "compatibility".into(), serde_json::json!({"marqdo": ">=3.0.0-rc.1,<4.0.0", "analysis": [2, 3]}), ); fs::write( &manifest, serde_json::to_vec(&Value::Object(object)).unwrap(), ) .unwrap(); let output = marqdo() .args(["tool", "install"]) .arg(&manifest) .output() .unwrap(); assert!(!output.status.success(), "{output:?}"); let stderr = String::from_utf8_lossy(&output.stderr); assert!(stderr.contains("compatibility"), "{stderr}"); }