Files
ftb-project-management/apps/web/stores/useTestCaseStore.ts
Script Generator 7fe3a3854a feat(test-case+bug): 测试用例模块 + Bug 模块完整实现
需求验收体系:
- 测试用例:五态状态机(待执行/执行中/通过/失败/阻塞)
- Bug:六态状态机(待修复/修复中/已修复/验证中/已关闭/已拒绝)
- Bug 通过测试用例间接关联需求(不冗余存版本)
- Bug 默认修复人 = 关联需求的开发任务负责人
- 测试进度 = 已执行用例/总用例, 通过率 = 通过/已执行

UI:
- 版本详情页新增"测试用例" Tab + "BUG" Tab
- 测试用例:统计栏+筛选+按需求分组列表+详情抽屉+提BUG入口
- Bug:统计栏+筛选+列表+详情抽屉(链式跳转用例→需求)
- 概览胶囊"测试"阶段进度联动
- "与我相关"新增测试用例/Bug分组

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-12 13:11:08 +08:00

96 lines
3.1 KiB
TypeScript

'use client';
import { create } from 'zustand';
import type { TestCase, TestCaseStatus } from '@/lib/test-case';
import { canTcTransition, generateCaseNo } from '@/lib/test-case';
const STORAGE_KEY = 'ftb_test_cases_v1';
function saveLocal(items: TestCase[]) {
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {}
}
function loadLocal(): TestCase[] | null {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) return JSON.parse(raw);
} catch {}
return null;
}
interface TestCaseState {
testCases: TestCase[];
fetchTestCases: () => void;
createTestCase: (data: Omit<TestCase, 'id' | 'caseNo' | 'createdAt' | 'updatedAt' | 'status'>) => TestCase;
updateTestCase: (id: string, data: Partial<TestCase>) => void;
deleteTestCase: (id: string) => void;
changeStatus: (id: string, to: TestCaseStatus, extra?: { failReason?: string; blockReason?: string }) => { ok: boolean; message?: string };
getByRequirement: (reqId: string) => TestCase[];
getByAssignee: (assigneeId: string) => TestCase[];
}
export const useTestCaseStore = create<TestCaseState>((set, get) => ({
testCases: [],
fetchTestCases: () => {
const cached = loadLocal();
if (cached) set({ testCases: cached });
},
createTestCase: (data) => {
const list = get().testCases;
const now = new Date().toISOString();
const tc: TestCase = {
...data,
id: `tc-${Date.now()}`,
caseNo: generateCaseNo(list),
status: 'pending',
createdAt: now,
updatedAt: now,
};
const updated = [...list, tc];
set({ testCases: updated });
saveLocal(updated);
return tc;
},
updateTestCase: (id, data) => {
const updated = get().testCases.map((c) =>
c.id === id ? { ...c, ...data, updatedAt: new Date().toISOString() } : c,
);
set({ testCases: updated });
saveLocal(updated);
},
deleteTestCase: (id) => {
const updated = get().testCases.filter((c) => c.id !== id);
set({ testCases: updated });
saveLocal(updated);
},
changeStatus: (id, to, extra) => {
const tc = get().testCases.find((c) => c.id === id);
if (!tc) return { ok: false, message: '用例不存在' };
if (!canTcTransition(tc.status, to)) {
return { ok: false, message: `不允许从「${tc.status}」流转到「${to}` };
}
const today = new Date().toISOString().slice(0, 10);
const patch: Partial<TestCase> = { status: to };
if (to === 'running' || to === 'passed' || to === 'failed' || to === 'blocked') {
patch.executedAt = today;
}
if (to === 'failed' && extra?.failReason) patch.failReason = extra.failReason;
if (to === 'blocked' && extra?.blockReason) patch.blockReason = extra.blockReason;
if (to === 'running') { patch.failReason = undefined; patch.blockReason = undefined; }
get().updateTestCase(id, patch);
return { ok: true };
},
getByRequirement: (reqId) => {
return get().testCases.filter((c) => c.requirementId === reqId);
},
getByAssignee: (assigneeId) => {
return get().testCases.filter((c) => c.assigneeId === assigneeId);
},
}));