99
1010 fab! ( :target ) { Fabricate ( :topic ) }
1111
12- def stub_semantic_search_with ( results )
13- DiscourseAi ::Embeddings ::VectorRepresentations :: BgeLargeEn
14- . any_instance
15- . expects ( :symmetric_topics_similarity_search )
16- . returns ( results . concat ( [ target . id ] ) )
12+ def seed_embeddings ( topics )
13+ schema = DiscourseAi ::Embeddings ::Schema . for ( Topic )
14+
15+ embeddings = [ 1 ] * 1024
16+ ( topics << target ) . each { | t | schema . store ( t , embeddings , "digest" ) }
1717 end
1818
1919 after { DiscourseAi ::Embeddings ::SemanticRelated . clear_cache_for ( target ) }
2020
2121 context "when the semantic search returns an unlisted topic" do
2222 fab! ( :unlisted_topic ) { Fabricate ( :topic , visible : false ) }
2323
24- before { stub_semantic_search_with ( [ unlisted_topic . id ] ) }
24+ before { seed_embeddings ( [ unlisted_topic ] ) }
2525
2626 it "filters it out" do
2727 expect ( topic_query . list_semantic_related_topics ( target ) . topics ) . to be_empty
@@ -31,7 +31,7 @@ def stub_semantic_search_with(results)
3131 context "when the semantic search returns a private topic" do
3232 fab! ( :private_topic ) { Fabricate ( :private_message_topic ) }
3333
34- before { stub_semantic_search_with ( [ private_topic . id ] ) }
34+ before { seed_embeddings ( [ private_topic ] ) }
3535
3636 it "filters it out" do
3737 expect ( topic_query . list_semantic_related_topics ( target ) . topics ) . to be_empty
@@ -43,7 +43,7 @@ def stub_semantic_search_with(results)
4343 fab! ( :category ) { Fabricate ( :private_category , group : group ) }
4444 fab! ( :secured_category_topic ) { Fabricate ( :topic , category : category ) }
4545
46- before { stub_semantic_search_with ( [ secured_category_topic . id ] ) }
46+ before { seed_embeddings ( [ secured_category_topic ] ) }
4747
4848 it "filters it out" do
4949 expect ( topic_query . list_semantic_related_topics ( target ) . topics ) . to be_empty
@@ -63,7 +63,7 @@ def stub_semantic_search_with(results)
6363
6464 before do
6565 SiteSetting . ai_embeddings_semantic_related_include_closed_topics = false
66- stub_semantic_search_with ( [ closed_topic . id ] )
66+ seed_embeddings ( [ closed_topic ] )
6767 end
6868
6969 it "filters it out" do
@@ -80,7 +80,7 @@ def stub_semantic_search_with(results)
8080 category_id : category . id ,
8181 notification_level : CategoryUser . notification_levels [ :muted ] ,
8282 )
83- stub_semantic_search_with ( [ topic . id ] )
83+ seed_embeddings ( [ topic ] )
8484 expect ( topic_query . list_semantic_related_topics ( target ) . topics ) . not_to include ( topic )
8585 end
8686 end
@@ -91,11 +91,7 @@ def stub_semantic_search_with(results)
9191 fab! ( :normal_topic_3 ) { Fabricate ( :topic ) }
9292 fab! ( :closed_topic ) { Fabricate ( :topic , closed : true ) }
9393
94- before do
95- stub_semantic_search_with (
96- [ closed_topic . id , normal_topic_1 . id , normal_topic_2 . id , normal_topic_3 . id ] ,
97- )
98- end
94+ before { seed_embeddings ( [ closed_topic , normal_topic_1 , normal_topic_2 , normal_topic_3 ] ) }
9995
10096 it "filters it out" do
10197 expect ( topic_query . list_semantic_related_topics ( target ) . topics ) . to eq (
@@ -117,7 +113,7 @@ def stub_semantic_search_with(results)
117113 fab! ( :included_topic ) { Fabricate ( :topic ) }
118114 fab! ( :excluded_topic ) { Fabricate ( :topic ) }
119115
120- before { stub_semantic_search_with ( [ included_topic . id , excluded_topic . id ] ) }
116+ before { seed_embeddings ( [ included_topic , excluded_topic ] ) }
121117
122118 let ( :modifier_block ) { Proc . new { |query | query . where . not ( id : excluded_topic . id ) } }
123119
0 commit comments