| 
									
										
										
										
											2023-03-31 11:04:34 +11:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rails_helper" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe DiscourseAi::Embeddings::SemanticRelated do | 
					
						
							|  |  |  |   fab!(:target) { Fabricate(:topic) } | 
					
						
							|  |  |  |   fab!(:normal_topic_1) { Fabricate(:topic) } | 
					
						
							|  |  |  |   fab!(:normal_topic_2) { Fabricate(:topic) } | 
					
						
							|  |  |  |   fab!(:normal_topic_3) { Fabricate(:topic) } | 
					
						
							|  |  |  |   fab!(:unlisted_topic) { Fabricate(:topic, visible: false) } | 
					
						
							|  |  |  |   fab!(:private_topic) { Fabricate(:private_message_topic) } | 
					
						
							|  |  |  |   fab!(:secured_category) { Fabricate(:category, read_restricted: true) } | 
					
						
							|  |  |  |   fab!(:secured_category_topic) { Fabricate(:topic, category: secured_category) } | 
					
						
							| 
									
										
										
										
											2023-05-09 15:30:50 -03:00
										 |  |  |   fab!(:closed_topic) { Fabricate(:topic, closed: true) } | 
					
						
							| 
									
										
										
										
											2023-03-31 11:04:34 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   before { SiteSetting.ai_embeddings_semantic_related_topics_enabled = true } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 16:58:09 -03:00
										 |  |  |   describe "#related_topic_ids_for" do | 
					
						
							| 
									
										
										
										
											2023-05-23 10:43:24 +10:00
										 |  |  |     context "when embeddings do not exist" do | 
					
						
							|  |  |  |       let (:topic) do | 
					
						
							| 
									
										
										
										
											2023-08-02 16:58:09 -03:00
										 |  |  |         Fabricate(:topic).tap { described_class.clear_cache_for(target) } | 
					
						
							| 
									
										
										
										
											2023-05-23 10:43:24 +10:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "queues job only once per 15 minutes" do | 
					
						
							|  |  |  |         results = nil | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect_enqueued_with(job: :generate_embeddings, args: { topic_id: topic.id }) do | 
					
						
							| 
									
										
										
										
											2023-08-02 16:58:09 -03:00
										 |  |  |           results = described_class.related_topic_ids_for(topic) | 
					
						
							| 
									
										
										
										
											2023-05-23 10:43:24 +10:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(results).to eq([]) | 
					
						
							| 
									
										
										
										
											2023-03-31 11:04:34 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-23 10:43:24 +10:00
										 |  |  |         expect_not_enqueued_with(job: :generate_embeddings, args: { topic_id: topic.id }) do | 
					
						
							| 
									
										
										
										
											2023-08-02 16:58:09 -03:00
										 |  |  |           results = described_class.related_topic_ids_for(topic) | 
					
						
							| 
									
										
										
										
											2023-05-23 10:43:24 +10:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(results).to eq([]) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-03-31 11:04:34 +11:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |