428 lines
12 KiB
TypeScript
428 lines
12 KiB
TypeScript
import { chunkUpload, getAllPendingUploads } from '@/lib/chunk-upload'
|
|
import { getOptimalConcurrency } from '@/lib/chunk-upload-config'
|
|
|
|
jest.mock('@/lib/chunk-upload-config', () => ({
|
|
getOptimalChunkSize: jest.fn().mockResolvedValue(1024 * 1024),
|
|
getOptimalConcurrency: jest.fn().mockResolvedValue(1),
|
|
getChunkTimeout: jest.fn(() => 30_000),
|
|
}))
|
|
|
|
type MockRequest<T = unknown> = {
|
|
result?: T
|
|
error?: unknown
|
|
onsuccess?: ((event?: { target: { result: T } }) => void) | null
|
|
onerror?: ((event?: { target: { error: unknown } }) => void) | null
|
|
onupgradeneeded?: ((event?: { target: { result: unknown } }) => void) | null
|
|
}
|
|
|
|
function createAsyncRequest<T>(executor: () => T): MockRequest<T> {
|
|
const request: MockRequest<T> = {}
|
|
|
|
setTimeout(() => {
|
|
try {
|
|
request.result = executor()
|
|
request.onsuccess?.({ target: { result: request.result as T } })
|
|
} catch (error) {
|
|
request.error = error
|
|
request.onerror?.({ target: { error } })
|
|
}
|
|
}, 0)
|
|
|
|
return request
|
|
}
|
|
|
|
function createIndexedDbMock(): IDBFactory {
|
|
const records = new Map<string, unknown>()
|
|
let initialized = false
|
|
|
|
const objectStore = {
|
|
put: (value: { fileId: string }) => createAsyncRequest(() => {
|
|
records.set(value.fileId, value)
|
|
return value.fileId
|
|
}),
|
|
get: (key: string) => createAsyncRequest(() => records.get(key)),
|
|
delete: (key: string) => createAsyncRequest(() => {
|
|
records.delete(key)
|
|
return undefined
|
|
}),
|
|
count: () => createAsyncRequest(() => records.size),
|
|
getAll: () => createAsyncRequest(() => Array.from(records.values())),
|
|
clear: () => createAsyncRequest(() => {
|
|
records.clear()
|
|
return undefined
|
|
}),
|
|
}
|
|
|
|
const db = {
|
|
objectStoreNames: {
|
|
contains: () => initialized,
|
|
},
|
|
createObjectStore: () => {
|
|
initialized = true
|
|
return objectStore
|
|
},
|
|
transaction: () => ({
|
|
objectStore: () => objectStore,
|
|
}),
|
|
}
|
|
|
|
return {
|
|
open: jest.fn(() => {
|
|
const request: MockRequest<typeof db> = {}
|
|
|
|
setTimeout(() => {
|
|
request.result = db
|
|
|
|
if (!initialized) {
|
|
request.onupgradeneeded?.({ target: { result: db } })
|
|
initialized = true
|
|
}
|
|
|
|
request.onsuccess?.({ target: { result: db } })
|
|
}, 0)
|
|
|
|
return request as unknown as IDBOpenDBRequest
|
|
}),
|
|
} as unknown as IDBFactory
|
|
}
|
|
|
|
class MockXMLHttpRequest {
|
|
static instances: MockXMLHttpRequest[] = []
|
|
static statuses: number[] = []
|
|
|
|
timeout = 0
|
|
status = 413
|
|
responseText = ''
|
|
upload = {
|
|
onloadstart: null as (() => void) | null,
|
|
onprogress: null as ((event: ProgressEvent) => void) | null,
|
|
}
|
|
onload: (() => void) | null = null
|
|
onerror: (() => void) | null = null
|
|
onabort: (() => void) | null = null
|
|
ontimeout: (() => void) | null = null
|
|
headers: Record<string, string> = {}
|
|
method = ''
|
|
url = ''
|
|
|
|
constructor() {
|
|
MockXMLHttpRequest.instances.push(this)
|
|
}
|
|
|
|
open(method: string, url: string) {
|
|
this.method = method
|
|
this.url = url
|
|
}
|
|
|
|
setRequestHeader(name: string, value: string) {
|
|
this.headers[name] = value
|
|
}
|
|
|
|
send() {
|
|
setTimeout(() => {
|
|
this.status = MockXMLHttpRequest.statuses.shift() ?? 413
|
|
this.upload.onloadstart?.()
|
|
this.onload?.()
|
|
}, 0)
|
|
}
|
|
}
|
|
|
|
describe('chunkUpload', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
MockXMLHttpRequest.instances = []
|
|
MockXMLHttpRequest.statuses = []
|
|
|
|
Object.defineProperty(global, 'indexedDB', {
|
|
configurable: true,
|
|
writable: true,
|
|
value: createIndexedDbMock(),
|
|
})
|
|
|
|
Object.defineProperty(global, 'fetch', {
|
|
configurable: true,
|
|
writable: true,
|
|
value: jest.fn(),
|
|
})
|
|
|
|
Object.defineProperty(global, 'XMLHttpRequest', {
|
|
configurable: true,
|
|
writable: true,
|
|
value: MockXMLHttpRequest,
|
|
})
|
|
})
|
|
|
|
test('空文件会在初始化前被直接拦截', async () => {
|
|
const emptyFile = new File([], 'empty.psd', { type: 'application/octet-stream' })
|
|
|
|
await expect(chunkUpload(emptyFile, { materialType: '任务-详情' })).rejects.toThrow('为空')
|
|
expect(global.fetch).not.toHaveBeenCalled()
|
|
await expect(getAllPendingUploads()).resolves.toEqual([])
|
|
})
|
|
|
|
test('upload/init 返回 400 时不再无限重试', async () => {
|
|
;(global.fetch as jest.Mock).mockResolvedValue({
|
|
ok: false,
|
|
status: 400,
|
|
json: async () => ({ error: '上传文件不能为空' }),
|
|
})
|
|
|
|
const file = new File(['hello'], 'source.psb', { type: 'application/octet-stream' })
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 5, retryDelay: 0 },
|
|
),
|
|
).rejects.toThrow('上传文件不能为空')
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(1)
|
|
await expect(getAllPendingUploads()).resolves.toEqual([])
|
|
})
|
|
|
|
test('upload/chunk 返回 413 时直接失败并清理会话', async () => {
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ uploadId: 'upload-too-large' }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ message: '清理成功' }),
|
|
})
|
|
|
|
const file = new File(['hello'], 'source.psb', { type: 'application/octet-stream' })
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 5, retryDelay: 0 },
|
|
),
|
|
).rejects.toThrow('上传分片超过入口服务器限制')
|
|
|
|
expect(MockXMLHttpRequest.instances).toHaveLength(1)
|
|
expect(global.fetch).toHaveBeenCalledTimes(2)
|
|
expect(global.fetch).toHaveBeenNthCalledWith(
|
|
2,
|
|
'/api/materials/upload/cleanup',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
body: JSON.stringify({ uploadId: 'upload-too-large' }),
|
|
}),
|
|
)
|
|
await expect(getAllPendingUploads()).resolves.toEqual([])
|
|
})
|
|
|
|
test('显式指定分片并发时不再读取网络自动并发配置', async () => {
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ uploadId: 'upload-explicit-concurrency' }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
json: async () => ({ message: '清理成功' }),
|
|
})
|
|
|
|
const file = new File(['hello'], 'source.psb', { type: 'application/octet-stream' })
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 1, retryDelay: 0, chunkConcurrency: 2 },
|
|
),
|
|
).rejects.toThrow('上传分片超过入口服务器限制')
|
|
|
|
expect(getOptimalConcurrency).not.toHaveBeenCalled()
|
|
expect(MockXMLHttpRequest.instances).toHaveLength(1)
|
|
})
|
|
|
|
test('会话刷新返回 503 时保留原 uploadId 和本地进度,不重新初始化', async () => {
|
|
MockXMLHttpRequest.statuses = [200, 404]
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ uploadId: 'upload-original' }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 503,
|
|
json: async () => ({
|
|
error: '上传会话服务暂时不可用,已保留上传进度,请稍后重试',
|
|
code: 'UPLOAD_SESSION_STORE_UNAVAILABLE',
|
|
}),
|
|
})
|
|
|
|
const file = new File(
|
|
[new Uint8Array(1024 * 1024 + 1)],
|
|
'source.psb',
|
|
{ type: 'application/octet-stream' },
|
|
)
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 1, retryDelay: 0, requestHeaders: { 'X-Session-Ticket': 'ticket-1' } },
|
|
),
|
|
).rejects.toThrow('已保留上传进度')
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(2)
|
|
expect(global.fetch).toHaveBeenNthCalledWith(
|
|
2,
|
|
'/api/materials/upload/refresh',
|
|
expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
'Content-Type': 'application/json',
|
|
'X-Session-Ticket': 'ticket-1',
|
|
}),
|
|
body: JSON.stringify({ uploadId: 'upload-original' }),
|
|
}),
|
|
)
|
|
expect(global.fetch).not.toHaveBeenCalledWith(
|
|
'/api/materials/upload/cleanup',
|
|
expect.anything(),
|
|
)
|
|
|
|
const pendingUploads = await getAllPendingUploads()
|
|
expect(pendingUploads).toHaveLength(1)
|
|
expect(pendingUploads[0]).toEqual(expect.objectContaining({
|
|
uploadId: 'upload-original',
|
|
uploadedChunks: expect.any(Set),
|
|
}))
|
|
expect(pendingUploads[0].uploadedChunks).toEqual(new Set([0]))
|
|
})
|
|
|
|
test('会话刷新网络中断时同样保留原 uploadId 和已上传分片', async () => {
|
|
MockXMLHttpRequest.statuses = [200, 404]
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ uploadId: 'upload-network-retry' }),
|
|
})
|
|
.mockRejectedValueOnce(new TypeError('Failed to fetch'))
|
|
|
|
const file = new File(
|
|
[new Uint8Array(1024 * 1024 + 1)],
|
|
'network-source.psb',
|
|
{ type: 'application/octet-stream' },
|
|
)
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 1, retryDelay: 0 },
|
|
),
|
|
).rejects.toThrow('已保留上传进度')
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(2)
|
|
expect(global.fetch).not.toHaveBeenCalledWith(
|
|
'/api/materials/upload/cleanup',
|
|
expect.anything(),
|
|
)
|
|
const pendingUploads = await getAllPendingUploads()
|
|
expect(pendingUploads).toEqual([
|
|
expect.objectContaining({
|
|
uploadId: 'upload-network-retry',
|
|
uploadedChunks: new Set([0]),
|
|
}),
|
|
])
|
|
})
|
|
|
|
test('refresh 的 404 即使不是 JSON 响应也会按确认过期重新初始化', async () => {
|
|
MockXMLHttpRequest.statuses = [404, 200, 200]
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ uploadId: 'upload-expired' }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 404,
|
|
json: async () => {
|
|
throw new Error('gateway html response')
|
|
},
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ uploadId: 'upload-reinitialized' }),
|
|
})
|
|
|
|
const file = new File(['hello'], 'source.psb', { type: 'application/octet-stream' })
|
|
const result = await chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{
|
|
maxRetries: 1,
|
|
retryDelay: 0,
|
|
skipComplete: true,
|
|
requestHeaders: {
|
|
Authorization: 'Bearer dingtalk-token',
|
|
'X-DingTalk-Performance-Session': 'session-token',
|
|
},
|
|
},
|
|
)
|
|
|
|
expect(result).toEqual(expect.objectContaining({
|
|
uploadId: 'upload-reinitialized',
|
|
chunksUploaded: true,
|
|
}))
|
|
expect(global.fetch).toHaveBeenCalledTimes(3)
|
|
expect(global.fetch).toHaveBeenNthCalledWith(
|
|
3,
|
|
'/api/materials/upload/init',
|
|
expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
Authorization: 'Bearer dingtalk-token',
|
|
'X-DingTalk-Performance-Session': 'session-token',
|
|
}),
|
|
}),
|
|
)
|
|
})
|
|
|
|
test('complete 返回 503 且重试耗尽时仍保留本地和服务器进度', async () => {
|
|
MockXMLHttpRequest.statuses = [200]
|
|
;(global.fetch as jest.Mock)
|
|
.mockResolvedValueOnce({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({ uploadId: 'upload-complete-retry' }),
|
|
})
|
|
.mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 503,
|
|
json: async () => ({
|
|
error: '上传会话服务暂时不可用,已保留上传进度,请稍后重试',
|
|
}),
|
|
})
|
|
|
|
const file = new File(['hello'], 'source.psb', { type: 'application/octet-stream' })
|
|
|
|
await expect(
|
|
chunkUpload(
|
|
file,
|
|
{ materialType: '任务-详情' },
|
|
{ maxRetries: 1, retryDelay: 0 },
|
|
),
|
|
).rejects.toThrow('已保留上传进度')
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(2)
|
|
expect(global.fetch).not.toHaveBeenCalledWith(
|
|
'/api/materials/upload/cleanup',
|
|
expect.anything(),
|
|
)
|
|
const pendingUploads = await getAllPendingUploads()
|
|
expect(pendingUploads).toEqual([
|
|
expect.objectContaining({ uploadId: 'upload-complete-retry' }),
|
|
])
|
|
})
|
|
})
|