require 'rails_helper'

module ExcelReports
  module GraphicReports
    RSpec.describe NatGeoTextGraphicsLog do
      let(:project) { create(:project) }

      describe '#to_xls' do
        it 'builds xls file' do
          graphics_element = create(:graphics_element)
          video = create(:video, graphics_elements: [graphics_element], project: project)
          package = double(:package)
          workbook = double(:workbook)
          stream = double(:stream)
          allow(Axlsx::Package).to receive(:new).and_return(package)
          allow(package).to receive(:workbook).and_return(workbook)

          expect(NatGeoTextGraphicsLogs::InternalProgramGfxLogSheet).to receive(:build).with(
            workbook,
            [graphics_element],
            video,
          )
          expect(package).to receive(:to_stream).and_return(stream)
          expect(stream).to receive(:read)

          described_class.new(video).to_xls
        end

        it 'sorts the element data by timecode' do
          graphics_elements = [
            build(:graphics_element, timecode_in: "00:00:10:00"),
            build(:graphics_element, timecode_in: "00:00:05:00"),
            build(:graphics_element, timecode_in: nil )
          ]
          video = create(:video, graphics_elements: graphics_elements, project: project)
          package = double(:package)
          workbook = double(:workbook)
          stream = double(:stream)
          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(stream).to receive(:read)

          expect(NatGeoTextGraphicsLogs::InternalProgramGfxLogSheet).to receive(:build).with(
            workbook, [graphics_elements.second, graphics_elements.first, graphics_elements.last], video,
          )

          described_class.new(video).to_xls
        end
      end
    end
  end
end
