require 'lib/executors/server_executor/expiration_scheduler'
require 'workers/delete_expired_server_worker'

RSpec.describe Devops::Executor::ExpirationScheduler do
  let(:server) { build(:server) }

  describe '#schedule_expiration!' do
    it 'schedules server deleting at given time' do
      expect(DeleteExpiredServerWorker).to receive(:perform_in).with(120, server_id: server.id)
      described_class.new('2m', server).schedule_expiration!
    end

    it "doesn't schedule job if expires is nil" do
      expect(DeleteExpiredServerWorker).not_to receive(:perform_in)
      described_class.new(nil, server).schedule_expiration!
    end
  end

  describe '#interval_in_seconds' do
    def interval_in_seconds(expires)
      described_class.new(expires, server).interval_in_seconds
    end

    it 'recognizes seconds' do
      expect(interval_in_seconds('2s')).to eq 2
    end

    it 'recognizes minutes' do
      expect(interval_in_seconds('3m')).to eq 180
    end

    it 'recognizes hours' do
      expect(interval_in_seconds('1h')).to eq 3600
    end

    it 'recognizes days' do
      expect(interval_in_seconds('1d')).to eq 86400
    end

    it 'recognizes weeks' do
      expect(interval_in_seconds('1w')).to eq 604800
    end

    it 'raises on wrong format' do
      expect { interval_in_seconds('wrong') }.to raise_error(StandardError)
    end
  end
end