# frozen_string_literal: true

require 'spec_helper'
require 'git/commands/remote/rename'

RSpec.describe Git::Commands::Remote::Rename, :integration do
  include_context 'in an empty repository'

  subject(:command) { described_class.new(execution_context) }

  let(:remote_dir) { Dir.mktmpdir }
  let(:remote_repo) do
    init_test_repo(remote_dir, initial_branch:)
  end

  after do
    FileUtils.rm_rf(remote_dir)
  end

  describe '#call' do
    context 'when the command succeeds' do
      it 'renames a configured remote' do
        repo.remote_add('origin', remote_repo.dir.to_s)

        result = command.call('origin', 'upstream')

        expect(result).to be_a(Git::CommandLine::Result)
        expect(repo.remote_list.map(&:name)).to include('upstream')
        expect(repo.remote_list.map(&:name)).not_to include('origin')
      end
    end

    context 'when the command fails' do
      it 'raises FailedError for a missing remote' do
        expect { command.call('origin', 'upstream') }.to raise_error(Git::FailedError, /No such remote/i)
      end
    end
  end
end
