tag_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Tag do
  4. describe 'validations' do
  5. it 'invalid with #' do
  6. expect(described_class.new(name: '#hello_world')).to_not be_valid
  7. end
  8. it 'invalid with .' do
  9. expect(described_class.new(name: '.abcdef123')).to_not be_valid
  10. end
  11. it 'invalid with spaces' do
  12. expect(described_class.new(name: 'hello world')).to_not be_valid
  13. end
  14. it 'valid with aesthetic' do
  15. expect(described_class.new(name: 'aesthetic')).to be_valid
  16. end
  17. end
  18. describe 'HASHTAG_RE' do
  19. subject { Tag::HASHTAG_RE }
  20. it 'does not match URLs with anchors with non-hashtag characters' do
  21. expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
  22. end
  23. it 'does not match URLs with hashtag-like anchors' do
  24. expect(subject.match('https://en.wikipedia.org/wiki/Ghostbusters_(song)#Lawsuit')).to be_nil
  25. end
  26. it 'does not match URLs with hashtag-like anchors after a numeral' do
  27. expect(subject.match('https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111895#c4')).to be_nil
  28. end
  29. it 'does not match URLs with hashtag-like anchors after an empty query parameter' do
  30. expect(subject.match('https://en.wikipedia.org/wiki/Ghostbusters_(song)?foo=#Lawsuit')).to be_nil
  31. end
  32. it 'matches #aesthetic' do
  33. expect(subject.match('this is #aesthetic').to_s).to eq '#aesthetic'
  34. end
  35. it 'matches digits at the start' do
  36. expect(subject.match('hello #3d').to_s).to eq '#3d'
  37. end
  38. it 'matches digits in the middle' do
  39. expect(subject.match('hello #l33ts35k').to_s).to eq '#l33ts35k'
  40. end
  41. it 'matches digits at the end' do
  42. expect(subject.match('hello #world2016').to_s).to eq '#world2016'
  43. end
  44. it 'matches underscores at the beginning' do
  45. expect(subject.match('hello #_test').to_s).to eq '#_test'
  46. end
  47. it 'matches underscores at the end' do
  48. expect(subject.match('hello #test_').to_s).to eq '#test_'
  49. end
  50. it 'matches underscores in the middle' do
  51. expect(subject.match('hello #one_two_three').to_s).to eq '#one_two_three'
  52. end
  53. it 'matches middle dots' do
  54. expect(subject.match('hello #one·two·three').to_s).to eq '#one·two·three'
  55. end
  56. it 'matches ・unicode in ぼっち・ざ・ろっく correctly' do
  57. expect(subject.match('testing #ぼっち・ざ・ろっく').to_s).to eq '#ぼっち・ざ・ろっく'
  58. end
  59. it 'matches ZWNJ' do
  60. expect(subject.match('just add #نرم‌افزار and').to_s).to eq '#نرم‌افزار'
  61. end
  62. it 'does not match middle dots at the start' do
  63. expect(subject.match('hello #·one·two·three')).to be_nil
  64. end
  65. it 'does not match middle dots at the end' do
  66. expect(subject.match('hello #one·two·three·').to_s).to eq '#one·two·three'
  67. end
  68. it 'does not match purely-numeric hashtags' do
  69. expect(subject.match('hello #0123456')).to be_nil
  70. end
  71. end
  72. describe '#to_param' do
  73. it 'returns name' do
  74. tag = Fabricate(:tag, name: 'foo')
  75. expect(tag.to_param).to eq 'foo'
  76. end
  77. end
  78. describe '.recently_used' do
  79. let(:account) { Fabricate(:account) }
  80. let(:other_person_status) { Fabricate(:status) }
  81. let(:out_of_range) { Fabricate(:status, account: account) }
  82. let(:older_in_range) { Fabricate(:status, account: account) }
  83. let(:newer_in_range) { Fabricate(:status, account: account) }
  84. let(:unused_tag) { Fabricate(:tag) }
  85. let(:used_tag_one) { Fabricate(:tag) }
  86. let(:used_tag_two) { Fabricate(:tag) }
  87. let(:used_tag_on_out_of_range) { Fabricate(:tag) }
  88. before do
  89. stub_const 'Tag::RECENT_STATUS_LIMIT', 2
  90. other_person_status.tags << used_tag_one
  91. out_of_range.tags << used_tag_on_out_of_range
  92. older_in_range.tags << used_tag_one
  93. older_in_range.tags << used_tag_two
  94. newer_in_range.tags << used_tag_one
  95. end
  96. it 'returns tags used by account within last X statuses ordered most used first' do
  97. results = described_class.recently_used(account)
  98. expect(results)
  99. .to eq([used_tag_one, used_tag_two])
  100. end
  101. end
  102. describe '.find_normalized' do
  103. it 'returns tag for a multibyte case-insensitive name' do
  104. upcase_string = 'abcABCabcABCやゆよ'
  105. downcase_string = 'abcabcabcabcやゆよ'
  106. tag = Fabricate(:tag, name: HashtagNormalizer.new.normalize(downcase_string))
  107. expect(described_class.find_normalized(upcase_string)).to eq tag
  108. end
  109. end
  110. describe '.matches_name' do
  111. it 'returns tags for multibyte case-insensitive names' do
  112. upcase_string = 'abcABCabcABCやゆよ'
  113. downcase_string = 'abcabcabcabcやゆよ'
  114. tag = Fabricate(:tag, name: HashtagNormalizer.new.normalize(downcase_string))
  115. expect(described_class.matches_name(upcase_string)).to eq [tag]
  116. end
  117. it 'uses the LIKE operator' do
  118. result = %q[SELECT "tags".* FROM "tags" WHERE LOWER("tags"."name") LIKE LOWER('100abc%')]
  119. expect(described_class.matches_name('100%abc').to_sql).to eq result
  120. end
  121. end
  122. describe '.matching_name' do
  123. it 'returns tags for multibyte case-insensitive names' do
  124. upcase_string = 'abcABCabcABCやゆよ'
  125. downcase_string = 'abcabcabcabcやゆよ'
  126. tag = Fabricate(:tag, name: HashtagNormalizer.new.normalize(downcase_string))
  127. expect(described_class.matching_name(upcase_string)).to eq [tag]
  128. end
  129. end
  130. describe '.find_or_create_by_names' do
  131. let(:upcase_string) { 'abcABCabcABCやゆよ' }
  132. let(:downcase_string) { 'abcabcabcabcやゆよ' }
  133. it 'runs a passed block once per tag regardless of duplicates' do
  134. count = 0
  135. described_class.find_or_create_by_names([upcase_string, downcase_string]) do |_tag|
  136. count += 1
  137. end
  138. expect(count).to eq 1
  139. end
  140. end
  141. describe '.search_for' do
  142. it 'finds tag records with matching names' do
  143. tag = Fabricate(:tag, name: 'match')
  144. _miss_tag = Fabricate(:tag, name: 'miss')
  145. results = described_class.search_for('match')
  146. expect(results).to eq [tag]
  147. end
  148. it 'finds tag records in case insensitive' do
  149. tag = Fabricate(:tag, name: 'MATCH')
  150. _miss_tag = Fabricate(:tag, name: 'miss')
  151. results = described_class.search_for('match')
  152. expect(results).to eq [tag]
  153. end
  154. it 'finds the exact matching tag as the first item' do
  155. similar_tag = Fabricate(:tag, name: 'matchlater', reviewed_at: Time.now.utc)
  156. tag = Fabricate(:tag, name: 'match', reviewed_at: Time.now.utc)
  157. results = described_class.search_for('match')
  158. expect(results).to eq [tag, similar_tag]
  159. end
  160. end
  161. end