require "rails_helper"

RSpec.describe VideoAnalysisService do
  let(:project) { create(:project) }
  let(:video_analysis) { VideoAnalysis.new(video, false) }
  let(:audio_analysis) { AudioAnalysis.new(video) }
  let(:edl_event_gateway) { instance_double(EdlEventGateway, edl_events: []) }

  before :each do
    allow(EdlEventGateway).to receive(:new).and_return(edl_event_gateway)
  end

  describe "#presenter" do
    context "when video has graphics only EDL" do
      let(:video) { create(:video, :with_graphics_only_edl_file, project: project, edl_timecode_start: "01:00:55:00") }

      it "fetches all edl events" do
        subject.presenter(video, video_analysis, audio_analysis)

        expect(EdlEventGateway).to have_received(:new).with(
          instance_of(FilesForRequest),
          "00:00:00:00",
          nil,
        )
      end

      context "when edl_timecode_start not present" do
        it "fetches offset then all graphics EDL events" do
          video.edl_timecode_start = nil
          allow(EdlEventGateway).to receive(:new).and_return(instance_double(EdlEventGateway, edl_timecode_start: "01:00:55:00", edl_events: []))

          subject.presenter(video, video_analysis, audio_analysis)

          expect(EdlEventGateway).to have_received(:new).with(
            instance_of(FilesForRequest),
            "00:00:00:00",
            "00:00:00:00",
          )
          expect(EdlEventGateway).to have_received(:new).with(
            instance_of(GraphicsFilesForRequest),
            "00:00:00:00",
            nil,
          )
        end
      end

      context "when edl_timecode_start present" do
        it "fetches all graphics EDL events using edl_timecode_start" do
          subject.presenter(video, video_analysis, audio_analysis)

          expect(EdlEventGateway).to have_received(:new).with(
            instance_of(GraphicsFilesForRequest),
            "00:00:00:00",
            nil,
          )
        end
      end
    end

    context "when video missing graphics only EDL" do
      let(:video) { create(:video, project: project) }

      it "does not fetch all graphics EDL events" do
        subject.presenter(video, video_analysis, audio_analysis)

        expect(EdlEventGateway).not_to have_received(:new).with(
          instance_of(GraphicsFilesForRequest),
          "00:00:00:00",
          nil
        )
      end
    end
  end
end
