feat(Bug): 状态流转接入单条工作流

This commit is contained in:
Script Generator
2026-06-25 14:47:28 +08:00
parent c617625a99
commit 125057ea55
3 changed files with 142 additions and 25 deletions

View File

@@ -0,0 +1,61 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import type { Bug } from './bug';
import { applyBugTransition } from './bug-workflow';
function bug(patch: Partial<Bug> = {}): Bug {
return {
id: 'bug-1',
bugNo: 'BUG-001',
versionId: 'version-1',
testCaseId: 'tc-1',
title: '排序未保存',
description: '拖拽后刷新丢失',
severity: 'major',
priority: 'P1',
reportedBy: 'QA',
assigneeId: 'Dev',
status: 'open',
logs: [],
createdAt: '2026-06-25T00:00:00.000Z',
updatedAt: '2026-06-25T00:00:00.000Z',
...patch,
};
}
test('open to fixing returns a single bug status patch', () => {
const result = applyBugTransition(bug(), 'fixing', 'Dev', {
now: new Date('2026-06-25T01:00:00.000Z'),
});
assert.equal(result.ok, true);
assert.equal(result.patch?.status, 'fixing');
assert.equal(result.patch?.logs?.length, 1);
});
test('fixing to fixed writes resolvedAt and resolution', () => {
const result = applyBugTransition(bug({ status: 'fixing' }), 'fixed', 'Dev', {
now: new Date('2026-06-25T02:00:00.000Z'),
resolution: '补充保存接口',
});
assert.equal(result.ok, true);
assert.equal(result.patch?.resolvedAt, '2026-06-25T02:00:00.000Z');
assert.equal(result.patch?.resolution, '补充保存接口');
});
test('verifying to closed writes closedAt', () => {
const result = applyBugTransition(bug({ status: 'verifying' }), 'closed', 'QA', {
now: new Date('2026-06-25T03:00:00.000Z'),
});
assert.equal(result.ok, true);
assert.equal(result.patch?.closedAt, '2026-06-25T03:00:00.000Z');
});
test('invalid bug transition is rejected', () => {
const result = applyBugTransition(bug(), 'closed', 'QA');
assert.equal(result.ok, false);
});

View File

@@ -0,0 +1,62 @@
import type { Bug, BugLog, BugStatus } from './bug';
import { canBugTransition } from './bug';
export interface BugTransitionOptions {
now?: Date;
resolution?: string;
}
export interface BugWorkflowResult {
ok: boolean;
patch?: Partial<Bug>;
message?: string;
}
function makeBugLog(
action: BugLog['action'],
operator: string,
nowIso: string,
from?: string,
to?: string,
remark?: string,
): BugLog {
return {
id: `log-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
action,
fromValue: from,
toValue: to,
operator,
remark,
createdAt: nowIso,
};
}
export function applyBugTransition(
bug: Bug,
to: BugStatus,
operator: string,
options: BugTransitionOptions = {},
): BugWorkflowResult {
if (!canBugTransition(bug.status, to)) {
return { ok: false, message: `不允许从「${bug.status}」流转到「${to}` };
}
const nowIso = (options.now ?? new Date()).toISOString();
const patch: Partial<Bug> = { status: to };
if (to === 'fixed') patch.resolvedAt = nowIso;
if (to === 'closed') patch.closedAt = nowIso;
if (options.resolution?.trim()) patch.resolution = options.resolution.trim();
const log = makeBugLog(
to === 'fixed' ? 'resolve' : 'status_change',
operator,
nowIso,
bug.status,
to,
options.resolution,
);
patch.logs = [...(bug.logs || []), log];
return { ok: true, patch };
}

View File

@@ -1,18 +1,17 @@
'use client'; 'use client';
import { create } from 'zustand'; import { create } from 'zustand';
import type { Bug, BugStatus, BugLog } from '@/lib/bug'; import type { Bug, BugStatus, BugLog } from '@/lib/bug';
import { canBugTransition, generateBugNo } from '@/lib/bug'; import { generateBugNo } from '@/lib/bug';
import { applyBugTransition } from '@/lib/bug-workflow';
import { loadServerData, saveServerData } from '@/lib/server-data';
const STORAGE_KEY = 'ftb_bugs_v1'; function saveStored(items: Bug[]) {
saveServerData('bugs', items).catch(() => {});
function saveLocal(items: Bug[]) {
try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); } catch {}
} }
function loadLocal(): Bug[] | null { async function loadStored(): Promise<Bug[] | null> {
try { try {
const raw = localStorage.getItem(STORAGE_KEY); return await loadServerData<Bug[]>('bugs');
if (raw) return JSON.parse(raw);
} catch {} } catch {}
return null; return null;
} }
@@ -23,7 +22,7 @@ function makeLog(action: BugLog['action'], operator: string, from?: string, to?:
interface BugState { interface BugState {
bugs: Bug[]; bugs: Bug[];
fetchBugs: () => void; fetchBugs: () => Promise<void>;
createBug: (data: Omit<Bug, 'id' | 'bugNo' | 'createdAt' | 'updatedAt' | 'status' | 'logs'>, operator: string) => Bug; createBug: (data: Omit<Bug, 'id' | 'bugNo' | 'createdAt' | 'updatedAt' | 'status' | 'logs'>, operator: string) => Bug;
updateBug: (id: string, data: Partial<Bug>) => void; updateBug: (id: string, data: Partial<Bug>) => void;
deleteBug: (id: string) => void; deleteBug: (id: string) => void;
@@ -37,8 +36,8 @@ interface BugState {
export const useBugStore = create<BugState>((set, get) => ({ export const useBugStore = create<BugState>((set, get) => ({
bugs: [], bugs: [],
fetchBugs: () => { fetchBugs: async () => {
const cached = loadLocal(); const cached = await loadStored();
if (cached) set({ bugs: cached }); if (cached) set({ bugs: cached });
}, },
@@ -57,7 +56,7 @@ export const useBugStore = create<BugState>((set, get) => ({
}; };
const updated = [...list, bug]; const updated = [...list, bug];
set({ bugs: updated }); set({ bugs: updated });
saveLocal(updated); saveStored(updated);
return bug; return bug;
}, },
@@ -66,29 +65,24 @@ export const useBugStore = create<BugState>((set, get) => ({
b.id === id ? { ...b, ...data, updatedAt: new Date().toISOString() } : b, b.id === id ? { ...b, ...data, updatedAt: new Date().toISOString() } : b,
); );
set({ bugs: updated }); set({ bugs: updated });
saveLocal(updated); saveStored(updated);
}, },
deleteBug: (id) => { deleteBug: (id) => {
const updated = get().bugs.filter((b) => b.id !== id); const updated = get().bugs.filter((b) => b.id !== id);
set({ bugs: updated }); set({ bugs: updated });
saveLocal(updated); saveStored(updated);
}, },
changeStatus: (id, to, operator, extra) => { changeStatus: (id, to, operator, extra) => {
const bug = get().bugs.find((b) => b.id === id); const bug = get().bugs.find((b) => b.id === id);
if (!bug) return { ok: false, message: 'Bug不存在' }; if (!bug) return { ok: false, message: 'Bug不存在' };
if (!canBugTransition(bug.status, to)) { const result = applyBugTransition(bug, to, operator, {
return { ok: false, message: `不允许从「${bug.status}」流转到「${to}` }; now: new Date(),
} resolution: extra?.resolution,
const now = new Date().toISOString(); });
const patch: Partial<Bug> = { status: to }; if (!result.ok || !result.patch) return { ok: false, message: result.message };
if (to === 'fixed') patch.resolvedAt = now; get().updateBug(id, result.patch);
if (to === 'closed') patch.closedAt = now;
if (extra?.resolution) patch.resolution = extra.resolution;
const log = makeLog(to === 'fixed' ? 'resolve' : 'status_change', operator, bug.status, to, extra?.resolution);
patch.logs = [...(bug.logs || []), log];
get().updateBug(id, patch);
return { ok: true }; return { ok: true };
}, },