# frozen_string_literal: true

require 'tzinfo'

RSpec.describe Html2rss::Selectors::PostProcessors::ParseTime do
  it { expect(described_class).to be < Html2rss::Selectors::PostProcessors::Base }

  context 'with known time_zone' do
    {
      'America/New_York' => 'Mon, 01 Jul 2019 12:00:00 -0400',
      'Europe/London' => 'Mon, 01 Jul 2019 12:00:00 +0100',
      'Europe/Berlin' => 'Mon, 01 Jul 2019 12:00:00 +0200'
    }.each_pair do |time_zone, expected|
      it "parses in time_zone #{time_zone}" do
        ctx = Html2rss::Selectors::Context.new(config: { channel: { time_zone: } })

        expect(described_class.new('2019-07-01 12:00', ctx).get).to eq expected
      end
    end
  end

  context 'with unknown time_zone' do
    it 'raises TZInfo::InvalidTimezoneIdentifier' do
      ctx = Html2rss::Selectors::Context.new(config: { channel: { time_zone: 'Foobar/Baz' } })

      expect { described_class.new('2019-07-01 12:00', ctx).get }.to raise_error(TZInfo::InvalidTimezoneIdentifier)
    end
  end

  context 'with nil time_zone' do
    it 'raises ArgumentError' do
      ctx = Html2rss::Selectors::Context.new(config: { channel: { time_zone: nil } })

      expect { described_class.new('2019-07-01 12:00', ctx).get }
        .to raise_error(ArgumentError, 'time_zone cannot be nil or empty')
    end
  end

  context 'with empty time_zone' do
    it 'raises ArgumentError' do
      ctx = Html2rss::Selectors::Context.new(config: { channel: { time_zone: '' } })

      expect { described_class.new('2019-07-01 12:00', ctx).get }
        .to raise_error(ArgumentError, 'time_zone cannot be nil or empty')
    end
  end
end
