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.

157 lines
5.3 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 'maintains case-insensitivity when combining keywords into a single matcher' do
  45. Glitch::KeywordMute.create!(account: alice, keyword: 'hot')
  46. Glitch::KeywordMute.create!(account: alice, keyword: 'cold')
  47. expect(matcher.matches?('This is a HOT take')).to be_truthy
  48. end
  49. it 'matches keywords surrounded by non-alphanumeric ornamentation' do
  50. Glitch::KeywordMute.create!(account: alice, keyword: 'hot')
  51. expect(matcher.matches?('(hot take)')).to be_truthy
  52. end
  53. it 'escapes metacharacters in keywords' do
  54. Glitch::KeywordMute.create!(account: alice, keyword: '(hot take)')
  55. expect(matcher.matches?('(hot take)')).to be_truthy
  56. end
  57. it 'uses case-folding rules appropriate for more than just English' do
  58. Glitch::KeywordMute.create!(account: alice, keyword: 'großeltern')
  59. expect(matcher.matches?('besuch der grosseltern')).to be_truthy
  60. end
  61. it 'matches keywords that are composed of multiple words' do
  62. Glitch::KeywordMute.create!(account: alice, keyword: 'a shiitake')
  63. expect(matcher.matches?('This is a shiitake')).to be_truthy
  64. expect(matcher.matches?('This is shiitake')).to_not be_truthy
  65. end
  66. end
  67. end
  68. describe '.tag_matcher_for' do
  69. let(:matcher) { Glitch::KeywordMute.tag_matcher_for(alice.id) }
  70. let(:status) { Fabricate(:status) }
  71. describe 'with no mutes' do
  72. before do
  73. Glitch::KeywordMute.delete_all
  74. end
  75. it 'does not match' do
  76. status.tags << Fabricate(:tag, name: 'xyzzy')
  77. expect(matcher.matches?(status.tags)).to be false
  78. end
  79. end
  80. describe 'with mutes' do
  81. it 'does not match keywords set by a different account' do
  82. status.tags << Fabricate(:tag, name: 'xyzzy')
  83. Glitch::KeywordMute.create!(account: bob, keyword: 'take')
  84. expect(matcher.matches?(status.tags)).to be false
  85. end
  86. it 'matches #xyzzy when given the mute "#xyzzy"' do
  87. status.tags << Fabricate(:tag, name: 'xyzzy')
  88. Glitch::KeywordMute.create!(account: alice, keyword: '#xyzzy')
  89. expect(matcher.matches?(status.tags)).to be true
  90. end
  91. it 'matches #thingiverse when given the non-whole-word mute "#thing"' do
  92. status.tags << Fabricate(:tag, name: 'thingiverse')
  93. Glitch::KeywordMute.create!(account: alice, keyword: '#thing', whole_word: false)
  94. expect(matcher.matches?(status.tags)).to be true
  95. end
  96. it 'matches #hashtag when given the mute "##hashtag""' do
  97. status.tags << Fabricate(:tag, name: 'hashtag')
  98. Glitch::KeywordMute.create!(account: alice, keyword: '##hashtag')
  99. expect(matcher.matches?(status.tags)).to be true
  100. end
  101. it 'matches #oatmeal when given the non-whole-word mute "oat"' do
  102. status.tags << Fabricate(:tag, name: 'oatmeal')
  103. Glitch::KeywordMute.create!(account: alice, keyword: 'oat', whole_word: false)
  104. expect(matcher.matches?(status.tags)).to be true
  105. end
  106. it 'does not match #oatmeal when given the mute "#oat"' do
  107. status.tags << Fabricate(:tag, name: 'oatmeal')
  108. Glitch::KeywordMute.create!(account: alice, keyword: 'oat')
  109. expect(matcher.matches?(status.tags)).to be false
  110. end
  111. end
  112. end
  113. end