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 };
}