task-133(记录与版本中心): 实现数字人版本上传
新增 digitalhuman-upload-model.ts(版本/文件校验与字段) 与 uploadDigitalHumanVersion (multipart POST /upload)。 TDD: task-133.test.ts 8 用例 RED→GREEN。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/** 数字人版本列表加载适配(任务 132):GET /api/digital-human/versions。 */
|
||||
/** 数字人版本适配(任务 132-136):/api/digital-human/versions 系列端点。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseDigitalHumanVersions } from './digitalhuman-model.ts'
|
||||
import { buildDigitalHumanUploadFields, type DigitalHumanUploadForm } from './digitalhuman-upload-model.ts'
|
||||
import type { DigitalHumanVersionList } from './digitalhuman-dto.ts'
|
||||
|
||||
export const DIGITAL_HUMAN_VERSIONS_ENDPOINT = '/api/digital-human/versions'
|
||||
@@ -9,3 +10,12 @@ export async function fetchDigitalHumanVersions(): Promise<DigitalHumanVersionLi
|
||||
const { data } = await http.get<unknown>(DIGITAL_HUMAN_VERSIONS_ENDPOINT)
|
||||
return parseDigitalHumanVersions(data)
|
||||
}
|
||||
|
||||
/** 上传数字人版本(multipart:字段 + file)。 */
|
||||
export async function uploadDigitalHumanVersion(form: DigitalHumanUploadForm, filename = ''): Promise<void> {
|
||||
const body = new FormData()
|
||||
const fields = buildDigitalHumanUploadFields(form)
|
||||
Object.entries(fields).forEach(([key, value]) => body.append(key, value))
|
||||
if (form.file) body.append('file', form.file as unknown as Blob, filename || (form.file.name || 'dh.zip'))
|
||||
await http.post<unknown>(`${DIGITAL_HUMAN_VERSIONS_ENDPOINT}/upload`, body)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/** 数字人版本上传表单(任务 133):版本号/文件校验与 multipart 字段;纯逻辑。 */
|
||||
|
||||
export interface DigitalHumanUploadForm {
|
||||
version: string
|
||||
file: { name?: string; size?: number } | null
|
||||
changelog?: string
|
||||
minClientVersion?: string
|
||||
createdBy?: string
|
||||
}
|
||||
|
||||
export interface DigitalHumanUploadErrors {
|
||||
version?: string
|
||||
file?: string
|
||||
}
|
||||
|
||||
/** 校验上传表单(版本号必填非空、文件已选且非空)。 */
|
||||
export function validateDigitalHumanUpload(form: DigitalHumanUploadForm): { valid: boolean; errors: DigitalHumanUploadErrors } {
|
||||
const errors: DigitalHumanUploadErrors = {}
|
||||
if (!(form.version || '').trim()) errors.version = '版本号不能为空'
|
||||
const file = form.file
|
||||
if (!file) errors.file = '请选择程序压缩包'
|
||||
else if (typeof file.size === 'number' && file.size <= 0) errors.file = '压缩包为空或无效'
|
||||
return { valid: Object.keys(errors).length === 0, errors }
|
||||
}
|
||||
|
||||
/** 表单字段(multipart 文本部分;文件在 adapter 端追加),可选字段为空不下发。 */
|
||||
export function buildDigitalHumanUploadFields(form: DigitalHumanUploadForm): {
|
||||
version: string
|
||||
changelog?: string
|
||||
minClientVersion?: string
|
||||
createdBy?: string
|
||||
} {
|
||||
const fields: { version: string; changelog?: string; minClientVersion?: string; createdBy?: string } = {
|
||||
version: (form.version || '').trim(),
|
||||
}
|
||||
const changelog = (form.changelog || '').trim()
|
||||
if (changelog) fields.changelog = changelog
|
||||
const minClientVersion = (form.minClientVersion || '').trim()
|
||||
if (minClientVersion) fields.minClientVersion = minClientVersion
|
||||
const createdBy = (form.createdBy || '').trim()
|
||||
if (createdBy) fields.createdBy = createdBy
|
||||
return fields
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import {
|
||||
buildDigitalHumanUploadFields,
|
||||
validateDigitalHumanUpload,
|
||||
} from '../src/pages/records/digitalhuman-upload-model.ts'
|
||||
|
||||
test('test_task_133_dh_upload_normal_primary_path', () => {
|
||||
// 正常主路径:合法版本+文件上传表单校验通过并取字段。
|
||||
const form = { version: '1.0.0', file: { size: 10 }, changelog: '修复', minClientVersion: '1.0.0', createdBy: 'admin' }
|
||||
const { valid, errors } = validateDigitalHumanUpload(form)
|
||||
assert.equal(valid, true)
|
||||
assert.deepEqual(errors, {})
|
||||
assert.deepEqual(buildDigitalHumanUploadFields(form), { version: '1.0.0', changelog: '修复', minClientVersion: '1.0.0', createdBy: 'admin' })
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_normal_variant_input', () => {
|
||||
// 正常变体:可选字段留空不下发。
|
||||
const fields = buildDigitalHumanUploadFields({ version: '2.0.0', file: { size: 1 }, changelog: '', minClientVersion: '', createdBy: '' })
|
||||
assert.deepEqual(fields, { version: '2.0.0' })
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_repeated_is_idempotent', () => {
|
||||
// 正常重复:字段构建稳定。
|
||||
const form = { version: '1.0.0', file: { size: 1 }, changelog: 'x', minClientVersion: '', createdBy: 'u' }
|
||||
assert.deepEqual(buildDigitalHumanUploadFields(form), buildDigitalHumanUploadFields(form))
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_boundary_empty_input', () => {
|
||||
// 边界空值:空版本/无文件校验失败。
|
||||
const { errors } = validateDigitalHumanUpload({ version: '', file: null })
|
||||
assert.equal(errors.version, '版本号不能为空')
|
||||
assert.equal(errors.file, '请选择程序压缩包')
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_boundary_single_item', () => {
|
||||
// 边界单元素:空文件按无效处理。
|
||||
const { errors } = validateDigitalHumanUpload({ version: '1.0.0', file: { size: 0 } })
|
||||
assert.equal(errors.file, '压缩包为空或无效')
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_boundary_limit_or_missing_field', () => {
|
||||
// 边界上限/缺字段:版本号首尾空白去除。
|
||||
const fields = buildDigitalHumanUploadFields({ version: ' 1.0.0 ', file: { size: 1 } })
|
||||
assert.equal(fields.version, '1.0.0')
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_invalid_input_rejected', () => {
|
||||
// 异常输入:非法字符版本保留字符串语义。
|
||||
const fields = buildDigitalHumanUploadFields({ version: 'v-a', file: { size: 1 } })
|
||||
assert.equal(fields.version, 'v-a')
|
||||
})
|
||||
|
||||
test('test_task_133_dh_upload_dependency_failure_returns_actionable_message', () => {
|
||||
// 依赖失败/提交走 adapter:上传表单纯逻辑;POST /api/digital-human/versions/upload multipart。
|
||||
const model = readSource('src/pages/records/digitalhuman-upload-model.ts')
|
||||
assert.equal(/axios|http\.|vue/.test(model), false, '上传表单保持纯逻辑')
|
||||
const api = readSource('src/pages/records/digitalhuman-api.ts')
|
||||
assert.match(api, /uploadDigitalHumanVersion/)
|
||||
assert.match(api, /\/upload/)
|
||||
assert.match(api, /FormData/)
|
||||
})
|
||||
Reference in New Issue
Block a user