skillhub/cli/test/unit/integrations/removal-inventory.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

56 lines
3.4 KiB
TypeScript

import { expect, test } from 'bun:test'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { commitRemovalInventory } from '../../../src/integrations/removal-inventory'
import type { RemovalBaseline } from '../../../src/integrations/native-contract'
import { InventoryStore } from '../../../src/stores/inventory-store'
test('removal commits exactly one target, journals before writing and preserves unrelated records', async () => {
const home = await mkdtemp(join(tmpdir(), 'skillhub-removal-inventory-'))
try {
const store = new InventoryStore(home)
const root = join(home, 'skills')
const target = { version: '1.0.0', fingerprint: 'fingerprint', agent: 'dsh', rootDir: root, installDir: join(root, 'writer'), installedAt: '2026-10-14' }
const retained = { ...target, agent: 'other', rootDir: join(home, 'other'), installDir: join(home, 'other/writer') }
const item = { registry: 'https://registry.example', namespace: 'demo', slug: 'writer', targets: [target, retained] }
const other = { ...item, slug: 'reader', targets: [{ ...target, installDir: join(root, 'reader') }] }
const initial = { items: [item, other] }
const baseline: Extract<RemovalBaseline, { status: 'ready' }> = { status: 'ready', kind: 'remove-stale-record',
inventoryItem: { ...item, targets: [target] }, target: { scope: 'user', root, path: target.installDir } }
await store.writeAtomic(initial)
let calls = 0
await commitRemovalInventory(store, baseline, async (before, after) => {
calls++
expect(await store.read()).toEqual(before)
expect(before).toEqual(initial)
expect(after.items).toEqual([{ ...item, targets: [retained] }, other])
})
expect(calls).toBe(1)
expect((await store.read()).items).toEqual([{ ...item, targets: [retained] }, other])
await expect(commitRemovalInventory(store, baseline, async () => {})).rejects.toThrow('inventory changed')
await store.writeAtomic(initial)
await expect(commitRemovalInventory(store, baseline, async () => { throw new Error('journal failed') })).rejects.toThrow('journal failed')
expect(await store.read()).toEqual(initial)
const occupied = { items: [...initial.items, { ...item, registry: 'https://other.example', targets: [target] }] }
await expect(store.writeAtomic(occupied)).rejects.toThrow('Ambiguous inventory target ownership')
expect(await store.read()).toEqual(initial)
const changed = { items: [{ ...item, targets: item.targets.map(t => ({ ...t, version: '2.0.0' })) }, other] }
await store.writeAtomic(changed)
await expect(commitRemovalInventory(store, baseline, async () => {})).rejects.toThrow('inventory changed')
expect(await store.read()).toEqual(changed)
const advancedSibling = { ...retained, version: '2.0.0', fingerprint: 'new-sibling' }
await store.writeAtomic({ items: [{ ...item, targets: [target, advancedSibling] }, other] })
await commitRemovalInventory(store, baseline, async () => {})
expect((await store.read()).items).toEqual([{ ...item, targets: [advancedSibling] }, other])
const single = { ...item, targets: [target] }
await store.writeAtomic({ items: [single, other] })
await commitRemovalInventory(store, { ...baseline, inventoryItem: single }, async () => {})
expect((await store.read()).items).toEqual([other])
} finally { await rm(home, { recursive: true, force: true }) }
})