From 64f9ac54cf2a89a02ce5ed0606f947ec5191aeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sat, 5 Sep 2026 17:10:08 +0800 Subject: [PATCH] =?UTF-8?q?task-105(=E4=BB=BB=E5=8A=A1=E4=B8=8E=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=88=86=E6=9E=90=E4=B8=AD=E5=BF=83):=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E8=A7=86=E9=A2=91=E4=BB=BB=E5=8A=A1=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 src/pages/tasks/image-video-selection.ts:卡片选择的纯 Set 操作 (切换/批量/全选判定/半选判定)。 TDD: task-105.test.ts 8 用例先 RED 后 GREEN。 --- .../src/pages/tasks/image-video-selection.ts | 48 +++++++++++++ admin-frontend-vue/tests/task-105.test.ts | 67 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 admin-frontend-vue/src/pages/tasks/image-video-selection.ts create mode 100644 admin-frontend-vue/tests/task-105.test.ts diff --git a/admin-frontend-vue/src/pages/tasks/image-video-selection.ts b/admin-frontend-vue/src/pages/tasks/image-video-selection.ts new file mode 100644 index 00000000..a580bac1 --- /dev/null +++ b/admin-frontend-vue/src/pages/tasks/image-video-selection.ts @@ -0,0 +1,48 @@ +/** 视频卡片批量选择状态(任务 105):不变量优先的纯 Set 操作,无框架依赖。 */ + +/** 空选择集。 */ +export function clearVideoSelection(): Set { + return new Set() +} + +function validKey(key: string): boolean { + return typeof key === 'string' && key.length > 0 +} + +/** 切换单卡选择(有则移除、无则加入)。 */ +export function videoSelectionToggle(selection: Set, key: string): Set { + 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, keys: readonly string[]): Set { + const next = new Set(selection) + for (const key of keys) if (validKey(key)) next.add(key) + return next +} + +/** 批量移除(缺失键无操作)。 */ +export function videoSelectionRemove(selection: Set, keys: readonly string[]): Set { + const next = new Set(selection) + for (const key of keys) next.delete(key) + return next +} + +export function countVideoSelection(selection: Set): number { + return selection.size +} + +/** 是否全部卡被选中(空卡列表视作未全选)。 */ +export function allCardsSelected(selection: Set, keys: readonly string[]): boolean { + return keys.length > 0 && keys.every((key) => selection.has(key)) +} + +/** 是否部分卡被选中(部分但非全部)。 */ +export function partialCardsSelected(selection: Set, keys: readonly string[]): boolean { + const selectedCount = keys.filter((key) => selection.has(key)).length + return selectedCount > 0 && selectedCount < keys.length +} diff --git a/admin-frontend-vue/tests/task-105.test.ts b/admin-frontend-vue/tests/task-105.test.ts new file mode 100644 index 00000000..c4e0d1e3 --- /dev/null +++ b/admin-frontend-vue/tests/task-105.test.ts @@ -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/) +})