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.

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