feat: phase 3 for git integration

This commit is contained in:
2026-02-16 11:46:47 +01:00
parent 4c437cbea2
commit 9d71aa63fb
18 changed files with 499 additions and 7 deletions

View File

@@ -4,6 +4,7 @@ const mockVersion = vi.fn();
const mockCheckIsRepo = vi.fn();
const mockRevparse = vi.fn();
const mockStatus = vi.fn();
const mockDiff = vi.fn();
const mockInit = vi.fn();
const mockRaw = vi.fn();
const mockAdd = vi.fn();
@@ -30,6 +31,7 @@ vi.mock('simple-git', () => ({
checkIsRepo: mockCheckIsRepo,
revparse: mockRevparse,
status: mockStatus,
diff: mockDiff,
init: mockInit,
raw: mockRaw,
add: mockAdd,
@@ -139,6 +141,20 @@ describe('GitEngine', () => {
});
});
describe('getDiff', () => {
it('should return patch output for a repository file', async () => {
mockDiff.mockResolvedValue('diff --git a/posts/first.md b/posts/first.md\n+hello');
const result = await gitEngine.getDiff('/tmp/project', 'posts/first.md');
expect(mockDiff).toHaveBeenCalledWith(['--', 'posts/first.md']);
expect(result).toEqual({
filePath: 'posts/first.md',
patch: 'diff --git a/posts/first.md b/posts/first.md\n+hello',
});
});
});
describe('ensureGitignore', () => {
it('should create .gitignore with default system metadata entries when missing', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));

View File

@@ -144,6 +144,7 @@ const mockGitEngine = {
checkAvailability: vi.fn(),
getRepoState: vi.fn(),
getStatus: vi.fn(),
getDiff: vi.fn(),
initializeRepo: vi.fn(),
ensureGitignore: vi.fn(),
pruneLfsCache: vi.fn(),
@@ -318,6 +319,23 @@ describe('IPC Handlers', () => {
});
});
describe('git:diff', () => {
it('should pass project path and file path to GitEngine.getDiff', async () => {
mockGitEngine.getDiff.mockResolvedValue({
filePath: 'posts/first.md',
patch: 'diff --git a/posts/first.md b/posts/first.md',
});
const result = await invokeHandler('git:diff', '/repo', 'posts/first.md');
expect(mockGitEngine.getDiff).toHaveBeenCalledWith('/repo', 'posts/first.md');
expect(result).toEqual({
filePath: 'posts/first.md',
patch: 'diff --git a/posts/first.md b/posts/first.md',
});
});
});
describe('git:init', () => {
it('should pass project path to GitEngine.initializeRepo', async () => {
mockGitEngine.initializeRepo.mockResolvedValue({ success: true });

View File

@@ -0,0 +1,45 @@
import React from 'react';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { GitDiffView } from '../../../src/renderer/components/GitDiffView/GitDiffView';
import { useAppStore } from '../../../src/renderer/store';
describe('GitDiffView', () => {
beforeEach(() => {
vi.clearAllMocks();
useAppStore.setState({
activeProject: {
id: 'project-1',
name: 'Test Project',
slug: 'test-project',
isActive: true,
dataPath: '/repo/path',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
});
(window as any).electronAPI = {
...(window as any).electronAPI,
git: {
...(window as any).electronAPI?.git,
getDiff: vi.fn().mockResolvedValue({
filePath: 'posts/first.md',
patch: 'diff --git a/posts/first.md b/posts/first.md\n+hello',
}),
},
app: {
...(window as any).electronAPI?.app,
getDefaultProjectPath: vi.fn().mockResolvedValue('/repo/path'),
},
};
});
it('loads and renders the git diff patch for the selected file', async () => {
render(<GitDiffView filePath="posts/first.md" />);
expect(await screen.findByText(/diff --git a\/posts\/first\.md b\/posts\/first\.md/i)).toBeInTheDocument();
expect((window as any).electronAPI.git.getDiff).toHaveBeenCalledWith('/repo/path', 'posts/first.md');
});
});

View File

@@ -4,6 +4,8 @@ import { render, screen, fireEvent, act } from '@testing-library/react';
import { GitSidebar } from '../../../src/renderer/components/GitSidebar/GitSidebar';
import { useAppStore } from '../../../src/renderer/store';
const getStore = () => useAppStore.getState();
describe('GitSidebar', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -17,6 +19,8 @@ describe('GitSidebar', () => {
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
tabs: [],
activeTabId: null,
});
(window as any).electronAPI = {
@@ -28,6 +32,8 @@ describe('GitSidebar', () => {
git: {
checkAvailability: vi.fn().mockResolvedValue({ gitFound: true, version: '2.49.0' }),
getRepoState: vi.fn().mockResolvedValue({ isRepo: false, hasRemote: false }),
getStatus: vi.fn().mockResolvedValue({ files: [], counts: { untracked: 0, modified: 0, deleted: 0, renamed: 0, staged: 0, total: 0 } }),
getDiff: vi.fn().mockResolvedValue({ filePath: 'posts/a.md', patch: 'diff --git a/posts/a.md b/posts/a.md' }),
init: vi.fn().mockResolvedValue({ success: true }),
ensureGitignore: vi.fn().mockResolvedValue({ updated: false, created: false, addedEntries: [] }),
onInitProgress: vi.fn().mockImplementation(() => () => {}),
@@ -49,6 +55,88 @@ describe('GitSidebar', () => {
expect(await screen.findByRole('button', { name: /initialize git/i })).toBeInTheDocument();
});
it('renders open changes list when repository exists', async () => {
(window as any).electronAPI.git.getRepoState = vi.fn().mockResolvedValue({
isRepo: true,
rootPath: '/repo/path',
currentBranch: 'main',
hasRemote: true,
});
(window as any).electronAPI.git.getStatus = vi.fn().mockResolvedValue({
files: [
{ path: 'posts/first.md', status: 'modified' },
{ path: 'posts/second.md', status: 'untracked' },
],
counts: { untracked: 1, modified: 1, deleted: 0, renamed: 0, staged: 0, total: 2 },
});
render(<GitSidebar />);
expect(await screen.findByText(/open changes/i)).toBeInTheDocument();
expect(screen.getByText('posts/first.md')).toBeInTheDocument();
expect(screen.getByText('posts/second.md')).toBeInTheDocument();
expect(screen.getByText(/version history/i)).toBeInTheDocument();
});
it('single click opens and reuses a transient git-diff tab', async () => {
(window as any).electronAPI.git.getRepoState = vi.fn().mockResolvedValue({
isRepo: true,
rootPath: '/repo/path',
currentBranch: 'main',
hasRemote: true,
});
(window as any).electronAPI.git.getStatus = vi.fn().mockResolvedValue({
files: [
{ path: 'posts/first.md', status: 'modified' },
{ path: 'posts/second.md', status: 'untracked' },
],
counts: { untracked: 1, modified: 1, deleted: 0, renamed: 0, staged: 0, total: 2 },
});
render(<GitSidebar />);
const first = await screen.findByRole('button', { name: /posts\/first\.md/i });
const second = screen.getByRole('button', { name: /posts\/second\.md/i });
await act(async () => {
fireEvent.click(first);
});
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({ type: 'git-diff', id: 'git-diff:posts/first.md', isTransient: true });
await act(async () => {
fireEvent.click(second);
});
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({ type: 'git-diff', id: 'git-diff:posts/second.md', isTransient: true });
});
it('double click opens a persistent git-diff tab', async () => {
(window as any).electronAPI.git.getRepoState = vi.fn().mockResolvedValue({
isRepo: true,
rootPath: '/repo/path',
currentBranch: 'main',
hasRemote: true,
});
(window as any).electronAPI.git.getStatus = vi.fn().mockResolvedValue({
files: [{ path: 'posts/first.md', status: 'modified' }],
counts: { untracked: 0, modified: 1, deleted: 0, renamed: 0, staged: 0, total: 1 },
});
render(<GitSidebar />);
const first = await screen.findByRole('button', { name: /posts\/first\.md/i });
await act(async () => {
fireEvent.doubleClick(first);
});
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({ type: 'git-diff', id: 'git-diff:posts/first.md', isTransient: false });
});
it('initializes repository and refreshes repo state after clicking Initialize Git', async () => {
const getRepoStateMock = vi
.fn()
@@ -70,7 +158,7 @@ describe('GitSidebar', () => {
});
await vi.waitFor(() => {
expect(screen.getByText(/git repository ready/i)).toBeInTheDocument();
expect(screen.getByText(/open changes/i)).toBeInTheDocument();
});
});

View File

@@ -147,6 +147,40 @@ describe('Tab Management', () => {
expect(getStore().tabs).toHaveLength(2);
expect(getStore().activeTabId).toBe('post-1');
});
it('should open git diff in a transient tab on single click', () => {
getStore().openTab({ type: 'git-diff', id: 'git-diff:posts/first.md', isTransient: true });
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({
type: 'git-diff',
id: 'git-diff:posts/first.md',
isTransient: true,
});
});
it('should reuse transient git diff tab on subsequent single click', () => {
getStore().openTab({ type: 'git-diff', id: 'git-diff:posts/first.md', isTransient: true });
getStore().openTab({ type: 'git-diff', id: 'git-diff:posts/second.md', isTransient: true });
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({
type: 'git-diff',
id: 'git-diff:posts/second.md',
isTransient: true,
});
});
it('should open git diff in a persistent tab on double click', () => {
getStore().openTab({ type: 'git-diff', id: 'git-diff:posts/first.md', isTransient: false });
expect(getStore().tabs).toHaveLength(1);
expect(getStore().tabs[0]).toMatchObject({
type: 'git-diff',
id: 'git-diff:posts/first.md',
isTransient: false,
});
});
});
describe('Closing Tabs', () => {

View File

@@ -48,6 +48,7 @@ Object.defineProperty(globalThis, 'window', {
checkAvailability: vi.fn(),
getRepoState: vi.fn(),
getStatus: vi.fn(),
getDiff: vi.fn(),
init: vi.fn(),
},
posts: {