require "rails_helper"

module ExcelReports
  module AudioReports
    RSpec.describe NatGeoMusicCueSheet do
      let(:video) { create(:video) }

      subject { described_class.new(video) }

      describe "#to_xls" do
        it "builds report" do
          package = instance_double(Axlsx::Package)
          workbook = instance_double(Axlsx::Workbook)
          stream = double(:stream, read: "my-report")
          allow(Axlsx::Package).to receive(:new).and_return(package)
          allow(package).to receive(:workbook).and_return(workbook)
          allow(package).to receive(:to_stream).and_return(stream)
          allow(NatGeoMusicCueSheets::MainSheet).to receive(:build)

          create(:audio_confirmation, :original, video: video)
          create(:audio_confirmation, :library, video: video)

          result = subject.to_xls

          expect(package).to have_received(:to_stream)
          expect(NatGeoMusicCueSheets::MainSheet).to have_received(:build).with(
            workbook,
            [instance_of(AudioConfirmationData), instance_of(AudioConfirmationData)],
          )
          expect(stream).to have_received(:read)
          expect(result).to eq("my-report")
        end

        it "sorts audio data by ascending timecode_in" do
          package = instance_double(Axlsx::Package)
          workbook = instance_double(Axlsx::Workbook)
          stream = double(:stream, read: "my-report")
          allow(Axlsx::Package).to receive(:new).and_return(package)
          allow(package).to receive(:workbook).and_return(workbook)
          allow(package).to receive(:to_stream).and_return(stream)
          allow(NatGeoMusicCueSheets::MainSheet).to receive(:build)

          earlier = create(:audio_confirmation, :original, video: video, timecode_in: "00:00:00:02")
          earliest = create(:audio_confirmation, :library, video: video, timecode_in: "00:00:00:01")
          early = create(:audio_confirmation, :library, video: video, timecode_in: "00:00:00:03")

          subject.to_xls

          expect(NatGeoMusicCueSheets::MainSheet).to have_received(:build).with(
            workbook,
            [
              AudioConfirmationData.new(earliest),
              AudioConfirmationData.new(earlier),
              AudioConfirmationData.new(early),
            ]
          )
        end
      end

      describe "#filename" do
        it "returns video filename with 'discovery-cue-sheet'" do
          expect(subject.filename).to eq "video_file-mp4_music-cue-sheet.xlsx"
        end
      end
    end
  end
end
