1
0
forked from boos/BoosAPI

feat: add test for unchanged candidates when avoidSec is omitted and improve condition check in filterRecentlyFailedCandidates

This commit is contained in:
cita
2026-03-09 23:35:21 +08:00
parent 217dff0f72
commit 74d2e559ec
2 changed files with 29 additions and 6 deletions
+23
View File
@@ -76,6 +76,29 @@ describe('filterRecentlyFailedCandidates', () => {
expect(result.map((c) => c.id).sort()).toEqual(['a', 'b']);
});
it('keeps candidates unchanged when avoidSec is omitted', () => {
const nowMs = Date.now();
const candidates: Candidate[] = [
{
id: 'recent-failure',
channel: {
failCount: 3,
lastFailAt: new Date(nowMs - 5 * 1000).toISOString(),
},
},
{
id: 'healthy',
channel: {
failCount: 0,
lastFailAt: null,
},
},
];
const result = filterRecentlyFailedCandidates(candidates, nowMs);
expect(result).toEqual(candidates);
});
it('does not penalize stale failures outside the avoidance window', () => {
const nowMs = Date.now();
const candidates: Candidate[] = [
+6 -6
View File
@@ -167,13 +167,13 @@ export function filterRecentlyFailedCandidates<T extends { channel: FailureAware
nowMs = Date.now(),
avoidSec?: number,
): T[] {
if (candidates.length <= 1) return candidates;
if (avoidSec <= 0) return candidates;
if (candidates.length <= 1) return candidates;
if (avoidSec == null || avoidSec <= 0) return candidates;
const healthy = candidates.filter((candidate) => !isChannelRecentlyFailed(candidate.channel, nowMs, avoidSec));
// If all channels failed recently, keep them all and let weight/random decide.
return healthy.length > 0 ? healthy : candidates;
}
// If all channels failed recently, keep them all and let weight/random decide.
return healthy.length > 0 ? healthy : candidates;
}
export interface RouteDecisionCandidate {
channelId: number;