Display provider credits in the UI

This commit is contained in:
Alex Gleason committed 2025-09-19 17:12:57 -05:00
1 parent e570ee0236
commit 234418babd
4 files changed
+250 -14

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { CreditsBadge } from './CreditsBadge';
import { TestApp } from '@/test/TestApp';
import type { AIConnection } from '@/contexts/AISettingsContext';
// Mock the useAICredits hook
vi.mock('@/hooks/useAICredits', () => ({
useAICredits: vi.fn(),
}));
import { useAICredits } from '@/hooks/useAICredits';
const mockUseAICredits = vi.mocked(useAICredits);
describe('CreditsBadge', () => {
const mockConnection: AIConnection = {
baseURL: 'https://api.example.com/v1',
apiKey: 'test-key',
};
it('should render credits badge with formatted amount', () => {
mockUseAICredits.mockReturnValue({
data: { object: 'credits', amount: 25.50 },
isLoading: false,
error: null,
isSuccess: true,
isError: false,
} as any);
render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(screen.getByText('$25.50')).toBeInTheDocument();
});
it('should not render anything when loading', () => {
mockUseAICredits.mockReturnValue({
data: undefined,
isLoading: true,
error: null,
isSuccess: false,
isError: false,
} as any);
const { container } = render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(container.firstChild).toBeNull();
});
it('should not render anything when there is an error', () => {
mockUseAICredits.mockReturnValue({
data: undefined,
isLoading: false,
error: new Error('Failed to fetch'),
isSuccess: false,
isError: true,
} as any);
const { container } = render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(container.firstChild).toBeNull();
});
it('should not render anything when no credits data is available', () => {
mockUseAICredits.mockReturnValue({
data: undefined,
isLoading: false,
error: null,
isSuccess: true,
isError: false,
} as any);
const { container } = render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(container.firstChild).toBeNull();
});
it('should format large amounts correctly', () => {
mockUseAICredits.mockReturnValue({
data: { object: 'credits', amount: 1234.56 },
isLoading: false,
error: null,
isSuccess: true,
isError: false,
} as any);
render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(screen.getByText('$1,234.56')).toBeInTheDocument();
});
it('should format zero amounts correctly', () => {
mockUseAICredits.mockReturnValue({
data: { object: 'credits', amount: 0 },
isLoading: false,
error: null,
isSuccess: true,
isError: false,
} as any);
render(
<TestApp>
<CreditsBadge providerId="test-provider" connection={mockConnection} />
</TestApp>
);
expect(screen.getByText('$0.00')).toBeInTheDocument();
});
});
+40
View File
@@ -0,0 +1,40 @@
import { Badge } from '@/components/ui/badge';
import { useAICredits } from '@/hooks/useAICredits';
import type { AIConnection } from '@/contexts/AISettingsContext';
interface CreditsBadgeProps {
providerId: string;
connection: AIConnection;
className?: string;
}
/**
* Displays a credits badge showing the remaining credits for an AI provider
*/
export function CreditsBadge({ providerId, connection, className }: CreditsBadgeProps) {
const { data: credits, isLoading, error } = useAICredits(providerId, connection);
// Don't render anything if there's an error (provider doesn't support credits endpoint)
if (error || isLoading) {
return null;
}
// Don't render if no credits data is available
if (!credits) {
return null;
}
// Format the amount as currency
const formattedAmount = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(credits.amount);
return (
<Badge variant="secondary" className={className}>
{formattedAmount}
</Badge>
);
}