@capyclaw/capyclaw 0.2.0 → 0.3.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.
- package/README.md +20 -2
- package/client/client.js +273 -0
- package/index.js +197 -15
- package/lib/environment.mjs +79 -3
- package/lib/jobs.mjs +116 -0
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +10 -3
- package/test/plugin.test.mjs +79 -1
- package/test/stability.test.mjs +576 -0
- package/test/wizard.test.mjs +219 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
// Stability suite: fuzzed inputs, version/platform edge cases, idempotency,
|
|
2
|
+
// concurrency, disposal leaks, and export/sync robustness. All deterministic.
|
|
3
|
+
import test from 'node:test'
|
|
4
|
+
import assert from 'node:assert/strict'
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import os from 'node:os'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { apply, executeEnvironmentPlan } from '../index.js'
|
|
9
|
+
import { MANIFEST } from '../manifest.js'
|
|
10
|
+
import { detectPlatform, normalizeEnvironmentManifest, planEnvironment, createEnvironmentManifest } from '../lib/environment.mjs'
|
|
11
|
+
|
|
12
|
+
// ---------- helpers ----------
|
|
13
|
+
function mulberry32(seed) {
|
|
14
|
+
let a = seed >>> 0
|
|
15
|
+
return () => {
|
|
16
|
+
a |= 0; a = (a + 0x6D2B79F5) | 0
|
|
17
|
+
let t = Math.imul(a ^ (a >>> 15), 1 | a)
|
|
18
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
|
|
19
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function deepFreeze(value) {
|
|
23
|
+
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
24
|
+
Object.freeze(value)
|
|
25
|
+
for (const v of Object.values(value)) deepFreeze(v)
|
|
26
|
+
}
|
|
27
|
+
return value
|
|
28
|
+
}
|
|
29
|
+
function tmpProfile(deps = {}, installed = {}) {
|
|
30
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
31
|
+
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ dependencies: deps }))
|
|
32
|
+
for (const [name, version] of Object.entries(installed)) {
|
|
33
|
+
const pkgDir = path.join(dir, 'node_modules', ...name.split('/'))
|
|
34
|
+
fs.mkdirSync(pkgDir, { recursive: true })
|
|
35
|
+
fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({ name, version }))
|
|
36
|
+
}
|
|
37
|
+
return dir
|
|
38
|
+
}
|
|
39
|
+
function harness({ dir = null, bundles = [], installResult = { application: 'applied' }, listBundlesError = null } = {}) {
|
|
40
|
+
const tools = new Map()
|
|
41
|
+
const calls = []
|
|
42
|
+
const ctx = {
|
|
43
|
+
profileContext: { dir },
|
|
44
|
+
tools: { register(tool) { tools.set(tool.name, tool); return () => tools.delete(tool.name) } },
|
|
45
|
+
pluginManager: {
|
|
46
|
+
async listBundles() { if (listBundlesError) throw listBundlesError; return bundles },
|
|
47
|
+
async installBundle(...args) { calls.push(['install', ...args]); return installResult },
|
|
48
|
+
async setBundleEnabled(...args) { calls.push(['enable', ...args]); return { application: 'applied' } },
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
const dispose = apply(ctx)
|
|
52
|
+
return { tools, calls, dispose, ctx }
|
|
53
|
+
}
|
|
54
|
+
const plan = (overrides = {}) => planEnvironment({
|
|
55
|
+
manifest: { schemaVersion: 1, name: 't', packages: [{ name: 'demo', spec: 'demo@1.0.0' }], bundles: [] },
|
|
56
|
+
platform: 'windows', installed: {}, dshVersion: null, allowUpgrade: false, ...overrides,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// ---------- platform detection ----------
|
|
60
|
+
test('detectPlatform maps every known OS and rejects the rest deterministically', () => {
|
|
61
|
+
assert.equal(detectPlatform('win32'), 'windows')
|
|
62
|
+
assert.equal(detectPlatform('darwin'), 'macos')
|
|
63
|
+
assert.equal(detectPlatform('linux'), 'linux')
|
|
64
|
+
for (const p of ['freebsd', 'aix', 'sunos', 'android', 'win64', 'WIN32', '', null, undefined, 0, {}]) {
|
|
65
|
+
if (p === undefined) continue // defaults to process.platform
|
|
66
|
+
assert.equal(detectPlatform(p), 'unsupported', `platform ${String(p)}`)
|
|
67
|
+
}
|
|
68
|
+
assert.equal(detectPlatform(), detectPlatform(process.platform))
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
// ---------- manifest normalization fuzz ----------
|
|
72
|
+
test('normalizeEnvironmentManifest rejects malformed shapes with TypeError only (300 fuzz cases)', () => {
|
|
73
|
+
const rand = mulberry32(20261123)
|
|
74
|
+
const pick = (arr) => arr[Math.floor(rand() * arr.length)]
|
|
75
|
+
const weirdNames = ['', ' ', 'a b', '@', '@scope/', '工具', 'x'.repeat(500), '../evil', 'a/b', null, 42, {}]
|
|
76
|
+
const weirdSpecs = ['', ' ', 'demo@', 'demo@^1.0.0', 'demo@latest', 'demo@1.0.0+build', 'demo@01.2.3', 'link:../x', 'file:../x', 'git+https://x/y.git', 'npm:other@1.0.0', '/abs/path', '../rel', null, 7, []]
|
|
77
|
+
const weirdPlatforms = [null, 'windows', ['android'], ['windows', 'windows'], [null], [1], ['WINDOWS'], [], {}]
|
|
78
|
+
let accepted = 0, rejected = 0
|
|
79
|
+
for (let i = 0; i < 300; i++) {
|
|
80
|
+
const raw = {
|
|
81
|
+
schemaVersion: pick([1, 1, 1, 2, 0, '1', null]),
|
|
82
|
+
name: pick(['ok', '', null, 12]),
|
|
83
|
+
packages: pick([
|
|
84
|
+
[{ name: pick(weirdNames), spec: pick(weirdSpecs), platforms: pick(weirdPlatforms), required: pick([true, false, 'yes', null]) }],
|
|
85
|
+
[{ name: 'demo', spec: 'demo@1.0.0' }, { name: 'demo', spec: 'demo@1.0.0' }],
|
|
86
|
+
null, 'nope', [null], [{}],
|
|
87
|
+
]),
|
|
88
|
+
bundles: pick([[], ['ok', 42, null], 'nope', null]),
|
|
89
|
+
dsh: pick([{}, { version: '0.2.0' }, null, 'nope']),
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
const normalized = normalizeEnvironmentManifest(raw)
|
|
93
|
+
assert.equal(normalized.schemaVersion, 1)
|
|
94
|
+
assert.ok(Array.isArray(normalized.packages))
|
|
95
|
+
for (const p of normalized.packages) {
|
|
96
|
+
assert.ok(typeof p.name === 'string' && p.name.trim())
|
|
97
|
+
assert.ok(typeof p.spec === 'string' && p.spec.trim())
|
|
98
|
+
assert.ok(p.platforms.every((x) => ['windows', 'macos', 'linux'].includes(x)))
|
|
99
|
+
}
|
|
100
|
+
accepted++
|
|
101
|
+
} catch (error) {
|
|
102
|
+
assert.ok(error instanceof TypeError, `case ${i} threw ${error.constructor.name}: ${error.message}`)
|
|
103
|
+
rejected++
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
assert.ok(accepted > 0 && rejected > 0, `fuzz should exercise both paths (accepted=${accepted}, rejected=${rejected})`)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('normalizeEnvironmentManifest rejects non-object roots', () => {
|
|
110
|
+
for (const raw of [null, undefined, 0, 'x', [], true]) {
|
|
111
|
+
assert.throws(() => normalizeEnvironmentManifest(raw), TypeError)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// ---------- version edge cases through the planner ----------
|
|
116
|
+
test('exact-version gate rejects malformed, ranged, protocol, and mismatched specs', () => {
|
|
117
|
+
const badSpecs = [
|
|
118
|
+
'demo@^1.0.0', 'demo@~1.0.0', 'demo@*', 'demo@latest', 'demo@1.x', 'demo@>=1 <2',
|
|
119
|
+
'demo@01.2.3', 'demo@1.02.3', 'demo@1.2.03', 'demo@1.0.0-01', 'demo@1.0.0-alpha.01',
|
|
120
|
+
'demo@1.0.0+build', 'demo@1.0.0 ', 'demo@ 1.0.0', 'demo@v1.0.0', 'demo@1.0', 'demo@1.0.0.0',
|
|
121
|
+
'demo@', 'other@1.0.0', 'link:../demo', 'file:../demo', 'git+https://x/demo.git', 'npm:demo@1.0.0',
|
|
122
|
+
'demo@-1.0.0', 'demo@1.0.0-', 'demo@1.0.0-..a',
|
|
123
|
+
]
|
|
124
|
+
for (const spec of badSpecs) {
|
|
125
|
+
const result = plan({ manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec }], bundles: [] } })
|
|
126
|
+
assert.equal(result.ready, false, `spec ${spec} must block`)
|
|
127
|
+
assert.ok(result.blockers.some((b) => b.includes('exact package@version')), `spec ${spec} blocker text`)
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('valid exact and prerelease versions plan cleanly; huge versions are accepted', () => {
|
|
132
|
+
for (const version of ['1.0.0', '0.0.0', '10.20.30', '1.0.0-0', '1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-rc.1', '1.0.0-x.7.z.92', '999999999999.0.0']) {
|
|
133
|
+
const result = plan({ manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec: `demo@${version}` }], bundles: [] } })
|
|
134
|
+
assert.equal(result.ready, true, `version ${version}`)
|
|
135
|
+
assert.equal(result.actions[0].action, 'install')
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('installed version comparison is exact, scoped-name safe, and gates changes', () => {
|
|
140
|
+
const manifest = { schemaVersion: 1, packages: [{ name: '@scope/pkg', spec: '@scope/pkg@1.2.3' }], bundles: [] }
|
|
141
|
+
const keep = plan({ manifest, installed: { '@scope/pkg': '@scope/pkg@1.2.3' } })
|
|
142
|
+
assert.equal(keep.actions[0].action, 'keep')
|
|
143
|
+
assert.equal(keep.ready, true)
|
|
144
|
+
const change = plan({ manifest, installed: { '@scope/pkg': '@scope/pkg@1.2.4' } })
|
|
145
|
+
assert.equal(change.actions[0].action, 'version-change')
|
|
146
|
+
assert.equal(change.ready, false)
|
|
147
|
+
const allowed = plan({ manifest, installed: { '@scope/pkg': '@scope/pkg@1.2.4' }, allowUpgrade: true })
|
|
148
|
+
assert.equal(allowed.ready, true)
|
|
149
|
+
const prereleaseDiff = plan({ manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec: 'demo@1.0.0-rc.1' }], bundles: [] }, installed: { demo: 'demo@1.0.0-rc.2' } })
|
|
150
|
+
assert.equal(prereleaseDiff.actions[0].action, 'version-change')
|
|
151
|
+
const localLink = plan({ installed: { demo: null } })
|
|
152
|
+
assert.equal(localLink.actions[0].action, 'install')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// ---------- platform matrix ----------
|
|
156
|
+
test('platform matrix: every OS x platform-subset combination plans consistently', () => {
|
|
157
|
+
const subsets = [['windows'], ['macos'], ['linux'], ['windows', 'macos'], ['windows', 'linux'], ['macos', 'linux'], ['windows', 'macos', 'linux']]
|
|
158
|
+
for (const platform of ['windows', 'macos', 'linux']) {
|
|
159
|
+
for (const platforms of subsets) {
|
|
160
|
+
const result = plan({ platform, manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec: 'demo@1.0.0', platforms }], bundles: [] } })
|
|
161
|
+
if (platforms.includes(platform)) {
|
|
162
|
+
assert.equal(result.actions.length, 1, `${platform} ${platforms}`)
|
|
163
|
+
assert.equal(result.skipped.length, 0)
|
|
164
|
+
} else {
|
|
165
|
+
assert.equal(result.actions.length, 0)
|
|
166
|
+
assert.equal(result.skipped[0].reason, 'unsupported-platform')
|
|
167
|
+
}
|
|
168
|
+
assert.equal(result.ready, true)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test('unsupported OS blocks only when a package is required', () => {
|
|
174
|
+
const required = plan({ platform: 'unsupported', manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec: 'demo@1.0.0', required: true }], bundles: [] } })
|
|
175
|
+
assert.equal(required.ready, false)
|
|
176
|
+
assert.ok(required.blockers.includes('Unsupported operating system'))
|
|
177
|
+
const optional = plan({ platform: 'unsupported', manifest: { schemaVersion: 1, packages: [{ name: 'demo', spec: 'demo@1.0.0', required: false }], bundles: [] } })
|
|
178
|
+
assert.equal(optional.ready, true)
|
|
179
|
+
assert.equal(optional.actions.length, 0)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// ---------- DSH version gate ----------
|
|
183
|
+
test('DSH version requirement gates mismatch and unknown, passes match and absence', () => {
|
|
184
|
+
const manifest = { schemaVersion: 1, dsh: { version: '0.2.2-rc.5' }, packages: [], bundles: [] }
|
|
185
|
+
assert.equal(plan({ manifest, dshVersion: '0.2.2-rc.5' }).ready, true)
|
|
186
|
+
const mismatch = plan({ manifest, dshVersion: '0.2.1' })
|
|
187
|
+
assert.equal(mismatch.ready, false)
|
|
188
|
+
assert.ok(mismatch.blockers.some((b) => b.startsWith('DSH 0.2.2-rc.5 required')))
|
|
189
|
+
const unknown = plan({ manifest, dshVersion: null })
|
|
190
|
+
assert.equal(unknown.ready, false)
|
|
191
|
+
assert.ok(unknown.blockers.some((b) => b.startsWith('Cannot verify required DSH version')))
|
|
192
|
+
assert.equal(plan({ dshVersion: null }).ready, true)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
// ---------- determinism, purity, scale ----------
|
|
196
|
+
test('planner is deterministic, pure (frozen inputs), and idempotent across 100 repeats', () => {
|
|
197
|
+
const manifest = deepFreeze({ schemaVersion: 1, name: 'frozen', dsh: { version: '0.2.0' }, packages: [{ name: 'demo', spec: 'demo@1.0.0', platforms: ['windows', 'linux'] }, { name: 'opt', spec: 'opt@2.0.0', required: false }], bundles: ['demo'] })
|
|
198
|
+
const installed = deepFreeze({ demo: 'demo@1.0.0' })
|
|
199
|
+
const first = plan({ manifest, installed, dshVersion: '0.2.0' })
|
|
200
|
+
for (let i = 0; i < 100; i++) {
|
|
201
|
+
assert.deepEqual(plan({ manifest, installed, dshVersion: '0.2.0' }), first)
|
|
202
|
+
}
|
|
203
|
+
assert.equal(first.actions.find((a) => a.name === 'demo').action, 'keep')
|
|
204
|
+
assert.equal(first.actions.find((a) => a.name === 'opt').action, 'install')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
test('starter MANIFEST normalizes and plans ready on every platform, 100 repeats', () => {
|
|
208
|
+
for (let i = 0; i < 100; i++) {
|
|
209
|
+
const normalized = normalizeEnvironmentManifest(MANIFEST)
|
|
210
|
+
assert.equal(normalized.packages.length, 8)
|
|
211
|
+
for (const platform of ['windows', 'macos', 'linux', 'unsupported']) {
|
|
212
|
+
const result = planEnvironment({ manifest: MANIFEST, platform, installed: {}, dshVersion: null })
|
|
213
|
+
assert.equal(result.ready, true, `starter on ${platform}`)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('large manifest (500 packages) plans without degradation or mutation', () => {
|
|
219
|
+
const packages = Array.from({ length: 500 }, (_, i) => ({ name: `pkg-${i}`, spec: `pkg-${i}@1.${i % 50}.0` }))
|
|
220
|
+
const manifest = deepFreeze({ schemaVersion: 1, packages, bundles: [] })
|
|
221
|
+
const result = plan({ manifest })
|
|
222
|
+
assert.equal(result.actions.length, 500)
|
|
223
|
+
assert.equal(result.ready, true)
|
|
224
|
+
assert.ok(result.actions.every((a) => a.action === 'install'))
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
test('unicode and scoped names survive normalization and planning', () => {
|
|
228
|
+
const manifest = { schemaVersion: 1, packages: [{ name: '工具', spec: '工具@1.0.0' }, { name: '@a/b', spec: '@a/b@2.0.0' }], bundles: [] }
|
|
229
|
+
const result = plan({ manifest, installed: { 工具: '工具@1.0.0', '@a/b': '@a/b@2.0.0' } })
|
|
230
|
+
assert.equal(result.ready, true)
|
|
231
|
+
assert.ok(result.actions.every((a) => a.action === 'keep'))
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
// ---------- export robustness ----------
|
|
235
|
+
test('export survives missing/corrupt profile and non-object dependencies', async () => {
|
|
236
|
+
const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
237
|
+
const h = harness({ dir: emptyDir })
|
|
238
|
+
const out = path.join(emptyDir, 'nested', 'deep', 'export.json')
|
|
239
|
+
const result = await h.tools.get('capyclaw_export_environment').execute({ outputPath: out })
|
|
240
|
+
assert.equal(result.ok, true)
|
|
241
|
+
assert.equal(result.packageCount, 0)
|
|
242
|
+
h.dispose()
|
|
243
|
+
fs.rmSync(emptyDir, { recursive: true, force: true })
|
|
244
|
+
|
|
245
|
+
const corruptDir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
246
|
+
fs.writeFileSync(path.join(corruptDir, 'package.json'), '{broken json!!')
|
|
247
|
+
const h2 = harness({ dir: corruptDir })
|
|
248
|
+
const result2 = await h2.tools.get('capyclaw_export_environment').execute({ outputPath: path.join(corruptDir, 'e.json') })
|
|
249
|
+
assert.equal(result2.packageCount, 0)
|
|
250
|
+
h2.dispose()
|
|
251
|
+
fs.rmSync(corruptDir, { recursive: true, force: true })
|
|
252
|
+
|
|
253
|
+
for (const deps of ['weird', ['a', 'b'], null, 42]) {
|
|
254
|
+
const dir = tmpProfile(deps)
|
|
255
|
+
const h3 = harness({ dir })
|
|
256
|
+
const r = await h3.tools.get('capyclaw_export_environment').execute({ outputPath: path.join(dir, 'e.json') })
|
|
257
|
+
assert.equal(r.packageCount, 0)
|
|
258
|
+
h3.dispose()
|
|
259
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('export omits every machine-local spec form even when node_modules resolves a version', async () => {
|
|
264
|
+
const localSpecs = ['link:../local', 'file:../local', 'workspace:*', 'git+https://x/y.git', 'github:u/r', 'npm:other@1.0.0', '../relative', '/absolute/path', 'C:\\abs\\path']
|
|
265
|
+
const deps = { 'reg-pkg': '^1.0.0' }
|
|
266
|
+
const installed = { 'reg-pkg': '1.2.3' }
|
|
267
|
+
for (const [i, spec] of localSpecs.entries()) {
|
|
268
|
+
deps[`local-${i}`] = spec
|
|
269
|
+
installed[`local-${i}`] = '9.9.9' // resolvable version must NOT leak
|
|
270
|
+
}
|
|
271
|
+
const dir = tmpProfile(deps, installed)
|
|
272
|
+
const h = harness({ dir, bundles: [{ name: 'reg-pkg', enabled: true }, { name: 'off', enabled: false }] })
|
|
273
|
+
const out = path.join(dir, 'export.json')
|
|
274
|
+
const result = await h.tools.get('capyclaw_export_environment').execute({ outputPath: out })
|
|
275
|
+
const manifest = JSON.parse(fs.readFileSync(out, 'utf8'))
|
|
276
|
+
assert.deepEqual(manifest.packages.map((p) => p.spec), ['reg-pkg@1.2.3'])
|
|
277
|
+
assert.deepEqual(manifest.bundles, ['reg-pkg'])
|
|
278
|
+
assert.equal(result.omittedLocalLinks, localSpecs.length)
|
|
279
|
+
h.dispose()
|
|
280
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
test('export is byte-deterministic across 10 runs and survives bundle inventory failure', async () => {
|
|
284
|
+
const dir = tmpProfile({ a: '^1.0.0', b: '~2.0.0' }, { a: '1.5.0', b: '2.3.4' })
|
|
285
|
+
const h = harness({ dir, bundles: [{ name: 'a', enabled: true }] })
|
|
286
|
+
const out = path.join(dir, 'export.json')
|
|
287
|
+
let first = null
|
|
288
|
+
for (let i = 0; i < 10; i++) {
|
|
289
|
+
await h.tools.get('capyclaw_export_environment').execute({ outputPath: out })
|
|
290
|
+
const bytes = fs.readFileSync(out, 'utf8')
|
|
291
|
+
if (first === null) first = bytes
|
|
292
|
+
assert.equal(bytes, first)
|
|
293
|
+
}
|
|
294
|
+
h.dispose()
|
|
295
|
+
const h2 = harness({ dir, listBundlesError: new Error('inventory down') })
|
|
296
|
+
const result = await h2.tools.get('capyclaw_export_environment').execute({ outputPath: out })
|
|
297
|
+
assert.equal(result.ok, true)
|
|
298
|
+
assert.equal(result.bundleCount, 0)
|
|
299
|
+
assert.equal(result.packageCount, 2)
|
|
300
|
+
h2.dispose()
|
|
301
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
test('export rejects missing outputPath and status rejects bad manifest paths', async () => {
|
|
305
|
+
const h = harness()
|
|
306
|
+
await assert.rejects(() => h.tools.get('capyclaw_export_environment').execute({}), TypeError)
|
|
307
|
+
await assert.rejects(() => h.tools.get('capyclaw_status').execute({ manifestPath: path.join(os.tmpdir(), 'capyclaw-does-not-exist-12345.json') }))
|
|
308
|
+
const badDir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
309
|
+
const badFile = path.join(badDir, 'bad.json')
|
|
310
|
+
fs.writeFileSync(badFile, '{not json')
|
|
311
|
+
await assert.rejects(() => h.tools.get('capyclaw_status').execute({ manifestPath: badFile }))
|
|
312
|
+
await assert.rejects(() => h.tools.get('capyclaw_status').execute({ manifestPath: badDir })) // directory
|
|
313
|
+
const wrongSchema = path.join(badDir, 'wrong.json')
|
|
314
|
+
fs.writeFileSync(wrongSchema, JSON.stringify({ schemaVersion: 2, packages: [] }))
|
|
315
|
+
await assert.rejects(() => h.tools.get('capyclaw_sync').execute({ manifestPath: wrongSchema }), TypeError)
|
|
316
|
+
h.dispose()
|
|
317
|
+
fs.rmSync(badDir, { recursive: true, force: true })
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
// ---------- sync/apply robustness ----------
|
|
321
|
+
function applyHarness({ failInstall = false, throwInstall = false, application = 'applied', failEnable = false, inventoryFailsAfterInstall = false } = {}) {
|
|
322
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
323
|
+
const deps = {}
|
|
324
|
+
const enabled = new Set()
|
|
325
|
+
const calls = []
|
|
326
|
+
let installedOnce = false
|
|
327
|
+
const writeProfile = () => fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ dependencies: deps }))
|
|
328
|
+
writeProfile()
|
|
329
|
+
const ctx = {
|
|
330
|
+
profileContext: { dir },
|
|
331
|
+
tools: { register() { return () => {} } },
|
|
332
|
+
pluginManager: {
|
|
333
|
+
async listBundles() {
|
|
334
|
+
if (inventoryFailsAfterInstall && installedOnce) throw new Error('inventory exploded')
|
|
335
|
+
return Object.keys(deps).map((name) => ({ name, enabled: enabled.has(name) }))
|
|
336
|
+
},
|
|
337
|
+
async installBundle(spec, options) {
|
|
338
|
+
calls.push(['install', spec, options])
|
|
339
|
+
if (throwInstall) throw new Error('install threw')
|
|
340
|
+
if (failInstall) return { application: 'failed', error: 'registry unavailable' }
|
|
341
|
+
const at = spec.lastIndexOf('@')
|
|
342
|
+
const name = spec.slice(0, at), version = spec.slice(at + 1)
|
|
343
|
+
deps[name] = version
|
|
344
|
+
const pkgDir = path.join(dir, 'node_modules', ...name.split('/'))
|
|
345
|
+
fs.mkdirSync(pkgDir, { recursive: true })
|
|
346
|
+
fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({ name, version }))
|
|
347
|
+
writeProfile()
|
|
348
|
+
enabled.add(name)
|
|
349
|
+
installedOnce = true
|
|
350
|
+
return { application }
|
|
351
|
+
},
|
|
352
|
+
async setBundleEnabled(name, on) {
|
|
353
|
+
calls.push(['enable', name, on])
|
|
354
|
+
if (failEnable) return { application: 'failed', error: 'enable denied' }
|
|
355
|
+
if (on) enabled.add(name); else enabled.delete(name)
|
|
356
|
+
return { application: 'applied' }
|
|
357
|
+
},
|
|
358
|
+
async updateBundle(name, version) {
|
|
359
|
+
calls.push(['update', name, version])
|
|
360
|
+
if (failInstall) return { from: deps[name], to: version, step: 'install', result: { application: 'failed', error: 'registry unavailable' } }
|
|
361
|
+
const from = deps[name]
|
|
362
|
+
deps[name] = version
|
|
363
|
+
const pkgDir = path.join(dir, 'node_modules', ...name.split('/'))
|
|
364
|
+
fs.mkdirSync(pkgDir, { recursive: true })
|
|
365
|
+
fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({ name, version }))
|
|
366
|
+
writeProfile()
|
|
367
|
+
enabled.add(name)
|
|
368
|
+
installedOnce = true
|
|
369
|
+
return { from, to: version, step: 'install', result: { application } }
|
|
370
|
+
},
|
|
371
|
+
async removeBundle(name) {
|
|
372
|
+
calls.push(['remove', name])
|
|
373
|
+
delete deps[name]
|
|
374
|
+
enabled.delete(name)
|
|
375
|
+
fs.rmSync(path.join(dir, 'node_modules', ...name.split('/')), { recursive: true, force: true })
|
|
376
|
+
writeProfile()
|
|
377
|
+
return { stage: 'remove', target: name, application: 'applied' }
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
}
|
|
381
|
+
const tools = new Map()
|
|
382
|
+
ctx.tools.register = (tool) => { tools.set(tool.name, tool); return () => tools.delete(tool.name) }
|
|
383
|
+
const dispose = apply(ctx)
|
|
384
|
+
return { dir, deps, calls, tools, dispose, enabled, ctx }
|
|
385
|
+
}
|
|
386
|
+
function writeManifest(dir, manifest) {
|
|
387
|
+
const file = path.join(dir, 'manifest.json')
|
|
388
|
+
fs.writeFileSync(file, JSON.stringify(manifest))
|
|
389
|
+
return file
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
test('apply is idempotent: second run keeps everything and installs nothing new', async () => {
|
|
393
|
+
const h = applyHarness()
|
|
394
|
+
const manifestPath = writeManifest(h.dir, { schemaVersion: 1, name: 'idem', packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }, { name: '@s/beta', spec: '@s/beta@2.0.0' }], bundles: [] })
|
|
395
|
+
const first = await h.tools.get('capyclaw_sync').execute({ manifestPath, apply: true })
|
|
396
|
+
assert.equal(first.mode, 'applied')
|
|
397
|
+
assert.equal(first.ok, true)
|
|
398
|
+
assert.equal(h.calls.filter((c) => c[0] === 'install').length, 2)
|
|
399
|
+
assert.ok(first.profileBackup && fs.existsSync(first.profileBackup))
|
|
400
|
+
const backup = JSON.parse(fs.readFileSync(first.profileBackup, 'utf8'))
|
|
401
|
+
assert.deepEqual(backup.dependencies, {}) // backup captured pre-apply state
|
|
402
|
+
const installCalls = h.calls.length
|
|
403
|
+
const second = await h.tools.get('capyclaw_sync').execute({ manifestPath, apply: true })
|
|
404
|
+
assert.equal(second.ok, true)
|
|
405
|
+
assert.ok(second.final.actions.every((a) => a.action === 'keep'))
|
|
406
|
+
assert.equal(h.calls.length, installCalls) // no new manager calls
|
|
407
|
+
h.dispose()
|
|
408
|
+
fs.rmSync(h.dir, { recursive: true, force: true })
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
test('apply blocks version changes without allowUpgrade and never touches the profile', async () => {
|
|
412
|
+
const h = applyHarness()
|
|
413
|
+
const manifestPath = writeManifest(h.dir, { schemaVersion: 1, packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }], bundles: [] })
|
|
414
|
+
await h.tools.get('capyclaw_sync').execute({ manifestPath, apply: true })
|
|
415
|
+
const before = fs.readFileSync(path.join(h.dir, 'package.json'), 'utf8')
|
|
416
|
+
const upgradeManifest = writeManifest(h.dir, { schemaVersion: 1, name: 'up', packages: [{ name: 'alpha', spec: 'alpha@2.0.0' }], bundles: [] })
|
|
417
|
+
const blocked = await h.tools.get('capyclaw_sync').execute({ manifestPath: upgradeManifest, apply: true })
|
|
418
|
+
assert.equal(blocked.mode, 'blocked')
|
|
419
|
+
assert.equal(blocked.ok, false)
|
|
420
|
+
assert.equal(fs.readFileSync(path.join(h.dir, 'package.json'), 'utf8'), before)
|
|
421
|
+
const allowed = await h.tools.get('capyclaw_sync').execute({ manifestPath: upgradeManifest, apply: true, allowUpgrade: true })
|
|
422
|
+
assert.equal(allowed.ok, true)
|
|
423
|
+
assert.equal(h.deps.alpha, '2.0.0')
|
|
424
|
+
h.dispose()
|
|
425
|
+
fs.rmSync(h.dir, { recursive: true, force: true })
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
test('apply blocks missing bundles, reports install/enable failures, and flags restart-required', async () => {
|
|
429
|
+
const h = applyHarness()
|
|
430
|
+
const missing = writeManifest(h.dir, { schemaVersion: 1, packages: [], bundles: ['ghost'] })
|
|
431
|
+
const blocked = await h.tools.get('capyclaw_sync').execute({ manifestPath: missing, apply: true })
|
|
432
|
+
assert.equal(blocked.mode, 'blocked')
|
|
433
|
+
assert.ok(blocked.blockers.some((b) => b.includes('Bundle is not installed: ghost')))
|
|
434
|
+
h.dispose()
|
|
435
|
+
|
|
436
|
+
const failing = applyHarness({ failInstall: true })
|
|
437
|
+
const m1 = writeManifest(failing.dir, { schemaVersion: 1, packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }], bundles: [] })
|
|
438
|
+
const r1 = await failing.tools.get('capyclaw_sync').execute({ manifestPath: m1, apply: true })
|
|
439
|
+
assert.equal(r1.ok, false)
|
|
440
|
+
assert.equal(r1.results[0].error, 'registry unavailable')
|
|
441
|
+
failing.dispose()
|
|
442
|
+
fs.rmSync(failing.dir, { recursive: true, force: true })
|
|
443
|
+
|
|
444
|
+
const throwing = applyHarness({ throwInstall: true })
|
|
445
|
+
const m2 = writeManifest(throwing.dir, { schemaVersion: 1, packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }], bundles: [] })
|
|
446
|
+
const r2 = await throwing.tools.get('capyclaw_sync').execute({ manifestPath: m2, apply: true })
|
|
447
|
+
assert.equal(r2.ok, false)
|
|
448
|
+
assert.match(r2.results[0].error, /install threw/)
|
|
449
|
+
throwing.dispose()
|
|
450
|
+
fs.rmSync(throwing.dir, { recursive: true, force: true })
|
|
451
|
+
|
|
452
|
+
const restarting = applyHarness({ application: 'restart-required' })
|
|
453
|
+
const m3 = writeManifest(restarting.dir, { schemaVersion: 1, packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }], bundles: [] })
|
|
454
|
+
const r3 = await restarting.tools.get('capyclaw_sync').execute({ manifestPath: m3, apply: true })
|
|
455
|
+
assert.equal(r3.restartRequired, true)
|
|
456
|
+
assert.equal(r3.ok, true)
|
|
457
|
+
restarting.dispose()
|
|
458
|
+
fs.rmSync(restarting.dir, { recursive: true, force: true })
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
test('post-install inventory failure is reported honestly instead of crashing or claiming success', async () => {
|
|
462
|
+
const h = applyHarness({ inventoryFailsAfterInstall: true })
|
|
463
|
+
const manifestPath = writeManifest(h.dir, { schemaVersion: 1, packages: [{ name: 'alpha', spec: 'alpha@1.0.0' }], bundles: ['alpha'] })
|
|
464
|
+
const result = await h.tools.get('capyclaw_sync').execute({ manifestPath, apply: true })
|
|
465
|
+
assert.equal(result.ok, false)
|
|
466
|
+
assert.ok(result.results.some((r) => r.ok === false && /inventory/.test(r.error)))
|
|
467
|
+
h.dispose()
|
|
468
|
+
fs.rmSync(h.dir, { recursive: true, force: true })
|
|
469
|
+
|
|
470
|
+
const direct = applyHarness({ inventoryFailsAfterInstall: true })
|
|
471
|
+
const results = await executeEnvironmentPlan(direct.ctx, { manifest: { bundles: [] }, actions: [{ name: 'alpha', spec: 'alpha@1.0.0', action: 'install' }] })
|
|
472
|
+
assert.ok(results.some((r) => r.ok === false && /inventory/.test(r.error)))
|
|
473
|
+
direct.dispose()
|
|
474
|
+
fs.rmSync(direct.dir, { recursive: true, force: true })
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
test('enable failure after successful install is reported as not ok', async () => {
|
|
478
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'capyclaw-stability-'))
|
|
479
|
+
const calls = []
|
|
480
|
+
const ctx = {
|
|
481
|
+
profileContext: { dir },
|
|
482
|
+
tools: { register() { return () => {} } },
|
|
483
|
+
pluginManager: {
|
|
484
|
+
async listBundles() { return [{ name: 'alpha', enabled: false }] },
|
|
485
|
+
async installBundle() { return { application: 'applied' } },
|
|
486
|
+
async setBundleEnabled() { calls.push('enable'); return { application: 'failed', error: 'enable denied' } },
|
|
487
|
+
},
|
|
488
|
+
}
|
|
489
|
+
const results = await executeEnvironmentPlan(ctx, { manifest: { bundles: ['alpha'] }, actions: [] })
|
|
490
|
+
assert.equal(results.length, 1)
|
|
491
|
+
assert.equal(results[0].ok, false)
|
|
492
|
+
assert.equal(results[0].error, 'enable denied')
|
|
493
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
494
|
+
})
|
|
495
|
+
|
|
496
|
+
// ---------- concurrency ----------
|
|
497
|
+
test('40 concurrent status/preview calls stay consistent and read-only', async () => {
|
|
498
|
+
const dir = tmpProfile({ demo: '^1.0.0' }, { demo: '1.0.0' })
|
|
499
|
+
const before = fs.readFileSync(path.join(dir, 'package.json'), 'utf8')
|
|
500
|
+
const h = harness({ dir })
|
|
501
|
+
const jobs = []
|
|
502
|
+
for (let i = 0; i < 40; i++) {
|
|
503
|
+
jobs.push(i % 2 === 0
|
|
504
|
+
? h.tools.get('capyclaw_status').execute({})
|
|
505
|
+
: h.tools.get('capyclaw_sync').execute({ platform: ['windows', 'macos', 'linux'][i % 3] }))
|
|
506
|
+
}
|
|
507
|
+
const results = await Promise.all(jobs)
|
|
508
|
+
for (const [i, r] of results.entries()) {
|
|
509
|
+
if (i % 2 === 0) assert.equal(r.platform, detectPlatform())
|
|
510
|
+
else { assert.equal(r.mode, 'preview'); assert.equal(r.platform, ['windows', 'macos', 'linux'][i % 3]) }
|
|
511
|
+
}
|
|
512
|
+
assert.equal(h.calls.length, 0)
|
|
513
|
+
assert.equal(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'), before)
|
|
514
|
+
h.dispose()
|
|
515
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
test('20 concurrent exports to distinct paths produce identical manifests', async () => {
|
|
519
|
+
const dir = tmpProfile({ a: '^1.0.0' }, { a: '1.2.3' })
|
|
520
|
+
const h = harness({ dir, bundles: [{ name: 'a', enabled: true }] })
|
|
521
|
+
const outs = Array.from({ length: 20 }, (_, i) => path.join(dir, `out-${i}.json`))
|
|
522
|
+
await Promise.all(outs.map((outputPath) => h.tools.get('capyclaw_export_environment').execute({ outputPath })))
|
|
523
|
+
const contents = outs.map((p) => fs.readFileSync(p, 'utf8'))
|
|
524
|
+
assert.ok(contents.every((c) => c === contents[0]))
|
|
525
|
+
h.dispose()
|
|
526
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
// ---------- registration/disposal leak stress ----------
|
|
530
|
+
test('500 apply/dispose cycles leave no registered tools and tolerate double dispose', () => {
|
|
531
|
+
const tools = new Map()
|
|
532
|
+
const ctx = {
|
|
533
|
+
profileContext: { dir: null },
|
|
534
|
+
tools: { register(tool) { tools.set(tool.name, tool); return () => tools.delete(tool.name) } },
|
|
535
|
+
pluginManager: { async listBundles() { return [] } },
|
|
536
|
+
}
|
|
537
|
+
for (let i = 0; i < 500; i++) {
|
|
538
|
+
const dispose = apply(ctx)
|
|
539
|
+
assert.equal(tools.size, 5)
|
|
540
|
+
dispose()
|
|
541
|
+
assert.equal(tools.size, 0)
|
|
542
|
+
dispose() // double dispose must be safe
|
|
543
|
+
assert.equal(tools.size, 0)
|
|
544
|
+
}
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
test('createEnvironmentManifest round-trips through normalize 100 times without drift', () => {
|
|
548
|
+
const packages = [{ name: 'a', spec: 'a@1.0.0' }, { name: '@s/b', spec: '@s/b@2.0.0', platforms: ['linux'], required: false }]
|
|
549
|
+
let current = createEnvironmentManifest({ name: 'round', dshVersion: '0.2.0', packages, bundles: ['a'] })
|
|
550
|
+
for (let i = 0; i < 100; i++) {
|
|
551
|
+
const next = normalizeEnvironmentManifest(JSON.parse(JSON.stringify(current)))
|
|
552
|
+
assert.deepEqual(next, current)
|
|
553
|
+
current = next
|
|
554
|
+
}
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
test('planner protects machine-local dev packages on every platform and even with allowUpgrade', () => {
|
|
558
|
+
const manifest = { schemaVersion: 1, name: 'm', packages: [
|
|
559
|
+
{ name: 'dev', spec: 'dev@1.0.0' },
|
|
560
|
+
{ name: 'fresh', spec: 'fresh@2.0.0' },
|
|
561
|
+
], bundles: [] }
|
|
562
|
+
for (const platform of ['windows', 'macos', 'linux', 'unsupported']) {
|
|
563
|
+
for (const allowUpgrade of [false, true]) {
|
|
564
|
+
const plan = planEnvironment({ manifest, platform, installed: { dev: null }, local: { dev: 'link:C:/dev/checkout' }, allowUpgrade })
|
|
565
|
+
const dev = plan.actions.find((a) => a.name === 'dev')
|
|
566
|
+
assert.equal(dev.action, 'keep', `${platform}/${allowUpgrade}`)
|
|
567
|
+
assert.equal(dev.local, true)
|
|
568
|
+
assert.equal(dev.current, 'link:C:/dev/checkout')
|
|
569
|
+
assert.equal(dev.destructive, false)
|
|
570
|
+
assert.deepEqual(plan.localPackages, [{ name: 'dev', spec: 'link:C:/dev/checkout' }])
|
|
571
|
+
const fresh = plan.actions.find((a) => a.name === 'fresh')
|
|
572
|
+
if (platform === 'unsupported') assert.equal(fresh, undefined)
|
|
573
|
+
else assert.equal(fresh.action, 'install')
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
})
|