dsh-project-flow 0.9.0 → 0.9.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/README.md +58 -11
  3. package/cordis.patch.yml +30 -11
  4. package/docs/DETAIL.md +6 -6
  5. package/docs/FEATURES.md +12 -5
  6. package/docs/LAUNCH.md +3 -3
  7. package/docs/MOMENTS.md +4 -4
  8. package/docs/USAGE.md +2 -2
  9. package/domains.mjs +1 -1
  10. package/evidence.mjs +19 -3
  11. package/flow.schema.mjs +192 -35
  12. package/gate.mjs +49 -23
  13. package/index.js +639 -120
  14. package/locale/en.json +15 -4
  15. package/locale/zh.json +17 -6
  16. package/package.json +5 -3
  17. package/tests/_heartbeat-regression.test.mjs +124 -0
  18. package/tests/aesthetic.test.mjs +534 -0
  19. package/tests/assertions-e2e.mjs +234 -0
  20. package/tests/bench-check.mjs +32 -0
  21. package/tests/client-bundle.test.mjs +707 -0
  22. package/tests/contract.test.mjs +137 -0
  23. package/tests/desktop.test.mjs +130 -0
  24. package/tests/domains.test.mjs +220 -0
  25. package/tests/e2e.mjs +300 -0
  26. package/tests/evidence.test.mjs +408 -0
  27. package/tests/extra-phase.test.mjs +133 -0
  28. package/tests/feedback.test.mjs +187 -0
  29. package/tests/flow-create.test.mjs +168 -0
  30. package/tests/flow-gate.test.mjs +232 -0
  31. package/tests/flow-schema.test.mjs +213 -0
  32. package/tests/gate.test.mjs +246 -0
  33. package/tests/issue-loop.test.mjs +254 -0
  34. package/tests/issues-e2e.mjs +246 -0
  35. package/tests/panel-independence.test.mjs +78 -0
  36. package/tests/panel-report.test.mjs +129 -0
  37. package/tests/patch-ledger.test.mjs +280 -0
  38. package/tests/paths.mjs +100 -0
  39. package/tests/preview-path.test.mjs +69 -0
  40. package/tests/qa-assertions.test.mjs +352 -0
  41. package/tests/run-all.mjs +90 -0
  42. package/tests/stage-model.test.mjs +120 -0
  43. package/tests/stop-resume.test.mjs +260 -0
  44. package/tests/watch-root.test.mjs +167 -0
  45. package/tests/watchdog-tokens.test.mjs +53 -0
  46. package/tests/watchdog.test.mjs +305 -0
@@ -0,0 +1,352 @@
1
+ /**
2
+ * 断言执行器(T3)的单测。
3
+ *
4
+ * 这一套盯的是四件事,别的一概不管:
5
+ * 1. Requirement.md 的前置块能读能写,指纹只认判据本身
6
+ * 2. 白名单是拦得住的那层,不是建议
7
+ * 3. 真起进程,退出码、超时、输出尾巴都得对
8
+ * 4. 判据结论压得过模型的话:没过就是 FAIL,没有 check 就是 unverified
9
+ */
10
+ import { test } from 'node:test'
11
+ import assert from 'node:assert/strict'
12
+ import { join } from 'node:path'
13
+ import { pathToFileURL } from 'node:url'
14
+ import { mkdtemp, rm } from 'node:fs/promises'
15
+ import { tmpdir } from 'node:os'
16
+ import { LIVE_DIR } from './paths.mjs'
17
+
18
+ const mod = await import(pathToFileURL(join(LIVE_DIR, 'index.js')).href + `?t=${Date.now()}`)
19
+ const {
20
+ parseRequirementChecks,
21
+ renderRequirementChecks,
22
+ requirementFingerprint,
23
+ allowedCommandsOf,
24
+ tokenizeCommand,
25
+ checkDenial,
26
+ runRequirementChecks,
27
+ gateAssertions,
28
+ buildCheckEntries,
29
+ stripInlineCheck,
30
+ renderAssertionsBlock,
31
+ criteriaInPlay,
32
+ } = mod
33
+
34
+ const NODE = JSON.stringify(process.execPath)
35
+ const CHECKED_AT = new Date().toISOString()
36
+
37
+ async function withTemp(fn) {
38
+ const dir = await mkdtemp(join(tmpdir(), 'pf-qa-'))
39
+ try {
40
+ return await fn(dir)
41
+ } finally {
42
+ await rm(dir, { recursive: true, force: true })
43
+ }
44
+ }
45
+
46
+ const requirementWith = entries => `${renderRequirementChecks(entries)}\n\n# 需求\n\n把这件事做对\n\n## 用户要求\n\n1. 做对\n`
47
+
48
+ // ── 前置块 ────────────────────────────────────────────────────
49
+
50
+ test('前置块:check 字段能读出来,散文不会被当成命令', () => {
51
+ const text = requirementWith([
52
+ { req: 1, cmd: `${NODE} -e "process.exit(1)"`, expectExit: 0 },
53
+ { req: 2, cmd: '', expectExit: 0 },
54
+ ]) + '\n## 用户要求\n\n1. check: 这句在正文里,不该被吃掉\n'
55
+ const entries = parseRequirementChecks(text)
56
+ assert.equal(entries.length, 2)
57
+ assert.equal(entries[0].req, 1)
58
+ assert.match(entries[0].cmd, /process\.exit\(1\)/u)
59
+ assert.equal(entries[1].cmd, '')
60
+ })
61
+
62
+ test('前置块:写进去再读回来是同一份(引号、路径、反斜杠都不丢)', () => {
63
+ const source = [
64
+ { req: 1, cmd: 'node -e "console.log(1 + 2)"', expectExit: 0 },
65
+ { req: 2, cmd: 'npm run test:ci -- --reporter=dot', expectExit: 0 },
66
+ { req: 3, cmd: 'git diff --quiet HEAD -- "C:/a b/c.js"', expectExit: 1 },
67
+ ]
68
+ const back = parseRequirementChecks(renderRequirementChecks(source))
69
+ assert.deepEqual(back, source)
70
+ })
71
+
72
+ test('前置块:没有前置块就是空表,不是抛错', () => {
73
+ assert.deepEqual(parseRequirementChecks('# 需求\n\n没有前置块\n'), [])
74
+ assert.deepEqual(parseRequirementChecks(''), [])
75
+ assert.deepEqual(parseRequirementChecks(undefined), [])
76
+ })
77
+
78
+ // ── 指纹 ──────────────────────────────────────────────────────
79
+
80
+ test('指纹:同样的判据算同一个值,换个命令就变', () => {
81
+ const a = [{ req: 1, cmd: 'node -e "process.exit(0)"', expectExit: 0 }]
82
+ const b = [{ req: 1, cmd: 'node -e "process.exit(0)"', expectExit: 0 }]
83
+ const c = [{ req: 1, cmd: 'node -e "process.exit(1)"', expectExit: 0 }]
84
+ assert.equal(requirementFingerprint(a), requirementFingerprint(b))
85
+ assert.notEqual(requirementFingerprint(a), requirementFingerprint(c))
86
+ assert.equal(requirementFingerprint(a).length, 64)
87
+ })
88
+
89
+ test('指纹:只改要求正文不影响指纹,改判据才影响', () => {
90
+ const entries = [{ req: 1, cmd: 'node -e "1"', expectExit: 0 }]
91
+ const before = requirementFingerprint(parseRequirementChecks(requirementWith(entries)))
92
+ const proseEdited = requirementFingerprint(parseRequirementChecks(
93
+ requirementWith(entries).replace('把这件事做对', '把这件事做好一点'),
94
+ ))
95
+ const checkEdited = requirementFingerprint(parseRequirementChecks(
96
+ requirementWith([{ req: 1, cmd: 'node -e "0"', expectExit: 0 }]),
97
+ ))
98
+ assert.equal(before, proseEdited)
99
+ assert.notEqual(before, checkEdited)
100
+ })
101
+
102
+ // ── 白名单 ────────────────────────────────────────────────────
103
+
104
+ test('白名单:默认那 9 类放行,配置只能往上加', () => {
105
+ const allowed = allowedCommandsOf({})
106
+ for (const name of ['node', 'npm', 'pnpm', 'python', 'pytest', 'git', 'cargo', 'go', 'dotnet']) {
107
+ assert.ok(allowed.includes(name), `${name} 应该在默认白名单里`)
108
+ }
109
+ const more = allowedCommandsOf({ checkAllowedCommands: 'tsc, eslint' })
110
+ assert.ok(more.includes('tsc'))
111
+ assert.ok(more.includes('node'), '加了新命令不该把默认的挤掉')
112
+ })
113
+
114
+ test('白名单:rm / curl / ssh / powershell -enc 一律拒掉', () => {
115
+ const allowed = allowedCommandsOf({})
116
+ assert.equal(checkDenial('rm -rf /', allowed).denied, true)
117
+ assert.equal(checkDenial('curl http://x/y', allowed).denied, true)
118
+ assert.equal(checkDenial('ssh user@host', allowed).denied, true)
119
+ assert.equal(checkDenial('powershell -enc SQBFAFgA', allowed).denied, true)
120
+ assert.equal(checkDenial('C:\\Windows\\System32\\curl.exe -s http://x', allowed).denied, true)
121
+ assert.equal(checkDenial('', allowed).denied, true)
122
+ })
123
+
124
+ test('白名单:放行的命令连参数一起交出来,命令名去路径去扩展名', () => {
125
+ const allowed = allowedCommandsOf({})
126
+ const ok = checkDenial('node -e "process.exit(0)"', allowed)
127
+ assert.equal(ok.denied, false)
128
+ assert.equal(ok.name, 'node')
129
+ assert.deepEqual(ok.args, ['-e', 'process.exit(0)'])
130
+ const win = checkDenial('C:\\tools\\node.exe -v', allowed)
131
+ assert.equal(win.denied, false)
132
+ assert.equal(win.name, 'node')
133
+ })
134
+
135
+ test('切命令:引号里的空格不拆,引号本身不留在参数里', () => {
136
+ assert.deepEqual(tokenizeCommand('node -e "console.log(1)"'), ['node', '-e', 'console.log(1)'])
137
+ assert.deepEqual(tokenizeCommand("git log --grep='a b'"), ['git', 'log', '--grep=a b'])
138
+ assert.deepEqual(tokenizeCommand(' npm run build '), ['npm', 'run', 'build'])
139
+ })
140
+
141
+ // ── 真起进程 ──────────────────────────────────────────────────
142
+
143
+ test('真跑:退出码非 0 记 fail,输出尾巴带得回来', async () => {
144
+ await withTemp(async dir => {
145
+ const assertions = await runRequirementChecks({
146
+ entries: [{ req: 1, cmd: `${NODE} -e "console.log('BOOM'); process.exit(3)"`, expectExit: 0 }],
147
+ cwd: dir,
148
+ allowed: allowedCommandsOf({}),
149
+ })
150
+ assert.equal(assertions.length, 1)
151
+ assert.equal(assertions[0].status, 'fail')
152
+ assert.equal(assertions[0].exit, 3)
153
+ assert.match(assertions[0].stdoutTail, /BOOM/u)
154
+ assert.ok(assertions[0].durationMs >= 0)
155
+ })
156
+ })
157
+
158
+ test('真跑:退出码等于 check_exit 才算过', async () => {
159
+ await withTemp(async dir => {
160
+ const assertions = await runRequirementChecks({
161
+ entries: [{ req: 1, cmd: `${NODE} -e "process.exit(2)"`, expectExit: 2 }],
162
+ cwd: dir,
163
+ allowed: allowedCommandsOf({}),
164
+ })
165
+ assert.equal(assertions[0].exit, 2)
166
+ assert.equal(assertions[0].status, 'pass')
167
+ })
168
+ })
169
+
170
+ test('真跑:没有 check 的要求是 unverified,那条命令根本不会执行', async () => {
171
+ await withTemp(async dir => {
172
+ const assertions = await runRequirementChecks({
173
+ entries: [{ req: 1, cmd: '', expectExit: 0 }, { req: 2, cmd: ' ', expectExit: 0 }],
174
+ cwd: dir,
175
+ allowed: allowedCommandsOf({}),
176
+ })
177
+ assert.deepEqual(assertions.map(item => item.status), ['unverified', 'unverified'])
178
+ assert.deepEqual(assertions.map(item => item.cmd), [null, null])
179
+ })
180
+ })
181
+
182
+ test('真跑:超时被收掉,记 timeout: true 并算 FAIL', async () => {
183
+ await withTemp(async dir => {
184
+ const assertions = await runRequirementChecks({
185
+ entries: [{ req: 1, cmd: `${NODE} -e "setTimeout(() => {}, 5000)"`, expectExit: 0 }],
186
+ cwd: dir,
187
+ allowed: allowedCommandsOf({}),
188
+ timeoutMs: 400,
189
+ })
190
+ assert.equal(assertions[0].timeout, true)
191
+ assert.equal(assertions[0].status, 'fail')
192
+ })
193
+ })
194
+
195
+ test('真跑:被白名单拒的命令不会执行,但要在结果里说清楚是拒了', async () => {
196
+ await withTemp(async dir => {
197
+ const assertions = await runRequirementChecks({
198
+ entries: [{ req: 1, cmd: 'rm -rf /', expectExit: 0 }],
199
+ cwd: dir,
200
+ allowed: allowedCommandsOf({}),
201
+ })
202
+ assert.equal(assertions[0].status, 'denied')
203
+ assert.equal(assertions[0].denied, true)
204
+ assert.match(assertions[0].stdoutTail, /白名单拒绝/u)
205
+ })
206
+ })
207
+
208
+ test('真跑:输出太长只留尾部 8000 字符', async () => {
209
+ await withTemp(async dir => {
210
+ const assertions = await runRequirementChecks({
211
+ entries: [{
212
+ req: 1,
213
+ cmd: `${NODE} -e "process.stdout.write('A'.repeat(12000) + 'TAIL')"`,
214
+ expectExit: 0,
215
+ }],
216
+ cwd: dir,
217
+ allowed: allowedCommandsOf({}),
218
+ tailChars: 8000,
219
+ })
220
+ assert.equal(assertions[0].stdoutTail.length, 8000)
221
+ assert.ok(assertions[0].stdoutTail.endsWith('TAIL'))
222
+ })
223
+ })
224
+
225
+ // ── 判据结论 ──────────────────────────────────────────────────
226
+
227
+ test('判据:有命令没过就是 FAIL,哪怕模型说 PASS', () => {
228
+ const gate = gateAssertions([
229
+ { req: 1, cmd: 'node x', exit: 0, durationMs: 1, stdoutTail: '', status: 'pass' },
230
+ { req: 2, cmd: 'node y', exit: 1, durationMs: 1, stdoutTail: '', status: 'fail' },
231
+ ], 2)
232
+ assert.equal(gate.verdict, 'FAIL')
233
+ assert.equal(gate.failed.length, 1)
234
+ })
235
+
236
+ test('判据:有要求没定义 check 就是 UNVERIFIED,不能升级成 PASS', () => {
237
+ const gate = gateAssertions([
238
+ { req: 1, cmd: 'node x', exit: 0, durationMs: 1, stdoutTail: '', status: 'pass' },
239
+ { req: 2, cmd: null, exit: null, durationMs: 0, stdoutTail: '', status: 'unverified' },
240
+ ], 2)
241
+ assert.equal(gate.verdict, 'UNVERIFIED')
242
+ assert.equal(gate.unverified.length, 1)
243
+ })
244
+
245
+ test('判据:全过才是 PASS;要求条数比断言多出来的也算判不了', () => {
246
+ const ok = gateAssertions([
247
+ { req: 1, cmd: 'node x', exit: 0, durationMs: 1, stdoutTail: '', status: 'pass' },
248
+ ], 1)
249
+ assert.equal(ok.verdict, 'PASS')
250
+ const short = gateAssertions([
251
+ { req: 1, cmd: 'node x', exit: 0, durationMs: 1, stdoutTail: '', status: 'pass' },
252
+ ], 2)
253
+ assert.equal(short.verdict, 'UNVERIFIED')
254
+ assert.deepEqual(short.uncovered, [2])
255
+ })
256
+
257
+ test('判据:一条 check 都没有时整组是 UNVERIFIED,不是 PASS', () => {
258
+ const gate = gateAssertions([
259
+ { req: 1, cmd: null, exit: null, durationMs: 0, stdoutTail: '', status: 'unverified' },
260
+ ], 1)
261
+ assert.equal(gate.verdict, 'UNVERIFIED')
262
+ assert.equal(gate.passed.length, 0)
263
+ })
264
+
265
+ test('判据有没有启用:全 unverified 不算启用(老项目照旧走),混着写才算', () => {
266
+ assert.equal(criteriaInPlay([
267
+ { req: 1, cmd: null, exit: null, durationMs: 0, stdoutTail: '', status: 'unverified' },
268
+ ]), false)
269
+ assert.equal(criteriaInPlay([
270
+ { req: 1, cmd: 'node x', exit: 0, durationMs: 1, stdoutTail: '', status: 'pass' },
271
+ { req: 2, cmd: null, exit: null, durationMs: 0, stdoutTail: '', status: 'unverified' },
272
+ ]), true)
273
+ assert.equal(criteriaInPlay([]), false)
274
+ })
275
+
276
+ // ── 从 requirements 生成断言表 ─────────────────────────────────
277
+
278
+ test('要求里自带 check 行:抽得出来,正文里不留', () => {
279
+ const entries = buildCheckEntries([
280
+ '改完要能跑\ncheck: node -e "process.exit(0)"',
281
+ '再补一个 || check: npm test',
282
+ '这条没判据',
283
+ ], undefined)
284
+ assert.equal(entries.length, 3)
285
+ assert.equal(entries[0].cmd, 'node -e "process.exit(0)"')
286
+ assert.equal(entries[1].cmd, 'npm test')
287
+ assert.equal(entries[2].cmd, '')
288
+ assert.equal(stripInlineCheck('改完要能跑\ncheck: node -e "1"'), '改完要能跑')
289
+ assert.equal(stripInlineCheck('再补一个 || check: npm test'), '再补一个')
290
+ })
291
+
292
+ test('checks 参数优先,能覆盖 check_exit', () => {
293
+ const entries = buildCheckEntries(
294
+ ['跑测试', '跑 lint'],
295
+ [
296
+ { req: 1, check: 'pytest -q', checkExit: 0 },
297
+ { req: 2, check: 'node lint.js', checkExit: 2 },
298
+ ],
299
+ )
300
+ assert.equal(entries[0].cmd, 'pytest -q')
301
+ assert.equal(entries[1].cmd, 'node lint.js')
302
+ assert.equal(entries[1].expectExit, 2)
303
+ })
304
+
305
+ // ── 写进 QA_Report.md 的前置块 ────────────────────────────────
306
+
307
+ test('报告前置块:requirement 要求的那五个字段一个不少', () => {
308
+ const block = renderAssertionsBlock([
309
+ {
310
+ req: 1,
311
+ cmd: 'node -e "process.exit(1)"',
312
+ exit: 1,
313
+ durationMs: 42,
314
+ stdoutTail: 'line one\nline two',
315
+ status: 'fail',
316
+ },
317
+ ], { verdict: 'FAIL', round: 2 })
318
+ assert.match(block, /^---\n/u)
319
+ assert.match(block, /verdict: "FAIL"/u)
320
+ assert.match(block, /round: 2/u)
321
+ assert.match(block, /- req: 1/u)
322
+ assert.match(block, /cmd: "node -e /u)
323
+ assert.match(block, /exit: 1/u)
324
+ assert.match(block, /durationMs: 42/u)
325
+ assert.match(block, /status: fail/u)
326
+ assert.match(block, /stdoutTail: \|/u)
327
+ assert.match(block, /line one/u)
328
+ })
329
+
330
+ test('报告前置块:没有断言时写成空数组,不是空着一片', () => {
331
+ const block = renderAssertionsBlock([], { verdict: 'PASS', round: 0 })
332
+ assert.match(block, /assertions: \[\]/u)
333
+ })
334
+
335
+ test('报告的正文自述里要带上退出码,别只说「没过」', async () => {
336
+ const fn = mod.describeAssertions
337
+ assert.equal(typeof fn, 'function')
338
+ const text = fn(gateAssertions([
339
+ { req: 1, cmd: 'node y', exit: 1, durationMs: 7, stdoutTail: 'FAIL detail', status: 'fail' },
340
+ { req: 2, cmd: null, exit: null, durationMs: 0, stdoutTail: '', status: 'unverified' },
341
+ ], 2))
342
+ assert.match(text, /退出码 1/u)
343
+ assert.match(text, /FAIL detail/u)
344
+ assert.match(text, /unverified/u)
345
+ })
346
+
347
+ // ── 一点自证 ──────────────────────────────────────────────────
348
+
349
+ test('这套测试自己跑在真 node 上(不是靠 mock 过的关)', () => {
350
+ assert.ok(process.execPath.length > 0)
351
+ assert.ok(CHECKED_AT.startsWith('20'))
352
+ })
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 一条命令跑完全部套件。
3
+ *
4
+ * 这个文件随包发布:下载解压后 `node tests/run-all.mjs` 就能验这个包本身,
5
+ * 不用先装到别处。以前它在开发工作区里、不进包,于是 README 写「33 套全过」
6
+ * 而用户拿到的压缩包里根本没有能跑的东西——那句数字对用户来说无从复核。
7
+ *
8
+ * 顺序按「越快、越可能出错先跑」:单测 → 接线 → 部署一致性 → 端到端。
9
+ * 任何一套非零退出就整体失败,不跳过后面(后面几套的信息也有用)。
10
+ */
11
+ import { spawn } from 'node:child_process'
12
+ import { dirname, join } from 'node:path'
13
+ import { fileURLToPath } from 'node:url'
14
+ import { checkDependencies, LIVE_DIR } from './paths.mjs'
15
+
16
+ const here = dirname(fileURLToPath(import.meta.url))
17
+
18
+ // 依赖没装好时先给一句能照着做的话,而不是让九套测试各报一次 ERRODE。
19
+ if (!checkDependencies()) process.exit(1)
20
+
21
+ const SUITES = [
22
+ ['证据模块单测', 'evidence.test.mjs'],
23
+ ['门控单测', 'gate.test.mjs'],
24
+ ['守护器单测', 'watchdog.test.mjs'],
25
+ ['守护器心跳回归', '_heartbeat-regression.test.mjs'],
26
+ ['token 计数单测', 'watchdog-tokens.test.mjs'],
27
+ ['停止与继续', 'stop-resume.test.mjs'],
28
+ ['工作流目录门控', 'flow-gate.test.mjs'],
29
+ ['状态栏看哪个目录', 'watch-root.test.mjs'],
30
+ ['质检读预览图的路径', 'preview-path.test.mjs'],
31
+ ['diff / 停机说明 / 盲区记忆', 'feedback.test.mjs'],
32
+ ['状态栏 bundle 与进度条', 'client-bundle.test.mjs'],
33
+ ['审美约束', 'aesthetic.test.mjs'],
34
+ ['领域配置', 'domains.test.mjs'],
35
+ ['桌面工具接线', 'desktop.test.mjs'],
36
+ ['端到端流水', 'e2e.mjs'],
37
+ ['断言执行器', 'qa-assertions.test.mjs'],
38
+ ['断言端到端', 'assertions-e2e.mjs'],
39
+ ['问题闭环', 'issue-loop.test.mjs'],
40
+ ['问题闭环端到端', 'issues-e2e.mjs'],
41
+ ['补丁台账', 'patch-ledger.test.mjs'],
42
+ ['声明式流程', 'flow-schema.test.mjs'],
43
+ ['中间阶段', 'extra-phase.test.mjs'],
44
+ ['阶段级模型', 'stage-model.test.mjs'],
45
+ ['面板数据', 'panel-report.test.mjs'],
46
+ ['状态栏独立性', 'panel-independence.test.mjs'],
47
+ ['完成契约', 'contract.test.mjs'],
48
+ ['回归语料', 'bench-check.mjs'],
49
+ ['工作流目录自动创建', 'flow-create.test.mjs'],
50
+ ]
51
+
52
+ function run(file) {
53
+ return new Promise(resolve => {
54
+ const child = spawn(process.execPath, [join(here, file)], {
55
+ cwd: here,
56
+ stdio: ['ignore', 'pipe', 'pipe'],
57
+ })
58
+ let text = ''
59
+ child.stdout.on('data', chunk => { text += chunk })
60
+ child.stderr.on('data', chunk => { text += chunk })
61
+ child.on('close', code => resolve({ code, text }))
62
+ })
63
+ }
64
+
65
+ const bad = []
66
+ for (const [label, file] of SUITES) {
67
+ const { code, text } = await run(file)
68
+ const pass = /^✅ pass (\d+)/mu.exec(text)
69
+ const fail = /^❌ fail (\d+)/mu.exec(text)
70
+ const total = /(\d+)\/(\d+) 通过/u.exec(text)
71
+ const tally = pass !== null
72
+ ? `${pass[1]} 通过${fail === null ? '' : ` / ${fail[1]} 失败`}`
73
+ : total !== null
74
+ ? `${total[1]}/${total[2]}`
75
+ : code === 0 ? 'ok' : '失败'
76
+ console.log(`${code === 0 ? '✔' : '✖'} ${label.padEnd(16)} ${file.padEnd(24)} ${tally}`)
77
+ if (code !== 0) bad.push({ label, file, text })
78
+ }
79
+
80
+ if (bad.length > 0) {
81
+ console.log('')
82
+ for (const item of bad) {
83
+ console.log(`── ${item.label}(${item.file})失败片段 ──`)
84
+ console.log(item.text.split(/\r?\n/u).filter(line => /✖|Error|错误|失败|→/u.test(line)).slice(0, 12).join('\n'))
85
+ console.log('')
86
+ }
87
+ }
88
+
89
+ console.log(bad.length === 0 ? `\n全部 ${SUITES.length} 套通过` : `\n${bad.length} 套失败`)
90
+ process.exit(bad.length === 0 ? 0 : 1)
@@ -0,0 +1,120 @@
1
+ /**
2
+ * 阶段级模型路由(T6)的测试。
3
+ *
4
+ * 要守两件事:
5
+ * 1. 优先级顺序:flow.yaml 的 phase.model > project-flow.json > 继承主对话(空串)
6
+ * 2. 回退日志要能看出是哪一级顶住了——三级都试过之后只记第一条等于没说
7
+ *
8
+ * 上半部分真调 stageModelOf;下半部分是静态断言,盯回退日志的字段和校验入口。
9
+ */
10
+ import { test } from 'node:test'
11
+ import assert from 'node:assert/strict'
12
+ import { join } from 'node:path'
13
+ import { readFile } from 'node:fs/promises'
14
+ import { pathToFileURL } from 'node:url'
15
+ import { LIVE_DIR } from './paths.mjs'
16
+
17
+ const mod = await import(pathToFileURL(join(LIVE_DIR, 'index.js')).href + `?t=${Date.now()}`)
18
+ const { stageModelOf } = mod
19
+
20
+ const source = await readFile(join(LIVE_DIR, 'index.js'), 'utf8')
21
+
22
+ const settings = { provider: 'workbuddy', planProvider: '', planModel: '', buildProvider: '', buildModel: '', checkProvider: '', checkModel: '' }
23
+
24
+ const flowOf = phases => ({ process: { name: 't' }, phases })
25
+
26
+ test('flow.yaml 里写了 model,就用它——压过配置', () => {
27
+ const flow = flowOf([
28
+ { id: 'req', reads: [], writes: [] },
29
+ { id: 'plan', reads: [], writes: [], model: { provider: 'wb', model: 'kimi-k3.1-pro' } },
30
+ { id: 'build', reads: [], writes: [], model: { provider: 'wb', model: 'hy4flash' } },
31
+ { id: 'qa', reads: [], writes: [] },
32
+ ])
33
+ const plan = stageModelOf(settings, flow, 2, { llmProvider: 'workbuddy', model: '配置里的模型' })
34
+ assert.equal(plan.model, 'kimi-k3.1-pro')
35
+ assert.equal(plan.llmProvider, 'wb')
36
+ assert.equal(plan.source, 'flow.yaml')
37
+
38
+ const build = stageModelOf(settings, flow, 3, { llmProvider: 'workbuddy', model: '配置里的模型' })
39
+ assert.equal(build.model, 'hy4flash')
40
+ })
41
+
42
+ test('flow.yaml 那个阶段没写 model,退回配置里的', () => {
43
+ const flow = flowOf([
44
+ { id: 'req' }, { id: 'plan' }, { id: 'build' }, { id: 'qa' },
45
+ ])
46
+ const plan = stageModelOf(settings, flow, 2, { llmProvider: 'workbuddy', model: 'hy4flash' })
47
+ assert.equal(plan.model, 'hy4flash')
48
+ assert.equal(plan.llmProvider, 'workbuddy')
49
+ assert.equal(plan.source, 'config')
50
+ })
51
+
52
+ test('两边都空就是继承主对话——空串不是「没设」', () => {
53
+ const flow = flowOf([{ id: 'req' }, { id: 'plan' }, { id: 'build' }, { id: 'qa' }])
54
+ const plan = stageModelOf(settings, flow, 2, { llmProvider: '', model: '' })
55
+ assert.equal(plan.model, '')
56
+ assert.equal(plan.llmProvider, '')
57
+ assert.equal(plan.source, 'inherit')
58
+ })
59
+
60
+ test('flow 缺失时不炸,等于没配', () => {
61
+ const plan = stageModelOf(settings, undefined, 2, { llmProvider: 'workbuddy', model: 'hy4flash' })
62
+ assert.equal(plan.model, 'hy4flash')
63
+ const also = stageModelOf(settings, { phases: [] }, 2, { llmProvider: '', model: '' })
64
+ assert.equal(also.model, '')
65
+ })
66
+
67
+ test('阶段序号越界也不炸', () => {
68
+ const flow = flowOf([{ id: 'req' }])
69
+ const out = stageModelOf(settings, flow, 4, { llmProvider: 'wb', model: 'x' })
70
+ assert.equal(out.model, 'x')
71
+ })
72
+
73
+ test('只写 provider 不写 model,只换 provider', () => {
74
+ const flow = flowOf([
75
+ { id: 'req' }, { id: 'plan' }, { id: 'build' }, { id: 'qa', model: { provider: 'other' } },
76
+ ])
77
+ const qa = stageModelOf(settings, flow, 4, { llmProvider: 'workbuddy', model: 'deepseek-v4-pro' })
78
+ assert.equal(qa.llmProvider, 'other')
79
+ assert.equal(qa.model, 'deepseek-v4-pro', '没写 model 就该沿用配置里的那个')
80
+ })
81
+
82
+ test('三个阶段都真的走了 stageModelOf,没有哪个漏掉', () => {
83
+ assert.ok(source.includes('const planRoute = stageModelOf('), '规划段要接')
84
+ assert.ok(source.includes('const buildRoute = stageModelOf('), '实施段要接')
85
+ assert.ok(source.includes('const checkRoute = stageModelOf('), '质检段要接')
86
+ // 接上之后就不能再直接拿 settings 里的模型字段喂 stageOptions 了。
87
+ // 盯的是 `provider: settings.provider` 紧接着 `llmProvider: settings.xxxProvider`
88
+ // 这个组合——单独一句 `llmProvider: settings.buildProvider` 还留着,
89
+ // 那是 stageModelOf 的 fallback 参数,是应该有的。
90
+ //
91
+ // 规划段不查:审美采集(stageKey: 'search')本来就用 planProvider,
92
+ // 它不是四阶里的任何一阶,不该被 phase.model 覆盖。
93
+ for (const key of ['buildProvider', 'checkProvider']) {
94
+ const stale = new RegExp(`provider: settings\\.provider,\\s*\\n\\s*llmProvider: settings\\.${key}`, 'u')
95
+ assert.ok(!stale.test(source), `${key} 那一段还留着直接读 settings 的老写法`)
96
+ }
97
+ // 规划段单独确认:真正的规划调用喂的是 planRoute,不是 settings
98
+ assert.ok(source.includes('provider: planRoute.provider'), '规划段要用 planRoute')
99
+ })
100
+
101
+ test('回退日志带 tried / reason / fellBackTo 三段', () => {
102
+ assert.ok(source.includes('tried=${describe(item.route)}'), '要记试过哪一级')
103
+ assert.ok(source.includes('reason=${messageOf(item.error)'), '要记失败原因')
104
+ assert.ok(source.includes('fellBackTo=${describe(used)}'), '要记最后落到哪一级')
105
+ assert.ok(source.includes('const failures = []'), '要收集每一级的失败,不是只留第一个')
106
+ })
107
+
108
+ test('模型名写错在保存时就拒,并列出可用的', () => {
109
+ assert.ok(source.includes('const unknownModelMessage = async model =>'), '要有校验函数')
110
+ assert.ok(source.includes('不在本机可用列表里,没有写入'), '拒绝时要说清没写入')
111
+ assert.ok(source.includes('return { ok: false, line: unknown }'), 'project_set_model 要真的拿它拦')
112
+ })
113
+
114
+ test('一个模型都探不到时放行,不把人锁死', () => {
115
+ const at = source.indexOf('const unknownModelMessage = async model =>')
116
+ assert.ok(at > 0)
117
+ const window = source.slice(at, at + 900)
118
+ assert.ok(window.includes('if (known.length === 0) return undefined'),
119
+ '探测不到列表就得放行——那时候拦人等于把人锁在配置外面')
120
+ })