Skip to content

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

4.9 KB
Raw Blame
# frozen_string_literal: true

RSpec.describe DiscourseAi::Agents::Tools::MovePosts do
  fab!(:llm_model)
  fab!(:bot_user, :admin)
  let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }
  fab!(:topic)
  fab!(:post1, :post) { Fabricate(:post, topic: topic) }
  fab!(:post2, :post) { Fabricate(:post, topic: topic) }
  fab!(:destination_topic, :topic)

  before do
    enable_current_plugin
    SiteSetting.ai_bot_enabled = true
  end

  def tool(params = nil, **kwargs)
    params ||= kwargs
    described_class.new(params, bot_user: bot_user, llm: llm, context: context)
  end

  let(:context) { DiscourseAi::Agents::BotContext.new }

  it "previews the source, destination and selected post numbers" do
    preview =
      tool(
        topic_id: topic.id,
        post_ids: [post2.id],
        destination_topic_id: destination_topic.id,
        reason: "Organizing",
      )

    expect(preview.approval_title).to include(topic.title, topic.url)
    expect(preview.approval_changes).to eq(
      [{ label: "Changing destination:", before: topic.title, after: destination_topic.title }],
    )
    expect(preview.approval_parameters).to eq(
      [{ label: "Posts", value: "Post ##{post2.post_number}" }],
    )
  end

  it "previews a new topic and its category" do
    category = Fabricate(:category)
    preview =
      tool(
        topic_id: topic.id,
        post_ids: [post2.id],
        new_title: "A separate discussion",
        category_id: category.id,
      )

    expect(preview.approval_changes.first[:after]).to eq("A separate discussion")
    expect(preview.approval_parameters).to eq(
      [
        { label: "Posts", value: "Post ##{post2.post_number}" },
        { label: "Category", value: category.name, color: category.color },
      ],
    )
  end

  describe "#invoke" do
    fab!(:user, :trust_level_4)
    let(:context) { DiscourseAi::Agents::BotContext.new(user: user) }

    it "leaves posts unchanged when the user can only reply in the destination category" do
      category = Fabricate(:category)
      category.set_permissions(everyone: :reply)
      category.save!

      expect do
        expect do
          tool(
            topic_id: topic.id,
            post_ids: [post2.id],
            new_title: "A separate discussion",
            category_id: category.id,
            reason: "Organizing the discussion",
          ).invoke
        end.to raise_error(Discourse::InvalidAccess)
      end.not_to change { [Topic.count, Post.count, post2.reload.topic_id] }
    end
  end

  it "moves posts to an existing topic" do
    result =
      tool(
        topic_id: topic.id,
        post_ids: [post2.id],
        destination_topic_id: destination_topic.id,
        reason: "Off-topic posts",
      ).invoke

    expect(result[:status]).to eq("success")
    expect(result[:destination_topic_id]).to eq(destination_topic.id)
    expect(post2.reload.topic_id).to eq(destination_topic.id)
  end

  it "moves posts to a new topic" do
    result =
      tool(
        topic_id: topic.id,
        post_ids: [post2.id],
        new_title: "Split discussion about something else",
        reason: "Splitting off-topic discussion",
      ).invoke

    expect(result[:status]).to eq("success")
    expect(result[:destination_topic_id]).to be_present
    expect(post2.reload.topic_id).to eq(result[:destination_topic_id])
  end

  it "returns an error when neither destination_topic_id nor new_title is provided" do
    result = tool(topic_id: topic.id, post_ids: [post2.id], reason: "Test").invoke

    expect(result[:status]).to eq("error")
  end

  it "returns an error when topic is not found" do
    result =
      tool(
        topic_id: -1,
        post_ids: [post2.id],
        destination_topic_id: destination_topic.id,
        reason: "Test",
      ).invoke

    expect(result[:status]).to eq("error")
  end

  it "returns an error when post_ids is empty" do
    result =
      tool(
        topic_id: topic.id,
        post_ids: [],
        destination_topic_id: destination_topic.id,
        reason: "Test",
      ).invoke

    expect(result[:status]).to eq("error")
  end

  it "returns an error when reason is blank" do
    result =
      tool(
        topic_id: topic.id,
        post_ids: [post2.id],
        destination_topic_id: destination_topic.id,
        reason: " ",
      ).invoke

    expect(result[:status]).to eq("error")
  end

  it "returns an error when context user lacks permission" do
    regular_user = Fabricate(:user, trust_level: TrustLevel[0])
    ctx = DiscourseAi::Agents::BotContext.new(user: regular_user)
    t =
      described_class.new(
        {
          topic_id: topic.id,
          post_ids: [post2.id],
          destination_topic_id: destination_topic.id,
          reason: "test",
        },
        bot_user: bot_user,
        llm: llm,
        context: ctx,
      )
    result = t.invoke

    expect(result[:status]).to eq("error")
    expect(result[:error]).to include("not allowed")
  end
end