Refactor Git to accept a nostr object

This commit is contained in:
Alex Gleason committed 2025-09-21 14:31:13 -05:00
1 parent f797ec55c5
commit 69c1000063
16 files changed
+184 -379

No files matched your search

+43 -9
View File
@@ -5,22 +5,40 @@ import { useFS } from './useFS';
import { useGitSettings } from './useGitSettings';
import { Git } from '@/lib/git';
import type { JSRuntimeFS } from '@/lib/JSRuntime';
import type { NPool } from '@nostrify/nostrify';
// Mock the dependencies
vi.mock('./useFS');
vi.mock('./useGitSettings');
vi.mock('@nostrify/react', () => ({
useNostr: vi.fn(),
}));
vi.mock('@/lib/git');
const mockUseFS = vi.mocked(useFS);
const mockUseGitSettings = vi.mocked(useGitSettings);
const MockGit = vi.mocked(Git);
// Import the mocked useNostr after mocking
import { useNostr } from '@nostrify/react';
const mockUseNostr = vi.mocked(useNostr);
describe('useGit', () => {
const mockFS = {} as JSRuntimeFS;
const mockNostr = {
req: vi.fn(),
query: vi.fn(),
event: vi.fn(),
group: vi.fn(),
relay: vi.fn(),
relays: new Map(),
close: vi.fn(),
} as unknown as NPool;
beforeEach(() => {
vi.clearAllMocks();
mockUseFS.mockReturnValue({ fs: mockFS });
mockUseNostr.mockReturnValue({ nostr: mockNostr });
});
it('creates Git instance with default CORS proxy', () => {
@@ -36,9 +54,14 @@ describe('useGit', () => {
isConfigured: false,
});
renderHook(() => useGit());
const { result } = renderHook(() => useGit());
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org');
expect(MockGit).toHaveBeenCalledWith({
fs: mockFS,
nostr: mockNostr,
corsProxy: 'https://cors.isomorphic-git.org',
});
expect(result.current).toEqual({ git: expect.any(Object) });
});
it('creates Git instance with custom CORS proxy', () => {
@@ -54,14 +77,17 @@ describe('useGit', () => {
isConfigured: false,
});
renderHook(() => useGit());
const { result } = renderHook(() => useGit());
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://custom-cors.example.com');
expect(MockGit).toHaveBeenCalledWith({
fs: mockFS,
nostr: mockNostr,
corsProxy: 'https://custom-cors.example.com',
});
expect(result.current).toEqual({ git: expect.any(Object) });
});
it('recreates Git instance when CORS proxy changes', () => {
const { rerender } = renderHook(() => useGit());
// First render with default proxy
mockUseGitSettings.mockReturnValue({
settings: {
@@ -75,8 +101,12 @@ describe('useGit', () => {
isConfigured: false,
});
rerender();
expect(MockGit).toHaveBeenCalledWith(mockFS, 'https://cors.isomorphic-git.org');
const { rerender } = renderHook(() => useGit());
expect(MockGit).toHaveBeenCalledWith({
fs: mockFS,
nostr: mockNostr,
corsProxy: 'https://cors.isomorphic-git.org',
});
// Second render with custom proxy
mockUseGitSettings.mockReturnValue({
@@ -92,6 +122,10 @@ describe('useGit', () => {
});
rerender();
expect(MockGit).toHaveBeenLastCalledWith(mockFS, 'https://custom-cors.example.com');
expect(MockGit).toHaveBeenLastCalledWith({
fs: mockFS,
nostr: mockNostr,
corsProxy: 'https://custom-cors.example.com',
});
});
});
+7 -4
View File
@@ -1,19 +1,22 @@
import { useMemo } from 'react';
import { Git } from '@/lib/git';
import { useFS } from '@/hooks/useFS';
import { useNostr } from '@nostrify/react';
import { useGitSettings } from '@/hooks/useGitSettings';
/**
* Hook that provides a Git instance configured with the virtual filesystem
* and configurable CORS proxy for GitHub/GitLab repositories.
*/
export function useGit() {
export function useGit(): { git: Git } {
const { fs } = useFS();
const { nostr } = useNostr();
const { settings } = useGitSettings();
const { corsProxy } = settings;
const git = useMemo(() => {
return new Git(fs, settings.corsProxy);
}, [fs, settings.corsProxy]);
return new Git({ fs, nostr, corsProxy });
}, [fs, nostr, corsProxy]);
return git;
return { git };
}
+1 -1
View File
@@ -40,7 +40,7 @@ interface GitStatus {
}
export function useGitStatus(projectId: string | null) {
const git = useGit();
const { git } = useGit();
return useQuery({
queryKey: ['git-status', projectId],
+5 -1
View File
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { ProjectsManager } from '@/lib/ProjectsManager';
import { useFS } from '@/hooks/useFS';
import { useGit } from './useGit';
/**
* Hook that provides a ProjectsManager instance.
@@ -8,8 +9,11 @@ import { useFS } from '@/hooks/useFS';
*/
export function useProjectsManager() {
const { fs } = useFS();
const { git } = useGit();
const projectsManager = useMemo(() => new ProjectsManager(fs), [fs]);
const projectsManager = useMemo(() => {
return new ProjectsManager({ fs, git });
}, [fs, git]);
return projectsManager;
}