task-107(任务与重复分析中心): 实现视频任务下载失败摘要
新增 image-video-download-feedback.ts:zip 文件名 Content-Disposition 解析与 按成功/失败计数生成下载结果摘要(全失败/部分失败提示)。 TDD: task-107.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/** 视频批量下载反馈(任务 107):zip 文件名解析与下载失败摘要,纯逻辑。 */
|
||||
|
||||
/** 从 Content-Disposition 解析下载文件名(rfc5987 优先),失败回默认名。 */
|
||||
export function zipFilenameFromDisposition(disposition: string | undefined): string {
|
||||
const text = typeof disposition === 'string' ? disposition : ''
|
||||
const encoded = /filename\*=UTF-8''([^;]+)/i.exec(text)
|
||||
if (encoded) {
|
||||
try {
|
||||
const decoded = decodeURIComponent(encoded[1])
|
||||
if (decoded.trim()) return decoded.trim()
|
||||
} catch {
|
||||
// 回退到普通 filename
|
||||
}
|
||||
}
|
||||
const plain = /filename="?([^";]+)"?/i.exec(text)
|
||||
return plain && plain[1] ? plain[1].trim() : 'video-tasks.zip'
|
||||
}
|
||||
|
||||
function finiteCount(value: number | undefined): number {
|
||||
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : 0
|
||||
}
|
||||
|
||||
/** 依据服务端成功/失败计数生成下载结果摘要文案。 */
|
||||
export function downloadResultSummary(counts: { fileCount?: number; errorCount?: number; selected?: number }): string {
|
||||
const fileCount = finiteCount(counts.fileCount)
|
||||
const errorCount = finiteCount(counts.errorCount)
|
||||
const selected = finiteCount(counts.selected)
|
||||
if (fileCount === 0 && errorCount === 0) return '未产生下载结果'
|
||||
if (errorCount === 0) return `已下载 ${fileCount} 个视频`
|
||||
const allFailed = fileCount === 0 || (selected > 0 && errorCount >= selected)
|
||||
return allFailed
|
||||
? `全部 ${errorCount} 个视频下载失败,详见打包中的错误清单`
|
||||
: `打包完成:成功 ${fileCount} 个,失败 ${errorCount} 个,详见打包中的错误清单`
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import {
|
||||
downloadResultSummary,
|
||||
zipFilenameFromDisposition,
|
||||
} from '../src/pages/tasks/image-video-download-feedback.ts'
|
||||
|
||||
test('test_task_107_download_feedback_normal_primary_path', () => {
|
||||
// 正常主路径:无失败时给出成功数量文案。
|
||||
assert.equal(downloadResultSummary({ fileCount: 18, errorCount: 0 }), '已下载 18 个视频')
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_normal_variant_input', () => {
|
||||
// 正常变体:部分失败给出成功/失败摘要。
|
||||
assert.match(downloadResultSummary({ fileCount: 16, errorCount: 2 }), /成功 16/)
|
||||
assert.match(downloadResultSummary({ fileCount: 16, errorCount: 2 }), /失败 2/)
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_repeated_is_idempotent', () => {
|
||||
// 正常重复:同一输入摘要稳定。
|
||||
assert.equal(downloadResultSummary({ fileCount: 0, errorCount: 3, selected: 3 }), downloadResultSummary({ fileCount: 0, errorCount: 3, selected: 3 }))
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_boundary_empty_input', () => {
|
||||
// 边界空值:无任何计数回空态文案。
|
||||
assert.equal(downloadResultSummary({}), '未产生下载结果')
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_boundary_single_item', () => {
|
||||
// 边界单元素:全部失败给出明确失败提示。
|
||||
assert.match(downloadResultSummary({ fileCount: 0, errorCount: 3, selected: 3 }), /全部 3 个.*失败/)
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_boundary_limit_or_missing_field', () => {
|
||||
// 边界上限/缺字段:file_count 缺失但错误大于 0 按全失败提示。
|
||||
assert.match(downloadResultSummary({ errorCount: 1, selected: 1 }), /全部 1 个.*失败/)
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_invalid_input_rejected', () => {
|
||||
// 异常输入:Content-Disposition 解析失败回默认 zip 名。
|
||||
assert.equal(zipFilenameFromDisposition(''), 'video-tasks.zip')
|
||||
assert.equal(zipFilenameFromDisposition('attachment'), 'video-tasks.zip')
|
||||
})
|
||||
|
||||
test('test_task_107_download_feedback_dependency_failure_returns_actionable_message', () => {
|
||||
// 依赖失败/可操作:下载反馈纯逻辑、无框架/http。
|
||||
const mod = readSource('src/pages/tasks/image-video-download-feedback.ts')
|
||||
assert.equal(/axios|http\.|vue/.test(mod), false, '下载反馈保持纯逻辑')
|
||||
assert.match(mod, /zipFilenameFromDisposition/)
|
||||
})
|
||||
Reference in New Issue
Block a user