# typed: true
# frozen_string_literal: true

require "download_strategy"

RSpec.describe DownloadStrategyDetector do
  describe "::detect" do
    subject(:strategy_detector) { described_class.detect(url, strategy) }

    let(:url) { "invalidurl" }
    let(:strategy) { nil }

    context "when given Git URL" do
      let(:url) { "git://example.com/foo.git" }

      it { is_expected.to eq(GitDownloadStrategy) }
    end

    context "when given SSH Git URL" do
      let(:url) { "ssh://git@example.com/foo.git" }

      it { is_expected.to eq(GitDownloadStrategy) }
    end

    context "when given a GitHub Git URL" do
      let(:url) { "https://github.com/homebrew/brew.git" }

      it { is_expected.to eq(GitHubGitDownloadStrategy) }
    end

    context "when given a PyPI URL" do
      let(:url) do
        "https://files.pythonhosted.org/packages/ab/cd/efg/example-package-1.2.3.tar.gz"
      end

      it { is_expected.to eq(PyPIDownloadStrategy) }
    end

    it "defaults to curl" do
      expect(strategy_detector).to eq(CurlDownloadStrategy)
    end

    it "raises an error when passed an unrecognized strategy" do
      expect do
        described_class.detect("foo", Class.new)
      end.to raise_error(TypeError)
    end
  end
end
