require 'rails_helper'

RSpec.describe ZoomMeeting, type: :model do
  let(:zoom_meeting) { build(:zoom_meeting, api_meeting_id: nil) }
  let(:meeting_dictionary) { {"start_url" => "https://start_url", "join_url" => "https://join_url"} }

  before :all do
    WebMock.disable_net_connect!(allow_localhost: true)
  end

  before :each do
    allow_any_instance_of(ZoomGateway).to receive(:find_meeting).and_return(meeting_dictionary)
    allow_any_instance_of(ZoomGateway).to receive(:create_meeting).and_return("meeting_id")
    allow_any_instance_of(ZoomGateway).to receive(:create_host).and_return("host_id")
  end


  describe "attachments" do
    it { is_expected.to respond_to(:recording) }
  end

  describe "validations" do
    context '#recording' do
      it { is_expected.to allow_content_type("video/mp4").for(:recording) }
      it { is_expected.not_to allow_content_types("image/png").for(:recording) }
    end
  end

  describe 'associations' do
    it { is_expected.to belong_to(:zoom_user) }
    it { is_expected.to belong_to(:project).optional(true) }
  end

  describe 'after_create' do
    it 'should assign api meeting id' do
      expect {
        zoom_meeting.save
      }.to change { zoom_meeting.api_meeting_id }
    end
  end

  describe ".meeting" do
    it 'returns meeting dictionary' do
      expect(zoom_meeting.meeting).to eq(meeting_dictionary)
    end

    it 'recreates meeting if the current on is expired' do
      # set the meeting id
      zoom_meeting.save

      # simulate expiration
      allow_any_instance_of(ZoomGateway).to receive(:find_meeting).with("meeting_id").and_raise(ZoomGateway::MeetingExpired)
      allow_any_instance_of(ZoomGateway).to receive(:find_meeting).with("new_meeting_id").and_return(meeting_dictionary)
      allow_any_instance_of(ZoomGateway).to receive(:create_meeting).and_return("new_meeting_id")

      expect {
        zoom_meeting.meeting
      }.to change { zoom_meeting.api_meeting_id }
    end
  end

  describe '.meeting_url' do
    before do
      zoom_meeting.api_meeting_id = "meeting_id"
      zoom_meeting.save
    end

    context 'meeting is already started' do
      it 'returns join_url' do
        zoom_meeting.started!
        expect(zoom_meeting.meeting_url).to eq('https://join_url')
      end
    end

    context 'meeting is not started yet' do
      it 'return start_url' do
        zoom_meeting.created!
        expect(zoom_meeting.meeting_url).to eq('https://start_url')
      end
    end

    context 'meeting is ended' do
      it 'returns start_url' do
        zoom_meeting.ended!
        expect(zoom_meeting.meeting_url).to eq('https://start_url')
      end
    end
  end

  describe ".join_url" do
    it "delegates to the meeting hash" do
      expect(zoom_meeting.meeting).to receive(:[]).with("join_url")
      zoom_meeting.join_url
    end
  end

  describe ".start_url" do
    it "delegates to the meeting hash" do
      expect(zoom_meeting.meeting).to receive(:[]).with("start_url")
      zoom_meeting.start_url
    end
  end
end
