dsh-memorytree-rule 0.0.0-stage → 1.0.2
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.
- package/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +144 -3
- package/bin/mt.mjs +589 -0
- package/client.js +227 -0
- package/cordis.patch.yml +14 -0
- package/docs/DETAIL.md +69 -0
- package/docs/DETAIL.svg +85 -0
- package/docs/EMOTION.md +99 -0
- package/examples/AGENTS.demo.md +22 -0
- package/examples/AGENTS.template.md +212 -0
- package/icon.svg +17 -0
- package/index.js +418 -0
- package/lib/board.mjs +1113 -0
- package/lib/cards.mjs +56 -0
- package/lib/daily.mjs +140 -0
- package/lib/db.mjs +84 -0
- package/lib/demo.mjs +161 -0
- package/lib/diary.mjs +92 -0
- package/lib/emotion.mjs +474 -0
- package/lib/import-legacy.mjs +167 -0
- package/lib/ingest.mjs +279 -0
- package/lib/levels.mjs +40 -0
- package/lib/paths.mjs +85 -0
- package/lib/report.mjs +2130 -0
- package/lib/rules.mjs +635 -0
- package/lib/runtime.mjs +232 -0
- package/lib/schema.mjs +363 -0
- package/lib/search.mjs +264 -0
- package/lib/store.mjs +1334 -0
- package/lib/text.mjs +161 -0
- package/lib/tools.mjs +962 -0
- package/lib/vector.mjs +51 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +81 -6
- package/tests/emotion.test.mjs +215 -0
- package/tests/memory-tree.test.mjs +620 -0
- package/tests/regressions.test.mjs +834 -0
- package/tests/renumber.test.mjs +276 -0
- package/tests/trunk-bucket.test.mjs +225 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// tests/renumber.test.mjs — 库内编号编辑、重排、引用联动与待写入池的纯 Node 单测。
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { openMemoryDb } from "../lib/db.mjs";
|
|
8
|
+
import { MemoryStore } from "../lib/store.mjs";
|
|
9
|
+
import { syncTrunk } from "../lib/ingest.mjs";
|
|
10
|
+
import { normalizeNumberInput, parseRuleNum, parseAgentSections, planRenumber, removeAgentSectionRule, replaceAgentSectionRule, trunkGroups } from "../lib/rules.mjs";
|
|
11
|
+
import { runBoardAction } from "../lib/board.mjs";
|
|
12
|
+
import { checksum } from "../lib/text.mjs";
|
|
13
|
+
import { MemoryRuntime } from "../lib/runtime.mjs";
|
|
14
|
+
import { buildReportHtml } from "../lib/report.mjs";
|
|
15
|
+
|
|
16
|
+
const FIXTURE = [
|
|
17
|
+
"# AGENTS.md",
|
|
18
|
+
"",
|
|
19
|
+
"## §1 主干(大脑)",
|
|
20
|
+
"",
|
|
21
|
+
"#### §1-1 基本判据",
|
|
22
|
+
"",
|
|
23
|
+
"- **§1-1-1 甲**:这是甲,见 §1-1-2。",
|
|
24
|
+
"- **§1-1-2 乙**:这是乙,见 §1-1-1。",
|
|
25
|
+
"- **§1-1-3 丙**:这是丙。",
|
|
26
|
+
"",
|
|
27
|
+
"#### §1-2 决策与授权",
|
|
28
|
+
"",
|
|
29
|
+
"- **§1-2-1 前**:这条在 §1-2。",
|
|
30
|
+
"",
|
|
31
|
+
"## §2 情感枝",
|
|
32
|
+
"",
|
|
33
|
+
"内容。",
|
|
34
|
+
"",
|
|
35
|
+
].join("\n");
|
|
36
|
+
|
|
37
|
+
function makeStore() {
|
|
38
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mt-renum-"));
|
|
39
|
+
const db = openMemoryDb(path.join(dir, "memory.db"));
|
|
40
|
+
const store = new MemoryStore(db, { clock: () => Date.now() });
|
|
41
|
+
return { dir, db, store };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function patch(overrides = {}) {
|
|
45
|
+
return {
|
|
46
|
+
id: "p1",
|
|
47
|
+
uid: "trunk:§1-1-3",
|
|
48
|
+
title: "丙",
|
|
49
|
+
oldNum: "§1-1-3",
|
|
50
|
+
newNum: "§1-1-1",
|
|
51
|
+
oldText: "丙:这是丙。",
|
|
52
|
+
newText: "丙:这是丙。",
|
|
53
|
+
kind: "number",
|
|
54
|
+
baseChecksum: checksum(FIXTURE),
|
|
55
|
+
...overrides,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
test("编号输入:三种写法都能归一化,§2 明确拒绝", () => {
|
|
60
|
+
assert.equal(normalizeNumberInput("trunk:§1-1-3").num, "§1-1-3");
|
|
61
|
+
assert.equal(normalizeNumberInput("§1-1-3").num, "§1-1-3");
|
|
62
|
+
assert.equal(normalizeNumberInput("1-1-3").num, "§1-1-3");
|
|
63
|
+
const rejected = normalizeNumberInput("§2-1");
|
|
64
|
+
assert.equal(rejected.ok, false);
|
|
65
|
+
assert.match(rejected.error, /只支持 §1/);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("编号解析:组号/序号/uid", () => {
|
|
69
|
+
assert.deepEqual(parseRuleNum("§1-3-7"), { groupNum: 3, index: 7, group: "§1-3" });
|
|
70
|
+
assert.equal(parseRuleNum("随便"), null);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("分组:#### 标题也能识别,不再只认 ###", () => {
|
|
74
|
+
const groups = trunkGroups(FIXTURE);
|
|
75
|
+
assert.deepEqual(groups.map((g) => g.key), ["§1-1", "§1-2"]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("重排 + 引用联动:§1-1-3 移到最前,后续编号顺延", () => {
|
|
79
|
+
const plan = planRenumber(FIXTURE, [patch()]);
|
|
80
|
+
assert.equal(plan.ok, true, JSON.stringify(plan.conflicts));
|
|
81
|
+
assert.equal(plan.mode, "renumber");
|
|
82
|
+
assert.equal(plan.refsChanged, 2);
|
|
83
|
+
assert.match(plan.newText, /- \*\*§1-1-1 丙\*\*:这是丙。/);
|
|
84
|
+
assert.match(plan.newText, /- \*\*§1-1-2 甲\*\*:这是甲,见 §1-1-3。/);
|
|
85
|
+
assert.match(plan.newText, /- \*\*§1-1-3 乙\*\*:这是乙,见 §1-1-2。/);
|
|
86
|
+
assert.ok(!plan.newText.includes("@@MTNUM_"));
|
|
87
|
+
assert.ok(plan.planToken);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("仅正文:只改目标规则,不重排、不动引用", () => {
|
|
91
|
+
const plan = planRenumber(FIXTURE, [patch({ newNum: "§1-1-3", kind: "text", newText: "丙:改写后的正文。" })]);
|
|
92
|
+
assert.equal(plan.ok, true, JSON.stringify(plan.conflicts));
|
|
93
|
+
assert.equal(plan.mode, "content");
|
|
94
|
+
assert.equal(plan.numberChanged, false);
|
|
95
|
+
assert.equal(plan.refsChanged, 0);
|
|
96
|
+
assert.match(plan.newText, /§1-1-3 丙\*\*:改写后的正文。/);
|
|
97
|
+
assert.match(plan.newText, /§1-1-2 乙\*\*:这是乙,见 §1-1-1。/);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("跨组移动:源组顺延,目标组插入,引用跟着跨组", () => {
|
|
101
|
+
const plan = planRenumber(FIXTURE, [patch({ oldNum: "§1-1-2", newNum: "§1-2-1", uid: "trunk:§1-1-2", title: "乙", oldText: "乙:这是乙,见 §1-1-1。", newText: "乙:这是乙,见 §1-1-1。" })]);
|
|
102
|
+
assert.equal(plan.ok, true, JSON.stringify(plan.conflicts));
|
|
103
|
+
assert.match(plan.newText, /- \*\*§1-1-1 甲\*\*:这是甲,见 §1-2-1。/);
|
|
104
|
+
assert.match(plan.newText, /- \*\*§1-1-2 丙\*\*:这是丙。/);
|
|
105
|
+
assert.match(plan.newText, /- \*\*§1-2-1 乙\*\*:这是乙,见 §1-1-1。/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("冲突:目标号重复、越界、文件已变都拒绝", () => {
|
|
109
|
+
const dup = planRenumber(FIXTURE, [
|
|
110
|
+
patch({ id: "p1", uid: "trunk:§1-1-2", oldNum: "§1-1-2", newNum: "§1-1-1", oldText: "乙:这是乙,见 §1-1-1。", newText: "乙:这是乙,见 §1-1-1。" }),
|
|
111
|
+
patch({ id: "p2" }),
|
|
112
|
+
]);
|
|
113
|
+
assert.equal(dup.ok, false);
|
|
114
|
+
assert.ok(dup.conflicts.some((c) => c.code === "duplicate-target"));
|
|
115
|
+
|
|
116
|
+
const out = planRenumber(FIXTURE, [patch({ newNum: "§1-1-9" })]);
|
|
117
|
+
assert.equal(out.ok, false);
|
|
118
|
+
assert.ok(out.conflicts.some((c) => c.code === "target-out-of-range"));
|
|
119
|
+
|
|
120
|
+
const stale = planRenumber(FIXTURE, [patch({ baseChecksum: "bad" })]);
|
|
121
|
+
assert.equal(stale.ok, false);
|
|
122
|
+
assert.ok(stale.conflicts.some((c) => c.code === "stale-file"));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("集成:暂存 → 检索冲突 → 确认写入,备份并清空待写入", () => {
|
|
126
|
+
const { dir, db, store } = makeStore();
|
|
127
|
+
const agents = path.join(dir, "AGENTS.md");
|
|
128
|
+
fs.writeFileSync(agents, FIXTURE, "utf8");
|
|
129
|
+
const options = { agentsPath: agents, backupDir: path.join(dir, "backups") };
|
|
130
|
+
try {
|
|
131
|
+
syncTrunk(store, agents, { scope: "user" });
|
|
132
|
+
const tr = store.list({ layers: ["trunk"], status: "active" }).find((n) => n.uid === "trunk:§1-1-3");
|
|
133
|
+
assert.ok(tr, "应先同步出 §1-1-3");
|
|
134
|
+
store.setTrunkBucket("trunk:§1-1-3", "移动组");
|
|
135
|
+
|
|
136
|
+
const staged = runBoardAction(store, { op: "stage-patch", id: tr.id, num: "trunk:§1-1-1", text: "丙:这是丙。" }, options);
|
|
137
|
+
assert.equal(staged.ok, true, staged.error);
|
|
138
|
+
assert.equal(store.pendingPatches().length, 1);
|
|
139
|
+
assert.equal(fs.readFileSync(agents, "utf8"), FIXTURE, "暂存时不得改 AGENTS.md");
|
|
140
|
+
|
|
141
|
+
const scan = runBoardAction(store, { op: "scan-patches" }, options);
|
|
142
|
+
assert.equal(scan.ok, true);
|
|
143
|
+
assert.equal(scan.conflicts.length, 0);
|
|
144
|
+
assert.ok(scan.planToken);
|
|
145
|
+
|
|
146
|
+
const applied = runBoardAction(store, { op: "apply-patches", scanToken: scan.planToken }, options);
|
|
147
|
+
assert.equal(applied.ok, true, applied.error);
|
|
148
|
+
const after = fs.readFileSync(agents, "utf8");
|
|
149
|
+
assert.match(after, /- \*\*§1-1-1 丙\*\*:这是丙。/);
|
|
150
|
+
assert.match(after, /- \*\*§1-1-2 甲\*\*:这是甲,见 §1-1-3。/);
|
|
151
|
+
assert.match(after, /- \*\*§1-1-3 乙\*\*:这是乙,见 §1-1-2。/);
|
|
152
|
+
assert.ok(applied.backup && fs.existsSync(applied.backup), "写文件前应留备份");
|
|
153
|
+
assert.equal(store.pendingPatches().length, 0);
|
|
154
|
+
assert.equal(store.patchBatches(1).length, 1);
|
|
155
|
+
const active = store.list({ layers: ["trunk"], status: "active" });
|
|
156
|
+
assert.deepEqual(active.map((n) => n.uid).sort(), ["trunk:§1-1-1", "trunk:§1-1-2", "trunk:§1-1-3", "trunk:§1-2-1"]);
|
|
157
|
+
const buckets = store.trunkBuckets();
|
|
158
|
+
assert.equal(buckets["trunk:§1-1-1"], "移动组", "编号变化后看板大标题应跟着规则走");
|
|
159
|
+
assert.equal(buckets["trunk:§1-1-3"], undefined);
|
|
160
|
+
} finally {
|
|
161
|
+
try { db.close(); } catch { /* 忽略 */ }
|
|
162
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("看板:渲染编号输入、待确定写入池与检索冲突入口", () => {
|
|
167
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mt-renum-view-"));
|
|
168
|
+
const agents = path.join(dir, "AGENTS.md");
|
|
169
|
+
fs.writeFileSync(agents, FIXTURE, "utf8");
|
|
170
|
+
const rt = new MemoryRuntime({ dataDir: dir, agentsPath: agents, backupDir: path.join(dir, "backups") });
|
|
171
|
+
try {
|
|
172
|
+
const store = rt.open();
|
|
173
|
+
syncTrunk(store, agents, { scope: "user" });
|
|
174
|
+
const tr = store.list({ layers: ["trunk"], status: "active" }).find((n) => n.uid === "trunk:§1-1-3");
|
|
175
|
+
const staged = runBoardAction(store, { op: "stage-patch", id: tr.id, num: "trunk:§1-1-1", text: "丙:这是丙。" }, { agentsPath: agents, backupDir: path.join(dir, "backups") });
|
|
176
|
+
assert.equal(staged.ok, true, staged.error);
|
|
177
|
+
const html = buildReportHtml(rt, store, null);
|
|
178
|
+
assert.match(html, /待确定写入/);
|
|
179
|
+
assert.match(html, /已写入记录/);
|
|
180
|
+
assert.match(html, /检索冲突/);
|
|
181
|
+
assert.match(html, /data-patch-del=/);
|
|
182
|
+
assert.match(html, /id="edit-num"/);
|
|
183
|
+
assert.match(html, /id="scan-modal"/);
|
|
184
|
+
assert.ok(html.includes("/^trunk:§1-\\d+-\\d+$/"), "看板内联正则必须保留 \\d 转义");
|
|
185
|
+
assert.match(html, /§1-1-3 → §1-1-1/);
|
|
186
|
+
} finally {
|
|
187
|
+
try { rt.close(); } catch { /* 忽略 */ }
|
|
188
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const SECTION_FIXTURE = [
|
|
193
|
+
"# AGENTS.md",
|
|
194
|
+
"",
|
|
195
|
+
"## §1 主干(大脑)",
|
|
196
|
+
"",
|
|
197
|
+
"#### §1-1 基本判据",
|
|
198
|
+
"",
|
|
199
|
+
"- **§1-1-1 甲**:这是甲。",
|
|
200
|
+
"",
|
|
201
|
+
"## §2 情感枝",
|
|
202
|
+
"",
|
|
203
|
+
"- **§2-1 相处**:相处正文。",
|
|
204
|
+
"- **§2-2 工作**:工作正文。",
|
|
205
|
+
"",
|
|
206
|
+
"## §3 任务枝",
|
|
207
|
+
"",
|
|
208
|
+
"- **§3-1 任务规则**:任务正文。",
|
|
209
|
+
"",
|
|
210
|
+
"## §4 判断叶",
|
|
211
|
+
"",
|
|
212
|
+
"- **§4-1 判断**:判断正文。",
|
|
213
|
+
"",
|
|
214
|
+
].join("\n");
|
|
215
|
+
|
|
216
|
+
test("§2–§4 解析:三节、行号与全文", () => {
|
|
217
|
+
const sections = parseAgentSections(SECTION_FIXTURE);
|
|
218
|
+
assert.deepEqual(sections.map((s) => s.key), ["§2", "§3", "§4"]);
|
|
219
|
+
assert.deepEqual(sections.map((s) => s.rules.length), [2, 1, 1]);
|
|
220
|
+
const first = sections[0].rules[0];
|
|
221
|
+
assert.equal(first.num, "§2-1");
|
|
222
|
+
assert.equal(first.title, "相处");
|
|
223
|
+
assert.equal(first.text, "相处:相处正文。");
|
|
224
|
+
assert.equal(first.startLine, 11);
|
|
225
|
+
assert.equal(first.endLine, 11);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("§2–§4 编辑/删除:只改目标块,保留其它规则", () => {
|
|
229
|
+
const replaced = replaceAgentSectionRule(SECTION_FIXTURE, "§2-1", "新标题:新正文");
|
|
230
|
+
assert.equal(replaced.ok, true, replaced.error);
|
|
231
|
+
assert.match(replaced.text, /- \*\*§2-1 新标题\*\*:新正文/);
|
|
232
|
+
assert.match(replaced.text, /- \*\*§2-2 工作\*\*:工作正文。/);
|
|
233
|
+
assert.equal((replaced.text.match(/§2-1/g) || []).length, 1);
|
|
234
|
+
|
|
235
|
+
const removed = removeAgentSectionRule(SECTION_FIXTURE, "§2-2");
|
|
236
|
+
assert.equal(removed.ok, true, removed.error);
|
|
237
|
+
assert.doesNotMatch(removed.text, /§2-2/);
|
|
238
|
+
assert.match(removed.text, /- \*\*§2-1 相处\*\*:相处正文。/);
|
|
239
|
+
assert.match(removed.text, /## §2 情感枝/);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test("§1 分组内的说明文字会让重排拒绝,而不是静默丢失", () => {
|
|
243
|
+
const raw = FIXTURE.replace(
|
|
244
|
+
"- **§1-1-2 乙**:这是乙,见 §1-1-1。",
|
|
245
|
+
"- **§1-1-2 乙**:这是乙,见 §1-1-1。\n\n> 这里是主人写的说明,不属于任何规则。",
|
|
246
|
+
);
|
|
247
|
+
const plan = planRenumber(raw, [patch({ baseChecksum: checksum(raw) })]);
|
|
248
|
+
assert.equal(plan.ok, false);
|
|
249
|
+
assert.match(plan.error, /非规则文字/);
|
|
250
|
+
assert.equal(plan.conflicts[0].code, "non-rule-text");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("§2–§4 看板写操作:编辑/删除都备份并落盘", () => {
|
|
254
|
+
const { dir, db, store } = makeStore();
|
|
255
|
+
const agents = path.join(dir, "AGENTS.md");
|
|
256
|
+
fs.writeFileSync(agents, SECTION_FIXTURE, "utf8");
|
|
257
|
+
const options = { agentsPath: agents, backupDir: path.join(dir, "backups") };
|
|
258
|
+
try {
|
|
259
|
+
const wrote = runBoardAction(store, { op: "write-section-rule", num: "§2-1", text: "新标题:新正文", confirm: true }, options);
|
|
260
|
+
assert.equal(wrote.ok, true, wrote.error);
|
|
261
|
+
assert.ok(wrote.backup && fs.existsSync(wrote.backup));
|
|
262
|
+
let current = fs.readFileSync(agents, "utf8");
|
|
263
|
+
assert.match(current, /- \*\*§2-1 新标题\*\*:新正文/);
|
|
264
|
+
assert.match(current, /- \*\*§3-1 任务规则\*\*:任务正文。/);
|
|
265
|
+
|
|
266
|
+
const deleted = runBoardAction(store, { op: "delete-section-rule", num: "§3-1", confirm: true }, options);
|
|
267
|
+
assert.equal(deleted.ok, true, deleted.error);
|
|
268
|
+
current = fs.readFileSync(agents, "utf8");
|
|
269
|
+
assert.doesNotMatch(current, /§3-1/);
|
|
270
|
+
assert.match(current, /- \*\*§2-1 新标题\*\*:新正文/);
|
|
271
|
+
assert.match(current, /- \*\*§4-1 判断\*\*:判断正文。/);
|
|
272
|
+
} finally {
|
|
273
|
+
try { db.close(); } catch { /* 忽略 */ }
|
|
274
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
275
|
+
}
|
|
276
|
+
});
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// tests/trunk-bucket.test.mjs — 主干大标题:按编号自动分组 + 用户指定覆盖。
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import os from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { MemoryRuntime } from "../lib/runtime.mjs";
|
|
8
|
+
import { syncTrunk } from "../lib/ingest.mjs";
|
|
9
|
+
import { buildReportHtml } from "../lib/report.mjs";
|
|
10
|
+
import { runBoardAction } from "../lib/board.mjs";
|
|
11
|
+
|
|
12
|
+
const AGENTS = [
|
|
13
|
+
"# AGENTS.md",
|
|
14
|
+
"",
|
|
15
|
+
"## §1 主干(大脑)",
|
|
16
|
+
"",
|
|
17
|
+
"### §1-1 基本判据",
|
|
18
|
+
"",
|
|
19
|
+
"- **§1-1-1 我是谁**:测试规则甲。",
|
|
20
|
+
"",
|
|
21
|
+
"- **§1-1-2 永久规则**:测试规则乙。",
|
|
22
|
+
"",
|
|
23
|
+
"### §1-6 环境事实",
|
|
24
|
+
"",
|
|
25
|
+
"- **§1-6-1 可信度**:测试规则丙。",
|
|
26
|
+
"",
|
|
27
|
+
"## §2 情感枝",
|
|
28
|
+
"",
|
|
29
|
+
"内容。",
|
|
30
|
+
"",
|
|
31
|
+
].join("\n");
|
|
32
|
+
|
|
33
|
+
function setupWith(agentsText) {
|
|
34
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "mt-bucket-"));
|
|
35
|
+
const agents = path.join(dir, "AGENTS.md");
|
|
36
|
+
fs.writeFileSync(agents, agentsText, "utf8");
|
|
37
|
+
const rt = new MemoryRuntime({ dataDir: dir, agentsPath: agents });
|
|
38
|
+
const store = rt.open();
|
|
39
|
+
syncTrunk(store, agents, { scope: "user" });
|
|
40
|
+
return {
|
|
41
|
+
dir,
|
|
42
|
+
agents,
|
|
43
|
+
rt,
|
|
44
|
+
store,
|
|
45
|
+
options: { agentsPath: agents, backupDir: path.join(dir, "backups") },
|
|
46
|
+
html() {
|
|
47
|
+
return buildReportHtml(rt, store, null);
|
|
48
|
+
},
|
|
49
|
+
cleanup() {
|
|
50
|
+
try {
|
|
51
|
+
rt.close();
|
|
52
|
+
} catch {
|
|
53
|
+
/* 忽略 */
|
|
54
|
+
}
|
|
55
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function setup() {
|
|
61
|
+
return setupWith(AGENTS);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
test("主干大标题:没指定时按 §1 编号自动分组", () => {
|
|
65
|
+
const s = setup();
|
|
66
|
+
try {
|
|
67
|
+
const html = s.html();
|
|
68
|
+
assert.match(html, /<span class="grp">DSH本体规则<\/span><em>2 条<\/em>/);
|
|
69
|
+
assert.match(html, /<span class="grp">本机环境事实<\/span><em>1 条<\/em>/);
|
|
70
|
+
assert.doesNotMatch(html, /<span class="grp">其他规则<\/span>/, "空分组不渲染");
|
|
71
|
+
assert.match(html, /id="bucket-modal"/);
|
|
72
|
+
assert.match(html, /data-act="bucket"/);
|
|
73
|
+
} finally {
|
|
74
|
+
s.cleanup();
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("主干大标题:§1-1-4..7 不被特殊分组,整组按编号连续", () => {
|
|
79
|
+
const rules = Array.from({ length: 8 }, (_, i) => `- **§1-1-${i + 1} 规则${i + 1}**:测试内容。`).join("\n\n");
|
|
80
|
+
const agentsText = [
|
|
81
|
+
"# AGENTS.md",
|
|
82
|
+
"",
|
|
83
|
+
"## §1 主干(大脑)",
|
|
84
|
+
"",
|
|
85
|
+
"### §1-1 基本判据",
|
|
86
|
+
"",
|
|
87
|
+
rules,
|
|
88
|
+
"",
|
|
89
|
+
"### §1-5 环境事实",
|
|
90
|
+
"",
|
|
91
|
+
"- **§1-5-1 机器**:测试内容。",
|
|
92
|
+
"",
|
|
93
|
+
"## §2 情感枝",
|
|
94
|
+
"",
|
|
95
|
+
"内容。",
|
|
96
|
+
"",
|
|
97
|
+
].join("\n");
|
|
98
|
+
const s = setupWith(agentsText);
|
|
99
|
+
try {
|
|
100
|
+
const html = s.html();
|
|
101
|
+
assert.match(html, /<span class="grp">DSH本体规则<\/span><em>8 条<\/em>/);
|
|
102
|
+
assert.match(html, /<span class="grp">本机环境事实<\/span><em>1 条<\/em>/);
|
|
103
|
+
assert.doesNotMatch(html, /<span class="grp">其他规则<\/span>/, "§1-1-4..7 不应落入其他规则");
|
|
104
|
+
const group = html.match(/<span class="grp">DSH本体规则<\/span>[\s\S]*?<\/details>/);
|
|
105
|
+
assert.ok(group, "找到 DSH本体规则 分组");
|
|
106
|
+
const rids = [...group[0].matchAll(/<span class="rid">([^<]+)<\/span>/g)].map((m) => m[1]);
|
|
107
|
+
assert.deepEqual(rids, Array.from({ length: 8 }, (_, i) => `§1-1-${i + 1}`));
|
|
108
|
+
} finally {
|
|
109
|
+
s.cleanup();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("主干大标题:指定后看板按新大标题分组", () => {
|
|
114
|
+
const s = setup();
|
|
115
|
+
try {
|
|
116
|
+
const done = runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-1-1", bucket: "其他规则" }, s.options);
|
|
117
|
+
assert.equal(done.ok, true, done.error);
|
|
118
|
+
assert.equal(s.store.trunkBuckets()["trunk:§1-1-1"], "其他规则");
|
|
119
|
+
const html = s.html();
|
|
120
|
+
assert.match(html, /<span class="grp">其他规则<\/span><em>1 条<\/em>/);
|
|
121
|
+
assert.match(html, /<span class="grp">DSH本体规则<\/span><em>1 条<\/em>/);
|
|
122
|
+
} finally {
|
|
123
|
+
s.cleanup();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("主干大标题:可新建大标题,写进 BOARD 供下拉选择", () => {
|
|
128
|
+
const s = setup();
|
|
129
|
+
try {
|
|
130
|
+
const done = runBoardAction(
|
|
131
|
+
s.store,
|
|
132
|
+
{ op: "set-trunk-bucket", uid: "trunk:§1-1-2", bucket: "网络与外部输入" },
|
|
133
|
+
s.options,
|
|
134
|
+
);
|
|
135
|
+
assert.equal(done.ok, true, done.error);
|
|
136
|
+
const html = s.html();
|
|
137
|
+
assert.match(html, /<span class="grp">网络与外部输入<\/span><em>1 条<\/em>/);
|
|
138
|
+
assert.match(html, /"buckets":\[[^\]]*"网络与外部输入"/);
|
|
139
|
+
} finally {
|
|
140
|
+
s.cleanup();
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("主干大标题:__default 恢复自动分组", () => {
|
|
145
|
+
const s = setup();
|
|
146
|
+
try {
|
|
147
|
+
runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-1-1", bucket: "其他规则" }, s.options);
|
|
148
|
+
const reset = runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-1-1", bucket: "__default" }, s.options);
|
|
149
|
+
assert.equal(reset.ok, true, reset.error);
|
|
150
|
+
assert.equal(reset.mode, "trunk-bucket-reset");
|
|
151
|
+
assert.equal(s.store.trunkBuckets()["trunk:§1-1-1"], undefined);
|
|
152
|
+
assert.match(s.html(), /<span class="grp">DSH本体规则<\/span><em>2 条<\/em>/);
|
|
153
|
+
} finally {
|
|
154
|
+
s.cleanup();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("主干大标题:只接受带编号的主干规则与合法名字", () => {
|
|
159
|
+
const s = setup();
|
|
160
|
+
try {
|
|
161
|
+
assert.equal(runBoardAction(s.store, { op: "set-trunk-bucket", uid: "leaf:12", bucket: "其他规则" }, s.options).ok, false);
|
|
162
|
+
assert.equal(runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§2-1", bucket: "其他规则" }, s.options).ok, false);
|
|
163
|
+
assert.equal(runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-1-1", bucket: " " }, s.options).ok, false);
|
|
164
|
+
assert.equal(runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-1-1", bucket: "x".repeat(25) }, s.options).ok, false);
|
|
165
|
+
const missing = runBoardAction(s.store, { op: "set-trunk-bucket", uid: "trunk:§1-9-9", bucket: "其他规则" }, s.options);
|
|
166
|
+
assert.equal(missing.ok, false);
|
|
167
|
+
assert.match(missing.error, /找不到/);
|
|
168
|
+
} finally {
|
|
169
|
+
s.cleanup();
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("主干大标题:归枝写入主干时一起记下大标题", () => {
|
|
174
|
+
const s = setup();
|
|
175
|
+
try {
|
|
176
|
+
const leaf = s.store.add({
|
|
177
|
+
layer: "leaf",
|
|
178
|
+
scope: "user",
|
|
179
|
+
subject: "self",
|
|
180
|
+
title: "新约定",
|
|
181
|
+
text: "每次答复先给结论,再给理由。",
|
|
182
|
+
importance: 0.85,
|
|
183
|
+
}).node;
|
|
184
|
+
const done = runBoardAction(
|
|
185
|
+
s.store,
|
|
186
|
+
{
|
|
187
|
+
op: "write-trunk-new",
|
|
188
|
+
id: leaf.id,
|
|
189
|
+
text: leaf.text,
|
|
190
|
+
confirm: true,
|
|
191
|
+
checks: [true, true, true, true, true],
|
|
192
|
+
group: "§1-1",
|
|
193
|
+
bucket: "其他规则",
|
|
194
|
+
},
|
|
195
|
+
s.options,
|
|
196
|
+
);
|
|
197
|
+
assert.equal(done.ok, true, done.error || JSON.stringify(done));
|
|
198
|
+
assert.equal(done.bucket, "其他规则");
|
|
199
|
+
assert.equal(done.node.uid, "trunk:§1-1-3");
|
|
200
|
+
assert.equal(s.store.trunkBuckets()["trunk:§1-1-3"], "其他规则");
|
|
201
|
+
assert.match(fs.readFileSync(s.agents, "utf8"), /每次答复先给结论/);
|
|
202
|
+
const html = s.html();
|
|
203
|
+
assert.match(html, /<span class="grp">其他规则<\/span><em>1 条<\/em>/);
|
|
204
|
+
assert.match(html, /<span class="grp">DSH本体规则<\/span><em>2 条<\/em>/);
|
|
205
|
+
} finally {
|
|
206
|
+
s.cleanup();
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("主干大标题:看板内联脚本可解析", () => {
|
|
211
|
+
const s = setup();
|
|
212
|
+
try {
|
|
213
|
+
const html = s.html();
|
|
214
|
+
const scripts = Array.from(html.matchAll(/<script(?![^>]*type=)[^>]*>([\s\S]*?)<\/script>/g)).map((m) => m[1]);
|
|
215
|
+
assert.ok(scripts.length >= 1, "应该至少有一段内联脚本");
|
|
216
|
+
for (const code of scripts) {
|
|
217
|
+
assert.doesNotThrow(() => new Function(code), "内联脚本有语法错误");
|
|
218
|
+
}
|
|
219
|
+
assert.match(html, /id="trunk-bucket-line"/);
|
|
220
|
+
assert.match(html, /fillBucketSelect/);
|
|
221
|
+
assert.match(html, /openBucketForBranch/);
|
|
222
|
+
} finally {
|
|
223
|
+
s.cleanup();
|
|
224
|
+
}
|
|
225
|
+
});
|