You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

  1. require 'rails_helper'
  2. RSpec.describe TrendingTags do
  3. describe '.record_use!' do
  4. pending
  5. end
  6. describe '.update!' do
  7. let!(:at_time) { Time.now.utc }
  8. let!(:tag1) { Fabricate(:tag, name: 'Catstodon') }
  9. let!(:tag2) { Fabricate(:tag, name: 'DogsOfMastodon') }
  10. let!(:tag3) { Fabricate(:tag, name: 'OCs') }
  11. before do
  12. allow(Redis.current).to receive(:pfcount) do |key|
  13. case key
  14. when "activity:tags:#{tag1.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
  15. 2
  16. when "activity:tags:#{tag1.id}:#{at_time.beginning_of_day.to_i}:accounts"
  17. 16
  18. when "activity:tags:#{tag2.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
  19. 0
  20. when "activity:tags:#{tag2.id}:#{at_time.beginning_of_day.to_i}:accounts"
  21. 4
  22. when "activity:tags:#{tag3.id}:#{(at_time - 1.day).beginning_of_day.to_i}:accounts"
  23. 13
  24. end
  25. end
  26. Redis.current.zadd('trending_tags', 0.9, tag3.id)
  27. Redis.current.sadd("trending_tags:used:#{at_time.beginning_of_day.to_i}", [tag1.id, tag2.id])
  28. tag3.update(max_score: 0.9, max_score_at: (at_time - 1.day).beginning_of_day + 12.hours)
  29. described_class.update!(at_time)
  30. end
  31. it 'calculates and re-calculates scores' do
  32. expect(described_class.get(10, filtered: false)).to eq [tag1, tag3]
  33. end
  34. it 'omits hashtags below threshold' do
  35. expect(described_class.get(10, filtered: false)).to_not include(tag2)
  36. end
  37. it 'decays scores' do
  38. expect(Redis.current.zscore('trending_tags', tag3.id)).to be < 0.9
  39. end
  40. end
  41. describe '.trending?' do
  42. let(:tag) { Fabricate(:tag) }
  43. before do
  44. 10.times { |i| Redis.current.zadd('trending_tags', i + 1, Fabricate(:tag).id) }
  45. end
  46. it 'returns true if the hashtag is within limit' do
  47. Redis.current.zadd('trending_tags', 11, tag.id)
  48. expect(described_class.trending?(tag)).to be true
  49. end
  50. it 'returns false if the hashtag is outside the limit' do
  51. Redis.current.zadd('trending_tags', 0, tag.id)
  52. expect(described_class.trending?(tag)).to be false
  53. end
  54. end
  55. end