discourse/spec/jobs/reviewable_priorities_spec.rb
Roman Rizzi d4b5a81b05
FIX: Recalculate scores only when approving or transitioning to pending. (#13009)
Recalculating a ReviewableFlaggedPost's score after rejecting or ignoring it sets the score as 0, which means that we can't find them after reviewing. They don't surpass the minimum priority threshold and are hidden.

Additionally, we only want to use agreed flags when calculating the different priority thresholds.
2021-05-10 14:09:04 -03:00

84 lines
2.7 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Jobs::ReviewablePriorities do
it "needs returns 0s with no existing reviewables" do
Jobs::ReviewablePriorities.new.execute({})
expect_min_score(:low, 0.0)
expect_min_score(:medium, 0.0)
expect_min_score(:high, 0.0)
expect(Reviewable.score_required_to_hide_post).to eq(8.33)
end
fab!(:user_0) { Fabricate(:user) }
fab!(:user_1) { Fabricate(:user) }
def create_reviewables(count, status: :approved)
minimum_threshold = SiteSetting.reviewable_low_priority_threshold
(1..count).each { |i| create_with_score(minimum_threshold + i) }
end
def create_with_score(score, status: :approved)
Fabricate(:reviewable_flagged_post, status: Reviewable.statuses[status]).tap do |reviewable|
reviewable.add_score(user_0, PostActionType.types[:off_topic])
reviewable.add_score(user_1, PostActionType.types[:off_topic])
reviewable.update!(score: score)
end
end
it "needs a minimum amount of reviewables before it calculates anything" do
create_reviewables(5)
Jobs::ReviewablePriorities.new.execute({})
expect_min_score(:low, 0.0)
expect_min_score(:medium, 0.0)
expect_min_score(:high, 0.0)
expect(Reviewable.score_required_to_hide_post).to eq(8.33)
end
it "will set priorities based on the maximum score" do
create_reviewables(Jobs::ReviewablePriorities.min_reviewables)
Jobs::ReviewablePriorities.new.execute({})
expect_min_score(:low, SiteSetting.reviewable_low_priority_threshold)
expect_min_score(:medium, 9.0)
expect_min_score(:high, 14.0)
expect(Reviewable.score_required_to_hide_post).to eq(9.33)
end
it 'ignore negative scores when calculating priorities' do
create_reviewables(Jobs::ReviewablePriorities.min_reviewables)
negative_score = -9
10.times { create_with_score(negative_score) }
Jobs::ReviewablePriorities.new.execute({})
expect_min_score(:low, SiteSetting.reviewable_low_priority_threshold)
expect_min_score(:medium, 9.0)
expect_min_score(:high, 14.0)
expect(Reviewable.score_required_to_hide_post).to eq(9.33)
end
it 'ignores non-approved reviewables' do
create_reviewables(Jobs::ReviewablePriorities.min_reviewables)
low_score = 2
10.times { create_with_score(low_score, status: :pending) }
Jobs::ReviewablePriorities.new.execute({})
expect_min_score(:low, SiteSetting.reviewable_low_priority_threshold)
expect_min_score(:medium, 9.0)
expect_min_score(:high, 14.0)
expect(Reviewable.score_required_to_hide_post).to eq(9.33)
end
def expect_min_score(priority, score)
expect(Reviewable.min_score_for_priority(priority)).to eq(score)
end
end