fix: harden scoped tools and settings persistence

This commit is contained in:
lofyer
2026-08-13 14:56:53 +08:00
parent bf1ec5d2f1
commit aab961226f
62 changed files with 4343 additions and 1314 deletions
+50 -1
View File
@@ -1,5 +1,9 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { waitForCleanup } from './shutdown'
import {
runCleanupBeforeDeadline,
settleCleanupPhases,
waitForCleanup
} from './shutdown'
afterEach(() => {
vi.useRealTimers()
@@ -23,4 +27,49 @@ describe('waitForCleanup', () => {
await expect(result).resolves.toBe(false)
})
it('runs dependent cleanup phases in order despite failures', async () => {
const order: string[] = []
await settleCleanupPhases([
[
async () => {
order.push('ipc')
throw new Error('cleanup failed')
}
],
[
async () => {
order.push('gateway')
}
],
[
async () => {
order.push('knowledge')
}
]
])
expect(order).toEqual(['ipc', 'gateway', 'knowledge'])
})
it('finalizes databases only after cleanup beats the deadline', async () => {
const finalize = vi.fn()
await expect(
runCleanupBeforeDeadline(Promise.resolve(), 100, finalize)
).resolves.toBe(true)
expect(finalize).toHaveBeenCalledOnce()
vi.useFakeTimers()
const timedOutFinalize = vi.fn()
const result = runCleanupBeforeDeadline(
new Promise(() => {}),
100,
timedOutFinalize
)
await vi.advanceTimersByTimeAsync(100)
await expect(result).resolves.toBe(false)
expect(timedOutFinalize).not.toHaveBeenCalled()
})
})