require 'spec_helper'

describe RecordedTrack do

  before do
      @user = FactoryGirl.create(:user)
      @connection = FactoryGirl.create(:connection, :user => @user)
      @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument')
      @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
      @recording = FactoryGirl.create(:recording, :owner => @user)
  end

  it "should copy from a regular track properly" do
    @recorded_track = RecordedTrack.create_from_track(@track, @recording)
  
    @recorded_track.user.id.should == @track.connection.user.id
    @recorded_track.instrument.id.should == @track.instrument.id
    @recorded_track.next_part_to_upload.should == 0
    @recorded_track.fully_uploaded.should == false
  end
  
  it "should update the next part to upload properly" do
    @recorded_track = RecordedTrack.create_from_track(@track, @recording)
    @recorded_track.upload_part_complete(0)
    @recorded_track.upload_part_complete(1)
    @recorded_track.upload_part_complete(2)
    @recorded_track.next_part_to_upload.should == 3
  end


  it "should error if the wrong part is uploaded" do
    @recorded_track = RecordedTrack.create_from_track(@track, @recording)
    @recorded_track.upload_part_complete(0)
    @recorded_track.upload_part_complete(1)
    expect { @recorded_track.upload_part_complete(3) }.to raise_error
    @recorded_track.next_part_to_upload.should == 2
  end

  it "properly finds a recorded track given its upload filename" do
    @recorded_track = RecordedTrack.create_from_track(@track, @recording)
    RecordedTrack.find_by_upload_filename("recording_#{@recorded_track.id}").should == @recorded_track
  end

  it "gets a url for the track" do
    @recorded_track = RecordedTrack.create_from_track(@track, @recording)
    @recorded_track.url.should == S3Manager.url(S3Manager.hashed_filename("recorded_track", @recorded_track.id))
  end


end


