Files
crawler-plugin/admin-frontend-vue/tests/pure/chunk-upload-resume.test.ts
T

227 lines
9.0 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.
// P2-7/P2-8/P3-5(后管分片通道 AC-9/AC-11 精神)。
//
// 评审实录:`src/api/chunk-upload.ts` 每次调用都新 init 一个 upload_id ⇒ 紧随的 GET 必空、
// 放弃时又把会话 DELETE 掉,**等于根本没有续传**;逐片超时还按整包体积算(120s + MB×15s),
// 8MB 的片能拿到约 130 分钟。本文件按用户端 `shared/api/upload.ts` 的同一口径钉四件事:
// ① 同一文件身份复用 upload_id(不重新 init);② 瞬态失败不 DELETE、会话留在本地供下次续传;
// ③ 只有服务端确实说会话没了(404/「不存在或已过期」)才重开;④ 逐片超时按本片体积给。
//
// 跑法:npm run test:mount(tests/pure/** 与 tests/mount/** 同走 vitest)。
import { beforeEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
post: vi.fn(),
get: vi.fn(),
delete: vi.fn(),
}))
// 只顶掉 http 实例,unwrap 用真的:会话回收在后端是 HTTP 200+success:false,
// 判因逻辑要吃真实解包语义(含中文文案把关),换桩会把这一半形状测漏。
vi.mock('@/api/http', async (importOriginal) => {
const actual = await importOriginal<{ unwrap: <T>(payload: unknown) => T }>()
return {
...actual,
http: { post: mocks.post, get: mocks.get, delete: mocks.delete },
}
})
import {
CHUNK_UPLOAD_PART_SIZE,
CHUNK_UPLOAD_THRESHOLD,
chunkSessionKeyOf,
partTimeoutOf,
uploadFileInChunks,
} from '@/api/chunk-upload'
const RESUMED_ID = 'r'.repeat(32)
const FRESH_ID = 'n'.repeat(32)
function axiosResponse(status: number, body: unknown) {
return { status, data: body }
}
function javaOk(body: unknown) {
return axiosResponse(200, { success: true, message: '', data: body })
}
function makeFile(name: string, bytes: number): File {
return new File([new Uint8Array(bytes)], name, { type: 'application/octet-stream' })
}
function seedSession(file: File, uploadId: string): void {
localStorage.setItem(chunkSessionKeyOf(file.name, file), JSON.stringify({
uploadId,
size: file.size,
lastModified: file.lastModified,
}))
}
function storedUploadId(file: File): string {
const raw = localStorage.getItem(chunkSessionKeyOf(file.name, file))
if (!raw) return ''
return String((JSON.parse(raw) as { uploadId?: unknown }).uploadId ?? '')
}
function postedUrls(): string[] {
return mocks.post.mock.calls.map(([url]) => String(url))
}
function partPosts(): Array<{ url: string; timeout: number }> {
return mocks.post.mock.calls
.filter(([url]) => /\/part\/\d+$/.test(String(url)))
.map(([url, , config]) => ({
url: String(url),
timeout: Number((config as { timeout?: unknown } | undefined)?.timeout ?? -1),
}))
}
function initCount(): number {
return postedUrls().filter((url) => url.includes('/chunk/init')).length
}
/** init/逐片/complete 一律成功;complete 返回拼装后的 fileKey。 */
function routePosts(uploadId: string) {
mocks.post.mockImplementation((url: string) => Promise.resolve(
String(url).includes('/chunk/init')
? javaOk({ upload_id: uploadId, total_parts: 2 })
: String(url).includes('/complete')
? javaOk({ fileKey: 'tmp/pkg.zip', originalFilename: 'pkg.zip', size: 1 })
: javaOk({ upload_id: String(url).split('/')[5], received_parts: 1 }),
))
}
describe('P2-7 后管分片必须真的续传', () => {
beforeEach(() => {
vi.clearAllMocks()
localStorage.clear()
routePosts(FRESH_ID)
})
it('同一文件身份复用 upload_id:探测成功就一条 init 都不许发', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(javaOk({ upload_id: RESUMED_ID, uploaded_parts: [0] }))
await uploadFileInChunks(file, file.name)
expect(initCount(), '会话还在服务端,重开 init 就是作废已传片').toBe(0)
expect(
partPosts().map((part) => part.url),
'第 0 片服务端已有,只补第 1 片',
).toEqual([`/api/files/chunk/${RESUMED_ID}/part/1`])
})
it('瞬态失败(分片打不出去):不 DELETE 会话,且本地仍留着 upload_id', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(javaOk({ upload_id: RESUMED_ID, uploaded_parts: [] }))
mocks.post.mockRejectedValue(new Error('Network Error'))
await expect(uploadFileInChunks(file, file.name)).rejects.toThrow(/Network Error/)
expect(mocks.delete.mock.calls, 'DELETE 会把已达片号一起清掉,下次只能从 0').length(0)
expect(storedUploadId(file), '会话要留给下一次(含刷新后)续传').toBe(RESUMED_ID)
})
it('探测 503:瞬态 ⇒ 保留原会话,不重开', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(axiosResponse(503, '<html>Bad Gateway</html>'))
await uploadFileInChunks(file, file.name)
expect(initCount()).toBe(0)
expect(partPosts().every((part) => part.url.includes(RESUMED_ID))).toBe(true)
})
it('探测超时(网络层错误):瞬态 ⇒ 同样保留原会话', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockRejectedValue(new Error('timeout of 30000ms exceeded'))
await uploadFileInChunks(file, file.name)
expect(initCount()).toBe(0)
expect(partPosts().every((part) => part.url.includes(RESUMED_ID))).toBe(true)
})
it('HTTP 404:会话确实没了 ⇒ 清状态重开', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(axiosResponse(404, { success: false, message: 'Not Found', data: null }))
await uploadFileInChunks(file, file.name)
expect(initCount(), '会话已回收,不重建就永远卡在同一个死会话上').toBe(1)
expect(partPosts().every((part) => part.url.includes(FRESH_ID))).toBe(true)
})
it('后端 HTTP 200+「分片会话不存在或已过期」(现网形状)⇒ 同样重开', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(axiosResponse(
200,
{ success: false, message: '分片会话不存在或已过期', data: null },
))
await uploadFileInChunks(file, file.name)
expect(initCount()).toBe(1)
expect(partPosts().every((part) => part.url.includes(FRESH_ID))).toBe(true)
})
it('拼装成功 ⇒ 本地会话清掉(同一文件再传要开新会话)', async () => {
const file = makeFile('pkg.zip', CHUNK_UPLOAD_PART_SIZE * 2)
seedSession(file, RESUMED_ID)
mocks.get.mockResolvedValue(javaOk({ upload_id: RESUMED_ID, uploaded_parts: [] }))
await uploadFileInChunks(file, file.name)
expect(storedUploadId(file), '完成后会话已拼装落库,本地不该留残号').toBe('')
})
})
describe('P2-8 逐片超时按本片体积', () => {
it('每片预算只看这一片有多大,不得按整包体积放大', async () => {
const file = makeFile('big.zip', CHUNK_UPLOAD_PART_SIZE * 5)
mocks.get.mockResolvedValue(javaOk({ upload_id: FRESH_ID, uploaded_parts: [] }))
routePosts(FRESH_ID)
await uploadFileInChunks(file, file.name)
const parts = partPosts()
expect(parts.length, '40MB=5 片').toBe(5)
// 整包口径会给出 120s + 40×15s = 720s;按片口径 8MB 片=300s。判红要留出余量差。
for (const part of parts) {
expect(part.timeout, `片 ${part.url} 的超时没有按本片体积算`).toBeGreaterThan(0)
expect(
part.timeout,
`片 ${part.url} 的超时 ${part.timeout}ms 是按整包体积算的(P2-8)`,
).toBeLessThanOrEqual(partTimeoutOf(CHUNK_UPLOAD_PART_SIZE))
}
expect(partTimeoutOf(CHUNK_UPLOAD_PART_SIZE), '8MB 片的预算不该超过 10 分钟')
.toBeLessThanOrEqual(600_000)
expect(partTimeoutOf(1), '再小的片也要有保底,不能给 0').toBeGreaterThanOrEqual(60_000)
})
it('init 与 complete 的预算不再随整包体积线性放大', async () => {
const file = makeFile('big.zip', CHUNK_UPLOAD_PART_SIZE * 5)
mocks.get.mockResolvedValue(javaOk({ upload_id: FRESH_ID, uploaded_parts: [] }))
routePosts(FRESH_ID)
await uploadFileInChunks(file, file.name)
const initCall = mocks.post.mock.calls.find(([url]) => String(url).includes('/chunk/init'))
const initTimeout = Number((initCall?.[2] as { timeout?: unknown } | undefined)?.timeout ?? -1)
expect(initTimeout, 'init 只发一条登记请求,按整包体积给 720s 是把失败拖到天荒地老').toBeLessThan(300_000)
})
})
describe('P3-5 阈值单点', () => {
it('后管这一侧的 8MB 只有一个出口:阈值由片大小派生', () => {
expect(CHUNK_UPLOAD_THRESHOLD, '阈值必须与片大小同源(P3-5 三处各写一遍的收口)')
.toBe(CHUNK_UPLOAD_PART_SIZE)
expect(CHUNK_UPLOAD_PART_SIZE).toBe(8 * 1024 * 1024)
})
})