feat: WEB-01 core Design interpreter over public Analysis 3

Implement the first real Design capability in the unique owner repository:
- src/analysis.rs runs the verified CLI check --emit with direct argv, validates
  Analysis 3 schema/source_digest, and snapshots one module without any implicit
  discovery or Markdown parser.
- src/document.rs selects the exact Knowledge H2 by anchor, direct retained H3
  sections, and the single GFM 组件 table from the analysis AST; cell text is
  read from the analysis-provided source by UTF-8 byte span.
- src/lib.rs binds design links by exact candidate_path + anchor (never label or
  filename guessing), validates rows/slots/widths, recursion, containment cycles
  and module/depth/byte limits, and builds deterministic component definitions,
  separate instances, source identities and a provisional handoff.
- Stable diagnostics separate Engine failures (exit 2) from author-contract
  errors (exit 1); no partial model on failure.
- tests/design_contract.rs exercises the real CLI for recursive layout,
  definition reuse, width forms and the main negative cases; scripts/ci.sh is the
  explicit entry (fmt/clippy/test) with a verified CLI.

Scenarios/input/event/dependency contracts and the full WEB-03 handoff remain
open and are recorded as unverified in the evidence document.

Refs cflmy/marqdo-web-design#1, cflmy/marqdo#6
This commit was merged in pull request #3.
This commit is contained in:
cflmy committed 2026-11-27 02:33:33 +08:00
1 parent 39c99e59d2
commit 75cd26ef89
13 files changed
+1649 -15

No files matched your search

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