From 65af5e35a6d3348e38be7015317619f19f4b94c0 Mon Sep 17 00:00:00 2001 From: Developers Digest <124798203+developersdigest@users.noreply.github.com> Date: Wed, 16 Sep 2026 16:49:57 -0400 Subject: [PATCH] fix: omit inferred credit usage above plan allowance --- src/__tests__/commands/credit-usage.test.ts | 39 ++++++++++++++++++++- src/commands/credit-usage.ts | 10 ++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/__tests__/commands/credit-usage.test.ts b/src/__tests__/commands/credit-usage.test.ts index 13d9c712d4..719e270224 100644 --- a/src/__tests__/commands/credit-usage.test.ts +++ b/src/__tests__/commands/credit-usage.test.ts @@ -3,7 +3,10 @@ */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; -import { executeCreditUsage } from '../../commands/credit-usage'; +import { + executeCreditUsage, + handleCreditUsageCommand, +} from '../../commands/credit-usage'; import { initializeConfig } from '../../utils/config'; import { setupTest, teardownTest } from '../utils/mock-client'; @@ -25,6 +28,40 @@ describe('executeCreditUsage', () => { vi.clearAllMocks(); }); + it.each([ + [1109895, 'Remaining Credits: 1,109,895\nPlan Credits: 100,000\n'], + [ + 100000, + 'Remaining Credits: 100,000\nPlan Credits: 100,000\nUsed Credits: 0 (0.0%)\n', + ], + [ + 25000, + 'Remaining Credits: 25,000\nPlan Credits: 100,000\nUsed Credits: 75,000 (75.0%)\n', + ], + ])( + 'formats a remaining balance of %i without negative usage', + async (remainingCredits, expected) => { + mockFetch.mockResolvedValue({ + ok: true, + json: async () => ({ + data: { + remainingCredits, + planCredits: 100000, + billingPeriodStart: null, + billingPeriodEnd: null, + }, + }), + }); + const stdout = vi.spyOn(process.stdout, 'write').mockReturnValue(true); + try { + await handleCreditUsageCommand(); + expect(stdout).toHaveBeenCalledWith(expected); + } finally { + stdout.mockRestore(); + } + } + ); + describe('API call generation', () => { it('should make GET request to correct endpoint', async () => { const mockResponse = { diff --git a/src/commands/credit-usage.ts b/src/commands/credit-usage.ts index b064f5aefd..117d921003 100644 --- a/src/commands/credit-usage.ts +++ b/src/commands/credit-usage.ts @@ -111,10 +111,14 @@ function formatReadable(data: CreditUsageResult['data']): string { lines.push(`Remaining Credits: ${formatNumber(data.remainingCredits)}`); if (data.planCredits > 0) { - const usedCredits = data.planCredits - data.remainingCredits; - const usagePercent = ((usedCredits / data.planCredits) * 100).toFixed(1); lines.push(`Plan Credits: ${formatNumber(data.planCredits)}`); - lines.push(`Used Credits: ${formatNumber(usedCredits)} (${usagePercent}%)`); + if (data.remainingCredits <= data.planCredits) { + const usedCredits = data.planCredits - data.remainingCredits; + const usagePercent = ((usedCredits / data.planCredits) * 100).toFixed(1); + lines.push( + `Used Credits: ${formatNumber(usedCredits)} (${usagePercent}%)` + ); + } } // Format billing period if available