feat: better diff. integration

This commit is contained in:
2026-02-16 12:03:22 +01:00
parent 9d71aa63fb
commit c5c3a55a5c
14 changed files with 336 additions and 33 deletions

View File

@@ -4,6 +4,18 @@ import { render, screen } from '@testing-library/react';
import { GitDiffView } from '../../../src/renderer/components/GitDiffView/GitDiffView';
import { useAppStore } from '../../../src/renderer/store';
vi.mock('@monaco-editor/react', () => ({
__esModule: true,
default: (_props: unknown) => null,
DiffEditor: (props: { original: string; modified: string; language?: string }) => (
<div data-testid="monaco-diff-editor">
<div>original:{props.original}</div>
<div>modified:{props.modified}</div>
<div>language:{props.language}</div>
</div>
),
}));
describe('GitDiffView', () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -24,9 +36,10 @@ describe('GitDiffView', () => {
...(window as any).electronAPI,
git: {
...(window as any).electronAPI?.git,
getDiff: vi.fn().mockResolvedValue({
getDiffContent: vi.fn().mockResolvedValue({
filePath: 'posts/first.md',
patch: 'diff --git a/posts/first.md b/posts/first.md\n+hello',
original: '# old line',
modified: '# new line',
}),
},
app: {
@@ -36,10 +49,12 @@ describe('GitDiffView', () => {
};
});
it('loads and renders the git diff patch for the selected file', async () => {
it('loads and renders Monaco diff editor with original and modified content', 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');
expect(await screen.findByTestId('monaco-diff-editor')).toBeInTheDocument();
expect((window as any).electronAPI.git.getDiffContent).toHaveBeenCalledWith('/repo/path', 'posts/first.md');
expect(screen.getByText('original:# old line')).toBeInTheDocument();
expect(screen.getByText('modified:# new line')).toBeInTheDocument();
});
});

View File

@@ -34,6 +34,7 @@ describe('GitSidebar', () => {
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' }),
getHistory: vi.fn().mockResolvedValue([]),
init: vi.fn().mockResolvedValue({ success: true }),
ensureGitignore: vi.fn().mockResolvedValue({ updated: false, created: false, addedEntries: [] }),
onInitProgress: vi.fn().mockImplementation(() => () => {}),
@@ -69,6 +70,15 @@ describe('GitSidebar', () => {
],
counts: { untracked: 1, modified: 1, deleted: 0, renamed: 0, staged: 0, total: 2 },
});
(window as any).electronAPI.git.getHistory = vi.fn().mockResolvedValue([
{
hash: 'abc123',
shortHash: 'abc123',
date: '2026-02-16T10:00:00.000Z',
subject: 'feat: add git sidebar',
author: 'Dev One',
},
]);
render(<GitSidebar />);
@@ -76,6 +86,25 @@ describe('GitSidebar', () => {
expect(screen.getByText('posts/first.md')).toBeInTheDocument();
expect(screen.getByText('posts/second.md')).toBeInTheDocument();
expect(screen.getByText(/version history/i)).toBeInTheDocument();
expect(screen.getByText(/feat: add git sidebar/i)).toBeInTheDocument();
expect(screen.getByText(/abc123/i)).toBeInTheDocument();
});
it('uses the same section-title class as posts published heading', async () => {
(window as any).electronAPI.git.getRepoState = vi.fn().mockResolvedValue({
isRepo: true,
rootPath: '/repo/path',
currentBranch: 'main',
hasRemote: true,
});
render(<GitSidebar />);
const openChangesHeader = await screen.findByText(/open changes/i);
const historyHeader = screen.getByText(/version history/i);
expect(openChangesHeader).toHaveClass('sidebar-section-title');
expect(historyHeader).toHaveClass('sidebar-section-title');
});
it('single click opens and reuses a transient git-diff tab', async () => {