require 'spec_helper'

describe Mix 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)
    @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
    @mix = Mix.schedule(@recording, "{}")
  end
 
  it "should create a mix for a user's recording properly" do
    @mix.recording_id.should == @recording.id
    @mix.manifest.should == "{}"
    @mix.mix_server.should be_nil
    @mix.started_at.should be_nil
    @mix.completed_at.should be_nil
  end

  it "should fail to create a mix if the userid doesn't own the recording" do
    @user2 = FactoryGirl.create(:user)
    expect { Mix.schedule(@recording) }.to raise_error
  end
  
  it "should fail if the recording doesn't exist" do
    expect { @mix2 = Mix.schedule(Recording.find('lskdjflsd')) }.to raise_error
  end

  it "should return a mix when the cron asks for it" do
    this_mix = Mix.next("server")
    this_mix.id.should == @mix.id
    @mix.reload
    @mix.started_at.should_not be_nil
    @mix.mix_server.should == "server"
    @mix.completed_at.should be_nil
  end

  it "should record when a mix has finished" do
    Mix.find(@mix.id).finish(10000, "md5hash")
    @mix.reload
    @mix.completed_at.should_not be_nil
    @mix.length.should == 10000
    @mix.md5.should == "md5hash"
  end

  it "should re-run a mix if it was started a long time ago" do
    this_mix = Mix.next("server")
    @mix.reload
    @mix.started_at -= 1000000
    @mix.save
    this_mix = Mix.next("server")
    this_mix.id.should == @mix.id
  end

end


