# frozen_string_literal: true

require 'spec_helper'
require 'git/commands/worktree/repair'

RSpec.describe Git::Commands::Worktree::Repair, :integration,
               skip: unless_git('2.29.0', 'git worktree repair') do
  include_context 'in an empty repository'

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

  before do
    write_file('README.md', "# Hello\n")
    repo.add('README.md')
    repo.commit('Initial commit')
  end

  describe '#call' do
    context 'when the command succeeds' do
      it 'returns a CommandLineResult' do
        # git worktree repair inspects the CWD to detect linked worktree context;
        # run from within the repo directory so it does not mistakenly scan the
        # test runner's CWD.
        Dir.chdir(repo_dir) do
          result = command.call
          expect(result).to be_a(Git::CommandLine::Result)
        end
      end
    end

    context 'when the command fails' do
      it 'raises FailedError outside a git repository' do
        Dir.mktmpdir do |tmpdir|
          non_repo = Git.init(tmpdir, initial_branch: 'main')
          FileUtils.rm_rf(File.join(tmpdir, '.git'))
          non_git_command = described_class.new(non_repo.execution_context)
          expect { non_git_command.call }
            .to raise_error(Git::FailedError, /not a git repository/)
        end
      end
    end
  end
end
