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.

171 lines
6.0 KiB

  1. require 'rails_helper'
  2. RSpec.describe Glitch::KeywordMute, type: :model do
  3. let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) }
  4. let(:bob) { Fabricate(:account, username: 'bob').tap(&:save!) }
  5. Unscoped = Glitch::KeywordMute::Scopes::Unscoped
  6. describe '.text_matcher_for' do
  7. let(:matcher) { Glitch::KeywordMute.text_matcher_for(alice.id) }
  8. describe 'with no mutes' do
  9. before do
  10. Glitch::KeywordMute.delete_all
  11. end
  12. it 'does not match' do
  13. expect(matcher.matches?('This is a hot take', Unscoped)).to be_falsy
  14. end
  15. end
  16. describe 'with mutes' do
  17. it 'does not match keywords set by a different account' do
  18. Glitch::KeywordMute.create!(account: bob, keyword: 'take')
  19. expect(matcher.matches?('This is a hot take', Unscoped)).to be_falsy
  20. end
  21. it 'does not match if no keywords match the status text' do
  22. Glitch::KeywordMute.create!(account: alice, keyword: 'cold')
  23. expect(matcher.matches?('This is a hot take', Unscoped)).to be_falsy
  24. end
  25. it 'considers word boundaries when matching' do
  26. Glitch::KeywordMute.create!(account: alice, keyword: 'bob', whole_word: true)
  27. expect(matcher.matches?('bobcats', Unscoped)).to be_falsy
  28. end
  29. it 'matches substrings if whole_word is false' do
  30. Glitch::KeywordMute.create!(account: alice, keyword: 'take', whole_word: false)
  31. expect(matcher.matches?('This is a shiitake mushroom', Unscoped)).to be_truthy
  32. end
  33. it 'matches keywords at the beginning of the text' do
  34. Glitch::KeywordMute.create!(account: alice, keyword: 'take')
  35. expect(matcher.matches?('Take this', Unscoped)).to be_truthy
  36. end
  37. it 'matches keywords at the end of the text' do
  38. Glitch::KeywordMute.create!(account: alice, keyword: 'take')
  39. expect(matcher.matches?('This is a hot take', Unscoped)).to be_truthy
  40. end
  41. it 'matches if at least one keyword case-insensitively matches the text' do
  42. Glitch::KeywordMute.create!(account: alice, keyword: 'hot')
  43. expect(matcher.matches?('This is a HOT take', Unscoped)).to be_truthy
  44. end
  45. it 'matches if at least one non-whole-word keyword case-insensitively matches the text' do
  46. Glitch::KeywordMute.create!(account: alice, keyword: 'hot', whole_word: false)
  47. expect(matcher.matches?('This is a HOTTY take', Unscoped)).to be_truthy
  48. end
  49. it 'maintains case-insensitivity when combining keywords into a single matcher' do
  50. Glitch::KeywordMute.create!(account: alice, keyword: 'hot')
  51. Glitch::KeywordMute.create!(account: alice, keyword: 'cold')
  52. expect(matcher.matches?('This is a HOT take', Unscoped)).to be_truthy
  53. end
  54. it 'matches keywords surrounded by non-alphanumeric ornamentation' do
  55. Glitch::KeywordMute.create!(account: alice, keyword: 'hot')
  56. expect(matcher.matches?('(hot take)', Unscoped)).to be_truthy
  57. end
  58. it 'escapes metacharacters in whole-word keywords' do
  59. Glitch::KeywordMute.create!(account: alice, keyword: '(hot take)')
  60. expect(matcher.matches?('(hot take)', Unscoped)).to be_truthy
  61. end
  62. it 'escapes metacharacters in non-whole-word keywords' do
  63. Glitch::KeywordMute.create!(account: alice, keyword: '(-', whole_word: false)
  64. expect(matcher.matches?('bad (-)', Unscoped)).to be_truthy
  65. end
  66. it 'uses case-folding rules appropriate for more than just English' do
  67. Glitch::KeywordMute.create!(account: alice, keyword: 'großeltern')
  68. expect(matcher.matches?('besuch der grosseltern', Unscoped)).to be_truthy
  69. end
  70. it 'matches keywords that are composed of multiple words' do
  71. Glitch::KeywordMute.create!(account: alice, keyword: 'a shiitake')
  72. expect(matcher.matches?('This is a shiitake', Unscoped)).to be_truthy
  73. expect(matcher.matches?('This is shiitake', Unscoped)).to_not be_truthy
  74. end
  75. end
  76. end
  77. describe '.tag_matcher_for' do
  78. let(:matcher) { Glitch::KeywordMute.tag_matcher_for(alice.id) }
  79. let(:status) { Fabricate(:status) }
  80. describe 'with no mutes' do
  81. before do
  82. Glitch::KeywordMute.delete_all
  83. end
  84. it 'does not match' do
  85. status.tags << Fabricate(:tag, name: 'xyzzy')
  86. expect(matcher.matches?(status.tags, Unscoped)).to be false
  87. end
  88. end
  89. describe 'with mutes' do
  90. it 'does not match keywords set by a different account' do
  91. status.tags << Fabricate(:tag, name: 'xyzzy')
  92. Glitch::KeywordMute.create!(account: bob, keyword: 'take')
  93. expect(matcher.matches?(status.tags, Unscoped)).to be false
  94. end
  95. it 'matches #xyzzy when given the mute "#xyzzy"' do
  96. status.tags << Fabricate(:tag, name: 'xyzzy')
  97. Glitch::KeywordMute.create!(account: alice, keyword: '#xyzzy')
  98. expect(matcher.matches?(status.tags, Unscoped)).to be true
  99. end
  100. it 'matches #thingiverse when given the non-whole-word mute "#thing"' do
  101. status.tags << Fabricate(:tag, name: 'thingiverse')
  102. Glitch::KeywordMute.create!(account: alice, keyword: '#thing', whole_word: false)
  103. expect(matcher.matches?(status.tags, Unscoped)).to be true
  104. end
  105. it 'matches #hashtag when given the mute "##hashtag""' do
  106. status.tags << Fabricate(:tag, name: 'hashtag')
  107. Glitch::KeywordMute.create!(account: alice, keyword: '##hashtag')
  108. expect(matcher.matches?(status.tags, Unscoped)).to be true
  109. end
  110. it 'matches #oatmeal when given the non-whole-word mute "oat"' do
  111. status.tags << Fabricate(:tag, name: 'oatmeal')
  112. Glitch::KeywordMute.create!(account: alice, keyword: 'oat', whole_word: false)
  113. expect(matcher.matches?(status.tags, Unscoped)).to be true
  114. end
  115. it 'does not match #oatmeal when given the mute "#oat"' do
  116. status.tags << Fabricate(:tag, name: 'oatmeal')
  117. Glitch::KeywordMute.create!(account: alice, keyword: 'oat')
  118. expect(matcher.matches?(status.tags, Unscoped)).to be false
  119. end
  120. end
  121. end
  122. end