# frozen_string_literal: true

require 'spec_helper'
require 'git/commands/am/show_current_patch'

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

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

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

  describe '#call' do
    context 'when the command succeeds' do
      before do
        # Create a patch that will conflict when applied, leaving an am session in progress
        write_file('file.txt', "line2\n")
        repo.add('file.txt')
        repo.commit('Change to line2')

        mbox_content = execution_context.command_capturing('format-patch', '--stdout', 'HEAD~1').stdout
        mbox_file = File.join(repo_dir, 'patches.mbox')
        File.write(mbox_file, mbox_content, mode: 'wb')

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

        # Create a conflicting commit so that git am fails and leaves the session open
        write_file('file.txt', "conflict\n")
        repo.add('file.txt')
        repo.commit('Conflicting commit')

        execution_context.command_capturing('am', mbox_file, chdir: repo_dir, raise_on_failure: false)
      end

      it 'returns a CommandLineResult' do
        result = command.call

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

    context 'when the command fails' do
      it 'raises FailedError when no am session is in progress' do
        expect { command.call }
          .to raise_error(Git::FailedError, /am/)
      end
    end
  end
end
