friends_of_friends_source_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe AccountSuggestions::FriendsOfFriendsSource do
  4. describe '#get' do
  5. subject { described_class.new }
  6. let!(:bob) { Fabricate(:account, discoverable: true, hide_collections: false) }
  7. let!(:alice) { Fabricate(:account, discoverable: true, hide_collections: true) }
  8. let!(:eve) { Fabricate(:account, discoverable: true, hide_collections: false) }
  9. let!(:mallory) { Fabricate(:account, discoverable: false, hide_collections: false) }
  10. let!(:eugen) { Fabricate(:account, discoverable: true, hide_collections: false) }
  11. let!(:neil) { Fabricate(:account, discoverable: true, hide_collections: false) }
  12. let!(:john) { Fabricate(:account, discoverable: true, hide_collections: false) }
  13. let!(:jerk) { Fabricate(:account, discoverable: true, hide_collections: false) }
  14. let!(:larry) { Fabricate(:account, discoverable: true, hide_collections: false) }
  15. let!(:morty) { Fabricate(:account, discoverable: true, hide_collections: false, memorial: true) }
  16. context 'with follows and blocks' do
  17. before do
  18. bob.block!(jerk)
  19. FollowRecommendationMute.create!(account: bob, target_account: neil)
  20. # bob follows eugen, alice and larry
  21. [eugen, alice, larry].each { |account| bob.follow!(account) }
  22. # alice follows eve and mallory
  23. [john, mallory].each { |account| alice.follow!(account) }
  24. # eugen follows eve, john, jerk, larry, neil and morty
  25. [eve, mallory, jerk, larry, neil, morty].each { |account| eugen.follow!(account) }
  26. end
  27. it 'returns eligible accounts', :aggregate_failures do
  28. results = subject.get(bob)
  29. # eve is returned through eugen
  30. expect(results).to include([eve.id, :friends_of_friends])
  31. # john is not reachable because alice hides who she follows
  32. expect(results).to_not include([john.id, :friends_of_friends])
  33. # mallory is not discoverable
  34. expect(results).to_not include([mallory.id, :friends_of_friends])
  35. # larry is not included because he's followed already
  36. expect(results).to_not include([larry.id, :friends_of_friends])
  37. # jerk is blocked
  38. expect(results).to_not include([jerk.id, :friends_of_friends])
  39. # the suggestion for neil has already been rejected
  40. expect(results).to_not include([neil.id, :friends_of_friends])
  41. # morty is not included because his account is in memoriam
  42. expect(results).to_not include([morty.id, :friends_of_friends])
  43. end
  44. end
  45. context 'with deterministic order' do
  46. before do
  47. # bob follows eve and mallory
  48. [eve, mallory].each { |account| bob.follow!(account) }
  49. # eve follows eugen, john, and jerk
  50. [jerk, eugen, john].each { |account| eve.follow!(account) }
  51. # mallory follows eugen, john, and neil
  52. [neil, eugen, john].each { |account| mallory.follow!(account) }
  53. john.follow!(eugen)
  54. john.follow!(neil)
  55. end
  56. it 'returns eligible accounts in the expected order' do
  57. expect(subject.get(bob)).to eq expected_results
  58. end
  59. it 'contains correct underlying source data' do
  60. expect(source_query_values)
  61. .to contain_exactly(
  62. [john.id, 2, 2], # Followed by 2 friends of bob (eve, mallory), 2 followers total (breaks tie)
  63. [eugen.id, 2, 3], # Followed by 2 friends of bob (eve, mallory), 3 followers total
  64. [jerk.id, 1, 1], # Followed by 1 friends of bob (eve), 1 followers total (breaks tie)
  65. [neil.id, 1, 2] # Followed by 1 friends of bob (mallory), 2 followers total
  66. )
  67. end
  68. def expected_results
  69. [
  70. [john.id, :friends_of_friends],
  71. [eugen.id, :friends_of_friends],
  72. [jerk.id, :friends_of_friends],
  73. [neil.id, :friends_of_friends],
  74. ]
  75. end
  76. def source_query_values
  77. subject.source_query(bob).to_a
  78. end
  79. end
  80. end
  81. end