2024-04-03 23:20:43 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
describe "StalledWiki" do
|
|
|
|
|
fab!(:topic_1) { Fabricate(:topic) }
|
|
|
|
|
fab!(:automation) { Fabricate(:automation, trigger: DiscourseAutomation::Triggers::STALLED_WIKI) }
|
|
|
|
|
fab!(:post_creator_1) { Fabricate(:user, admin: true) }
|
|
|
|
|
let!(:post) do
|
|
|
|
|
post_creator =
|
|
|
|
|
PostCreator.new(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
topic_id: topic_1.id,
|
|
|
|
|
raw: "this is a post that will become a wiki",
|
|
|
|
|
)
|
|
|
|
|
post_creator.create
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "allows manual trigger" do
|
|
|
|
|
triggerable = DiscourseAutomation::Triggerable.new(automation.trigger)
|
|
|
|
|
expect(triggerable.settings[DiscourseAutomation::Triggerable::MANUAL_TRIGGER_KEY]).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "default" do
|
|
|
|
|
before do
|
|
|
|
|
automation.upsert_field!("stalled_after", "choices", { value: "PT10H" }, target: "trigger")
|
|
|
|
|
automation.upsert_field!("retriggered_after", "choices", { value: "PT1H" }, target: "trigger")
|
2024-05-24 22:15:53 +08:00
|
|
|
|
I18n.backend.store_translations(
|
|
|
|
|
:en,
|
|
|
|
|
{
|
|
|
|
|
discourse_automation: {
|
|
|
|
|
scriptables: {
|
|
|
|
|
something_about_us: {
|
|
|
|
|
title: "Something about us.",
|
|
|
|
|
description: "We rock!",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-04-03 23:20:43 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "supports manual triggering" do
|
|
|
|
|
DiscourseAutomation::AutomationSerializer.new(automation, root: "automation").as_json
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the post has been revised recently" do
|
|
|
|
|
it "doesn’t trigger" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 40.minutes.ago },
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list.length).to eq(0)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the post hasn’t been revised recently" do
|
|
|
|
|
it "triggers" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 1.month.ago },
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list.length).to eq(1)
|
|
|
|
|
expect(list[0]["kind"]).to eq("stalled_wiki")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when trigger has a category" do
|
|
|
|
|
fab!(:category)
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
automation.upsert_field!(
|
|
|
|
|
"stalled_after",
|
|
|
|
|
"choices",
|
|
|
|
|
{ value: "PT10H" },
|
|
|
|
|
target: "trigger",
|
|
|
|
|
)
|
|
|
|
|
automation.upsert_field!(
|
|
|
|
|
"retriggered_after",
|
|
|
|
|
"choices",
|
|
|
|
|
{ value: "PT1H" },
|
|
|
|
|
target: "trigger",
|
|
|
|
|
)
|
|
|
|
|
automation.upsert_field!(
|
|
|
|
|
"restricted_category",
|
|
|
|
|
"category",
|
|
|
|
|
{ value: category.id },
|
|
|
|
|
target: "trigger",
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the post is in a sub category" do
|
|
|
|
|
before do
|
|
|
|
|
subcategory = Fabricate(:category, parent_category: category)
|
|
|
|
|
subcategory.save!
|
|
|
|
|
|
|
|
|
|
post.topic.update(category: subcategory)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "doesn’t trigger" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 40.minutes.ago },
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list =
|
|
|
|
|
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list).to be_empty
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the post is in this category" do
|
|
|
|
|
before { post.topic.update(category: category) }
|
|
|
|
|
|
|
|
|
|
it "triggers" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 1.month.ago },
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list =
|
|
|
|
|
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list.length).to eq(1)
|
|
|
|
|
expect(list[0]["kind"]).to eq("stalled_wiki")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the post is not in this category" do
|
|
|
|
|
it "doesn’t trigger" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 40.minutes.ago },
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list =
|
|
|
|
|
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list).to be_empty
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when trigger hasn’t been running recently" do
|
|
|
|
|
before { freeze_time 2.hours.from_now }
|
|
|
|
|
|
|
|
|
|
it "sets custom field" do
|
|
|
|
|
expect(post.reload.custom_fields["stalled_wiki_triggered_at"]).to eq(nil)
|
|
|
|
|
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 1.month.ago },
|
|
|
|
|
)
|
2024-05-07 04:12:55 +08:00
|
|
|
|
Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil)
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(post.reload.custom_fields["stalled_wiki_triggered_at"]).to eq(Time.zone.now.to_s)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "triggers again" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 2.months.ago },
|
|
|
|
|
)
|
|
|
|
|
post.upsert_custom_fields(stalled_wiki_triggered_at: 2.months.ago)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list.length).to eq(1)
|
|
|
|
|
expect(list[0]["kind"]).to eq("stalled_wiki")
|
|
|
|
|
expect(post.reload.custom_fields["stalled_wiki_triggered_at"]).to eq(Time.zone.now.to_s)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when trigger has been running recently" do
|
|
|
|
|
before { freeze_time 2.hours.from_now }
|
|
|
|
|
|
|
|
|
|
it "doesn’t trigger again" do
|
|
|
|
|
post.revise(
|
|
|
|
|
post_creator_1,
|
|
|
|
|
{ wiki: true },
|
|
|
|
|
{ force_new_version: true, revised_at: 1.month.ago },
|
|
|
|
|
)
|
|
|
|
|
post.upsert_custom_fields(stalled_wiki_triggered_at: 10.minutes.ago)
|
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
|
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
|
|
expect(list.length).to eq(0)
|
|
|
|
|
expect(post.reload.custom_fields["stalled_wiki_triggered_at"]).to eq(10.minutes.ago.to_s)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|