diff --git a/src/components/GitSettingsProvider.test.tsx b/src/components/GitSettingsProvider.test.tsx index 8aab6379..f15c1963 100644 --- a/src/components/GitSettingsProvider.test.tsx +++ b/src/components/GitSettingsProvider.test.tsx @@ -7,7 +7,7 @@ import type { GitCredential } from '@/contexts/GitSettingsContext'; // Test component that uses the hook function TestComponent() { - const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings(); + const { settings, updateSettings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings(); const handleAdd = () => { const credential: GitCredential = { @@ -25,13 +25,21 @@ function TestComponent() { removeCredential('https://github.com'); }; + const handleUpdateCorsProxy = () => { + updateSettings({ corsProxy: 'https://custom-cors.example.com' }); + }; + + + return (
{isConfigured ? 'configured' : 'not-configured'}
{Object.keys(settings.credentials).length}
+
{settings.corsProxy}
+ {settings.credentials['https://github.com'] && (
{settings.credentials['https://github.com'].username}: @@ -126,12 +134,13 @@ describe('GitSettingsProvider', () => { // Check that settings were saved to localStorage const stored = localStorage.getItem('git-settings'); expect(stored).toBeTruthy(); - + const parsed = JSON.parse(stored!); expect(parsed.credentials['https://github.com']).toEqual({ username: 'git', password: 'test-token', }); + expect(parsed.corsProxy).toBe('https://cors.isomorphic-git.org'); }); it('loads settings from localStorage', () => { @@ -143,6 +152,7 @@ describe('GitSettingsProvider', () => { password: 'gitlab-token', }, }, + corsProxy: 'https://custom-cors.example.com', }; localStorage.setItem('git-settings', JSON.stringify(settings)); @@ -154,5 +164,32 @@ describe('GitSettingsProvider', () => { expect(screen.getByTestId('configured')).toHaveTextContent('configured'); expect(screen.getByTestId('count')).toHaveTextContent('1'); + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://custom-cors.example.com'); }); + + it('provides default CORS proxy', () => { + render( + + + + ); + + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org'); + }); + + it('can update CORS proxy', () => { + render( + + + + ); + + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://cors.isomorphic-git.org'); + + fireEvent.click(screen.getByText('Update CORS Proxy')); + + expect(screen.getByTestId('cors-proxy')).toHaveTextContent('https://custom-cors.example.com'); + }); + + }); \ No newline at end of file diff --git a/src/components/GitSettingsProvider.tsx b/src/components/GitSettingsProvider.tsx index 61d5885b..61e32d07 100644 --- a/src/components/GitSettingsProvider.tsx +++ b/src/components/GitSettingsProvider.tsx @@ -7,6 +7,7 @@ interface GitSettingsProviderProps { const DEFAULT_SETTINGS: GitSettings = { credentials: {}, + corsProxy: 'https://cors.isomorphic-git.org', }; const STORAGE_KEY = 'git-settings'; @@ -21,6 +22,7 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { const settings = parsed as Partial; return { credentials: settings.credentials || {}, + corsProxy: settings.corsProxy || DEFAULT_SETTINGS.corsProxy, }; } } catch { diff --git a/src/contexts/GitSettingsContext.ts b/src/contexts/GitSettingsContext.ts index 76b49ed8..af0ae1f7 100644 --- a/src/contexts/GitSettingsContext.ts +++ b/src/contexts/GitSettingsContext.ts @@ -7,6 +7,7 @@ export interface GitCredential { export interface GitSettings { credentials: Record; // keyed by origin + corsProxy: string; } export interface GitSettingsContextType { diff --git a/src/hooks/useGit.test.ts b/src/hooks/useGit.test.ts new file mode 100644 index 00000000..d24fffe2 --- /dev/null +++ b/src/hooks/useGit.test.ts @@ -0,0 +1,97 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { renderHook } from '@testing-library/react'; +import { useGit } from './useGit'; +import { useFS } from './useFS'; +import { useGitSettings } from './useGitSettings'; +import { Git } from '@/lib/git'; +import type { JSRuntimeFS } from '@/lib/JSRuntime'; + +// Mock the dependencies +vi.mock('./useFS'); +vi.mock('./useGitSettings'); +vi.mock('@/lib/git'); + +const mockUseFS = vi.mocked(useFS); +const mockUseGitSettings = vi.mocked(useGitSettings); +const MockGit = vi.mocked(Git); + +describe('useGit', () => { + const mockFS = {} as JSRuntimeFS; + + beforeEach(() => { + vi.clearAllMocks(); + mockUseFS.mockReturnValue({ fs: mockFS }); + }); + + it('creates Git instance with default CORS proxy', () => { + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: {}, + corsProxy: 'https://cors.isomorphic-git.org', + }, + updateSettings: vi.fn(), + addCredential: vi.fn(), + removeCredential: vi.fn(), + updateCredential: vi.fn(), + isConfigured: false, + }); + + renderHook(() => useGit()); + + expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org'); + }); + + it('creates Git instance with custom CORS proxy', () => { + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: {}, + corsProxy: 'https://custom-cors.example.com', + }, + updateSettings: vi.fn(), + addCredential: vi.fn(), + removeCredential: vi.fn(), + updateCredential: vi.fn(), + isConfigured: false, + }); + + renderHook(() => useGit()); + + expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://custom-cors.example.com'); + }); + + it('recreates Git instance when CORS proxy changes', () => { + const { rerender } = renderHook(() => useGit()); + + // First render with default proxy + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: {}, + corsProxy: 'https://cors.isomorphic-git.org', + }, + updateSettings: vi.fn(), + addCredential: vi.fn(), + removeCredential: vi.fn(), + updateCredential: vi.fn(), + isConfigured: false, + }); + + rerender(); + expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org'); + + // Second render with custom proxy + mockUseGitSettings.mockReturnValue({ + settings: { + credentials: {}, + corsProxy: 'https://custom-cors.example.com', + }, + updateSettings: vi.fn(), + addCredential: vi.fn(), + removeCredential: vi.fn(), + updateCredential: vi.fn(), + isConfigured: false, + }); + + rerender(); + expect(MockGit).toHaveBeenLastCalledWith(mockFS, 'https://custom-cors.example.com'); + }); +}); \ No newline at end of file diff --git a/src/hooks/useGit.ts b/src/hooks/useGit.ts index 405993ea..65a542d9 100644 --- a/src/hooks/useGit.ts +++ b/src/hooks/useGit.ts @@ -1,17 +1,19 @@ import { useMemo } from 'react'; import { Git } from '@/lib/git'; import { useFS } from '@/hooks/useFS'; +import { useGitSettings } from '@/hooks/useGitSettings'; /** * Hook that provides a Git instance configured with the virtual filesystem - * and appropriate CORS proxy for GitHub/GitLab repositories. + * and configurable CORS proxy for GitHub/GitLab repositories. */ export function useGit() { const { fs } = useFS(); + const { settings } = useGitSettings(); const git = useMemo(() => { - return new Git(fs, 'https://cors.isomorphic-git.org'); - }, [fs]); + return new Git(fs, settings.corsProxy); + }, [fs, settings.corsProxy]); return git; } \ No newline at end of file diff --git a/src/lib/commands/git/clone.ts b/src/lib/commands/git/clone.ts index 2c440e39..3996b82d 100644 --- a/src/lib/commands/git/clone.ts +++ b/src/lib/commands/git/clone.ts @@ -1,4 +1,4 @@ -import http from 'isomorphic-git/http/web'; +// http is handled by the Git class import type { JSRuntimeFS } from "../../JSRuntime"; import type { ShellCommandResult } from "../ShellCommand"; import { createSuccessResult, createErrorResult } from "../ShellCommand"; @@ -47,10 +47,9 @@ export class GitCloneCommand implements GitSubcommand { // Directory doesn't exist, which is what we want } - // Parse URL to determine if we need CORS proxy - let url: URL; + // Validate URL format try { - url = new URL(repository); + new URL(repository); } catch { return createErrorResult(`fatal: repository '${repository}' does not appear to be a git repository`); } diff --git a/src/lib/commands/git/commit.ts b/src/lib/commands/git/commit.ts index c8f17cf7..073aed05 100644 --- a/src/lib/commands/git/commit.ts +++ b/src/lib/commands/git/commit.ts @@ -103,7 +103,7 @@ export class GitCommitCommand implements GitSubcommand { } // Create the commit - let commitOptions: { + const commitOptions: { dir: string; message: string; author: { name: string; email: string }; diff --git a/src/lib/git.test.ts b/src/lib/git.test.ts new file mode 100644 index 00000000..bdf44f80 --- /dev/null +++ b/src/lib/git.test.ts @@ -0,0 +1,164 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { Git } from './git'; +import git from 'isomorphic-git'; +import type { JSRuntimeFS } from './JSRuntime'; + +// Mock isomorphic-git +vi.mock('isomorphic-git'); +vi.mock('isomorphic-git/http/web'); + +const mockGit = vi.mocked(git); + +describe('Git', () => { + const mockFS = {} as JSRuntimeFS; + let gitInstance: Git; + + beforeEach(() => { + vi.clearAllMocks(); + gitInstance = new Git(mockFS, 'https://cors.example.com'); + }); + + describe('constructor', () => { + it('creates instance with default regex when none provided', () => { + const git = new Git(mockFS, 'https://cors.example.com'); + expect(git).toBeInstanceOf(Git); + }); + + + }); + + describe('clone', () => { + beforeEach(() => { + mockGit.clone = vi.fn().mockResolvedValue(undefined); + }); + + it('uses CORS proxy for all URLs', async () => { + await gitInstance.clone({ + dir: '/test', + url: 'https://github.com/user/repo.git', + }); + + expect(mockGit.clone).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'https://github.com/user/repo.git', + }); + }); + + it('uses CORS proxy for GitLab URLs', async () => { + await gitInstance.clone({ + dir: '/test', + url: 'https://gitlab.com/user/repo.git', + }); + + expect(mockGit.clone).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'https://gitlab.com/user/repo.git', + }); + }); + + it('uses CORS proxy for custom URLs', async () => { + await gitInstance.clone({ + dir: '/test', + url: 'https://custom.example.com/user/repo.git', + }); + + expect(mockGit.clone).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'https://custom.example.com/user/repo.git', + }); + }); + + it('handles HTTP URLs correctly', async () => { + await gitInstance.clone({ + dir: '/test', + url: 'http://github.com/user/repo.git', + }); + + expect(mockGit.clone).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'http://github.com/user/repo.git', + }); + }); + }); + + describe('getRemoteInfo', () => { + beforeEach(() => { + mockGit.getRemoteInfo = vi.fn().mockResolvedValue({}); + }); + + it('uses CORS proxy for matching URLs', async () => { + await gitInstance.getRemoteInfo({ + url: 'https://github.com/user/repo.git', + }); + + expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({ + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + url: 'https://github.com/user/repo.git', + }); + }); + + it('uses CORS proxy for all URLs', async () => { + await gitInstance.getRemoteInfo({ + url: 'https://custom.example.com/user/repo.git', + }); + + expect(mockGit.getRemoteInfo).toHaveBeenCalledWith({ + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + url: 'https://custom.example.com/user/repo.git', + }); + }); + }); + + describe('fetch with remote resolution', () => { + beforeEach(() => { + mockGit.fetch = vi.fn().mockResolvedValue(undefined); + mockGit.listRemotes = vi.fn().mockResolvedValue([ + { remote: 'origin', url: 'https://github.com/user/repo.git' } + ]); + }); + + it('uses CORS proxy for all fetch operations', async () => { + await gitInstance.fetch({ + dir: '/test', + remote: 'origin', + }); + + expect(mockGit.fetch).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + remote: 'origin', + }); + }); + + it('handles fetch with direct URL parameter', async () => { + await gitInstance.fetch({ + dir: '/test', + url: 'https://gitlab.com/user/repo.git', + }); + + expect(mockGit.fetch).toHaveBeenCalledWith({ + fs: mockFS, + http: expect.any(Object), + corsProxy: 'https://cors.example.com', + dir: '/test', + url: 'https://gitlab.com/user/repo.git', + }); + }); + }); +}); \ No newline at end of file diff --git a/src/lib/git.ts b/src/lib/git.ts index 65ade641..4975e8a9 100644 --- a/src/lib/git.ts +++ b/src/lib/git.ts @@ -4,7 +4,7 @@ import type { JSRuntimeFS } from './JSRuntime'; /** * Git class that wraps isomorphic-git and provides a cleaner interface. - * Instantiate with an fs implementation and optional corsProxy string. + * Instantiate with an fs implementation, optional corsProxy string, and optional corsProxyRegex. * All methods have the same names as isomorphic-git but don't require * passing fs, http, and corsProxy each time. */ @@ -17,6 +17,8 @@ export class Git { this.corsProxy = corsProxy; } + + // Repository initialization and configuration async init(options: Omit[0], 'fs'>) { return git.init({ diff --git a/src/pages/GitSettings.tsx b/src/pages/GitSettings.tsx index f894a20a..4613375e 100644 --- a/src/pages/GitSettings.tsx +++ b/src/pages/GitSettings.tsx @@ -37,7 +37,7 @@ const PRESET_PROVIDERS: PresetProvider[] = [ ]; export function GitSettings() { - const { settings, addCredential, removeCredential, updateCredential } = useGitSettings(); + const { settings, updateSettings, addCredential, removeCredential, updateCredential } = useGitSettings(); const isMobile = useIsMobile(); const navigate = useNavigate(); const [customOrigin, setCustomOrigin] = useState(''); @@ -257,7 +257,7 @@ export function GitSettings() {
)} - {availablePresets.length > 0 && } + {/* Custom Provider */}
@@ -319,6 +319,30 @@ export function GitSettings() { + + {/* CORS Proxy Configuration */} + + + +

CORS Proxy

+
+ +
+
+ updateSettings({ corsProxy: e.target.value })} + /> +

+ CORS proxy server used for all Git operations with remote repositories. Required for browser-based Git operations. +

+
+
+
+
+