0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/plugins/discourse-ai/spec/jobs/scheduled/sentiment_backfill_spec.rb
Alan Guo Xiang Tan 6df9b0b357
DEV: Prefer RSpec primitives for test setup (#42122)
RSpec setup becomes harder to follow at either extreme: trivial fixture
wrappers hide lifecycle and intent, while forcing every named operation
inline repeats low-level protocol and configuration details.

This change documents and applies a test-setup hierarchy:

- use `fab!`, `let`, `let!`, `subject`, and inline `Fabricate` according
to lifecycle and role;
- use a small example-group method when parameterized behavior gives one
spec useful vocabulary;
- move helpers into auto-loaded `spec/support` only when they are shared
across spec files;
- use fabricators and page objects for the data shapes and system-test
interfaces they own.

Core and plugin support files are loaded centrally by `rails_helper`, so
plugin-specific support loaders are unnecessary. The migration specs
encountered during the sweep are removed according to repository policy;
production migrations are unchanged.
2026-07-30 11:17:22 +08:00

54 lines
1.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Jobs::SentimentBackfill do
subject(:job) { described_class.new }
before { enable_current_plugin }
describe "#execute" do
fab!(:post)
before do
SiteSetting.ai_sentiment_enabled = true
SiteSetting.ai_sentiment_backfill_maximum_posts_per_hour = 100
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
let(:expected_analysis) { DiscourseAi::Sentiment::SentimentSiteSettingJsonSchema.values.length }
it "backfills when settings are correct" do
SentimentInferenceStubs.stub_classification(post)
job.execute({})
expect(ClassificationResult.where(target: post).count).to eq(expected_analysis)
end
it "does nothing when batch size is zero" do
SiteSetting.ai_sentiment_backfill_maximum_posts_per_hour = 0
job.execute({})
expect(ClassificationResult.count).to be_zero
end
it "does nothing when sentiment is disabled" do
SiteSetting.ai_sentiment_enabled = false
job.execute({})
expect(ClassificationResult.count).to be_zero
end
it "respects the ai_sentiment_backfill_post_max_age_days setting" do
SentimentInferenceStubs.stub_classification(post)
SiteSetting.ai_sentiment_backfill_post_max_age_days = 80
post_2 = Fabricate(:post, created_at: 81.days.ago)
job.execute({})
expect(ClassificationResult.where(target: post).count).to eq(expected_analysis)
expect(ClassificationResult.where(target: post_2).count).to be_zero
end
end
end