# frozen_string_literal: true

require 'spec_helper'
require 'git/commands/clone'

require 'git/execution_context/global'

RSpec.describe Git::Commands::Clone, :integration do
  include Git::IntegrationTestHelpers

  # Clone creates new repositories, so it uses an unbound execution context
  # (no pre-existing repo), matching how Git.clone calls it in production.
  subject(:command) { described_class.new(execution_context) }

  let(:execution_context) { Git::ExecutionContext::Global.new }
  let(:source_dir) { Dir.mktmpdir }
  let(:clone_dir) { Dir.mktmpdir }

  before do
    source_repo = init_test_repo(source_dir)
    File.write(File.join(source_dir, 'file.txt'), "content\n", mode: 'wb')
    source_repo.add('file.txt')
    source_repo.commit('Initial commit')
  end

  after do
    FileUtils.rm_rf(source_dir)
    FileUtils.rm_rf(clone_dir)
  end

  describe '#call' do
    context 'when the command succeeds' do
      it 'returns a CommandLineResult' do
        result = command.call(source_dir, File.join(clone_dir, 'cloned'))

        expect(result).to be_a(Git::CommandLine::Result)
      end
    end

    context 'when the command fails' do
      it 'raises FailedError with a nonexistent source' do
        nonexistent = File.join(clone_dir, 'nonexistent_source')
        expect { command.call(nonexistent, File.join(clone_dir, 'cloned')) }.to raise_error(Git::FailedError)
      end
    end
  end
end
