require 'spec_helper'

describe ApiLinksController, type: :controller do
  render_views

  let(:partner1) {FactoryGirl.create(:affiliate_partner)}
  let(:user_partner1) { partner1.partner_user }

  before(:each) do
    ClaimedRecording.delete_all
    Recording.delete_all
    MusicSession.delete_all
    JamTrackRight.delete_all
    JamTrack.delete_all
    controller.current_user = nil
  end

  describe "jamtrack_song_index" do
    it "succeeds with empty data" do
      get :jamtrack_song_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(0)
    end

    it "succeeds with data" do
      jam_track = FactoryGirl.create(:jam_track)
      get :jamtrack_song_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(1)
      body[0]['url'].should_not be_nil
      body[0]['target'].should_not be_nil
    end
  end

  describe "jamkazam_general_index" do
    it "succeeds" do
      get :jamkazam_general_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(3)
    end
  end


  describe "session_index" do
    let(:open_params) {
      {
          name: "session 1",
          description: "my session",
          genres: ['ambient'],
          musician_access: true,
          fan_access: true,
          approval_required: true,
          fan_chat: true,
          legal_policy: 'Standard',
          language: 'eng',
          start: "Thu Jul 10 2020 10:00 PM",
          duration: 30,
          timezone: "Central Time (US & Canada),America/Chicago",
          open_rsvps: true,
          legal_terms: true,
          recurring_mode: 'once',
          isUnstructuredRsvp: true,
          rsvp_slots: [{ instrument_id: "other", proficiency_level: 1, approve: true}]
      }
    }

    it "succeeds with no data" do
      get :session_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(0)
    end


    it "succeeds with one scheduled session" do
      session = MusicSession.create(user_partner1, open_params)
      get :session_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(1)
    end
  end

  describe "recording_index" do

    it "succeeds with no data" do
      get :recording_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(0)
    end

    it "succeeds with one recording" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: user_partner1)
      get :recording_index, format:'json', affiliate_id: partner1.id
      response.status.should eq(200)
      body = JSON.parse(response.body)
      body.length.should eq(1)
    end
  end

end
