Skip to content

A platform for community discussion. Free, open, simple.

22.1 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseAi::AiModeration::SpamScanner do
  fab!(:flagging_user) do
    described_class.ensure_flagging_user!
    described_class.flagging_user
  end
  fab!(:moderator)
  fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
  fab!(:topic)
  fab!(:post) { Fabricate(:post, user: user, topic: topic) }
  fab!(:llm_model)
  fab!(:spam_setting) do
    AiModerationSetting.create!(
      setting_type: :spam,
      llm_model: llm_model,
      data: {
        custom_instructions: "test instructions",
      },
    )
  end

  before do
    SiteSetting.ai_spam_detection_user_id = flagging_user.id
    enable_current_plugin
    SiteSetting.ai_spam_detection_enabled = true
  end

  describe ".enabled?" do
    it "returns true when both settings are enabled" do
      expect(described_class.enabled?).to eq(true)
    end

    it "returns false when discourse_ai is disabled" do
      SiteSetting.discourse_ai_enabled = false
      expect(described_class.enabled?).to eq(false)
    end

    it "returns false when spam detection is disabled" do
      SiteSetting.ai_spam_detection_enabled = false
      expect(described_class.enabled?).to eq(false)
    end
  end

  describe ".should_scan_post?" do
    it "returns true for new users' posts" do
      expect(described_class.should_scan_post?(post)).to eq(true)
    end

    it "returns false for trusted users" do
      post.user.trust_level = TrustLevel[2]
      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "respects custom max trust level setting" do
      post.user.trust_level = TrustLevel[2]
      expect(described_class.should_scan_post?(post)).to eq(false)

      SiteSetting.ai_spam_detection_max_trust_level = 2
      expect(described_class.should_scan_post?(post)).to eq(true)

      SiteSetting.ai_spam_detection_max_trust_level = 3
      post.user.trust_level = TrustLevel[3]
      expect(described_class.should_scan_post?(post)).to eq(true)
    end

    it "returns false for bots" do
      post.user.id = -100
      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "returns false for staff" do
      post.user.moderator = true
      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "returns false for users with many public posts" do
      Fabricate(:post, user: user, topic: topic)
      Fabricate(:post, user: user, topic: topic)
      expect(described_class.should_scan_post?(post)).to eq(true)

      pm = Fabricate(:private_message_topic, user: user)
      Fabricate(:post, user: user, topic: pm)

      expect(described_class.should_scan_post?(post)).to eq(true)

      topic = Fabricate(:topic, user: user)
      Fabricate(:post, user: user, topic: topic)

      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "respects custom max post count setting" do
      3.times { Fabricate(:post, user: user) }
      expect(described_class.should_scan_post?(post)).to eq(false)

      SiteSetting.ai_spam_detection_max_post_count = 5
      expect(described_class.should_scan_post?(post)).to eq(true)

      Fabricate(:post, user: user)
      expect(described_class.should_scan_post?(post)).to eq(true)

      Fabricate(:post, user: user)
      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "returns false for private messages" do
      pm_topic = Fabricate(:private_message_topic)
      pm_post = Fabricate(:post, topic: pm_topic, user: user)
      expect(described_class.should_scan_post?(pm_post)).to eq(false)
    end

    it "returns false for nil posts" do
      expect(described_class.should_scan_post?(nil)).to eq(false)
    end

    it "returns false for posts with a missing user" do
      post.association(:user).target = nil
      post.association(:user).loaded!
      expect(described_class.should_scan_post?(post)).to eq(false)
    end

    it "returns false for posts with a missing topic" do
      post.association(:topic).target = nil
      post.association(:topic).loaded!
      expect(described_class.should_scan_post?(post)).to eq(false)
    end
  end

  describe ".perform_scan" do
    it "does nothing if post should not be scanned" do
      post.user.trust_level = TrustLevel[2]

      expect { described_class.perform_scan(post) }.not_to change { AiSpamLog.count }
    end

    it "scans when post should be scanned" do
      expect do
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: true, reason: "spam detected" }],
        ) { described_class.perform_scan!(post) }
      end.to change { AiSpamLog.count }.by(1)
    end
  end

  describe ".perform_scan!" do
    it "creates spam log entry when scanning post" do
      expect do
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: true, reason: "spam detected" }],
        ) { described_class.perform_scan!(post) }
      end.to change { AiSpamLog.count }.by(1)
    end

    it "uses the configured agent's updated default model for spam scans" do
      updated_model = Fabricate(:fake_model)
      spam_setting.ai_agent.update!(default_llm_id: updated_model.id)

      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: false }],
        llm: updated_model,
      ) { described_class.perform_scan!(post) }

      expect(AiSpamLog.find_by!(post: post).llm_model_id).to eq(updated_model.id)
    end

    it "does nothing when disabled" do
      SiteSetting.ai_spam_detection_enabled = false
      expect { described_class.perform_scan!(post) }.not_to change { AiSpamLog.count }
    end
  end

  describe ".perform_scan! with a decision model" do
    fab!(:decision_model) { Fabricate(:decision_model, id: -1) }

    before { spam_setting.update!(decision_model:) }

    def scan_with(probability, llm_responses: [])
      DiscourseAi::Decisions.with_canned_answers(spam: probability) do |canned|
        DiscourseAi::Completions::Llm.with_prepared_responses(llm_responses) do |_, _, prompts|
          described_class.perform_scan!(post, triggering_user_id: post.user_id)
          [canned.calls, prompts]
        end
      end
    end

    it "flags confident spam without asking the LLM" do
      decision_calls, prompts = scan_with(0.95)

      log = AiSpamLog.find_by!(post:)
      expect([log.is_spam, log.spam_probability, log.decision_model_id, log.llm_model_id]).to eq(
        [true, 0.95, decision_model.id, nil],
      )
      expect(log.reviewable).to be_present
      expect(post.user.reload).to be_silenced
      expect(prompts).to be_empty
      expect(decision_calls.first[:questions].first.instructions).to include("test instructions")
    end

    it "clears confident non-spam without asking the LLM" do
      _, prompts = scan_with(0.01)

      expect(AiSpamLog.find_by!(post:).is_spam).to eq(false)
      expect(prompts).to be_empty
    end

    it "lets the LLM decide unsure posts and keeps the decision model's probability" do
      scan_with(0.5, llm_responses: [{ spam: true, reason: "llm reason" }])

      log = AiSpamLog.find_by!(post:)
      expect([log.is_spam, log.reason, log.spam_probability, log.llm_model_id]).to eq(
        [true, "llm reason", 0.5, llm_model.id],
      )
    end

    it "lets the LLM decide when the decision model fails" do
      stub_request(:post, decision_model.url).to_return(status: 400, body: "{}")

      DiscourseAi::Completions::Llm.with_prepared_responses([{ spam: false }]) do
        described_class.perform_scan!(post)
      end

      expect(AiSpamLog.find_by!(post:).llm_model_id).to eq(llm_model.id)
    end

    it "decides every post at one cutoff without an LLM" do
      spam_setting.update!(llm_model: nil)

      scan_with(0.6)
      expect(AiSpamLog.find_by!(post:).is_spam).to eq(true)

      AiSpamLog.delete_all
      scan_with(0.4)
      expect(AiSpamLog.find_by!(post:).is_spam).to eq(false)
    end

    it "skips the scan without an LLM once the decision model is out of credits" do
      spam_setting.update!(llm_model: nil)
      Fabricate(:decision_model_credit_allocation, decision_model:, daily_used: 10_000)

      expect { described_class.perform_scan!(post) }.not_to change { AiSpamLog.count }
    end
  end

  describe ".scanned_max_times?" do
    it "returns true when post has been scanned 3 times" do
      3.times do
        AiSpamLog.create!(post: post, llm_model: llm_model, ai_api_audit_log_id: 1, is_spam: false)
      end

      expect(described_class.scanned_max_times?(post)).to eq(true)
    end

    it "returns false for posts scanned less than 3 times" do
      expect(described_class.scanned_max_times?(post)).to eq(false)
    end
  end

  describe ".significant_change?" do
    it "returns true for first edits" do
      expect(described_class.significant_change?(nil, "new content")).to eq(true)
    end

    it "returns true for significant changes" do
      old_version = "This is a test post"
      new_version = "This is a completely different post with new content"
      expect(described_class.significant_change?(old_version, new_version)).to eq(true)
    end

    it "returns false for minor changes" do
      old_version = "This is a test post"
      new_version = "This is a test Post" # Only capitalization change
      expect(described_class.significant_change?(old_version, new_version)).to eq(false)
    end
  end

  describe ".new_post" do
    it "enqueues spam scan job for eligible posts" do
      expect {
        described_class.new_post(post)
        described_class.after_cooked_post(post)
      }.to change(Jobs::AiSpamScan.jobs, :size).by(1)
    end

    it "doesn't enqueue jobs when disabled" do
      SiteSetting.ai_spam_detection_enabled = false
      expect { described_class.new_post(post) }.not_to change(Jobs::AiSpamScan.jobs, :size)
    end

    it "doesn't flag posts that were approved from the review queue" do
      reviewable =
        ReviewableQueuedPost.create!(
          created_by: Discourse.system_user,
          target_created_by: user,
          topic_id: topic.id,
          payload: {
            raw: "This is a test post from the queue",
          },
        )

      result = reviewable.perform(moderator, :approve_post)
      approved_post = result.created_post

      expect {
        described_class.new_post(approved_post)
        described_class.after_cooked_post(approved_post)
      }.not_to change(Jobs::AiSpamScan.jobs, :size)
    end
  end

  describe ".edited_post" do
    it "enqueues spam scan job for eligible edited posts" do
      PostRevision.create!(
        post: post,
        modifications: {
          raw: ["old content", "completely new content"],
        },
      )

      expect {
        described_class.edited_post(post)
        described_class.after_cooked_post(post)
      }.to change(Jobs::AiSpamScan.jobs, :size).by(1)
    end

    it "schedules delayed job when edited too soon after last scan" do
      AiSpamLog.create!(
        post: post,
        llm_model: llm_model,
        ai_api_audit_log_id: 1,
        is_spam: false,
        created_at: 5.minutes.ago,
      )

      expect {
        described_class.edited_post(post)
        described_class.after_cooked_post(post)
      }.to change(Jobs::AiSpamScan.jobs, :size).by(1)
    end

    it "passes the editor id in the job args without persisting it on the post" do
      editor = Fabricate(:user, trust_level: TrustLevel[0])
      post.update!(last_editor_id: editor.id)
      PostRevision.create!(
        post: post,
        user: editor,
        modifications: {
          raw: ["old content", "completely new content"],
        },
      )

      described_class.edited_post(post)
      described_class.after_cooked_post(post)

      job_args = Jobs::AiSpamScan.jobs.last["args"].first
      expect(post.reload.custom_fields).not_to have_key("discourse_ai_spam_scan_triggering_user_id")
      expect(job_args["triggering_user_id"]).to eq(editor.id)
    end

    it "does nothing when staff is the last revisor" do
      expect {
        PostRevisor.new(post).revise!(moderator, title: "#{post.topic.title} spam spam")
        described_class.after_cooked_post(post)
      }.not_to change(Jobs::AiSpamScan.jobs, :size)
    end

    it "does not create a reviewable when staff edits a topic" do
      Jobs.run_immediately!

      post = nil
      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: false, reason: "no spam detected" }],
      ) do
        post =
          create_post(
            user: user,
            title: "Topic to test AI spam flagging",
            raw: "Initial content that will pass initial scan.",
          )
      end

      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: true, reason: "spam detected" }],
      ) { PostRevisor.new(post).revise!(moderator, title: "A completely different title") }

      expect(Reviewable.where(target: post).count).to eq(0)
    end
  end

  describe ".hide_post" do
    fab!(:spam_post) { Fabricate(:post, user: user) }
    fab!(:second_spam_post) { Fabricate(:post, topic: spam_post.topic, user: user) }

    it "hides spam post and topic for first post" do
      described_class.hide_post(spam_post)

      expect(spam_post.reload.hidden).to eq(true)
      expect(second_spam_post.reload.hidden).to eq(false)
      expect(spam_post.reload.hidden_reason_id).to eq(
        Post.hidden_reasons[:new_user_spam_threshold_reached],
      )
    end

    it "doesn't hide the topic for non-first posts" do
      described_class.hide_post(second_spam_post)

      expect(spam_post.reload.hidden).to eq(false)
      expect(second_spam_post.reload.hidden).to eq(true)
      expect(spam_post.topic.reload.visible).to eq(true)
    end
  end

  it "unsilences flagging user if erroneously silenced" do
    described_class.flagging_user.update!(silenced_till: 1.day.from_now)
    expect(described_class.flagging_user.silenced?).to eq(false)
  end

  it "ensures flagging user is tl4" do
    described_class.flagging_user.update!(trust_level: 0)
    expect(described_class.flagging_user.trust_level).to eq(4)
  end

  it "unsuspends user if it was erroneously suspended" do
    described_class.flagging_user.update!(suspended_till: 1.day.from_now, suspended_at: 1.day.ago)
    expect(described_class.flagging_user.suspended?).to eq(false)
  end

  it "makes sure account is active" do
    described_class.flagging_user.update!(active: false)
    expect(described_class.flagging_user.active).to eq(true)
  end

  describe "integration test" do
    fab!(:llm_model)
    let(:api_audit_log) { Fabricate(:api_audit_log) }

    fab!(:post_with_uploaded_image)

    before { Jobs.run_immediately! }

    it "notifies moderators when the site asks to be notified about auto silences" do
      SiteSetting.notify_mods_when_user_silenced = true

      allow(GroupMessage).to receive(:create)

      described_class.new_post(post)
      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: true, reason: "spam detected" }],
      ) { post.rebake! }

      expect(GroupMessage).to have_received(:create).at_least(:once)
      expect(post.user.reload).to be_silenced
    end

    it "can correctly run tests" do
      prompts = nil
      result =
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: true, reason: "the reason is just because" }],
        ) do |_, _, _prompts|
          prompts = _prompts
          described_class.test_post(post, custom_instructions: "123")
        end

      expect(prompts.length).to eq(1)
      expect(result[:is_spam]).to eq(true)
      expect(result[:system_prompt]).to include("123")
      expect(result[:reason]).to eq("the reason is just because")

      result =
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: false, reason: "the reason is just because" }],
        ) do |_, _, _prompts|
          prompts = _prompts
          described_class.test_post(post, custom_instructions: "123")
        end

      expect(result[:is_spam]).to eq(false)
    end

    it "tests with the spam detector agent before any spam settings are saved" do
      spam_setting.destroy!

      result =
        DiscourseAi::Completions::Llm.with_prepared_responses([{ spam: true }]) do
          described_class.test_post(post, llm_model:, decision_model: nil)
        end

      expect(result[:is_spam]).to eq(true)
    end

    it "correctly handles spam scanning" do
      expect(described_class.flagging_user.id).not_to eq(Discourse.system_user.id)

      post = post_with_uploaded_image
      # this is surprising, core fabricator is not linking
      # we need it linked so we scan uploads
      post.link_post_uploads

      expect(described_class.should_scan_post?(post)).to eq(true)
      expect(post.upload_ids).to be_present

      described_class.new_post(post)

      prompt = nil
      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: true, reason: "spam detected" }],
      ) do |_, _, _prompts|
        # force a rebake so we actually scan
        post.rebake!
        prompt = _prompts.first
      end

      # its an array so lets just stringify it to make testing easier
      content = prompt.messages[1][:content][0]
      expect(content).to include(post.topic.title)
      expect(content).to include(post.raw)

      upload_ids = prompt.messages[1][:content].map { |m| m[:upload_id] if m.is_a?(Hash) }.compact
      expect(upload_ids).to be_present
      expect(upload_ids).to eq(post.upload_ids)

      log = AiSpamLog.find_by(post: post)

      expect(log.payload).to eq(content)
      expect(log.is_spam).to eq(true)
      expect(post.user.reload.silenced_till).to be_present
      expect(post.topic.reload.visible).to eq(false)

      history = UserHistory.where(action: UserHistory.actions[:silence_user]).order(:id).last

      url = "#{Discourse.base_url}/admin/plugins/discourse-ai/ai-spam"

      expect(history.target_user_id).to eq(post.user_id)
      expect(history.details).to include(
        I18n.t("discourse_ai.spam_detection.silence_reason", url: url),
      )

      expect(log.reviewable).to be_present
      expect(log.reviewable.created_by_id).to eq(described_class.flagging_user.id)

      expect(history.post_id).to eq(post.id)
      expect(history.reviewable_id).to eq(log.reviewable.id)

      log.reviewable.perform(moderator, :disagree_and_restore)

      expect(post.reload.hidden?).to eq(false)
      expect(post.topic.reload.visible).to eq(true)
      expect(post.user.reload.silenced?).to eq(false)
    end

    it "does not silence the author when another user adds spam to a wiki post" do
      wiki_editor = Fabricate(:user, trust_level: TrustLevel[0])
      wiki_post = Fabricate(:post, user: user, wiki: true, raw: "Helpful community wiki content")

      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: true, reason: "spam detected" }],
      ) do
        PostRevisor.new(wiki_post).revise!(
          wiki_editor,
          raw: "Buy questionable products from this completely different wiki content",
        )
        wiki_post.rebake!
      end

      log = AiSpamLog.find_by(post: wiki_post)

      expect(log.reviewable).to be_present
      expect(wiki_post.user.reload).not_to be_silenced
      expect(
        UserHistory.where(action: UserHistory.actions[:silence_user], target_user_id: user.id),
      ).to be_blank
    end

    it "skips posts already flagged by the scanner" do
      post = post_with_uploaded_image
      Fabricate(
        :post_action,
        post: post,
        user: described_class.flagging_user,
        post_action_type_id: PostActionType.types[:spam],
      )

      described_class.new_post(post)

      DiscourseAi::Completions::Llm.with_prepared_responses(
        [{ spam: true, reason: "spam detected" }],
      ) do |_, _, _prompts|
        # force a rebake so we actually scan
        post.rebake!
      end

      log = AiSpamLog.find_by(post: post)

      expect(log.reviewable).to be_nil
      expect(log.error).to eq("skipped because post already has a flag")
      expect(post.user.reload).not_to be_silenced
      expect(post.topic.reload).to be_visible
    end

    it "skips posts that already have an active flag" do
      flagger = Fabricate(:user)
      Fabricate(
        :post_action,
        post: post,
        user: flagger,
        post_action_type_id: PostActionType.types[:spam],
      )

      described_class.new_post(post)

      expect do
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: true, reason: "spam detected" }],
        ) { post.rebake! }
      end.not_to change { PostAction.active.flags.where(post: post).count }

      log = AiSpamLog.find_by(post: post)

      expect(log.reviewable).to be_nil
      expect(log.error).to eq("skipped because post already has a flag")
      expect(post.user.reload).not_to be_silenced
      expect(post.topic.reload).to be_visible
    end

    it "does not silence or hide when flag creation fails" do
      flagging_user = Fabricate(:user, trust_level: TrustLevel[4])
      SiteSetting.ai_spam_detection_user_id = flagging_user.id

      described_class.new_post(post)

      expect do
        DiscourseAi::Completions::Llm.with_prepared_responses(
          [{ spam: true, reason: "spam detected" }],
        ) { post.rebake! }
      end.not_to change { PostAction.active.flags.where(post: post).count }

      log = AiSpamLog.find_by(post: post)

      expect(log.reviewable).to be_nil
      expect(log.error).to match(/unable to flag post as spam/)
      expect(post.user.reload).not_to be_silenced
      expect(post.topic.reload).to be_visible
    end
  end

  it "includes location information and email in context" do
    user.update!(ip_address: "1.2.3.4", registration_ip_address: "5.6.7.8")

    ip_info_registration = { location: "New York", organization: "ISP1" }
    ip_info_last = { location: "London", organization: "ISP2" }

    DiscourseIpInfo
      .stubs(:get)
      .with("5.6.7.8", resolve_hostname: true)
      .returns(ip_info_registration)
    DiscourseIpInfo.stubs(:get).with("1.2.3.4", resolve_hostname: true).returns(ip_info_last)

    prompts = nil
    DiscourseAi::Completions::Llm.with_prepared_responses(
      [{ spam: true, reason: "just because" }],
    ) do |_, _, _prompts|
      prompts = _prompts
      described_class.test_post(post)
    end

    context = prompts.first.messages[1][:content]
    expect(context).to include("Registration Location: New York (ISP1)")
    expect(context).to include("Last Location: London (ISP2)")
    expect(context).to include(user.email)
  end
end