Skip to content

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

7.2 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseAi::Summarization::Strategies::TopicSummary do
  subject(:topic_summary) { described_class.new(topic) }

  fab!(:topic) { Fabricate(:topic, highest_post_number: 25) }
  fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
  fab!(:post_2) { Fabricate(:post, topic: topic, post_number: 2) }

  before { enable_current_plugin }

  describe "#targets_data" do
    shared_examples "includes only public-visible topics" do
      it "only includes visible posts" do
        post_2.update!(hidden: true)

        post_numbers = topic_summary.targets_data.map { |c| c[:id] }

        expect(post_numbers).to contain_exactly(1)
      end

      it "doesn't include posts without users" do
        post_2.update!(user_id: nil)

        post_numbers = topic_summary.targets_data.map { |c| c[:id] }

        expect(post_numbers).to contain_exactly(1)
      end

      it "doesn't include whispers" do
        post_2.update!(post_type: Post.types[:whisper])

        post_numbers = topic_summary.targets_data.map { |c| c[:id] }

        expect(post_numbers).to contain_exactly(1)
      end
    end

    context "when the topic has a best replies summary" do
      before { topic.update(has_summary: true) }

      it_behaves_like "includes only public-visible topics"
    end

    context "when the topic doesn't have a best replies summary" do
      before { topic.update(has_summary: false) }

      it_behaves_like "includes only public-visible topics"
    end

    context "when the topic has embed content cached" do
      it "embed content is used instead of the raw text" do
        topic_embed =
          Fabricate(
            :topic_embed,
            topic: topic,
            embed_content_cache: "<p>hello world new post :D</p>",
          )

        content = topic_summary.targets_data
        op_content = content.first[:text]

        expect(op_content).to include(topic_embed.embed_content_cache)
      end
    end

    context "when enable_names enabled and prioritize_username_in_ux disabled" do
      fab!(:user) { Fabricate(:user, name: "test") }

      it "includes the name" do
        SiteSetting.enable_names = true
        SiteSetting.prioritize_username_in_ux = false

        post_1.update!(user: user)

        content = topic_summary.targets_data
        poster_name = content.first[:poster]

        expect(poster_name).to eq("test")
      end
    end

    context "when enable_names enabled and prioritize_username_in_ux enabled" do
      fab!(:user) { Fabricate(:user, username: "test") }

      it "includes the username" do
        SiteSetting.enable_names = true
        SiteSetting.prioritize_username_in_ux = true

        post_1.update!(user: user)

        content = topic_summary.targets_data
        poster_name = content.first[:poster]

        expect(poster_name).to eq("test")
      end
    end

    context "when a topic without a best replies summary has many replies" do
      before do
        topic.update!(has_summary: false)
        (3..62).each { |post_number| Fabricate(:post, topic:, post_number:, user: post_2.user) }
        topic.posts.update_all(score: 0)
        topic.posts.where(post_number: 6..55).update_all(score: 10)
      end

      it "selects the opening, highest-scored, and latest posts in a single query" do
        queries = track_sql_queries { topic_summary.targets_data }

        expect(queries.grep(/FROM "posts"/).size).to eq(1)
        expect(topic_summary.targets_data.map { |content| content[:id] }).to eq([*1..55, *58..62])
      end

      it "still ranks unscored posts first, as Postgres sorts NULL scores first" do
        topic.posts.where(post_number: 56).update_all(score: nil)

        expect(topic_summary.targets_data.map { |content| content[:id] }).to include(56)
      end
    end
  end

  describe "#summary_fingerprint" do
    it "uses selected post numbers to compute the same sha as targets_data ids" do
      fingerprint = topic_summary.summary_fingerprint
      expected_sha =
        AiSummary.build_sha(topic_summary.targets_data.map { |content| content[:id] }.join)

      expect(fingerprint[:original_content_sha]).to eq(expected_sha)
    end

    it "tracks the latest post version timestamp in the selected set" do
      post_1.update!(last_version_at: 10.minutes.ago)
      post_2.update!(last_version_at: 2.minutes.ago)

      fingerprint = topic_summary.summary_fingerprint

      expect(fingerprint[:latest_version_at].to_i).to eq(post_2.last_version_at.to_i)
    end
  end

  describe "#as_llm_messages" do
    let(:contents) do
      [{ id: 1, poster: "user1", text: "First post content", last_version_at: Time.now }]
    end

    it "includes the topic title in the message" do
      topic.title = "Test Topic Title"

      messages = topic_summary.as_llm_messages(contents)
      content = messages.first[:content]

      expect(content).to include("The discussion title is: Test Topic Title")
    end

    it "requests output in the selected locale" do
      localized_summary = described_class.new(topic, locale: "he")

      message_content = localized_summary.as_llm_messages(contents).first[:content]

      expect(localized_summary.locale).to eq("he")
      expect(message_content).to include(
        "Write the summary in Hebrew (עברית), regardless of the language used in the input.",
      )
      expect(message_content).not_to include("required output locale")
    end

    context "when topic has a category" do
      fab!(:category) { Fabricate(:category, name: "Test Category") }

      it "includes the category name in the message" do
        topic.category = category

        messages = topic_summary.as_llm_messages(contents)
        content = messages.first[:content]

        expect(content).to include("Category: Test Category")
      end
    end

    context "when topic has tags" do
      fab!(:tag1) { Fabricate(:tag, name: "tag1") }
      fab!(:tag2) { Fabricate(:tag, name: "tag2") }

      it "includes the tag names in the message" do
        topic.tags = [tag1, tag2]

        messages = topic_summary.as_llm_messages(contents)
        content = messages.first[:content]

        expect(content).to include("Tags: tag1, tag2")
      end

      context "with hidden tags" do
        fab!(:hidden_tag) { Fabricate(:tag, name: "hidden") }

        before do
          Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
          topic.tags = [tag1, hidden_tag, tag2]
        end

        it "excludes hidden tags from summaries (summaries are cached and shared)" do
          # Summaries are cached and shared across all users, so hidden tags should never appear
          # regardless of who generates them
          messages = topic_summary.as_llm_messages(contents)
          content = messages.first[:content]

          expect(content).to include("Tags: tag1, tag2")
          expect(content).not_to include("hidden")
        end
      end
    end

    context "when topic has no category or tags" do
      it "doesn't include category or tags in the message" do
        topic.category = nil
        topic.tags = []

        messages = topic_summary.as_llm_messages(contents)
        content = messages.first[:content]

        expect(content).not_to include("Category:")
        expect(content).not_to include("Tags:")
      end
    end
  end
end