require 'spec_helper'

describe "social metadata", type: :feature do

  include MusicSessionHelper
  include RecordingHelper


  subject { page }

  shared_examples_for :has_default_metadata do
    it "should have default metadata" do
      page.find('meta[property="fb:app_id"]', :visible => false)['content'].should == Rails.application.config.facebook_app_id
      page.find('meta[property="og:title"]', :visible => false)['content'].should == "JamKazam"
      page.find('meta[property="og:description"]', :visible => false)['content'].should == "Play music together over the Internet as if in the same room."
      page.find('meta[property="og:image"]', :visible => false)['content'].include?("/assets/web/logo-256").should be true
      page.find('meta[property="og:image:width"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:image:height"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:type"]', :visible => false)['content'].should == "website"
    end
  end

  describe "default layout metadata" do
    let(:user) {FactoryGirl.create(:user) }

    describe "web layout" do
      before(:each) do
        visit '/'
      end
      it_behaves_like :has_default_metadata
    end

    describe "corp layout" do
      before(:each) do
        visit '/corp/about'
      end
      it_behaves_like :has_default_metadata
    end

    describe "client layout" do
      before(:each) do
        sign_in user
        visit '/client'
      end
      it_behaves_like :has_default_metadata
    end

    describe "landing layout" do
      before(:each) do
        visit '/signin'
      end
      it_behaves_like :has_default_metadata
    end


  end

  describe "music session metadata" do

    let(:user) { FactoryGirl.create(:user) }
    let(:connection) { FactoryGirl.create(:connection, :user => user) }
    let(:instrument) { FactoryGirl.create(:instrument, :description => 'a great instrument') }
    let(:track) { FactoryGirl.create(:track, :connection => connection, :instrument => instrument) }
    let(:music_session) {
      ms = FactoryGirl.create(:active_music_session, :creator => user, :musician_access => true)
      # ms.connections << connection
      ms.save!
      connection.join_the_session(ms, true, nil, user, 10)
      ms
    }

    it "renders facebook metadata" do
      visit "/sessions/#{music_session.id}"

      page.find('meta[property="fb:app_id"]', :visible => false)['content'].should == Rails.application.config.facebook_app_id
      page.find('meta[property="og:title"]', :visible => false)['content'].should == title_for_music_session(music_session.music_session)
      page.find('meta[property="og:url"]', :visible => false)['content'].include?("/sessions/#{music_session.id}").should be true
      page.find('meta[property="og:description"]', :visible => false)['content'].should == music_session.music_session.description
      page.find('meta[property="og:image"]', :visible => false)['content'].include?("/assets/web/logo-256").should be true
      page.find('meta[property="og:image:width"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:image:height"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:type"]', :visible => false)['content'].should == "website"
    end
  end

  describe "recording metadata" do

    before(:each) 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(:active_music_session, :creator => @user, :musician_access => true)
      # @music_session.connections << @connection
      @music_session.save
      @connection.join_the_session(@music_session, true, nil, @user, 10)
      @recording = Recording.start(@music_session, @user)
      @recording.stop
      @recording.reload
      @genre = FactoryGirl.create(:genre)
      @recording.claim(@user, "name", "description", @genre, true)
      @recording.reload
      @claimed_recording = @recording.claimed_recordings.first
    end

    it "renders facebook metadata" do
      visit "/recordings/#{@claimed_recording.id}"

      page.find('meta[property="fb:app_id"]', :visible => false)['content'].should == Rails.application.config.facebook_app_id
      page.find('meta[property="og:title"]', :visible => false)['content'].should == title_for_claimed_recording(@claimed_recording)
      page.find('meta[property="og:url"]', :visible => false)['content'].include?("/recordings/#{@claimed_recording.id}").should be true
      page.find('meta[property="og:description"]', :visible => false)['content'].should == @claimed_recording.name
      page.find('meta[property="og:image"]', :visible => false)['content'].include?("/assets/web/logo-256").should be true
      page.find('meta[property="og:image:width"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:image:height"]', :visible => false)['content'].should == "256"
      page.find('meta[property="og:type"]', :visible => false)['content'].should == "website"
    end
  end
end