discourse/plugins/automation/spec/jobs/discourse_automation_tracker_spec.rb
Osama Sayegh 3d4faf3272
FEATURE: Merge discourse-automation (#26432)
Automation (previously known as discourse-automation) is now a core plugin.
2024-04-03 18:20:43 +03:00

106 lines
2.8 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
require_relative "../discourse_automation_helper"
describe Jobs::DiscourseAutomationTracker do
before { SiteSetting.discourse_automation_enabled = true }
describe "pending automation" do
fab!(:automation) do
Fabricate(
:automation,
script: "gift_exchange",
trigger: DiscourseAutomation::Triggers::POINT_IN_TIME,
)
end
before do
automation.upsert_field!(
"giftee_assignment_messages",
"pms",
{ value: [{ raw: "foo", title: "bar" }] },
target: "script",
)
automation.upsert_field!("gift_exchangers_group", "group", { value: 1 }, target: "script")
end
context "when pending automation is in past" do
before do
automation.upsert_field!(
"execute_at",
"date_time",
{ value: 2.hours.from_now },
target: "trigger",
)
end
it "consumes the pending automation" do
freeze_time 4.hours.from_now do
expect { Jobs::DiscourseAutomationTracker.new.execute }.to change {
automation.pending_automations.count
}.by(-1)
end
end
end
context "when pending automation is in future" do
before do
automation.upsert_field!(
"execute_at",
"date_time",
{ value: 2.hours.from_now },
target: "trigger",
)
end
it "doesnt consume the pending automation" do
expect { Jobs::DiscourseAutomationTracker.new.execute }.not_to change {
automation.pending_automations.count
}
end
end
end
describe "pending pms" do
before { Jobs.run_later! }
fab!(:automation) do
Fabricate(
:automation,
script: DiscourseAutomation::Scripts::SEND_PMS,
trigger: DiscourseAutomation::Triggers::TOPIC,
)
end
let!(:pending_pm) do
automation.pending_pms.create!(
title: "Il pleure dans mon cœur Comme il pleut sur la ville;",
raw: "Quelle est cette langueur Qui pénètre mon cœur ?",
sender: "system",
execute_at: Time.now,
target_usernames: ["system"],
)
end
context "when pending pm is in past" do
before { pending_pm.update!(execute_at: 2.hours.ago) }
it "consumes the pending pm" do
expect { Jobs::DiscourseAutomationTracker.new.execute }.to change {
automation.pending_pms.count
}.by(-1)
end
end
context "when pending pm is in future" do
before { pending_pm.update!(execute_at: 2.hours.from_now) }
it "doesnt consume the pending pm" do
expect { Jobs::DiscourseAutomationTracker.new.execute }.not_to change {
automation.pending_pms.count
}
end
end
end
end