Assuming you’ve implemented the necessary changes to the schedule_reply
method in the lib/ai_bot/playground.rb
file, the next step is to create an integration test that validates the new behavior of the AI bot. This test will confirm that:
- The AI bot does not reply to direct user replies in an AI bot PM.
- The AI bot or the designated persona correctly replies when the user’s message is intended for it.
Let’s outline an integration test scenario using RSpec, located in spec/lib/modules/ai_bot/playground_spec.rb
, which confirms these behaviors:
require 'rails_helper'
RSpec.describe DiscourseAi::AiBot::Playground do
fab!(:bot_user) { User.find(-110) } # Assuming -110 is a bot's user ID for the sake of the example
fab!(:persona_user) { Fabricate(:user) } # Assuming this user is configured as a persona
fab!(:regular_user) { Fabricate(:user) }
let(:ai_bot_playground) { DiscourseAi::AiBot::Playground }
before do
SiteSetting.ai_bot_enabled = true
# Additional setup as necessary for enabling AI bot, personas, etc.
end
describe '#schedule_reply behavior' do
let!(:pm) do
Fabricate(
:private_message_topic,
user: regular_user,
topic_allowed_users: [
Fabricate.build(:topic_allowed_user, user: regular_user),
Fabricate.build(:topic_allowed_user, user: bot_user),
Fabricate.build(:topic_allowed_user, user: persona_user)
]
)
end
it 'does not reply to a direct reply to a regular user' do
original_post_count = pm.posts.count
# Simulate a post by a regular user directly replying to another regular user
PostCreator.create!(
regular_user,
raw: "Thanks for your input, @#{regular_user.username}",
topic_id: pm.id
)
# Assuming background jobs run synchronously for the sake of this example
expect(pm.reload.posts.count).to eq(original_post_count + 1) # Only the user's new post, no bot reply
end
it 'replies to a direct mention of a persona bot in PM' do
original_post_count = pm.posts.count
# Simulate a post mentioning the persona bot
PostCreator.create!(
regular_user,
raw: "Hey @#{persona_user.username}, can you give me more information?",
topic_id: pm.id
)
# Assuming background jobs run synchronously for the sake of this example
expect(pm.reload.posts.count).to eq(original_post_count + 2) # Includes the new post and bot's (persona's) reply
end
end
end
Key Points in This Example:
- This test suite contains two scenarios. One where a regular user replies to another regular user should not trigger a bot reply. The other, where a regular user mentions a persona, should result in a reply from the bot configured as that persona.
- The
fab!(:bot_user)
and fab!(:persona_user)
lines presume specific user setups. You’ll need to adjust these to match your actual bot and persona user configurations.
- The test assumes that the background jobs, which typically handle bot replies, run synchronously or are otherwise triggered within the scope of the test. Depending on your test environment setup, you might need to tweak this assumption (e.g., by explicitly running jobs or using tools like the
sidekiq/testing
inline mode for Sidekiq).
Next Steps:
- Run the Test: Execute this test suite to verify the implementation meets the expected behavior. Adjust the test or implementation as necessary based on the outcomes observed.
- Expand Test Coverage: Consider adding more tests to cover additional scenarios or edge cases that could arise from your specific implementation details.
- Review and Refine: Analyze test results for potential improvements or optimizations in both the test and the AI bot functionality to ensure robust and predictable behavior.