Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 43e2faf

Browse files
committed
DEV: Complete specs
1 parent d28bdfc commit 43e2faf

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

app/controllers/discourse_ai/sentiment/sentiment_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class SentimentController < ::Admin::StaffController
99

1010
def posts
1111
group_by = params[:group_by]&.to_sym
12-
group_value = params[:group_value]
13-
start_date = params[:start_date]
12+
group_value = params[:group_value].presence
13+
start_date = params[:start_date].presence
1414
end_date = params[:end_date]
1515
threshold = SENTIMENT_THRESHOLD
1616

@@ -58,7 +58,7 @@ def posts
5858
t.archetype = 'regular' AND
5959
p.user_id > 0 AND
6060
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
61-
(p.created_at > :start_date AND p.created_at < :end_date)
61+
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
6262
ORDER BY p.created_at DESC
6363
SQL
6464
group_value: group_value,

assets/javascripts/initializers/admin-reports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
}
1212

1313
// We need to import dynamically with CommonJS require because
14-
// using ESM import would cause the component to be imported globally
14+
// using ESM import in an initializer would cause the component to be imported globally
1515
// and cause errors for non-admin users since the component is only available to admins
1616
const AdminReportSentimentAnalysis =
1717
require("discourse/plugins/discourse-ai/discourse/components/admin-report-sentiment-analysis").default;
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
# frozen_string_literal: true
22

33
RSpec.describe DiscourseAi::Sentiment::SentimentAnalysisReport do
4-
fab!(:user_1) { Fabricate(:user) }
5-
fab!(:user_2) { Fabricate(:user) }
6-
fab!(:post_1) { Fabricate(:post, user: user_1) }
7-
fab!(:post_2) { Fabricate(:post, user: user_2) }
4+
fab!(:admin)
5+
fab!(:category)
6+
fab!(:topic) { Fabricate(:topic, category: category) }
7+
fab!(:post) { Fabricate(:post, user: admin, topic: topic) }
8+
fab!(:post_2) { Fabricate(:post, user: admin, topic: topic) }
9+
fab!(:classification_result) { Fabricate(:classification_result, target: post) }
810

9-
before do
10-
SiteSetting.discourse_ai_enabled = true
11-
SiteSetting.ai_embeddings_enabled = false
12-
end
11+
before { SiteSetting.ai_sentiment_enabled = true }
1312

1413
it "contains the correct filters" do
1514
report = Report.find("sentiment_analysis")
16-
pp report.availble_filters.keys
15+
expect(report.available_filters).to include("group_by", "sort_by", "category", "tag")
16+
end
17+
18+
it "contains the correct labels" do
19+
report = Report.find("sentiment_analysis")
20+
expect(report.labels).to eq(%w[Positive Neutral Negative])
1721
end
1822
end

spec/requests/sentiment/sentiment_controller_spec.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@
22

33
RSpec.describe DiscourseAi::Sentiment::SentimentController do
44
describe "#posts" do
5-
fab!(:user)
5+
fab!(:admin)
66
fab!(:category)
7-
fab!(:post) { Fabricate(:post, user: user) }
8-
fab!(:embedding_definition)
7+
fab!(:topic) { Fabricate(:topic, category: category) }
8+
fab!(:post) { Fabricate(:post, user: admin, topic: topic) }
9+
fab!(:post_2) { Fabricate(:post, user: admin, topic: topic) }
10+
fab!(:classification_result) { Fabricate(:classification_result, target: post) }
911

10-
# before do
11-
# SiteSetting.ai_embeddings_enabled = false
12-
# SiteSetting.ai_embeddings_selected_model = ""
13-
# sign_in(user)
14-
# end
12+
before do
13+
SiteSetting.ai_sentiment_enabled = true
14+
sign_in(admin)
15+
end
1516

1617
it "returns a posts based on params" do
17-
get "/discourse-ai/sentiment/posts.json",
18+
post.reload
19+
classification_result.reload
20+
21+
get "/discourse-ai/sentiment/posts",
1822
params: {
1923
group_by: "category",
2024
group_value: category.name,
21-
start_date: 1.month.ago.to_s,
22-
end_date: 0.days.ago.to_s,
23-
threshold: 0.6,
25+
threshold: 0.0,
2426
}
2527

26-
pp response.inspect
2728
expect(response).to be_successful
29+
30+
posts = JSON.parse(response.body)
31+
posts.each do |post|
32+
expect(post).to have_key("sentiment")
33+
expect(post["sentiment"]).to match(/positive|negative|neutral/)
34+
end
2835
end
2936
end
3037
end

0 commit comments

Comments
 (0)