Get accurate OpenRouter pricing

This commit is contained in:
Alex Gleason committed 2025-09-23 18:28:51 -05:00
1 parent aa35b13ad3
commit d0a520ce17
11 files changed
+81 -94

No files matched your search

+6 -16
View File
@@ -3,7 +3,6 @@ import { render, screen, fireEvent } from '@testing-library/react';
import { TestApp } from '@/test/TestApp';
import { CreditsBadge } from './CreditsBadge';
// Mock the useAICredits hook
vi.mock('@/hooks/useAICredits', () => ({
useAICredits: () => ({
@@ -16,7 +15,8 @@ vi.mock('@/hooks/useAICredits', () => ({
// Mock the fetch function
global.fetch = vi.fn();
const mockConnection = {
const mockProvider = {
id: 'test-provider',
baseURL: 'https://api.test.com/v1',
apiKey: 'test-key',
};
@@ -25,10 +25,7 @@ describe('CreditsBadge', () => {
it('renders the credits amount', () => {
render(
<TestApp>
<CreditsBadge
providerId="test-provider"
connection={mockConnection}
/>
<CreditsBadge provider={mockProvider} />
</TestApp>
);
@@ -41,8 +38,7 @@ describe('CreditsBadge', () => {
render(
<TestApp>
<CreditsBadge
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
onOpenDialog={mockCallback}
/>
</TestApp>
@@ -57,10 +53,7 @@ describe('CreditsBadge', () => {
it('has proper styling for clickable badge', () => {
render(
<TestApp>
<CreditsBadge
providerId="test-provider"
connection={mockConnection}
/>
<CreditsBadge provider={mockProvider} />
</TestApp>
);
@@ -72,10 +65,7 @@ describe('CreditsBadge', () => {
it('does not render dialog directly', () => {
render(
<TestApp>
<CreditsBadge
providerId="test-provider"
connection={mockConnection}
/>
<CreditsBadge provider={mockProvider} />
</TestApp>
);
+4 -8
View File
@@ -1,13 +1,9 @@
import { Badge } from '@/components/ui/badge';
import { AIProvider } from '@/contexts/AISettingsContext';
import { useAICredits } from '@/hooks/useAICredits';
interface CreditsBadgeProps {
providerId: string;
connection: {
baseURL: string;
apiKey?: string;
nostr?: boolean;
};
provider: AIProvider;
className?: string;
onOpenDialog?: () => void;
}
@@ -15,8 +11,8 @@ interface CreditsBadgeProps {
/**
* Displays a clickable credits badge showing the remaining credits for an AI provider
*/
export function CreditsBadge({ providerId, connection, className, onOpenDialog }: CreditsBadgeProps) {
const { data: credits, isLoading, error } = useAICredits(providerId, connection);
export function CreditsBadge({ provider, className, onOpenDialog }: CreditsBadgeProps) {
const { data: credits, isLoading, error } = useAICredits(provider);
// Don't render anything if there's an error (provider doesn't support credits endpoint)
if (error || isLoading) {
+10 -14
View File
@@ -2,14 +2,16 @@ import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { TestApp } from '@/test/TestApp';
import { CreditsDialog } from './CreditsDialog';
import type { AIProvider } from '@/contexts/AISettingsContext';
// Mock the fetch function
global.fetch = vi.fn();
const mockConnection = {
const mockProvider: AIProvider = {
id: 'test-provider',
baseURL: 'https://api.test.com/v1',
apiKey: 'test-key',
nostr: false,
};
describe('CreditsDialog', () => {
@@ -19,8 +21,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -36,8 +37,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={false}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -51,8 +51,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -67,8 +66,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -89,8 +87,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={false}
onOpenChange={mockCallback}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -101,8 +98,7 @@ describe('CreditsDialog', () => {
<CreditsDialog
open={true}
onOpenChange={mockCallback}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
+12 -15
View File
@@ -13,15 +13,12 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@
import { useToast } from '@/hooks/useToast';
import { createAIClient } from '@/lib/ai-client';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { AIProvider } from '@/contexts/AISettingsContext';
interface CreditsDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
providerId: string;
connection: {
baseURL: string;
apiKey?: string;
nostr?: boolean;
};
provider: AIProvider;
}
interface Payment {
@@ -184,7 +181,7 @@ function LightningPayment({ invoice, amount, onClose }: LightningPaymentProps) {
);
}
export function CreditsDialog({ open, onOpenChange, providerId, connection }: CreditsDialogProps) {
export function CreditsDialog({ open, onOpenChange, provider }: CreditsDialogProps) {
const { user } = useCurrentUser();
const { toast } = useToast();
const queryClient = useQueryClient();
@@ -196,9 +193,9 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// Query for payment history
const { data: payments, isLoading: isLoadingPayments } = useQuery({
queryKey: ['ai-payments', connection.nostr ? user?.pubkey ?? '' : '', providerId],
queryKey: ['ai-payments', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
queryFn: async (): Promise<Payment[]> => {
const ai = createAIClient(connection, user);
const ai = createAIClient(provider, user);
const data = await ai.get('/credits/payments?limit=50') as PaymentsResponse;
return data.data;
},
@@ -210,7 +207,7 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// Mutation for adding credits
const addCreditsMutation = useMutation({
mutationFn: async (request: AddCreditsRequest): Promise<Payment> => {
const ai = createAIClient(connection, user);
const ai = createAIClient(provider, user);
return await ai.post('/credits/add', {
body: request,
}) as Payment;
@@ -230,12 +227,12 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// Refresh payments list
queryClient.invalidateQueries({
queryKey: ['ai-payments', connection.nostr ? user?.pubkey ?? '' : '', providerId],
queryKey: ['ai-payments', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
});
// Refresh credits balance
queryClient.invalidateQueries({
queryKey: ['ai-credits', connection.nostr ? user?.pubkey ?? '' : '', providerId],
queryKey: ['ai-credits', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
});
},
onError: (error: Error) => {
@@ -251,7 +248,7 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// Mutation for checking payment status
const checkPaymentMutation = useMutation({
mutationFn: async (paymentId: string): Promise<Payment> => {
const ai = createAIClient(connection, user);
const ai = createAIClient(provider, user);
return await ai.get(`/credits/payments/${paymentId}`) as Payment;
},
onMutate: async (paymentId: string) => {
@@ -268,7 +265,7 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// Update the specific payment in the cache instead of refetching
queryClient.setQueryData<Payment[]>(
['ai-payments', connection.nostr ? user?.pubkey ?? '' : '', providerId],
['ai-payments', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
(oldPayments) => {
if (!oldPayments) return oldPayments;
return oldPayments.map(payment =>
@@ -280,7 +277,7 @@ export function CreditsDialog({ open, onOpenChange, providerId, connection }: Cr
// If payment completed, refresh credits balance
if (updatedPayment.status === 'completed') {
queryClient.invalidateQueries({
queryKey: ['ai-credits', connection.nostr ? user?.pubkey ?? '' : '', providerId],
queryKey: ['ai-credits', provider.nostr ? user?.pubkey ?? '' : '', provider.id],
});
toast({
title: 'Payment completed!',
+7 -7
View File
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import { TestApp } from '@/test/TestApp';
import type { AIProvider } from '@/contexts/AISettingsContext';
// Mock QRCode
vi.mock('qrcode', () => ({
@@ -18,9 +19,11 @@ const mockWebLN = {
// We need to import the component after mocking
const { CreditsDialog } = await import('./CreditsDialog');
const mockConnection = {
const mockProvider: AIProvider = {
id: 'test-provider',
baseURL: 'https://api.test.com/v1',
apiKey: 'test-key',
nostr: false,
};
describe('Lightning Payment', () => {
@@ -39,8 +42,7 @@ describe('Lightning Payment', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -61,8 +63,7 @@ describe('Lightning Payment', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);
@@ -77,8 +78,7 @@ describe('Lightning Payment', () => {
<CreditsDialog
open={true}
onOpenChange={() => {}}
providerId="test-provider"
connection={mockConnection}
provider={mockProvider}
/>
</TestApp>
);