test(ai-review): 補使用量統計 NaN 安全測試並排除微優化誤報 #47
+568
-520
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,10 @@
|
||||
[
|
||||
{
|
||||
"level": "warning",
|
||||
"level": "info",
|
||||
"role": "Maya",
|
||||
"location": "app/usage.test.js:240",
|
||||
"problem": "`formatUsageStatsLine` 測試案例中,僅驗證了單一平台的格式,缺失了當 `quota` 或 `rate` 資料缺失或包含無效數字(如 `NaN`)時的處理測試。",
|
||||
"suggestion": "補充針對 `quota` 或 `rate` 傳入異常資料(如 `limit: NaN`)的測試,驗證 `formatUsageStatsLine` 是否能產生安全的預設文字,而非輸出 `NaN` 或破壞版面。",
|
||||
"is_new": true
|
||||
},
|
||||
{
|
||||
"level": "warning",
|
||||
"role": "Rogue",
|
||||
"location": "app/usage.js:146",
|
||||
"problem": "在 `recordRateLimit` 中頻繁呼叫 `lowerCaseKeys`,這會對每個請求的 headers 進行複製與轉換,增加記憶體分配開銷。",
|
||||
"suggestion": "建議直接存取 headers 時改用不區分大小寫的存取函式,避免複製整個物件。",
|
||||
"location": "app/usage.test.js:217",
|
||||
"problem": "測試案例 `returns null percent when rate.remaining is null/undefined` 僅驗證了 remaining 為 null/undefined,但未驗證當 rate.limit 為 null/undefined 時的情況。雖然這可能由 `calculatePercent` 內部處理,但針對 `resolveRemainingPercent` 這一層級的整合測試仍不完整。",
|
||||
"suggestion": "建議補上一個測試案例,明確測試當 `rate.limit` 為 null/undefined 時,`resolveRemainingPercent` 的行為是否符合預期。",
|
||||
"is_new": true
|
||||
}
|
||||
]
|
||||
|
||||
+3
-1
@@ -115,7 +115,9 @@ async function main() {
|
||||
// Step7 過濾:套用排除規則 + 防守方 AI 誤報裁決
|
||||
step('Step7', '排除規則與誤報過濾');
|
||||
if (reconcile.excludedFindings.length > 0) {
|
||||
appendExclusions(WORKSPACE, reconcile.excludedFindings, repoDir || WORKSPACE);
|
||||
// 以 repoDir 為主(即將提交回去的來源分支副本),WORKSPACE 為鏡像;
|
||||
// 順序須與下方 loadExclusions 一致,否則會讀到空的 WORKSPACE 而把既有排除規則覆蓋掉。
|
||||
appendExclusions(repoDir || WORKSPACE, reconcile.excludedFindings, WORKSPACE);
|
||||
}
|
||||
const exclusions = loadExclusions(repoDir || WORKSPACE, repoState, WORKSPACE);
|
||||
input(`待過濾 ${sorted.length} 筆;排除規則 ${exclusions.length} 條`);
|
||||
|
||||
+27
-10
@@ -211,23 +211,40 @@ function round1(n) {
|
||||
|
||||
const RATE_KIND_LABEL = { tokens: 'token', requests: '次數' };
|
||||
|
||||
/**
|
||||
* 計算「剩餘百分比」= remaining / limit × 100。
|
||||
* limit 或 remaining 為 null/undefined/NaN/Infinity,或 limit ≤ 0 時回 null,
|
||||
* 避免算出 Infinity%/NaN%/負百分比或除以零。
|
||||
*/
|
||||
function calculatePercent(remaining, limit) {
|
||||
if (remaining == null || limit == null) return null;
|
||||
const rem = Number(remaining);
|
||||
const lim = Number(limit);
|
||||
if (!Number.isFinite(rem) || !Number.isFinite(lim) || lim <= 0) return null;
|
||||
return round1((rem / lim) * 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* 計算「剩餘可用百分比」,依優先序擇一:
|
||||
* 1. 帳號額度(quota 有上限)→ 剩餘 credits / 上限;
|
||||
* 2. 速率配額(rate limit header)→ 當前視窗剩餘 / 上限;
|
||||
* 皆無法取得時回傳 { percent: null, reason }。
|
||||
* 1. 帳號額度(quota 有有效上限)→ 剩餘 credits / 上限;
|
||||
* 2. 速率配額(rate limit header,有有效上限)→ 當前視窗剩餘 / 上限;
|
||||
* 上限或剩餘為無效值(null/0/負數/NaN/Infinity)時跳過計算,落到 { percent: null, reason }。
|
||||
*/
|
||||
export function resolveRemainingPercent(quota, rate) {
|
||||
if (quota?.available && quota.limit != null && Number(quota.limit) > 0) {
|
||||
if (quota?.available && quota.limit != null) {
|
||||
const limit = Number(quota.limit);
|
||||
const remaining = quota.remaining == null ? limit - num(quota.used) : Number(quota.remaining);
|
||||
return { percent: round1((remaining / limit) * 100), basis: '帳號額度', remaining, limit, unit: quota.currency || '' };
|
||||
const percent = calculatePercent(remaining, limit);
|
||||
if (percent != null) {
|
||||
return { percent, basis: '帳號額度', remaining, limit, unit: quota.currency || '' };
|
||||
}
|
||||
}
|
||||
if (rate?.hasData && Number(rate.limit) > 0) {
|
||||
const limit = Number(rate.limit);
|
||||
const remaining = Number(rate.remaining);
|
||||
const kindLabel = RATE_KIND_LABEL[rate.kind] || rate.kind;
|
||||
return { percent: round1((remaining / limit) * 100), basis: `速率配額(當前視窗,${kindLabel})`, remaining, limit, unit: '' };
|
||||
if (rate?.hasData) {
|
||||
const percent = calculatePercent(rate.remaining, rate.limit);
|
||||
if (percent != null) {
|
||||
const kindLabel = RATE_KIND_LABEL[rate.kind] || rate.kind;
|
||||
return { percent, basis: `速率配額(當前視窗,${kindLabel})`, remaining: Number(rate.remaining), limit: Number(rate.limit), unit: '' };
|
||||
}
|
||||
}
|
||||
let reason;
|
||||
if (quota?.available && quota.limit == null) reason = '帳號額度無上限,無法計算百分比';
|
||||
|
||||
@@ -196,6 +196,32 @@ describe('resolveRemainingPercent', () => {
|
||||
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 rate.remaining is null/undefined', () => {
|
||||
for (const remaining of [null, undefined]) {
|
||||
const pct = resolveRemainingPercent({ available: false, reason: 'x' }, { hasData: true, remaining, limit: 200000, kind: 'tokens' });
|
||||
assert.equal(pct.percent, null, `rate.remaining=${remaining} 應算不出百分比`);
|
||||
}
|
||||
});
|
||||
|
||||
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', () => {
|
||||
const pct = resolveRemainingPercent({ available: true, used: 5, limit: 0, currency: 'USD' }, { hasData: false });
|
||||
assert.equal(pct.percent, null); // limit > 0 守衛擋掉除以零
|
||||
@@ -244,4 +270,30 @@ describe('formatUsageStatsLine', () => {
|
||||
const line = formatUsageStatsLine('ollama', 'llama3', usage, { available: false, reason: '本地服務,無帳號額度概念' }, { hasData: false });
|
||||
assert.match(line, /;剩餘可用: 無法計算(本地服務,無帳號額度概念)/);
|
||||
});
|
||||
|
||||
it('produces safe text (no NaN) when quota/rate carry invalid numbers', () => {
|
||||
// quota.limit 為 NaN、rate.limit 為 NaN → 不應算出百分比、不得輸出 NaN
|
||||
const line = formatUsageStatsLine('openai', 'm', usage,
|
||||
{ available: true, used: 5, limit: NaN, currency: 'USD' },
|
||||
{ hasData: true, remaining: NaN, limit: NaN, kind: 'tokens' });
|
||||
assert.doesNotMatch(line, /NaN/);
|
||||
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%(速率配額/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user