闭社主体 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.

104 lines
3.4 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 no domain' do
  23. before do
  24. allow(Account).to receive(:find_remote)
  25. allow(Account).to receive(:search_for)
  26. subject.call('one', 10)
  27. end
  28. it 'uses find_remote with nil domain to look for local accounts' do
  29. expect(Account).to have_received(:find_remote).with('one', nil)
  30. end
  31. it 'uses search_for to find matches' do
  32. expect(Account).to have_received(:search_for).with('one', 10)
  33. end
  34. end
  35. describe 'when there is a domain' do
  36. before do
  37. allow(Account).to receive(:find_remote)
  38. end
  39. it 'uses find_remote to look for remote accounts' do
  40. subject.call('two@example.com', 10)
  41. expect(Account).to have_received(:find_remote).with('two', 'example.com')
  42. end
  43. describe 'and there is no account provided' do
  44. it 'uses search_for to find matches' do
  45. allow(Account).to receive(:search_for)
  46. subject.call('two@example.com', 10, false, nil)
  47. expect(Account).to have_received(:search_for).with('two example.com', 10)
  48. end
  49. end
  50. describe 'and there is an account provided' do
  51. it 'uses advanced_search_for to find matches' do
  52. account = Fabricate(:account)
  53. allow(Account).to receive(:advanced_search_for)
  54. subject.call('two@example.com', 10, false, account)
  55. expect(Account).to have_received(:advanced_search_for).with('two example.com', account, 10)
  56. end
  57. end
  58. end
  59. end
  60. describe 'with an exact match' do
  61. it 'returns exact match first, and does not return duplicates' do
  62. partial = Fabricate(:account, username: 'exactness')
  63. exact = Fabricate(:account, username: 'exact')
  64. results = subject.call('exact', 10)
  65. expect(results.size).to eq 2
  66. expect(results).to eq [exact, partial]
  67. end
  68. end
  69. describe 'when there is a domain but no exact match' do
  70. it 'follows the remote account when resolve is true' do
  71. service = double(call: nil)
  72. allow(FollowRemoteAccountService).to receive(:new).and_return(service)
  73. results = subject.call('newuser@remote.com', 10, true)
  74. expect(service).to have_received(:call).with('newuser@remote.com')
  75. end
  76. it 'does not follow the remote account when resolve is false' do
  77. service = double(call: nil)
  78. allow(FollowRemoteAccountService).to receive(:new).and_return(service)
  79. results = subject.call('newuser@remote.com', 10, false)
  80. expect(service).not_to have_received(:call)
  81. end
  82. end
  83. end
  84. end