diff --git a/src/lib/commands/git/push.test.ts b/src/lib/commands/git/push.test.ts new file mode 100644 index 00000000..aac97c4f --- /dev/null +++ b/src/lib/commands/git/push.test.ts @@ -0,0 +1,103 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { GitPushCommand } from './push'; +import type { Git } from '../../git'; +import type { JSRuntimeFS } from '../../JSRuntime'; + +describe('GitPushCommand', () => { + let pushCommand: GitPushCommand; + let mockGit: Partial; + let mockFS: Partial; + const mockPwd = '/test-repo'; + + beforeEach(() => { + // Mock the filesystem + mockFS = { + stat: vi.fn().mockResolvedValue({}), + }; + + // Mock Git methods + mockGit = { + currentBranch: vi.fn().mockResolvedValue('main'), + listRemotes: vi.fn().mockResolvedValue([]), + push: vi.fn().mockResolvedValue(undefined), + }; + + pushCommand = new GitPushCommand({ + git: mockGit as Git, + fs: mockFS as JSRuntimeFS, + pwd: mockPwd, + }); + }); + + it('should fail when not in a git repository', async () => { + const mockFSNoRepo: Partial = { + stat: vi.fn().mockRejectedValue(new Error('ENOENT')), + }; + + const cmd = new GitPushCommand({ + git: mockGit as Git, + fs: mockFSNoRepo as JSRuntimeFS, + pwd: '/non-existent', + }); + + const result = await cmd.execute(['origin', 'main']); + + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain('not a git repository'); + }); + + it('should fail when no remote is configured', async () => { + const result = await pushCommand.execute(['origin', 'main']); + + expect(result.exitCode).toBe(1); + expect(result.stderr).toContain('does not appear to be a git repository'); + }); + + it('should pass force flag to git.push when --force is used', async () => { + // Configure a mock remote + mockGit.listRemotes = vi.fn().mockResolvedValue([ + { remote: 'origin', url: 'https://example.com/repo.git' }, + ]); + + await pushCommand.execute(['origin', 'main', '--force']); + + // Verify that git.push was called with force: true + expect(mockGit.push).toHaveBeenCalledWith( + expect.objectContaining({ + force: true, + }) + ); + }); + + it('should pass force flag to git.push when -f is used', async () => { + // Configure a mock remote + mockGit.listRemotes = vi.fn().mockResolvedValue([ + { remote: 'origin', url: 'https://example.com/repo.git' }, + ]); + + await pushCommand.execute(['origin', 'main', '-f']); + + // Verify that git.push was called with force: true + expect(mockGit.push).toHaveBeenCalledWith( + expect.objectContaining({ + force: true, + }) + ); + }); + + it('should not pass force flag when not specified', async () => { + // Configure a mock remote + mockGit.listRemotes = vi.fn().mockResolvedValue([ + { remote: 'origin', url: 'https://example.com/repo.git' }, + ]); + + await pushCommand.execute(['origin', 'main']); + + // Verify that git.push was called with force: false + expect(mockGit.push).toHaveBeenCalledWith( + expect.objectContaining({ + force: false, + }) + ); + }); +}); diff --git a/src/lib/commands/git/push.ts b/src/lib/commands/git/push.ts index 4ce3e7bf..03f2cf4d 100644 --- a/src/lib/commands/git/push.ts +++ b/src/lib/commands/git/push.ts @@ -33,7 +33,7 @@ export class GitPushCommand implements GitSubcommand { return createErrorResult('fatal: not a git repository (or any of the parent directories): .git'); } - const { remote, branch } = this.parseArgs(args); + const { remote, branch, options } = this.parseArgs(args); // Get current branch if not specified let targetBranch = branch; @@ -75,6 +75,7 @@ export class GitPushCommand implements GitSubcommand { remote: remote, ref: targetBranch, remoteRef: targetBranch, + force: options.force, onAuth: (url) => findCredentialsForRepo(url, settings.credentials), signer: this.signer, }); @@ -85,12 +86,14 @@ export class GitPushCommand implements GitSubcommand { if (error instanceof Error) { if (error.message.includes('authentication')) { return createErrorResult('fatal: Authentication failed. Please configure git credentials.'); - } else if (error.message.includes('rejected')) { - return createErrorResult(`error: failed to push some refs to '${remoteUrl}'\nhint: Updates were rejected because the remote contains work that you do not have locally.`); + } else if (error.message.includes('rejected') || error.message.includes('Updates were rejected')) { + return createErrorResult(`error: failed to push some refs to '${remoteUrl}'\nhint: ${error.message}`); } else if (error.message.includes('network')) { return createErrorResult('fatal: unable to access remote repository. Please check your network connection.'); } else if (error.message.includes('Nostr signer is required')) { return createErrorResult('fatal: Nostr signer is required for pushing to Nostr repositories. Please log in with a Nostr account.'); + } else if (error.message.includes('Everything up-to-date')) { + return createSuccessResult('Everything up-to-date\n'); } }