# typed: true
# frozen_string_literal: true

RSpec.describe Cask::Artifact::BashCompletion, :cask do
  let(:cask_token) { "basic-cask" }
  let(:cask) { Cask::CaskLoader.load(cask_token) }

  context "with install" do
    let(:install_phase) do
      lambda do
        cask.artifacts.grep(described_class).each do |artifact|
          artifact.install_phase(command: NeverSudoSystemCommand, force: false)
        end
      end
    end

    let(:source_path) { cask.staged_path.join("test.bash") }
    let(:target_path) { cask.config.bash_completion.join("test") }
    let(:full_source_path) { cask.staged_path.join("test.bash-completion") }
    let(:full_target_path) { cask.config.bash_completion.join("test") }

    context "with completion" do
      let(:cask_token) { "with-shellcompletion" }

      it "links the completion to the proper directory" do
        source_path.dirname.mkpath
        source_path.write ""

        install_phase.call

        expect(File).to be_identical target_path, source_path
      end
    end

    context "with long completion" do
      let(:cask_token) { "with-shellcompletion-long" }

      it "links the completion to the proper directory" do
        full_source_path.dirname.mkpath
        full_source_path.write ""

        install_phase.call

        expect(File).to be_identical full_target_path, full_source_path
      end
    end
  end
end
