mtkit 1.16.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mtkit/__init__.py +36 -0
- mtkit/__main__.py +6 -0
- mtkit/asset.py +101 -0
- mtkit/audit.py +220 -0
- mtkit/builder.py +100 -0
- mtkit/cli.py +885 -0
- mtkit/conf.py +63 -0
- mtkit/coverage.py +175 -0
- mtkit/db.py +676 -0
- mtkit/evolve.py +142 -0
- mtkit/extract/__init__.py +17 -0
- mtkit/extract/code_facts.py +631 -0
- mtkit/extract/docx_loader.py +115 -0
- mtkit/extract/xmind_loader.py +125 -0
- mtkit/generate/__init__.py +10 -0
- mtkit/generate/llm.py +360 -0
- mtkit/generate/prompts.py +178 -0
- mtkit/generate/skeleton.py +59 -0
- mtkit/normalize.py +480 -0
- mtkit/parsers.py +318 -0
- mtkit/review.py +274 -0
- mtkit/selfheal.py +110 -0
- mtkit/tb.py +191 -0
- mtkit/track.py +309 -0
- mtkit/validator.py +174 -0
- mtkit/writer.py +137 -0
- mtkit-1.16.1.dist-info/METADATA +150 -0
- mtkit-1.16.1.dist-info/RECORD +31 -0
- mtkit-1.16.1.dist-info/WHEEL +5 -0
- mtkit-1.16.1.dist-info/entry_points.txt +2 -0
- mtkit-1.16.1.dist-info/top_level.txt +1 -0
mtkit/__init__.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
mtkit - AI 全站测试工程师
|
|
4
|
+
|
|
5
|
+
生成模式:计划先行(含测试设计方法)→ 授权 → 契约 → 质量门禁 → 标准 XMind 用例;
|
|
6
|
+
指导模式:测试答疑/问题定位/缺陷研判/需求澄清/回归建议,交互式指导。
|
|
7
|
+
使用即训练、授权后可自我迭代。
|
|
8
|
+
|
|
9
|
+
约定:
|
|
10
|
+
- 节点为 dict: {"t": 标题, "c": [子节点...]},子节点可省略;也兼容 (标题, [子节点]) 元组树与字符串叶子
|
|
11
|
+
- 用例节点标题以 "tc-" 开头,描述风格两种:
|
|
12
|
+
zen 风格(pc: / 步骤: / 预期: 平铺行)与 history 风格(操作步骤/预期结果 多级节点)
|
|
13
|
+
- 输出为 XMind Zen 格式(标准 zip,含 content.json / metadata.json / manifest.json)
|
|
14
|
+
|
|
15
|
+
用法:
|
|
16
|
+
mtkit gen -i cases.json -o out.xmind
|
|
17
|
+
mtkit gen-doc -i 方案.docx --hist 历史.xmind -o out.xmind # 骨架(无 LLM)
|
|
18
|
+
mtkit gen-doc -i 方案.docx --llm http --endpoint ... -o out.xmind # LLM 生成
|
|
19
|
+
mtkit facts -r 代码仓库 -o facts.json
|
|
20
|
+
mtkit diff -i out.xmind -f facts.json -d 方案.docx
|
|
21
|
+
mtkit validate -i out.xmind
|
|
22
|
+
mtkit info -i out.xmind
|
|
23
|
+
mtkit db check -c db.json # 数据库接入(严格只读)
|
|
24
|
+
mtkit db schema -c db.json -o schema.json
|
|
25
|
+
mtkit db verify -c db.json -i 契约.json
|
|
26
|
+
mtkit review -i 契约.json -f facts.json # 契约质量门禁
|
|
27
|
+
mtkit audit -i 用例.xmind # 用例资产体检
|
|
28
|
+
"""
|
|
29
|
+
__version__ = "1.16.1"
|
|
30
|
+
|
|
31
|
+
from .builder import build_sheet
|
|
32
|
+
from .validator import check_tree
|
|
33
|
+
from .writer import write_xmind, read_xmind
|
|
34
|
+
from . import tb
|
|
35
|
+
|
|
36
|
+
__all__ = ["build_sheet", "check_tree", "write_xmind", "read_xmind", "tb", "__version__"]
|
mtkit/__main__.py
ADDED
mtkit/asset.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Case asset metadata: sidecar written at render time, supports asset stale detection.
|
|
4
|
+
|
|
5
|
+
Metadata file: <xmind path>.meta.json (does not touch the xmind structure)
|
|
6
|
+
{
|
|
7
|
+
"asset": "xxx.xmind",
|
|
8
|
+
"generated_at": "2026-11-25T10:00:00",
|
|
9
|
+
"tool_version": "1.15.7",
|
|
10
|
+
"facts_hash": "md5 first 12 chars (optional)",
|
|
11
|
+
"case_count": 120,
|
|
12
|
+
"audited_at": "last audit time (optional)"
|
|
13
|
+
}
|
|
14
|
+
"""
|
|
15
|
+
import hashlib
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
|
|
20
|
+
META_SUFFIX = ".meta.json"
|
|
21
|
+
STALE_DEFAULT_DAYS = 30
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def write_meta(asset_path, case_count=0, facts_hash="", tool_version="", audited=False):
|
|
25
|
+
"""Write/update asset metadata sidecar (create if missing), return metadata path."""
|
|
26
|
+
meta_path = asset_path + META_SUFFIX
|
|
27
|
+
meta = read_meta(asset_path) or {}
|
|
28
|
+
meta.setdefault("asset", os.path.basename(asset_path))
|
|
29
|
+
meta["case_count"] = case_count
|
|
30
|
+
if facts_hash:
|
|
31
|
+
meta["facts_hash"] = facts_hash
|
|
32
|
+
if tool_version:
|
|
33
|
+
meta["tool_version"] = tool_version
|
|
34
|
+
meta["generated_at"] = meta.get("generated_at") or datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
|
35
|
+
if audited:
|
|
36
|
+
meta["audited_at"] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
|
37
|
+
with open(meta_path, "w", encoding="utf-8") as fp:
|
|
38
|
+
json.dump(meta, fp, ensure_ascii=False, indent=2)
|
|
39
|
+
return meta_path
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def read_meta(asset_path):
|
|
43
|
+
"""Read asset metadata (return None when missing)."""
|
|
44
|
+
meta_path = asset_path + META_SUFFIX
|
|
45
|
+
if not os.path.isfile(meta_path):
|
|
46
|
+
return None
|
|
47
|
+
with open(meta_path, "r", encoding="utf-8-sig") as fp:
|
|
48
|
+
return json.load(fp)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def facts_digest(facts):
|
|
52
|
+
"""facts summary hash (md5 first 12 chars), marks the code-facts version of the asset."""
|
|
53
|
+
payload = json.dumps(facts or {}, ensure_ascii=False, sort_keys=True, default=str)
|
|
54
|
+
return hashlib.md5(payload.encode("utf-8")).hexdigest()[:12]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def asset_age_days(asset_path, now=None):
|
|
58
|
+
"""Asset age in days: prefer metadata audited_at/generated_at, else file mtime."""
|
|
59
|
+
meta = read_meta(asset_path)
|
|
60
|
+
stamp = None
|
|
61
|
+
last = None
|
|
62
|
+
if meta:
|
|
63
|
+
stamp = meta.get("audited_at") or meta.get("generated_at")
|
|
64
|
+
if stamp:
|
|
65
|
+
try:
|
|
66
|
+
last = datetime.strptime(stamp[:19], "%Y-%m-%dT%H:%M:%S")
|
|
67
|
+
except ValueError:
|
|
68
|
+
last = None
|
|
69
|
+
if last is None and os.path.isfile(asset_path):
|
|
70
|
+
last = datetime.fromtimestamp(os.path.getmtime(asset_path))
|
|
71
|
+
if last is None:
|
|
72
|
+
return 0
|
|
73
|
+
now = now or datetime.now()
|
|
74
|
+
days = (now - last).days
|
|
75
|
+
return days if days > 0 else 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def stale_assets(directory, stale_days=STALE_DEFAULT_DAYS, now=None):
|
|
79
|
+
"""Scan .xmind assets under a directory, return those unaudited for more than stale_days.
|
|
80
|
+
|
|
81
|
+
Discovery is dual-source: .xmind files plus orphan .meta.json sidecars.
|
|
82
|
+
|
|
83
|
+
:return: [{"asset": path, "age_days": n, "meta": metadata or None}]
|
|
84
|
+
"""
|
|
85
|
+
results = []
|
|
86
|
+
if not os.path.isdir(directory):
|
|
87
|
+
return results
|
|
88
|
+
assets = set()
|
|
89
|
+
for root, dirs, files in os.walk(directory):
|
|
90
|
+
for name in files:
|
|
91
|
+
if name.endswith(".xmind"):
|
|
92
|
+
assets.add(os.path.join(root, name))
|
|
93
|
+
elif name.endswith(META_SUFFIX):
|
|
94
|
+
# orphan sidecar without the .xmind file itself
|
|
95
|
+
asset = os.path.join(root, name[: -len(META_SUFFIX)])
|
|
96
|
+
assets.add(asset)
|
|
97
|
+
for asset in sorted(assets):
|
|
98
|
+
age = asset_age_days(asset, now=now)
|
|
99
|
+
if age > stale_days:
|
|
100
|
+
results.append({"asset": asset, "age_days": age, "meta": read_meta(asset)})
|
|
101
|
+
return results
|
mtkit/audit.py
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Case asset audit: rule-level checks on an existing case tree (read from .xmind), finding unsuitable cases.
|
|
4
|
+
|
|
5
|
+
check items:
|
|
6
|
+
1. structure issues: reuse validator.check_tree (case nodes missing pc/steps/expects, non-compliant titles)
|
|
7
|
+
2. SQL readonly & executability: 「SQL核对」 nodes in the tree checked one by one (write ops rejected; missing tables/columns fail)
|
|
8
|
+
3. coverage gaps: reuse coverage.compare_interfaces/compare_features (interfaces/features vs case tree)
|
|
9
|
+
4. granularity outliers: cases with >8 steps or >200-char expect text flagged as 「粒度存疑」
|
|
10
|
+
|
|
11
|
+
product: issue list + coverage matrix + granularity stats; caller exits 1 when issues exist.
|
|
12
|
+
"""
|
|
13
|
+
import re
|
|
14
|
+
|
|
15
|
+
from .builder import CASE_TITLE_PATTERN
|
|
16
|
+
from .coverage import compare_features, compare_interfaces
|
|
17
|
+
from .db import check_readonly
|
|
18
|
+
from .normalize import SQL_TITLE_PATTERN, detect_style
|
|
19
|
+
from .validator import check_tree
|
|
20
|
+
|
|
21
|
+
MAX_STEPS = 8
|
|
22
|
+
MAX_EXPECT_LEN = 200
|
|
23
|
+
|
|
24
|
+
# circled numbers inside zen style step lines
|
|
25
|
+
_CIRCLE_STEP_PATTERN = re.compile(r"[①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮]")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def extract_sqls_from_tree(tree):
|
|
29
|
+
"""Walk the case tree, collect (case_title, sql_text).
|
|
30
|
+
|
|
31
|
+
- zen style: node title 「SQL核对: SELECT ...」, take the text after the colon
|
|
32
|
+
- history style: child node text of the 「SQL核对」 node
|
|
33
|
+
"""
|
|
34
|
+
results = []
|
|
35
|
+
|
|
36
|
+
def walk(nodes, case_title=""):
|
|
37
|
+
for node in nodes:
|
|
38
|
+
title = node.get("t") or ""
|
|
39
|
+
if CASE_TITLE_PATTERN.match(title):
|
|
40
|
+
case_title = title
|
|
41
|
+
if SQL_TITLE_PATTERN.search(title):
|
|
42
|
+
text = re.sub(r"^\s*SQL核对[::]?\s*", "", title, flags=re.IGNORECASE).strip()
|
|
43
|
+
if not text:
|
|
44
|
+
for child in node.get("c") or []:
|
|
45
|
+
text = (child.get("t") or "").strip()
|
|
46
|
+
if text:
|
|
47
|
+
break
|
|
48
|
+
if text:
|
|
49
|
+
results.append((case_title, text))
|
|
50
|
+
walk(node.get("c") or [], case_title)
|
|
51
|
+
|
|
52
|
+
walk(tree)
|
|
53
|
+
return results
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _count_steps(case_node):
|
|
57
|
+
"""Count steps in a case node subtree (zen circled numbers / history child count)."""
|
|
58
|
+
def walk(nodes, steps):
|
|
59
|
+
for node in nodes:
|
|
60
|
+
title = node.get("t") or ""
|
|
61
|
+
if title.startswith("步骤:") or title.startswith("操作步骤"):
|
|
62
|
+
circles = _CIRCLE_STEP_PATTERN.findall(title)
|
|
63
|
+
if circles:
|
|
64
|
+
steps[0] = max(steps[0], len(circles))
|
|
65
|
+
else:
|
|
66
|
+
children = node.get("c") or []
|
|
67
|
+
steps[0] = max(steps[0], len(children))
|
|
68
|
+
walk(node.get("c") or [], steps)
|
|
69
|
+
|
|
70
|
+
steps = [0]
|
|
71
|
+
walk(case_node.get("c") or [], steps)
|
|
72
|
+
return steps[0]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _expect_length(case_node):
|
|
76
|
+
"""Total expect text length of a case node subtree (zen line length / history subtree join)."""
|
|
77
|
+
def walk(nodes, length):
|
|
78
|
+
for node in nodes:
|
|
79
|
+
title = node.get("t") or ""
|
|
80
|
+
if title.startswith("预期:") or title.startswith("预期结果"):
|
|
81
|
+
length[0] = max(length[0], len(title))
|
|
82
|
+
walk(node.get("c") or [], length)
|
|
83
|
+
else:
|
|
84
|
+
walk(node.get("c") or [], length)
|
|
85
|
+
|
|
86
|
+
length = [0]
|
|
87
|
+
walk(case_node.get("c") or [], length)
|
|
88
|
+
return length[0]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def grain_stats(tree):
|
|
92
|
+
"""Case granularity stats: per-case steps/expect length, flag outlier cases."""
|
|
93
|
+
cases = []
|
|
94
|
+
outliers = []
|
|
95
|
+
|
|
96
|
+
def walk(nodes):
|
|
97
|
+
for node in nodes:
|
|
98
|
+
title = node.get("t") or ""
|
|
99
|
+
if CASE_TITLE_PATTERN.match(title):
|
|
100
|
+
steps = _count_steps(node)
|
|
101
|
+
expect_len = _expect_length(node)
|
|
102
|
+
cases.append({"case": title, "steps": steps, "expect_len": expect_len})
|
|
103
|
+
if steps > MAX_STEPS:
|
|
104
|
+
outliers.append({"case": title,
|
|
105
|
+
"message": "步骤数 %d 超过 %d,粒度存疑" % (steps, MAX_STEPS)})
|
|
106
|
+
if expect_len > MAX_EXPECT_LEN:
|
|
107
|
+
outliers.append({"case": title,
|
|
108
|
+
"message": "预期文本 %d 字超过 %d,粒度存疑" % (expect_len, MAX_EXPECT_LEN)})
|
|
109
|
+
walk(node.get("c") or [])
|
|
110
|
+
|
|
111
|
+
walk(tree)
|
|
112
|
+
return {"cases": cases, "outliers": outliers,
|
|
113
|
+
"max_steps": MAX_STEPS, "max_expect_len": MAX_EXPECT_LEN}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def audit_xmind(tree, facts=None, provider=None, style=None):
|
|
117
|
+
"""Audit main entry.
|
|
118
|
+
|
|
119
|
+
:param tree: case tree (read_tree output)
|
|
120
|
+
:param facts: facts.json output (optional)
|
|
121
|
+
:param provider: database provider (optional, SQL executability check)
|
|
122
|
+
:param style: zen/history (auto detect by default)
|
|
123
|
+
:return: {issues: [{case, rule, message}], coverage: {...}, stats: {...}}
|
|
124
|
+
"""
|
|
125
|
+
style = style or detect_style(tree)
|
|
126
|
+
facts = facts or {}
|
|
127
|
+
issues = []
|
|
128
|
+
|
|
129
|
+
# 1. structure
|
|
130
|
+
for message in check_tree(tree, style=style):
|
|
131
|
+
issues.append({"case": "", "rule": "结构", "message": message})
|
|
132
|
+
|
|
133
|
+
# 2. SQL readonly & executability
|
|
134
|
+
for case_title, sql in extract_sqls_from_tree(tree):
|
|
135
|
+
ok, reason = check_readonly(sql)
|
|
136
|
+
if not ok:
|
|
137
|
+
issues.append({"case": case_title, "rule": "SQL只读", "message": reason})
|
|
138
|
+
continue
|
|
139
|
+
if provider is not None:
|
|
140
|
+
try:
|
|
141
|
+
provider.explain(sql)
|
|
142
|
+
except Exception as exc:
|
|
143
|
+
issues.append({"case": case_title, "rule": "SQL可执行", "message": str(exc)})
|
|
144
|
+
|
|
145
|
+
# 3. coverage gaps
|
|
146
|
+
coverage = {
|
|
147
|
+
"interfaces": compare_interfaces(facts, tree) if facts.get("interfaces") else {"covered": [], "missing": []},
|
|
148
|
+
"features": compare_features(facts, tree) if facts.get("features") else {"covered": [], "missing": []},
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
# 4. granularity outliers
|
|
152
|
+
stats = grain_stats(tree)
|
|
153
|
+
for outlier in stats["outliers"]:
|
|
154
|
+
issues.append({"case": outlier["case"], "rule": "粒度", "message": outlier["message"]})
|
|
155
|
+
|
|
156
|
+
return {"issues": issues, "coverage": coverage, "stats": stats}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def render_audit_report(result):
|
|
160
|
+
"""Audit report markdown."""
|
|
161
|
+
issues = result["issues"]
|
|
162
|
+
coverage = result["coverage"]
|
|
163
|
+
stats = result["stats"]
|
|
164
|
+
lines = ["用例资产体检报告", ""]
|
|
165
|
+
lines.append("| 检查项 | 结果 |")
|
|
166
|
+
lines.append("|---|---|")
|
|
167
|
+
lines.append("| 用例总数 | %d |" % len(stats["cases"]))
|
|
168
|
+
lines.append("| 问题总数 | %d |" % len(issues))
|
|
169
|
+
lines.append("| 接口覆盖 | %d/%d |" % (
|
|
170
|
+
len(coverage["interfaces"]["covered"]),
|
|
171
|
+
len(coverage["interfaces"]["covered"]) + len(coverage["interfaces"]["missing"])))
|
|
172
|
+
lines.append("| 功能点覆盖 | %d/%d |" % (
|
|
173
|
+
len(coverage["features"]["covered"]),
|
|
174
|
+
len(coverage["features"]["covered"]) + len(coverage["features"]["missing"])))
|
|
175
|
+
lines.append("")
|
|
176
|
+
if issues:
|
|
177
|
+
lines.append("## 问题清单")
|
|
178
|
+
lines.append("")
|
|
179
|
+
by_rule = {}
|
|
180
|
+
for issue in issues:
|
|
181
|
+
by_rule.setdefault(issue["rule"], []).append(issue)
|
|
182
|
+
for rule in ("结构", "SQL只读", "SQL可执行", "粒度"):
|
|
183
|
+
for issue in by_rule.pop(rule, []):
|
|
184
|
+
lines.append("- [%s] %s | %s" % (issue["rule"], issue["case"], issue["message"]))
|
|
185
|
+
for rule, items in by_rule.items():
|
|
186
|
+
for issue in items:
|
|
187
|
+
lines.append("- [%s] %s | %s" % (issue["rule"], issue["case"], issue["message"]))
|
|
188
|
+
lines.append("")
|
|
189
|
+
else:
|
|
190
|
+
lines.append("## 问题清单")
|
|
191
|
+
lines.append("")
|
|
192
|
+
lines.append("无问题")
|
|
193
|
+
lines.append("")
|
|
194
|
+
lines.append("## 覆盖矩阵")
|
|
195
|
+
lines.append("")
|
|
196
|
+
for key, label in (("interfaces", "接口"), ("features", "功能点")):
|
|
197
|
+
missing = coverage[key]["missing"]
|
|
198
|
+
covered = coverage[key]["covered"]
|
|
199
|
+
if covered:
|
|
200
|
+
lines.append("%s已覆盖:" % label)
|
|
201
|
+
for item in covered:
|
|
202
|
+
lines.append("- %s" % (item[0] if isinstance(item, (list, tuple)) else item))
|
|
203
|
+
lines.append("")
|
|
204
|
+
if missing:
|
|
205
|
+
lines.append("%s未覆盖:" % label)
|
|
206
|
+
for item in missing:
|
|
207
|
+
lines.append("- %s" % item)
|
|
208
|
+
lines.append("")
|
|
209
|
+
if not covered and not missing:
|
|
210
|
+
lines.append("%s:无对比数据" % label)
|
|
211
|
+
lines.append("")
|
|
212
|
+
lines.append("## 粒度统计")
|
|
213
|
+
lines.append("")
|
|
214
|
+
lines.append("| 用例 | 步骤数 | 预期长度 |")
|
|
215
|
+
lines.append("|---|---|---|")
|
|
216
|
+
for item in stats["cases"]:
|
|
217
|
+
lines.append("| %s | %s | %s |" % (item["case"], item["steps"] or "-", item["expect_len"] or "-"))
|
|
218
|
+
lines.append("")
|
|
219
|
+
lines.append("阈值:步骤 >%d 或预期 >%d 字标记粒度存疑" % (stats["max_steps"], stats["max_expect_len"]))
|
|
220
|
+
return "\n".join(lines)
|
mtkit/builder.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
XMind Zen format building: tree data -> sheet structure
|
|
4
|
+
|
|
5
|
+
node convention (consistent with the E:\test_case legacy generation script):
|
|
6
|
+
{"t": "title", "c": [child nodes...]}
|
|
7
|
+
|
|
8
|
+
generated structure:
|
|
9
|
+
[sheet] -> rootTopic -> children.attached -> [topic...]
|
|
10
|
+
"""
|
|
11
|
+
import re
|
|
12
|
+
import uuid
|
|
13
|
+
|
|
14
|
+
# case node title prefix: tc + CN/EN colon or dash (compatible with tc:/tc:/tc- and tb import formats)
|
|
15
|
+
CASE_TITLE_PATTERN = re.compile(r"^tc\s*[::-]", re.IGNORECASE)
|
|
16
|
+
|
|
17
|
+
# flat case title prefix (legacy convention, identifies zen style case nodes)
|
|
18
|
+
CASE_PREFIX = "tc-"
|
|
19
|
+
|
|
20
|
+
# required description line prefixes for a case (validation)
|
|
21
|
+
REQUIRED_SECTION_PREFIXES = ("pc:", "步骤:", "预期:")
|
|
22
|
+
|
|
23
|
+
# XMind Zen default metadata
|
|
24
|
+
DEFAULT_METADATA = {
|
|
25
|
+
"dataStructureVersion": "3",
|
|
26
|
+
"layoutEngineVersion": "5",
|
|
27
|
+
"creator": {"name": "AI", "version": "26.05.01107"},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
DEFAULT_MANIFEST = {"file-entries": {"content.json": {}, "metadata.json": {}}}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def new_id():
|
|
34
|
+
"""Generate a 24-char hex node ID (consistent with the XMind Zen format)"""
|
|
35
|
+
return uuid.uuid4().hex[:24]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def build_topic(node):
|
|
39
|
+
"""Recursively build a topic node.
|
|
40
|
+
|
|
41
|
+
:param node: dict(t=title, c=[child nodes...])
|
|
42
|
+
:return: XMind topic dict
|
|
43
|
+
"""
|
|
44
|
+
topic = {"id": new_id(), "title": node["t"]}
|
|
45
|
+
children = node.get("c") or []
|
|
46
|
+
if children:
|
|
47
|
+
topic["children"] = {"attached": [build_topic(child) for child in children]}
|
|
48
|
+
return topic
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def build_sheet(tree, root_title, metadata=None, manifest=None):
|
|
52
|
+
"""Build a case tree into a sheet structure.
|
|
53
|
+
|
|
54
|
+
:param tree: case tree [{t=..., c=[...]}, ...]
|
|
55
|
+
:param root_title: root node title
|
|
56
|
+
:param metadata: metadata.json content, defaults to built-ins
|
|
57
|
+
:param manifest: manifest.json content, defaults to built-ins
|
|
58
|
+
:return: dict(sheet=..., metadata=..., manifest=...)
|
|
59
|
+
"""
|
|
60
|
+
root = {
|
|
61
|
+
"id": new_id(),
|
|
62
|
+
"title": root_title,
|
|
63
|
+
"children": {"attached": [build_topic(child) for child in tree]},
|
|
64
|
+
}
|
|
65
|
+
sheet = {
|
|
66
|
+
"id": new_id(),
|
|
67
|
+
"revisionId": new_id(),
|
|
68
|
+
"class": "sheet",
|
|
69
|
+
"rootTopic": root,
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
"sheet": sheet,
|
|
73
|
+
"metadata": metadata or DEFAULT_METADATA,
|
|
74
|
+
"manifest": manifest or DEFAULT_MANIFEST,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def count_cases(nodes):
|
|
79
|
+
"""Recursively count cases: titles starting with tc[::-] count 1, otherwise recurse into children"""
|
|
80
|
+
total = 0
|
|
81
|
+
for node in nodes:
|
|
82
|
+
if CASE_TITLE_PATTERN.match(node["t"]):
|
|
83
|
+
total += 1
|
|
84
|
+
else:
|
|
85
|
+
total += count_cases(node.get("c") or [])
|
|
86
|
+
return total
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def count_nodes(root_topic):
|
|
90
|
+
"""统计 sheet 下全部 topic 节点数(含根节点)"""
|
|
91
|
+
total = 0
|
|
92
|
+
|
|
93
|
+
def walk(topic):
|
|
94
|
+
nonlocal total
|
|
95
|
+
total += 1
|
|
96
|
+
for child in (topic.get("children") or {}).get("attached") or []:
|
|
97
|
+
walk(child)
|
|
98
|
+
|
|
99
|
+
walk(root_topic)
|
|
100
|
+
return total
|