闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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