DevTask 状态简化为:待开发 → 开发中 → 自测 → 已提测(终态) - 提测即代表开发交付完成,不需要额外的"已完成" - 测试通过/失败产生的是 Bug,不影响 DevTask 状态 - 移除 TestCaseStore 中的自动完成副作用(不再需要) - 版本执行态推导规则同步更新 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);
|
|
},
|
|
}));
|