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.

116 lines
3.7 KiB

  1. require 'rails_helper'
  2. describe AccountSearchService do
  3. describe '.call' do
  4. describe 'with a query to ignore' do
  5. it 'returns empty array for missing query' do
  6. results = subject.call('', 10)
  7. expect(results).to eq []
  8. end
  9. it 'returns empty array for hashtag query' do
  10. results = subject.call('#tag', 10)
  11. expect(results).to eq []
  12. end
  13. end
  14. describe 'searching for a simple term that is not an exact match' do
  15. it 'does not return a nil entry in the array for the exact match' do
  16. match = Fabricate(:account, username: 'matchingusername')
  17. results = subject.call('match', 5)
  18. expect(results).to eq [match]
  19. end
  20. end
  21. describe 'searching local and remote users' do
  22. describe "when only '@'" do
  23. before do
  24. allow(Account).to receive(:find_remote)
  25. allow(Account).to receive(:search_for)
  26. subject.call('@', 10)
  27. end
  28. it 'uses find_remote with empty query to look for local accounts' do
  29. expect(Account).to have_received(:find_remote).with('', nil)
  30. end
  31. end
  32. describe 'when no domain' do
  33. before do
  34. allow(Account).to receive(:find_remote)
  35. allow(Account).to receive(:search_for)
  36. subject.call('one', 10)
  37. end
  38. it 'uses find_remote with nil domain to look for local accounts' do
  39. expect(Account).to have_received(:find_remote).with('one', nil)
  40. end
  41. it 'uses search_for to find matches' do
  42. expect(Account).to have_received(:search_for).with('one', 10)
  43. end
  44. end
  45. describe 'when there is a domain' do
  46. before do
  47. allow(Account).to receive(:find_remote)
  48. end
  49. it 'uses find_remote to look for remote accounts' do
  50. subject.call('two@example.com', 10)
  51. expect(Account).to have_received(:find_remote).with('two', 'example.com')
  52. end
  53. describe 'and there is no account provided' do
  54. it 'uses search_for to find matches' do
  55. allow(Account).to receive(:search_for)
  56. subject.call('two@example.com', 10, false, nil)
  57. expect(Account).to have_received(:search_for).with('two example.com', 10)
  58. end
  59. end
  60. describe 'and there is an account provided' do
  61. it 'uses advanced_search_for to find matches' do
  62. account = Fabricate(:account)
  63. allow(Account).to receive(:advanced_search_for)
  64. subject.call('two@example.com', 10, false, account)
  65. expect(Account).to have_received(:advanced_search_for).with('two example.com', account, 10)
  66. end
  67. end
  68. end
  69. end
  70. describe 'with an exact match' do
  71. it 'returns exact match first, and does not return duplicates' do
  72. partial = Fabricate(:account, username: 'exactness')
  73. exact = Fabricate(:account, username: 'exact')
  74. results = subject.call('exact', 10)
  75. expect(results.size).to eq 2
  76. expect(results).to eq [exact, partial]
  77. end
  78. end
  79. describe 'when there is a domain but no exact match' do
  80. it 'follows the remote account when resolve is true' do
  81. service = double(call: nil)
  82. allow(FollowRemoteAccountService).to receive(:new).and_return(service)
  83. results = subject.call('newuser@remote.com', 10, true)
  84. expect(service).to have_received(:call).with('newuser@remote.com')
  85. end
  86. it 'does not follow the remote account when resolve is false' do
  87. service = double(call: nil)
  88. allow(FollowRemoteAccountService).to receive(:new).and_return(service)
  89. results = subject.call('newuser@remote.com', 10, false)
  90. expect(service).not_to have_received(:call)
  91. end
  92. end
  93. end
  94. end