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