task-105(任务与重复分析中心): 实现视频任务批量选择

新增 src/pages/tasks/image-video-selection.ts:卡片选择的纯 Set 操作
(切换/批量/全选判定/半选判定)。

TDD: task-105.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
2026-09-05 17:10:08 +08:00
parent a816bfa899
commit 64f9ac54cf
2 changed files with 115 additions and 0 deletions
@@ -0,0 +1,48 @@
/** 视频卡片批量选择状态(任务 105):不变量优先的纯 Set 操作,无框架依赖。 */
/** 空选择集。 */
export function clearVideoSelection(): Set<string> {
return new Set<string>()
}
function validKey(key: string): boolean {
return typeof key === 'string' && key.length > 0
}
/** 切换单卡选择(有则移除、无则加入)。 */
export function videoSelectionToggle(selection: Set<string>, key: string): Set<string> {
const next = new Set(selection)
if (!validKey(key)) return next
if (next.has(key)) next.delete(key)
else next.add(key)
return next
}
/** 批量加入(幂等)。 */
export function videoSelectionAdd(selection: Set<string>, keys: readonly string[]): Set<string> {
const next = new Set(selection)
for (const key of keys) if (validKey(key)) next.add(key)
return next
}
/** 批量移除(缺失键无操作)。 */
export function videoSelectionRemove(selection: Set<string>, keys: readonly string[]): Set<string> {
const next = new Set(selection)
for (const key of keys) next.delete(key)
return next
}
export function countVideoSelection(selection: Set<string>): number {
return selection.size
}
/** 是否全部卡被选中(空卡列表视作未全选)。 */
export function allCardsSelected(selection: Set<string>, keys: readonly string[]): boolean {
return keys.length > 0 && keys.every((key) => selection.has(key))
}
/** 是否部分卡被选中(部分但非全部)。 */
export function partialCardsSelected(selection: Set<string>, keys: readonly string[]): boolean {
const selectedCount = keys.filter((key) => selection.has(key)).length
return selectedCount > 0 && selectedCount < keys.length
}
+67
View File
@@ -0,0 +1,67 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import { readSource } from './helpers.ts'
import {
allCardsSelected,
clearVideoSelection,
countVideoSelection,
partialCardsSelected,
videoSelectionAdd,
videoSelectionRemove,
videoSelectionToggle,
} from '../src/pages/tasks/image-video-selection.ts'
test('test_task_105_video_selection_normal_primary_path', () => {
// 正常主路径:切换单卡选择(加/减)结果正确。
const s1 = videoSelectionToggle(new Set(), '11:0')
assert.equal(countVideoSelection(s1), 1)
assert.ok(s1.has('11:0'))
const s2 = videoSelectionToggle(s1, '11:0')
assert.equal(countVideoSelection(s2), 0, '再次切换取消选择')
})
test('test_task_105_video_selection_normal_variant_input', () => {
// 正常变体:批量添加多卡。
const s = videoSelectionAdd(new Set(), ['11:0', '12:1', '11:0'])
assert.equal(countVideoSelection(s), 2, '重复键不重复计数')
assert.ok(s.has('12:1'))
})
test('test_task_105_video_selection_repeated_is_idempotent', () => {
// 正常重复:同一键重复添加幂等;操作不污染入参。
const input = new Set(['a'])
assert.deepEqual(videoSelectionAdd(input, ['a']), new Set(['a']))
assert.deepEqual([...input], ['a'])
})
test('test_task_105_video_selection_boundary_empty_input', () => {
// 边界空值:空选择计数为 0,全不选。
assert.equal(countVideoSelection(new Set()), 0)
assert.equal(allCardsSelected(new Set(), ['a', 'b']), false)
})
test('test_task_105_video_selection_boundary_single_item', () => {
// 边界单元素:仅一卡时选中=全选。
assert.equal(allCardsSelected(new Set(['a']), ['a']), true)
assert.equal(partialCardsSelected(new Set(['a']), ['a']), false)
})
test('test_task_105_video_selection_boundary_limit_or_missing_field', () => {
// 边界上限/缺字段:移除未选键为无操作。
const s = videoSelectionRemove(new Set(['a']), ['x'])
assert.deepEqual([...s], ['a'])
})
test('test_task_105_video_selection_invalid_input_rejected', () => {
// 异常输入:空字符串键被忽略。
const s = videoSelectionToggle(new Set(), '')
assert.equal(countVideoSelection(s), 0)
assert.deepEqual([...clearVideoSelection()], [])
})
test('test_task_105_video_selection_dependency_failure_returns_actionable_message', () => {
// 依赖失败/可操作:选择状态纯逻辑、无框架/http。
const mod = readSource('src/pages/tasks/image-video-selection.ts')
assert.equal(/axios|http\.|vue/.test(mod), false, '选择模块保持纯逻辑')
assert.match(mod, /partialCardsSelected/)
})