Files
shakespeare/src/lib/tools/GitCommitTool.test.ts
T

222 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GitCommitTool } from './GitCommitTool';
import type { JSRuntimeFS } from '../JSRuntime';
// Mock isomorphic-git
vi.mock('isomorphic-git', () => ({
default: {
statusMatrix: vi.fn(),
add: vi.fn(),
remove: vi.fn(),
commit: vi.fn(),
currentBranch: vi.fn(),
getConfig: vi.fn(),
},
}));
import git from 'isomorphic-git';
// Mock filesystem
const mockFS: JSRuntimeFS = {
readFile: vi.fn(),
writeFile: vi.fn(),
readdir: vi.fn(),
mkdir: vi.fn(),
stat: vi.fn(),
lstat: vi.fn(),
unlink: vi.fn(),
rmdir: vi.fn(),
rename: vi.fn(),
readlink: vi.fn(),
symlink: vi.fn(),
};
describe('GitCommitTool', () => {
let tool: GitCommitTool;
beforeEach(() => {
vi.clearAllMocks();
tool = new GitCommitTool(mockFS, '/test/project');
// Setup default mocks
vi.mocked(git.statusMatrix).mockResolvedValue([
['src/file1.ts', 1, 1, 0], // Modified file (present in head and workdir, but not staged)
['src/file2.ts', 0, 1, 0], // New file (absent in head, present in workdir, not staged)
]);
vi.mocked(git.add).mockResolvedValue();
vi.mocked(git.commit).mockResolvedValue('abcd1234567890abcdef1234567890abcdef12');
vi.mocked(git.currentBranch).mockResolvedValue('main');
vi.mocked(git.getConfig).mockRejectedValue(new Error('Config not found'));
});
it('should have correct description and inputSchema', () => {
expect(tool.description).toBe('Commit changes to git with a commit message. Automatically adds all unstaged files.');
expect(tool.inputSchema).toBeDefined();
});
it('should successfully commit changes', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
const result = await tool.execute({ message: 'Add new feature' });
expect(mockFS.stat).toHaveBeenCalledWith('/test/project/.git');
expect(git.statusMatrix).toHaveBeenCalledWith({
fs: mockFS,
dir: '/test/project',
});
expect(git.add).toHaveBeenCalled();
expect(git.commit).toHaveBeenCalledWith({
fs: mockFS,
dir: '/test/project',
message: 'Add new feature',
author: {
name: 'shakespeare.diy',
email: '[email protected]',
},
committer: {
name: 'shakespeare.diy',
email: '[email protected]',
},
});
expect(result).toContain('✅ Successfully committed 2 files (abcd123)');
expect(result).toContain('Add new feature');
});
it('should handle empty commit message', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
await expect(tool.execute({ message: '' })).rejects.toThrow('❌ Commit message cannot be empty');
});
it('should handle whitespace-only commit message', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
await expect(tool.execute({ message: ' ' })).rejects.toThrow('❌ Commit message cannot be empty');
});
it('should handle not being in a git repository', async () => {
// Mock .git directory does not exist
vi.mocked(mockFS.stat).mockRejectedValue(new Error('File not found'));
await expect(tool.execute({ message: 'Test commit' })).rejects.toThrow('❌ Not a git repository');
expect(mockFS.stat).toHaveBeenCalledWith('/test/project/.git');
});
it('should handle commit with special characters in message', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
const specialMessage = 'Fix: handle "quotes" and \'apostrophes\' in commit message';
const result = await tool.execute({ message: specialMessage });
expect(git.commit).toHaveBeenCalledWith(expect.objectContaining({
message: specialMessage,
}));
expect(result).toContain('✅ Successfully committed');
expect(result).toContain(specialMessage);
});
it('should handle multiline commit message', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
const multilineMessage = 'Add new feature\n\nThis commit adds a new feature that allows users to do something amazing.\n\nCloses #123';
const result = await tool.execute({ message: multilineMessage });
expect(git.commit).toHaveBeenCalled();
expect(result).toContain('✅ Successfully committed');
expect(result).toContain('Add new feature');
});
it('should handle no changes to commit', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
// Mock no changes in status matrix
vi.mocked(git.statusMatrix).mockResolvedValue([]);
const result = await tool.execute({ message: 'Update documentation' });
expect(result).toContain('ℹ️ No changes to commit. Working tree is clean.');
expect(git.commit).not.toHaveBeenCalled();
});
it('should provide detailed commit information', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
const result = await tool.execute({ message: 'Update documentation' });
expect(result).toContain('📝 Commit: "Update documentation"');
expect(result).toContain('🔗 Hash:');
expect(result).toContain('🌿 Branch:');
expect(result).toContain('📊 Changes:');
});
it('should use git config when available', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
// Mock git config
vi.mocked(git.getConfig)
.mockResolvedValueOnce('John Doe') // user.name
.mockResolvedValueOnce('[email protected]'); // user.email
const result = await tool.execute({ message: 'Test commit' });
expect(git.commit).toHaveBeenCalledWith({
fs: mockFS,
dir: '/test/project',
message: 'Test commit',
author: {
name: 'John Doe',
email: '[email protected]',
},
committer: {
name: 'John Doe',
email: '[email protected]',
},
});
expect(result).toContain('✅ Successfully committed');
});
it('should handle very long commit message', async () => {
// Mock .git directory exists
vi.mocked(mockFS.stat).mockResolvedValue({
isDirectory: () => true,
isFile: () => false,
});
const longMessage = 'A'.repeat(1000); // Very long commit message
const result = await tool.execute({ message: longMessage });
expect(result).toContain('✅ Successfully committed');
});
});