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