require 'spec_helper'

describe JamTracksBuilder do
  include UsesTempFiles
  include CarrierWave::Test::Matchers
  
  before (:all) do
    @user = FactoryGirl.create(:user)
    @jam_track = FactoryGirl.create(:jam_track)
    original_storage = JamTrackTrackUploader.storage = :fog
    original_storage = JamTrackRightUploader.storage = :fog
    @s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
  end

  after(:all) do
    JamTrackRightUploader.storage = @original_storage
    JamTrackTrackUploader.storage = @original_storage
  end

  before(:each) do
    pending "Works in production, but needs some local setup using the jamtracks git repo and more"
    @s3.delete_folder('jam_tracks')
  end

  it "should build" do
    ogg_path = File.join('spec', 'files', 'on.ogg')
    user = FactoryGirl.create(:user)
    jam_track_track = FactoryGirl.create(:jam_track_track)
    jam_track = jam_track_track.jam_track
    
    @s3.upload(jam_track_track.manually_uploaded_filename(:url_48), ogg_path)
    jam_track_track[:url_48] = jam_track_track.manually_uploaded_filename(:url_48)
    jam_track_track.save!
    
    jam_track_track[:url_48].should == jam_track_track.manually_uploaded_filename(:url_48)

    # verify it's on S3    
    @s3.exists?(jam_track_track[:url_48]).should be_true
    @s3.length(jam_track_track[:url_48]).should == File.size?(ogg_path)
    jam_track_track[:url_44].should be_nil

    # Check right
    jam_track_right = JamTrackRight.create(:user=>user, :jam_track=>jam_track)
    jam_track_right[:url_48].should be_nil
    jam_track_right[:url_44].should be_nil
    JamTracksBuilder.perform(jam_track_right.id, 48)
    jam_track_right.reload
    jam_track_right[:url_48].should == jam_track_right.store_dir + '/' + jam_track_right.filename(:url_48)
    jam_track_track[:url_44].should be_nil

    jam_track_right.signed_48.should be_true
    jam_track_right.signing_started_at_48.should_not be_nil
    jam_track_right.signed_44.should be_false
    jam_track_right.signing_started_at_44.should be_nil
  end

  describe "with bitrate 44" do
    it "should build" do
      ogg_path = File.join('spec', 'files', 'on.ogg')
      user = FactoryGirl.create(:user)

      # Should build bitrate 44 and only bitrate 44:
      jam_track_track = FactoryGirl.create(:jam_track_track)
      jam_track = jam_track_track.jam_track

      # uploader = JamTrackTrackUploader.new(jam_track_track, :url_44)
      # uploader.store!(File.open(ogg_path, 'rb'))
      @s3.upload(jam_track_track.manually_uploaded_filename(:url_44), ogg_path)
      jam_track_track[:url_44] = jam_track_track.manually_uploaded_filename(:url_44)
      jam_track_track.save!

      jam_track_track[:url_44].should == jam_track_track.manually_uploaded_filename(:url_44)

      # verify it's on S3    
      @s3.exists?(jam_track_track[:url_44]).should be_true
      @s3.length(jam_track_track[:url_44]).should == File.size?(ogg_path)
      jam_track_track[:url_48].should be_nil

      # Check right
      jam_track_right = JamTrackRight.create(:user=>user, :jam_track=>jam_track)
      jam_track_right[:url_44].should be_nil
      jam_track_right[:url_48].should be_nil
      JamTracksBuilder.perform(jam_track_right.id, 44)
      jam_track_right.reload
      jam_track_right[:url_44].should == jam_track_right.store_dir + '/' + jam_track_right.filename(:url_44)
      jam_track_right.url_44.should_not be_nil
      jam_track_track[:url_48].should be_nil

      jam_track_right.signed_44.should be_true
      jam_track_right.signing_started_at_44.should_not be_nil
      jam_track_right.signed_48.should be_false
      jam_track_right.signing_started_at_48.should be_nil
    end
  end
end