|
9 | 9 |
|
10 | 10 | describe "#execute" do |
11 | 11 | it "does nothing with blank item_ids" do |
12 | | - expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts) |
| 12 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive( |
| 13 | + :match_topic_to_concepts, |
| 14 | + ) |
13 | 15 |
|
14 | 16 | subject.execute(item_type: "topics", item_ids: []) |
15 | 17 | subject.execute(item_type: "topics", item_ids: nil) |
16 | 18 | end |
17 | 19 |
|
18 | 20 | it "does nothing with blank item_type" do |
19 | | - expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts) |
| 21 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive( |
| 22 | + :match_topic_to_concepts, |
| 23 | + ) |
20 | 24 |
|
21 | 25 | subject.execute(item_type: "", item_ids: [topic.id]) |
22 | 26 | subject.execute(item_type: nil, item_ids: [topic.id]) |
|
30 | 34 |
|
31 | 35 | context "with topics" do |
32 | 36 | it "processes topics in match_only mode" do |
33 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with( |
34 | | - topic, |
35 | | - ) |
| 37 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive( |
| 38 | + :match_topic_to_concepts, |
| 39 | + ).with(topic) |
36 | 40 |
|
37 | 41 | subject.execute(item_type: "topics", item_ids: [topic.id], match_only: true) |
38 | 42 | end |
39 | 43 |
|
40 | 44 | it "processes topics in generation mode" do |
41 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive( |
| 45 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive( |
42 | 46 | :generate_concepts_from_topic, |
43 | 47 | ).with(topic) |
44 | 48 |
|
|
47 | 51 |
|
48 | 52 | it "handles topics that don't exist" do |
49 | 53 | # Non-existent IDs should be silently skipped (no error expected) |
50 | | - expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_topic_to_concepts) |
| 54 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive( |
| 55 | + :match_topic_to_concepts, |
| 56 | + ) |
51 | 57 |
|
52 | 58 | subject.execute( |
53 | 59 | item_type: "topics", |
|
59 | 65 | it "processes multiple topics" do |
60 | 66 | topic2 = Fabricate(:topic) |
61 | 67 |
|
62 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with( |
63 | | - topic, |
64 | | - ) |
65 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).with( |
66 | | - topic2, |
67 | | - ) |
| 68 | + manager_instance = instance_double(DiscourseAi::InferredConcepts::Manager) |
| 69 | + allow(DiscourseAi::InferredConcepts::Manager).to receive(:new).and_return(manager_instance) |
| 70 | + |
| 71 | + expect(manager_instance).to receive(:match_topic_to_concepts).with(topic) |
| 72 | + expect(manager_instance).to receive(:match_topic_to_concepts).with(topic2) |
68 | 73 |
|
69 | 74 | subject.execute(item_type: "topics", item_ids: [topic.id, topic2.id], match_only: true) |
70 | 75 | end |
|
83 | 88 |
|
84 | 89 | context "with posts" do |
85 | 90 | it "processes posts in match_only mode" do |
86 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with( |
87 | | - post, |
88 | | - ) |
| 91 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive( |
| 92 | + :match_post_to_concepts, |
| 93 | + ).with(post) |
89 | 94 |
|
90 | 95 | subject.execute(item_type: "posts", item_ids: [post.id], match_only: true) |
91 | 96 | end |
92 | 97 |
|
93 | 98 | it "processes posts in generation mode" do |
94 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive( |
| 99 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive( |
95 | 100 | :generate_concepts_from_post, |
96 | 101 | ).with(post) |
97 | 102 |
|
|
100 | 105 |
|
101 | 106 | it "handles posts that don't exist" do |
102 | 107 | # Non-existent IDs should be silently skipped (no error expected) |
103 | | - expect(DiscourseAi::InferredConcepts::Manager).not_to receive(:match_post_to_concepts) |
| 108 | + expect_any_instance_of(DiscourseAi::InferredConcepts::Manager).not_to receive( |
| 109 | + :match_post_to_concepts, |
| 110 | + ) |
104 | 111 |
|
105 | 112 | subject.execute( |
106 | 113 | item_type: "posts", |
|
112 | 119 | it "processes multiple posts" do |
113 | 120 | post2 = Fabricate(:post) |
114 | 121 |
|
115 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with( |
116 | | - post, |
117 | | - ) |
118 | | - expect(DiscourseAi::InferredConcepts::Manager).to receive(:match_post_to_concepts).with( |
119 | | - post2, |
120 | | - ) |
| 122 | + manager_instance = instance_double(DiscourseAi::InferredConcepts::Manager) |
| 123 | + allow(DiscourseAi::InferredConcepts::Manager).to receive(:new).and_return(manager_instance) |
| 124 | + |
| 125 | + expect(manager_instance).to receive(:match_post_to_concepts).with(post) |
| 126 | + expect(manager_instance).to receive(:match_post_to_concepts).with(post2) |
121 | 127 |
|
122 | 128 | subject.execute(item_type: "posts", item_ids: [post.id, post2.id], match_only: true) |
123 | 129 | end |
124 | 130 | end |
125 | 131 |
|
126 | 132 | it "handles exceptions during processing" do |
127 | | - allow(DiscourseAi::InferredConcepts::Manager).to receive(:match_topic_to_concepts).and_raise( |
128 | | - StandardError.new("Test error"), |
129 | | - ) |
| 133 | + allow_any_instance_of(DiscourseAi::InferredConcepts::Manager).to receive( |
| 134 | + :match_topic_to_concepts, |
| 135 | + ).and_raise(StandardError.new("Test error")) |
130 | 136 |
|
131 | 137 | expect(Rails.logger).to receive(:error).with( |
132 | 138 | /Error generating concepts from topic #{topic.id}/, |
|
0 commit comments