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']); 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', () => { it('does not penalize stale failures outside the avoidance window', () => {
const nowMs = Date.now(); const nowMs = Date.now();
const candidates: Candidate[] = [ const candidates: Candidate[] = [
+1 -1
View File
@@ -168,7 +168,7 @@ export function filterRecentlyFailedCandidates<T extends { channel: FailureAware
avoidSec?: number, avoidSec?: number,
): T[] { ): T[] {
if (candidates.length <= 1) return candidates; if (candidates.length <= 1) return candidates;
if (avoidSec <= 0) return candidates; if (avoidSec == null || avoidSec <= 0) return candidates;
const healthy = candidates.filter((candidate) => !isChannelRecentlyFailed(candidate.channel, nowMs, avoidSec)); const healthy = candidates.filter((candidate) => !isChannelRecentlyFailed(candidate.channel, nowMs, avoidSec));
// If all channels failed recently, keep them all and let weight/random decide. // If all channels failed recently, keep them all and let weight/random decide.