82 lines
3.6 KiB
TypeScript
82 lines
3.6 KiB
TypeScript
/** @jest-environment node */
|
|
import { findPurgeFileReferences, purgeFileIdentity, removePurgeFile, type PurgeFile } from '@/lib/material-purge-files'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { deleteStoredUploadFileFromRemote } from '@/lib/remote-upload-sync'
|
|
import { processNextTaskDetailZipCleanup } from '@/lib/task-detail-zip-cleanup'
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
$queryRaw: jest.fn(),
|
|
taskDetailZipCleanup: { updateMany: jest.fn(), deleteMany: jest.fn() },
|
|
},
|
|
}))
|
|
jest.mock('@/lib/material-purge-files', () => ({
|
|
...jest.requireActual('@/lib/material-purge-files'),
|
|
findPurgeFileReferences: jest.fn(),
|
|
removePurgeFile: jest.fn(),
|
|
}))
|
|
jest.mock('@/lib/remote-upload-sync', () => ({ deleteStoredUploadFileFromRemote: jest.fn() }))
|
|
|
|
function archive(name: string): PurgeFile {
|
|
return { storage: 'local', key: 'materials/' + name, source: 'uploads/materials/' + name, outcome: 'pending' }
|
|
}
|
|
|
|
const claim = prisma.$queryRaw as jest.Mock
|
|
const references = findPurgeFileReferences as jest.Mock
|
|
const remove = removePurgeFile as jest.Mock
|
|
const deleteRemote = deleteStoredUploadFileFromRemote as jest.Mock
|
|
|
|
beforeEach(() => {
|
|
jest.resetAllMocks()
|
|
references.mockResolvedValue(new Set())
|
|
remove.mockResolvedValue(undefined)
|
|
deleteRemote.mockResolvedValue(undefined)
|
|
})
|
|
|
|
test('保留仍被引用的旧包,并重试已经删掉本地副本的远程清理', async () => {
|
|
const shared = archive('shared.zip')
|
|
const unused = archive('unused.zip')
|
|
const remoteRetry = archive('remote-retry.zip')
|
|
claim.mockResolvedValueOnce([{ id: 'cleanup-id', attempts: 1, files: [shared, unused, remoteRetry] }])
|
|
references.mockResolvedValueOnce(new Set([purgeFileIdentity(shared)]))
|
|
deleteRemote.mockImplementationOnce(async () => undefined).mockRejectedValueOnce(new Error('remote temporarily unavailable'))
|
|
const warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined)
|
|
try {
|
|
await expect(processNextTaskDetailZipCleanup('cleanup-id')).resolves.toBe(true)
|
|
expect(remove).not.toHaveBeenCalledWith(shared)
|
|
expect(remove).toHaveBeenCalledWith(unused)
|
|
expect(remove).toHaveBeenCalledWith(remoteRetry)
|
|
expect(prisma.taskDetailZipCleanup.updateMany).toHaveBeenCalledWith({
|
|
where: { id: 'cleanup-id', attempts: 1 },
|
|
data: { files: [shared, remoteRetry], nextAttemptAt: expect.any(Date) },
|
|
})
|
|
|
|
claim.mockResolvedValueOnce([{ id: 'cleanup-id', attempts: 2, files: [shared, remoteRetry] }])
|
|
await expect(processNextTaskDetailZipCleanup()).resolves.toBe(true)
|
|
expect(references).toHaveBeenLastCalledWith(prisma, [shared, remoteRetry])
|
|
expect(remove).toHaveBeenCalledWith(shared)
|
|
expect(deleteRemote).toHaveBeenLastCalledWith(remoteRetry.source)
|
|
expect(prisma.taskDetailZipCleanup.deleteMany).toHaveBeenCalledWith({ where: { id: 'cleanup-id', attempts: 2 } })
|
|
} finally {
|
|
warn.mockRestore()
|
|
}
|
|
})
|
|
|
|
test('引用查询失败时保留任务,跳过所有物理删除', async () => {
|
|
const file = archive('keep-until-verified.zip')
|
|
claim.mockResolvedValue([{ id: 'cleanup-id', attempts: 2, files: [file] }])
|
|
references.mockRejectedValue(new Error('database unavailable'))
|
|
const warn = jest.spyOn(console, 'warn').mockImplementation(() => undefined)
|
|
try {
|
|
await expect(processNextTaskDetailZipCleanup()).resolves.toBe(true)
|
|
expect(remove).not.toHaveBeenCalled()
|
|
expect(deleteRemote).not.toHaveBeenCalled()
|
|
expect(prisma.taskDetailZipCleanup.updateMany).toHaveBeenCalledWith({
|
|
where: { id: 'cleanup-id', attempts: 2 },
|
|
data: { files: [file], nextAttemptAt: expect.any(Date) },
|
|
})
|
|
} finally {
|
|
warn.mockRestore()
|
|
}
|
|
})
|