skillhub/cli/test/unit/integrations/installed-projection.test.ts
XiaoSeS 6c276d38fc
Some checks are pending
Security / Dependency Review (push) Waiting to run
Security / CodeQL (java-kotlin) (push) Waiting to run
Security / CodeQL (javascript-typescript) (push) Waiting to run
Security / CodeQL (python) (push) Waiting to run
fix(dsh): preserve legacy compatibility and Windows lifecycle safety (#1033)
保持共享库存 v2 与旧版可读普通日志,从真实入口派生启停;隔离扩展日志并保护恢复时的合法后续写入。修复 Windows 库存/日志原子替换及目标锁的短时共享冲突,不删除原数据、不抢占活锁、不重放事务。

补充安全探针诊断与仅公共下载内容的 CI 缓存,Profile 和安装状态仍逐次隔离。准备 CLI 0.3.0 / DSH 0.6.0,npm 发布另行执行。

最终提交的 CLI 三平台、DSH 14 项矩阵及 12 份启停场景摘要均通过。Windows 新 RC 依赖准备仍接近限额,作为 CI 准备稳定性风险保留。

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-10-29 21:08:14 +08:00

86 lines
5.5 KiB
TypeScript

import { expect, test } from 'bun:test'
import { realpath, mkdtemp, mkdir, rm, rename, symlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { readInstalledProjection } from '../../../src/integrations/installed-projection'
import type { InventoryItem } from '../../../src/stores/inventory-store'
const registry = 'https://registry.example'
test('installed projection preserves both scopes and fixed versions without scanning other targets', async () => {
const root = await realpath(await mkdtemp(join(tmpdir(), 'skillhub-projection-')))
try {
const user = join(root, 'user')
const project = join(root, 'project')
const item: InventoryItem = { registry: registry + '/', namespace: 'demo', slug: 'writer', targets: [user, project].map(rootDir => ({ version: '1.2.0', fingerprint: 'original', agent: 'dsh', rootDir,
installDir: join(rootDir, 'writer'), installedAt: '2026-10-14T00:00:00Z' })) }
for (const target of item.targets) {
await mkdir(join(target.installDir, '.skillhub'), { recursive: true })
await writeFile(join(target.installDir, '.skillhub/metadata.json'), JSON.stringify({ ...item, version: target.version, fingerprint: target.fingerprint,
agent: target.agent, installedAt: target.installedAt }))
// No hash claim: local content may differ; presence alone is shown by this projection.
await writeFile(join(target.installDir, 'SKILL.md'), 'locally edited')
}
const inventory = { read: async () => ({ items: [item,
{ ...item, registry: 'https://other.example' },
{ ...item, targets: [{ ...item.targets[0]!, rootDir: join(root, 'other-project'), installDir: '/not-read' }] },
{ ...item, targets: [{ ...item.targets[0]!, agent: 'other-agent' }] },
] }) }
const targets = [{ scope: 'user' as const, root: user }, { scope: 'project' as const, root: project }]
const result = await readInstalledProjection(inventory, registry, targets)
expect(result.complete).toBe(true)
expect(result.issues).toEqual([])
expect(result.items.map(value => [value.scope, value.version, value.state])).toEqual([
['user', '1.2.0', 'present'], ['project', '1.2.0', 'present'],
])
await rm(join(project, 'writer'), { recursive: true })
expect((await readInstalledProjection(inventory, registry, targets)).items[1]!.state).toBe('missing')
await symlink(join(user, 'writer'), join(project, 'writer'))
const linked = await readInstalledProjection(inventory, registry, targets)
expect(linked.complete).toBe(false)
expect(linked.items[1]!.state).toBe('mismatch')
await writeFile(join(user, 'writer/.skillhub/metadata.json'), '{')
expect((await readInstalledProjection(inventory, registry, targets)).items[0]!.state).toBe('metadata-invalid')
} finally { await rm(root, { recursive: true, force: true }) }
})
test('inventory failure is partial and escaping targets are reported without reading outside roots', async () => {
const failed = await readInstalledProjection({ read: async () => { throw new Error('permission denied') } }, registry, [])
expect(failed).toEqual({ items: [], complete: false, issues: ['inventory-unreadable'] })
const trusted = resolve('/trusted')
const item: InventoryItem = { registry, namespace: 'demo', slug: 'writer', targets: [
{ version: '1', agent: 'dsh', rootDir: trusted, installDir: resolve('/untrusted/secret'), installedAt: 'today' },
] }
const result = await readInstalledProjection({ read: async () => ({ items: [item, item] }) }, registry,
[{ scope: 'project', root: trusted }])
expect(result.complete).toBe(false)
expect(result.items).toHaveLength(1)
expect(result.items[0]!.state).toBe('mismatch')
expect(result.items[0]!.targetPath).toBe(join(trusted, 'writer'))
})
test('entry projection rejects missing, ambiguous and unsafe entries without inventing enabled state', async () => {
const root = await realpath(await mkdtemp(join(tmpdir(), 'skillhub-projection-entry-')))
try {
const path = join(root, 'writer')
const target = { version: '1.2.0', fingerprint: 'original', agent: 'dsh', rootDir: root,
installDir: path, installedAt: '2026-10-14T00:00:00Z' }
const item: InventoryItem = { registry, namespace: 'demo', slug: 'writer', targets: [target] }
await mkdir(join(path, '.skillhub'), { recursive: true })
await writeFile(join(path, '.skillhub/metadata.json'), JSON.stringify({ ...item, ...target }))
const inventory = { read: async () => ({ items: [item] }) }
const read = () => readInstalledProjection(inventory, registry, [{ scope: 'project', root }])
for (const mode of ['missing', 'enabled', 'both', 'disabled', 'symlink'] as const) {
if (mode === 'enabled') await writeFile(join(path, 'SKILL.md'), 'content')
if (mode === 'both') await writeFile(join(path, 'SKILL.md.disabled'), 'content')
if (mode === 'disabled') await rm(join(path, 'SKILL.md'))
if (mode === 'symlink') { await rename(join(path, 'SKILL.md.disabled'), join(path, 'other.md')); await symlink(join(path, 'other.md'), join(path, 'SKILL.md')) }
const result = await read()
const valid = mode === 'enabled' || mode === 'disabled'
expect(result.complete).toBe(valid)
expect(result.items[0]!.state).toBe(valid ? 'present' : 'unreadable')
expect(result.items[0]!.installationState).toBe(valid ? mode : undefined)
expect(result.issues).toEqual(valid ? [] : ['target-unreadable'])
}
} finally { await rm(root, { recursive: true, force: true }) }
})