fix(phase-5.2): 消项 CR 第二轮 P1/P2/P3(通道/证据链/交付时序/后管证据页)
This commit is contained in:
@@ -54,4 +54,40 @@ describe('AC-11 用户端大档上传', () => {
|
||||
`AC-11:9MB 视频仍走单次上传,实际出口:${urls.join(', ')}`,
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
// P2-9:上面那格的桩直接把包体当返回值,bodyOf 的真实 axios 分支(包体在 .data)零行为覆盖。
|
||||
// 形状误判的代价是静默的——upload_id 取不到就报"后端未返回分片会话",已达片号取不到就整包从 0 重传,
|
||||
// 而桩版本照样绿。这一格按真机形状(AxiosResponse{status, data})对撞,缺牙就判红。
|
||||
it('真实 axios 响应形状必须被认下来(P2-9 反桩形假绿)', async () => {
|
||||
const uploadId = 'b'.repeat(32)
|
||||
const envelope = (body: unknown) => ({
|
||||
status: 200,
|
||||
statusText: 'OK',
|
||||
headers: {},
|
||||
config: {},
|
||||
data: { success: true, message: '', data: body },
|
||||
})
|
||||
mocks.post.mockImplementation((url: string) => Promise.resolve(
|
||||
String(url).includes('/complete')
|
||||
? envelope({
|
||||
fileKey: 'tmp/big.mp4', originalFilename: 'big.mp4', localPath: '/tmp/big.mp4', size: 9437184,
|
||||
url: 'https://oss.example/big.mp4', objectKey: 'image-video/big.mp4', mediaType: 'video',
|
||||
})
|
||||
: envelope({ upload_id: uploadId, total_parts: 2, received_parts: 1 }),
|
||||
))
|
||||
mocks.get.mockResolvedValue(envelope({ upload_id: uploadId, uploaded_parts: [] }))
|
||||
|
||||
const file = new File([new Uint8Array(9 * 1024 * 1024)], 'big.mp4', { type: 'video/mp4' })
|
||||
const uploaded = await uploadImageVideoMedia(file)
|
||||
|
||||
expect(uploaded.objectKey, '包体在 .data 时仍要取到 complete 的结果').toBe('image-video/big.mp4')
|
||||
const partUrls = mocks.post.mock.calls
|
||||
.map(([url]) => String(url))
|
||||
.filter((url) => /\/part\/\d+$/.test(url))
|
||||
expect(partUrls.length, '9MB=2 片,一片都不能少').toBe(2)
|
||||
expect(
|
||||
partUrls.every((url) => url.includes(uploadId)),
|
||||
`bodyOf 没按 axios 形状取 upload_id,实际出口:${partUrls.join(', ')}`,
|
||||
).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
// P1-11(AC-9/铁律 18「瞬态失败不得把会话打掉」的用户端那一半)。
|
||||
//
|
||||
// 评审实录:GET 已达片号那一步用无差别 catch ⇒ 断网/超时/5xx 一律 clearStoredSession + 重开
|
||||
// upload_id,一次抖动就把 90MB 打回从 0。本文件按两个方向钉死:
|
||||
// ① 服务端确实说会话没了(HTTP 404/现网形状 HTTP 200+success:false+「不存在或已过期」)⇒ 重开;
|
||||
// ② 网络层失败/5xx/非会话类业务失败 ⇒ 保留 upload_id,绝不再发 init。
|
||||
// 另附 P2-9:桩必须能吃真实 axios 形状(包体在 .data),否则 bodyOf 的 axios 分支零行为覆盖,
|
||||
// 形状误判会让 done 成空集=静默从 0 而不判红。
|
||||
//
|
||||
// 跑法:npm run test:mount(tests/mount/** 走 vitest+happy-dom,localStorage 可用,不出网)。
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
post: vi.fn(),
|
||||
get: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/shared/api/http.ts', () => ({
|
||||
http: { post: mocks.post, get: mocks.get },
|
||||
post: mocks.post,
|
||||
get: mocks.get,
|
||||
unwrapJavaResponse: (value: unknown) => value,
|
||||
}))
|
||||
|
||||
import { UPLOAD_PART_SIZE, uploadTempFileToJava } from '@/shared/api/upload.ts'
|
||||
|
||||
const SESSION_ID = 's'.repeat(32)
|
||||
const FRESH_ID = 'f'.repeat(32)
|
||||
const TWO_PARTS = UPLOAD_PART_SIZE * 2
|
||||
|
||||
function sessionKeyOf(file: File): string {
|
||||
return `shufu:chunk:${file.name}:${file.size}:${file.lastModified}:`
|
||||
}
|
||||
|
||||
function seedSession(file: File, uploadId: string): void {
|
||||
localStorage.setItem(sessionKeyOf(file), JSON.stringify({
|
||||
uploadId,
|
||||
size: file.size,
|
||||
lastModified: file.lastModified,
|
||||
}))
|
||||
}
|
||||
|
||||
function storedUploadId(file: File): string {
|
||||
const raw = localStorage.getItem(sessionKeyOf(file))
|
||||
if (!raw) return ''
|
||||
return String((JSON.parse(raw) as { uploadId?: unknown }).uploadId ?? '')
|
||||
}
|
||||
|
||||
function makeBigFile(name = 'resume.mp4'): File {
|
||||
return new File([new Uint8Array(TWO_PARTS)], name, { type: 'video/mp4' })
|
||||
}
|
||||
|
||||
/** 真实 axios 出口形状:状态码与包体分家(测试桩过去直接把 body 当返回值,见 P2-9)。 */
|
||||
function axiosResponse(status: number, body: unknown) {
|
||||
return { status, data: body }
|
||||
}
|
||||
|
||||
function javaOk(body: unknown) {
|
||||
return axiosResponse(200, { success: true, message: '', data: body })
|
||||
}
|
||||
|
||||
function javaGone() {
|
||||
// 现网实读:ChunkedUploadService 抛 BusinessException 不带 code ⇒ GlobalExceptionHandler
|
||||
// 按 HTTP 200 + success:false 出,只有文案可依据。
|
||||
return axiosResponse(200, { success: false, message: '分片会话不存在或已过期', data: null })
|
||||
}
|
||||
|
||||
/** 按 URL 分派的 POST 桩:init 发新会话号,片与 complete 一律成功。 */
|
||||
function routePosts(initUploadId: string) {
|
||||
mocks.post.mockImplementation((url: string) => {
|
||||
const text = String(url)
|
||||
if (text.includes('/files/chunk/init')) {
|
||||
return Promise.resolve(javaOk({ upload_id: initUploadId, total_parts: 2 }))
|
||||
}
|
||||
if (text.includes('/complete')) {
|
||||
return Promise.resolve(javaOk({
|
||||
fileKey: 'tmp/resume.mp4', originalFilename: 'resume.mp4', localPath: '/tmp/resume.mp4',
|
||||
size: TWO_PARTS, url: 'https://oss.example/resume.mp4', objectKey: 'image-video/resume.mp4',
|
||||
}))
|
||||
}
|
||||
return Promise.resolve(javaOk({ received_parts: 1 }))
|
||||
})
|
||||
}
|
||||
|
||||
function postedUrls(): string[] {
|
||||
return mocks.post.mock.calls.map(([url]) => String(url))
|
||||
}
|
||||
|
||||
function initedCount(): number {
|
||||
return postedUrls().filter((url) => url.includes('/files/chunk/init')).length
|
||||
}
|
||||
|
||||
function partUrls(): string[] {
|
||||
return postedUrls().filter((url) => /\/part\/\d+$/.test(url))
|
||||
}
|
||||
|
||||
describe('P1-11 分片会话探测失败必须分因', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
localStorage.clear()
|
||||
routePosts(FRESH_ID)
|
||||
})
|
||||
|
||||
it('HTTP 404:服务端说会话没了 ⇒ 清状态并重新 init', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockResolvedValue(axiosResponse(404, { success: false, message: 'Not Found', data: null }))
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(initedCount(), '404 之后必须重新开会话').toBe(1)
|
||||
expect(partUrls().every((url) => url.includes(FRESH_ID)), '新片必须挂在新的会话号上').toBe(true)
|
||||
expect(partUrls().some((url) => url.includes(SESSION_ID)), '旧会话号已判死,不得再往它写片').toBe(false)
|
||||
})
|
||||
|
||||
it('HTTP 200+success:false+「不存在或已过期」(现网形状)⇒ 同样重开', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockResolvedValue(javaGone())
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(initedCount(), '会话确实回收后不重建就永远卡死').toBe(1)
|
||||
expect(partUrls().every((url) => url.includes(FRESH_ID))).toBe(true)
|
||||
})
|
||||
|
||||
it('HTTP 503:瞬态 ⇒ 保留 upload_id,一条 init 都不许发', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockResolvedValue(axiosResponse(503, '<html>Bad Gateway</html>'))
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(initedCount(), '一次网关抖动就把整包打回从 0,正是本迭代要消灭的行为').toBe(0)
|
||||
expect(partUrls().every((url) => url.includes(SESSION_ID)), '片仍要写进原来那个会话').toBe(true)
|
||||
})
|
||||
|
||||
it('探测超时(网络层错误):瞬态 ⇒ 保留 upload_id 并续传', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockRejectedValue(new Error('timeout of 30000ms exceeded'))
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(initedCount()).toBe(0)
|
||||
expect(partUrls().every((url) => url.includes(SESSION_ID))).toBe(true)
|
||||
})
|
||||
|
||||
it('非会话类业务失败(如权限):不许顺手清掉会话', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockResolvedValue(axiosResponse(200, { success: false, message: '无权访问该分片会话', data: null }))
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(initedCount()).toBe(0)
|
||||
expect(partUrls().every((url) => url.includes(SESSION_ID))).toBe(true)
|
||||
})
|
||||
|
||||
it('真实 axios 形状:已到片号必须被跳过(P2-9 形状误判即静默从 0)', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockResolvedValue(javaOk({ upload_id: SESSION_ID, uploaded_parts: [0] }))
|
||||
|
||||
await uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' })
|
||||
|
||||
expect(partUrls(), '第 0 片服务端已有,不得重传').toEqual([
|
||||
`/newApi/api/files/chunk/${SESSION_ID}/part/1`,
|
||||
])
|
||||
expect(mocks.get.mock.calls.length, '探测要带 upload_id 问服务端').toBe(1)
|
||||
})
|
||||
|
||||
it('探测失败+分片也失败 ⇒ 会话留在 localStorage,且不 DELETE', async () => {
|
||||
const file = makeBigFile()
|
||||
seedSession(file, SESSION_ID)
|
||||
mocks.get.mockRejectedValue(new Error('Network Error'))
|
||||
mocks.post.mockRejectedValue(new Error('Network Error'))
|
||||
|
||||
await expect(uploadTempFileToJava(file, { uploadToOss: true, moduleType: 'IMAGE_VIDEO' }))
|
||||
.rejects.toThrow(/Network Error/)
|
||||
|
||||
expect(storedUploadId(file), '瞬态失败后会话必须还在,下次刷新才能从断点续传').toBe(SESSION_ID)
|
||||
expect(initedCount()).toBe(0)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user