require 'spec_helper'

describe JamTrackMixdownPackage do
  include UsesTempFiles

  it "can be created (factory girl)" do
    package = FactoryGirl.create(:jam_track_mixdown_package)
  end

  it "can be created" do
    mixdown= FactoryGirl.create(:jam_track_mixdown)

    package = JamTrackMixdownPackage.create(mixdown, JamTrackMixdownPackage::FILE_TYPE_OGG, 48, 'jkz')

    package.errors.any?.should == false
  end


  describe "signing_state" do
    it "quiet" do
      package = FactoryGirl.create(:jam_track_mixdown_package)
      package.signing_state.should eq('QUIET')
    end

    it "signed" do
      package = FactoryGirl.create(:jam_track_mixdown_package, signed: true, signing_started_at: Time.now)
      package.signing_state.should eq('SIGNED')
    end

    it "error" do
      package = FactoryGirl.create(:jam_track_mixdown_package, error_count: 1)
      package.signing_state.should eq('ERROR')
    end

    it "signing" do
      package = FactoryGirl.create(:jam_track_mixdown_package, signing:true, signing_started_at: Time.now, packaging_steps: 3, current_packaging_step:0, last_step_at:Time.now)
      package.signing_state.should eq('SIGNING')
    end

    it "signing timeout" do
      package = FactoryGirl.create(:jam_track_mixdown_package, signing: true, signing_started_at: Time.now - (APP_CONFIG.signing_job_signing_max_time + 1), packaging_steps: 3, current_packaging_step:0, last_step_at:Time.now)
      package.signing_state.should eq('SIGNING_TIMEOUT')
    end

    it "queued" do
      package = FactoryGirl.create(:jam_track_mixdown_package, signing_queued_at: Time.now)
      package.signing_state.should eq('QUEUED')
    end

    it "signing timeout" do
      package = FactoryGirl.create(:jam_track_mixdown_package, signing_queued_at: Time.now - (APP_CONFIG.mixdown_job_queue_max_time + 1))
      package.signing_state.should eq('QUEUED_TIMEOUT')
    end
  end

  describe "stats" do

    it "empty" do
      JamTrackMixdownPackage.stats['count'].should eq(0)
    end

    it "signing" do
      package = FactoryGirl.create(:jam_track_mixdown_package)
      JamTrackMixdownPackage.stats.should eq('count' => 1,
                                    'signing_count' => 0)

      package.signing = true
      package.save!

      JamTrackMixdownPackage.stats.should eq('count' => 1,
                                    'signing_count' => 1)
    end
  end

  describe "estimated_queue_time" do
    it "succeeds with no data" do
      JamTrackMixdownPackage.estimated_queue_time.should eq(0)
    end

    it "mixdown packages of different sorts" do
      package = FactoryGirl.create(:jam_track_mixdown_package, speed_pitched: true)
      JamTrackMixdownPackage.estimated_queue_time.should eq(0)

      package.queued = true
      package.save!
      JamTrackMixdownPackage.estimated_queue_time.should eq(APP_CONFIG.estimated_slow_mixdown_time * 1)

      package.speed_pitched = false
      package.save!

      JamTrackMixdownPackage.estimated_queue_time.should eq(APP_CONFIG.estimated_fast_mixdown_time * 1)

      right = FactoryGirl.create(:jam_track_right)
      JamTrackMixdownPackage.estimated_queue_time.should eq(APP_CONFIG.estimated_fast_mixdown_time * 1)

      right.queued = true
      right.save!
      JamTrackMixdownPackage.estimated_queue_time.should eq(APP_CONFIG.estimated_fast_mixdown_time * 1 + APP_CONFIG.estimated_jam_track_time * 1)
    end

  end
end

