Files
ftb-project-management/apps/web/lib/xiaobao-risk-cache.test.ts
2026-06-29 18:37:39 +08:00

71 lines
2.4 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
import {
findCachedInsight,
upsertDailySnapshot,
upsertInsight,
type XiaobaoRiskInsightCacheItem,
} from './xiaobao-risk-cache';
const insightA: XiaobaoRiskInsightCacheItem = {
versionId: 'v-1',
riskSignature: 'sig-a',
insight: {
summary: '版本风险上升',
why: ['剩余工作较多'],
forecast: '可能延期 2 天',
suggestedActions: ['压缩低优先级范围'],
ownerHints: ['请项目负责人确认排期'],
generatedAt: '2026-06-29T08:00:00.000Z',
},
generatedAt: '2026-06-29T08:00:00.000Z',
};
test('findCachedInsight returns matching signature only', () => {
const rows: XiaobaoRiskInsightCacheItem[] = [
insightA,
{ ...insightA, versionId: 'v-2', riskSignature: 'sig-a' },
{ ...insightA, versionId: 'v-1', riskSignature: 'sig-b' },
];
assert.equal(findCachedInsight(rows, 'v-1', 'sig-a'), insightA);
assert.equal(findCachedInsight(rows, 'v-1', 'sig-b')?.versionId, 'v-1');
assert.equal(findCachedInsight(rows, 'v-2', 'sig-b'), undefined);
});
test('upsertDailySnapshot keeps one snapshot per version and date', () => {
const existing: XiaobaoRiskSnapshot = {
versionId: 'v-1',
date: '2026-06-29',
riskScore: 40,
riskLevel: 'attention',
openBugCount: 1,
failedTestCount: 0,
blockedCount: 0,
silentRiskCount: 0,
confidence: 80,
createdAt: '2026-06-29T08:00:00.000Z',
};
const replacement: XiaobaoRiskSnapshot = { ...existing, riskScore: 72, createdAt: '2026-06-29T09:00:00.000Z' };
const otherDay: XiaobaoRiskSnapshot = { ...existing, date: '2026-06-28', createdAt: '2026-06-28T09:00:00.000Z' };
const result = upsertDailySnapshot([existing, otherDay], replacement);
assert.deepEqual(result, [replacement, otherDay]);
});
test('upsertInsight replaces existing version signature pair', () => {
const replacement: XiaobaoRiskInsightCacheItem = {
...insightA,
insight: { ...insightA.insight, summary: '已重新生成' },
generatedAt: '2026-06-29T09:00:00.000Z',
};
const otherSignature: XiaobaoRiskInsightCacheItem = { ...insightA, riskSignature: 'sig-b' };
const otherVersion: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'v-2' };
const result = upsertInsight([insightA, otherSignature, otherVersion], replacement);
assert.deepEqual(result, [replacement, otherSignature, otherVersion]);
});