test(ai-review): 補使用量統計 NaN 安全測試並排除微優化誤報 #47

Merged
admin merged 15 commits from develop into master 2026-06-23 09:35:54 +00:00
Showing only changes of commit 791449161f - Show all commits
+36
View File
@@ -196,6 +196,25 @@ describe('resolveRemainingPercent', () => {
assert.match(pct.reason, /無上限/); assert.match(pct.reason, /無上限/);
}); });
it('returns null percent for non-finite or non-positive quota limits', () => {
for (const limit of [0, -5, Infinity, NaN, undefined]) {
const pct = resolveRemainingPercent({ available: true, used: 0, limit, remaining: limit, currency: 'USD' }, null);
assert.equal(pct.percent, null, `quota.limit=${limit} 應算不出百分比`);
}
});
it('returns null percent for non-finite or non-positive rate limits', () => {
for (const limit of [0, -1, Infinity, NaN]) {
const pct = resolveRemainingPercent({ available: false, reason: 'x' }, { hasData: true, remaining: limit, limit, kind: 'tokens' });
assert.equal(pct.percent, null, `rate.limit=${limit} 應算不出百分比`);
}
});
it('returns null percent when limit is finite but remaining is non-finite', () => {
const pct = resolveRemainingPercent({ available: true, used: 0, limit: 100, remaining: Infinity, currency: 'USD' }, null);
assert.equal(pct.percent, null);
});
it('does not divide by zero when quota.limit is 0', () => { it('does not divide by zero when quota.limit is 0', () => {
const pct = resolveRemainingPercent({ available: true, used: 5, limit: 0, currency: 'USD' }, { hasData: false }); const pct = resolveRemainingPercent({ available: true, used: 5, limit: 0, currency: 'USD' }, { hasData: false });
assert.equal(pct.percent, null); // limit > 0 守衛擋掉除以零 assert.equal(pct.percent, null); // limit > 0 守衛擋掉除以零
@@ -253,4 +272,21 @@ describe('formatUsageStatsLine', () => {
assert.doesNotMatch(line, /NaN/); assert.doesNotMatch(line, /NaN/);
assert.match(line, /剩餘可用: 無法計算/); assert.match(line, /剩餘可用: 無法計算/);
}); });
it('does not output Infinity/NaN/negative percent for invalid quota numbers', () => {
const ownUsage = { calls: 1, promptTokens: 1, completionTokens: 1, totalTokens: 2 };
for (const limit of [Infinity, 0, -5, NaN]) {
const line = formatUsageStatsLine('openai', 'm', ownUsage, { available: true, used: 0, limit, remaining: limit, currency: 'USD' }, null);
assert.doesNotMatch(line, /NaN|Infinity|-\d+%/);
assert.match(line, /剩餘可用: 無法計算/);
}
});
it('falls back to a valid rate percent when only the quota limit is invalid', () => {
const ownUsage = { calls: 1, promptTokens: 1, completionTokens: 1, totalTokens: 2 };
const line = formatUsageStatsLine('openai', 'm', ownUsage,
{ available: true, used: 0, limit: Infinity, currency: 'USD' }, // quota 無效
{ hasData: true, remaining: 150000, limit: 200000, kind: 'tokens' }); // rate 有效 → 75%
assert.match(line, /剩餘可用: 75%(速率配額/);
});
}); });