require 'spec_helper'
require 'huginn_scheduler'

describe HuginnScheduler do
  before(:each) do
    @scheduler = HuginnScheduler.new
    stub
  end

  it "should stop the scheduler" do
    mock.instance_of(Rufus::Scheduler).stop
    @scheduler.stop
  end

  it "schould register the schedules with the rufus scheduler and run" do
    mock.instance_of(Rufus::Scheduler).join
    @scheduler.run!
  end

  it "should run scheduled agents" do
    mock(Agent).run_schedule('every_1h')
    mock.instance_of(IO).puts('Queuing schedule for every_1h')
    @scheduler.send(:run_schedule, 'every_1h')
  end

  it "should propagate events" do
    mock(Agent).receive!
    stub.instance_of(IO).puts
    @scheduler.send(:propagate!)
  end

  it "schould clean up expired events" do
    mock(Event).cleanup_expired!
    stub.instance_of(IO).puts
    @scheduler.send(:cleanup_expired_events!)
  end

  describe "#hour_to_schedule_name" do
    it "for 0h" do
      @scheduler.send(:hour_to_schedule_name, 0).should == 'midnight'
    end

    it "for the forenoon" do
      @scheduler.send(:hour_to_schedule_name, 6).should == '6am'
    end

    it "for 12h" do
      @scheduler.send(:hour_to_schedule_name, 12).should == 'noon'
    end

    it "for the afternoon" do
      @scheduler.send(:hour_to_schedule_name, 17).should == '5pm'
    end
  end

  describe "cleanup_failed_jobs!" do
    before do
      3.times do |i|
        Delayed::Job.create(failed_at: Time.now - i.minutes)
      end
      @keep = Delayed::Job.order(:failed_at)[1]
    end

    it "work with set FAILED_JOBS_TO_KEEP env variable", focus: true do
      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(-1)
      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
      @keep.id.should == Delayed::Job.order(:failed_at)[0].id
    end


    it "work without the FAILED_JOBS_TO_KEEP env variable" do
      old = ENV['FAILED_JOBS_TO_KEEP']
      ENV['FAILED_JOBS_TO_KEEP'] = nil
      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
      ENV['FAILED_JOBS_TO_KEEP'] = old
    end
  end
end

describe Rufus::Scheduler do
  before :each do
    @taoe, Thread.abort_on_exception = Thread.abort_on_exception, false
    @oso, @ose, $stdout, $stderr = $stdout, $stderr, StringIO.new, StringIO.new

    @scheduler = Rufus::Scheduler.new

    stub.any_instance_of(Agents::SchedulerAgent).second_precision_enabled { true }

    @agent1 = Agents::SchedulerAgent.new(name: 'Scheduler 1', options: { schedule: '*/1 * * * * *' }).tap { |a|
      a.user = users(:bob)
      a.save!
    }
    @agent2 = Agents::SchedulerAgent.new(name: 'Scheduler 2', options: { schedule: '*/1 * * * * *' }).tap { |a|
      a.user = users(:bob)
      a.save!
    }
  end

  after :each do
    @scheduler.shutdown

    Thread.abort_on_exception = @taoe
    $stdout, $stderr = @oso, @ose
  end

  describe '#schedule_scheduler_agents' do
    it 'registers active SchedulerAgents' do
      @scheduler.schedule_scheduler_agents

      expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent)).to eq([@agent1, @agent2])
    end

    it 'unregisters disabled SchedulerAgents' do
      @scheduler.schedule_scheduler_agents

      @agent1.update!(disabled: true)

      @scheduler.schedule_scheduler_agents

      expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent)).to eq([@agent2])
    end

    it 'unregisters deleted SchedulerAgents' do
      @scheduler.schedule_scheduler_agents

      @agent2.delete

      @scheduler.schedule_scheduler_agents

      expect(@scheduler.scheduler_agent_jobs.map(&:scheduler_agent)).to eq([@agent1])
    end
  end
end
