Files
marqdo/tests/occlusion.rs
T
cflmy 6bba0be6d2 feat(mlsp): M6 T1 会话内陈旧检测 — repair_apply 前后对照 + session.summary
机制:repair_apply 内同一作用域已同时持有 source(改前) 与 new_source(改后),
所以「描述是否还成立」可纯内存判定,无需 git/台账/exec:
  stale ⟺ 标签文本未变 ∧ payload 指纹变了
按 (标签文本, 出现序号) 配对 → 移动代码不误报。

协议:
  请求加可选 check_labels(默认 false,零开销零噪音)
  响应加 labels.stale[{label,line,code_past,code_now}] + newly_seen
  新增 session.summary → {labels_stale_seen, labels_fixed, labels_outstanding, items},
  返回后清零(客户端收尾调用一次即闭合,不主动打屏)

取舍:
- 标签变了 → 不报(作者改描述即"已复核"信号)
- 新标签 → 不报(无历史可比)
- 指纹用 Debug:只需进程内一致;跨版本稳定指纹留给 T2/T3
- code_past/code_now 用 view::expr_display 渲染成人可读的表面语法

实测:
  改 payload 0.1→0.2、标签不变 → stale 且带 code_past="100 * 0.1" / code_now="100 * 0.2"
  同时改标签 → stale: []
  session.summary → stale_seen=1, outstanding=1, items=["按税率算总价"]

测试:3 个集成(含反向验证"改描述即复核不误报");
全量 cargo test --release:590 passed / 0 failed(M10 后 587 + 3)

文档:doc/design/change-m6-t1-session.md
2026-10-22 15:19:17 +08:00

237 lines
8.0 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}");
}