2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-04 01:15:08 +08:00
discourse/spec/support/problem_check_matcher.rb
Ted Johansson d446cc7318
FIX: Fix scheduled targeted problem checks (#35696)
Scheduled problem checks with multiple targets are not honouring the
`run_every` configuration.

For checks with multiple targets, all targets are checked in a single
instance of the problem check. However, we have one problem check
tracker per target.

This mismatch results in the `#ready_to_run?` method always creating a
tracker with no target when being checked.

This commit fixes that by:

**Expect checks to operate on a single target.**

This change makes it so that instances of a `ProblemCheck` class are
initialized with a target. So instead of 1-N we now have an N-N
relationship between checks and trackers.

Each instance can access their `target` through an attribute of the same
name.

This also means problem checks are back to returning a singular
`Problem` or `nil`, instead of `[Problem]` or `[]`.

For scheduled checks, this means that `ScheduleProblemChecks` now
enqueues `N` jobs (where `N` is the number of targets) per check instead
of `1` job per check.

**Update existing targeted checks to operate on a single target.**

This is essentially just removing the loop inside the check.
2025-11-10 10:09:14 +08:00

52 lines
1.5 KiB
Ruby

# frozen_string_literal: true
RSpec::Matchers.define :be_chill_about_it do
match { |service| expect(service.call).to be_blank }
end
RSpec::Matchers.define :have_a_problem do
chain :with_message do |message|
@message = message
end
chain :with_priority do |priority|
@priority = priority
end
chain :with_target do |target|
@target = target
end
match do |service|
@result = service.call
aggregate_failures do
expect(@result).to be_a(ProblemCheck::Problem)
expect(@result.priority).to(eq(@priority.to_s)) if @priority.present?
expect(@result.message).to(eq(@message)) if @message.present?
expect(@result.target).to(eq(@target)) if @target.present?
end
end
failure_message do |service|
if @result.blank?
"Expected check to have a problem, but it was chill about it."
elsif !@result.is_a?(ProblemCheck::Problem)
"Expected result to must be an instance of `Problem`."
elsif @priority.present? && @result.priority != @priority
"Expected problem to have priority `#{@priority}`, but got priority `#{@result.priority}`."
elsif @message.present? && @result.message != @message
<<~MESSAGE
Expected problem to have message:
> #{@message}
but got message:
> #{@result.message}
MESSAGE
elsif @target.present? && @result.target != @target
"Expected problem to have target `#{@target}`, but got target `#{@result.target}`."
end
end
end