dsh-project-flow 0.9.3 → 0.10.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 +197 -12
- package/README.md +25 -0
- package/checks/assertion.mjs +104 -0
- package/checks/command.mjs +416 -0
- package/checks/requirement.mjs +91 -0
- package/commands/slash.mjs +754 -0
- package/commands/steer.mjs +43 -0
- package/config/settings.mjs +217 -0
- package/docs/FEATURES.md +9 -6
- package/docs/anchors/file-whitelist.md +1 -1
- package/http/routes.mjs +252 -0
- package/index.js +173 -8612
- package/issues/issue.mjs +322 -0
- package/package.json +16 -1
- package/persistence/lock.mjs +67 -0
- package/persistence/log.mjs +43 -0
- package/persistence/recovery.mjs +118 -0
- package/persistence/state.mjs +202 -0
- package/pricing/estimate.mjs +72 -0
- package/prompts/rules.mjs +192 -0
- package/rollback/ledger.mjs +237 -0
- package/runner/pipeline.mjs +1190 -0
- package/runner/stage.mjs +370 -0
- package/runner/tools/context.mjs +64 -0
- package/runner/tools/decorate-cost.mjs +49 -0
- package/runner/tools/doctor.mjs +59 -0
- package/runner/tools/estimate.mjs +63 -0
- package/runner/tools/execute-run.mjs +255 -0
- package/runner/tools/models.mjs +55 -0
- package/runner/tools/resume.mjs +136 -0
- package/runner/tools/rollback.mjs +291 -0
- package/runner/tools/run.mjs +262 -0
- package/runner/tools/set.mjs +65 -0
- package/runner/tools/snapshots.mjs +83 -0
- package/runner/tools/status.mjs +70 -0
- package/runner/tools.mjs +94 -0
- package/shared/constants.mjs +273 -0
- package/shared/tool.mjs +32 -0
- package/shared/util.mjs +231 -0
- package/snapshot/repo.mjs +452 -0
- package/state/registry.mjs +40 -0
- package/tests/aesthetic.test.mjs +20 -12
- package/tests/apply-wiring.test.mjs +172 -0
- package/tests/assertions-e2e.mjs +2 -1
- package/tests/atomic-write.test.mjs +129 -0
- package/tests/client-bundle.test.mjs +3 -3
- package/tests/command-whitelist.test.mjs +135 -0
- package/tests/contract.test.mjs +2 -2
- package/tests/desktop.test.mjs +2 -2
- package/tests/domains.test.mjs +10 -7
- package/tests/e2e.mjs +2 -1
- package/tests/extra-phase.test.mjs +2 -2
- package/tests/feedback.test.mjs +2 -1
- package/tests/flow-gate.test.mjs +16 -56
- package/tests/issues-e2e.mjs +2 -1
- package/tests/panel-independence.test.mjs +2 -2
- package/tests/panel-report.test.mjs +2 -2
- package/tests/paths.mjs +70 -1
- package/tests/preview-path.test.mjs +2 -1
- package/tests/qa-assertions.test.mjs +10 -3
- package/tests/run-all.mjs +5 -0
- package/tests/snapshot-core.test.mjs +234 -0
- package/tests/stage-model.test.mjs +2 -2
- package/tests/stop-resume.test.mjs +7 -2
- package/tests/watch-root.test.mjs +2 -1
- package/ui/prompt.mjs +101 -0
- package/workspace/models.mjs +111 -0
- package/workspace/panel.mjs +359 -0
- package/workspace/paths.mjs +393 -0
- package/workspace/resume.mjs +107 -0
|
@@ -13,12 +13,12 @@ import assert from 'node:assert/strict'
|
|
|
13
13
|
import { join } from 'node:path'
|
|
14
14
|
import { readFile } from 'node:fs/promises'
|
|
15
15
|
import { pathToFileURL } from 'node:url'
|
|
16
|
-
import { LIVE_DIR } from './paths.mjs'
|
|
16
|
+
import { LIVE_DIR, sourceText } from './paths.mjs'
|
|
17
17
|
|
|
18
18
|
const mod = await import(pathToFileURL(join(LIVE_DIR, 'flow.schema.mjs')).href + `?t=${Date.now()}`)
|
|
19
19
|
const { loadFlow, BUILTIN_FLOW, describeFlowProblems } = mod
|
|
20
20
|
|
|
21
|
-
const source =
|
|
21
|
+
const source = sourceText()
|
|
22
22
|
|
|
23
23
|
const WITH_SECURITY = `process: { name: with-security, version: 1 }
|
|
24
24
|
phases:
|
package/tests/feedback.test.mjs
CHANGED
|
@@ -12,9 +12,10 @@ import { existsSync } from 'node:fs'
|
|
|
12
12
|
import { join, dirname } from 'node:path'
|
|
13
13
|
import { tmpdir } from 'node:os'
|
|
14
14
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
15
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
15
16
|
|
|
16
17
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
17
|
-
const LIVE = '
|
|
18
|
+
const LIVE = join(LIVE_DIR, 'index.js')
|
|
18
19
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
19
20
|
|
|
20
21
|
const results = []
|
package/tests/flow-gate.test.mjs
CHANGED
|
@@ -6,63 +6,24 @@
|
|
|
6
6
|
* 1. 该放的放不进去(工作流里开不了工)→ 这道门变成墙
|
|
7
7
|
* 2. 不该放的地方放进去(普通目录也能跑)→ 这道门形同虚设
|
|
8
8
|
*
|
|
9
|
-
*
|
|
9
|
+
* 以前这个套件从落盘的 index.js 里用正则切函数出来跑。拆成模块之后
|
|
10
|
+
* 那些函数住在 workspace/paths.mjs 里,切成文本片段反而更脆:
|
|
11
|
+
* 正则一改、函数一挪就报「找不到」。现在直接从模块 import 真身。
|
|
10
12
|
*/
|
|
11
|
-
import {
|
|
12
|
-
import { join
|
|
13
|
+
import { mkdtemp, stat } from 'node:fs/promises'
|
|
14
|
+
import { join } from 'node:path'
|
|
13
15
|
import { tmpdir } from 'node:os'
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return out.join('\n')
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function findLine(re) {
|
|
30
|
-
const i = lines.findIndex(line => re.test(line))
|
|
31
|
-
if (i === -1) throw new Error(`找不到 ${re}`)
|
|
32
|
-
return i
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const mod = [
|
|
36
|
-
"import { join, isAbsolute, resolve, relative, sep } from 'node:path'",
|
|
37
|
-
"import { mkdir } from 'node:fs/promises'",
|
|
38
|
-
"import { existsSync } from 'node:fs'",
|
|
39
|
-
lines[findLine(/^const FLOW_DIR_NAME = /u)],
|
|
40
|
-
lines[findLine(/^const FLOW_GATE_MODES = /u)],
|
|
41
|
-
// flowRootOf 现在依赖这两个:一个判「是不是系统盘」,一个挑「该挪到哪个盘」。
|
|
42
|
-
// 不切进来就是 ReferenceError。
|
|
43
|
-
sliceTopLevel(findLine(/^(?:export )?function onSystemDrive\(/u)),
|
|
44
|
-
sliceTopLevel(findLine(/^(?:export )?function pickDataDrive\(/u)),
|
|
45
|
-
sliceTopLevel(findLine(/^(?:export )?function flowModeOf\(/u)),
|
|
46
|
-
sliceTopLevel(findLine(/^(?:export )?function flowRootOf\(/u)),
|
|
47
|
-
// flowGateMessage 现在依赖这两个:一个判断「配置里写死了没」,
|
|
48
|
-
// 一个算出「真正该去的那一层」。不切进来就是 ReferenceError。
|
|
49
|
-
sliceTopLevel(findLine(/^(?:export )?function flowRootConfigured\(/u)),
|
|
50
|
-
sliceTopLevel(findLine(/^(?:export )?function flowRootTargetOf\(/u)),
|
|
51
|
-
sliceTopLevel(findLine(/^(?:export )?function insideFlow\(/u)),
|
|
52
|
-
sliceTopLevel(findLine(/^(?:export )?function flowGateMessage\(/u)),
|
|
53
|
-
sliceTopLevel(findLine(/^(?:export )?async function ensureFlowRoot\(/u)),
|
|
54
|
-
'export { FLOW_DIR_NAME, FLOW_GATE_MODES, flowModeOf, flowRootOf, flowRootConfigured, flowRootTargetOf, insideFlow, flowGateMessage, ensureFlowRoot }',
|
|
55
|
-
].join('\n\n')
|
|
56
|
-
// 切出来的函数有些带 `export` 前缀(它们要导出给别的测试用),再统一导出
|
|
57
|
-
// 一次就会撞成 Duplicate export。先剥掉函数上的那种,`export {` 那行留着当地唯一出口。
|
|
58
|
-
.replace(/^export (?!\{)/gmu, '')
|
|
59
|
-
|
|
60
|
-
const tmp = join(TRIAL, '_gate_extracted.mjs')
|
|
61
|
-
// 这个目录在一次干净的包里还不存在(开发工作区里是跑别的套件时建出来的)。
|
|
62
|
-
// 依赖「上一个测试顺手建了它」是最容易在别人机器上崩的一类写法。
|
|
63
|
-
await mkdir(TRIAL, { recursive: true })
|
|
64
|
-
await writeFile(tmp, mod, 'utf8')
|
|
65
|
-
const { insideFlow, flowRootOf, flowGateMessage, flowModeOf, ensureFlowRoot, FLOW_DIR_NAME } = await import(pathToFileURL(tmp).href)
|
|
16
|
+
import { TRIAL } from './paths.mjs'
|
|
17
|
+
import { FLOW_DIR_NAME } from '../shared/constants.mjs'
|
|
18
|
+
import {
|
|
19
|
+
insideFlow,
|
|
20
|
+
flowRootOf,
|
|
21
|
+
flowGateMessage,
|
|
22
|
+
flowModeOf,
|
|
23
|
+
ensureFlowRoot,
|
|
24
|
+
} from '../workspace/paths.mjs'
|
|
25
|
+
|
|
26
|
+
void TRIAL
|
|
66
27
|
|
|
67
28
|
const results = []
|
|
68
29
|
const pending = []
|
|
@@ -228,5 +189,4 @@ for (const r of results) {
|
|
|
228
189
|
if (!r.ok) failed += 1
|
|
229
190
|
}
|
|
230
191
|
console.log(`\n${results.length - failed}/${results.length} 通过`)
|
|
231
|
-
await unlink(tmp).catch(() => {})
|
|
232
192
|
process.exit(failed === 0 ? 0 : 1)
|
package/tests/issues-e2e.mjs
CHANGED
|
@@ -9,9 +9,10 @@ import { existsSync, readFileSync } from 'node:fs'
|
|
|
9
9
|
import { join, dirname } from 'node:path'
|
|
10
10
|
import { tmpdir } from 'node:os'
|
|
11
11
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
12
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
12
13
|
|
|
13
14
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
14
|
-
const LIVE = '
|
|
15
|
+
const LIVE = join(LIVE_DIR, 'index.js')
|
|
15
16
|
process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-issues-home-'))
|
|
16
17
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
17
18
|
const { parseIssueBlock } = mod
|
|
@@ -15,9 +15,9 @@ import { test } from 'node:test'
|
|
|
15
15
|
import assert from 'node:assert/strict'
|
|
16
16
|
import { join } from 'node:path'
|
|
17
17
|
import { readFile } from 'node:fs/promises'
|
|
18
|
-
import { LIVE_DIR } from './paths.mjs'
|
|
18
|
+
import { LIVE_DIR, sourceText } from './paths.mjs'
|
|
19
19
|
|
|
20
|
-
const host =
|
|
20
|
+
const host = sourceText()
|
|
21
21
|
const client = await readFile(join(LIVE_DIR, 'client.js'), 'utf8')
|
|
22
22
|
|
|
23
23
|
test('target 的四档:自己的在跑的 → 自己碰过的 → 当前目录 → 归属未知的老项目', () => {
|
|
@@ -12,12 +12,12 @@ import assert from 'node:assert/strict'
|
|
|
12
12
|
import { join } from 'node:path'
|
|
13
13
|
import { readFile } from 'node:fs/promises'
|
|
14
14
|
import { pathToFileURL } from 'node:url'
|
|
15
|
-
import { LIVE_DIR } from './paths.mjs'
|
|
15
|
+
import { LIVE_DIR, sourceText } from './paths.mjs'
|
|
16
16
|
|
|
17
17
|
const mod = await import(pathToFileURL(join(LIVE_DIR, 'index.js')).href + `?t=${Date.now()}`)
|
|
18
18
|
const { parseAssertionBlock, parseIssueBlock } = mod
|
|
19
19
|
|
|
20
|
-
const source =
|
|
20
|
+
const source = sourceText()
|
|
21
21
|
|
|
22
22
|
const REPORT = `---
|
|
23
23
|
verdict: "FAIL(断言未过 1 条)"
|
package/tests/paths.mjs
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* 所以默认值不是硬编码的安装路径,而是「上一级目录」——即包根。
|
|
8
8
|
* 想验别的副本仍然可以用 PF_LIVE_DIR 点名。
|
|
9
9
|
*/
|
|
10
|
-
import { existsSync } from 'node:fs'
|
|
10
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
11
11
|
import { join, dirname } from 'node:path'
|
|
12
12
|
import { tmpdir } from 'node:os'
|
|
13
13
|
import { fileURLToPath } from 'node:url'
|
|
@@ -31,6 +31,75 @@ export const LIVE_DIR = (() => {
|
|
|
31
31
|
return 'C:/Users/ASUS/.dsh/profiles/desktop/node_modules/dsh-project-flow'
|
|
32
32
|
})()
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* 插件源码的全部分子,拼成一段。
|
|
36
|
+
*
|
|
37
|
+
* 有几种测试验的不是「函数怎么算」,而是「代码里确实写了这行」——
|
|
38
|
+
* 比如「project_run 要收 forbidden 参数」「实施段要贴契约」。
|
|
39
|
+
* 以前这些断言直接扫 index.js。
|
|
40
|
+
*
|
|
41
|
+
* 拆分之后这些代码分散到 runner/tools.mjs、runner/pipeline.mjs 等文件里,
|
|
42
|
+
* 只扫 index.js 必然找不到。所以改成扫整棵树:断言想验的是「这件事在代码里
|
|
43
|
+
* 做了」,至于做在哪个文件里,不该是断言的一部分。
|
|
44
|
+
*
|
|
45
|
+
* 只收 .mjs / .js,排除 tests、_tools、node_modules、bench——那些不是产品代码。
|
|
46
|
+
*/
|
|
47
|
+
export function sourceText() {
|
|
48
|
+
return Object.values(sourceFiles()).join('\n')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 按文件分开的源码:文件名 → 内容。
|
|
53
|
+
*
|
|
54
|
+
* 有些断言验的是「A 出现在 B 之前」——比如「审美判定在函数级、写在那个
|
|
55
|
+
* if 分支之前」。这种顺序在同一个文件里是有意义的;把所有文件拼成一段
|
|
56
|
+
* 之后,文件之间的先后就成了拼接顺序,断言测到的是拼接器怎么写,
|
|
57
|
+
* 不是代码怎么写。
|
|
58
|
+
*
|
|
59
|
+
* 所以位置类断言要按文件分组跑:在哪个文件里成立就算成立。
|
|
60
|
+
*/
|
|
61
|
+
export function sourceFiles() {
|
|
62
|
+
const skip = new Set(['tests', '_tools', 'node_modules', 'bench', 'docs', 'assets', 'locale', '_trial'])
|
|
63
|
+
const out = {}
|
|
64
|
+
const walk = dir => {
|
|
65
|
+
let items
|
|
66
|
+
try {
|
|
67
|
+
items = readdirSync(dir, { withFileTypes: true })
|
|
68
|
+
} catch {
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
for (const item of items) {
|
|
72
|
+
if (item.name.startsWith('.')) continue
|
|
73
|
+
const full = join(dir, item.name)
|
|
74
|
+
if (item.isDirectory()) {
|
|
75
|
+
if (skip.has(item.name)) continue
|
|
76
|
+
walk(full)
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
if (!/\.(mjs|js)$/u.test(item.name)) continue
|
|
80
|
+
// client.js 是浏览器半边,和 Host 逻辑不是一回事
|
|
81
|
+
if (item.name === 'client.js') continue
|
|
82
|
+
out[full.slice(LIVE_DIR.length + 1).replace(/\\/gu, '/')] = readFileSync(full, 'utf8')
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
walk(LIVE_DIR)
|
|
86
|
+
return out
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 位置类断言:在某个文件里「a 在 b 之前」就算成立。
|
|
91
|
+
*
|
|
92
|
+
* 三个都不存在也返回 false——那说明这行代码没了,不是顺序问题。
|
|
93
|
+
*/
|
|
94
|
+
export function orderHolds(files, before, after) {
|
|
95
|
+
for (const text of Object.values(files)) {
|
|
96
|
+
const a = text.indexOf(before)
|
|
97
|
+
const b = text.indexOf(after)
|
|
98
|
+
if (a !== -1 && b !== -1 && a < b) return true
|
|
99
|
+
}
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
34
103
|
/**
|
|
35
104
|
* 临时产物目录。放跑测试时的中间文件,不放被验的对象。
|
|
36
105
|
*
|
|
@@ -11,9 +11,10 @@ import { mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
|
|
|
11
11
|
import { join } from 'node:path'
|
|
12
12
|
import { tmpdir } from 'node:os'
|
|
13
13
|
import { pathToFileURL } from 'node:url'
|
|
14
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
14
15
|
|
|
15
16
|
process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-preview-home-'))
|
|
16
|
-
const LIVE = '
|
|
17
|
+
const LIVE = join(LIVE_DIR, 'index.js')
|
|
17
18
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
18
19
|
|
|
19
20
|
// 读图只看扩展名,不校验内容,所以给个合法 PNG 头就够了
|
|
@@ -127,9 +127,16 @@ test('白名单:放行的命令连参数一起交出来,命令名去路径
|
|
|
127
127
|
assert.equal(ok.denied, false)
|
|
128
128
|
assert.equal(ok.name, 'node')
|
|
129
129
|
assert.deepEqual(ok.args, ['-e', 'process.exit(0)'])
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
// 带路径的写法不再只看名字:PATH 之外的 C:\tools\node.exe 名字也叫 node,
|
|
131
|
+
// 但它不是这台机器真正那份 node,放行等于任何同名二进制都能跑。
|
|
132
|
+
const outside = checkDenial('C:\\tools\\node.exe -v', allowed)
|
|
133
|
+
assert.equal(outside.denied, true)
|
|
134
|
+
assert.match(outside.reason, /路径/u)
|
|
135
|
+
// 真实的那份 node(process.execPath)必须照旧放行:本机 node 不在 PATH 里,
|
|
136
|
+
// Requirement 的判据全靠它跑。
|
|
137
|
+
const inside = checkDenial(`${JSON.stringify(process.execPath)} -v`, allowed)
|
|
138
|
+
assert.equal(inside.denied, false)
|
|
139
|
+
assert.equal(inside.name, 'node')
|
|
133
140
|
})
|
|
134
141
|
|
|
135
142
|
test('切命令:引号里的空格不拆,引号本身不留在参数里', () => {
|
package/tests/run-all.mjs
CHANGED
|
@@ -24,6 +24,7 @@ const SUITES = [
|
|
|
24
24
|
['守护器单测', 'watchdog.test.mjs'],
|
|
25
25
|
['守护器心跳回归', '_heartbeat-regression.test.mjs'],
|
|
26
26
|
['token 计数单测', 'watchdog-tokens.test.mjs'],
|
|
27
|
+
['装配冒烟:apply 到底挂上了什么', 'apply-wiring.test.mjs'],
|
|
27
28
|
['停止与继续', 'stop-resume.test.mjs'],
|
|
28
29
|
['命令把话送到模型', 'steer-command.test.mjs'],
|
|
29
30
|
['工作流目录门控', 'flow-gate.test.mjs'],
|
|
@@ -40,6 +41,10 @@ const SUITES = [
|
|
|
40
41
|
['问题闭环', 'issue-loop.test.mjs'],
|
|
41
42
|
['问题闭环端到端', 'issues-e2e.mjs'],
|
|
42
43
|
['补丁台账', 'patch-ledger.test.mjs'],
|
|
44
|
+
['原子写', 'atomic-write.test.mjs'],
|
|
45
|
+
['命令白名单', 'command-whitelist.test.mjs'],
|
|
46
|
+
// 快照核心要真起 git、打十几张快照,比别的单测慢,放在单测段末尾
|
|
47
|
+
['快照核心', 'snapshot-core.test.mjs'],
|
|
43
48
|
['声明式流程', 'flow-schema.test.mjs'],
|
|
44
49
|
['中间阶段', 'extra-phase.test.mjs'],
|
|
45
50
|
['阶段级模型', 'stage-model.test.mjs'],
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 快照核心:diff 要看得见「从没被 add 过的新文件」,回滚要删得掉它们。
|
|
3
|
+
*
|
|
4
|
+
* 这一套盯的是 0.10.1 修的那个根:`git diff <commit>` 只比较提交和索引,
|
|
5
|
+
* 实施段新建、又没被 add 的文件两边都没有——于是 diff 里不出现、回滚时
|
|
6
|
+
* 也不会被删,用户看到的现象是「回滚了,新文件还在」。八类场景各自独立,
|
|
7
|
+
* 失败时能直接看出坏在哪一步。
|
|
8
|
+
*/
|
|
9
|
+
import { test } from 'node:test'
|
|
10
|
+
import assert from 'node:assert/strict'
|
|
11
|
+
import { mkdtemp, mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
import { tmpdir } from 'node:os'
|
|
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)
|
|
18
|
+
const {
|
|
19
|
+
takeSnapshot,
|
|
20
|
+
snapshotDiff,
|
|
21
|
+
restoreAll,
|
|
22
|
+
restorePrecise,
|
|
23
|
+
attemptFiles,
|
|
24
|
+
pruneSnapshots,
|
|
25
|
+
listSnapshots,
|
|
26
|
+
gitAvailable,
|
|
27
|
+
} = mod
|
|
28
|
+
|
|
29
|
+
const PROJECT = 'demo'
|
|
30
|
+
|
|
31
|
+
const ready = (await gitAvailable()) === true
|
|
32
|
+
if (!ready) {
|
|
33
|
+
// 不能静默 exit 0:那会让「没装 git 的机器」看起来像全过。
|
|
34
|
+
console.log('⚠️ 跳过快照核心:这台机器上没有可用的 git,快照与回滚不可用')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** 平台不支持的用例统一写成 skip,而不是删掉。 */
|
|
38
|
+
const needGit = { skip: ready ? false : '没有可用的 git' }
|
|
39
|
+
|
|
40
|
+
async function withWorkspace(fn) {
|
|
41
|
+
const dir = await mkdtemp(join(tmpdir(), 'pf-snap-'))
|
|
42
|
+
try {
|
|
43
|
+
return await fn(dir)
|
|
44
|
+
} finally {
|
|
45
|
+
await rm(dir, { recursive: true, force: true })
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const exists = async path => {
|
|
50
|
+
try {
|
|
51
|
+
await stat(path)
|
|
52
|
+
return true
|
|
53
|
+
} catch {
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const read = async path => readFile(path, 'utf8')
|
|
59
|
+
|
|
60
|
+
// ── a 未跟踪文件进 diff ──────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
test('diff:新建的文件(从没 add 过)也在差异里,中文路径是字面不是八进制', needGit, async () => {
|
|
63
|
+
await withWorkspace(async dir => {
|
|
64
|
+
await writeFile(join(dir, 'base.txt'), 'base\n', 'utf8')
|
|
65
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
66
|
+
assert.equal(before.ok, true, before.reason)
|
|
67
|
+
await writeFile(join(dir, 'new.txt'), 'new\n', 'utf8')
|
|
68
|
+
await writeFile(join(dir, '中文.txt'), '内容\n', 'utf8')
|
|
69
|
+
const diff = await snapshotDiff(dir, PROJECT, before.commit)
|
|
70
|
+
const byPath = new Map(diff.map(entry => [entry.path, entry.status]))
|
|
71
|
+
assert.equal(byPath.get('new.txt'), '新增')
|
|
72
|
+
assert.equal(byPath.get('中文.txt'), '新增', `中文路径没字面出现,实际有:${diff.map(e => e.path).join(' / ')}`)
|
|
73
|
+
assert.ok(!diff.some(entry => /\\\d{3}/u.test(entry.path)), '路径里不该出现八进制转义')
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// ── b full 回滚删新增 ────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
test('full 回滚:新增文件被删掉,改过的那份回到快照内容,回滚后无差异', needGit, async () => {
|
|
80
|
+
await withWorkspace(async dir => {
|
|
81
|
+
await writeFile(join(dir, 'changed.txt'), '旧\n', 'utf8')
|
|
82
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
83
|
+
assert.equal(before.ok, true, before.reason)
|
|
84
|
+
await writeFile(join(dir, 'changed.txt'), '新\n', 'utf8')
|
|
85
|
+
await writeFile(join(dir, 'new.txt'), 'added\n', 'utf8')
|
|
86
|
+
|
|
87
|
+
const result = await restoreAll(dir, PROJECT, before.commit)
|
|
88
|
+
assert.equal(result.ok, true, result.reason)
|
|
89
|
+
assert.ok(result.removed >= 1, `removed=${result.removed}`)
|
|
90
|
+
assert.equal(await exists(join(dir, 'new.txt')), false, '新增文件没被删掉')
|
|
91
|
+
assert.equal(await read(join(dir, 'changed.txt')), '旧\n')
|
|
92
|
+
assert.deepEqual(await snapshotDiff(dir, PROJECT, before.commit), [])
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// ── c precise 回滚删新增 ────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
test('precise 回滚:清单里的新增文件被删,清单外的不动', needGit, async () => {
|
|
99
|
+
await withWorkspace(async dir => {
|
|
100
|
+
await writeFile(join(dir, 'changed.txt'), '旧\n', 'utf8')
|
|
101
|
+
await writeFile(join(dir, 'keep.txt'), '原样\n', 'utf8')
|
|
102
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
103
|
+
assert.equal(before.ok, true, before.reason)
|
|
104
|
+
await writeFile(join(dir, 'changed.txt'), '新\n', 'utf8')
|
|
105
|
+
await writeFile(join(dir, 'new.txt'), 'added\n', 'utf8')
|
|
106
|
+
|
|
107
|
+
const scoped = await restorePrecise(dir, PROJECT, before.commit, ['changed.txt', 'new.txt'])
|
|
108
|
+
assert.equal(scoped.ok, true, scoped.reason)
|
|
109
|
+
assert.equal(scoped.removed, 1)
|
|
110
|
+
assert.equal(await exists(join(dir, 'new.txt')), false, '新增文件没被删掉')
|
|
111
|
+
assert.equal(await read(join(dir, 'changed.txt')), '旧\n')
|
|
112
|
+
|
|
113
|
+
// scope 生效:只传 changed.txt 时,new.txt 不在清单里,不许删
|
|
114
|
+
await writeFile(join(dir, 'new.txt'), 'added\n', 'utf8')
|
|
115
|
+
const narrow = await restorePrecise(dir, PROJECT, before.commit, ['changed.txt'])
|
|
116
|
+
assert.equal(narrow.ok, true, narrow.reason)
|
|
117
|
+
assert.equal(await exists(join(dir, 'new.txt')), true, '清单外的文件被误删了')
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('precise 回滚:删除后因此变空的目录会被清掉,非空目录保留', needGit, async () => {
|
|
122
|
+
await withWorkspace(async dir => {
|
|
123
|
+
await writeFile(join(dir, 'base.txt'), 'base\n', 'utf8')
|
|
124
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
125
|
+
assert.equal(before.ok, true, before.reason)
|
|
126
|
+
await mkdir(join(dir, 'made', 'deep'), { recursive: true })
|
|
127
|
+
await writeFile(join(dir, 'made', 'deep', 'a.txt'), 'a\n', 'utf8')
|
|
128
|
+
await writeFile(join(dir, 'made', 'b.txt'), 'b\n', 'utf8')
|
|
129
|
+
|
|
130
|
+
const result = await restorePrecise(dir, PROJECT, before.commit, ['made/b.txt'])
|
|
131
|
+
assert.equal(result.ok, true, result.reason)
|
|
132
|
+
assert.equal(await exists(join(dir, 'made', 'b.txt')), false)
|
|
133
|
+
assert.equal(await exists(join(dir, 'made')), true, '非空目录不该被删')
|
|
134
|
+
assert.equal(await exists(join(dir, 'base.txt')), true)
|
|
135
|
+
})
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
// ── d 模型不写 files 台账仍记录 ─────────────────────────────
|
|
139
|
+
|
|
140
|
+
test('台账:模型没写 files 时退回 diff,写了就以它为准', needGit, async () => {
|
|
141
|
+
await withWorkspace(async dir => {
|
|
142
|
+
await writeFile(join(dir, 'base.txt'), 'base\n', 'utf8')
|
|
143
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
144
|
+
assert.equal(before.ok, true, before.reason)
|
|
145
|
+
await writeFile(join(dir, 'new.txt'), 'new\n', 'utf8')
|
|
146
|
+
|
|
147
|
+
const missing = await attemptFiles(dir, PROJECT, before.commit, undefined)
|
|
148
|
+
assert.ok(missing.includes('new.txt'), `undefined 时没记录到 new.txt:${missing.join(' / ')}`)
|
|
149
|
+
const empty = await attemptFiles(dir, PROJECT, before.commit, [])
|
|
150
|
+
assert.ok(empty.includes('new.txt'), `空数组时没记录到 new.txt:${empty.join(' / ')}`)
|
|
151
|
+
const claimed = await attemptFiles(dir, PROJECT, before.commit, ['only/this.txt'])
|
|
152
|
+
assert.deepEqual(claimed, ['only/this.txt'], '自述非空时不该再算 diff')
|
|
153
|
+
assert.deepEqual(await attemptFiles(dir, PROJECT, '', undefined), [], '没有快照就没有 diff 可退')
|
|
154
|
+
})
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
// ── e 用户手建与模型改动可区分 ──────────────────────────────
|
|
158
|
+
|
|
159
|
+
test('区分:快照后的两次改动,各归各的那一段 diff', needGit, async () => {
|
|
160
|
+
await withWorkspace(async dir => {
|
|
161
|
+
await writeFile(join(dir, 'base.txt'), 'base\n', 'utf8')
|
|
162
|
+
const before = await takeSnapshot(dir, PROJECT, '实施前')
|
|
163
|
+
assert.equal(before.ok, true, before.reason)
|
|
164
|
+
await writeFile(join(dir, 'model.txt'), 'model\n', 'utf8')
|
|
165
|
+
const after = await takeSnapshot(dir, PROJECT, '实施后')
|
|
166
|
+
assert.equal(after.ok, true, after.reason)
|
|
167
|
+
await writeFile(join(dir, 'user.txt'), 'user\n', 'utf8')
|
|
168
|
+
|
|
169
|
+
const fromAfter = (await snapshotDiff(dir, PROJECT, after.commit)).map(entry => entry.path)
|
|
170
|
+
assert.ok(fromAfter.includes('user.txt'), `after 之后没看到 user.txt:${fromAfter.join(' / ')}`)
|
|
171
|
+
assert.ok(!fromAfter.includes('model.txt'), 'model.txt 是快照里的内容,不该算 after 之后的改动')
|
|
172
|
+
|
|
173
|
+
const fromBefore = (await snapshotDiff(dir, PROJECT, before.commit)).map(entry => entry.path)
|
|
174
|
+
assert.ok(fromBefore.includes('model.txt'))
|
|
175
|
+
assert.ok(fromBefore.includes('user.txt'))
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// ── f snapshotExcludes 排除的文件不被误删 ───────────────────
|
|
180
|
+
|
|
181
|
+
test('排除规则:快照忽略的文件既不进 diff,也不会被回滚删掉', needGit, async () => {
|
|
182
|
+
await withWorkspace(async dir => {
|
|
183
|
+
await writeFile(join(dir, 'base.txt'), 'base\n', 'utf8')
|
|
184
|
+
const before = await takeSnapshot(dir, PROJECT, '基线')
|
|
185
|
+
assert.equal(before.ok, true, before.reason)
|
|
186
|
+
await mkdir(join(dir, 'node_modules'), { recursive: true })
|
|
187
|
+
await writeFile(join(dir, 'node_modules', 'keep.txt'), 'keep\n', 'utf8')
|
|
188
|
+
await mkdir(join(dir, '.project-flow', 'x'), { recursive: true })
|
|
189
|
+
await writeFile(join(dir, '.project-flow', 'x', 'y.txt'), 'y\n', 'utf8')
|
|
190
|
+
|
|
191
|
+
const diff = await snapshotDiff(dir, PROJECT, before.commit)
|
|
192
|
+
assert.ok(!diff.some(entry => entry.path.startsWith('node_modules/')), 'node_modules 不该进 diff')
|
|
193
|
+
assert.ok(!diff.some(entry => entry.path.startsWith('.project-flow/')), '.project-flow 不该进 diff')
|
|
194
|
+
|
|
195
|
+
const result = await restoreAll(dir, PROJECT, before.commit)
|
|
196
|
+
assert.equal(result.ok, true, result.reason)
|
|
197
|
+
assert.equal(await exists(join(dir, 'node_modules', 'keep.txt')), true, '被排除的文件被误删了')
|
|
198
|
+
assert.equal(await exists(join(dir, '.project-flow', 'x', 'y.txt')), true, '.project-flow 里的文件被误删了')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// ── g pruneSnapshots 截断数量正确 ───────────────────────────
|
|
203
|
+
|
|
204
|
+
test('截断:留 3 张之后 rev-list 就是 3 条,pruned 数对得上', needGit, async () => {
|
|
205
|
+
await withWorkspace(async dir => {
|
|
206
|
+
for (let i = 0; i < 6; i += 1) {
|
|
207
|
+
await writeFile(join(dir, 'c.txt'), `${i}\n`, 'utf8')
|
|
208
|
+
const snap = await takeSnapshot(dir, PROJECT, `第 ${i + 1} 张`)
|
|
209
|
+
assert.equal(snap.ok, true, snap.reason)
|
|
210
|
+
}
|
|
211
|
+
const before = (await listSnapshots(dir, PROJECT, 20)).length
|
|
212
|
+
assert.equal(before, 6)
|
|
213
|
+
|
|
214
|
+
const pruned = await pruneSnapshots(dir, PROJECT, 3)
|
|
215
|
+
assert.equal(pruned.ok, true, pruned.reason)
|
|
216
|
+
assert.equal(pruned.pruned, 3)
|
|
217
|
+
// rev-list --count HEAD 就是日志条数,这里直接用同一个读数
|
|
218
|
+
assert.equal((await listSnapshots(dir, PROJECT, 20)).length, 3)
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
// ── h snapshotKeep 生效 ─────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
test('保留上限:takeSnapshot 带 keep 时自己就把数量截到该值', needGit, async () => {
|
|
225
|
+
await withWorkspace(async dir => {
|
|
226
|
+
for (let i = 0; i < 5; i += 1) {
|
|
227
|
+
await writeFile(join(dir, 'c.txt'), `${i}\n`, 'utf8')
|
|
228
|
+
const snap = await takeSnapshot(dir, PROJECT, `第 ${i + 1} 张`, { keep: 2 })
|
|
229
|
+
assert.equal(snap.ok, true, snap.reason)
|
|
230
|
+
}
|
|
231
|
+
const list = await listSnapshots(dir, PROJECT, 20)
|
|
232
|
+
assert.equal(list.length, 2)
|
|
233
|
+
})
|
|
234
|
+
})
|
|
@@ -12,12 +12,12 @@ import assert from 'node:assert/strict'
|
|
|
12
12
|
import { join } from 'node:path'
|
|
13
13
|
import { readFile } from 'node:fs/promises'
|
|
14
14
|
import { pathToFileURL } from 'node:url'
|
|
15
|
-
import { LIVE_DIR } from './paths.mjs'
|
|
15
|
+
import { LIVE_DIR, sourceText } from './paths.mjs'
|
|
16
16
|
|
|
17
17
|
const mod = await import(pathToFileURL(join(LIVE_DIR, 'index.js')).href + `?t=${Date.now()}`)
|
|
18
18
|
const { stageModelOf } = mod
|
|
19
19
|
|
|
20
|
-
const source =
|
|
20
|
+
const source = sourceText()
|
|
21
21
|
|
|
22
22
|
const settings = { provider: 'workbuddy', planProvider: '', planModel: '', buildProvider: '', buildModel: '', checkProvider: '', checkModel: '' }
|
|
23
23
|
|
|
@@ -12,9 +12,14 @@ import { existsSync } from 'node:fs'
|
|
|
12
12
|
import { join, dirname } from 'node:path'
|
|
13
13
|
import { tmpdir } from 'node:os'
|
|
14
14
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
15
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
void dirname
|
|
18
|
+
void fileURLToPath
|
|
19
|
+
// 以前这里把安装路径写死了。别人下载这个包、或在开发目录里跑,都会
|
|
20
|
+
// 悄悄验到「已经装好的旧副本」——看起来全过,其实验的不是手上这份代码。
|
|
21
|
+
// 走 paths.mjs 就跟着 PF_LIVE_DIR / 包根走。
|
|
22
|
+
const LIVE = join(LIVE_DIR, 'index.js')
|
|
18
23
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
19
24
|
|
|
20
25
|
const results = []
|
|
@@ -14,9 +14,10 @@ import { mkdir, writeFile, rm, mkdtemp } from 'node:fs/promises'
|
|
|
14
14
|
import { join, dirname } from 'node:path'
|
|
15
15
|
import { tmpdir } from 'node:os'
|
|
16
16
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
17
|
+
import { LIVE_DIR } from './paths.mjs'
|
|
17
18
|
|
|
18
19
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
19
|
-
const LIVE = '
|
|
20
|
+
const LIVE = join(LIVE_DIR, 'index.js')
|
|
20
21
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|
|
21
22
|
|
|
22
23
|
const SETTINGS = {
|