|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | +require "bas/orchestrator/manager" |
| 5 | + |
| 6 | +RSpec.describe Bas::Orchestrator::Manager do |
| 7 | + let(:schedules) do |
| 8 | + [ |
| 9 | + { path: "websites_availability/fetch_domain_services_from_notion.rb", interval: 600_000 }, |
| 10 | + { path: "websites_availability/notify_domain_availability.rb", interval: 60_000 }, |
| 11 | + { path: "websites_availability/garbage_collector.rb", time: ["00:00"] }, |
| 12 | + { path: "pto_next_week/fetch_next_week_pto_from_notion.rb", time: ["12:40"], day: ["Monday"] }, |
| 13 | + { path: "pto/fetch_pto_from_notion.rb", time: ["13:10"] } |
| 14 | + ] |
| 15 | + end |
| 16 | + |
| 17 | + let(:manager) { described_class.new(schedules) } |
| 18 | + |
| 19 | + before do |
| 20 | + allow(manager).to receive(:current_time).and_return("12:40") |
| 21 | + allow(manager).to receive(:current_day).and_return("Monday") |
| 22 | + allow(manager).to receive(:time_in_milliseconds).and_return(10_000) |
| 23 | + allow(manager).to receive(:system).and_return(true) |
| 24 | + end |
| 25 | + |
| 26 | + describe "#execute_interval" do |
| 27 | + it "executes scripts when interval has elapsed" do |
| 28 | + script = schedules[0] |
| 29 | + manager.instance_variable_set(:@last_executions, { script[:path] => 0 }) |
| 30 | + allow(manager).to receive(:time_in_milliseconds).and_return(600_000) |
| 31 | + |
| 32 | + expect { manager.send(:execute_interval, script) }.to(change do |
| 33 | + manager.instance_variable_get(:@last_executions)[script[:path]] |
| 34 | + end) |
| 35 | + end |
| 36 | + |
| 37 | + it "does not execute script if interval has not elapsed" do |
| 38 | + script = schedules[0] |
| 39 | + manager.instance_variable_set(:@last_executions, { script[:path] => 0 }) |
| 40 | + allow(manager).to receive(:time_in_milliseconds).and_return(10_000) |
| 41 | + |
| 42 | + expect { manager.send(:execute_interval, script) }.not_to(change do |
| 43 | + manager.instance_variable_get(:@last_executions)[script[:path]] |
| 44 | + end) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe "#execute_time" do |
| 49 | + it "executes scripts at exact time" do |
| 50 | + script = schedules[2] |
| 51 | + allow(manager).to receive(:current_time).and_return("00:00") |
| 52 | + |
| 53 | + expect { manager.send(:execute_time, script) }.to(change do |
| 54 | + manager.instance_variable_get(:@last_executions)[script[:path]] |
| 55 | + end) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe "#execute_day" do |
| 60 | + it "executes scripts at specific time and day" do |
| 61 | + script = schedules[3] |
| 62 | + allow(manager).to receive(:current_time).and_return("12:40") |
| 63 | + allow(manager).to receive(:current_day).and_return("Monday") |
| 64 | + |
| 65 | + expect { manager.send(:execute_day, script) }.to(change do |
| 66 | + manager.instance_variable_get(:@last_executions)[script[:path]] |
| 67 | + end) |
| 68 | + end |
| 69 | + |
| 70 | + it "does not execute script if time is correct but the day is incorrect" do |
| 71 | + script = schedules[3] |
| 72 | + allow(manager).to receive(:current_time).and_return("12:40") |
| 73 | + allow(manager).to receive(:current_day).and_return("Tuesday") |
| 74 | + |
| 75 | + expect { manager.send(:execute_day, script) }.not_to(change do |
| 76 | + manager.instance_variable_get(:@last_executions)[script[:path]] |
| 77 | + end) |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments