|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Jobs::SummariesBackfill do |
| 4 | + fab!(:topic) { Fabricate(:topic, word_count: 200, highest_post_number: 2) } |
| 5 | + let(:limit) { 24 } # guarantee two summaries per batch |
| 6 | + |
| 7 | + before do |
| 8 | + assign_fake_provider_to(:ai_summarization_model) |
| 9 | + SiteSetting.ai_summarization_enabled = true |
| 10 | + SiteSetting.ai_summary_backfill_maximum_topics_per_hour = limit |
| 11 | + end |
| 12 | + |
| 13 | + describe "#current_budget" do |
| 14 | + context "when no summary has been backfilled yet" do |
| 15 | + it "returns the full budget" do |
| 16 | + expect(subject.current_budget).to eq(limit) |
| 17 | + end |
| 18 | + |
| 19 | + it "ignores summaries generated by users" do |
| 20 | + Fabricate(:ai_summary, target: topic, origin: AiSummary.origins[:human]) |
| 21 | + |
| 22 | + expect(subject.current_budget).to eq(limit) |
| 23 | + end |
| 24 | + |
| 25 | + it "only accounts for complete type summaries" do |
| 26 | + Fabricate(:topic_ai_gist, target: topic, origin: AiSummary.origins[:human]) |
| 27 | + |
| 28 | + expect(subject.current_budget).to eq(limit) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context "when we already backfilled stuff" do |
| 33 | + fab!(:backfilled_summary) do |
| 34 | + Fabricate(:ai_summary, target: topic, origin: AiSummary.origins[:system]) |
| 35 | + end |
| 36 | + |
| 37 | + context "if it was within the budget window" do |
| 38 | + it "reduces our budget" do |
| 39 | + expect(subject.current_budget).to eq(limit - 1) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "if it wasn't within the budget window" do |
| 44 | + before { freeze_time(2.hours.from_now) } |
| 45 | + |
| 46 | + it "returns the full budget" do |
| 47 | + freeze_time(2.hours.from_now) |
| 48 | + |
| 49 | + expect(subject.current_budget).to eq(limit) |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + describe "#backfill_candidates" do |
| 56 | + it "only selects posts with enough words" do |
| 57 | + topic.update!(word_count: 100) |
| 58 | + |
| 59 | + expect(subject.backfill_candidates).to be_empty |
| 60 | + end |
| 61 | + |
| 62 | + it "ignores up to date summaries" do |
| 63 | + Fabricate(:ai_summary, target: topic, content_range: (1..2)) |
| 64 | + |
| 65 | + expect(subject.backfill_candidates).to be_empty |
| 66 | + end |
| 67 | + |
| 68 | + it "orders candidates by topic#last_posted_at" do |
| 69 | + topic.update!(last_posted_at: 1.minute.ago) |
| 70 | + topic_2 = Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago) |
| 71 | + |
| 72 | + expect(subject.backfill_candidates.map(&:id)).to contain_exactly(topic.id, topic_2.id) |
| 73 | + end |
| 74 | + |
| 75 | + it "prioritizes topics without summaries" do |
| 76 | + topic_2 = |
| 77 | + Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago, highest_post_number: 1) |
| 78 | + topic.update!(last_posted_at: 1.minute.ago) |
| 79 | + Fabricate(:ai_summary, target: topic, content_range: (1..1)) |
| 80 | + |
| 81 | + expect(subject.backfill_candidates.map(&:id)).to contain_exactly(topic_2.id, topic.id) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "#execute" do |
| 86 | + it "backfills a batch" do |
| 87 | + topic_2 = |
| 88 | + Fabricate(:topic, word_count: 200, last_posted_at: 2.minutes.ago, highest_post_number: 1) |
| 89 | + topic.update!(last_posted_at: 1.minute.ago) |
| 90 | + Fabricate(:ai_summary, target: topic, created_at: 3.hours.ago, content_range: (1..1)) |
| 91 | + |
| 92 | + summary_1 = "Summary of topic_2" |
| 93 | + summary_2 = "Summary of topic" |
| 94 | + |
| 95 | + DiscourseAi::Completions::Llm.with_prepared_responses([summary_1, summary_2]) do |
| 96 | + subject.execute({}) |
| 97 | + end |
| 98 | + |
| 99 | + expect(AiSummary.find_by(target: topic_2).summarized_text).to eq(summary_1) |
| 100 | + expect(AiSummary.find_by(target: topic).summarized_text).to eq(summary_2) |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments