2023-05-05 14:28:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe DiscourseAi::AiBot::BotController do
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:user)
|
2024-04-15 09:22:06 -04:00
|
|
|
fab!(:pm_topic) { Fabricate(:private_message_topic) }
|
|
|
|
fab!(:pm_post) { Fabricate(:post, topic: pm_topic) }
|
|
|
|
|
2023-05-16 13:38:21 -04:00
|
|
|
before { sign_in(user) }
|
|
|
|
|
2024-04-15 09:22:06 -04:00
|
|
|
describe "#show_debug_info" do
|
|
|
|
before do
|
|
|
|
SiteSetting.ai_bot_enabled = true
|
|
|
|
SiteSetting.discourse_ai_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a 403 when the user cannot debug the AI bot conversation" do
|
|
|
|
get "/discourse-ai/ai-bot/post/#{pm_post.id}/show-debug-info"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns debug info if the user can debug the AI bot conversation" do
|
|
|
|
user = pm_topic.topic_allowed_users.first.user
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
AiApiAuditLog.create!(
|
|
|
|
post_id: pm_post.id,
|
|
|
|
provider_id: 1,
|
|
|
|
topic_id: pm_topic.id,
|
|
|
|
raw_request_payload: "request",
|
|
|
|
raw_response_payload: "response",
|
|
|
|
request_tokens: 1,
|
|
|
|
response_tokens: 2,
|
|
|
|
)
|
|
|
|
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
SiteSetting.ai_bot_debugging_allowed_groups = user.groups.first.id.to_s
|
2023-05-05 14:28:31 -04:00
|
|
|
|
2024-04-15 09:22:06 -04:00
|
|
|
get "/discourse-ai/ai-bot/post/#{pm_post.id}/show-debug-info"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(response.parsed_body["request_tokens"]).to eq(1)
|
|
|
|
expect(response.parsed_body["response_tokens"]).to eq(2)
|
|
|
|
expect(response.parsed_body["raw_request_payload"]).to eq("request")
|
|
|
|
expect(response.parsed_body["raw_response_payload"]).to eq("response")
|
|
|
|
|
|
|
|
post2 = Fabricate(:post, topic: pm_topic)
|
|
|
|
|
|
|
|
# return previous post if current has no debug info
|
|
|
|
get "/discourse-ai/ai-bot/post/#{post2.id}/show-debug-info"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["request_tokens"]).to eq(1)
|
|
|
|
expect(response.parsed_body["response_tokens"]).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#stop_streaming_response" do
|
2023-05-05 14:28:31 -04:00
|
|
|
let(:redis_stream_key) { "gpt_cancel:#{pm_post.id}" }
|
|
|
|
|
|
|
|
before { Discourse.redis.setex(redis_stream_key, 60, 1) }
|
|
|
|
|
|
|
|
it "returns a 403 when the user cannot see the PM" do
|
|
|
|
post "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the key using to track the streaming" do
|
|
|
|
sign_in(pm_topic.topic_allowed_users.first.user)
|
|
|
|
|
|
|
|
post "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(Discourse.redis.get(redis_stream_key)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2023-05-16 13:38:21 -04:00
|
|
|
|
|
|
|
describe "#show_bot_username" do
|
|
|
|
it "returns the username_lower of the selected bot" do
|
2024-06-18 13:32:14 -04:00
|
|
|
gpt_35_bot = Fabricate(:llm_model, name: "gpt-3.5-turbo")
|
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
SiteSetting.ai_bot_enabled = true
|
2024-06-18 13:32:14 -04:00
|
|
|
toggle_enabled_bots(bots: [gpt_35_bot])
|
|
|
|
|
|
|
|
expected_username =
|
|
|
|
DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo").username_lower
|
2023-05-16 13:38:21 -04:00
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
get "/discourse-ai/ai-bot/bot-username", params: { username: gpt_35_bot.name }
|
2023-05-16 13:38:21 -04:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["bot_username"]).to eq(expected_username)
|
|
|
|
end
|
|
|
|
end
|
2023-05-05 14:28:31 -04:00
|
|
|
end
|