2023-02-24 11:25:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
require_relative "../support/toxicity_inference_stubs"
|
|
|
|
|
|
|
|
describe DiscourseAI::ChatMessageClassification do
|
|
|
|
fab!(:chat_message) { Fabricate(:chat_message) }
|
|
|
|
|
|
|
|
let(:model) { DiscourseAI::Toxicity::ToxicityClassification.new }
|
|
|
|
let(:classification) { described_class.new(model) }
|
|
|
|
|
|
|
|
describe "#classify!" do
|
|
|
|
before { ToxicityInferenceStubs.stub_chat_message_classification(chat_message, 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!(chat_message)
|
|
|
|
|
2023-02-27 14:21:40 -05:00
|
|
|
result = ClassificationResult.find_by(target: chat_message, classification_type: model.type)
|
2023-02-24 11:25:02 -05:00
|
|
|
|
2023-02-27 14:21:40 -05:00
|
|
|
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 when the model decides we should" do
|
|
|
|
SiteSetting.ai_toxicity_flag_automatically = true
|
|
|
|
|
|
|
|
classification.classify!(chat_message)
|
|
|
|
|
|
|
|
expect(ReviewableChatMessage.where(target: chat_message).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't flags the message if the model decides we shouldn't" do
|
|
|
|
SiteSetting.ai_toxicity_flag_automatically = false
|
|
|
|
|
|
|
|
classification.classify!(chat_message)
|
|
|
|
|
|
|
|
expect(ReviewableChatMessage.where(target: chat_message).count).to be_zero
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|