feat(analysis): consume the current public Analysis v3 contract

- add the process boundary for the core CLI: explicit MARQDO_BIN/--marqdo-bin
  only (never PATH), argv-only invocation of check ENTRY --emit, bounded
  timeout, private temp artifact, exit checks and absolute-path sanitisation
- deserialize every Analysis record with closed serde shapes and validate
  semantic invariants that JSON Schema cannot express: identities, source
  digests, UTF-8 spans, unit tree/order/mode ancestry, anchors, functions and
  signatures, references states, calls, resources, diagnostics and budgets
- validate statement/expression trees structurally with depth and span checks
- convert accepted modules into DocumentFacts, preserving honest upstream
  reference states and one node per Exec unit/function
- merge modules across entry contexts only when identity and source agree,
  otherwise report knowledge.analysis_context_conflict
- add the real rc.4 Analysis positive fixture (with provenance and regeneration
  script), tamper negative tests and optional real-binary integration tests
This commit is contained in:
marqdo-knowledge committed 2026-11-26 00:29:19 +08:00
1 parent 682717a610
commit 59d5afe10b
14 files changed
+3474 -57

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
//! 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<PathBuf> {
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);
}