2014-06-16 20:41:37 +00:00
require 'spec_helper'
describe ApiMusicSessionsController do
render_views
let ( :tracks ) { [ { 'sound' = > 'mono' , 'client_track_id' = > 'abc' , 'instrument_id' = > 'piano' } ] }
let ( :user ) { FactoryGirl . create ( :user ) }
let ( :conn ) { FactoryGirl . create ( :connection , :user = > user ) }
let ( :other ) { FactoryGirl . create ( :user ) }
let ( :other_conn ) { FactoryGirl . create ( :connection , user : other , ip_address : '2.2.2.2' ) }
let ( :third_user ) { FactoryGirl . create ( :user ) }
before ( :each ) do
controller . current_user = user
ActiveMusicSession . delete_all
MusicSession . delete_all
end
describe " ams_index " do
it " no results " do
get :ams_index , { client_id : conn . client_id }
response . should be_success
json = JSON . parse ( response . body , :symbolize_names = > true )
json . length . should == 0
end
it " just self " do
# create a session with self in it
ams = FactoryGirl . create ( :active_music_session , creator : user )
conn . join_the_session ( ams . music_session , true , tracks , user , 10 )
conn . errors . any? . should be_false
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
json . length . should == 1
json [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :latency ] . should be_nil
end
it " someone else with no score with self " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , user , 10 )
other_conn . errors . any? . should be_false
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
json . length . should == 1
json [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :latency ] . should be_nil
end
it " someone else with a score with a self " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , other , 10 )
other_conn . errors . any? . should be_false
Score . createx ( conn . locidispid , conn . client_id , conn . addr , other_conn . locidispid , other_conn . client_id , other_conn . addr , 20 , nil )
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
json . length . should == 1
2014-06-16 23:13:39 +00:00
json [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :latency ] . should == 17 . 5 . ceil
2014-06-16 20:41:37 +00:00
end
it " someone else with a score with a self, and another with an approved RSVP " do
# create a session with someone else in it, but no score
ams = FactoryGirl . create ( :active_music_session , creator : other )
other_conn . join_the_session ( ams . music_session , true , tracks , other , 10 )
other_conn . errors . any? . should be_false
2014-06-16 23:13:39 +00:00
# we need to make sure that third_user, (the RSVP user) has a locidispid matching the searching user
third_user . last_jam_audio_latency = 10 # RSVP's are an 'offline' search, meaning they use user.last_jam_audio_latency instead of connection.last_jam_audio_latency
third_user . last_jam_locidispid = conn . locidispid
third_user . save!
2014-06-16 20:41:37 +00:00
Score . createx ( conn . locidispid , conn . client_id , conn . addr , other_conn . locidispid , other_conn . client_id , other_conn . addr , 20 , nil )
rsvp_slot = FactoryGirl . create ( :rsvp_slot , music_session : ams . music_session , instrument : Instrument . find ( 'piano' ) )
rsvp_request = FactoryGirl . create ( :rsvp_request , user : third_user )
rsvp_request_rsvp_slot = FactoryGirl . create ( :rsvp_request_rsvp_slot , chosen : true , rsvp_request : rsvp_request , rsvp_slot : rsvp_slot )
get :ams_index , { client_id : conn . client_id }
json = JSON . parse ( response . body , :symbolize_names = > true )
json . length . should == 1
json [ 0 ] [ :active_music_session ] [ :participants ] [ 0 ] [ :user ] [ :latency ] . should_not be_nil
json [ 0 ] [ :approved_rsvps ] [ 0 ] [ :id ] . should == third_user . id
2014-06-16 23:13:39 +00:00
json [ 0 ] [ :approved_rsvps ] [ 0 ] [ :latency ] . should == 17 . 5 . ceil # ( 20 (network score) + 10 (third_user.last_jam_audio_latency)+ 5 (user.last_jam_audio_latency) ) / 2
2014-06-16 20:41:37 +00:00
end
end
end