Files
marqdo-web-design/tests/design_contract.rs
cflmy 9684e388ce feat: interpret the remaining retained H3 contracts (WEB-01)
Add the input/event/scenario/interaction/dependency/acceptance model and
validation on top of the core loader:
- 输入/事件 plain-text tables with non-empty unique names; links rejected;
- 场景 only on leaf components, ASCII slug states unique, exactly one initial,
  content cell is exactly one link to a Knowledge unit (Exec and design
  component declarations rejected), scene limit 32;
- 交互 state references, non-empty unique event labels per state, reachability
  from the initial state, multi-state requires transitions, 128 limit;
- 依赖 must target loaded design components with non-empty reasons and an
  acyclic design dependency graph;
- 验收 keeps the direct section text, rejects empty content, never interprets
  it as a test;
- contract-aligned limits (100 docs/200 defs/1000 instances/12 depth/12 slots
  per row/1 MiB file/16 MiB total) and a richer deterministic handoff.

14 real-CLI contract tests now cover the positives and main negatives. Docs and
the WEB-01 evidence record which security/concurrency cases and WEB-03 handoff
remain open; WEB-01 stays open.

Refs cflmy/marqdo-web-design#1, cflmy/marqdo#6
2026-11-27 02:37:22 +08:00

525 lines
20 KiB
Rust

//! WEB-01 contract tests against the real verified CLI (no mocked Analysis).
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use marqdo_web_design::{handoff, load, statistics, Error, LoadConfig};
static UNIQUE: AtomicU64 = AtomicU64::new(0);
fn cli() -> PathBuf {
PathBuf::from(
std::env::var("MARQDO_BIN")
.expect("set MARQDO_BIN to the verified marqdo executable for WEB-01 tests"),
)
}
struct TempProject {
path: PathBuf,
}
impl TempProject {
fn new() -> Self {
let unique = UNIQUE.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!(
"marqdo-web-design-test-{}-{unique}",
std::process::id()
));
std::fs::create_dir_all(&path).unwrap();
Self { path }
}
fn write(&self, relative: &str, text: &str) {
let path = self.path.join(relative);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
fn config(&self, entry: &str, unit: &str) -> LoadConfig {
LoadConfig::new(cli(), self.path.join(entry), unit, self.path.clone())
}
}
impl Drop for TempProject {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
fn design_code(error: Error) -> &'static str {
match error {
Error::Design(items) => items[0].code,
Error::Engine(items) => panic!("expected design error, got engine: {items:?}"),
}
}
fn engine_code(error: Error) -> &'static str {
match error {
Error::Engine(items) => items[0].code,
Error::Design(items) => panic!("expected engine error, got design: {items:?}"),
}
}
const INDEX: &str = r#"# 页面
## @knowledge 工作台
面向研究人员的页面。
### 内容
一些正文。
### 组件
| 行 | 槽 | 组件 | 宽度 |
| --- | --- | --- | --- |
| 1 | nav | [导航](components/nav.mq.md#导航) | 220px |
| 1 | main | [工作区](components/workspace.mq.md#工作区) | 1fr |
"#;
const NAV: &str = r#"# 导航组件
## @knowledge 导航
### 内容
导航正文。
### 组件
| 行 | 槽 | 组件 | 宽度 |
| --- | --- | --- | --- |
| 1 | leaf | [叶子](leaf.mq.md#叶子) | auto |
"#;
const LEAF: &str = r#"# 叶子组件
## @knowledge 叶子
### 内容
叶子正文。
"#;
fn basic_project() -> TempProject {
let project = TempProject::new();
project.write("index.mq.md", INDEX);
project.write("components/nav.mq.md", NAV);
project.write("components/leaf.mq.md", LEAF);
project.write(
"components/workspace.mq.md",
"# 工作区页面\n\n## @knowledge 工作区\n\n### 内容\n\n工作区正文。\n",
);
project
}
#[test]
fn loads_recursive_layout_with_stable_source_identity() {
let project = basic_project();
let design = load(&project.config("index.mq.md", "工作台")).expect("load");
assert_eq!(design.root, "index.mq.md#工作台");
assert_eq!(statistics(&design), (4, 4));
assert_eq!(design.definitions["index.mq.md#工作台"].slots.len(), 2);
let root_slot = &design.definitions["index.mq.md#工作台"].slots[0];
assert_eq!(root_slot.slot, "nav");
assert_eq!(root_slot.row, 1);
assert_eq!(root_slot.width.to_string(), "220px");
assert_eq!(root_slot.target, "components/nav.mq.md#导航");
assert_eq!(
design.definitions["components/nav.mq.md#导航"].slots[0].target,
"components/leaf.mq.md#叶子"
);
assert_eq!(design.sources.len(), 4);
for source in &design.sources {
assert_eq!(source.source_digest.len(), 64);
assert!(!source.module_id.is_empty());
}
let first = handoff(&design);
assert!(first.contains("components/leaf.mq.md#叶子"));
assert!(first.contains("sha256:"));
let again = load(&project.config("index.mq.md", "工作台")).expect("deterministic load");
assert_eq!(first, handoff(&again));
}
#[test]
fn reuses_definition_but_keeps_separate_instances() {
let project = TempProject::new();
project.write(
"index.mq.md",
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | left | [叶子](leaf.mq.md#叶子) | auto |\n| 1 | right | [叶子](leaf.mq.md#叶子) | auto |\n",
);
project.write("leaf.mq.md", LEAF);
let design = load(&project.config("index.mq.md", "工作台")).expect("load");
assert_eq!(design.definitions.len(), 2);
let ids: Vec<&str> = design
.instances
.iter()
.map(|item| item.id.as_str())
.collect();
assert_eq!(ids, vec!["root", "root/left", "root/right"]);
assert_eq!(design.instances[1].definition, "leaf.mq.md#叶子");
assert_eq!(design.instances[2].definition, "leaf.mq.md#叶子");
assert_ne!(design.instances[1].id, design.instances[2].id);
}
#[test]
fn accepts_content_only_component_and_width_forms() {
let project = TempProject::new();
project.write("index.mq.md", INDEX);
project.write("components/nav.mq.md", &NAV.replace("220px", "1200px"));
project.write("components/leaf.mq.md", LEAF);
project.write(
"components/workspace.mq.md",
"# 工作区页面\n\n## @knowledge 工作区\n\n### 内容\n\n正文。\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [叶子](leaf.mq.md#叶子) | 12fr |\n",
);
load(&project.config("index.mq.md", "工作台")).expect("width forms");
}
#[test]
fn rejects_missing_anchor_and_non_knowledge_units() {
let project = basic_project();
let error = load(&project.config("index.mq.md", "不存在")).unwrap_err();
assert!(matches!(error, Error::Design(_)));
assert_eq!(design_code(error), "web.unit.anchor_missing");
let exec = TempProject::new();
exec.write("index.mq.md", "# @exec 页面\n\n## 函数\n\n`return 1`\n");
let error = load(&exec.config("index.mq.md", "函数")).unwrap_err();
assert_eq!(design_code(error), "web.unit.not_knowledge_h2");
let hashed = basic_project();
let mut config = hashed.config("index.mq.md", "工作台");
config.unit = "#工作台".to_string();
assert_eq!(
design_code(load(&config).unwrap_err()),
"web.unit.anchor_invalid"
);
}
#[test]
fn rejects_invalid_component_tables_and_cells() {
let cases: &[(&str, &str)] = &[
(
"| 行 | 槽 | 组件 |\n| --- | --- | --- |\n| 1 | nav | [导航](components/nav.mq.md#导航) |\n",
"web.table.header",
),
(
"| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | nav | 纯文本 | 220px |\n",
"web.link.none",
),
(
"| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | nav | [导航](components/nav.mq.md#导航) | 50% |\n",
"web.layout.width",
),
(
"| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | nav | [导航](components/nav.mq.md#导航) | auto |\n| 1 | nav | [导航](components/nav.mq.md#导航) | auto |\n",
"web.layout.slot_duplicate",
),
(
"| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 2 | nav | [导航](components/nav.mq.md#导航) | auto |\n",
"web.layout.row_order",
),
];
for (table, expected) in cases {
let project = TempProject::new();
project.write(
"index.mq.md",
&format!("# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n{table}"),
);
project.write("components/nav.mq.md", NAV);
project.write("components/leaf.mq.md", LEAF);
let error = load(&project.config("index.mq.md", "工作台")).unwrap_err();
assert_eq!(design_code(error), *expected, "table case: {table}");
}
}
#[test]
fn rejects_scope_anchor_and_cycle_errors() {
let outside = TempProject::new();
outside.write(
"index.mq.md",
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [外部](../../outside.mq.md#目标) | auto |\n",
);
outside.write(
"outside.mq.md",
"# 目标页面\n\n## @knowledge 目标\n\n### 内容\n\nx\n",
);
// Analysis reports an out-of-scope link without a usable candidate path, so
// the deterministic rejection is the candidate diagnostic (never a guess).
assert_eq!(
design_code(load(&outside.config("index.mq.md", "工作台")).unwrap_err()),
"web.link.candidate"
);
let missing = basic_project();
missing.write(
"components/nav.mq.md",
"# 导航组件\n\n## @knowledge 导航\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | leaf | [缺失](leaf.mq.md#不存在) | auto |\n",
);
assert_eq!(
design_code(load(&missing.config("index.mq.md", "工作台")).unwrap_err()),
"web.unit.anchor_missing"
);
let cycle = TempProject::new();
cycle.write(
"index.mq.md",
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [甲](components/a.mq.md#甲) | auto |\n",
);
cycle.write(
"components/a.mq.md",
"# 甲页面\n\n## @knowledge 甲\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | b | [乙](b.mq.md#乙) | auto |\n",
);
cycle.write(
"components/b.mq.md",
"# 乙页面\n\n## @knowledge 乙\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [甲](a.mq.md#甲) | auto |\n",
);
let cycle_error = load(&cycle.config("index.mq.md", "工作台")).unwrap_err();
let cycle_code = match &cycle_error {
Error::Design(items) => items[0].code,
Error::Engine(items) => items[0].code,
};
assert_eq!(cycle_code, "web.link.cycle", "{cycle_error:?}");
}
#[test]
fn rejects_engine_failures_and_limits() {
let project = basic_project();
let mut config = project.config("index.mq.md", "工作台");
config.cli = PathBuf::from("/nonexistent/marqdo");
assert_eq!(
engine_code(load(&config).unwrap_err()),
"web.engine.cli_missing"
);
let mut limited = project.config("index.mq.md", "工作台");
limited.max_modules = 1;
assert_eq!(
design_code(load(&limited).unwrap_err()),
"web.limit.modules"
);
let mut shallow = project.config("index.mq.md", "工作台");
shallow.max_depth = 0;
assert_eq!(design_code(load(&shallow).unwrap_err()), "web.limit.depth");
let mut outside_entry = project.config("index.mq.md", "工作台");
outside_entry.root = project.path.join("components");
assert_eq!(
design_code(load(&outside_entry).unwrap_err()),
"web.unit.entry_outside_root"
);
}
#[test]
fn entry_must_be_mq_md_and_expose_analysis_schema() {
let project = basic_project();
let config = project.config("index.mq.md", "工作台");
// A missing entry is an engine/input failure, not a design violation.
let mut missing = config.clone();
missing.entry = project.path.join("nope.mq.md");
assert_eq!(engine_code(load(&missing).unwrap_err()), "web.engine.entry");
}
const STATES: &str =
"# 状态内容\n\n## @knowledge 空状态\n\n暂无数据。\n\n## @knowledge 成功\n\n保存成功。\n";
#[test]
fn loads_full_leaf_contract_inputs_events_scenarios_interactions_dependencies() {
let project = TempProject::new();
project.write(
"index.mq.md",
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [任务面板](components/a.mq.md#甲) | 1fr |\n| 1 | b | [详情](components/b.mq.md#乙) | 2fr |\n",
);
project.write(
"components/a.mq.md",
"# A 组件\n\n## @knowledge 甲\n\n### 输入\n\n| 名称 | 类型 | 说明 |\n| --- | --- | --- |\n| 项目 | Text | 项目名 |\n\n### 事件\n\n| 名称 | 数据 | 说明 |\n| --- | --- | --- |\n| 提交 | Result | 提交结果 |\n\n### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| empty | [空状态](states.mq.md#空状态) | true |\n| done | [成功](states.mq.md#成功) | false |\n\n### 交互\n\n| 当前 | 事件 | 下一状态 |\n| --- | --- | --- |\n| empty | 模拟保存 | done |\n\n### 依赖\n\n| 组件 | 原因 |\n| --- | --- |\n| [详情](b.mq.md#乙) | 提交后展示详情 |\n\n### 验收\n\n- 模拟保存后显示成功状态。\n",
);
project.write("components/states.mq.md", STATES);
project.write(
"components/b.mq.md",
"# B 组件\n\n## @knowledge 乙\n\n### 内容\n\n详情正文。\n",
);
let design = load(&project.config("index.mq.md", "工作台")).expect("full contract load");
let a = &design.definitions["components/a.mq.md#甲"];
assert_eq!(a.inputs.len(), 1);
assert_eq!(a.inputs[0].name, "项目");
assert_eq!(a.events.len(), 1);
assert_eq!(a.events[0].name, "提交");
assert_eq!(a.scenarios.len(), 2);
assert_eq!(a.scenarios.iter().filter(|scene| scene.initial).count(), 1);
assert_eq!(a.scenarios[0].content.anchor, "空状态");
assert_eq!(a.interactions.len(), 1);
assert_eq!(a.interactions[0].next, "done");
assert_eq!(a.dependencies.len(), 1);
assert_eq!(a.dependencies[0].target, "components/b.mq.md#乙");
assert!(a.acceptance.as_ref().unwrap().text.contains("模拟保存"));
let text = handoff(&design);
assert!(text.contains("scenario `empty` (initial)"));
assert!(text.contains("transition `empty` --模拟保存--> `done`"));
assert!(text.contains("dependency `components/b.mq.md#乙`"));
assert!(text.contains("acceptance:"));
}
fn leaf_project(sections: &str) -> TempProject {
let project = TempProject::new();
project.write(
"a.mq.md",
&format!("# A 组件\n\n## @knowledge 甲\n\n{sections}"),
);
project.write("states.mq.md", STATES);
project
}
fn scene_error(sections: &str) -> &'static str {
let project = leaf_project(sections);
design_code(load(&project.config("a.mq.md", "甲")).unwrap_err())
}
#[test]
fn rejects_invalid_scene_and_interaction_tables() {
let cases: &[(&str, &str)] = &[
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n| b | [成](states.mq.md#成功) | true |\n\n### 交互\n\n| 当前 | 事件 | 下一状态 |\n| --- | --- | --- |\n| a | go | b |\n",
"web.scene.initial",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n| a | [成](states.mq.md#成功) | false |\n",
"web.scene.state_duplicate",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| Bad | [空](states.mq.md#空状态) | true |\n",
"web.scene.state",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | 纯文本 | true |\n",
"web.link.none",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [自己](#甲) | true |\n",
"web.scene.content_component",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n| b | [成](states.mq.md#成功) | false |\n",
"web.interaction.missing",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n| b | [成](states.mq.md#成功) | false |\n\n### 交互\n\n| 当前 | 事件 | 下一状态 |\n| --- | --- | --- |\n| b | back | a |\n",
"web.interaction.unreachable",
),
(
"### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n\n### 交互\n\n| 当前 | 事件 | 下一状态 |\n| --- | --- | --- |\n| a | go | missing |\n",
"web.interaction.state",
),
(
"### 交互\n\n| 当前 | 事件 | 下一状态 |\n| --- | --- | --- |\n| a | go | b |\n",
"web.interaction.without_scene",
),
];
for (sections, expected) in cases {
assert_eq!(scene_error(sections), *expected, "case: {sections}");
}
}
#[test]
fn scenes_are_only_allowed_on_leaf_components() {
let project = TempProject::new();
project.write(
"a.mq.md",
"# A 组件\n\n## @knowledge 甲\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | child | [B](b.mq.md#乙) | auto |\n\n### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| a | [空](states.mq.md#空状态) | true |\n",
);
project.write("b.mq.md", "# B 组件\n\n## @knowledge 乙\n\n### 内容\n\nx\n");
project.write("states.mq.md", STATES);
assert_eq!(
design_code(load(&project.config("a.mq.md", "甲")).unwrap_err()),
"web.scene.not_leaf"
);
}
#[test]
fn rejects_invalid_inputs_dependencies_and_acceptance() {
let project = leaf_project(
"### 输入\n\n| 名称 | 类型 | 说明 |\n| --- | --- | --- |\n| x | T | d |\n| x | T | d |\n",
);
assert_eq!(
design_code(load(&project.config("a.mq.md", "甲")).unwrap_err()),
"web.input.name"
);
let linked = leaf_project(
"### 输入\n\n| 名称 | 类型 | 说明 |\n| --- | --- | --- |\n| [x](states.mq.md#空状态) | T | d |\n",
);
assert_eq!(
design_code(load(&linked.config("a.mq.md", "甲")).unwrap_err()),
"web.input.text"
);
let unknown = leaf_project(
"### 依赖\n\n| 组件 | 原因 |\n| --- | --- |\n| [目标](target.mq.md#目标) | 需要 |\n",
);
unknown.write(
"target.mq.md",
"# 目标页面\n\n## @knowledge 目标\n\n### 内容\n\nx\n",
);
assert_eq!(
design_code(load(&unknown.config("a.mq.md", "甲")).unwrap_err()),
"web.dependency.unknown"
);
let empty = leaf_project("### 验收\n\n \n");
assert_eq!(
design_code(load(&empty.config("a.mq.md", "甲")).unwrap_err()),
"web.acceptance.empty"
);
}
#[test]
fn rejects_dependency_cycles() {
let project = TempProject::new();
project.write(
"index.mq.md",
"# 主页面\n\n## @knowledge 页面\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [A](a.mq.md#甲) | auto |\n| 1 | b | [B](b.mq.md#乙) | auto |\n",
);
project.write(
"a.mq.md",
"# A 组件\n\n## @knowledge 甲\n\n### 依赖\n\n| 组件 | 原因 |\n| --- | --- |\n| [B](b.mq.md#乙) | 组合 |\n",
);
project.write(
"b.mq.md",
"# B 组件\n\n## @knowledge 乙\n\n### 依赖\n\n| 组件 | 原因 |\n| --- | --- |\n| [A](a.mq.md#甲) | 组合 |\n",
);
assert_eq!(
design_code(load(&project.config("index.mq.md", "页面")).unwrap_err()),
"web.dependency.cycle"
);
}
#[test]
fn enforces_per_file_and_slot_limits() {
let project = basic_project();
let mut per_file = project.config("index.mq.md", "工作台");
per_file.max_file_bytes = 16;
assert_eq!(
design_code(load(&per_file).unwrap_err()),
"web.limit.source"
);
let wide = TempProject::new();
let mut table = String::from("| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n");
for slot in 0..13 {
table.push_str(&format!(
"| 1 | s{slot} | [叶子](leaf.mq.md#叶子) | auto |\n"
));
}
wide.write(
"index.mq.md",
&format!("# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n{table}"),
);
wide.write("leaf.mq.md", LEAF);
assert_eq!(
design_code(load(&wide.config("index.mq.md", "工作台")).unwrap_err()),
"web.layout.slots_per_row"
);
}