A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
RSpec.describe DiscourseAi::Sentiment::EntryPoint do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
before { enable_current_plugin }
describe "admin dashboard report registration" do
it "excludes the per-emotion reports while keeping the sentiment summaries" do
DiscourseAi::Sentiment::Emotions::LIST.each do |emotion|
expect(Report.dashboard_excluded_report_types).to include("emotion_#{emotion}")
end
expect(Report.dashboard_excluded_report_types).not_to include(
"overall_sentiment",
"sentiment_analysis",
)
end
end
describe "registering event callbacks" do
context "when editing a post" do
fab!(:post) { Fabricate(:post, user: user) }
let(:revisor) { PostRevisor.new(post) }
it "queues a job on update if sentiment analysis is enabled" do
SiteSetting.ai_sentiment_enabled = true
expect { revisor.revise!(user, raw: "This is my new test") }.to change(
Jobs::PostSentimentAnalysis.jobs,
:size,
).by(1)
end
it "does nothing if sentiment analysis is disabled" do
SiteSetting.ai_sentiment_enabled = false
expect { revisor.revise!(user, raw: "This is my new test") }.not_to change(
Jobs::PostSentimentAnalysis.jobs,
:size,
)
end
end
end
describe "custom reports" do
before do
SiteSetting.ai_sentiment_model_configs =
"[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]"
end
fab!(:pm, :private_message_post)
fab!(:post_1, :post)
fab!(:post_2, :post)
describe "overall_sentiment report" do
fab!(:post_1_classification) do
Fabricate(
:sentiment_classification,
target: post_1,
classification: {
negative: 0.2,
neutral: 0.3,
positive: 0.7,
},
)
end
fab!(:post_2_classification) do
Fabricate(
:sentiment_classification,
target: post_2,
classification: {
negative: 0.65,
neutral: 0.2,
positive: 0.1,
},
)
end
fab!(:private_message_classification) do
Fabricate(
:sentiment_classification,
target: pm,
classification: post_1_classification.classification,
)
end
it "calculate averages using only public posts" do
report = Report.find("overall_sentiment")
overall_sentiment = report.data[0][:data][0][:y].to_i
expect(overall_sentiment).to eq(0)
end
it "exports the report without any errors" do
exporter = Jobs::ExportCsvFile.new
exporter.entity = "report"
exporter.extra = ActiveSupport::HashWithIndifferentAccess.new(name: "overall_sentiment")
exported_csv = []
exporter.report_export { |entry| exported_csv << entry }
expect(exported_csv[0]).to eq(["Day", "Overall sentiment (Positive - Negative)"])
expect(exported_csv[1]).to eq([post_1.created_at.to_date.to_s, "0"])
end
end
describe "post_emotion report" do
fab!(:post_1_classification) do
Fabricate(
:sentiment_classification,
target: post_1,
model_used: "SamLowe/roberta-base-go_emotions",
classification: {
love: 0.9444406,
admiration: 0.013724019,
surprise: 0.010188869,
excitement: 0.007888741,
curiosity: 0.006301749,
joy: 0.004060776,
confusion: 0.0028238264,
approval: 0.0018160914,
realization: 0.001174849,
neutral: 0.0008561869,
amusement: 0.00075853954,
disapproval: 0.0006987994,
disappointment: 0.0006166883,
anger: 0.0006000542,
annoyance: 0.0005615011,
desire: 0.00046368592,
fear: 0.00045117878,
sadness: 0.00041727215,
gratitude: 0.00041727215,
optimism: 0.00037112957,
disgust: 0.00035552034,
nervousness: 0.00022954118,
embarrassment: 0.0002049572,
caring: 0.00017737568,
remorse: 0.00011407586,
grief: 0.0001006716,
pride: 0.00009681493,
relief: 0.00008919009,
},
)
end
fab!(:post_2_classification) do
Fabricate(
:sentiment_classification,
target: post_2,
model_used: "SamLowe/roberta-base-go_emotions",
classification: {
love: 0.8444406,
admiration: 0.113724019,
surprise: 0.010188869,
excitement: 0.007888741,
curiosity: 0.006301749,
joy: 0.004060776,
confusion: 0.0028238264,
approval: 0.0018160914,
realization: 0.001174849,
neutral: 0.0008561869,
amusement: 0.00075853954,
disapproval: 0.0006987994,
disappointment: 0.0006166883,
anger: 0.0006000542,
annoyance: 0.0005615011,
desire: 0.00046368592,
fear: 0.00045117878,
sadness: 0.00041727215,
gratitude: 0.00041727215,
optimism: 0.00037112957,
disgust: 0.00035552034,
nervousness: 0.00022954118,
embarrassment: 0.0002049572,
caring: 0.00017737568,
remorse: 0.00011407586,
grief: 0.0001006716,
pride: 0.00009681493,
relief: 0.00008919009,
},
)
end
fab!(:private_message_classification) do
Fabricate(
:sentiment_classification,
target: pm,
model_used: "SamLowe/roberta-base-go_emotions",
classification: post_2_classification.classification,
)
end
it "calculate averages using only public posts" do
threshold = 0.10
report = Report.find("emotion_love")
data_point = report.data
data_point.each do |point|
expected =
(post_1_classification.classification["love"] > threshold ? 1 : 0) +
(post_2_classification.classification["love"] > threshold ? 1 : 0)
expect(point[:y]).to eq(expected)
end
end
end
end
describe "independent capability enable flags" do
fab!(:post)
fab!(:admin)
[[false, false], [true, false], [false, true], [true, true]].each do |sentiment, emotion|
it "gates edited-post jobs and report visibility with sentiment=#{sentiment} and emotion=#{emotion}" do
SiteSetting.ai_sentiment_enabled = sentiment
SiteSetting.ai_sentiment_emotion_enabled = emotion
Fabricate(:sentiment_classification, target: post)
PostRevisor.new(post).revise!(post.user, raw: "#{post.raw} Edited content")
expect(job_enqueued?(job: :post_sentiment_analysis, args: { post_id: post.id })).to eq(
sentiment || emotion,
)
serializer = CurrentUserSerializer.new(admin, scope: admin.guardian)
expect(serializer.can_see_sentiment_reports).to eq(sentiment || emotion)
end
end
it "keeps report visibility off without stored results even when emotion is enabled" do
SiteSetting.ai_sentiment_emotion_enabled = true
serializer = CurrentUserSerializer.new(admin, scope: admin.guardian)
expect(serializer.can_see_sentiment_reports).to eq(false)
end
end
end