require 'spec_helper'

describe ApiClaimedRecordingsController do
  render_views

  before(:each) do
    S3Manager.set_unit_test
    @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)
    @music_session = FactoryGirl.create(:music_session, :creator => @user, :musician_access => true)      
    @music_session.connections << @connection
    @music_session.save
    @recording = Recording.start(@music_session.id, @user)
    @recording.stop
    @recording.reload
    @genre = FactoryGirl.create(:genre)
    @recording.claim(@user, "name", @genre, true, true)
    @recording.reload
    @claimed_recording = @recording.claimed_recordings.first
  end    
  
  describe "GET 'show'" do
    
    it "should show the right thing when one recording just finished" do
      controller.current_user = @user
      get :show, :id => @claimed_recording.id
#      puts response.body
      response.should be_success  
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["id"].should == @claimed_recording.id
      json["name"].should == @claimed_recording.name
      json["recording"]["id"].should == @recording.id
      json["recording"]["mixes"].length.should == 0
      json["recording"]["band"].should be_nil
      json["recorded_tracks"].length.should == 1
      json["recorded_tracks"].first["id"].should == @recording.recorded_tracks.first.id
      json["recorded_tracks"].first["url"].should == @recording.recorded_tracks.first.url
      json["recorded_tracks"].first["instrument"]["id"].should == @instrument.id
      json["recorded_tracks"].first["user"]["id"].should == @user.id
    end
    
    it "should show the right thing when one recording was just uploaded" do
      @recording.recorded_tracks.first.upload_complete      
      controller.current_user = @user
      get :show, :id => @claimed_recording.id
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["recording"]["mixes"].length.should == 1
      json["recording"]["mixes"].first["id"].should == @recording.mixes.first.id
      json["recording"]["mixes"].first["url"].should == @recording.mixes.first.url     
      json["recording"]["mixes"].first["is_completed"].should == false
    end


    it "should show the right thing when the mix was just uploaded" do
      @recording.recorded_tracks.first.upload_complete      
      @mix = Mix.next("server")
      @mix.finish(10000, "md5")
      controller.current_user = @user
      get :show, :id => @claimed_recording.id
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["recording"]["mixes"].length.should == 1
      json["recording"]["mixes"].first["id"].should == @recording.mixes.first.id
      json["recording"]["mixes"].first["is_completed"].should == true
    end
  end
  
  describe "GET 'index'" do
    it "should generate a single output" do
      controller.current_user = @user
      get :index
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json.length.should == 1
      json.first["id"].should == @claimed_recording.id

    end
  end
  
=begin
 We can't test these because rspec doesn't like that we return 204. It causes rails to return a 406. 
  describe "DELETE 'destroy'" do
    it "should delete properly" do
      controller.current_user = @user
      delete :delete, :id => @claimed_recording.id
      puts response
      puts response.body
      
      response.should be_success
      expect { ClaimedRecording.find(@claimed_recording.id) }.to raise_error
    end
  end
=end  


end