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

123 lines
4.2 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 安装包(跨分片阈值)调 uploadSoftwareVersion,
// 必须出现 POST /files/chunk/init;今天它走 presign → 一次 PUT 直传,
// 512MB 断网只能从 0 重来。本文件不出网,mock 掉 http 与 axios 直传实例。
import { beforeEach, describe, it, expect, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
post: vi.fn(),
get: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
directPut: vi.fn(),
}))
vi.mock('axios', () => ({
default: {
create: () => ({ put: mocks.directPut }),
},
}))
vi.mock('@/api/http', () => ({
http: { post: mocks.post, get: mocks.get, put: mocks.put, delete: mocks.delete },
unwrap: (value: unknown) => {
const envelope = value as { success?: boolean; data?: unknown; message?: string }
if (envelope && typeof envelope === 'object' && 'success' in envelope) {
if (!envelope.success) {
throw new Error(envelope.message || '请求失败')
}
return envelope.data
}
return value
},
}))
import { uploadSoftwareVersion } from '@/pages/records/version-api.ts'
import { uploadTutorialPackage } from '@/pages/records/tutorial-api.ts'
describe('AC-11 后管大档上传', () => {
// 会话跨尝试复用后,两格之间必须清态:残留的 upload_id 会让第二格跳过本该发生的 init。
beforeEach(() => {
vi.clearAllMocks()
localStorage.clear()
})
it('9MB 安装包必须走分片 init', async () => {
// 预签名与分片两套响应揉成一个超集:哪条实现路径先走都能拿到自己要的字段。
mocks.post.mockResolvedValue({
data: {
success: true,
data: {
upload_url: 'https://oss.example/put',
file_url: 'https://oss.example/pkg.zip',
version: '9.9.9',
object_key: 'client/versions/pkg.zip',
upload_id: 'a'.repeat(32),
uploaded_parts: [],
item: null,
},
},
})
mocks.get.mockResolvedValue({
data: { success: true, data: { upload_id: 'a'.repeat(32), uploaded_parts: [] } },
})
mocks.directPut.mockResolvedValue({})
const file = new Blob([new Uint8Array(9 * 1024 * 1024)], {
type: 'application/octet-stream',
})
await uploadSoftwareVersion('9.9.9', file)
const urls = mocks.post.mock.calls.map(([url]) => String(url))
expect(
urls.some((url) => url.includes('/files/chunk/init')),
`AC-11:9MB 安装包仍走 presign 直传,实际出口:${urls.join(', ')}`,
).toBe(true)
// P2-6:删掉 uploadFileInChunks(…) 真调用后,这一格必须跟着红——所以还要核对分片片确实发出去了。
expect(
mocks.post.mock.calls.filter(([url]) => /\/files\/chunk\/[^/]+\/part\/\d+$/.test(String(url))).length,
'AC-11:走分片就要真把片 POST 出去',
).toBe(2)
})
// P2-6:教程包那条道过去只有源码尺子覆盖(而尺子当时吃的是死常量),删掉真调用没人判红。
it('9MB 教程包必须走分片 init', async () => {
localStorage.clear()
mocks.post.mockResolvedValue({
data: {
success: true,
data: {
upload_url: 'https://oss.example/put',
file_url: 'https://oss.example/tutorial.zip',
version: '9.9.9',
object_key: 'client/tutorials/tutorial.zip',
upload_id: 'b'.repeat(32),
uploaded_parts: [],
item: null,
},
},
})
mocks.get.mockResolvedValue({
data: { success: true, data: { upload_id: 'b'.repeat(32), uploaded_parts: [] } },
})
mocks.directPut.mockResolvedValue({})
const file = new Blob([new Uint8Array(9 * 1024 * 1024)], {
type: 'application/octet-stream',
})
await uploadTutorialPackage(file, 'tutorial.zip', '9.9.9')
const urls = mocks.post.mock.calls.map(([url]) => String(url))
expect(
urls.some((url) => url.includes('/files/chunk/init')),
`AC-11:9MB 教程包仍走 presign 直传,实际出口:${urls.join(', ')}`,
).toBe(true)
expect(
mocks.directPut.mock.calls.length,
'AC-11:大档不该再走一次 PUT 直传',
).toBe(0)
})
})