skillhub/cli/test/unit/platform/atomic-rename.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

29 lines
1.5 KiB
TypeScript

import { expect, test } from 'bun:test'
import { mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { renameAtomicFile } from '../../../src/platform/atomic-rename'
test.each(['EPERM', 'EBUSY'])('Windows atomic rename retries transient %s without removing the destination', async code => {
const root = await mkdtemp(join(tmpdir(), 'skillhub-atomic-rename-'))
const from = join(root, 'temporary'); const to = join(root, 'inventory')
try {
await writeFile(from, 'next'); await writeFile(to, 'original')
let calls = 0
await renameAtomicFile(from, to, async (source, destination) => {
expect(await readFile(to, 'utf8')).toBe('original')
expect(await readFile(from, 'utf8')).toBe('next')
if (++calls < 3) throw Object.assign(new Error('Sharing failure'), { code })
await rename(source, destination)
}, 'win32')
expect(calls).toBe(3)
expect(await readFile(to, 'utf8')).toBe('next')
} finally { await rm(root, { recursive: true, force: true }) }
})
test.each([['linux', 'EPERM'], ['darwin', 'EBUSY'], ['win32', 'EACCES'], ['win32', 'ENOENT']] as const)(
'%s does not retry %s outside the Windows sharing-failure boundary', async (platform, code) => {
const error = Object.assign(new Error('Permanent failure'), { code }); let calls = 0
await expect(renameAtomicFile('source', 'destination', async () => { calls++; throw error }, platform)).rejects.toBe(error)
expect(calls).toBe(1)
})