Files
crawler-plugin/frontend-vue/tests/mount/pending-phase4-大档上传接线.mount.test.ts
T

94 lines
3.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.
// AC-11(R4):用户端大档上传接线的「行为用例」那一半。
//
// Pending Phase 4 激活。9MB 视频(跨 8MB 分片阈值)调 uploadImageVideoMedia,
// 必须出现 POST /files/chunk/init;今天它只打 POST /files/upload(单次 120s)。
//
// 本文件走 vitest(tests/mount/**),用 mock 的 http 出口对撞 URL 序列,不出网。
import { describe, it, expect, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
post: vi.fn(),
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
deleteFn: vi.fn(),
}))
vi.mock('@/shared/api/http.ts', () => ({
http: mocks,
post: mocks.post,
get: mocks.get,
put: mocks.put,
del: mocks.deleteFn,
unwrapJavaResponse: (value: unknown) => value,
}))
import { uploadImageVideoMedia } from '@/shared/api/types/modules/image-video.ts'
describe('AC-11 用户端大档上传', () => {
it('9MB 视频必须走分片 init', async () => {
const payload = {
success: true,
data: {
url: 'https://oss.example/evidence.mp4',
objectKey: 'image-video/obj.mp4',
originalFilename: 'big.mp4',
mediaType: 'video',
upload_id: 'a'.repeat(32),
uploaded_parts: [],
},
}
mocks.post.mockResolvedValue(payload)
mocks.get.mockResolvedValue({
success: true,
data: { upload_id: 'a'.repeat(32), uploaded_parts: [] },
})
const file = new File([new Uint8Array(9 * 1024 * 1024)], 'big.mp4', { type: 'video/mp4' })
await uploadImageVideoMedia(file)
const urls = mocks.post.mock.calls.map(([url]) => String(url))
expect(
urls.some((url) => url.includes('/files/chunk/init')),
`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)
})
})