@capyclaw/capyclaw 0.0.0-stage → 0.2.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 (81) hide show
  1. package/README.md +22 -2
  2. package/cordis.patch.yml +5 -0
  3. package/icon.svg +5 -0
  4. package/index.js +178 -0
  5. package/lib/environment.mjs +122 -0
  6. package/locale/en.json +6 -0
  7. package/locale/zh.json +6 -0
  8. package/manifest.js +18 -0
  9. package/node_modules/@capyclaw/foundation/README.md +51 -0
  10. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  11. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  12. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  13. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  14. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  15. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  16. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  17. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  18. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  19. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  20. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  21. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  22. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  31. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  32. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  33. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  34. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  35. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  36. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  37. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  38. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  39. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  40. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  41. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  42. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  43. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  44. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  45. package/node_modules/@capyclaw/foundation/package.json +53 -0
  46. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  47. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  48. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  49. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  50. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  51. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  73. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  74. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  75. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  76. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  77. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  78. package/package.json +71 -4
  79. package/test/environment.test.mjs +68 -0
  80. package/test/package.test.mjs +24 -0
  81. package/test/plugin.test.mjs +101 -0
@@ -0,0 +1,181 @@
1
+ /**
2
+ * 极简跨平台 ZIP 读写(store + deflate 读,store 写;无外部依赖)——全仓单一真源。
3
+ *
4
+ * 之前三处实现各持一份(plugin-development 写 / tools-plugin-release 读 /
5
+ * @capyclaw/suite-kit zip-read 读),本模块合并之。旧位置保留兼容 shim。
6
+ *
7
+ * 写入:store 模式(内容不压缩,跨平台稳定,产物可被任何标准读取器读)。
8
+ * 读取:支持 store(0) 与 deflate(8);接受 Buffer 或文件路径——本地文件与
9
+ * 远端下载源共用同一条校验/解包路径。
10
+ */
11
+ import fs from 'node:fs'
12
+ import path from 'node:path'
13
+ import zlib from 'node:zlib'
14
+
15
+ /* ------------------------------------------------------------------ 写入 */
16
+
17
+ const CRC_TABLE = (() => {
18
+ const table = new Uint32Array(256)
19
+ for (let n = 0; n < 256; n++) {
20
+ let c = n
21
+ for (let k = 0; k < 8; k++) c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1)
22
+ table[n] = c >>> 0
23
+ }
24
+ return table
25
+ })()
26
+
27
+ function crc32(buffer) {
28
+ let crc = 0xffffffff
29
+ for (let i = 0; i < buffer.length; i++) crc = CRC_TABLE[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8)
30
+ return (crc ^ 0xffffffff) >>> 0
31
+ }
32
+
33
+ function dosDateTime(date = new Date()) {
34
+ const year = Math.max(1980, date.getFullYear())
35
+ const time = ((date.getHours() & 0x1f) << 11) | ((date.getMinutes() & 0x3f) << 5) | (Math.floor(date.getSeconds() / 2) & 0x1f)
36
+ const day = (((year - 1980) & 0x7f) << 9) | (((date.getMonth() + 1) & 0x0f) << 5) | (date.getDate() & 0x1f)
37
+ return { time, day }
38
+ }
39
+
40
+ function walkFiles(dir, base = dir) {
41
+ const out = []
42
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
43
+ const full = path.join(dir, entry.name)
44
+ if (entry.isDirectory()) out.push(...walkFiles(full, base))
45
+ else if (entry.isFile()) out.push(path.relative(base, full).replace(/\\/g, '/'))
46
+ }
47
+ return out.sort()
48
+ }
49
+
50
+ /** 把目录写成 store 模式 ZIP。返回打包的相对路径列表。 */
51
+ export function writeZipFromDir(sourceDir, outFile) {
52
+ const files = walkFiles(sourceDir)
53
+ const now = dosDateTime()
54
+ const localParts = []
55
+ const centralParts = []
56
+ let offset = 0
57
+
58
+ for (const name of files) {
59
+ const data = fs.readFileSync(path.join(sourceDir, name))
60
+ const nameBytes = Buffer.from(name, 'utf8')
61
+ const crc = crc32(data)
62
+
63
+ const local = Buffer.alloc(30)
64
+ local.writeUInt32LE(0x04034b50, 0)
65
+ local.writeUInt16LE(20, 4)
66
+ local.writeUInt16LE(0x0800, 6)
67
+ local.writeUInt16LE(0, 8)
68
+ local.writeUInt16LE(now.time, 10)
69
+ local.writeUInt16LE(now.day, 12)
70
+ local.writeUInt32LE(crc, 14)
71
+ local.writeUInt32LE(data.length, 18)
72
+ local.writeUInt32LE(data.length, 22)
73
+ local.writeUInt16LE(nameBytes.length, 26)
74
+ local.writeUInt16LE(0, 28)
75
+ localParts.push(local, nameBytes, data)
76
+
77
+ const central = Buffer.alloc(46)
78
+ central.writeUInt32LE(0x02014b50, 0)
79
+ central.writeUInt16LE(20, 4)
80
+ central.writeUInt16LE(20, 6)
81
+ central.writeUInt16LE(0x0800, 8)
82
+ central.writeUInt16LE(0, 10)
83
+ central.writeUInt16LE(now.time, 12)
84
+ central.writeUInt16LE(now.day, 14)
85
+ central.writeUInt32LE(crc, 16)
86
+ central.writeUInt32LE(data.length, 20)
87
+ central.writeUInt32LE(data.length, 24)
88
+ central.writeUInt16LE(nameBytes.length, 28)
89
+ central.writeUInt16LE(0, 30)
90
+ central.writeUInt16LE(0, 32)
91
+ central.writeUInt16LE(0, 34)
92
+ central.writeUInt16LE(0, 36)
93
+ central.writeUInt32LE(0, 38)
94
+ central.writeUInt32LE(offset, 42)
95
+ centralParts.push(central, nameBytes)
96
+
97
+ offset += local.length + nameBytes.length + data.length
98
+ }
99
+
100
+ const centralBuffer = Buffer.concat(centralParts)
101
+ const eocd = Buffer.alloc(22)
102
+ eocd.writeUInt32LE(0x06054b50, 0)
103
+ eocd.writeUInt16LE(0, 4)
104
+ eocd.writeUInt16LE(0, 6)
105
+ eocd.writeUInt16LE(files.length, 8)
106
+ eocd.writeUInt16LE(files.length, 10)
107
+ eocd.writeUInt32LE(centralBuffer.length, 12)
108
+ eocd.writeUInt32LE(offset, 16)
109
+ eocd.writeUInt16LE(0, 20)
110
+
111
+ fs.mkdirSync(path.dirname(outFile), { recursive: true })
112
+ fs.writeFileSync(outFile, Buffer.concat([...localParts, centralBuffer, eocd]))
113
+ return files
114
+ }
115
+
116
+ /* ------------------------------------------------------------------ 读取 */
117
+
118
+ function locateEocd(data, label) {
119
+ for (let i = data.length - 22; i >= 0; i--) {
120
+ if (data.readUInt32LE(i) === 0x06054b50) return i
121
+ }
122
+ throw new Error(`不是有效的 ZIP: ${label}`)
123
+ }
124
+
125
+ /**
126
+ * 列出 ZIP 全部条目名。
127
+ * @param {Buffer|string} input - ZIP 字节或文件路径
128
+ */
129
+ export function listZipEntryNames(input, label = 'zip') {
130
+ const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
131
+ const eocd = locateEocd(data, label)
132
+ const count = data.readUInt16LE(eocd + 10)
133
+ let offset = data.readUInt32LE(eocd + 16)
134
+ const names = []
135
+ for (let i = 0; i < count; i++) {
136
+ if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
137
+ const nameLength = data.readUInt16LE(offset + 28)
138
+ const extraLength = data.readUInt16LE(offset + 30)
139
+ const commentLength = data.readUInt16LE(offset + 32)
140
+ names.push(data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8'))
141
+ offset += 46 + nameLength + extraLength + commentLength
142
+ }
143
+ return names
144
+ }
145
+
146
+ /**
147
+ * 从 ZIP 读单个条目的原始字节;不存在返回 undefined。
148
+ * @param {Buffer|string} input - ZIP 字节或文件路径
149
+ */
150
+ export function readZipEntryBuffer(input, entryName, label = 'zip') {
151
+ const data = Buffer.isBuffer(input) ? input : fs.readFileSync(input)
152
+ const eocd = locateEocd(data, label)
153
+ const count = data.readUInt16LE(eocd + 10)
154
+ let offset = data.readUInt32LE(eocd + 16)
155
+ for (let i = 0; i < count; i++) {
156
+ if (data.readUInt32LE(offset) !== 0x02014b50) throw new Error(`ZIP 中央目录损坏: ${label}`)
157
+ const method = data.readUInt16LE(offset + 10)
158
+ const compSize = data.readUInt32LE(offset + 20)
159
+ const nameLength = data.readUInt16LE(offset + 28)
160
+ const extraLength = data.readUInt16LE(offset + 30)
161
+ const commentLength = data.readUInt16LE(offset + 32)
162
+ const localOffset = data.readUInt32LE(offset + 42)
163
+ const name = data.subarray(offset + 46, offset + 46 + nameLength).toString('utf8')
164
+ if (name === entryName) {
165
+ const localNameLength = data.readUInt16LE(localOffset + 26)
166
+ const localExtraLength = data.readUInt16LE(localOffset + 28)
167
+ const start = localOffset + 30 + localNameLength + localExtraLength
168
+ const body = data.subarray(start, start + compSize)
169
+ if (method === 0) return body
170
+ if (method === 8) return zlib.inflateRawSync(body)
171
+ throw new Error(`不支持的 ZIP 压缩方式 ${method}: ${label}`)
172
+ }
173
+ offset += 46 + nameLength + extraLength + commentLength
174
+ }
175
+ return undefined
176
+ }
177
+
178
+ /** 读取 ZIP 中央目录条目名(文件路径版;打包自检/测试用)。 */
179
+ export function listZipEntries(file) {
180
+ return listZipEntryNames(fs.readFileSync(file), file)
181
+ }
package/package.json CHANGED
@@ -1,6 +1,73 @@
1
1
  {
2
2
  "name": "@capyclaw/capyclaw",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Portable DSH environment setup and profile migration for Windows, macOS, and Linux.",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./runtime": "./index.js",
10
+ "./manifest": "./manifest.js",
11
+ "./environment": "./lib/environment.mjs",
12
+ "./locale/*.json": "./locale/*.json",
13
+ "./icon": "./icon.svg",
14
+ "./package.json": "./package.json"
15
+ },
16
+ "dsh": {
17
+ "bundle": {
18
+ "patch": "./cordis.patch.yml"
19
+ },
20
+ "category": "tool",
21
+ "platform": "both"
22
+ },
23
+ "meta": {
24
+ "title": "CapyClaw environment manager",
25
+ "description": "Detects Windows, macOS, or Linux and previews or applies a portable DSH profile environment. Supports export, import, platform-specific packages, profile backup, and post-install verification."
26
+ },
27
+ "files": [
28
+ "index.js",
29
+ "manifest.js",
30
+ "lib/",
31
+ "test/",
32
+ "locale/",
33
+ "icon.svg",
34
+ "cordis.patch.yml",
35
+ "README.md"
36
+ ],
37
+ "scripts": {
38
+ "test": "node --test test/*.test.mjs",
39
+ "check": "node --check index.js && node --check lib/environment.mjs"
40
+ },
41
+ "dependencies": {
42
+ "@capyclaw/foundation": "^0.1.10",
43
+ "@capyclaw/plugin-kit": "^0.2.7"
44
+ },
45
+ "engines": {
46
+ "node": ">=18",
47
+ "dsh": "^0.2.0-rc.2"
48
+ },
49
+ "license": "MIT",
50
+ "keywords": [
51
+ "dsh",
52
+ "dsh-plugin",
53
+ "environment",
54
+ "migration",
55
+ "cross-platform"
56
+ ],
57
+ "author": {
58
+ "name": "CapyClaw",
59
+ "url": "https://github.com/capyclaw"
60
+ },
61
+ "repository": {
62
+ "type": "git",
63
+ "url": "git+https://github.com/Maple496/capyclaw.git",
64
+ "directory": "B/official/capyclaw"
65
+ },
66
+ "publishConfig": {
67
+ "access": "public"
68
+ },
69
+ "bundledDependencies": [
70
+ "@capyclaw/foundation",
71
+ "@capyclaw/plugin-kit"
72
+ ]
73
+ }
@@ -0,0 +1,68 @@
1
+ import test from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { createEnvironmentManifest, detectPlatform, normalizeEnvironmentManifest, planEnvironment } from '../lib/environment.mjs'
4
+
5
+ const manifest = {
6
+ schemaVersion: 1,
7
+ name: 'portable',
8
+ dsh: {},
9
+ packages: [
10
+ { name: 'common', spec: 'common@1.0.0' },
11
+ { name: 'win-only', spec: 'win-only@1.0.0', platforms: ['windows'] },
12
+ ],
13
+ bundles: [],
14
+ }
15
+
16
+ test('detects supported desktop operating systems', () => {
17
+ assert.equal(detectPlatform('win32'), 'windows')
18
+ assert.equal(detectPlatform('darwin'), 'macos')
19
+ assert.equal(detectPlatform('linux'), 'linux')
20
+ assert.equal(detectPlatform('freebsd'), 'unsupported')
21
+ })
22
+
23
+ test('plans only packages for the detected target platform', () => {
24
+ const plan = planEnvironment({ manifest, platform: 'macos' })
25
+ assert.deepEqual(plan.actions.map((item) => item.name), ['common'])
26
+ assert.equal(plan.skipped[0].name, 'win-only')
27
+ assert.equal(plan.ready, true)
28
+ })
29
+
30
+ test('requires explicit permission before changing installed versions', () => {
31
+ const blocked = planEnvironment({ manifest, platform: 'linux', installed: { common: 'common@0.9.0' } })
32
+ assert.equal(blocked.ready, false)
33
+ assert.equal(blocked.actions[0].action, 'version-change')
34
+ const allowed = planEnvironment({ manifest, platform: 'linux', installed: { common: 'common@0.9.0' }, allowUpgrade: true })
35
+ assert.equal(allowed.ready, true)
36
+ })
37
+
38
+ test('compares actual installed versions with exact requested versions', () => {
39
+ const ranged = { ...manifest, packages: [{ name: 'common', spec: 'common@^1.0.0' }] }
40
+ const current = planEnvironment({ manifest: ranged, platform: 'linux', installed: { common: 'common@1.2.3' } })
41
+ assert.equal(current.actions[0].action, 'version-change')
42
+ assert.equal(current.ready, false)
43
+ const exact = planEnvironment({ manifest, platform: 'linux', installed: { common: 'common@1.0.0' } })
44
+ assert.equal(exact.actions[0].action, 'keep')
45
+ })
46
+
47
+ test('validates exact semver forms including prereleases', () => {
48
+ const prerelease = { ...manifest, packages: [{ name: 'common', spec: 'common@1.0.0-rc.2' }] }
49
+ assert.equal(planEnvironment({ manifest: prerelease, installed: { common: 'common@1.0.0-rc.2' } }).actions[0].action, 'keep')
50
+ const invalid = { ...manifest, packages: [{ name: 'common', spec: 'common@01.0.0' }] }
51
+ assert.equal(planEnvironment({ manifest: invalid, installed: { common: 'common@1.0.0' } }).actions[0].action, 'version-change')
52
+ })
53
+
54
+ test('rejects malformed manifests and duplicate package names', () => {
55
+ assert.throws(() => normalizeEnvironmentManifest({ schemaVersion: 2, packages: [] }), /schemaVersion/)
56
+ assert.throws(() => normalizeEnvironmentManifest({ ...manifest, packages: [manifest.packages[0], manifest.packages[0]] }), /Duplicate/)
57
+ const ranged = { ...manifest, packages: [{ name: 'common', spec: 'common@^1.0.0' }] }
58
+ const blocked = planEnvironment({ manifest: ranged, platform: 'linux' })
59
+ assert.equal(blocked.ready, false)
60
+ assert.match(blocked.blockers[0], /exact package@version/)
61
+ })
62
+
63
+ test('creates portable manifests without machine paths or host-derived names', () => {
64
+ const exported = createEnvironmentManifest({ packages: [{ name: 'pkg', spec: 'pkg@1.2.3' }], platform: 'linux' })
65
+ assert.equal(exported.name, 'Exported DSH profile')
66
+ assert.equal(exported.packages[0].platforms.includes('linux'), true)
67
+ assert.equal(JSON.stringify(exported).includes('C:\\Users'), false)
68
+ })
@@ -0,0 +1,24 @@
1
+ import test from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import fs from 'node:fs'
4
+ import path from 'node:path'
5
+ import { fileURLToPath } from 'node:url'
6
+
7
+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
8
+
9
+ test('DSH bundle metadata points at a package-root apply plugin entry', () => {
10
+ const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
11
+ assert.equal(pkg.dsh.platform, 'both')
12
+ assert.equal(pkg.main, './index.js')
13
+ assert.equal(pkg.exports['.'], './index.js')
14
+ assert.equal(fs.existsSync(path.join(root, pkg.main)), true)
15
+ })
16
+
17
+ test('starter manifest contains no host-specific paths and only exact registry pins', async () => {
18
+ const { MANIFEST } = await import('../manifest.js')
19
+ assert.equal(JSON.stringify(MANIFEST).includes('C:\\Users\\'), false)
20
+ for (const item of MANIFEST.packages) {
21
+ assert.match(item.spec, new RegExp(`^${item.name.replace(/[.*+?^${}()|[\\]\\]/g, '\\$&')}@\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?$`))
22
+ assert.equal(item.required, false)
23
+ }
24
+ })
@@ -0,0 +1,101 @@
1
+ import test from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { apply, executeEnvironmentPlan } from '../index.js'
4
+
5
+ function harness({ bundles = [], installResult = { application: 'applied' } } = {}) {
6
+ const tools = new Map()
7
+ const calls = []
8
+ const ctx = {
9
+ profileContext: { dir: null },
10
+ tools: { register(tool) { tools.set(tool.name, tool); return () => tools.delete(tool.name) } },
11
+ pluginManager: {
12
+ async listBundles() { return bundles },
13
+ async installBundle(...args) { calls.push(['install', ...args]); return installResult },
14
+ async setBundleEnabled(...args) { calls.push(['enable', ...args]); return { application: 'applied' } },
15
+ },
16
+ }
17
+ const dispose = apply(ctx)
18
+ return { tools, calls, dispose, ctx }
19
+ }
20
+
21
+ test('registers portable tools and defaults sync to read-only preview', async () => {
22
+ const h = harness()
23
+ const result = await h.tools.get('capyclaw_sync').execute({ platform: 'linux' })
24
+ assert.equal(result.mode, 'preview')
25
+ assert.equal(result.platform, 'linux')
26
+ assert.equal(h.calls.length, 0)
27
+ h.dispose()
28
+ assert.equal(h.tools.size, 0)
29
+ })
30
+
31
+ test('explicit apply installs the planned package through the native manager', async () => {
32
+ const h = harness()
33
+ const plan = {
34
+ manifest: { packages: [], bundles: [] },
35
+ actions: [{ name: 'demo', spec: 'demo@1.2.3', action: 'install' }],
36
+ }
37
+ const results = await executeEnvironmentPlan(h.ctx, plan)
38
+ assert.equal(results[0].ok, true)
39
+ assert.equal(h.calls[0][0], 'install')
40
+ assert.equal(h.calls[0][1], 'demo@1.2.3')
41
+ assert.deepEqual(h.calls[0][2].expected, { name: 'demo', version: '1.2.3' })
42
+ })
43
+
44
+ test('does not re-enable bundles that are already enabled', async () => {
45
+ const h = harness({ bundles: [{ name: 'already-on', enabled: true }] })
46
+ const results = await executeEnvironmentPlan(h.ctx, {
47
+ manifest: { bundles: ['already-on'] }, actions: [],
48
+ })
49
+ assert.deepEqual(results, [])
50
+ assert.deepEqual(h.calls, [])
51
+ })
52
+
53
+ test('reports native installation failure rather than claiming success', async () => {
54
+ const h = harness({ installResult: { application: 'failed', error: 'registry unavailable' } })
55
+ const results = await executeEnvironmentPlan(h.ctx, {
56
+ manifest: { bundles: [] },
57
+ actions: [{ name: 'demo', spec: 'demo@1.2.3', action: 'install' }],
58
+ })
59
+ assert.equal(results[0].ok, false)
60
+ assert.equal(results[0].error, 'registry unavailable')
61
+ })
62
+
63
+ function statefulHarness({ installEnables }) {
64
+ const calls = []
65
+ let bundles = []
66
+ const ctx = {
67
+ profileContext: { dir: null },
68
+ tools: { register() { return () => {} } },
69
+ pluginManager: {
70
+ async listBundles() { return bundles },
71
+ async installBundle(spec) {
72
+ calls.push(['install', spec])
73
+ if (installEnables) bundles = [{ name: 'demo', enabled: true }]
74
+ return { application: 'applied' }
75
+ },
76
+ async setBundleEnabled(name, on) { calls.push(['enable', name, on]); return { application: 'applied' } },
77
+ },
78
+ }
79
+ return { ctx, calls }
80
+ }
81
+
82
+ test('installs a package that is also a selected bundle and does not double-enable it', async () => {
83
+ const { ctx, calls } = statefulHarness({ installEnables: true })
84
+ const results = await executeEnvironmentPlan(ctx, {
85
+ manifest: { bundles: ['demo'] },
86
+ actions: [{ name: 'demo', spec: 'demo@1.2.3', action: 'install' }],
87
+ })
88
+ assert.equal(results.length, 1)
89
+ assert.equal(results[0].action, 'install')
90
+ assert.deepEqual(calls, [['install', 'demo@1.2.3']])
91
+ })
92
+
93
+ test('enables a selected bundle when installation did not already enable it', async () => {
94
+ const { ctx, calls } = statefulHarness({ installEnables: false })
95
+ const results = await executeEnvironmentPlan(ctx, {
96
+ manifest: { bundles: ['demo'] },
97
+ actions: [{ name: 'demo', spec: 'demo@1.2.3', action: 'install' }],
98
+ })
99
+ assert.deepEqual(calls, [['install', 'demo@1.2.3'], ['enable', 'demo', true]])
100
+ assert.equal(results.some((r) => r.action === 'enable' && r.ok), true)
101
+ })