require 'spec_helper'

describe 'DailyJob' do
  describe "calendar cleanup" do
    shared_examples_for :calendar_cleanup do |trigger_delete, end_count|
      before :each do
        Calendar.destroy_all
        @creator = FactoryGirl.create(:user)
        @creator.calendars << Calendar.new(
          :name=>"Test Cal",
          :description=>"This is a test",
          :start_at=>(Time.now),
          :end_at=>Time.now,
          :trigger_delete=>trigger_delete,
          :target_uid=>"2112"
        )
      end

      it "properly purges old 'delete' calendars" do
        @creator.reload
        @creator.calendars.should have(1).items

        JamRuby::DailyJob.perform
        @creator.reload
        @creator.calendars.should have(1).items

        Timecop.travel(Time.now + 5.weeks)
        JamRuby::DailyJob.perform
        @creator.reload
        @creator.calendars.should have(end_count).items
        Timecop.return
      end
    end

    describe "whacks old 'delete' calendars" do
      it_behaves_like :calendar_cleanup, true, 0
    end

    describe "doesn't whacks non 'delete' calendars" do
      it_behaves_like :calendar_cleanup, false, 1
    end
  end # calendar cleanpu
end #spec
