e0fa376d4f
Avoid infinite activity score orders Items with a zero activity score were getting an activity sorting score of -infinity, because we base the value on log2(score). Adjusting the calculation avoids this edge case, by always basing the sorting score on a non-zero number. --------- Co-authored-by: Kevin McConnell <kevin@37signals.com>
66 lines
1.9 KiB
Ruby
66 lines
1.9 KiB
Ruby
require "test_helper"
|
|
|
|
class Bubble::ScorableTest < ActiveSupport::TestCase
|
|
test "a bubble has a score that increases with activity" do
|
|
bubble = bubbles(:logo)
|
|
|
|
score = bubble.activity_score
|
|
assert_operator score, :>, 0
|
|
|
|
with_current_user :kevin do
|
|
bubble.capture Comment.create(body: "This is exciting!")
|
|
end
|
|
|
|
assert_operator bubble.activity_score, :>, score
|
|
end
|
|
|
|
test "commenting on a bubble boosts its score more than boosting it" do
|
|
bubble = bubbles(:logo)
|
|
bubble.rescore
|
|
|
|
comment_change = capture_change -> { bubble.activity_score } do
|
|
with_current_user :kevin do
|
|
bubble.capture Comment.create(body: "This is exciting!")
|
|
end
|
|
end
|
|
|
|
boost_change = capture_change -> { bubble.activity_score } do
|
|
with_current_user :kevin do
|
|
bubble.boost!
|
|
end
|
|
end
|
|
|
|
assert_operator comment_change, :>, boost_change
|
|
end
|
|
|
|
test "recent activity counts more than older activity in the ordering" do
|
|
with_current_user :kevin do
|
|
travel_to 5.days.ago
|
|
bubble_old = buckets(:writebook).bubbles.create! status: :published, title: "old"
|
|
bubble_mid = buckets(:writebook).bubbles.create! status: :published, title: "mid"
|
|
bubble_new = buckets(:writebook).bubbles.create! status: :published, title: "new"
|
|
|
|
bubble_old.boost!
|
|
bubble_old.boost!
|
|
|
|
travel_back
|
|
travel_to 2.days.ago
|
|
bubble_mid.boost!
|
|
|
|
travel_back
|
|
bubble_new.boost!
|
|
|
|
assert_equal [ bubble_new, bubble_mid, bubble_old ], Bubble.where(id: [ bubble_old, bubble_mid, bubble_new ]).ordered_by_activity
|
|
end
|
|
end
|
|
|
|
test "bubbles with no activity have a valid activity_score_order" do
|
|
bubble = Bubble.create! bucket: buckets(:writebook), creator: users(:kevin)
|
|
|
|
bubble.rescore
|
|
|
|
assert bubble.activity_score.zero?
|
|
assert_not bubble.activity_score_order.infinite?
|
|
end
|
|
end
|