规则:`>` 调用上方的叙述段即其"描述";段内反引号实体必须被该调用用到,
或能解析到模块/内置,否则报 label.implicit_stale(warning)。
段落边界:紧邻上一行起,允许一个分隔空行(blockquote 前常空一行),
遇第二个空行/标题/代码行即止。
关键决策:**默认关闭(--implicit-labels 开启)**,依据是实测噪音:
- 初版(任何反引号都算断言):lib+ext/ai 上 6 条警告,**全为误报**
(`## open`、`{ path, kind }`、`source=`、`exec`/`net`)
- 加"实体名形状"过滤后:2 条
- 再加默认关闭:**0 条**
理由:描述一个调用可以用任意措辞,机械校验必然模糊;
默认开启就制造疲劳(违背 warning-fatigue-control)。这与 L1 不同:
L1 是"能证明才报",隐式检查是"可疑就报",必须 opt-in。
实体名过滤:首字符字母/_/非ASCII,其余字母数字/_/./非ASCII非空白
→ 排除 `## open` / `{ path, kind }` / `source=` / `path=`。
用法:marqdo check 文件 --implicit-labels
测试:3 个集成(默认静默 / opt-in 报不一致 / opt-in 对一致描述仍静默,
后两条是反向验证"不乱响");全量 593 passed / 0 failed(M6 后 590 + 3)。
文档:doc/design/change-m5-implicit-occlusion.md
280 lines
9.2 KiB
Rust
280 lines
9.2 KiB
Rust
//! `<TAB>` M2: occlusion syntax end-to-end (CLI + `MARQDO_MARKUP=occlusion`).
|
|
//!
|
|
//! The mode flag keeps `[键](集合)` (classic index) intact until M7 migrates the
|
|
//! corpus; these tests pin both sides of the switch.
|
|
|
|
use std::process::Command;
|
|
|
|
fn run(path: &str, markup: Option<&str>) -> (i32, String, String) {
|
|
let mut cmd = Command::new(env!("CARGO_BIN_EXE_marqdo"));
|
|
cmd.args(["run", path]);
|
|
if let Some(m) = markup {
|
|
cmd.env("MARQDO_MARKUP", m);
|
|
}
|
|
let out = cmd.output().expect("run marqdo");
|
|
(
|
|
out.status.code().unwrap_or(1),
|
|
String::from_utf8_lossy(&out.stdout).into_owned(),
|
|
String::from_utf8_lossy(&out.stderr).into_owned(),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn occlusion_value_is_payload() {
|
|
let (code, stdout, stderr) = run("tests/occlusion/value.mq.md", Some("occlusion"));
|
|
assert_eq!(code, 0, "stderr={stderr}");
|
|
assert_eq!(stdout.trim_end(), "a=2\nb=42\nc=True");
|
|
}
|
|
|
|
#[test]
|
|
fn occlusion_off_keeps_classic_index() {
|
|
let (code, stdout, stderr) = run("tests/occlusion/classic-index.mq.md", None);
|
|
assert_eq!(code, 0, "stderr={stderr}");
|
|
assert_eq!(stdout.trim_end(), "值");
|
|
}
|
|
|
|
#[test]
|
|
fn occlusion_mode_rejects_legacy_index_shape() {
|
|
// In occlusion mode `[键](集合)` is read as an occlusion whose payload is the
|
|
// collection — i.e. it returns the *whole* map, not the member. This is why
|
|
// M7 must migrate before flipping the default; here we only pin that the
|
|
// shape no longer means "index".
|
|
let (code, stdout, stderr) = run("tests/occlusion/classic-index.mq.md", Some("occlusion"));
|
|
assert_eq!(code, 0, "stderr={stderr}");
|
|
assert_ne!(stdout.trim_end(), "值", "occlusion mode must not index");
|
|
}
|
|
|
|
// ---- M2.5: label diagnostics (`label.missing` / `label.empty_payload`) ----
|
|
|
|
fn check(path: &str, markup: &str) -> (i32, String) {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.args(["check", path])
|
|
.env("MARQDO_MARKUP", markup)
|
|
.output()
|
|
.expect("run marqdo check");
|
|
(
|
|
out.status.code().unwrap_or(1),
|
|
format!(
|
|
"{}{}",
|
|
String::from_utf8_lossy(&out.stdout),
|
|
String::from_utf8_lossy(&out.stderr)
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn label_missing_is_an_error() {
|
|
let (code, text) = check("tests/occlusion/empty-label.mq.md", "occlusion");
|
|
assert_eq!(code, 1, "text={text}");
|
|
assert!(text.contains("遮蔽缺少描述"), "text={text}");
|
|
}
|
|
|
|
#[test]
|
|
fn label_empty_payload_is_a_warning() {
|
|
let (code, text) = check("tests/occlusion/empty-payload.mq.md", "occlusion");
|
|
assert_eq!(code, 0, "warnings must not fail the build: text={text}");
|
|
assert!(text.contains("空载荷遮蔽"), "text={text}");
|
|
}
|
|
|
|
#[test]
|
|
fn label_ok_has_no_diagnostics() {
|
|
let (code, text) = check("tests/occlusion/clean.mq.md", "occlusion");
|
|
assert_eq!(code, 0, "text={text}");
|
|
assert!(!text.contains("空载荷"), "text={text}");
|
|
assert!(!text.contains("缺少描述"), "text={text}");
|
|
}
|
|
|
|
// ---- M4: L1 reference consistency (`label.ref_dangling`) ----
|
|
|
|
#[test]
|
|
fn label_ref_dangling_is_an_error() {
|
|
let (code, text) = check("tests/occlusion/ref-dangling.mq.md", "occlusion");
|
|
assert_eq!(code, 1, "text={text}");
|
|
assert!(text.contains("描述引用了不存在的实体"), "text={text}");
|
|
}
|
|
|
|
#[test]
|
|
fn label_refs_that_resolve_are_fine() {
|
|
let (code, text) = check("tests/occlusion/ref-ok.mq.md", "occlusion");
|
|
assert_eq!(code, 0, "text={text}");
|
|
assert!(!text.contains("不存在的实体"), "text={text}");
|
|
}
|
|
|
|
// ---- M9: two-tier label search (`labels` CLI + MLSP `labels.search`) ----
|
|
|
|
fn labels(args: &[&str]) -> (i32, String) {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.arg("labels")
|
|
.args(args)
|
|
.env("MARQDO_MARKUP", "occlusion")
|
|
.output()
|
|
.expect("run marqdo labels");
|
|
(
|
|
out.status.code().unwrap_or(1),
|
|
format!(
|
|
"{}{}",
|
|
String::from_utf8_lossy(&out.stdout),
|
|
String::from_utf8_lossy(&out.stderr)
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn labels_pending_uses_builtin_groups() {
|
|
let (code, text) = labels(&["tests/occlusion/labels.mq.md", "--count"]);
|
|
assert_eq!(code, 0, "text={text}");
|
|
assert_eq!(text.trim(), "2", "expected 待开发 + 模拟: {text}");
|
|
}
|
|
|
|
#[test]
|
|
fn labels_grep_replaces_builtin() {
|
|
let (code, text) = labels(&["tests/occlusion/labels.mq.md", "--grep", "联调", "--count"]);
|
|
assert_eq!(code, 0, "text={text}");
|
|
assert_eq!(text.trim(), "1", "text={text}");
|
|
}
|
|
|
|
#[test]
|
|
fn labels_grep_with_builtin_unions() {
|
|
let (code, text) = labels(&[
|
|
"tests/occlusion/labels.mq.md",
|
|
"--grep",
|
|
"联调",
|
|
"--also-builtin",
|
|
"--count",
|
|
]);
|
|
assert_eq!(code, 0, "text={text}");
|
|
assert_eq!(text.trim(), "3", "text={text}");
|
|
}
|
|
|
|
#[test]
|
|
fn mlsp_labels_search_returns_hits() {
|
|
use std::io::Write;
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.arg("mlsp")
|
|
.env("MARQDO_MARKUP", "occlusion")
|
|
.stdin(std::process::Stdio::piped())
|
|
.stdout(std::process::Stdio::piped())
|
|
.spawn()
|
|
.expect("spawn mlsp");
|
|
let req = r#"{"id":1,"method":"labels.search","params":{"path":"tests/occlusion/labels.mq.md","also_builtin":true}}"#;
|
|
child
|
|
.stdin
|
|
.as_mut()
|
|
.unwrap()
|
|
.write_all(format!("{req}\n").as_bytes())
|
|
.unwrap();
|
|
let out = child.wait_with_output().unwrap();
|
|
let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
|
assert!(text.contains("\"count\":2"), "text={text}");
|
|
assert!(text.contains("客户留存率"), "text={text}");
|
|
}
|
|
|
|
// ---- M6 (T1): session-scoped staleness via `repair_apply` ----
|
|
|
|
fn mlsp_session(reqs: &[&str]) -> Vec<String> {
|
|
use std::io::Write;
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.arg("mlsp")
|
|
.env("MARQDO_MARKUP", "occlusion")
|
|
.stdin(std::process::Stdio::piped())
|
|
.stdout(std::process::Stdio::piped())
|
|
.spawn()
|
|
.expect("spawn mlsp");
|
|
let payload: String = reqs.iter().map(|r| format!("{r}\n")).collect();
|
|
child
|
|
.stdin
|
|
.as_mut()
|
|
.unwrap()
|
|
.write_all(payload.as_bytes())
|
|
.unwrap();
|
|
let out = child.wait_with_output().unwrap();
|
|
String::from_utf8_lossy(&out.stdout)
|
|
.lines()
|
|
.map(|s| s.to_string())
|
|
.collect()
|
|
}
|
|
|
|
fn repair_req(src: &str, new_line: &str) -> String {
|
|
let src_json = serde_json::to_string(src).unwrap();
|
|
let line_json = serde_json::to_string(new_line).unwrap();
|
|
format!(
|
|
r#"{{"id":1,"method":"repair_apply","params":{{"source":{src_json},"ranges":[[3,3]],"edits":[{{"op":"replace","line":3,"text":{line_json}}}],"check_labels":true}}}}"#
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn t1_flags_payload_change_only() {
|
|
let src = "# main\n\n**a = [按税率算总价](100 * 0.1)**\n> print text=`a`\n";
|
|
let out = mlsp_session(&[&repair_req(src, "**a = [按税率算总价](100 * 0.2)**")]);
|
|
let line = out.join("\n");
|
|
assert!(line.contains("\"stale\""), "{line}");
|
|
assert!(line.contains("100 * 0.1"), "must show code_past: {line}");
|
|
assert!(line.contains("100 * 0.2"), "must show code_now: {line}");
|
|
}
|
|
|
|
#[test]
|
|
fn t1_silent_when_label_also_changes() {
|
|
let src = "# main\n\n**a = [按税率算总价](100 * 0.1)**\n> print text=`a`\n";
|
|
let out = mlsp_session(&[&repair_req(
|
|
src,
|
|
"**a = [改成按两倍税率算](100 * 0.2)**",
|
|
)]);
|
|
let line = out.join("\n");
|
|
assert!(line.contains("\"stale\":[]"), "label edit = author reviewed: {line}");
|
|
}
|
|
|
|
#[test]
|
|
fn t1_session_summary_reports_counters() {
|
|
let src = "# main\n\n**a = [按税率算总价](100 * 0.1)**\n> print text=`a`\n";
|
|
let out = mlsp_session(&[
|
|
&repair_req(src, "**a = [按税率算总价](100 * 0.2)**"),
|
|
r#"{"id":2,"method":"session.summary","params":{}}"#,
|
|
]);
|
|
let summary = out.last().expect("summary line");
|
|
assert!(summary.contains("\"labels_stale_seen\":1"), "{summary}");
|
|
assert!(summary.contains("\"labels_outstanding\":1"), "{summary}");
|
|
}
|
|
|
|
// ---- M5: implicit occlusion (prose above `>` calls) ----
|
|
|
|
#[test]
|
|
fn implicit_labels_off_by_default() {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.args(["check", "tests/occlusion/implicit-stale.mq.md"])
|
|
.output()
|
|
.expect("check");
|
|
let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
|
assert!(
|
|
!text.contains("调用上方的描述"),
|
|
"must stay silent by default: {text}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn implicit_labels_opt_in_reports_mismatch() {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.args([
|
|
"check",
|
|
"tests/occlusion/implicit-stale.mq.md",
|
|
"--implicit-labels",
|
|
])
|
|
.output()
|
|
.expect("check");
|
|
let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
|
assert!(text.contains("调用上方的描述提到 `税率`"), "{text}");
|
|
}
|
|
|
|
#[test]
|
|
fn implicit_labels_opt_in_stays_quiet_when_consistent() {
|
|
let out = Command::new(env!("CARGO_BIN_EXE_marqdo"))
|
|
.args([
|
|
"check",
|
|
"tests/occlusion/implicit-ok.mq.md",
|
|
"--implicit-labels",
|
|
])
|
|
.output()
|
|
.expect("check");
|
|
let text = String::from_utf8_lossy(&out.stdout).into_owned();
|
|
assert!(!text.contains("调用上方的描述"), "{text}");
|
|
}
|