58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
/** @jest-environment node */
|
||
|
||
import { POST } from '@/app/api/materials/upload/chunk/route'
|
||
import { getAuthenticatedUploadUserId } from '@/lib/upload-auth'
|
||
import {
|
||
getUploadSession,
|
||
UploadSessionStoreUnavailableError,
|
||
} from '@/lib/upload-sessions'
|
||
|
||
jest.mock('@/lib/upload-auth', () => ({
|
||
getAuthenticatedUploadUserId: jest.fn(),
|
||
}))
|
||
|
||
jest.mock('@/lib/upload-sessions', () => {
|
||
class MockUploadSessionStoreUnavailableError extends Error {}
|
||
return {
|
||
getUploadSession: jest.fn(),
|
||
updateChunkWithStats: jest.fn(),
|
||
UploadSessionStoreUnavailableError: MockUploadSessionStoreUnavailableError,
|
||
}
|
||
})
|
||
|
||
function createRequest() {
|
||
const headers = new Map<string, string>([
|
||
['content-type', 'application/octet-stream'],
|
||
['x-upload-id', 'upload-active'],
|
||
['x-chunk-index', '0'],
|
||
['x-file-name', 'source.psd'],
|
||
['x-chunk-size', '4'],
|
||
])
|
||
return {
|
||
headers: { get: (name: string) => headers.get(name.toLowerCase()) || null },
|
||
nextUrl: { searchParams: new URLSearchParams() },
|
||
} as any
|
||
}
|
||
|
||
describe('/api/materials/upload/chunk Redis availability', () => {
|
||
beforeEach(() => {
|
||
jest.clearAllMocks()
|
||
;(getAuthenticatedUploadUserId as jest.Mock).mockResolvedValue('user-1')
|
||
})
|
||
|
||
test('会话存储不可用时返回可重试 503,而不是 404', async () => {
|
||
;(getUploadSession as jest.Mock).mockRejectedValue(
|
||
new UploadSessionStoreUnavailableError('redis unavailable'),
|
||
)
|
||
|
||
const response = await POST(createRequest())
|
||
|
||
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',
|
||
})
|
||
})
|
||
})
|