Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/__tests__/commands/credit-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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 = {
Expand Down
10 changes: 7 additions & 3 deletions src/commands/credit-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading