229 lines
8.2 KiB
Rust
229 lines
8.2 KiB
Rust
mod common;
|
|
|
|
use std::path::Path;
|
|
|
|
use common::{analysis_value, document, fixture_path, projection, require_core};
|
|
use marqdo_lint::{
|
|
input::{AnalysisStatus, MarqdoRunner, ProjectInput},
|
|
model::ExecutionProjection,
|
|
};
|
|
use serde_json::json;
|
|
|
|
#[test]
|
|
fn saved_revision_three_artifact_is_typed_and_self_contained() {
|
|
for stem in [
|
|
"branches",
|
|
"unicode",
|
|
"bom",
|
|
"knowledge",
|
|
"inventory",
|
|
"multi-arm",
|
|
"loop-jumps",
|
|
] {
|
|
let projection = projection(stem);
|
|
assert_eq!(projection.schema_version, 3);
|
|
assert_eq!(projection.producer.markdown_profile, "marqdo.md.v1");
|
|
let module = projection.entry_module().expect("entry module");
|
|
assert_eq!(
|
|
module
|
|
.source_snapshot(fixture_path(&format!("{stem}.mq.md")))
|
|
.unwrap()
|
|
.digest_hex(),
|
|
module.source_digest
|
|
);
|
|
let document = document(stem);
|
|
let module = document.module.as_ref().unwrap();
|
|
assert_eq!(module.source_digest, document.source.digest_hex());
|
|
assert!(!document.markdown.headings.is_empty());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn retired_same_revision_shape_is_rejected_with_regeneration_hint() {
|
|
for field in ["source", "document", "anchors"] {
|
|
let mut raw = analysis_value("branches");
|
|
raw["modules"][0].as_object_mut().unwrap().remove(field);
|
|
let error = ExecutionProjection::from_json(raw).unwrap_err();
|
|
assert!(error.contains("旧 revision 3"), "{error}");
|
|
assert!(error.contains("重新生成"), "{error}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn schema_revision_unknown_or_v2_is_rejected_without_decoding() {
|
|
for version in [2, 4, 999] {
|
|
let mut raw = analysis_value("branches");
|
|
raw["schema_version"] = json!(version);
|
|
let error = ExecutionProjection::from_json(raw).unwrap_err();
|
|
assert!(error.contains("revision 3"), "{error}");
|
|
assert!(error.contains(&version.to_string()), "{error}");
|
|
}
|
|
let mut raw = analysis_value("branches");
|
|
raw["schema"] = json!("marqdo.analysis.old");
|
|
assert!(ExecutionProjection::from_json(raw)
|
|
.unwrap_err()
|
|
.contains("marqdo.analysis"));
|
|
}
|
|
|
|
#[test]
|
|
fn source_digest_and_span_invariants_fail_closed() {
|
|
let mut digest = analysis_value("branches");
|
|
digest["modules"][0]["source_digest"] = json!("0".repeat(64));
|
|
let error = ExecutionProjection::from_json(digest).unwrap_err();
|
|
assert!(error.contains("sha256(source)"), "{error}");
|
|
|
|
let mut short = analysis_value("branches");
|
|
short["modules"][0]["source_digest"] = json!("abc");
|
|
assert!(ExecutionProjection::from_json(short)
|
|
.unwrap_err()
|
|
.contains("source_digest"));
|
|
|
|
let mut out_of_bounds = analysis_value("branches");
|
|
out_of_bounds["modules"][0]["units"][0]["span"]["end"] = json!(999_999);
|
|
assert!(ExecutionProjection::from_json(out_of_bounds)
|
|
.unwrap_err()
|
|
.contains("超出源码长度"));
|
|
|
|
let mut utf8 = analysis_value("unicode");
|
|
let source = utf8["modules"][0]["source"].as_str().unwrap().to_string();
|
|
let emoji = source.find('😀').unwrap();
|
|
utf8["modules"][0]["functions"][0]["span"]["start"] = json!(emoji + 1);
|
|
let error = ExecutionProjection::from_json(utf8).unwrap_err();
|
|
assert!(
|
|
error.contains("UTF-8") || error.contains("字符边界"),
|
|
"{error}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_statement_or_reference_state_is_rejected() {
|
|
let mut raw = analysis_value("multi-arm");
|
|
raw["modules"][0]["functions"][0]["body"][0]["kind"] = json!("goto");
|
|
assert!(ExecutionProjection::from_json(raw)
|
|
.unwrap_err()
|
|
.contains("goto"));
|
|
|
|
let mut raw = analysis_value("multi-arm");
|
|
raw["modules"][0]["functions"][0]["body"][0]["arms"][1]["body"] = json!({});
|
|
assert!(ExecutionProjection::from_json(raw)
|
|
.unwrap_err()
|
|
.contains("body"));
|
|
|
|
let mut raw = analysis_value("branches");
|
|
raw["modules"][0]["references"] = json!([{
|
|
"source": raw["modules"][0]["id"],
|
|
"source_unit": raw["modules"][0]["units"][1]["id"],
|
|
"url": "./missing.mq.md",
|
|
"span": {"start": 0, "end": 1},
|
|
"state": "ambiguous",
|
|
"target_kind": null,
|
|
"target_module": null,
|
|
"target_unit": null,
|
|
"candidate_package_instance_id": null,
|
|
"candidate_path": "missing.mq.md",
|
|
"fragment": null,
|
|
"candidates": []
|
|
}]);
|
|
let error = ExecutionProjection::from_json(raw).unwrap_err();
|
|
assert!(error.contains("ambiguous"), "{error}");
|
|
}
|
|
|
|
#[test]
|
|
fn live_core_uses_one_check_and_artifact_source_without_reopening_modules() {
|
|
let Some(binary) = require_core() else {
|
|
return;
|
|
};
|
|
let runner = MarqdoRunner::discover(Some(Path::new(&binary))).unwrap();
|
|
let entry = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/multi/index.mq.md");
|
|
let project = ProjectInput::load(&entry, &runner, false).unwrap();
|
|
assert_eq!(project.analysis_status, AnalysisStatus::Complete);
|
|
assert_eq!(project.documents.len(), 2);
|
|
assert!(project.artifact_digest.unwrap().starts_with("sha256:"));
|
|
for document in &project.documents {
|
|
let module = document.module.as_ref().unwrap();
|
|
assert_eq!(module.source, document.source.text());
|
|
assert_eq!(module.source_digest, document.source.digest_hex());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn failed_check_is_analysis_incomplete_and_never_a_clean_result() {
|
|
let Some(binary) = require_core() else {
|
|
return;
|
|
};
|
|
let runner = MarqdoRunner::discover(Some(Path::new(&binary))).unwrap();
|
|
let project = ProjectInput::load(
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/golden/invalid-control.mq.md"),
|
|
&runner,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
assert_eq!(project.analysis_status, AnalysisStatus::AnalysisIncomplete);
|
|
assert!(project.analysis.is_none());
|
|
assert!(project.core_output.contains("sem.unclosed_control"));
|
|
assert!(project.documents[0].module.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn module_and_entry_identity_must_be_unique_and_safe() {
|
|
let mut duplicate = analysis_value("branches");
|
|
let first = duplicate["modules"][0].clone();
|
|
duplicate["modules"].as_array_mut().unwrap().push(first);
|
|
assert!(ExecutionProjection::from_json(duplicate)
|
|
.unwrap_err()
|
|
.contains("重复"));
|
|
|
|
let mut entry = analysis_value("branches");
|
|
entry["project"]["entry_module_id"] = json!("root:default::other.mq.md");
|
|
assert!(ExecutionProjection::from_json(entry)
|
|
.unwrap_err()
|
|
.contains("entry_module_id"));
|
|
|
|
let mut path = analysis_value("branches");
|
|
path["modules"][0]["relative_path"] = json!("../escape.mq.md");
|
|
assert!(ExecutionProjection::from_json(path)
|
|
.unwrap_err()
|
|
.contains("relative_path"));
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn failed_check_never_reads_a_stale_artifact_left_at_the_emit_path() {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let Some(binary) = require_core() else {
|
|
return;
|
|
};
|
|
let work = tempfile::tempdir().unwrap();
|
|
let source = work.path().join("branches.mq.md");
|
|
std::fs::copy(
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/golden/branches.mq.md"),
|
|
&source,
|
|
)
|
|
.unwrap();
|
|
let projection = std::fs::read(fixture_path("branches.analysis.json")).unwrap();
|
|
std::fs::write(work.path().join("stale.json"), projection).unwrap();
|
|
let proxy = work.path().join("stale-core");
|
|
std::fs::write(
|
|
&proxy,
|
|
"#!/bin/sh\nset -eu\ncase \"$1\" in\n --version) echo 'marqdo 3.0.0-rc.4' ;;\n check) case \"$4\" in --emit) cp \"$0.json\" \"$5\" ;; esac; exit 2 ;;\n *) exit 2 ;;\nesac\n",
|
|
)
|
|
.unwrap();
|
|
std::fs::set_permissions(&proxy, std::fs::Permissions::from_mode(0o755)).unwrap();
|
|
std::fs::copy(
|
|
work.path().join("stale.json"),
|
|
work.path().join("stale-core.json"),
|
|
)
|
|
.unwrap();
|
|
let runner = MarqdoRunner::discover(Some(&proxy)).unwrap();
|
|
let project = ProjectInput::load(&source, &runner, false).unwrap();
|
|
assert_eq!(
|
|
project.analysis_status,
|
|
AnalysisStatus::AnalysisIncomplete,
|
|
"a successful decode of the stale artifact would be a protocol violation"
|
|
);
|
|
assert!(project.analysis.is_none());
|
|
assert!(project.documents[0].module.is_none());
|
|
let _ = binary;
|
|
}
|