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
+11
View File
@@ -122,6 +122,17 @@ describe('judgeConversationsResolved', () => {
]);
});
it('ignores entries whose idx is not an integer (e.g. a string)', async () => {
const items = [{ idx: 0 }, { idx: 1 }];
// 字串 idx '0' 不可冒充整數 idx 0 把它改成 resolved
const chatFn = async () => [{ idx: '0', resolved: true }, { idx: 1.5, resolved: true }, { idx: 1, resolved: true }];
const verdicts = await judgeConversationsResolved(items, chatFn);
assert.deepEqual(verdicts, [
{ idx: 0, resolved: false },
{ idx: 1, resolved: true },
]);
});
it('propagates errors thrown by chatFn to the caller', async () => {
await assert.rejects(
() => judgeConversationsResolved([{ idx: 0 }], async () => { throw new Error('LLM down'); }),
+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', () => {