|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | + |
| 5 | +RSpec.describe DiscourseAi::AiModeration::SpamScanner do |
| 6 | + fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) } |
| 7 | + fab!(:topic) { Fabricate(:topic) } |
| 8 | + fab!(:post) { Fabricate(:post, user: user, topic: topic) } |
| 9 | + fab!(:llm_model) { Fabricate(:llm_model) } |
| 10 | + fab!(:spam_setting) do |
| 11 | + AiModerationSetting.create!( |
| 12 | + setting_type: :spam, |
| 13 | + llm_model: llm_model, |
| 14 | + data: { custom_instructions: "test instructions" } |
| 15 | + ) |
| 16 | + end |
| 17 | + |
| 18 | + before do |
| 19 | + SiteSetting.discourse_ai_enabled = true |
| 20 | + SiteSetting.ai_spam_detection_enabled = true |
| 21 | + end |
| 22 | + |
| 23 | + describe ".enabled?" do |
| 24 | + it "returns true when both settings are enabled" do |
| 25 | + expect(described_class.enabled?).to eq(true) |
| 26 | + end |
| 27 | + |
| 28 | + it "returns false when discourse_ai is disabled" do |
| 29 | + SiteSetting.discourse_ai_enabled = false |
| 30 | + expect(described_class.enabled?).to eq(false) |
| 31 | + end |
| 32 | + |
| 33 | + it "returns false when spam detection is disabled" do |
| 34 | + SiteSetting.ai_spam_detection_enabled = false |
| 35 | + expect(described_class.enabled?).to eq(false) |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + describe ".should_scan_post?" do |
| 40 | + it "returns true for new users' posts" do |
| 41 | + expect(described_class.should_scan_post?(post)).to eq(true) |
| 42 | + end |
| 43 | + |
| 44 | + it "returns false for trusted users" do |
| 45 | + post.user.trust_level = TrustLevel[2] |
| 46 | + expect(described_class.should_scan_post?(post)).to eq(false) |
| 47 | + end |
| 48 | + |
| 49 | + it "returns false for users with many public posts" do |
| 50 | + Fabricate(:post, user: user, topic: topic) |
| 51 | + Fabricate(:post, user: user, topic: topic) |
| 52 | + expect(described_class.should_scan_post?(post)).to eq(true) |
| 53 | + |
| 54 | + pm = Fabricate(:private_message_topic, user: user) |
| 55 | + Fabricate(:post, user: user, topic: pm) |
| 56 | + |
| 57 | + expect(described_class.should_scan_post?(post)).to eq(true) |
| 58 | + |
| 59 | + topic = Fabricate(:topic, user: user) |
| 60 | + Fabricate(:post, user: user, topic: topic) |
| 61 | + |
| 62 | + expect(described_class.should_scan_post?(post)).to eq(false) |
| 63 | + end |
| 64 | + |
| 65 | + it "returns false for private messages" do |
| 66 | + pm_topic = Fabricate(:private_message_topic) |
| 67 | + pm_post = Fabricate(:post, topic: pm_topic, user: user) |
| 68 | + expect(described_class.should_scan_post?(pm_post)).to eq(false) |
| 69 | + end |
| 70 | + |
| 71 | + it "returns false for nil posts" do |
| 72 | + expect(described_class.should_scan_post?(nil)).to eq(false) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe ".scanned_max_times?" do |
| 77 | + it "returns true when post has been scanned 3 times" do |
| 78 | + 3.times do |
| 79 | + AiSpamLog.create!( |
| 80 | + post: post, |
| 81 | + llm_model: llm_model, |
| 82 | + ai_api_audit_log_id: 1, |
| 83 | + is_spam: false |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + expect(described_class.scanned_max_times?(post)).to eq(true) |
| 88 | + end |
| 89 | + |
| 90 | + it "returns false for posts scanned less than 3 times" do |
| 91 | + expect(described_class.scanned_max_times?(post)).to eq(false) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + describe ".significant_change?" do |
| 96 | + it "returns true for first edits" do |
| 97 | + expect(described_class.significant_change?(nil, "new content")).to eq(true) |
| 98 | + end |
| 99 | + |
| 100 | + it "returns true for significant changes" do |
| 101 | + old_version = "This is a test post" |
| 102 | + new_version = "This is a completely different post with new content" |
| 103 | + expect(described_class.significant_change?(old_version, new_version)).to eq(true) |
| 104 | + end |
| 105 | + |
| 106 | + it "returns false for minor changes" do |
| 107 | + old_version = "This is a test post" |
| 108 | + new_version = "This is a test Post" # Only capitalization change |
| 109 | + expect(described_class.significant_change?(old_version, new_version)).to eq(false) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + describe ".new_post" do |
| 114 | + it "enqueues spam scan job for eligible posts" do |
| 115 | + Jobs.expects(:enqueue).with(:ai_spam_scan, post_id: post.id) |
| 116 | + described_class.new_post(post) |
| 117 | + end |
| 118 | + |
| 119 | + it "doesn't enqueue jobs when disabled" do |
| 120 | + SiteSetting.ai_spam_detection_enabled = false |
| 121 | + Jobs.expects(:enqueue).never |
| 122 | + described_class.new_post(post) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + describe ".edited_post" do |
| 127 | + it "enqueues spam scan job for eligible edited posts" do |
| 128 | + PostRevision.create!( |
| 129 | + post: post, |
| 130 | + modifications: { raw: ["old content", "completely new content"] } |
| 131 | + ) |
| 132 | + |
| 133 | + Jobs.expects(:enqueue).with(:ai_spam_scan, post_id: post.id) |
| 134 | + described_class.edited_post(post) |
| 135 | + end |
| 136 | + |
| 137 | + it "schedules delayed job when edited too soon after last scan" do |
| 138 | + AiSpamLog.create!( |
| 139 | + post: post, |
| 140 | + llm_model: llm_model, |
| 141 | + ai_api_audit_log_id: 1, |
| 142 | + is_spam: false, |
| 143 | + created_at: 5.minutes.ago |
| 144 | + ) |
| 145 | + |
| 146 | + Jobs.expects(:enqueue_in) |
| 147 | + described_class.edited_post(post) |
| 148 | + end |
| 149 | + end |
| 150 | + |
| 151 | + describe "integration test" do |
| 152 | + fab!(:llm_model) { Fabricate(:llm_model) } |
| 153 | + let(:api_audit_log) { Fabricate(:api_audit_log) } |
| 154 | + |
| 155 | + before do |
| 156 | + Jobs.run_immediately! |
| 157 | + end |
| 158 | + |
| 159 | + it "Correctly handles spam scanning" do |
| 160 | + # we need a proper audit log so |
| 161 | + prompt = nil |
| 162 | + DiscourseAi::Completions::Llm.with_prepared_responses(["spam"]) do |_,_,_prompts| |
| 163 | + described_class.new_post(post) |
| 164 | + prompt = _prompts.first |
| 165 | + end |
| 166 | + |
| 167 | + content = prompt.messages[1][:content] |
| 168 | + expect(content).to include(post.topic.title) |
| 169 | + expect(content).to include(post.raw) |
| 170 | + |
| 171 | + log = AiSpamLog.find_by(post: post) |
| 172 | + |
| 173 | + expect(log.payload).to eq(content) |
| 174 | + expect(log.is_spam).to eq(true) |
| 175 | + expect(post.user.reload.silenced_till).to be_present |
| 176 | + |
| 177 | + # hmm maybe it should be? |
| 178 | + #expect(post.topic.visible).to eq(false) |
| 179 | + end |
| 180 | + end |
| 181 | +end |
0 commit comments