dsh-project-flow 0.8.3 → 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.
- package/CHANGELOG.md +0 -0
- package/README.md +166 -358
- package/bench/corpus/qa-fail-3-issues.md +33 -0
- package/bench/corpus/qa-fail-assertion.md +20 -0
- package/bench/corpus/qa-fail-missing-detect.md +21 -0
- package/bench/corpus/qa-pass.md +9 -0
- package/bench/corpus/qa-reverify-partial.md +30 -0
- package/bench/corpus/qa-unverified-only.md +15 -0
- package/bench/corpus/requirement-no-check.md +8 -0
- package/bench/corpus/requirement-with-check.md +16 -0
- package/bench/corpus/reverify-table-not-defects.md +18 -0
- package/bench/run.mjs +142 -0
- package/client.js +195 -9
- package/cordis.patch.yml +29 -9
- package/docs/CHECKS.md +100 -0
- package/docs/DETAIL.md +6 -6
- package/docs/FEATURES.md +12 -5
- package/docs/ISSUES.md +75 -0
- package/docs/LAUNCH.md +3 -3
- package/docs/LEDGER.md +46 -0
- package/docs/MOMENTS.md +4 -4
- package/docs/USAGE.md +2 -2
- package/docs/adr/0001-four-stage-isolation.md +25 -0
- package/docs/adr/0002-file-handoff-not-context.md +27 -0
- package/docs/adr/0003-diff-over-self-report.md +27 -0
- package/docs/adr/0004-issue-level-rework.md +30 -0
- package/docs/adr/0005-assertion-over-prose.md +30 -0
- package/docs/anchors/assertion-exec.md +39 -0
- package/docs/anchors/cost-accounting.md +44 -0
- package/docs/anchors/diff-reconciliation.md +35 -0
- package/docs/anchors/file-whitelist.md +29 -0
- package/docs/anchors/intent-gate.md +41 -0
- package/docs/anchors/issue-loop.md +40 -0
- package/docs/anchors/patch-ledger.md +38 -0
- package/docs/anchors/watchdog.md +30 -0
- package/domains.mjs +1 -1
- package/evidence.mjs +19 -3
- package/flow.schema.mjs +600 -0
- package/gate.mjs +49 -23
- package/index.js +2800 -134
- package/locale/en.json +15 -4
- package/locale/zh.json +17 -6
- package/package.json +11 -2
- package/tests/_heartbeat-regression.test.mjs +124 -0
- package/tests/_wd_sandbox/.project-flow/demo/run.log +1 -0
- package/tests/_wd_sandbox/a.txt +1 -0
- package/tests/aesthetic.test.mjs +534 -0
- package/tests/assertions-e2e.mjs +234 -0
- package/tests/bench-check.mjs +32 -0
- package/tests/client-bundle.test.mjs +707 -0
- package/tests/contract.test.mjs +137 -0
- package/tests/desktop.test.mjs +130 -0
- package/tests/domains.test.mjs +220 -0
- package/tests/e2e.mjs +300 -0
- package/tests/evidence.test.mjs +408 -0
- package/tests/extra-phase.test.mjs +133 -0
- package/tests/feedback.test.mjs +187 -0
- package/tests/flow-create.test.mjs +168 -0
- package/tests/flow-gate.test.mjs +232 -0
- package/tests/flow-schema.test.mjs +213 -0
- package/tests/gate.test.mjs +246 -0
- package/tests/issue-loop.test.mjs +254 -0
- package/tests/issues-e2e.mjs +246 -0
- package/tests/panel-independence.test.mjs +78 -0
- package/tests/panel-report.test.mjs +129 -0
- package/tests/patch-ledger.test.mjs +280 -0
- package/tests/paths.mjs +94 -0
- package/tests/preview-path.test.mjs +69 -0
- package/tests/qa-assertions.test.mjs +352 -0
- package/tests/run-all.mjs +90 -0
- package/tests/stage-model.test.mjs +120 -0
- package/tests/stop-resume.test.mjs +260 -0
- package/tests/watch-root.test.mjs +167 -0
- package/tests/watchdog-tokens.test.mjs +49 -0
- package/tests/watchdog.test.mjs +301 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 面板数据(T5 数据层)的测试。
|
|
3
|
+
*
|
|
4
|
+
* 面板要显示的东西里,issues 和 assertions 得从 QA_Report.md 读回来。
|
|
5
|
+
* 这一套只盯解析和接线,不管界面长什么样——界面那块测不了。
|
|
6
|
+
*
|
|
7
|
+
* 关键一条:读不到报告要返回空结构,不能抛。没跑过质检的项目本来就没有
|
|
8
|
+
* 这份文件,面板不该因为它白屏。
|
|
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 { parseAssertionBlock, parseIssueBlock } = mod
|
|
19
|
+
|
|
20
|
+
const source = await readFile(join(LIVE_DIR, 'index.js'), 'utf8')
|
|
21
|
+
|
|
22
|
+
const REPORT = `---
|
|
23
|
+
verdict: "FAIL(断言未过 1 条)"
|
|
24
|
+
round: 1
|
|
25
|
+
assertions:
|
|
26
|
+
- req: 1
|
|
27
|
+
cmd: "node -e \\"process.exit(1)\\""
|
|
28
|
+
exit: 1
|
|
29
|
+
durationMs: 63
|
|
30
|
+
status: fail
|
|
31
|
+
- req: 2
|
|
32
|
+
cmd: "npm run test:ci"
|
|
33
|
+
exit: 0
|
|
34
|
+
durationMs: 1840
|
|
35
|
+
status: pass
|
|
36
|
+
- req: 3
|
|
37
|
+
cmd: ""
|
|
38
|
+
exit: null
|
|
39
|
+
durationMs: 0
|
|
40
|
+
status: unverified
|
|
41
|
+
issues:
|
|
42
|
+
- id: Q-001
|
|
43
|
+
severity: P0
|
|
44
|
+
location: src/foo.ts:88
|
|
45
|
+
claim: 任务书要求 X,实际未实现
|
|
46
|
+
detect: npm test -- foo.test.ts
|
|
47
|
+
pass: 全部 12 条通过
|
|
48
|
+
status: open
|
|
49
|
+
- id: Q-002
|
|
50
|
+
severity: P1
|
|
51
|
+
claim: 另一条
|
|
52
|
+
status: closed
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
# 质检结论:FAIL
|
|
56
|
+
|
|
57
|
+
正文随便写点什么。
|
|
58
|
+
`
|
|
59
|
+
|
|
60
|
+
test('断言块能读回来:req / cmd / exit / status', () => {
|
|
61
|
+
const rows = parseAssertionBlock(REPORT)
|
|
62
|
+
assert.equal(rows.length, 3)
|
|
63
|
+
assert.equal(rows[0].req, 1)
|
|
64
|
+
assert.equal(rows[0].cmd, 'node -e "process.exit(1)"')
|
|
65
|
+
assert.equal(rows[0].exit, 1)
|
|
66
|
+
assert.equal(rows[0].status, 'fail')
|
|
67
|
+
assert.equal(rows[1].status, 'pass')
|
|
68
|
+
assert.equal(rows[1].durationMs, 1840)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('没写 exit 的记 null,不记 0——「没跑」和「跑过了是 0」是两件事', () => {
|
|
72
|
+
const rows = parseAssertionBlock(REPORT)
|
|
73
|
+
assert.equal(rows[2].status, 'unverified')
|
|
74
|
+
assert.equal(rows[2].exit, null)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('issues 段不会被当成 assertions 读进去', () => {
|
|
78
|
+
const rows = parseAssertionBlock(REPORT)
|
|
79
|
+
assert.equal(rows.length, 3, 'issues 下面那两条不该混进断言')
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('没有前置块就返回空数组', () => {
|
|
83
|
+
assert.deepEqual(parseAssertionBlock('# 只有正文\n\n没有前置块'), [])
|
|
84
|
+
assert.deepEqual(parseAssertionBlock(''), [])
|
|
85
|
+
assert.deepEqual(parseAssertionBlock(undefined), [])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('issues 和 assertions 能同时读,互不串味', () => {
|
|
89
|
+
const issues = parseIssueBlock(REPORT)
|
|
90
|
+
assert.equal(issues.length, 2)
|
|
91
|
+
assert.equal(issues[0].id, 'Q-001')
|
|
92
|
+
assert.equal(issues[0].severity, 'P0')
|
|
93
|
+
assert.equal(issues[0].status, 'open')
|
|
94
|
+
assert.equal(issues[1].status, 'closed')
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('panelSnapshot 把报告摘要带出去了', () => {
|
|
98
|
+
assert.ok(source.includes('report: await panelReportOf(watch, name)'),
|
|
99
|
+
'项目对象里要带 report 字段')
|
|
100
|
+
assert.ok(source.includes('async function panelReportOf(watch, name)'),
|
|
101
|
+
'要有那个摘要函数')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('读不到报告返回空结构,不抛', () => {
|
|
105
|
+
const at = source.indexOf('async function panelReportOf(watch, name)')
|
|
106
|
+
assert.ok(at > 0)
|
|
107
|
+
const window = source.slice(at, at + 1400)
|
|
108
|
+
assert.ok(window.includes('const empty = {'), '要有空结构兜底')
|
|
109
|
+
assert.ok(window.includes('return empty'), '读不到就返回空结构')
|
|
110
|
+
assert.ok(window.includes('} catch {'), '读文件失败要吞掉,不往外抛')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('只留还开着的问题,closed 的不进面板', () => {
|
|
114
|
+
const at = source.indexOf('async function panelReportOf(watch, name)')
|
|
115
|
+
const window = source.slice(at, at + 1400)
|
|
116
|
+
assert.ok(
|
|
117
|
+
window.includes("item.status === 'open' || item.status === 'reopened'"),
|
|
118
|
+
'面板只该显示 open 和 reopened',
|
|
119
|
+
)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('挂掉的命令单独拎出来,带退出码', () => {
|
|
123
|
+
const at = source.indexOf('async function panelReportOf')
|
|
124
|
+
// 函数体约 1600 字符,取 2200 留余量——窗口开小了会把 failed 那段切掉,
|
|
125
|
+
// 之前就是 1400 导致这条假失败。
|
|
126
|
+
const window = source.slice(at, at + 2200)
|
|
127
|
+
assert.ok(window.includes("item.status === 'fail'"), 'fail 要进 failed')
|
|
128
|
+
assert.ok(window.includes('exit: Number.isInteger(item.exit)'), '退出码要带上')
|
|
129
|
+
})
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 补丁台账(T2)的单测。
|
|
3
|
+
*
|
|
4
|
+
* 盯四件事:
|
|
5
|
+
* 1. 台账逐行 JSON 追加写,不覆盖
|
|
6
|
+
* 2. 坏 JSON 行报错,不静默跳过
|
|
7
|
+
* 3. ledger 回滚只动台账里的文件,不碰别的
|
|
8
|
+
* 4. normalizedPatchPath 把反斜杠统一成正斜杠
|
|
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, mkdir, writeFile, rm, readFile } from 'node:fs/promises'
|
|
15
|
+
import { existsSync, writeFileSync, appendFileSync } from 'node:fs'
|
|
16
|
+
import { tmpdir } from 'node:os'
|
|
17
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
18
|
+
|
|
19
|
+
const mod = await import(pathToFileURL(join(LIVE_DIR, 'index.js')).href + `?t=${Date.now()}`)
|
|
20
|
+
const {
|
|
21
|
+
normalizedPatchPath,
|
|
22
|
+
appendLedgerEntry,
|
|
23
|
+
readLedger,
|
|
24
|
+
recordFileWrite,
|
|
25
|
+
commitLedgerEntry,
|
|
26
|
+
rollbackByLedger,
|
|
27
|
+
checkLedgerCoverage,
|
|
28
|
+
} = mod
|
|
29
|
+
|
|
30
|
+
async function withTempProject(fn) {
|
|
31
|
+
const dir = await mkdtemp(join(tmpdir(), 'pf-ledger-'))
|
|
32
|
+
const project = 'test-ledger'
|
|
33
|
+
const cwd = dir
|
|
34
|
+
try {
|
|
35
|
+
return await fn({ cwd, project, dir })
|
|
36
|
+
} finally {
|
|
37
|
+
await rm(dir, { recursive: true, force: true })
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── normalizedPatchPath ───────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
test('反斜杠统一成正斜杠', () => {
|
|
44
|
+
assert.equal(normalizedPatchPath('src\\a.ts'), 'src/a.ts')
|
|
45
|
+
assert.equal(normalizedPatchPath('src\\sub\\b.ts'), 'src/sub/b.ts')
|
|
46
|
+
assert.equal(normalizedPatchPath('src/a.ts'), 'src/a.ts')
|
|
47
|
+
assert.equal(normalizedPatchPath('.\\src\\c.ts'), 'src/c.ts')
|
|
48
|
+
assert.equal(normalizedPatchPath(''), '')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('同一文件两种斜杠归一后是同一个 key', () => {
|
|
52
|
+
const a = normalizedPatchPath('src\\a.ts')
|
|
53
|
+
const b = normalizedPatchPath('src/a.ts')
|
|
54
|
+
assert.equal(a, b)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// ── 追加写 ────────────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
test('台账逐行 JSON 追加写,不覆盖', async () => {
|
|
60
|
+
await withTempProject(async ({ cwd, project }) => {
|
|
61
|
+
appendLedgerEntry(cwd, project, { index: 1, batchId: 'R1', stage: 3, path: 'src/a.ts', beforeSha: 'aaa', afterSha: 'bbb', status: 'applied', ts: '2026-01-01T00:00:00Z' })
|
|
62
|
+
appendLedgerEntry(cwd, project, { index: 2, batchId: 'R1', stage: 3, path: 'src/b.ts', beforeSha: null, afterSha: 'ccc', status: 'applied', ts: '2026-01-01T00:00:01Z' })
|
|
63
|
+
const entries = readLedger(cwd, project)
|
|
64
|
+
assert.equal(entries.length, 2)
|
|
65
|
+
assert.equal(entries[0].path, 'src/a.ts')
|
|
66
|
+
assert.equal(entries[1].path, 'src/b.ts')
|
|
67
|
+
assert.equal(entries[1].beforeSha, null)
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('台账不存在时读出来是空表,不抛错', async () => {
|
|
72
|
+
await withTempProject(async ({ cwd, project }) => {
|
|
73
|
+
assert.deepEqual(readLedger(cwd, project), [])
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// ── 坏 JSON 行 ────────────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
test('坏 JSON 行报错并列出行号,不静默跳过', async () => {
|
|
80
|
+
await withTempProject(async ({ cwd, project }) => {
|
|
81
|
+
const path = join(cwd, '.project-flow', project, 'ledger', 'patch-ledger.jsonl')
|
|
82
|
+
await mkdir(join(cwd, '.project-flow', project, 'ledger'), { recursive: true })
|
|
83
|
+
// 第一行是好的,第二行是坏的
|
|
84
|
+
writeFileSync(path, '{"index":1,"path":"a.ts"}\n{bad json}\n', 'utf8')
|
|
85
|
+
assert.throws(() => readLedger(cwd, project), /坏 JSON/u)
|
|
86
|
+
assert.throws(() => readLedger(cwd, project), /第 2 行/u)
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('台账回滚遇到坏 JSON 时拒绝执行,一个文件都不动', async () => {
|
|
91
|
+
await withTempProject(async ({ cwd, project, dir }) => {
|
|
92
|
+
// 先放一个文件
|
|
93
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
94
|
+
await writeFile(join(dir, 'src/a.ts'), 'hello\n', 'utf8')
|
|
95
|
+
// 台账里放一行好的 + 一行坏的
|
|
96
|
+
const path = join(cwd, '.project-flow', project, 'ledger', 'patch-ledger.jsonl')
|
|
97
|
+
await mkdir(join(cwd, '.project-flow', project, 'ledger'), { recursive: true })
|
|
98
|
+
writeFileSync(path, '{"index":1,"batchId":"R1","stage":3,"path":"src/a.ts","beforeSha":null,"afterSha":"abc","status":"applied"}\n{broken}\n', 'utf8')
|
|
99
|
+
// 回滚必须拒绝
|
|
100
|
+
await assert.rejects(() => rollbackByLedger(cwd, project), /坏 JSON/u)
|
|
101
|
+
// 文件还在
|
|
102
|
+
assert.ok(existsSync(join(dir, 'src/a.ts')))
|
|
103
|
+
})
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// ── ledger 回滚:走快照仓库,真还原内容 ─────────────────────
|
|
107
|
+
//
|
|
108
|
+
// 这一组是任务书的四个场景。老实现只比 beforeSha(而且是写入后才算的),
|
|
109
|
+
// 所以「回滚」从来没改过任何文件,测试也一直是绿的——测的是「没坏」不是「有用」。
|
|
110
|
+
// 现在每条都要求:内容真的变回去了。
|
|
111
|
+
|
|
112
|
+
/** 建一个带 git 快照仓库的最小工作区。 */
|
|
113
|
+
async function withSnapshotProject(fn) {
|
|
114
|
+
const { execFileSync } = await import('node:child_process')
|
|
115
|
+
const dir = await mkdtemp(join(tmpdir(), 'pf-ledger-snap-'))
|
|
116
|
+
const project = 'test-ledger'
|
|
117
|
+
const gitDir = join(dir, '.project-flow', project, 'snap.git')
|
|
118
|
+
const git = args => execFileSync('git', [`--git-dir=${gitDir}`, `--work-tree=${dir}`, ...args], { cwd: dir })
|
|
119
|
+
|
|
120
|
+
await mkdir(gitDir, { recursive: true })
|
|
121
|
+
execFileSync('git', ['init', '--bare', '--quiet', gitDir], { cwd: dir })
|
|
122
|
+
git(['config', 'core.bare', 'false'])
|
|
123
|
+
git(['config', 'core.worktree', dir])
|
|
124
|
+
git(['config', 'user.name', 'test'])
|
|
125
|
+
git(['config', 'user.email', 't@t.local'])
|
|
126
|
+
git(['config', 'commit.gpgsign', 'false'])
|
|
127
|
+
|
|
128
|
+
/** 打一张快照,返回 commit sha。 */
|
|
129
|
+
const snap = label => {
|
|
130
|
+
git(['add', '-A'])
|
|
131
|
+
git(['commit', '--quiet', '--allow-empty', '-m', label])
|
|
132
|
+
return git(['rev-parse', 'HEAD']).toString().trim()
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
return await fn({ cwd: dir, project, dir, snap, git })
|
|
136
|
+
} finally {
|
|
137
|
+
await rm(dir, { recursive: true, force: true })
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
test('回滚:修改已有文件,内容真的回到修改前', async () => {
|
|
142
|
+
await withSnapshotProject(async ({ cwd, project, dir, snap }) => {
|
|
143
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
144
|
+
await writeFile(join(dir, 'src/a.ts'), 'v1 原文\n', 'utf8')
|
|
145
|
+
const before = snap('实施前')
|
|
146
|
+
|
|
147
|
+
// 实施段改写
|
|
148
|
+
await writeFile(join(dir, 'src/a.ts'), 'v2 被改坏了\n', 'utf8')
|
|
149
|
+
const entry = await recordFileWrite(cwd, project, 'src/a.ts', 'R1', 3, 1, before)
|
|
150
|
+
commitLedgerEntry(cwd, project, entry, 'sha-after')
|
|
151
|
+
|
|
152
|
+
const result = await rollbackByLedger(cwd, project, { dryRun: false })
|
|
153
|
+
assert.equal(result.ok, true, `回滚应成功:${JSON.stringify(result.failed)}`)
|
|
154
|
+
assert.equal(await readFile(join(dir, 'src/a.ts'), 'utf8'), 'v1 原文\n',
|
|
155
|
+
'文件内容必须真的回到实施前')
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('回滚:新增文件,回滚后它被删掉', async () => {
|
|
160
|
+
await withSnapshotProject(async ({ cwd, project, dir, snap }) => {
|
|
161
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
162
|
+
await writeFile(join(dir, 'src/keep.ts'), '原有文件\n', 'utf8')
|
|
163
|
+
const before = snap('实施前')
|
|
164
|
+
|
|
165
|
+
// 实施段新建一个文件
|
|
166
|
+
await writeFile(join(dir, 'src/new.ts'), '新建的\n', 'utf8')
|
|
167
|
+
const entry = await recordFileWrite(cwd, project, 'src/new.ts', 'R1', 3, 1, before)
|
|
168
|
+
commitLedgerEntry(cwd, project, entry, 'sha-after')
|
|
169
|
+
|
|
170
|
+
const result = await rollbackByLedger(cwd, project, { dryRun: false })
|
|
171
|
+
assert.equal(result.ok, true, `回滚应成功:${JSON.stringify(result.failed)}`)
|
|
172
|
+
assert.equal(existsSync(join(dir, 'src/new.ts')), false, '新增的文件要被删掉')
|
|
173
|
+
assert.equal(await readFile(join(dir, 'src/keep.ts'), 'utf8'), '原有文件\n', '没在台账里的文件不动')
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('回滚:用户在实施后又手改过,仍然回到实施前而不是覆盖成别的东西', async () => {
|
|
178
|
+
await withSnapshotProject(async ({ cwd, project, dir, snap }) => {
|
|
179
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
180
|
+
await writeFile(join(dir, 'src/a.ts'), 'v1 原文\n', 'utf8')
|
|
181
|
+
const before = snap('实施前')
|
|
182
|
+
|
|
183
|
+
await writeFile(join(dir, 'src/a.ts'), 'v2 实施改写\n', 'utf8')
|
|
184
|
+
const entry = await recordFileWrite(cwd, project, 'src/a.ts', 'R1', 3, 1, before)
|
|
185
|
+
commitLedgerEntry(cwd, project, entry, 'sha-after')
|
|
186
|
+
|
|
187
|
+
// 用户之后又手动改了一版
|
|
188
|
+
await writeFile(join(dir, 'src/a.ts'), 'v3 用户手改\n', 'utf8')
|
|
189
|
+
|
|
190
|
+
const result = await rollbackByLedger(cwd, project, { dryRun: false })
|
|
191
|
+
assert.equal(result.ok, true, `回滚应成功:${JSON.stringify(result.failed)}`)
|
|
192
|
+
// 语义是「回到这批写入之前」,所以是 v1,不是 v3 也不是 v2
|
|
193
|
+
assert.equal(await readFile(join(dir, 'src/a.ts'), 'utf8'), 'v1 原文\n',
|
|
194
|
+
'回滚的语义是回到这批写入之前,用户手改的那版会被 pre-rollback 快照保住')
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('回滚:默认干跑,一个文件都不动、台账也不追加', async () => {
|
|
199
|
+
await withSnapshotProject(async ({ cwd, project, dir, snap }) => {
|
|
200
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
201
|
+
await writeFile(join(dir, 'src/a.ts'), 'v1 原文\n', 'utf8')
|
|
202
|
+
const before = snap('实施前')
|
|
203
|
+
|
|
204
|
+
await writeFile(join(dir, 'src/a.ts'), 'v2 改坏\n', 'utf8')
|
|
205
|
+
const entry = await recordFileWrite(cwd, project, 'src/a.ts', 'R1', 3, 1, before)
|
|
206
|
+
commitLedgerEntry(cwd, project, entry, 'sha-after')
|
|
207
|
+
|
|
208
|
+
const beforeEntries = readLedger(cwd, project).length
|
|
209
|
+
// 不传 dryRun:默认必须是干跑
|
|
210
|
+
const result = await rollbackByLedger(cwd, project)
|
|
211
|
+
assert.equal(result.dryRun, true, '默认必须干跑')
|
|
212
|
+
assert.equal(await readFile(join(dir, 'src/a.ts'), 'utf8'), 'v2 改坏\n', '干跑不许动磁盘')
|
|
213
|
+
assert.equal(readLedger(cwd, project).length, beforeEntries, '干跑不许追加台账')
|
|
214
|
+
// 但要说清会动什么
|
|
215
|
+
assert.ok(Array.isArray(result.plan) && result.plan.length > 0, '干跑要给出计划')
|
|
216
|
+
assert.equal(result.plan[0].action, 'restore')
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
test('回滚:老条目没记快照时明确报还原不了,不假装成功', async () => {
|
|
221
|
+
await withTempProject(async ({ cwd, project, dir }) => {
|
|
222
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
223
|
+
await writeFile(join(dir, 'src/a.ts'), '改过的内容\n', 'utf8')
|
|
224
|
+
// 模拟老版本写的条目:只有 beforeSha,没有 beforeCommit
|
|
225
|
+
appendLedgerEntry(cwd, project, {
|
|
226
|
+
index: 1, batchId: 'R1', stage: 3, path: 'src/a.ts',
|
|
227
|
+
beforeSha: 'deadbeef', afterSha: 'deadbeef', status: 'applied', ts: '2026-01-01',
|
|
228
|
+
})
|
|
229
|
+
const result = await rollbackByLedger(cwd, project, { dryRun: false })
|
|
230
|
+
assert.equal(result.ok, false, '还原不了就必须报失败')
|
|
231
|
+
assert.ok(result.failed.some(line => /没有内容来源/u.test(line)), JSON.stringify(result.failed))
|
|
232
|
+
assert.equal(await readFile(join(dir, 'src/a.ts'), 'utf8'), '改过的内容\n', '没来源就不动文件')
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
// ── 越界检测 ──────────────────────────────────────────────────
|
|
237
|
+
|
|
238
|
+
test('越界检测:台账里的文件算 in,不在的算 outOfBounds', async () => {
|
|
239
|
+
await withTempProject(async ({ cwd, project }) => {
|
|
240
|
+
appendLedgerEntry(cwd, project, { index: 1, batchId: 'R1', stage: 3, path: 'src/a.ts', beforeSha: null, afterSha: 'abc', status: 'applied', ts: '2026-01-01' })
|
|
241
|
+
appendLedgerEntry(cwd, project, { index: 2, batchId: 'R1', stage: 3, path: 'src/b.ts', beforeSha: null, afterSha: 'def', status: 'applied', ts: '2026-01-01' })
|
|
242
|
+
const coverage = checkLedgerCoverage(cwd, project, ['src/a.ts', 'src/c.ts', 'src\\b.ts'])
|
|
243
|
+
assert.equal(coverage.total, 3)
|
|
244
|
+
assert.equal(coverage.inLedger, 2)
|
|
245
|
+
assert.deepEqual(coverage.outOfBounds, ['src/c.ts'])
|
|
246
|
+
})
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
test('越界检测:反斜杠路径归一后能对上台账里的正斜杠', async () => {
|
|
250
|
+
await withTempProject(async ({ cwd, project }) => {
|
|
251
|
+
appendLedgerEntry(cwd, project, { index: 1, batchId: 'R1', stage: 3, path: 'src/sub/a.ts', beforeSha: null, afterSha: 'abc', status: 'applied', ts: '2026-01-01' })
|
|
252
|
+
const coverage = checkLedgerCoverage(cwd, project, ['src\\sub\\a.ts'])
|
|
253
|
+
assert.equal(coverage.outOfBounds.length, 0)
|
|
254
|
+
})
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
// ── recordFileWrite + commitLedgerEntry ───────────────────────
|
|
258
|
+
|
|
259
|
+
test('recordFileWrite 拿到 beforeSha,commitLedgerEntry 补 afterSha 并追加台账', async () => {
|
|
260
|
+
await withTempProject(async ({ cwd, project, dir }) => {
|
|
261
|
+
await mkdir(join(dir, 'src'), { recursive: true })
|
|
262
|
+
await writeFile(join(dir, 'src/a.ts'), 'before\n', 'utf8')
|
|
263
|
+
const entry = await recordFileWrite(cwd, project, 'src/a.ts', 'R1', 3, 1)
|
|
264
|
+
assert.ok(entry.beforeSha !== null)
|
|
265
|
+
assert.equal(entry.path, 'src/a.ts')
|
|
266
|
+
assert.equal(entry.status, 'pending')
|
|
267
|
+
// 改文件
|
|
268
|
+
await writeFile(join(dir, 'src/a.ts'), 'after\n', 'utf8')
|
|
269
|
+
const { createHash } = await import('node:crypto')
|
|
270
|
+
const shaAfter = createHash('sha256').update('after\n').digest('hex')
|
|
271
|
+
const committed = commitLedgerEntry(cwd, project, entry, shaAfter)
|
|
272
|
+
assert.equal(committed.status, 'applied')
|
|
273
|
+
assert.equal(committed.afterSha, shaAfter)
|
|
274
|
+
// 台账里有这条
|
|
275
|
+
const entries = readLedger(cwd, project)
|
|
276
|
+
assert.equal(entries.length, 1)
|
|
277
|
+
assert.equal(entries[0].path, 'src/a.ts')
|
|
278
|
+
assert.equal(entries[0].status, 'applied')
|
|
279
|
+
})
|
|
280
|
+
})
|
package/tests/paths.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 测试要验的是「这个包本身的代码」。
|
|
3
|
+
*
|
|
4
|
+
* 在开发工作区里,被验的对象可能是 node_modules 里的已装副本;但这个包
|
|
5
|
+
* 自带 tests/ 目录,用户下载解压后要能直接 `node tests/run-all.mjs` 验自己。
|
|
6
|
+
*
|
|
7
|
+
* 所以默认值不是硬编码的安装路径,而是「上一级目录」——即包根。
|
|
8
|
+
* 想验别的副本仍然可以用 PF_LIVE_DIR 点名。
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync } from 'node:fs'
|
|
11
|
+
import { join, dirname } from 'node:path'
|
|
12
|
+
import { fileURLToPath } from 'node:url'
|
|
13
|
+
|
|
14
|
+
export const HERE = dirname(fileURLToPath(import.meta.url))
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 被验的插件目录。
|
|
18
|
+
*
|
|
19
|
+
* 优先级:
|
|
20
|
+
* 1. PF_LIVE_DIR 环境变量(指到别处,比如已装的副本)
|
|
21
|
+
* 2. tests/ 的上一级 —— 也就是这个包自己
|
|
22
|
+
* 3. 本机默认安装位置(万一有人把 tests 单独拷出去跑)
|
|
23
|
+
*/
|
|
24
|
+
export const LIVE_DIR = (() => {
|
|
25
|
+
const explicit = process.env.PF_LIVE_DIR
|
|
26
|
+
if (explicit !== undefined && explicit.trim() !== '') return explicit.trim()
|
|
27
|
+
const parent = dirname(HERE)
|
|
28
|
+
// 包根里应该有 index.js;有就认它
|
|
29
|
+
if (existsSync(join(parent, 'index.js'))) return parent
|
|
30
|
+
return 'C:/Users/ASUS/.dsh/profiles/desktop/node_modules/dsh-project-flow'
|
|
31
|
+
})()
|
|
32
|
+
|
|
33
|
+
/** 临时产物目录。放跑测试时的中间文件,不放被验的对象。 */
|
|
34
|
+
export const TRIAL = join(HERE, '_trial')
|
|
35
|
+
|
|
36
|
+
/** 被验的 index.js 绝对路径。 */
|
|
37
|
+
export function targetIndex() {
|
|
38
|
+
const explicit = process.env.PF_TARGET
|
|
39
|
+
if (explicit !== undefined && explicit.trim() !== '') return explicit
|
|
40
|
+
return join(LIVE_DIR, 'index.js')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 被验的 index.js 所在目录,同目录的模块也从这里找。 */
|
|
44
|
+
export function targetDir() {
|
|
45
|
+
return dirname(targetIndex())
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 同名模块在「被验目录」里的路径。 */
|
|
49
|
+
export function sibling(name) {
|
|
50
|
+
return join(targetDir(), name)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 找不到就直接报清楚,别让后面的断言跑在不存在的对象上。 */
|
|
54
|
+
export function requireTarget() {
|
|
55
|
+
const path = targetIndex()
|
|
56
|
+
if (!existsSync(path)) {
|
|
57
|
+
console.error(`找不到被验的 index.js:${path}`)
|
|
58
|
+
console.error('用 PF_LIVE_DIR 指定插件目录,或先确认包解压完整。')
|
|
59
|
+
process.exit(1)
|
|
60
|
+
}
|
|
61
|
+
return path
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 跑测试之前先确认依赖装好了。
|
|
66
|
+
*
|
|
67
|
+
* 解压一个 tgz 不等于装好了:`index.js` 会 import `@deepseek-ai/schemastery`,
|
|
68
|
+
* 那是 peer 依赖,由宿主(DSH)或 `npm i` 提供。直接解压后跑测试会得到
|
|
69
|
+
* 九套 ERR_MODULE_NOT_FOUND,报错长得像代码坏了——实际只是没装。
|
|
70
|
+
*
|
|
71
|
+
* 这里提前检查,给一句能照着做的话。
|
|
72
|
+
*/
|
|
73
|
+
export function checkDependencies() {
|
|
74
|
+
const markers = [
|
|
75
|
+
join(LIVE_DIR, 'node_modules', '@deepseek-ai'),
|
|
76
|
+
join(dirname(LIVE_DIR), '@deepseek-ai'),
|
|
77
|
+
// pnpm 的布局:.pnpm 下面放着真实包
|
|
78
|
+
join(dirname(LIVE_DIR), '.pnpm'),
|
|
79
|
+
]
|
|
80
|
+
if (markers.some(marker => existsSync(marker))) return true
|
|
81
|
+
|
|
82
|
+
console.error(''
|
|
83
|
+
+ '跑测试前缺依赖:这个目录里找不到 @deepseek-ai/schemastery。\n'
|
|
84
|
+
+ `被验目录:${LIVE_DIR}\n`
|
|
85
|
+
+ '\n'
|
|
86
|
+
+ '三种办法,挑一种:\n'
|
|
87
|
+
+ ' 1. 装进 DSH 的 profile,然后指过去验:\n'
|
|
88
|
+
+ ' PF_LIVE_DIR=<profile>/node_modules/dsh-project-flow node tests/run-all.mjs\n'
|
|
89
|
+
+ ' 2. 就地装依赖:\n'
|
|
90
|
+
+ ' npm i\n'
|
|
91
|
+
+ ' 3. 只想确认包完整、不想装东西:\n'
|
|
92
|
+
+ ' node -e "require(\'fs\').readdirSync(\'.\').length" # 或者直接看文件在不在\n')
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 质检读预览图的路径,必须和提示词让子会话写入的路径是同一个。
|
|
3
|
+
*
|
|
4
|
+
* 真踩过:提示词说「导出到 preview/」,子会话照做,40 张图躺在程序目录下;
|
|
5
|
+
* 而 previewImageParts 读的是 .project-flow/<项目>/ —— 那里只有 snap.git。
|
|
6
|
+
* 结果质检每次都判「没有预览图」→ FAIL → 返工,白烧好几轮。
|
|
7
|
+
*/
|
|
8
|
+
import { test, before, after } from 'node:test'
|
|
9
|
+
import assert from 'node:assert/strict'
|
|
10
|
+
import { mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
import { tmpdir } from 'node:os'
|
|
13
|
+
import { pathToFileURL } from 'node:url'
|
|
14
|
+
|
|
15
|
+
process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-preview-home-'))
|
|
16
|
+
const LIVE = 'C:/Users/ASUS/.dsh/profiles/desktop/node_modules/dsh-project-flow/index.js'
|
|
17
|
+
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
18
|
+
|
|
19
|
+
// 读图只看扩展名,不校验内容,所以给个合法 PNG 头就够了
|
|
20
|
+
const PNG = Buffer.from(
|
|
21
|
+
'89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000a49444154789c6300010000050001' +
|
|
22
|
+
'0d0a2db40000000049454e44ae426082',
|
|
23
|
+
'hex',
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
let cwd
|
|
27
|
+
|
|
28
|
+
before(async () => {
|
|
29
|
+
cwd = await mkdtemp(join(tmpdir(), 'pf-preview-'))
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
after(async () => {
|
|
33
|
+
await rm(cwd, { recursive: true, force: true }).catch(() => {})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('★ 读的是 cwd/preview —— 提示词让子会话写的地方', async () => {
|
|
37
|
+
await mkdir(join(cwd, 'preview'), { recursive: true })
|
|
38
|
+
await writeFile(join(cwd, 'preview', 'p01.png'), PNG)
|
|
39
|
+
await writeFile(join(cwd, 'preview', 'p02.png'), PNG)
|
|
40
|
+
// 状态目录也造一个空壳,证明不会误读那边
|
|
41
|
+
await mkdir(join(cwd, '.project-flow', 'demo'), { recursive: true })
|
|
42
|
+
|
|
43
|
+
const got = await mod.previewImageParts(cwd, 'demo')
|
|
44
|
+
assert.equal(got.parts.length, 2, `应该读到 2 张,实际:${got.note}`)
|
|
45
|
+
assert.ok(got.note.includes('喂了 2 张'), got.note)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('两种位置都认:图放在状态目录下也能读到', async () => {
|
|
49
|
+
const other = await mkdtemp(join(tmpdir(), 'pf-preview2-'))
|
|
50
|
+
try {
|
|
51
|
+
await mkdir(join(other, '.project-flow', 'demo', 'preview'), { recursive: true })
|
|
52
|
+
await writeFile(join(other, '.project-flow', 'demo', 'preview', 'p01.png'), PNG)
|
|
53
|
+
const got = await mod.previewImageParts(other, 'demo')
|
|
54
|
+
assert.equal(got.parts.length, 1, got.note)
|
|
55
|
+
} finally {
|
|
56
|
+
await rm(other, { recursive: true, force: true }).catch(() => {})
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('真没有图时如实说,不编', async () => {
|
|
61
|
+
const empty = await mkdtemp(join(tmpdir(), 'pf-preview3-'))
|
|
62
|
+
try {
|
|
63
|
+
const got = await mod.previewImageParts(empty, 'demo')
|
|
64
|
+
assert.equal(got.parts.length, 0)
|
|
65
|
+
assert.equal(got.note, '没有预览图')
|
|
66
|
+
} finally {
|
|
67
|
+
await rm(empty, { recursive: true, force: true }).catch(() => {})
|
|
68
|
+
}
|
|
69
|
+
})
|