test(ai-review): 補非整數 idx、空 apiKeys 與 limit 為 0 的邊界測試

This commit is contained in:
Jeffery
2026-06-23 13:34:41 +08:00
parent ff075c8438
commit 1f9e0d906c
2 changed files with 25 additions and 0 deletions
+14
View File
@@ -123,6 +123,14 @@ describe('fetchAccountQuota', () => {
assert.equal(q.available, false);
assert.match(q.reason, /未支援/);
});
it('degrades gracefully when apiKeys is empty or undefined', async () => {
const get = async () => { throw new Error('should not be called'); };
// 空陣列 / 未提供 key 都不應丟錯,依平台回報無法取得或不適用
assert.equal((await fetchAccountQuota('openai', { apiKeys: [], baseURL: 'https://api.openai.com/v1' }, { get })).available, false);
assert.equal((await fetchAccountQuota('ollama', { apiKeys: [] }, { get })).available, false);
assert.equal((await fetchAccountQuota('claude', {}, { get })).available, false);
});
});
describe('recordRateLimit / getRateLimit', () => {
@@ -178,6 +186,12 @@ describe('resolveRemainingPercent', () => {
assert.equal(pct.percent, null);
assert.match(pct.reason, /無上限/);
});
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 守衛擋掉除以零
assert.ok(typeof pct.reason === 'string' && pct.reason.length > 0);
});
});
describe('formatUsageStats', () => {