task-103(任务与重复分析中心): 实现视频任务列表/卡片加载
新增 image-video-model.ts(snake 任务行解析、videos 安全地址与展平为视频卡片) 与 image-video-api.ts(GET /api/admin/image-video-tasks 加载适配)。 TDD: task-103.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
/** 视频任务列表加载适配(任务 103):GET /api/admin/image-video-tasks + 筛选归一与解析。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseImageVideoPage, type ImageVideoPageResult } from './image-video-model.ts'
|
||||
import { toImageVideoQuery, type ImageVideoFilter } from './image-video-filter.ts'
|
||||
|
||||
export const IMAGE_VIDEO_TASKS_ENDPOINT = '/api/admin/image-video-tasks'
|
||||
|
||||
export async function fetchImageVideoTasks(
|
||||
filter: ImageVideoFilter,
|
||||
page: number,
|
||||
pageSize: number,
|
||||
): Promise<ImageVideoPageResult> {
|
||||
const { data } = await http.get<unknown>(IMAGE_VIDEO_TASKS_ENDPOINT, {
|
||||
params: toImageVideoQuery(filter, page, pageSize),
|
||||
})
|
||||
return parseImageVideoPage(data)
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/** 视频任务列表/卡片模型(任务 103/104):解析 /api/admin/image-video-tasks snake 行并展平为视频卡片,纯逻辑。 */
|
||||
import { unwrap } from '../../api/envelope.ts'
|
||||
import { IMAGE_VIDEO_DEFAULT_PAGE_SIZE } from './image-video-filter.ts'
|
||||
import { normalizeTaskStatus, type TaskId, type TaskStatus } from './task-model.ts'
|
||||
|
||||
export const IMAGE_VIDEO_PAGE_SIZE_DEFAULT = IMAGE_VIDEO_DEFAULT_PAGE_SIZE
|
||||
|
||||
export interface ImageVideoVideo {
|
||||
sourceUrl: string
|
||||
archivedUrl: string
|
||||
displayUrl: string
|
||||
objectKey: string
|
||||
archiveStatus: string
|
||||
archiveError: string
|
||||
}
|
||||
|
||||
export interface ImageVideoRow {
|
||||
taskId: TaskId
|
||||
userId?: number | null
|
||||
username: string
|
||||
groupName: string
|
||||
status: TaskStatus
|
||||
cozeStatus: string
|
||||
cozeExecuteId: string
|
||||
videos: ImageVideoVideo[]
|
||||
videoUrl: string
|
||||
debugUrl: string
|
||||
archiveStatus: string
|
||||
archiveError: string
|
||||
submittedAt?: string
|
||||
completedAt?: string
|
||||
}
|
||||
|
||||
export interface ImageVideoCard {
|
||||
key: string
|
||||
task: ImageVideoRow
|
||||
video: ImageVideoVideo | null
|
||||
videoIndex: number
|
||||
}
|
||||
|
||||
export interface ImageVideoPageResult {
|
||||
items: ImageVideoRow[]
|
||||
total: number
|
||||
page: number
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
export function emptyImageVideoPage(): ImageVideoPageResult {
|
||||
return { items: [], total: 0, page: 1, pageSize: IMAGE_VIDEO_PAGE_SIZE_DEFAULT }
|
||||
}
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
function numberOrNull(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
||||
}
|
||||
|
||||
/** 安全管理端地址:仅 http(s) 视为可访问。 */
|
||||
export function safeAdminUrl(value: unknown): string {
|
||||
const url = text(value)
|
||||
return /^https?:\/\//i.test(url) ? url : ''
|
||||
}
|
||||
|
||||
/** 解析单条视频对象(display_url 等)。 */
|
||||
export function toImageVideoVideo(raw: unknown): ImageVideoVideo | null {
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const r = raw as Record<string, unknown>
|
||||
return {
|
||||
sourceUrl: safeAdminUrl(r.source_url ?? r.sourceUrl),
|
||||
archivedUrl: safeAdminUrl(r.archived_url ?? r.archivedUrl),
|
||||
displayUrl: safeAdminUrl(r.display_url ?? r.displayUrl),
|
||||
objectKey: text(r.object_key ?? r.objectKey),
|
||||
archiveStatus: text(r.archive_status ?? r.archiveStatus),
|
||||
archiveError: text(r.archive_error ?? r.archiveError),
|
||||
}
|
||||
}
|
||||
|
||||
/** 解析单条任务行;缺 task_id 视为无效。 */
|
||||
export function toImageVideoRow(raw: unknown): ImageVideoRow | null {
|
||||
if (!raw || typeof raw !== 'object') return null
|
||||
const r = raw as Record<string, unknown>
|
||||
const taskId = numberOrNull(r.task_id ?? r.taskId)
|
||||
if (taskId === null || taskId < 1) return null
|
||||
const task: ImageVideoRow = {
|
||||
taskId: String(taskId) as TaskId,
|
||||
username: text(r.username),
|
||||
groupName: text(r.group_name ?? r.groupName),
|
||||
status: normalizeTaskStatus(r.status),
|
||||
cozeStatus: text(r.coze_status ?? r.cozeStatus),
|
||||
cozeExecuteId: text(r.coze_execute_id ?? r.cozeExecuteId),
|
||||
videos: Array.isArray(r.videos)
|
||||
? r.videos.map((v) => toImageVideoVideo(v)).filter((v): v is ImageVideoVideo => v !== null)
|
||||
: [],
|
||||
videoUrl: safeAdminUrl(r.video_url ?? r.videoUrl),
|
||||
debugUrl: safeAdminUrl(r.debug_url ?? r.debugUrl),
|
||||
archiveStatus: text(r.archive_status ?? r.archiveStatus),
|
||||
archiveError: text(r.archive_error ?? r.archiveError),
|
||||
}
|
||||
const userId = numberOrNull(r.user_id ?? r.userId)
|
||||
if (userId !== null && userId >= 1) task.userId = userId
|
||||
const submittedAt = text(r.submitted_at ?? r.submittedAt)
|
||||
if (submittedAt) task.submittedAt = submittedAt
|
||||
const completedAt = text(r.completed_at ?? r.completedAt)
|
||||
if (completedAt) task.completedAt = completedAt
|
||||
return task
|
||||
}
|
||||
|
||||
/** 把任务行展平为“任务:视频下标”卡片;无 videos 的任务展为一张空卡。 */
|
||||
export function flattenImageVideoTasks(items: readonly ImageVideoRow[]): ImageVideoCard[] {
|
||||
const cards: ImageVideoCard[] = []
|
||||
for (const task of items) {
|
||||
if (!task.videos.length) {
|
||||
cards.push({ key: `${task.taskId}:empty`, task, video: null, videoIndex: 0 })
|
||||
continue
|
||||
}
|
||||
task.videos.forEach((video, index) => {
|
||||
cards.push({ key: `${task.taskId}:${index}`, task, video, videoIndex: index })
|
||||
})
|
||||
}
|
||||
return cards
|
||||
}
|
||||
|
||||
/** 归一化视频任务分页负载为前端结果;缺省字段回默认。 */
|
||||
export function parseImageVideoPage(payload: unknown): ImageVideoPageResult {
|
||||
const out = emptyImageVideoPage()
|
||||
const core = unwrap<unknown>(payload)
|
||||
if (!core || typeof core !== 'object') return out
|
||||
const record = core as Record<string, unknown>
|
||||
if (Array.isArray(record.items)) {
|
||||
out.items = record.items
|
||||
.map((raw) => toImageVideoRow(raw))
|
||||
.filter((item): item is ImageVideoRow => item !== null)
|
||||
}
|
||||
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
||||
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
||||
const rawSize = record.page_size ?? record.pageSize
|
||||
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import {
|
||||
flattenImageVideoTasks,
|
||||
parseImageVideoPage,
|
||||
toImageVideoVideo,
|
||||
} from '../src/pages/tasks/image-video-model.ts'
|
||||
|
||||
test('test_task_103_video_list_load_normal_primary_path', () => {
|
||||
// 正常主路径:snake 任务行(含 videos)解析并展平为视频卡片。
|
||||
const page = parseImageVideoPage({
|
||||
success: true,
|
||||
data: {
|
||||
items: [
|
||||
{
|
||||
task_id: 11,
|
||||
user_id: 3,
|
||||
username: '张伟',
|
||||
group_name: '华东组',
|
||||
status: 'SUCCESS',
|
||||
videos: [{ display_url: 'https://oss/a/1.mp4', source_url: 'https://src/1.mp4', object_key: 'a/1.mp4', archive_status: 'SUCCESS' }],
|
||||
submitted_at: '2026-01-01 10:00:00',
|
||||
completed_at: '2026-01-01 10:05:00',
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
page: 1,
|
||||
page_size: 20,
|
||||
},
|
||||
})
|
||||
assert.equal(page.items.length, 1)
|
||||
const task = page.items[0]
|
||||
assert.equal(task.taskId, '11')
|
||||
assert.equal(task.username, '张伟')
|
||||
assert.equal(task.groupName, '华东组')
|
||||
assert.equal(task.status, 'SUCCESS')
|
||||
assert.equal(task.videos.length, 1)
|
||||
assert.equal(task.videos[0].displayUrl, 'https://oss/a/1.mp4')
|
||||
assert.equal(page.total, 1)
|
||||
assert.equal(page.pageSize, 20)
|
||||
const cards = flattenImageVideoTasks(page.items)
|
||||
assert.equal(cards.length, 1)
|
||||
assert.equal(cards[0].key, '11:0')
|
||||
assert.equal(cards[0].video?.displayUrl, 'https://oss/a/1.mp4')
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_normal_variant_input', () => {
|
||||
// 正常变体:无 videos 的任务展平为单张空卡。
|
||||
const cards = flattenImageVideoTasks([{ taskId: '7', status: 'RUNNING', username: 'A', videos: [], groupName: '' }])
|
||||
assert.equal(cards.length, 1)
|
||||
assert.equal(cards[0].key, '7:empty')
|
||||
assert.equal(cards[0].video, null)
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_repeated_is_idempotent', () => {
|
||||
// 正常重复:解析与展平稳定、不改输入。
|
||||
const payload = { data: { items: [{ task_id: 1, status: 'FAILED', videos: [] }], total: 1, page: 1, page_size: 20 } }
|
||||
assert.deepEqual(parseImageVideoPage(payload), parseImageVideoPage(payload))
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_boundary_empty_input', () => {
|
||||
// 边界空值:空负载回默认分页空结果。
|
||||
const page = parseImageVideoPage({})
|
||||
assert.deepEqual(page.items, [])
|
||||
assert.equal(page.total, 0)
|
||||
assert.equal(page.pageSize, 20)
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_boundary_single_item', () => {
|
||||
// 边界单元素:单任务单视频展平结果与排序正确;缺 task_id 行被过滤。
|
||||
const page = parseImageVideoPage({ items: [{ task_id: 5, videos: [] }, { username: 'no-id' }], total: 2, page: 1, page_size: 20 })
|
||||
assert.equal(page.items.length, 1)
|
||||
assert.equal(flattenImageVideoTasks(page.items)[0].key, '5:empty')
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_boundary_limit_or_missing_field', () => {
|
||||
// 边界上限/缺字段:非 http 内联地址按不可播放处理。
|
||||
const video = toImageVideoVideo({ display_url: 'ftp://bad/x.mp4', object_key: 'k' })
|
||||
assert.ok(video)
|
||||
assert.equal(video.displayUrl, '')
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_invalid_input_rejected', () => {
|
||||
// 异常输入:success=false 抛后端 message;非对象行不入列表。
|
||||
assert.throws(() => parseImageVideoPage({ success: false, message: '无权访问视频任务' }), /无权访问/)
|
||||
assert.equal(toImageVideoVideo('garbage'), null)
|
||||
})
|
||||
|
||||
test('test_task_103_video_list_load_dependency_failure_returns_actionable_message', () => {
|
||||
// 依赖失败/加载走 adapter:GET /api/admin/image-video-tasks + http.get + 解析。
|
||||
const api = readSource('src/pages/tasks/image-video-api.ts')
|
||||
assert.match(api, /\/api\/admin\/image-video-tasks/)
|
||||
assert.match(api, /http\.get/)
|
||||
assert.match(api, /parseImageVideoPage/)
|
||||
const model = readSource('src/pages/tasks/image-video-model.ts')
|
||||
assert.equal(/axios|http\./.test(model), false, '视频列表解析保持纯逻辑')
|
||||
})
|
||||
Reference in New Issue
Block a user