From b87934da1a91897fc3980d6736f4e1c28490247f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 12 Oct 2025 22:15:57 -0500 Subject: [PATCH] Add special URL format to add a provider --- src/App.test.tsx | 7 +- src/AppRouter.tsx | 2 + src/components/AddProviderDialog.test.tsx | 133 +++++++++++++++ src/components/AddProviderDialog.tsx | 179 +++++++++++++++++++++ src/components/URLFragmentHandler.test.tsx | 154 ++++++++++++++++++ src/components/URLFragmentHandler.tsx | 80 +++++++++ 6 files changed, 553 insertions(+), 2 deletions(-) create mode 100644 src/components/AddProviderDialog.test.tsx create mode 100644 src/components/AddProviderDialog.tsx create mode 100644 src/components/URLFragmentHandler.test.tsx create mode 100644 src/components/URLFragmentHandler.tsx diff --git a/src/App.test.tsx b/src/App.test.tsx index bedec00c..0e6e429d 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -1,8 +1,11 @@ import { render } from '@testing-library/react'; -import { test } from 'vitest'; +import { test, expect } from 'vitest'; import App from './App'; test('App', () => { - render(); + const { container } = render(); + + // Basic smoke test to ensure App renders without crashing + expect(container).toBeInTheDocument(); }) \ No newline at end of file diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 5ad7e102..ad95ec65 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -1,6 +1,7 @@ import { BrowserRouter, Route, Routes } from "react-router-dom"; import { ScrollToTop } from "./components/ScrollToTop"; import { SessionMonitor } from "./components/SessionMonitor"; +import { URLFragmentHandler } from "./components/URLFragmentHandler"; import Index from "./pages/Index"; import Clone from "./pages/Clone"; @@ -24,6 +25,7 @@ export function AppRouter() { + } /> } /> diff --git a/src/components/AddProviderDialog.test.tsx b/src/components/AddProviderDialog.test.tsx new file mode 100644 index 00000000..65412431 --- /dev/null +++ b/src/components/AddProviderDialog.test.tsx @@ -0,0 +1,133 @@ +import { describe, it, expect, vi } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { TestApp } from '@/test/TestApp'; +import { AddProviderDialog } from './AddProviderDialog'; +import type { AIProvider } from '@/contexts/AISettingsContext'; + +describe('AddProviderDialog', () => { + const mockProvider: AIProvider = { + id: 'test-provider', + baseURL: 'https://api.test.com/v1', + apiKey: 'sk-test-key-123', + }; + + it('renders new provider dialog', () => { + const onOpenChange = vi.fn(); + + render( + + + + ); + + // Provider name is used as the heading (fallback to id since it's not in presets) + expect(screen.getByRole('heading', { name: 'test-provider' })).toBeInTheDocument(); + expect(screen.getByText(/Add test-provider to your configuration/i)).toBeInTheDocument(); + }); + + it('shows add provider button for new provider', () => { + const onOpenChange = vi.fn(); + + render( + + + + ); + + // No cancel button, only add provider button + expect(screen.queryByRole('button', { name: /cancel/i })).not.toBeInTheDocument(); + expect(screen.getByRole('button', { name: /add provider/i })).toBeInTheDocument(); + }); + + it('calls onOpenChange when add provider is clicked', async () => { + const user = userEvent.setup(); + const onOpenChange = vi.fn(); + + render( + + + + ); + + const addButton = screen.getByRole('button', { name: /add provider/i }); + await user.click(addButton); + + expect(onOpenChange).toHaveBeenCalledWith(false); + }); + + it('does not display provider details for new provider', () => { + const onOpenChange = vi.fn(); + + render( + + + + ); + + // Provider details (API key, base URL, etc.) are not displayed for new providers + expect(screen.queryByText('••••••••')).not.toBeInTheDocument(); + expect(screen.queryByText('sk-test-key-123')).not.toBeInTheDocument(); + expect(screen.queryByText('https://api.test.com/v1')).not.toBeInTheDocument(); + }); + + it('displays provider name from presets when available', () => { + const onOpenChange = vi.fn(); + // Use a provider that exists in presets + const openrouterProvider: AIProvider = { + id: 'openrouter', + baseURL: 'https://openrouter.ai/api/v1', + apiKey: 'sk-test-123', + }; + + render( + + + + ); + + // Should show the preset name "OpenRouter" instead of the id + expect(screen.getByText(/Add OpenRouter to your configuration/i)).toBeInTheDocument(); + }); + + it('uses provider id as fallback name when not in presets', () => { + const onOpenChange = vi.fn(); + const customProvider: AIProvider = { + id: 'my-custom-provider', + baseURL: 'https://api.custom.com/v1', + }; + + render( + + + + ); + + // Should use the id as the name since it's not in presets + expect(screen.getByText(/Add my-custom-provider to your configuration/i)).toBeInTheDocument(); + }); +}); diff --git a/src/components/AddProviderDialog.tsx b/src/components/AddProviderDialog.tsx new file mode 100644 index 00000000..3b9dfcb3 --- /dev/null +++ b/src/components/AddProviderDialog.tsx @@ -0,0 +1,179 @@ +import { useEffect, useRef } from 'react'; +import { Check, Sparkles, ArrowRight, AlertCircle } from 'lucide-react'; +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; +import { Button } from '@/components/ui/button'; +import { useAISettings } from '@/hooks/useAISettings'; +import { AI_PROVIDER_PRESETS } from '@/lib/aiProviderPresets'; +import type { AIProvider } from '@/contexts/AISettingsContext'; + +interface AddProviderDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + provider: AIProvider; +} + +export function AddProviderDialog({ open, onOpenChange, provider }: AddProviderDialogProps) { + const scrollableContentRef = useRef(null); + + const { settings, setProvider } = useAISettings(); + + // Check if provider already exists and if it's identical + const existingProvider = settings.providers.find(p => p.id === provider.id); + const isIdentical = existingProvider && + existingProvider.baseURL === provider.baseURL && + existingProvider.apiKey === provider.apiKey && + existingProvider.nostr === provider.nostr && + existingProvider.proxy === provider.proxy; + + // Get provider name from presets or fallback to id + const getProviderName = () => { + const preset = AI_PROVIDER_PRESETS.find( + p => p.id === provider.id && p.baseURL === provider.baseURL + ); + return preset?.name || provider.id; + }; + + const providerName = getProviderName(); + + const handleAddProvider = () => { + setProvider(provider); + onOpenChange(false); + }; + + // Scroll to top when dialog opens + useEffect(() => { + if (open && scrollableContentRef.current) { + scrollableContentRef.current.scrollTop = 0; + } + }, [open]); + + return ( + + + + + Add Provider + + + +
+
+ {isIdentical ? ( + // Provider already exists with identical configuration +
+
+ +
+
+

Already Configured

+

+ You already have {providerName} configured with the same settings. +

+
+ +
+ ) : existingProvider ? ( + // Provider exists but with different configuration +
+
+ +
+
+

Provider Already Exists

+

+ A provider with this ID already exists but with different settings. Would you like to overwrite it? +

+
+ +
+
+

Current Settings

+
+
+ Base URL: +
{existingProvider.baseURL}
+
+ {existingProvider.apiKey && ( +
+ API Key: +
••••••••
+
+ )} + {existingProvider.nostr && ( +
+ Nostr Auth: Enabled +
+ )} + {existingProvider.proxy && ( +
+ Proxy: Enabled +
+ )} +
+
+ +
+

New Settings

+
+
+ Base URL: +
{provider.baseURL}
+
+ {provider.apiKey && ( +
+ API Key: +
••••••••
+
+ )} + {provider.nostr && ( +
+ Nostr Auth: Enabled +
+ )} + {provider.proxy && ( +
+ Proxy: Enabled +
+ )} +
+
+
+ +
+ + +
+
+ ) : ( + // New provider +
+
+ +
+
+

{providerName}

+

+ Add {providerName} to your configuration? +

+
+ +
+ +
+
+ )} +
+
+
+
+ ); +} diff --git a/src/components/URLFragmentHandler.test.tsx b/src/components/URLFragmentHandler.test.tsx new file mode 100644 index 00000000..aa2a27d4 --- /dev/null +++ b/src/components/URLFragmentHandler.test.tsx @@ -0,0 +1,154 @@ +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; +import { render, screen, waitFor } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { URLFragmentHandler } from './URLFragmentHandler'; + +// Mock the AddProviderDialog component +vi.mock('./AddProviderDialog', () => ({ + AddProviderDialog: ({ open, provider }: { open: boolean; provider: { id: string; baseURL: string; apiKey?: string; nostr?: boolean; proxy?: boolean } }) => { + if (!open) return null; + return ( +
+
Provider ID: {provider.id}
+
Base URL: {provider.baseURL}
+ {provider.apiKey &&
API Key: {provider.apiKey}
} + {provider.nostr &&
Nostr: true
} + {provider.proxy &&
Proxy: true
} +
+ ); + }, +})); + +describe('URLFragmentHandler', () => { + const originalHash = window.location.hash; + + beforeEach(() => { + // Clear hash before each test + window.location.hash = ''; + }); + + afterEach(() => { + // Restore original hash + window.location.hash = originalHash; + }); + + it('does not render dialog when no hash is present', () => { + window.location.hash = ''; + + render( + + + + ); + + expect(screen.queryByTestId('add-provider-dialog')).not.toBeInTheDocument(); + }); + + it('does not render dialog when hash does not contain provider params', () => { + window.location.hash = '#some-random-hash'; + + render( + + + + ); + + expect(screen.queryByTestId('add-provider-dialog')).not.toBeInTheDocument(); + }); + + it('parses provider from URL fragment and displays dialog', async () => { + window.location.hash = '#id=openrouter&baseURL=https://openrouter.ai/api/v1'; + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByTestId('add-provider-dialog')).toBeInTheDocument(); + }); + + expect(screen.getByText('Provider ID: openrouter')).toBeInTheDocument(); + expect(screen.getByText('Base URL: https://openrouter.ai/api/v1')).toBeInTheDocument(); + }); + + it('parses API key from URL fragment', async () => { + window.location.hash = '#id=openrouter&baseURL=https://openrouter.ai/api/v1&apiKey=sk-test-123'; + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByTestId('add-provider-dialog')).toBeInTheDocument(); + }); + + expect(screen.getByText('API Key: sk-test-123')).toBeInTheDocument(); + }); + + it('parses nostr flag from URL fragment', async () => { + window.location.hash = '#id=shakespeare&baseURL=https://ai.shakespeare.diy/v1&nostr=true'; + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByTestId('add-provider-dialog')).toBeInTheDocument(); + }); + + expect(screen.getByText('Nostr: true')).toBeInTheDocument(); + }); + + it('parses proxy flag from URL fragment', async () => { + window.location.hash = '#id=custom&baseURL=https://api.custom.com/v1&proxy=true'; + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByTestId('add-provider-dialog')).toBeInTheDocument(); + }); + + expect(screen.getByText('Proxy: true')).toBeInTheDocument(); + }); + + it('parses all parameters from complete URL', async () => { + window.location.hash = '#id=openrouter&baseURL=https://openrouter.ai/api&apiKey=sk-b7719bfc06f04bb29086f380969c76eb&nostr=false&proxy=true'; + + render( + + + + ); + + await waitFor(() => { + expect(screen.getByTestId('add-provider-dialog')).toBeInTheDocument(); + }); + + expect(screen.getByText('Provider ID: openrouter')).toBeInTheDocument(); + expect(screen.getByText('Base URL: https://openrouter.ai/api')).toBeInTheDocument(); + expect(screen.getByText('API Key: sk-b7719bfc06f04bb29086f380969c76eb')).toBeInTheDocument(); + expect(screen.getByText('Proxy: true')).toBeInTheDocument(); + }); + + it('requires both id and baseURL to show dialog', () => { + window.location.hash = '#id=openrouter'; + + render( + + + + ); + + expect(screen.queryByTestId('add-provider-dialog')).not.toBeInTheDocument(); + }); +}); diff --git a/src/components/URLFragmentHandler.tsx b/src/components/URLFragmentHandler.tsx new file mode 100644 index 00000000..b97a69fe --- /dev/null +++ b/src/components/URLFragmentHandler.tsx @@ -0,0 +1,80 @@ +import { useEffect, useState } from 'react'; +import { useLocation, useNavigate } from 'react-router-dom'; +import { AddProviderDialog } from '@/components/AddProviderDialog'; +import type { AIProvider } from '@/contexts/AISettingsContext'; + +/** + * Component that handles URL fragments for adding AI providers + * Example URL: https://shakespeare.diy/#id=openrouter&baseURL=https://openrouter.ai/api&apiKey=sk-123 + */ +export function URLFragmentHandler() { + const location = useLocation(); + const navigate = useNavigate(); + const [providerFromURL, setProviderFromURL] = useState(null); + const [dialogOpen, setDialogOpen] = useState(false); + + useEffect(() => { + // Only process if there's a hash + if (!location.hash) return; + + // Remove the leading '#' and parse as URLSearchParams + const fragment = location.hash.slice(1); + const params = new URLSearchParams(fragment); + + // Check if we have the required parameters for a provider + const id = params.get('id'); + const baseURL = params.get('baseURL'); + + if (!id || !baseURL) { + // Not a provider URL, ignore + return; + } + + // Build the provider object + const provider: AIProvider = { + id, + baseURL, + }; + + // Add optional parameters + const apiKey = params.get('apiKey'); + if (apiKey) { + provider.apiKey = apiKey; + } + + const nostr = params.get('nostr'); + if (nostr === 'true') { + provider.nostr = true; + } + + const proxy = params.get('proxy'); + if (proxy === 'true') { + provider.proxy = true; + } + + // Set the provider and open dialog + setProviderFromURL(provider); + setDialogOpen(true); + + // Remove the fragment from the URL immediately + // Preserve the current path and search params + navigate(location.pathname + location.search, { replace: true }); + }, [location, navigate]); + + const handleDialogClose = () => { + setDialogOpen(false); + setProviderFromURL(null); + }; + + if (!providerFromURL) { + return null; + } + + return ( + + ); +}