核心改动: - TestCase 主归属 versionId,requirementId 改为可选语义标签 - Bug 新增 versionId(直接挂版本)+ requirementId(可选追溯) - 新增 calcVersionExecutionStatus() 版本执行态推导函数 规则:DevTask全部提测→已提测,TestCase全部通过+Bug全关→可发布 - 概览页展示推导出的执行态标签 - BugTab 改为直接 versionId 查询(不再走 testCase→req 链路) - 统计卡片"参与人员"替换为"未关闭Bug" 心智模型: 版本(执行线)→ DevTask/TestCase/Bug 需求(语义线)→ 解释为什么做,不驱动流程 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
101 lines
3.2 KiB
TypeScript
101 lines
3.2 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[];
|
|
getByVersion: (versionId: 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);
|
|
},
|
|
|
|
getByVersion: (versionId) => {
|
|
return get().testCases.filter((c) => c.versionId === versionId);
|
|
},
|
|
|
|
getByAssignee: (assigneeId) => {
|
|
return get().testCases.filter((c) => c.assigneeId === assigneeId);
|
|
},
|
|
}));
|