48 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** @jest-environment node */
import { POST } from '@/app/api/materials/upload/complete/route'
import { getServerSession } from 'next-auth'
import {
getUploadSession,
UploadSessionStoreUnavailableError,
} from '@/lib/upload-sessions'
jest.mock('next-auth', () => ({ getServerSession: jest.fn() }))
jest.mock('@/lib/auth', () => ({ authOptions: {} }))
jest.mock('@/lib/image-processor', () => ({ sanitizeFileName: (value: string) => value }))
jest.mock('@/lib/upload-sessions', () => {
class MockUploadSessionStoreUnavailableError extends Error {}
return {
getUploadSession: jest.fn(),
UploadSessionStoreUnavailableError: MockUploadSessionStoreUnavailableError,
}
})
describe('/api/materials/upload/complete Redis availability', () => {
beforeEach(() => {
jest.clearAllMocks()
;(getServerSession as jest.Mock).mockResolvedValue({ user: { id: 'user-1' } })
})
test('会话存储不可用时返回可重试 503,而不是 404', async () => {
;(getUploadSession as jest.Mock).mockRejectedValue(
new UploadSessionStoreUnavailableError('redis unavailable'),
)
const request = {
json: jest.fn().mockResolvedValue({
uploadId: 'upload-active',
originalFileName: 'source.psd',
}),
} as any
const response = await POST(request)
expect(response.status).toBe(503)
expect(response.headers.get('retry-after')).toBe('3')
await expect(response.json()).resolves.toEqual({
error: '上传会话服务暂时不可用,已保留上传进度,请稍后重试',
code: 'UPLOAD_SESSION_STORE_UNAVAILABLE',
})
})
})