Skip to content

A platform for community discussion. Free, open, simple.

6.0 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseAi::Embeddings::SemanticSearch do
  subject(:semantic_search) { described_class.new(Guardian.new(user)) }

  fab!(:post)
  fab!(:user)
  fab!(:vector_def, :embedding_definition)

  let(:query) { "test_query" }

  before do
    enable_current_plugin
    SiteSetting.ai_embeddings_selected_model = vector_def.id
    assign_fake_provider_to(:ai_default_llm_model)
  end

  describe "#search_for_topics" do
    let(:hypothetical_post) { "This is an hypothetical post generated from the keyword test_query" }
    let(:hyde_embedding) { [0.049382] * vector_def.dimensions }

    before { EmbeddingsGenerationStubs.hugging_face_service(hypothetical_post, hyde_embedding) }

    after { described_class.clear_cache_for(query) }

    def insert_candidate(candidate, embedding = hyde_embedding)
      DiscourseAi::Embeddings::Schema.for(Topic).store(candidate, embedding, "digest")
    end

    def trigger_search(query, **options)
      DiscourseAi::Completions::Llm.with_prepared_responses([hypothetical_post]) do
        semantic_search.search_for_topics(query, **options)
      end
    end

    it "returns the first post of a topic included in the asymmetric search results" do
      insert_candidate(post.topic)

      posts = trigger_search(query)

      expect(posts).to contain_exactly(post)
    end

    context "with HYDE enabled" do
      it "uses HYDE embeddings and not asymmetric ones" do
        insert_candidate(post.topic)

        vector = DiscourseAi::Embeddings::Vector.instance
        allow(vector).to receive(:vector_from).and_call_original

        posts = trigger_search(query)

        expect(vector).not_to have_received(:vector_from).with(query, true)
        expect(posts).to contain_exactly(post)
      end
    end

    describe "applies different scopes to the candidates" do
      context "when the topic is not visible" do
        it "returns an empty list" do
          post.topic.update!(visible: false)
          insert_candidate(post.topic)

          posts = trigger_search(query)

          expect(posts).to be_empty
        end
      end

      context "when the post is not public" do
        it "returns an empty list" do
          pm_post = Fabricate(:private_message_post)
          insert_candidate(pm_post.topic)

          posts = trigger_search(query)

          expect(posts).to be_empty
        end
      end

      context "when personal messages are requested" do
        it "returns no results for an anonymous user" do
          insert_candidate(Fabricate(:private_message_post).topic)

          posts =
            described_class.new(Guardian.new(nil)).search_for_topics(query, private_messages: true)

          expect(posts).to be_empty
        end

        it "returns only personal messages the user can see" do
          visible_pm = Fabricate(:private_message_post, recipient: user)
          hidden_pm = Fabricate(:private_message_post)
          insert_candidate(visible_pm.topic)
          insert_candidate(hidden_pm.topic)

          posts = trigger_search(query, private_messages: true)

          expect(posts).to contain_exactly(visible_pm)
        end

        it "limits vector ranking to personal messages the user can see" do
          SiteSetting.search_page_size = 0
          visible_pm = Fabricate(:private_message_post, recipient: user)
          distant_embedding = [1.0] + Array.new(vector_def.dimensions - 1, 0.0)
          insert_candidate(visible_pm.topic, distant_embedding)

          9.times { insert_candidate(Fabricate(:private_message_post).topic) }

          posts = trigger_search(query, private_messages: true)

          expect(posts).to contain_exactly(visible_pm)
        end
      end

      context "when public topics are requested" do
        it "excludes personal messages before vector ranking" do
          SiteSetting.search_page_size = 0
          distant_embedding = [1.0] + Array.new(vector_def.dimensions - 1, 0.0)
          insert_candidate(post.topic, distant_embedding)

          9.times { insert_candidate(Fabricate(:private_message_post).topic) }

          posts = trigger_search(query)

          expect(posts).to contain_exactly(post)
        end
      end

      context "when the post type is not visible" do
        it "returns an empty list" do
          post.update!(post_type: Post.types[:whisper])
          insert_candidate(post.topic)

          posts = trigger_search(query)

          expect(posts).to be_empty
        end
      end

      context "when the post is not the first post in the topic" do
        it "returns an empty list" do
          reply = Fabricate(:reply)
          reply.topic.first_post.trash!
          insert_candidate(reply.topic)

          posts = trigger_search(query)

          expect(posts).to be_empty
        end
      end

      context "when the post is not a candidate" do
        it "doesn't include it in the results" do
          post_2 = Fabricate(:post)
          insert_candidate(post.topic)

          posts = trigger_search(query)

          expect(posts).not_to include(post_2)
        end
      end

      context "when the post belongs to a secured category" do
        fab!(:group)
        fab!(:private_category) { Fabricate(:private_category, group: group) }

        before do
          post.topic.update!(category: private_category)
          insert_candidate(post.topic)
        end

        it "returns an empty list" do
          posts = trigger_search(query)

          expect(posts).to be_empty
        end

        it "returns the results if the user has access to the category" do
          group.add(user)

          posts = trigger_search(query)

          expect(posts).to contain_exactly(post)
        end

        context "while searching as anon" do
          it "returns an empty list" do
            posts =
              DiscourseAi::Completions::Llm.with_prepared_responses([hypothetical_post]) do
                described_class.new(Guardian.new(nil)).search_for_topics(query)
              end

            expect(posts).to be_empty
          end
        end
      end
    end
  end
end