0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-10 21:51:49 +08:00
discourse/plugins/automation/spec/triggers/stalled_wiki_spec.rb
Joffrey JAFFEUX 217f538ffd
FIX: prevents error if retriggered_after is not set (#37140)
This fix this current exception:

```
Job exception: undefined method `match' for nil

iso8601-0.13.0/lib/iso8601/duration.rb:225:in `parse'
iso8601-0.13.0/lib/iso8601/duration.rb:197:in `atomize'
iso8601-0.13.0/lib/iso8601/duration.rb:32:in `initialize'
/var/www/discourse/plugins/automation/app/jobs/scheduled/discourse_automation/stalled_wiki_tracker.rb:29:in `new'
/var/www/discourse/plugins/automation/app/jobs/scheduled/discourse_automation/stalled_wiki_tracker.rb:29:in `block (2 levels) in execute'
```
2026-01-15 12:19:19 +01:00

228 lines
6.8 KiB
Ruby
Vendored
Raw Permalink 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
describe "StalledWiki" do
fab!(:topic_1, :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")
I18n.backend.store_translations(
:en,
{
discourse_automation: {
scriptables: {
something_about_us: {
title: "Something about us.",
description: "We rock!",
},
},
},
},
)
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 "doesnt trigger" do
post.revise(
post_creator_1,
{ wiki: true },
{ force_new_version: true, revised_at: 40.minutes.ago },
)
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
expect(list.length).to eq(0)
end
end
context "when the post hasnt been revised recently" do
it "triggers" do
post.revise(
post_creator_1,
{ wiki: true },
{ force_new_version: true, revised_at: 1.month.ago },
)
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
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 "doesnt trigger" do
post.revise(
post_creator_1,
{ wiki: true },
{ force_new_version: true, revised_at: 40.minutes.ago },
)
list =
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
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 },
)
list =
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
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 "doesnt trigger" do
post.revise(
post_creator_1,
{ wiki: true },
{ force_new_version: true, revised_at: 40.minutes.ago },
)
list =
capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
expect(list).to be_empty
end
end
end
context "when trigger hasnt 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 },
)
Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil)
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)
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
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)
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
expect(list.length).to eq(0)
expect(post.reload.custom_fields["stalled_wiki_triggered_at"]).to eq(10.minutes.ago.to_s)
end
end
context "when retriggered_after is not set" do
before do
automation.upsert_field!(
"retriggered_after",
"choices",
{ value: nil },
target: "trigger",
)
end
it "doesn't trigger again for previously triggered posts" 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: 1.week.ago)
list = capture_contexts { Jobs::DiscourseAutomation::StalledWikiTracker.new.execute(nil) }
expect(list.length).to eq(0)
end
end
end
end
end