sillyspec 3.20.7 → 3.21.0

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 (54) hide show
  1. package/package.json +1 -1
  2. package/src/change-list.js +51 -16
  3. package/src/contract-matrix.js +67 -0
  4. package/src/stages/execute.js +37 -18
  5. package/src/stages/plan-postcheck.js +234 -0
  6. package/src/stages/plan.js +13 -0
  7. package/docs/brainstorm-plan-contract.md +0 -64
  8. package/docs/plan-execute-contract.md +0 -123
  9. package/docs/platform-scan-protocol.md +0 -298
  10. package/docs/revision-mode.md +0 -115
  11. package/docs/sillyspec/file-lifecycle/known-implementation-gaps.md +0 -99
  12. package/docs/sillyspec/file-lifecycle/platform-workflows-sync.md +0 -223
  13. package/docs/sillyspec/file-lifecycle/stage-artifacts.md +0 -167
  14. package/docs/sillyspec/file-lifecycle/storage-and-state.md +0 -148
  15. package/docs/sillyspec/file-lifecycle/worktree-and-guard.md +0 -211
  16. package/docs/sillyspec/file-lifecycle.md +0 -143
  17. package/docs/workflow-contract-regression.md +0 -106
  18. package/docs/worktree-isolation.md +0 -252
  19. package/test/brainstorm-plan-contract.test.mjs +0 -273
  20. package/test/check-syntax.mjs +0 -26
  21. package/test/cli-top-level-aliases.test.mjs +0 -174
  22. package/test/contract-artifacts.test.mjs +0 -323
  23. package/test/decision-ref-version.mjs +0 -85
  24. package/test/decision-supersede.test.mjs +0 -277
  25. package/test/knowledge-match.test.mjs +0 -231
  26. package/test/plan-execute-contract.test.mjs +0 -357
  27. package/test/plan-optimization.test.mjs +0 -572
  28. package/test/platform-artifacts.test.mjs +0 -190
  29. package/test/platform-failure-samples.test.mjs +0 -199
  30. package/test/platform-recovery-chain.test.mjs +0 -179
  31. package/test/platform-recovery.test.mjs +0 -167
  32. package/test/platform-scan-p0.test.mjs +0 -186
  33. package/test/quick-recommend.test.mjs +0 -146
  34. package/test/revision-v1.test.mjs +0 -1145
  35. package/test/run-sanitize-project-name.test.mjs +0 -51
  36. package/test/run-scan-postcheck-fail.test.mjs +0 -64
  37. package/test/run-scan-project-parse.test.mjs +0 -200
  38. package/test/run-tests.mjs +0 -48
  39. package/test/runtime-cleanup-keeps-worktree.test.mjs +0 -107
  40. package/test/scan-docs-yaml-placeholders.test.mjs +0 -84
  41. package/test/scan-knowledge.test.mjs +0 -175
  42. package/test/scan-paths.test.mjs +0 -68
  43. package/test/scan-postcheck-project-priority.test.mjs +0 -85
  44. package/test/scan-postcheck.test.mjs +0 -197
  45. package/test/scan-workflow-anyfailed-block.test.mjs +0 -52
  46. package/test/spec-dir.test.mjs +0 -206
  47. package/test/stage-contract-failed-post-check.test.mjs +0 -102
  48. package/test/stage-contract.test.mjs +0 -301
  49. package/test/stage-definitions.test.mjs +0 -39
  50. package/test/wait-gates.test.mjs +0 -501
  51. package/test/workflow-spec-base.test.mjs +0 -142
  52. package/test/worktree-deps-provision.test.mjs +0 -148
  53. package/test/worktree-guard.test.mjs +0 -136
  54. package/test/worktree-native-overlay.test.mjs +0 -188
@@ -1,148 +0,0 @@
1
- /**
2
- * worktree-deps-provision 测试
3
- * 覆盖 FR-01/02/03/05:lockfileHash + provisionDeps(junction 快路径 / install 兜底判定 / generic→n/a)
4
- * change 2026-06-28-worktree-deps-provision
5
- */
6
- import { provisionDeps, lockfileHash } from '../src/worktree-deps.js'
7
- import { mkdtempSync, writeFileSync, mkdirSync, existsSync, rmSync } from 'fs'
8
- import { join } from 'path'
9
- import { tmpdir } from 'os'
10
-
11
- let failed = 0
12
- let passed = 0
13
- const failures = []
14
- function assert(cond, msg) {
15
- if (cond) { passed++; console.log(` ✅ PASS: ${msg}`) }
16
- else { failed++; failures.push(msg); console.log(` ❌ FAIL: ${msg}`) }
17
- }
18
-
19
- const tmpDirs = []
20
- function mkTmp(prefix) {
21
- const d = mkdtempSync(join(tmpdir(), `wtdeps-${prefix}-`))
22
- tmpDirs.push(d)
23
- return d
24
- }
25
- function cleanup() {
26
- for (const d of tmpDirs) {
27
- try { rmSync(d, { recursive: true, force: true }) } catch {}
28
- }
29
- }
30
-
31
- console.log('=== worktree-deps-provision 测试 ===\n')
32
-
33
- // ── lockfileHash ──
34
- console.log('--- lockfileHash ---')
35
- {
36
- const d = mkTmp('lh')
37
- assert(lockfileHash(d) === null, '空目录返回 null')
38
- writeFileSync(join(d, 'pnpm-lock.yaml'), 'lockversion: 6.0\npackages: []\n')
39
- const h1 = lockfileHash(d)
40
- assert(typeof h1 === 'string' && h1.length === 16, `pnpm-lock 返回 16 位 hash: ${h1}`)
41
-
42
- const d2 = mkTmp('lh2')
43
- writeFileSync(join(d2, 'package-lock.json'), '{"lockfileVersion":3}')
44
- const h2 = lockfileHash(d2)
45
- assert(typeof h2 === 'string' && h2.length === 16, `package-lock 返回 hash: ${h2}`)
46
- assert(h1 !== h2, '不同 lockfile 内容产生不同 hash')
47
-
48
- const d3 = mkTmp('lh3')
49
- writeFileSync(join(d3, 'yarn.lock'), '# yarn lock v1\n')
50
- assert(typeof lockfileHash(d3) === 'string', 'yarn.lock 返回 hash')
51
-
52
- const d4 = mkTmp('lh4')
53
- writeFileSync(join(d4, 'package.json'), '{"name":"x"}')
54
- assert(typeof lockfileHash(d4) === 'string', '无 lockfile 时回退 hash package.json')
55
-
56
- // 一致性:相同内容相同 hash
57
- const d5 = mkTmp('lh5')
58
- writeFileSync(join(d5, 'pnpm-lock.yaml'), 'lockversion: 6.0\npackages: []\n')
59
- assert(lockfileHash(d5) === h1, '相同 lockfile 内容产生相同 hash(快路径判定基础)')
60
- }
61
-
62
- // ── provisionDeps: generic → n/a ──
63
- console.log('\n--- provisionDeps: generic / 无依赖 → n/a ---')
64
- {
65
- const wt = mkTmp('gen-wt')
66
- const main = mkTmp('gen-main')
67
- const r = provisionDeps(wt, main, {})
68
- assert(r.depsStatus === 'n/a', `无 package.json/lockfile 的目录 → n/a(实际 ${r.depsStatus})`)
69
- assert(!existsSync(join(wt, 'node_modules')), 'n/a 不创建 node_modules')
70
- }
71
-
72
- // ── provisionDeps: nodejs junction 快路径(lockfile 一致)──
73
- console.log('\n--- provisionDeps: nodejs lockfile 一致 → linked (junction/symlink) ---')
74
- {
75
- const main = mkTmp('link-main')
76
- const wt = mkTmp('link-wt')
77
- // 主 checkout:有 node_modules + lockfile
78
- mkdirSync(join(main, 'node_modules'))
79
- writeFileSync(join(main, 'node_modules', '.placeholder'), 'x')
80
- const lockContent = 'lockversion: 6.0\npackages: []\n'
81
- writeFileSync(join(main, 'package-lock.json'), lockContent)
82
- writeFileSync(join(main, 'package.json'), '{"name":"main","scripts":{}}')
83
- // worktree:相同 lockfile(hash 一致)+ package.json
84
- writeFileSync(join(wt, 'package-lock.json'), lockContent)
85
- writeFileSync(join(wt, 'package.json'), '{"name":"wt","scripts":{}}')
86
-
87
- const r = provisionDeps(wt, main, {})
88
- assert(r.depsStatus === 'linked', `lockfile 一致 → linked(实际 ${r.depsStatus})`)
89
- assert(['junction', 'symlink', 'install'].includes(r.depsMethod), `depsMethod 合法(${r.depsMethod})`)
90
- assert(r.depsSource === 'main-checkout', `depsSource=main-checkout(${r.depsSource})`)
91
- assert(existsSync(join(wt, 'node_modules')), 'worktree/node_modules 已创建(junction/symlink)')
92
- assert(existsSync(join(wt, 'node_modules', '.placeholder')), 'junction 指向 main 的内容可访问')
93
- assert(typeof r.depsLockHash === 'string', 'depsLockHash 已记录')
94
- }
95
-
96
- // ── provisionDeps: lockfile 不一致 → 走 install 兜底(不验证真实 install,只验证不走 linked)──
97
- console.log('\n--- provisionDeps: lockfile 不一致 → 不走 linked 快路径 ---')
98
- {
99
- const main = mkTmp('mis-main')
100
- const wt = mkTmp('mis-wt')
101
- mkdirSync(join(main, 'node_modules'))
102
- writeFileSync(join(main, 'package-lock.json'), 'lockversion-A')
103
- writeFileSync(join(main, 'package.json'), '{"name":"main"}')
104
- writeFileSync(join(wt, 'package-lock.json'), 'lockversion-B-DIFFERENT') // 不一致
105
- writeFileSync(join(wt, 'package.json'), '{"name":"wt"}')
106
-
107
- const r = provisionDeps(wt, main, {})
108
- // lockfile 不一致 → 不会 linked;会尝试 install(真实 install 在 CI 可能失败/超时)
109
- assert(r.depsStatus !== 'linked', `lockfile 不一致时不应 linked(实际 ${r.depsStatus})`)
110
- assert(['installed', 'failed'].includes(r.depsStatus), `兜底为 installed 或 failed(实际 ${r.depsStatus})`)
111
- // install 失败时应带 depsError
112
- if (r.depsStatus === 'failed') {
113
- assert(typeof r.depsError === 'string' && r.depsError.length > 0, 'failed 带 depsError 信息')
114
- }
115
- }
116
-
117
- // ── provisionDeps: main 无 node_modules → 不走 linked ──
118
- console.log('\n--- provisionDeps: main 无 node_modules → 不走 linked ---')
119
- {
120
- const main = mkTmp('nomain-main')
121
- const wt = mkTmp('nomain-wt')
122
- const lockContent = 'same-lock\n'
123
- writeFileSync(join(main, 'package-lock.json'), lockContent)
124
- writeFileSync(join(main, 'package.json'), '{"name":"main"}')
125
- writeFileSync(join(wt, 'package-lock.json'), lockContent)
126
- writeFileSync(join(wt, 'package.json'), '{"name":"wt"}')
127
- // main 无 node_modules
128
-
129
- const r = provisionDeps(wt, main, {})
130
- assert(r.depsStatus !== 'linked', `main 无 node_modules 时不应 linked(实际 ${r.depsStatus})`)
131
- }
132
-
133
- // ── provisionDeps: maven/gradle 推断(无 node_modules,验证不崩)──
134
- console.log('\n--- provisionDeps: 非 nodejs 项目类型不崩 ---')
135
- {
136
- const wt = mkTmp('maven-wt')
137
- writeFileSync(join(wt, 'pom.xml'), '<project></project>')
138
- const r = provisionDeps(wt, mkTmp('maven-main'), {})
139
- assert(['installed', 'failed'].includes(r.depsStatus), `maven 项目返回 installed/failed(${r.depsStatus},mvn 可能不可用)`)
140
- }
141
-
142
- cleanup()
143
-
144
- console.log(`\n==================================================`)
145
- console.log(`✅ 通过: ${passed} ❌ 失败: ${failed}`)
146
- console.log(failed === 0 ? '全部通过' : `❌ 失败项: ${failures.join('; ')}`)
147
- console.log(`==================================================`)
148
- process.exit(failed === 0 ? 0 : 1)
@@ -1,136 +0,0 @@
1
- import assert from 'node:assert/strict'
2
- import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
3
- import { join } from 'node:path'
4
- import { tmpdir } from 'node:os'
5
- import { shouldBlock } from '../src/hooks/worktree-guard.js'
6
-
7
- const root = join(tmpdir(), `sillyspec-guard-test-${Date.now()}`)
8
- const changeName = '2026-06-04-guard-test'
9
- const runtimeDir = join(root, '.sillyspec', '.runtime')
10
- const registeredWorktree = join(runtimeDir, 'worktrees', changeName)
11
- const unregisteredWorktree = join(runtimeDir, 'worktrees', 'other-change')
12
-
13
- mkdirSync(registeredWorktree, { recursive: true })
14
- mkdirSync(unregisteredWorktree, { recursive: true })
15
- writeFileSync(join(runtimeDir, 'gate-status.json'), JSON.stringify({
16
- stage: 'execute',
17
- changes: [changeName],
18
- updatedAt: new Date().toISOString(),
19
- }, null, 2))
20
- writeFileSync(join(registeredWorktree, 'meta.json'), JSON.stringify({
21
- changeName,
22
- worktreePath: registeredWorktree,
23
- mode: 'worktree',
24
- }, null, 2))
25
-
26
- try {
27
- assert.equal(
28
- shouldBlock({ tool: 'Write', filePath: join(registeredWorktree, 'src', 'ok.js'), cwd: root }).blocked,
29
- false,
30
- 'registered worktree writes should be allowed'
31
- )
32
-
33
- assert.equal(
34
- shouldBlock({ tool: 'Bash', command: 'npm run build', cwd: registeredWorktree }).blocked,
35
- false,
36
- 'bash commands from a registered worktree cwd should be allowed'
37
- )
38
-
39
- assert.equal(
40
- shouldBlock({ tool: 'Write', filePath: join(unregisteredWorktree, 'src', 'blocked.js'), cwd: root }).blocked,
41
- true,
42
- 'unregistered worktree storage writes should be blocked'
43
- )
44
-
45
- assert.equal(
46
- shouldBlock({ tool: 'Write', filePath: join(root, '.sillyspec', 'docs', 'note.md'), cwd: root }).blocked,
47
- false,
48
- 'ordinary .sillyspec docs should remain writable'
49
- )
50
-
51
- writeFileSync(join(runtimeDir, 'gate-status.json'), JSON.stringify({
52
- stage: 'scan',
53
- changes: [changeName],
54
- updatedAt: new Date().toISOString(),
55
- }, null, 2))
56
- writeFileSync(join(runtimeDir, 'scan-guard.json'), JSON.stringify({
57
- sourceCommit: 'new-head',
58
- startedAt: '2026-06-16T10:00:00.000Z',
59
- forceRescan: false,
60
- }, null, 2))
61
- const scanDoc = join(root, '.sillyspec', 'docs', 'app', 'scan', 'ARCHITECTURE.md')
62
- mkdirSync(join(root, '.sillyspec', 'docs', 'app', 'scan'), { recursive: true })
63
- writeFileSync(scanDoc, [
64
- '---',
65
- 'source_commit: old-head',
66
- 'updated_at: 2026-06-16T09:00:00.000Z',
67
- '---',
68
- '# Architecture',
69
- '',
70
- ].join('\n'))
71
- assert.equal(
72
- shouldBlock({ tool: 'Write', filePath: scanDoc, cwd: root }).blocked,
73
- true,
74
- 'scan overwrite should block stale source_commit without --force-rescan'
75
- )
76
- writeFileSync(join(runtimeDir, 'scan-guard.json'), JSON.stringify({
77
- sourceCommit: 'new-head',
78
- startedAt: '2026-06-16T10:00:00.000Z',
79
- forceRescan: true,
80
- }, null, 2))
81
- assert.equal(
82
- shouldBlock({ tool: 'Write', filePath: scanDoc, cwd: root }).blocked,
83
- false,
84
- 'scan overwrite should allow stale source_commit with --force-rescan'
85
- )
86
- const externalSpec = join(root, 'external-spec')
87
- const externalScanDoc = join(externalSpec, 'docs', 'app', 'scan', 'ARCHITECTURE.md')
88
- mkdirSync(join(externalSpec, '.runtime'), { recursive: true })
89
- mkdirSync(join(externalSpec, 'docs', 'app', 'scan'), { recursive: true })
90
- writeFileSync(join(externalSpec, '.runtime', 'scan-guard.json'), JSON.stringify({
91
- sourceCommit: 'external-new-head',
92
- startedAt: '2026-06-16T10:00:00.000Z',
93
- forceRescan: false,
94
- }, null, 2))
95
- writeFileSync(externalScanDoc, [
96
- '---',
97
- 'source_commit: external-old-head',
98
- 'updated_at: 2026-06-16T09:00:00.000Z',
99
- '---',
100
- '# Architecture',
101
- '',
102
- ].join('\n'))
103
- assert.equal(
104
- shouldBlock({ tool: 'Write', filePath: externalScanDoc, cwd: root }).blocked,
105
- true,
106
- 'external specRoot scan overwrite should read specRoot/.runtime/scan-guard.json'
107
- )
108
-
109
- rmSync(join(runtimeDir, 'gate-status.json'), { force: true })
110
- writeFileSync(join(root, '.sillyspec', 'local.yaml'), [
111
- 'worktreeHook:',
112
- ' readonlyCommands:',
113
- ' - custom-read',
114
- '',
115
- ].join('\n'))
116
- assert.equal(
117
- shouldBlock({ tool: 'Bash', command: 'custom-read status', cwd: root }).blocked,
118
- false,
119
- '.sillyspec/local.yaml readonlyCommands should extend the bash whitelist'
120
- )
121
-
122
- writeFileSync(join(runtimeDir, 'gate-status.json'), JSON.stringify({
123
- stage: 'quick',
124
- changes: [changeName],
125
- updatedAt: new Date().toISOString(),
126
- }, null, 2))
127
- assert.equal(
128
- shouldBlock({ tool: 'Write', filePath: join(root, 'src', 'quick.js'), cwd: root }).blocked,
129
- false,
130
- 'quick writes should still be allowed in the main workspace'
131
- )
132
-
133
- console.log('✅ worktree guard regression checks passed')
134
- } finally {
135
- rmSync(root, { recursive: true, force: true })
136
- }
@@ -1,188 +0,0 @@
1
- /**
2
- * worktree native-worktree overlay regression tests
3
- *
4
- * 验证 Bug 1 + Bug 2 修复:
5
- * 1. worktreeBase 固定到主仓库路径,不跟着 cwd 变化
6
- * 2. 禁止 self-overlay(source === target 时)
7
- * 3. native-worktree meta 缺失时 recover,不 overlay
8
- * 4. meta 已存在时幂等返回,不重建
9
- */
10
-
11
- import fs from 'fs'
12
- import path from 'path'
13
- import os from 'os'
14
- import { execSync } from 'child_process'
15
-
16
- // ── Test 1: _resolveMainRepoRoot 在主仓库内返回 cwd ──
17
-
18
- async function test1_mainRepoRoot() {
19
- const d = fs.mkdtempSync(path.join(os.tmpdir(), 'wt-'))
20
- execSync('git init', { cwd: d, stdio: 'pipe' })
21
- execSync('git config user.email "test@test.com"', { cwd: d, stdio: 'pipe' })
22
- execSync('git config user.name "test"', { cwd: d, stdio: 'pipe' })
23
- execSync('git commit --allow-empty -m "init"', { cwd: d, stdio: 'pipe' })
24
-
25
- // Add .gitignore to allow worktreeBase path
26
- fs.mkdirSync(path.join(d, '.sillyspec'), { recursive: true })
27
- fs.writeFileSync(path.join(d, '.gitignore'), '.sillyspec/\n')
28
-
29
- const { WorktreeManager } = await import('../src/worktree.js')
30
- const wm = new WorktreeManager({ cwd: d })
31
- const root = wm._resolveMainRepoRoot()
32
- console.assert(root === d, `Test 1 FAIL: expected ${d}, got ${root}`)
33
- console.log('✅ Test 1: main repo root resolves to cwd')
34
-
35
- fs.rmSync(d, { recursive: true })
36
- }
37
-
38
- // ── Test 2: native-worktree 检测 + 幂等守卫 ──
39
-
40
- async function test2_nativeWorktreeIdempotent() {
41
- const d = fs.mkdtempSync(path.join(os.tmpdir(), 'wt-'))
42
- execSync('git init', { cwd: d, stdio: 'pipe' })
43
- execSync('git config user.email "test@test.com"', { cwd: d, stdio: 'pipe' })
44
- execSync('git config user.name "test"', { cwd: d, stdio: 'pipe' })
45
- execSync('git commit --allow-empty -m "init"', { cwd: d, stdio: 'pipe' })
46
-
47
- const wtDir = path.join(d, 'wt')
48
- execSync(`git worktree add ${wtDir} -b test-branch`, { cwd: d, stdio: 'pipe' })
49
-
50
- // wtDir is a linked worktree
51
- const { WorktreeManager } = await import('../src/worktree.js')
52
-
53
- // First: create meta from inside worktree (simulating native-worktree mode)
54
- const wm1 = new WorktreeManager({ cwd: wtDir })
55
- // This simulates what happens when Claude Code runs in the worktree
56
- // and SillySpec calls create() — detectIsolation should return inWorktree=true
57
- // and meta should be written to main repo's worktreeBase, not worktree's
58
-
59
- // Verify worktreeBase points to main repo
60
- const expectedBase = path.join(d, '.sillyspec', '.runtime', 'worktrees')
61
- console.assert(wm1.worktreeBase === expectedBase, `Test 2 FAIL: worktreeBase=${wm1.worktreeBase}, expected=${expectedBase}`)
62
- console.log('✅ Test 2: worktreeBase fixed to main repo path')
63
-
64
- fs.rmSync(d, { recursive: true })
65
- }
66
-
67
- // ── Test 3: self-overlay 禁止 ──
68
-
69
- async function test3_selfOverlayBlocked() {
70
- const d = fs.mkdtempSync(path.join(os.tmpdir(), 'wt-'))
71
- execSync('git init', { cwd: d, stdio: 'pipe' })
72
- execSync('git config user.email "test@test.com"', { cwd: d, stdio: 'pipe' })
73
- execSync('git config user.name "test"', { cwd: d, stdio: 'pipe' })
74
- execSync('git commit --allow-empty -m "init"', { cwd: d, stdio: 'pipe' })
75
- fs.mkdirSync(path.join(d, '.sillyspec'), { recursive: true })
76
- fs.writeFileSync(path.join(d, '.gitignore'), '.sillyspec/\n')
77
-
78
- const { WorktreeManager } = await import('../src/worktree.js')
79
- const wm = new WorktreeManager({ cwd: d })
80
-
81
- // Create with in-place-fallback mode (source === target)
82
- const result = wm._createInPlaceMeta('test-change', {
83
- worktreePath: d,
84
- branch: 'test-branch',
85
- mode: 'in-place-fallback',
86
- })
87
-
88
- console.assert(result.mode === 'in-place-fallback', `Test 3 FAIL: mode=${result.mode}`)
89
- // 返回值不包含 baselineFiles(是简化的返回结构),验证 meta 文件本身
90
- const meta3 = wm.getMeta('test-change')
91
- console.assert(meta3 && meta3.baselineFiles.length === 0, `Test 3 FAIL: meta baselineFiles should be empty`)
92
- console.log('✅ Test 3: self-overlay blocked, baselineFiles empty')
93
-
94
- fs.rmSync(d, { recursive: true })
95
- }
96
-
97
- // ── Test 4: meta 幂等,不重复 overlay ──
98
-
99
- async function test4_metaIdempotent() {
100
- const d = fs.mkdtempSync(path.join(os.tmpdir(), 'wt-'))
101
- execSync('git init', { cwd: d, stdio: 'pipe' })
102
- execSync('git config user.email "test@test.com"', { cwd: d, stdio: 'pipe' })
103
- execSync('git config user.name "test"', { cwd: d, stdio: 'pipe' })
104
- execSync('git commit --allow-empty -m "init"', { cwd: d, stdio: 'pipe' })
105
- fs.mkdirSync(path.join(d, '.sillyspec'), { recursive: true })
106
- fs.writeFileSync(path.join(d, '.gitignore'), '.sillyspec/\n')
107
-
108
- const { WorktreeManager } = await import('../src/worktree.js')
109
- const wm = new WorktreeManager({ cwd: d })
110
-
111
- // First create
112
- const r1 = wm._createInPlaceMeta('test-change', {
113
- worktreePath: d,
114
- branch: 'test-branch',
115
- mode: 'in-place-fallback',
116
- })
117
-
118
- // Second create (should return existing, not re-overlay)
119
- const r2 = wm._createInPlaceMeta('test-change', {
120
- worktreePath: d,
121
- branch: 'test-branch',
122
- mode: 'in-place-fallback',
123
- })
124
-
125
- console.assert(r1.baseHash === r2.baseHash, `Test 4 FAIL: hashes differ`)
126
- console.log('✅ Test 4: _createInPlaceMeta idempotent')
127
-
128
- fs.rmSync(d, { recursive: true })
129
- }
130
-
131
- // ── Test 5: native-worktree meta 恢复 ──
132
-
133
- async function test5_nativeRecovery() {
134
- const d = fs.mkdtempSync(path.join(os.tmpdir(), 'wt-'))
135
- execSync('git init', { cwd: d, stdio: 'pipe' })
136
- execSync('git config user.email "test@test.com"', { cwd: d, stdio: 'pipe' })
137
- execSync('git config user.name "test"', { cwd: d, stdio: 'pipe' })
138
- execSync('git commit --allow-empty -m "init"', { cwd: d, stdio: 'pipe' })
139
-
140
- const wtDir = path.join(d, 'wt')
141
- execSync(`git worktree add ${wtDir} -b test-branch`, { cwd: d, stdio: 'pipe' })
142
-
143
- const { WorktreeManager } = await import('../src/worktree.js')
144
- const wm = new WorktreeManager({ cwd: wtDir })
145
-
146
- // Simulate meta missing in native-worktree (should recover, not overlay)
147
- const result = wm._recoverNativeWorktreeMeta('test-change', {
148
- worktreePath: wtDir,
149
- branch: 'test-branch',
150
- })
151
-
152
- console.assert(result.mode === 'native-worktree', `Test 5 FAIL: mode=${result.mode}`)
153
- // 返回值不包含 baselineFiles,验证 meta 文件本身
154
- const meta5 = wm.getMeta('test-change')
155
- console.assert(meta5 && meta5.baselineFiles.length === 0, `Test 5 FAIL: meta baselineFiles should be empty`)
156
-
157
- // Verify meta is readable now
158
- const meta = wm.getMeta('test-change')
159
- console.assert(meta !== null, `Test 5 FAIL: meta should exist after recovery`)
160
- console.assert(meta.mode === 'native-worktree', `Test 5 FAIL: meta.mode=${meta.mode}`)
161
- console.log('✅ Test 5: native-worktree meta recovery without overlay')
162
-
163
- fs.rmSync(d, { recursive: true })
164
- }
165
-
166
- // ── Run all ──
167
-
168
- const tests = [
169
- ['main repo root', test1_mainRepoRoot],
170
- ['native worktree worktreeBase', test2_nativeWorktreeIdempotent],
171
- ['self-overlay blocked', test3_selfOverlayBlocked],
172
- ['meta idempotent', test4_metaIdempotent],
173
- ['native recovery', test5_nativeRecovery],
174
- ]
175
-
176
- let passed = 0, failed = 0
177
- for (const [name, fn] of tests) {
178
- try {
179
- await fn()
180
- passed++
181
- } catch (e) {
182
- console.log(`❌ ${name}: ${e.message}`)
183
- failed++
184
- }
185
- }
186
-
187
- console.log(`\n${passed}/${tests.length} passed`)
188
- if (failed) process.exit(1)