feat(v2.4): 切换测试与缺陷主写

This commit is contained in:
2026-07-08 14:15:49 +08:00
parent 1bf630a7bc
commit 2358dbd0b5
18 changed files with 1458 additions and 30 deletions

View File

@@ -1,7 +1,9 @@
import { api } from './api';
import type { Bug, BugSeverity, BugStatus } from './bug';
import type { DevTask, DevTaskStatus, Reference } from './dev-task';
import type { Priority } from './derive';
import type { Requirement, RequirementStatus, SourceType } from './requirement';
import type { TestCase, TestCaseStatus } from './test-case';
import type { VersionPlan, VersionPlanLog, VersionPlanRequirementCoverage } from './version-plan';
import type { WorkActivity, WorkActivityCategory, WorkActivitySourceType } from './work-activity';
@@ -194,6 +196,52 @@ interface DomainDevTaskRow {
updatedAt?: string | Date | null;
}
interface DomainTestCaseRow {
id: string;
versionId: string;
requirementId?: string | null;
categoryId?: string | null;
code: string;
title: string;
description?: string | null;
status?: string | null;
roundNo?: number | null;
priority?: string | number | null;
assigneeId?: string | null;
creatorId?: string | null;
plannedTestAt?: string | Date | null;
plannedEndAt?: string | Date | null;
startedAt?: string | Date | null;
completedAt?: string | Date | null;
estimateHours?: number | null;
aiEstimateHours?: number | null;
references?: unknown;
aiDraft?: boolean | null;
aiDraftAt?: string | Date | null;
createdAt?: string | Date | null;
updatedAt?: string | Date | null;
}
interface DomainBugRow {
id: string;
versionId: string;
testCaseId?: string | null;
code: string;
title: string;
description?: string | null;
status?: string | null;
severity?: string | null;
priority?: string | number | null;
assigneeId?: string | null;
reporterId?: string | null;
plannedFixAt?: string | Date | null;
resolvedAt?: string | Date | null;
closedAt?: string | Date | null;
resolution?: string | null;
createdAt?: string | Date | null;
updatedAt?: string | Date | null;
}
interface DomainWorkActivityRow {
id: string;
actorId?: string | null;
@@ -211,6 +259,11 @@ interface DomainMutationResponse<T> {
activities?: DomainWorkActivityRow[];
}
interface DomainBatchMutationResponse<T> {
items: T[];
activities?: DomainWorkActivityRow[];
}
export async function listVersionPlansByVersionId(versionId: string): Promise<VersionPlan[]> {
const rows = await api.get<DomainVersionPlanRow[]>(`/versions/${versionId}/plans`);
return rows.map(normalizeVersionPlan);
@@ -301,6 +354,88 @@ export async function deleteDevTaskByVersionId(versionId: string, taskId: string
await api.delete(`/versions/${versionId}/dev-tasks/${taskId}`);
}
export async function listTestCasesByVersionId(versionId: string): Promise<TestCase[]> {
const rows = await api.get<DomainTestCaseRow[]>(`/versions/${versionId}/test-cases`);
return rows.map(normalizeTestCase);
}
export async function createTestCaseByVersionId(versionId: string, data: Partial<TestCase>) {
return normalizeMutation(
await api.post<DomainMutationResponse<DomainTestCaseRow>>(`/versions/${versionId}/test-cases`, toTestCasePayload(data)),
normalizeTestCase,
);
}
export async function createTestCasesByVersionId(versionId: string, items: Partial<TestCase>[]) {
return normalizeBatchMutation(
await api.post<DomainBatchMutationResponse<DomainTestCaseRow>>(
`/versions/${versionId}/test-cases/batch`,
{ items: items.map(toTestCasePayload) },
),
normalizeTestCase,
);
}
export async function updateTestCaseByVersionId(versionId: string, testCaseId: string, data: Partial<TestCase>) {
return normalizeMutation(
await api.patch<DomainMutationResponse<DomainTestCaseRow>>(`/versions/${versionId}/test-cases/${testCaseId}`, toTestCasePayload(data)),
normalizeTestCase,
);
}
export async function updateTestCaseStatusByVersionId(versionId: string, testCaseId: string, status: TestCaseStatus) {
return normalizeMutation(
await api.patch<DomainMutationResponse<DomainTestCaseRow>>(`/versions/${versionId}/test-cases/${testCaseId}/status`, { status }),
normalizeTestCase,
);
}
export async function deleteTestCaseByVersionId(versionId: string, testCaseId: string): Promise<void> {
await api.delete(`/versions/${versionId}/test-cases/${testCaseId}`);
}
export async function listBugsByVersionId(versionId: string): Promise<Bug[]> {
const rows = await api.get<DomainBugRow[]>(`/versions/${versionId}/bugs`);
return rows.map(normalizeBug);
}
export async function createBugByVersionId(versionId: string, data: Partial<Bug>) {
return normalizeMutation(
await api.post<DomainMutationResponse<DomainBugRow>>(`/versions/${versionId}/bugs`, toBugPayload(data)),
normalizeBug,
);
}
export async function updateBugByVersionId(versionId: string, bugId: string, data: Partial<Bug>) {
return normalizeMutation(
await api.patch<DomainMutationResponse<DomainBugRow>>(`/versions/${versionId}/bugs/${bugId}`, toBugPayload(data)),
normalizeBug,
);
}
export async function updateBugStatusByVersionId(
versionId: string,
bugId: string,
status: BugStatus,
options: { operator?: string; resolution?: string } = {},
) {
return normalizeMutation(
await api.patch<DomainMutationResponse<DomainBugRow>>(`/versions/${versionId}/bugs/${bugId}/status`, { status, ...options }),
normalizeBug,
);
}
export async function transferBugByVersionId(versionId: string, bugId: string, assigneeId: string, operator?: string) {
return normalizeMutation(
await api.patch<DomainMutationResponse<DomainBugRow>>(`/versions/${versionId}/bugs/${bugId}/transfer`, { assigneeId, operator }),
normalizeBug,
);
}
export async function deleteBugByVersionId(versionId: string, bugId: string): Promise<void> {
await api.delete(`/versions/${versionId}/bugs/${bugId}`);
}
function normalizeProductRoot(product: RootProduct): RootProduct {
return {
...product,
@@ -508,6 +643,116 @@ function toDevTaskStatus(value: string | null | undefined): DevTaskStatus {
return value === 'in_progress' || value === 'testing' || value === 'submitted' ? value : 'todo';
}
function toTestCasePayload(data: Partial<TestCase>) {
return {
...(data.versionId !== undefined && { versionId: data.versionId }),
...(data.requirementId !== undefined && { requirementId: data.requirementId }),
...(data.categoryId !== undefined && { categoryId: data.categoryId }),
...(data.caseNo !== undefined && { caseNo: data.caseNo }),
...(data.title !== undefined && { title: data.title }),
...(data.description !== undefined && { description: data.description }),
...(data.status !== undefined && { status: data.status }),
...(data.roundNo !== undefined && { roundNo: data.roundNo }),
...(data.priority !== undefined && { priority: priorityToNumber(data.priority) }),
...(data.assigneeId !== undefined && { assigneeId: data.assigneeId }),
...(data.createdBy !== undefined && { createdBy: data.createdBy }),
...(data.plannedTestAt !== undefined && { plannedTestAt: data.plannedTestAt }),
...(data.plannedEndAt !== undefined && { plannedEndAt: data.plannedEndAt }),
...(data.startedAt !== undefined && { startedAt: data.startedAt }),
...(data.completedAt !== undefined && { completedAt: data.completedAt }),
...(data.estimateHours !== undefined && { estimateHours: data.estimateHours }),
...(data.aiEstimateHours !== undefined && { aiEstimateHours: data.aiEstimateHours }),
...(data.references !== undefined && { references: data.references }),
...(data.aiDraft !== undefined && { aiDraft: data.aiDraft }),
...(data.aiDraftAt !== undefined && { aiDraftAt: data.aiDraftAt }),
};
}
function normalizeTestCase(row: DomainTestCaseRow): TestCase {
return {
id: row.id,
caseNo: row.code,
versionId: row.versionId,
requirementId: row.requirementId ?? undefined,
roundNo: row.roundNo && row.roundNo > 0 ? Math.floor(row.roundNo) : 1,
title: row.title,
description: row.description ?? '',
categoryId: row.categoryId ?? '',
priority: toPriority(row.priority),
assigneeId: row.assigneeId ?? undefined,
status: toTestCaseStatus(row.status),
estimateHours: row.estimateHours ?? undefined,
aiEstimateHours: row.aiEstimateHours ?? undefined,
plannedTestAt: optionalIso(row.plannedTestAt),
plannedEndAt: optionalIso(row.plannedEndAt),
startedAt: optionalIso(row.startedAt),
completedAt: optionalIso(row.completedAt),
executedAt: optionalIso(row.completedAt),
references: asArray<Reference>(row.references),
aiDraft: Boolean(row.aiDraft),
aiDraftAt: optionalIso(row.aiDraftAt),
createdBy: row.creatorId ?? '',
createdAt: isoString(row.createdAt),
updatedAt: isoString(row.updatedAt),
};
}
function toTestCaseStatus(value: string | null | undefined): TestCaseStatus {
if (value === 'running' || value === 'passed' || value === 'failed' || value === 'blocked') return value;
return 'pending';
}
function toBugPayload(data: Partial<Bug>) {
return {
...(data.versionId !== undefined && { versionId: data.versionId }),
...(data.testCaseId !== undefined && { testCaseId: data.testCaseId }),
...(data.bugNo !== undefined && { bugNo: data.bugNo }),
...(data.title !== undefined && { title: data.title }),
...(data.description !== undefined && { description: data.description }),
...(data.status !== undefined && { status: data.status }),
...(data.severity !== undefined && { severity: data.severity }),
...(data.priority !== undefined && { priority: priorityToNumber(data.priority) }),
...(data.assigneeId !== undefined && { assigneeId: data.assigneeId }),
...(data.reportedBy !== undefined && { reportedBy: data.reportedBy }),
...(data.plannedFixAt !== undefined && { plannedFixAt: data.plannedFixAt }),
...(data.resolvedAt !== undefined && { resolvedAt: data.resolvedAt }),
...(data.closedAt !== undefined && { closedAt: data.closedAt }),
...(data.resolution !== undefined && { resolution: data.resolution }),
};
}
function normalizeBug(row: DomainBugRow): Bug {
return {
id: row.id,
bugNo: row.code,
versionId: row.versionId,
testCaseId: row.testCaseId ?? '',
title: row.title,
description: row.description ?? '',
severity: toBugSeverity(row.severity),
priority: toPriority(row.priority),
reportedBy: row.reporterId ?? '',
assigneeId: row.assigneeId ?? '',
status: toBugStatus(row.status),
resolvedAt: optionalIso(row.resolvedAt),
closedAt: optionalIso(row.closedAt),
resolution: row.resolution ?? undefined,
plannedFixAt: optionalIso(row.plannedFixAt),
createdAt: isoString(row.createdAt),
updatedAt: isoString(row.updatedAt),
};
}
function toBugStatus(value: string | null | undefined): BugStatus {
if (value === 'fixing' || value === 'fixed' || value === 'verifying' || value === 'closed' || value === 'rejected') return value;
return 'open';
}
function toBugSeverity(value: string | null | undefined): BugSeverity {
if (value === 'critical' || value === 'major' || value === 'trivial') return value;
return 'minor';
}
function normalizeMutation<Row, Item>(
response: DomainMutationResponse<Row>,
mapper: (row: Row) => Item,
@@ -518,6 +763,16 @@ function normalizeMutation<Row, Item>(
};
}
function normalizeBatchMutation<Row, Item>(
response: DomainBatchMutationResponse<Row>,
mapper: (row: Row) => Item,
): { items: Item[]; activities: WorkActivity[] } {
return {
items: (response.items ?? []).map(mapper),
activities: (response.activities ?? []).map(normalizeWorkActivity),
};
}
function normalizeWorkActivity(row: DomainWorkActivityRow): WorkActivity {
const metadata = isRecord(row.metadata) ? row.metadata : {};
return {

View File

@@ -0,0 +1,53 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import test from 'node:test';
const testCaseStore = () => readFileSync(join(process.cwd(), 'stores/useTestCaseStore.ts'), 'utf8');
const bugStore = () => readFileSync(join(process.cwd(), 'stores/useBugStore.ts'), 'utf8');
function storeMethodBody(text: string, name: string) {
const implementationStart = text.indexOf('export const');
assert.notEqual(implementationStart, -1, 'missing store implementation');
const start = text.indexOf(` ${name}:`, implementationStart);
assert.notEqual(start, -1, `missing store method ${name}`);
let depth = 0;
let sawFirstBrace = false;
for (let i = start; i < text.length; i += 1) {
const char = text[i];
if (char === '{') {
depth += 1;
sawFirstBrace = true;
}
if (char === '}') {
depth -= 1;
if (sawFirstBrace && depth === 0) return text.slice(start, i + 1);
}
}
throw new Error(`could not extract store method ${name}`);
}
test('test case store uses domain APIs for version-scoped writes', () => {
const text = testCaseStore();
assert.match(text, /from '@\/lib\/domain-api'/);
assert.match(storeMethodBody(text, 'createTestCase'), /createTestCaseByVersionId\(tc\.versionId,/);
assert.match(storeMethodBody(text, 'createTestCases'), /createTestCasesByVersionId\(created\[0\]\.versionId,/);
assert.match(storeMethodBody(text, 'updateTestCase'), /updateTestCaseByVersionId\(versionId, id,/);
assert.match(storeMethodBody(text, 'changeStatus'), /updateTestCaseStatusByVersionId\(tc\.versionId,/);
assert.doesNotMatch(storeMethodBody(text, 'createTestCase'), /saveServerData\('test-cases'/);
assert.doesNotMatch(storeMethodBody(text, 'updateTestCase'), /saveServerData\('test-cases'/);
});
test('bug store uses domain APIs for version-scoped writes', () => {
const text = bugStore();
assert.match(text, /from '@\/lib\/domain-api'/);
assert.match(storeMethodBody(text, 'createBug'), /createBugByVersionId\(bug\.versionId,/);
assert.match(storeMethodBody(text, 'updateBug'), /updateBugByVersionId\(versionId, id,/);
assert.match(storeMethodBody(text, 'changeStatus'), /updateBugStatusByVersionId\(bug\.versionId,/);
assert.match(storeMethodBody(text, 'transferBug'), /transferBugByVersionId\(bug\.versionId,/);
assert.doesNotMatch(storeMethodBody(text, 'createBug'), /saveServerData\('bugs'/);
assert.doesNotMatch(storeMethodBody(text, 'updateBug'), /saveServerData\('bugs'/);
});