Combine CreditsDialog into AIProviderConfigDialog

This commit is contained in:
Alex Gleason committed 2025-12-30 21:11:48 -06:00
1 parent a0d3c4fbd0
commit ef67b9b166
14 files changed
+1647 -1566

No files matched your search

File diff suppressed because it is too large. Load diff
+75 -26
View File
@@ -1,9 +1,18 @@
import { describe, it, expect, vi } from 'vitest'; import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react'; import { render, screen, waitFor } from '@testing-library/react';
import { TestApp } from '@/test/TestApp'; import { TestApp } from '@/test/TestApp';
import { CreditsDialog } from './CreditsDialog'; import { AIProviderConfigDialog } from './AIProviderConfigDialog';
import type { AIProvider } from '@/contexts/AISettingsContext'; import type { AIProvider } from '@/contexts/AISettingsContext';
// Mock useAICredits to return credits data
vi.mock('@/hooks/useAICredits', () => ({
useAICredits: () => ({
data: { amount: 10.50 },
error: null,
isLoading: false,
}),
}));
// Mock the fetch function // Mock the fetch function
global.fetch = vi.fn(); global.fetch = vi.fn();
@@ -15,30 +24,43 @@ const mockProvider: AIProvider = {
nostr: false, nostr: false,
}; };
describe('CreditsDialog', () => { describe('AIProviderConfigDialog - Credits Tab', () => {
it('renders dialog when open', () => { beforeEach(() => {
vi.clearAllMocks();
});
it('renders dialog when open with credits tab', async () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
expect(screen.getByText('Credits')).toBeInTheDocument(); // Wait for the dialog to render with tabs
expect(screen.getByText('Amount (USD)')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByRole('tab', { name: /credits/i })).toBeInTheDocument();
});
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
expect(screen.getByText('Recent Transactions')).toBeInTheDocument(); expect(screen.getByText('Recent Transactions')).toBeInTheDocument();
}); });
it('does not render when closed', () => { it('does not render when closed', () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={false} open={false}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
/> />
</TestApp> </TestApp>
); );
@@ -46,49 +68,70 @@ describe('CreditsDialog', () => {
expect(screen.queryByText('Credits')).not.toBeInTheDocument(); expect(screen.queryByText('Credits')).not.toBeInTheDocument();
}); });
it('shows payment method options', () => { it('shows payment method options', async () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
expect(screen.getByText('Payment Method')).toBeInTheDocument(); // Wait for accordion to be available and click to expand
expect(screen.getByText('Amount (USD)')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
});
// The content is in an accordion that needs to be expanded
// Check for accordion trigger text instead
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
}); });
it('shows preset amount buttons', () => { it('shows preset amount buttons', async () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
expect(screen.getByText('$5')).toBeInTheDocument(); // Wait for the Buy Credits accordion to be visible
expect(screen.getByText('$10')).toBeInTheDocument(); // The "add" accordion defaults to being open
expect(screen.getByText('$25')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByText('$50')).toBeInTheDocument(); expect(screen.getByText('Buy Credits')).toBeInTheDocument();
expect(screen.getByText('$100')).toBeInTheDocument(); });
// The preset buttons should be visible since the accordion defaults to open
await waitFor(() => {
expect(screen.getByText('$5')).toBeInTheDocument();
expect(screen.getByText('$10')).toBeInTheDocument();
expect(screen.getByText('$25')).toBeInTheDocument();
expect(screen.getByText('$50')).toBeInTheDocument();
expect(screen.getByText('$100')).toBeInTheDocument();
});
}); });
it('calls onDialogStateChange callback when dialog state changes', async () => {
it('calls onDialogStateChange callback when dialog state changes', () => {
const mockCallback = vi.fn(); const mockCallback = vi.fn();
const { rerender } = render( const { rerender } = render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={false} open={false}
onOpenChange={mockCallback} onOpenChange={mockCallback}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
/> />
</TestApp> </TestApp>
); );
@@ -96,14 +139,20 @@ describe('CreditsDialog', () => {
// Simulate opening the dialog // Simulate opening the dialog
rerender( rerender(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={mockCallback} onOpenChange={mockCallback}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
expect(screen.getByText('Credits')).toBeInTheDocument(); // Wait for the Credits tab to appear
await waitFor(() => {
expect(screen.getByRole('tab', { name: /credits/i })).toBeInTheDocument();
});
}); });
}); });
File diff suppressed because it is too large. Load diff
+35 -11
View File
@@ -1,8 +1,17 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'; import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react'; import { render, screen, waitFor } from '@testing-library/react';
import { TestApp } from '@/test/TestApp'; import { TestApp } from '@/test/TestApp';
import type { AIProvider } from '@/contexts/AISettingsContext'; import type { AIProvider } from '@/contexts/AISettingsContext';
// Mock useAICredits to return credits data
vi.mock('@/hooks/useAICredits', () => ({
useAICredits: () => ({
data: { amount: 10.50 },
error: null,
isLoading: false,
}),
}));
// Mock QRCode // Mock QRCode
vi.mock('qrcode', () => ({ vi.mock('qrcode', () => ({
default: { default: {
@@ -17,7 +26,7 @@ const mockWebLN = {
}; };
// We need to import the component after mocking // We need to import the component after mocking
const { CreditsDialog } = await import('./CreditsDialog'); const { AIProviderConfigDialog } = await import('./AIProviderConfigDialog');
const mockProvider: AIProvider = { const mockProvider: AIProvider = {
id: 'test-provider', id: 'test-provider',
@@ -40,16 +49,21 @@ describe('Lightning Payment', () => {
it('shows QR code for Lightning payment', async () => { it('shows QR code for Lightning payment', async () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
// Should show the normal form initially // Should show the Credits tab with Buy Credits accordion
expect(screen.getByText('Amount (USD)')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
});
}); });
it('shows WebLN button when available', async () => { it('shows WebLN button when available', async () => {
@@ -61,30 +75,40 @@ describe('Lightning Payment', () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
// Should show the normal form initially // Should show the Credits tab with Buy Credits accordion
expect(screen.getByText('Amount (USD)')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
});
}); });
it('displays copy invoice button', async () => { it('displays copy invoice button', async () => {
render( render(
<TestApp> <TestApp>
<CreditsDialog <AIProviderConfigDialog
open={true} open={true}
onOpenChange={() => {}} onOpenChange={() => {}}
provider={mockProvider} provider={mockProvider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</TestApp> </TestApp>
); );
// Should show the normal form initially // Should show the Credits tab with Buy Credits accordion
expect(screen.getByText('Payment Method')).toBeInTheDocument(); await waitFor(() => {
expect(screen.getByText('Buy Credits')).toBeInTheDocument();
});
}); });
}); });
+5 -2
View File
@@ -15,7 +15,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useLoginActions } from '@/hooks/useLoginActions'; import { useLoginActions } from '@/hooks/useLoginActions';
import { useProviderModels } from '@/hooks/useProviderModels'; import { useProviderModels } from '@/hooks/useProviderModels';
import { useAICredits } from '@/hooks/useAICredits'; import { useAICredits } from '@/hooks/useAICredits';
import { CreditsDialog } from '@/components/CreditsDialog'; import { AIProviderConfigDialog } from '@/components/AIProviderConfigDialog';
import { OnboardingCreditsBadge } from '@/components/OnboardingCreditsBadge'; import { OnboardingCreditsBadge } from '@/components/OnboardingCreditsBadge';
import { ShakespeareLogo } from '@/components/ShakespeareLogo'; import { ShakespeareLogo } from '@/components/ShakespeareLogo';
import { ModelPricing } from '@/components/ModelPricing'; import { ModelPricing } from '@/components/ModelPricing';
@@ -726,7 +726,7 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
{/* Credits Dialog */} {/* Credits Dialog */}
{showCreditsDialog && selectedModel && selectedProvider && ( {showCreditsDialog && selectedModel && selectedProvider && (
<CreditsDialog <AIProviderConfigDialog
open={showCreditsDialog} open={showCreditsDialog}
onOpenChange={(open) => { onOpenChange={(open) => {
if (!open) { if (!open) {
@@ -739,6 +739,9 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
baseURL: selectedProvider.baseURL, baseURL: selectedProvider.baseURL,
nostr: selectedProvider.nostr || undefined, nostr: selectedProvider.nostr || undefined,
}} }}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
)} )}
</> </>
+3 -3
View File
@@ -23,9 +23,9 @@ vi.mock('react-router-dom', () => ({
useNavigate: () => vi.fn() useNavigate: () => vi.fn()
})); }));
// Mock CreditsDialog to avoid NostrProvider dependency // Mock AIProviderConfigDialog to avoid NostrProvider dependency
vi.mock('./CreditsDialog', () => ({ vi.mock('./AIProviderConfigDialog', () => ({
CreditsDialog: () => null AIProviderConfigDialog: () => null
})); }));
describe('Quilly', () => { describe('Quilly', () => {
+5 -2
View File
@@ -6,7 +6,7 @@ import { QuillySVG } from '@/components/ui/QuillySVG';
import { useAISettings } from '@/hooks/useAISettings'; import { useAISettings } from '@/hooks/useAISettings';
import { parseProviderModel } from '@/lib/parseProviderModel'; import { parseProviderModel } from '@/lib/parseProviderModel';
import { useAICredits } from '@/hooks/useAICredits'; import { useAICredits } from '@/hooks/useAICredits';
import { CreditsDialog } from './CreditsDialog'; import { AIProviderConfigDialog } from './AIProviderConfigDialog';
import { ProjectPreviewConsoleError } from '@/lib/consoleMessages'; import { ProjectPreviewConsoleError } from '@/lib/consoleMessages';
import { useState } from 'react'; import { useState } from 'react';
import { MalformedToolCallError } from '@/lib/errors/MalformedToolCallError'; import { MalformedToolCallError } from '@/lib/errors/MalformedToolCallError';
@@ -279,10 +279,13 @@ function QuillyCreditsButton({ provider }: QuillyCreditsButtonProps) {
<button className="text-primary underline" onClick={() => setShowCreditsDialog(true)}> <button className="text-primary underline" onClick={() => setShowCreditsDialog(true)}>
{credits.data ? `Add credits (${credits.data.amount.toFixed(2)} remaining)` : 'Add credits'} {credits.data ? `Add credits (${credits.data.amount.toFixed(2)} remaining)` : 'Add credits'}
</button> </button>
<CreditsDialog <AIProviderConfigDialog
open={showCreditsDialog} open={showCreditsDialog}
onOpenChange={setShowCreditsDialog} onOpenChange={setShowCreditsDialog}
provider={provider} provider={provider}
onUpdate={() => {}}
onRemove={() => {}}
initialTab="credits"
/> />
</> </>
); );
+1
View File
@@ -381,6 +381,7 @@
"startBuilding": "Start Building", "startBuilding": "Start Building",
"continueButton": "Continue", "continueButton": "Continue",
"credits": "Credits", "credits": "Credits",
"availableCredits": "Available Credits",
"back": "Back", "back": "Back",
"buyCredits": "Buy Credits", "buyCredits": "Buy Credits",
"amountUSD": "Amount (USD)", "amountUSD": "Amount (USD)",
+1
View File
@@ -348,6 +348,7 @@
"startBuilding": "Fara Ginawa", "startBuilding": "Fara Ginawa",
"continueButton": "Ci gaba", "continueButton": "Ci gaba",
"credits": "Kuɗi", "credits": "Kuɗi",
"availableCredits": "Kuɗin da ake da su",
"back": "Koma", "back": "Koma",
"buyCredits": "Sayi Kuɗi", "buyCredits": "Sayi Kuɗi",
"amountUSD": "Adadi (USD)", "amountUSD": "Adadi (USD)",
+1
View File
@@ -345,6 +345,7 @@
"startBuilding": "Malite Iwu", "startBuilding": "Malite Iwu",
"continueButton": "Gaa n'ihu", "continueButton": "Gaa n'ihu",
"credits": "Kredit", "credits": "Kredit",
"availableCredits": "Kredit dị",
"back": "Laghachi", "back": "Laghachi",
"buyCredits": "Zụta Kredit", "buyCredits": "Zụta Kredit",
"amountUSD": "Ego (USD)", "amountUSD": "Ego (USD)",
+1
View File
@@ -357,6 +357,7 @@
"startBuilding": "Começar a Construir", "startBuilding": "Começar a Construir",
"continueButton": "Continuar", "continueButton": "Continuar",
"credits": "Créditos", "credits": "Créditos",
"availableCredits": "Créditos Disponíveis",
"back": "Voltar", "back": "Voltar",
"buyCredits": "Comprar Créditos", "buyCredits": "Comprar Créditos",
"amountUSD": "Valor (USD)", "amountUSD": "Valor (USD)",
+1
View File
@@ -345,6 +345,7 @@
"startBuilding": "Bẹrẹ Kikọ", "startBuilding": "Bẹrẹ Kikọ",
"continueButton": "Tẹsiwaju", "continueButton": "Tẹsiwaju",
"credits": "Awọn Kirẹditi", "credits": "Awọn Kirẹditi",
"availableCredits": "Awọn Kirẹditi to wa",
"back": "Pada", "back": "Pada",
"buyCredits": "Ra Awọn Kirẹditi", "buyCredits": "Ra Awọn Kirẹditi",
"amountUSD": "Iye owo (USD)", "amountUSD": "Iye owo (USD)",
+1
View File
@@ -357,6 +357,7 @@
"startBuilding": "开始构建", "startBuilding": "开始构建",
"continueButton": "继续", "continueButton": "继续",
"credits": "积分", "credits": "积分",
"availableCredits": "可用积分",
"back": "返回", "back": "返回",
"buyCredits": "购买积分", "buyCredits": "购买积分",
"amountUSD": "金额(美元)", "amountUSD": "金额(美元)",
+8 -19
View File
@@ -4,7 +4,6 @@ import { Bot, ChevronDown, RotateCcw, FileText, Edit, X } from 'lucide-react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea'; import { Textarea } from '@/components/ui/textarea';
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion';
import { CreditsDialog } from '@/components/CreditsDialog';
import { Separator } from '@/components/ui/separator'; import { Separator } from '@/components/ui/separator';
import { Skeleton } from '@/components/ui/skeleton'; import { Skeleton } from '@/components/ui/skeleton';
import { ProviderTile } from '@/components/ProviderTile'; import { ProviderTile } from '@/components/ProviderTile';
@@ -30,13 +29,13 @@ export function AISettings() {
const { settings, updateSettings, setProvider, removeProvider, isLoading } = useAISettings(); const { settings, updateSettings, setProvider, removeProvider, isLoading } = useAISettings();
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { config, defaultConfig, updateConfig } = useAppContext(); const { config, defaultConfig, updateConfig } = useAppContext();
const [activeCreditsDialog, setActiveCreditsDialog] = useState<string | null>(null);
const [showAdvanced, setShowAdvanced] = useState(false); const [showAdvanced, setShowAdvanced] = useState(false);
const [systemPromptInput, setSystemPromptInput] = useState(config.systemPrompt || defaultSystemPrompt); const [systemPromptInput, setSystemPromptInput] = useState(config.systemPrompt || defaultSystemPrompt);
// Dialog state // Dialog state
const [selectedProviderId, setSelectedProviderId] = useState<string | null>(null); const [selectedProviderId, setSelectedProviderId] = useState<string | null>(null);
const [configDialogOpen, setConfigDialogOpen] = useState(false); const [configDialogOpen, setConfigDialogOpen] = useState(false);
const [configDialogTab, setConfigDialogTab] = useState<'credits' | 'edit'>('edit');
const [selectedPreset, setSelectedPreset] = useState<PresetProvider | null>(null); const [selectedPreset, setSelectedPreset] = useState<PresetProvider | null>(null);
const [addDialogOpen, setAddDialogOpen] = useState(false); const [addDialogOpen, setAddDialogOpen] = useState(false);
const [customProviderDialogOpen, setCustomProviderDialogOpen] = useState(false); const [customProviderDialogOpen, setCustomProviderDialogOpen] = useState(false);
@@ -162,7 +161,11 @@ export function AISettings() {
badge={ badge={
<CreditsBadge <CreditsBadge
provider={provider} provider={provider}
onOpenDialog={() => setActiveCreditsDialog(provider.id)} onOpenDialog={() => {
setSelectedProviderId(provider.id);
setConfigDialogTab('credits');
setConfigDialogOpen(true);
}}
/> />
} }
/> />
@@ -181,7 +184,7 @@ export function AISettings() {
provider={provider} provider={provider}
onUpdate={handleSetProvider} onUpdate={handleSetProvider}
onRemove={() => handleRemoveProvider(provider.id)} onRemove={() => handleRemoveProvider(provider.id)}
onOpenCreditsDialog={() => setActiveCreditsDialog(provider.id)} initialTab={configDialogTab}
/> />
); );
})()} })()}
@@ -342,21 +345,7 @@ export function AISettings() {
</> </>
)} )}
{/* Render credits dialog */}
{activeCreditsDialog && (() => {
const provider = settings.providers.find(p => p.id === activeCreditsDialog);
return provider && (
<CreditsDialog
open={true}
onOpenChange={(open) => {
if (!open) {
setActiveCreditsDialog(null);
}
}}
provider={provider}
/>
);
})()}
</SettingsPageLayout> </SettingsPageLayout>
); );
} }