dsh-project-flow 0.9.0 → 0.9.1

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 (48) 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/_wd_sandbox/.project-flow/demo/run.log +1 -0
  19. package/tests/_wd_sandbox/a.txt +1 -0
  20. package/tests/aesthetic.test.mjs +534 -0
  21. package/tests/assertions-e2e.mjs +234 -0
  22. package/tests/bench-check.mjs +32 -0
  23. package/tests/client-bundle.test.mjs +707 -0
  24. package/tests/contract.test.mjs +137 -0
  25. package/tests/desktop.test.mjs +130 -0
  26. package/tests/domains.test.mjs +220 -0
  27. package/tests/e2e.mjs +300 -0
  28. package/tests/evidence.test.mjs +408 -0
  29. package/tests/extra-phase.test.mjs +133 -0
  30. package/tests/feedback.test.mjs +187 -0
  31. package/tests/flow-create.test.mjs +168 -0
  32. package/tests/flow-gate.test.mjs +232 -0
  33. package/tests/flow-schema.test.mjs +213 -0
  34. package/tests/gate.test.mjs +246 -0
  35. package/tests/issue-loop.test.mjs +254 -0
  36. package/tests/issues-e2e.mjs +246 -0
  37. package/tests/panel-independence.test.mjs +78 -0
  38. package/tests/panel-report.test.mjs +129 -0
  39. package/tests/patch-ledger.test.mjs +280 -0
  40. package/tests/paths.mjs +94 -0
  41. package/tests/preview-path.test.mjs +69 -0
  42. package/tests/qa-assertions.test.mjs +352 -0
  43. package/tests/run-all.mjs +90 -0
  44. package/tests/stage-model.test.mjs +120 -0
  45. package/tests/stop-resume.test.mjs +260 -0
  46. package/tests/watch-root.test.mjs +167 -0
  47. package/tests/watchdog-tokens.test.mjs +49 -0
  48. package/tests/watchdog.test.mjs +301 -0
@@ -0,0 +1,408 @@
1
+ import { test, before, after } from 'node:test'
2
+ import { pathToFileURL } from 'node:url'
3
+ import { sibling } from './paths.mjs'
4
+ import assert from 'node:assert/strict'
5
+ import { mkdir, writeFile, rm, readFile } from 'node:fs/promises'
6
+ import { join, dirname } from 'node:path'
7
+ import { fileURLToPath } from 'node:url'
8
+ const {
9
+ runTestCommand,
10
+ detectTestCommand,
11
+ collectFileChanges,
12
+ extractRisks,
13
+ costOf,
14
+ buildEvidence,
15
+ writeEvidence,
16
+ readEvidence,
17
+ evidenceSummary,
18
+ EVIDENCE_FILE,
19
+ } = await import(pathToFileURL(sibling('evidence.mjs')).href + `?t=${Date.now()}`)
20
+
21
+ const here = dirname(fileURLToPath(import.meta.url))
22
+ const sandbox = join(here, '_ev_sandbox')
23
+
24
+ before(async () => {
25
+ await rm(sandbox, { recursive: true, force: true })
26
+ await mkdir(sandbox, { recursive: true })
27
+ })
28
+ after(async () => {
29
+ await rm(sandbox, { recursive: true, force: true })
30
+ })
31
+
32
+ // ── 测试命令 ───────────────────────────────────────
33
+
34
+ test('exit 0 才算通过', async () => {
35
+ const ok = await runTestCommand('exit 0', sandbox)
36
+ assert.equal(ok.ok, true)
37
+ assert.equal(ok.exitCode, 0)
38
+ assert.equal(ok.ran, true)
39
+ })
40
+
41
+ test('非零退出码判不通过,且记录真实码', async () => {
42
+ const bad = await runTestCommand('exit 3', sandbox)
43
+ assert.equal(bad.ok, false)
44
+ assert.equal(bad.exitCode, 3)
45
+ })
46
+
47
+ test('命令不存在也是不通过,不崩', async () => {
48
+ const missing = await runTestCommand('definitely-not-a-real-cmd-xyz', sandbox)
49
+ assert.equal(missing.ok, false)
50
+ assert.notEqual(missing.exitCode, 0)
51
+ })
52
+
53
+ test('没配命令时不跑,ok 为 false 但 ran 为 false', async () => {
54
+ const none = await runTestCommand('', sandbox)
55
+ assert.equal(none.ok, false)
56
+ assert.equal(none.ran, false)
57
+ assert.match(none.reason, /没有配置/u)
58
+ })
59
+
60
+ test('超时被杀也算不通过', async () => {
61
+ const slow = await runTestCommand('sleep 5', sandbox, 300)
62
+ assert.equal(slow.ok, false)
63
+ })
64
+
65
+ test('捕获 stdout 尾巴,供证据包引用', async () => {
66
+ const out = await runTestCommand('echo hello-evidence', sandbox)
67
+ assert.equal(out.ok, true)
68
+ assert.match(out.stdoutTail, /hello-evidence/u)
69
+ })
70
+
71
+ // ── 行数变化 ───────────────────────────────────────
72
+
73
+ test('有快照时按 numstat 给出每个文件的行数', async () => {
74
+ const calls = []
75
+ const fakeGit = async args => {
76
+ calls.push(args)
77
+ if (args.includes('--name-status')) {
78
+ return { ok: true, stdout: 'M\tsrc/a.js\nD\told.js\nA\tnew.js\n' }
79
+ }
80
+ return {
81
+ ok: true,
82
+ stdout: '12\t3\tsrc/a.js\n0\t5\told.js\n7\t0\tnew.js\n',
83
+ }
84
+ }
85
+ const rows = await collectFileChanges(fakeGit, [
86
+ { before: 'c1', after: 'c2' },
87
+ ], [])
88
+ assert.equal(rows.length, 3)
89
+ assert.deepEqual(rows[0], {
90
+ path: 'src/a.js', change: 'modified', added_lines: 12, deleted_lines: 3, measured: true,
91
+ })
92
+ assert.equal(rows[1].change, 'deleted')
93
+ assert.equal(rows[2].change, 'added')
94
+ // 用的是第一轮的 before 和最后一轮的 after
95
+ assert.ok(calls[0].includes('c1'))
96
+ assert.ok(calls[0].includes('c2'))
97
+ })
98
+
99
+ test('变更类型以 name-status 为准,不靠行数猜', async () => {
100
+ // 纯追加的旧文件:numstat 是 +6/-0,看起来像新增,但 name-status 说它是 M
101
+ const fakeGit = async args => {
102
+ if (args.includes('--name-status')) return { ok: true, stdout: 'M\tREADME.md\n' }
103
+ return { ok: true, stdout: '6\t0\tREADME.md\n' }
104
+ }
105
+ const rows = await collectFileChanges(fakeGit, [{ before: 'a', after: 'b' }], [])
106
+ assert.equal(rows[0].change, 'modified', '纯追加不等于新增文件')
107
+ assert.equal(rows[0].added_lines, 6)
108
+ })
109
+
110
+ test('name-status 拿不到时按行数降级推断', async () => {
111
+ const fakeGit = async args => {
112
+ if (args.includes('--name-status')) return { ok: false, stdout: '' }
113
+ return { ok: true, stdout: '6\t0\tonly-add.js\n' }
114
+ }
115
+ const rows = await collectFileChanges(fakeGit, [{ before: 'a', after: 'b' }], [])
116
+ assert.equal(rows[0].change, 'added')
117
+ })
118
+
119
+ test('多轮返工取第一轮前 / 最后一轮后,累加结果', async () => {
120
+ let seen = null
121
+ const fakeGit = async args => {
122
+ seen = args
123
+ return { ok: true, stdout: '1\t1\tx.js\n' }
124
+ }
125
+ await collectFileChanges(fakeGit, [
126
+ { before: 'r1-before', after: 'r1-after' },
127
+ { before: 'r2-before', after: 'r2-after' },
128
+ ], [])
129
+ assert.ok(seen.includes('r1-before'))
130
+ assert.ok(seen.includes('r2-after'))
131
+ })
132
+
133
+ test('二进制文件行数记 null,不冒充 0', async () => {
134
+ const fakeGit = async () => ({ ok: true, stdout: '-\t-\tlogo.png\n' })
135
+ const rows = await collectFileChanges(fakeGit, [{ before: 'a', after: 'b' }], [])
136
+ assert.equal(rows[0].added_lines, null)
137
+ assert.equal(rows[0].measured, false)
138
+ })
139
+
140
+ test('没有快照时给文件清单,行数写 null 而不是 0', async () => {
141
+ const rows = await collectFileChanges(async () => ({ ok: false }), [], ['a.js', 'b.js'])
142
+ assert.equal(rows.length, 2)
143
+ assert.equal(rows[0].added_lines, null)
144
+ assert.equal(rows[0].measured, false)
145
+ })
146
+
147
+ test('git 不可用又没文件,返回空数组', async () => {
148
+ const rows = await collectFileChanges(async () => ({ ok: false }), [], [])
149
+ assert.deepEqual(rows, [])
150
+ })
151
+
152
+ // ── 风险提取 ───────────────────────────────────────
153
+
154
+ test('从 QA_Report 的「可接受的风险」节抠条目', () => {
155
+ const qa = [
156
+ '# 质检结论:PASS',
157
+ '',
158
+ 'PASS',
159
+ '',
160
+ '## 核过的要求',
161
+ '- 要求一已实现',
162
+ '',
163
+ '## 可接受的风险',
164
+ '- 没写并发测试,单机场景够用',
165
+ '- 大文件上传未做分片,暂限 10MB',
166
+ '',
167
+ '## 其它',
168
+ '- 这条不该被算进去',
169
+ ].join('\n')
170
+ const risks = extractRisks(qa)
171
+ assert.equal(risks.length, 2)
172
+ assert.match(risks[0], /并发测试/u)
173
+ assert.ok(!risks.some(item => item.includes('不该被算')))
174
+ })
175
+
176
+ test('英文标题的风险节也认', () => {
177
+ assert.equal(extractRisks('## Risks\n- a\n- b\n').length, 2)
178
+ })
179
+
180
+ test('没有风险节就返回空', () => {
181
+ assert.deepEqual(extractRisks('# 结论\nPASS\n'), [])
182
+ assert.deepEqual(extractRisks(''), [])
183
+ assert.deepEqual(extractRisks(undefined), [])
184
+ })
185
+
186
+ // ── 成本 ───────────────────────────────────────────
187
+
188
+ test('token 换算成美元和人民币', () => {
189
+ const cost = costOf({
190
+ inputTokens: 1_000_000,
191
+ outputTokens: 500_000,
192
+ pricing: { input: 0.14, output: 0.28 },
193
+ cnyRate: 7.2,
194
+ })
195
+ // 0.14 + 0.14 = 0.28 USD
196
+ assert.equal(cost.estimated_usd, 0.28)
197
+ assert.equal(cost.estimated_cny, 2.016)
198
+ assert.equal(cost.cny_rate, 7.2)
199
+ })
200
+
201
+ test('汇率非法就退回默认,不产生 NaN', () => {
202
+ const cost = costOf({
203
+ inputTokens: 1_000_000, outputTokens: 0,
204
+ pricing: { input: 1, output: 0 }, cnyRate: 'abc',
205
+ })
206
+ assert.ok(Number.isFinite(cost.estimated_cny))
207
+ assert.equal(cost.cny_rate, 7.2)
208
+ })
209
+
210
+ // ── 整份证据包 ─────────────────────────────────────
211
+
212
+ const sample = over => ({
213
+ project: 'demo',
214
+ artifacts: join(sandbox, '.project-flow', 'demo'),
215
+ startedAt: '2026-11-30T00:00:00.000Z',
216
+ intent: '做一个东西',
217
+ requirements: ['a', 'b'],
218
+ test: { command: 'pnpm test', ran: true, ok: true, exitCode: 0, durationMs: 1200, reason: 'exit code 0' },
219
+ cost: costOf({ inputTokens: 200_000, outputTokens: 100_000, pricing: { input: 0.14, output: 0.28 }, cnyRate: 7.2 }),
220
+ counters: { stageRetries: 2, stageCalls: 3, byStage: { plan: 0, build: 2, check: 0 }, reworks: 1, rollbacks: 0 },
221
+ files: [{ path: 'a.js', change: 'modified', added_lines: 10, deleted_lines: 2, measured: true }],
222
+ qa: { verdict: 'PASS', reasons: ['核过'], acceptableRisks: ['没写并发测试'] },
223
+ assumptions: ['字体用系统默认'],
224
+ ...over,
225
+ })
226
+
227
+ test('五个必填字段齐全', async () => {
228
+ const ev = await buildEvidence(sample())
229
+ for (const key of ['requirement_met', 'cost_incurred', 'retry_count', 'files_modified', 'remaining_risks']) {
230
+ assert.ok(key in ev, `缺字段 ${key}`)
231
+ }
232
+ })
233
+
234
+ test('requirement_met 直接来自 exit code', async () => {
235
+ assert.equal((await buildEvidence(sample())).requirement_met, true)
236
+ const failed = await buildEvidence(sample({
237
+ test: { command: 'pnpm test', ran: true, ok: false, exitCode: 1, durationMs: 900, reason: 'exit code 1' },
238
+ }))
239
+ assert.equal(failed.requirement_met, false)
240
+ assert.equal(failed.requirement_source.exit_code, 1)
241
+ })
242
+
243
+ test('成本标了估算口径,不冒充账单', async () => {
244
+ const ev = await buildEvidence(sample())
245
+ assert.equal(ev.cost_incurred.method, 'estimated_from_characters')
246
+ assert.equal(ev.cost_incurred.total_tokens, 300_000)
247
+ assert.match(ev.cost_incurred.precision_note, /估算/u)
248
+ })
249
+
250
+ test('retry_count 拆开三来源', async () => {
251
+ const ev = await buildEvidence(sample())
252
+ assert.equal(ev.retry_count.stage_retries, 2)
253
+ assert.equal(ev.retry_count.reworks, 1)
254
+ assert.equal(ev.retry_count.rollbacks, 0)
255
+ assert.equal(ev.retry_count.total, 3)
256
+ assert.equal(ev.retry_count.by_stage.build, 2)
257
+ assert.equal(ev.retry_count.stage_calls, 3)
258
+ })
259
+
260
+ test('一次跑通时 retry_count.total 必须是 0,不能把首次调用算成重试', async () => {
261
+ const ev = await buildEvidence(sample({
262
+ counters: { stageRetries: 0, stageCalls: 3, byStage: { plan: 0, build: 0, check: 0 }, reworks: 0, rollbacks: 0 },
263
+ }))
264
+ assert.equal(ev.retry_count.total, 0)
265
+ assert.equal(ev.retry_count.stage_calls, 3)
266
+ })
267
+
268
+ test('max_rework_reached 只在真用满时才是 true', async () => {
269
+ assert.equal((await buildEvidence(sample({
270
+ counters: { stageRetries: 0, stageCalls: 3, byStage: { plan: 0, build: 0, check: 0 }, reworks: 0, rollbacks: 0, maxReworkReached: false },
271
+ }))).retry_count.max_rework_reached, false)
272
+ assert.equal((await buildEvidence(sample({
273
+ counters: { stageRetries: 0, stageCalls: 6, byStage: { plan: 0, build: 0, check: 0 }, reworks: 2, rollbacks: 0, maxReworkReached: true },
274
+ }))).retry_count.max_rework_reached, true)
275
+ })
276
+
277
+ test('风险合并 QA 报告与假设妥协并去重', async () => {
278
+ const ev = await buildEvidence(sample({
279
+ qa: { verdict: 'PASS', reasons: [], acceptableRisks: ['同一个', '只有一条'] },
280
+ assumptions: ['同一个', '另一条'],
281
+ }))
282
+ assert.equal(ev.remaining_risks.length, 3)
283
+ assert.equal(ev.risk_sources.from_qa_report, 2)
284
+ assert.equal(ev.risk_sources.from_assumptions, 2)
285
+ })
286
+
287
+ test('文件汇总行数正确,二进制让 measured 为 false', async () => {
288
+ const ev = await buildEvidence(sample({
289
+ files: [
290
+ { path: 'a.js', change: 'modified', added_lines: 10, deleted_lines: 2, measured: true },
291
+ { path: 'b.png', change: 'added', added_lines: null, deleted_lines: null, measured: false },
292
+ ],
293
+ }))
294
+ assert.equal(ev.files_summary.count, 2)
295
+ assert.equal(ev.files_summary.added_lines, 10)
296
+ assert.equal(ev.files_summary.measured, false)
297
+ })
298
+
299
+ // ── 猜测试命令 ─────────────────────────────────────
300
+ //
301
+ // testCommand 默认 auto,这几条锁的是「猜错了也要说清依据」,
302
+ // 而不是「猜得准」——环境千差万别,唯一不能容忍的是静默装作没测试。
303
+
304
+ test('猜测试命令:没有测试就直接说没有,不编一条', async () => {
305
+ const dir = join(sandbox, 'probe-empty')
306
+ await mkdir(dir, { recursive: true })
307
+ const r = await detectTestCommand(dir)
308
+ assert.equal(r.command, '')
309
+ assert.match(r.why, /没找到/u)
310
+ })
311
+
312
+ test('猜测试命令:有 test_*.py 就走 unittest discover,且不带引号', async () => {
313
+ const dir = join(sandbox, 'probe-py')
314
+ await mkdir(dir, { recursive: true })
315
+ await writeFile(join(dir, 'test_demo.py'), 'def test_x():\n assert True\n')
316
+ const r = await detectTestCommand(dir)
317
+ assert.match(r.command, /-m unittest discover/u)
318
+ assert.match(r.command, /-p test_\*\.py/u)
319
+ // 带引号会被 cmd /c 吃掉一层,实测跑出 0 个测试
320
+ assert.doesNotMatch(r.command, /"/u)
321
+ assert.match(r.why, /test_demo\.py/u)
322
+ })
323
+
324
+ test('猜测试命令:package.json 里的占位脚本不算测试', async () => {
325
+ const dir = join(sandbox, 'probe-node')
326
+ await mkdir(dir, { recursive: true })
327
+ await writeFile(join(dir, 'package.json'), JSON.stringify({
328
+ scripts: { test: 'echo "Error: no test specified" && exit 1' },
329
+ }))
330
+ const none = await detectTestCommand(dir)
331
+ assert.notEqual(none.command, 'npm test')
332
+ await writeFile(join(dir, 'package.json'), JSON.stringify({ scripts: { test: 'vitest run' } }))
333
+ const yes = await detectTestCommand(dir)
334
+ assert.equal(yes.command, 'npm test')
335
+ assert.match(yes.why, /scripts\.test/u)
336
+ })
337
+
338
+ test('猜测试命令:有 pytest 配置就走 pytest,不退回 unittest', async () => {
339
+ const dir = join(sandbox, 'probe-pytest')
340
+ await mkdir(dir, { recursive: true })
341
+ await writeFile(join(dir, 'conftest.py'), '')
342
+ await writeFile(join(dir, 'test_a.py'), 'def test_a():\n pass\n')
343
+ const r = await detectTestCommand(dir)
344
+ assert.match(r.command, /-m pytest/u)
345
+ assert.match(r.why, /pytest/u)
346
+ })
347
+
348
+ test('猜测试命令:没给目录就明说猜不了', async () => {
349
+ const r = await detectTestCommand('')
350
+ assert.equal(r.command, '')
351
+ assert.match(r.why, /猜不了/u)
352
+ })
353
+
354
+ test('猜测试命令:根目录没有、子目录有,就往下扫一层并指明该在哪跑', async () => {
355
+ const dir = join(sandbox, 'probe-nested')
356
+ const sub = join(dir, 'output')
357
+ await mkdir(sub, { recursive: true })
358
+ await writeFile(join(sub, 'test_nested.py'), 'def test_n():\n pass\n')
359
+ const r = await detectTestCommand(dir)
360
+ assert.match(r.command, /-m unittest discover/u)
361
+ // 产物落在子目录里是常态:本机实测踩过,根目录干干净净,探测直接说没找到
362
+ assert.equal(r.cwd, sub)
363
+ assert.match(r.why, /output\//u)
364
+ })
365
+
366
+ test('猜测试命令:不往 node_modules 这类目录里钻', async () => {
367
+ const dir = join(sandbox, 'probe-skip')
368
+ const sub = join(dir, 'node_modules')
369
+ await mkdir(sub, { recursive: true })
370
+ await writeFile(join(sub, 'test_fake.py'), 'def test_f():\n pass\n')
371
+ const r = await detectTestCommand(dir)
372
+ assert.equal(r.command, '')
373
+ })
374
+
375
+ test('猜出来的命令找不到可执行文件时,理由里要点明是环境问题', async () => {
376
+ const dir = join(sandbox, 'probe-noexec')
377
+ await mkdir(dir, { recursive: true })
378
+ await writeFile(join(dir, 'test_b.py'), 'def test_b():\n pass\n')
379
+ const r = await detectTestCommand(dir)
380
+ // 这台机器上 PATH 的 python 是应用商店占位程序,探测要么挑到 DSH 自带的,
381
+ // 要么如实写「找不到可用的 python」——两种都行,不能一个字不说。
382
+ assert.match(r.why, /test_b\.py/u)
383
+ assert.ok(r.command !== '' || r.why.includes('python'))
384
+ })
385
+
386
+ // ── 落盘 ───────────────────────────────────────────
387
+
388
+ test('写到约定文件名,读回来一致', async () => {
389
+ const dir = join(sandbox, '.project-flow', 'demo')
390
+ await mkdir(dir, { recursive: true })
391
+ const ev = await buildEvidence(sample())
392
+ const path = await writeEvidence(dir, ev)
393
+ assert.match(path, new RegExp(`${EVIDENCE_FILE}$`))
394
+ const back = await readEvidence(dir)
395
+ assert.deepEqual(back, ev)
396
+ // 必须是合法 JSON,机器能读
397
+ const raw = await readFile(path, 'utf8')
398
+ assert.doesNotThrow(() => JSON.parse(raw))
399
+ })
400
+
401
+ test('摘要一行一件事,便于塞进返回值', async () => {
402
+ const ev = await buildEvidence(sample())
403
+ const line = evidenceSummary(ev, '/tmp/x.json')
404
+ assert.match(line, /测试 通过/u)
405
+ assert.match(line, /约 300000 token/u)
406
+ assert.match(line, /重试 3 次/u)
407
+ assert.match(line, /残留风险 2 条/u)
408
+ })
@@ -0,0 +1,133 @@
1
+ /**
2
+ * 中间阶段(T4 第二部分)的测试。
3
+ *
4
+ * 要守的就一件事:**flow.yaml 能在实施和质检之间插阶段,而内置四阶不受影响**。
5
+ *
6
+ * 上半部分验 schema:五个环节的配置能过校验,slice(2, -1) 切出来的正是中间那个。
7
+ * 下半部分是静态断言:执行器里确实有那段循环,且空数组时不跑任何东西。
8
+ *
9
+ * 真起子会话跑五阶需要 dsh 运行环境,这里跑不了;这一层只保证调度逻辑对。
10
+ */
11
+ import { test } from 'node:test'
12
+ import assert from 'node:assert/strict'
13
+ import { join } from 'node:path'
14
+ import { readFile } from 'node:fs/promises'
15
+ import { pathToFileURL } from 'node:url'
16
+ import { LIVE_DIR } from './paths.mjs'
17
+
18
+ const mod = await import(pathToFileURL(join(LIVE_DIR, 'flow.schema.mjs')).href + `?t=${Date.now()}`)
19
+ const { loadFlow, BUILTIN_FLOW, describeFlowProblems } = mod
20
+
21
+ const source = await readFile(join(LIVE_DIR, 'index.js'), 'utf8')
22
+
23
+ const WITH_SECURITY = `process: { name: with-security, version: 1 }
24
+ phases:
25
+ - id: requirement
26
+ reads: []
27
+ writes: [Requirement.md]
28
+ gate: { kind: artifact_present }
29
+ - id: plan
30
+ reads: [Requirement.md]
31
+ writes: [Plan.md]
32
+ gate: { kind: artifact_present }
33
+ - id: build
34
+ reads: [Plan.md]
35
+ writes: [Execution_Log.md]
36
+ gate: { kind: artifact_present }
37
+ - id: security
38
+ reads: [Plan.md]
39
+ writes: [Security_Report.md]
40
+ instructions: 评审这份方案的安全风险,把结论写进 Security_Report.md
41
+ gate: { kind: artifact_present }
42
+ - id: qa
43
+ reads: [Requirement.md, Plan.md, Execution_Log.md]
44
+ writes: [QA_Report.md]
45
+ gate: { kind: artifact_present, on_fail: build }
46
+ `
47
+
48
+ test('插了 security 的五环节配置能过校验', () => {
49
+ const r = loadFlow(WITH_SECURITY)
50
+ assert.equal(r.ok, true, describeFlowProblems(r))
51
+ assert.equal(r.phases.length, 5)
52
+ assert.deepEqual(r.phases.map(p => p.id), ['requirement', 'plan', 'build', 'security', 'qa'])
53
+ })
54
+
55
+ test('slice(3, -1) 切出来的正是插在中间的那些', () => {
56
+ const r = loadFlow(WITH_SECURITY)
57
+ assert.ok(r.ok, describeFlowProblems(r))
58
+ const middle = r.phases.slice(3, -1)
59
+ assert.equal(middle.length, 1)
60
+ assert.equal(middle[0].id, 'security')
61
+ })
62
+
63
+ test('内置四阶切出来是空的——这就是「行为不变」的兑现处', () => {
64
+ // 四个阶段时 slice(3, -1) 必须一个都不剩。
65
+ // 写成 slice(2, -1) 会切出 build,内置四阶下等于把实施阶段当中间阶段又跑一遍。
66
+ const middle = BUILTIN_FLOW.phases.slice(3, -1)
67
+ assert.equal(middle.length, 0)
68
+ })
69
+
70
+ test('额外环节没写 instructions 会被提醒,但不拦', () => {
71
+ const r = loadFlow(`process: { name: x }
72
+ phases:
73
+ - id: requirement
74
+ - id: plan
75
+ - id: build
76
+ - id: security
77
+ reads: [Plan.md]
78
+ - id: qa
79
+ `)
80
+ assert.equal(r.ok, true, describeFlowProblems(r))
81
+ assert.ok(
82
+ r.warnings.some(w => w.message.includes('security') && w.message.includes('instructions')),
83
+ `应该有一条提到 security 缺 instructions 的提醒,实际:${JSON.stringify(r.warnings)}`,
84
+ )
85
+ })
86
+
87
+ test('内置四阶那四个不写 instructions 不该被提醒', () => {
88
+ const r = loadFlow(`process: { name: x }
89
+ phases:
90
+ - id: requirement
91
+ - id: plan
92
+ - id: build
93
+ - id: qa
94
+ `)
95
+ assert.equal(r.ok, true, describeFlowProblems(r))
96
+ assert.equal(
97
+ r.warnings.filter(w => w.message.includes('instructions')).length,
98
+ 0,
99
+ '内置四阶各有专用实现,不该要求写 instructions',
100
+ )
101
+ })
102
+
103
+ test('执行器里有中间阶段那段循环', () => {
104
+ assert.ok(source.includes('phases.slice(3, -1)'), '要按位置切出中间阶段;用 slice(2,-1) 会把 build 也切进去')
105
+ assert.ok(source.includes('const middlePhases'), '切完要存成变量')
106
+ assert.ok(source.includes('for (const phase of middlePhases)'), '要真的循环跑')
107
+ })
108
+
109
+ test('中间阶段没写 instructions 就跳过,不硬跑', () => {
110
+ assert.ok(
111
+ source.includes('没写 instructions,跳过'),
112
+ '没有指令的中间阶段应该跳过并记日志,而不是拿空 prompt 起一个子会话',
113
+ )
114
+ })
115
+
116
+ test('中间阶段失败要停机,不能悄悄往下走质检', () => {
117
+ const at = source.indexOf('for (const phase of middlePhases)')
118
+ assert.ok(at > 0, '找不到中间阶段循环')
119
+ const window = source.slice(at, at + 4000)
120
+ assert.ok(window.includes("outcome: 'blocked'"), '中间阶段失败要 blocked 收场')
121
+ })
122
+
123
+ test('入口允许超过四个环节,但少于四个要拒', () => {
124
+ assert.ok(source.includes('phases.length < 4'), '少于四个环节应该拒跑')
125
+ assert.ok(!source.includes('phases.length !== 4'), '不该再有「必须正好四个」的死判断')
126
+ })
127
+
128
+ test('中间阶段按 artifact_present 判门禁', () => {
129
+ const at = source.indexOf('for (const phase of middlePhases)')
130
+ const window = source.slice(at, at + 4000)
131
+ assert.ok(window.includes("gateOfMiddle.kind === 'artifact_present'"), '要认 artifact_present')
132
+ assert.ok(window.includes('没产出它声明的任何文件'), '产物缺失要报出来')
133
+ })