2023-02-24 11:25:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
require_relative "../support/toxicity_inference_stubs"
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
describe DiscourseAi::PostClassificator do
|
2023-02-24 11:25:02 -05:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
let(:model) { DiscourseAi::Toxicity::ToxicityClassification.new }
|
2023-02-24 11:25:02 -05:00
|
|
|
let(:classification) { described_class.new(model) }
|
|
|
|
|
|
|
|
describe "#classify!" do
|
|
|
|
before { ToxicityInferenceStubs.stub_post_classification(post, toxic: true) }
|
|
|
|
|
2023-02-27 14:21:40 -05:00
|
|
|
it "stores the model classification data" do
|
2023-02-24 11:25:02 -05:00
|
|
|
classification.classify!(post)
|
2023-02-27 14:21:40 -05:00
|
|
|
result = ClassificationResult.find_by(target: post, classification_type: model.type)
|
|
|
|
|
|
|
|
classification = result.classification.symbolize_keys
|
|
|
|
|
|
|
|
expect(classification).to eq(ToxicityInferenceStubs.toxic_response)
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "flags the message and hides the post when the model decides we should" do
|
|
|
|
SiteSetting.ai_toxicity_flag_automatically = true
|
|
|
|
|
|
|
|
classification.classify!(post)
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
expect(ReviewableAiPost.where(target: post).count).to eq(1)
|
2023-02-24 11:25:02 -05:00
|
|
|
expect(post.reload.hidden?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't flags the message if the model decides we shouldn't" do
|
|
|
|
SiteSetting.ai_toxicity_flag_automatically = false
|
|
|
|
|
|
|
|
classification.classify!(post)
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
expect(ReviewableAiPost.where(target: post).count).to be_zero
|
2023-03-07 13:39:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the model accuracy in the payload" do
|
|
|
|
SiteSetting.ai_toxicity_flag_automatically = true
|
|
|
|
classification.classify!(post)
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
reviewable = ReviewableAiPost.find_by(target: post)
|
2023-03-07 13:39:28 -05:00
|
|
|
|
|
|
|
expect(
|
|
|
|
reviewable.payload.dig("accuracies", SiteSetting.ai_toxicity_inference_service_api_model),
|
|
|
|
).to be_zero
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|