require "rails_helper"

describe VideoAnalyses::EdlEventsController do
  let(:user) { create(:user) }
  let(:project) { create(:project, account: user.primary_account) }
  let(:video) { create(:video, project: project) }

  before :each do
    sign_in(user)
  end

  describe "#create" do
    context "when 1 EDL event" do
      before :each do
        allow(EdlEventGateway).to receive(:new).and_return(
          double("gateway", edl_events: [
            EdlEvent.new(
              timecode_in: "timecode_in",
              timecode_out: "timecode_out",
              duration: "duration",
              source_file_name: "source_file_name",
              clip_name: "clip_name",
              description: "description",
            )
          ])
        )
      end

      it "sets timecode, edl_events, info_message" do
        post :create, xhr: true, params: { video_id: video, edl_event: { time_elapsed: 2.5 } }

        expect(response).to be_successful
        expect(assigns(:timecode)).to eq(Timecode.from_seconds(2.5).to_s)
        expect(assigns(:edl_events)).to eq([
          EdlEvent.new(
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
          )
        ])
        expect(assigns(:info_message)).to eq "One EDL event for this timecode was found and is shown below"
      end
    end

    context "when no EDL events" do
      before :each do
        allow(EdlEventGateway).to receive(:new).and_return(
          double("gateway", edl_events: [])
        )
      end

      it "sets timecode, edl_events, info_message" do
        post :create, xhr: true, params: { video_id: video, edl_event: { time_elapsed: 2.5 } }

        expect(response).to be_successful
        expect(assigns(:timecode)).to eq(Timecode.from_seconds(2.5).to_s)
        expect(assigns(:edl_events)).to eq([])
        expect(assigns(:info_message)).to eq "No EDL events for this timecode were found"
      end
    end

    context "when multiple EDL events" do
      before :each do
        allow(EdlEventGateway).to receive(:new).and_return(
          double("gateway", edl_events: [
            EdlEvent.new(
              timecode_in: "timecode_in",
              timecode_out: "timecode_out",
              duration: "duration",
              source_file_name: "source_file_name",
              clip_name: "clip_name",
              description: "description",
            ),
            EdlEvent.new(
              timecode_in: "timecode_in",
              timecode_out: "timecode_out",
              duration: "duration",
              source_file_name: "source_file_name",
              clip_name: "clip_name",
              description: "description",
            ),
          ])
        )
      end

      it "sets timecode, edl_events, info_message" do
        post :create, xhr: true, params: { video_id: video, edl_event: { time_elapsed: 2.5 } }

        expect(response).to be_successful
        expect(assigns(:timecode)).to eq(Timecode.from_seconds(2.5).to_s)
        expect(assigns(:edl_events)).to eq([
          EdlEvent.new(
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
          ),
          EdlEvent.new(
            timecode_in: "timecode_in",
            timecode_out: "timecode_out",
            duration: "duration",
            source_file_name: "source_file_name",
            clip_name: "clip_name",
            description: "description",
          ),
        ])
        expect(assigns(:info_message)).to eq "Multiple EDL events for this timecode were found and are shown below"
      end
    end
  end
end
