# frozen_string_literal: true

require 'spec_helper'
require 'git/commands/apply'

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

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

  before do
    write_file('file.txt', "line1\n")
    repo.add('file.txt')
    repo.commit('Initial commit')
  end

  describe '#call' do
    context 'when the command succeeds' do
      let(:patch_file) { File.join(repo_dir, 'add_line2.patch') }

      before do
        write_file('file.txt', "line1\nline2\n")
        repo.add('file.txt')
        repo.commit('Add line2')

        patch_content = repo.diff_full('HEAD~1', 'HEAD')
        File.write(patch_file, "#{patch_content}\n", mode: 'wb')

        repo.reset('HEAD~1', hard: true)
      end

      it 'returns a CommandLineResult' do
        result = command.call(patch_file, chdir: repo_dir)

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

    context 'when the command fails' do
      it 'raises FailedError for an invalid patch' do
        bad_patch = File.join(repo_dir, 'bad.patch')
        File.write(bad_patch, "This is not a valid patch\n", mode: 'wb')

        expect { command.call(bad_patch, chdir: repo_dir) }
          .to raise_error(Git::FailedError, /bad\.patch/)
      end
    end
  end
end
