require 'katello_test_helper'
require 'helpers/product_test_data'

# used for including tested module
module Katello
  class DummyObject
    include SyncManagementHelper
    include SyncManagementHelper::RepoMethods
  end
end

module Katello
  describe SyncManagementHelper do
    include OrchestrationHelper

    before do
      @routes = Katello::Engine.routes
      disable_org_orchestration
      ProductTestData::PRODUCT_WITH_ATTRS.merge!(:provider => provider, :organization => provider.organization)
    end

    let(:organization) do
      get_organization
    end
    let(:provider) { organization.redhat_provider }
    let(:env_name) { 'test_environment' }
    let(:environment) { organization.library }
    let(:object) { DummyObject.new }
    let(:product_1) { Product.create!(ProductTestData::PRODUCT_WITH_ATTRS) }

    describe "#collect_repos (katello)" do #TODO: headpin
      subject { object.collect_repos([product_1], environment).first }
      it { value(subject.keys).must_include(:name, :id) }
    end

    describe "#repos? (katello)" do #TODO: headpin
      subject { object.repos?(object.collect_repos([product_1], environment).first) }
      it "should return false for a product without repos" do
        value(subject).must_equal(false)
      end
    end

    describe "#collect_minor" do
      let(:repositories) { ['1', '2', '2', nil].map { |minor| Repository.new(:root => RootRepository.new(:minor => minor)) } }
      let(:collected_by_minor) { object.collect_minor(repositories) }
      subject { collected_by_minor }
      it { value(subject.size).must_equal(2) }
    end

    describe "repositories with minor" do
      let(:repositories) { ['1', '2', '2', nil].map { |minor| Repository.new(:root => RootRepository.new(:minor => minor)) } }
      subject { object.collect_minor(repositories).first }
      it { value(subject.size).must_equal(2) }
      it { value(subject.keys).must_include('1', '2') }
      it "should group repositories by minor" do
        value(subject['2'].size).must_equal(2)
      end
    end

    describe "repositories without minor" do
      let(:repositories) { ['1', '2', '2', nil].map { |minor| Repository.new(:root => RootRepository.new(:minor => minor)) } }
      subject { object.collect_minor(repositories).last }
      it { value(subject.size).must_equal(1) }
    end

    describe "#collect_arches" do
      let(:repositories) { %w(i386 i386 x86_64).map { |arch| Repository.new(:root => RootRepository.new(:arch => arch)) } }
      subject { object.collect_arches(repositories) }
      it { value(subject.size).must_equal(2) }
      it { value(subject.keys).must_include('i386', 'x86_64') }
      it "should group repositories by architecture" do
        value(subject['i386'].size).must_equal(2)
      end
    end

    describe "#minors" do
      subject { object.minors('1' => [Repository.new(:root => RootRepository.new)]).first }
      it { value(subject.keys).must_include(:id, :name) }
    end

    describe "#arches" do
      subject { object.arches([Repository.new(:root => RootRepository.new(:arch => 'i386'))]).first }
      it { value(subject.keys).must_include(:id, :name) }
    end
  end
end
