require 'rails_helper'

RSpec.describe BroadcastRecordingStarringsController, type: :controller do
  render_views

  let(:user) { create(:user) }
  let(:account) { user.primary_account }
  let(:project) { create(:project, account: user.primary_account) }

  before do
    sign_in user
    stub_mux_live_stream
  end

  describe "#create" do
    let(:broadcast) { create(:broadcast, project: project, name: "New Broadcast") }
    let(:recordings) { create_list(:broadcast_recording, 5, :with_random_asset_uid, broadcast: broadcast) }
    let(:starred_recordings) { create_list(:broadcast_recording, 5, :with_random_asset_uid, broadcast: broadcast, starred: true) }

    it "sets star property to true when recording is starred" do
      recordings.each do |recording|
        expect(recording.starred).to be_falsey
      end

      post :create, params: { project_id: project, broadcast_id: broadcast, broadcast_recording_id: recordings.first.id }, xhr: true

      expect(recordings.first.reload.starred).to eq true

      recordings[1..5].each do |recording|
        expect(recording.reload.starred).to be_falsey
      end
    end

    it "sets star property to false when recording is unstarred" do
      starred_recordings.each do |recording|
        expect(recording.starred).to be_truthy
      end

      post :create, params: { project_id: project, broadcast_id: broadcast, broadcast_recording_id: starred_recordings.first.id }, xhr: true

      expect(starred_recordings.first.reload.starred).to eq false

      starred_recordings[1..5].each do |recording|
        expect(recording.reload.starred).to eq true
      end
    end
  end
end
