A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
RSpec.describe DiscourseAi::Agents::Tools::LockPost do
fab!(:llm_model)
fab!(:bot_user, :admin)
let(:llm) { DiscourseAi::Completions::Llm.proxy(llm_model) }
fab!(:post)
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 post and its locked state without exposing parameter IDs" do
preview = tool(post_id: post.id, locked: true, reason: "Preventing edits")
expect(preview.approval_title).to include(post.topic.title, post.url)
expect(preview.approval_changes).to eq(
[{ label: "Changing status:", before: "Unlocked", after: "Locked" }],
)
expect(preview.approval_parameters).to eq([])
post.update!(locked_by_id: Discourse.system_user.id)
expect(tool(post_id: post.id, locked: false).approval_changes).to eq(
[{ label: "Changing status:", before: "Locked", after: "Unlocked" }],
)
end
it "locks the post when locked is true" do
result = tool(post_id: post.id, locked: true, reason: "Preventing edits").invoke
expect(result[:status]).to eq("success")
expect(post.reload.locked_by_id).to eq((bot_user || Discourse.system_user).id)
end
it "unlocks the post when locked is false" do
post.update!(locked_by_id: Discourse.system_user.id)
result = tool(post_id: post.id, locked: false, reason: "Allowing edits again").invoke
expect(result[:status]).to eq("success")
expect(post.reload.locked_by_id).to be_nil
end
it "returns an error when post is not found" do
result = tool(post_id: -1, locked: true, reason: "Test").invoke
expect(result[:status]).to eq("error")
end
it "returns an error when reason is blank" do
result = tool(post_id: post.id, locked: true, 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(
{ post_id: post.id, locked: true, 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