Skip to content

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

DEV: Remove duplication from AI helper assistant specs

Régis Hanol4346bd2f58f2
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
index 0f41e5da353..154924ae50e 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
@@ -3,13 +3,27 @@
RSpec.describe DiscourseAi::AiHelper::Assistant do
subject(:assistant) { described_class.new }
- fab!(:user)
+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:empty_locale_user) { Fabricate(:user, locale: "") }
before do
enable_current_plugin
assign_fake_provider_to(:ai_default_llm_model)
- Group.refresh_automatic_groups!
+ end
+
+ def stub_streamed_completion(deltas)
+ body =
+ deltas
+ .map { |delta| { choices: [{ index: 0, delta: { content: delta } }] } }
+ .push({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })
+ .map { |event| "data: #{event.to_json}\n\n" }
+ .join
+ body << "data: [DONE]\n\n"
+
+ stub_request(:post, "https://api.openai.com/v1/chat/completions").to_return(
+ status: 200,
+ body: body,
+ )
end
let(:english_text) { <<~STRING }
@@ -67,8 +81,11 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
)
end
- it "returns all available prompts" do
+ it "returns all available prompts with their locations" do
prompts = assistant.available_prompts(user)
+ names_in = ->(location) do
+ prompts.select { |prompt| prompt[:location].include?(location) }.map { |p| p[:name] }
+ end
expect(prompts.map { |p| p[:name] }).to contain_exactly(
"translate",
@@ -78,30 +95,14 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
"explain",
"replace_dates",
)
- end
-
- it "returns all prompts to be shown in the composer" do
- prompts = assistant.available_prompts(user)
- filtered_prompts = prompts.select { |prompt| prompt[:location].include?("composer") }
-
- expect(filtered_prompts.map { |p| p[:name] }).to contain_exactly(
+ expect(names_in.("composer")).to contain_exactly(
"translate",
"generate_titles",
"proofread",
"markdown_table",
"replace_dates",
)
- end
-
- it "returns all prompts to be shown in the post menu" do
- prompts = assistant.available_prompts(user)
- filtered_prompts = prompts.select { |prompt| prompt[:location].include?("post") }
-
- expect(filtered_prompts.map { |p| p[:name] }).to contain_exactly(
- "translate",
- "explain",
- "proofread",
- )
+ expect(names_in.("post")).to contain_exactly("translate", "explain", "proofread")
end
it "does not raise an error when effective_locale does not exactly match keys in LocaleSiteSetting" do
@@ -198,16 +199,6 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
context "when handling edge cases" do
before { DiscourseAi::AiHelper::Assistant.clear_prompt_cache! }
- it "does not include illustrate_post when no image generation tools are enabled" do
- # Disable all image generation tools
- AiTool.where(is_image_generation_tool: true).update_all(enabled: false)
- DiscourseAi::AiHelper::Assistant.clear_prompt_cache!
-
- prompts = assistant.available_prompts(user)
-
- expect(prompts.map { |p| p[:name] }).not_to include("illustrate_post")
- end
-
it "handles tool discovery returning empty array" do
# Create a tool that looks like an image tool but isn't
AiTool.create!(
@@ -236,8 +227,6 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
DiscourseAi::AiHelper::Assistant.clear_prompt_cache!
- # Should not raise error, just exclude illustrate_post
- expect { assistant.available_prompts(user) }.not_to raise_error
prompts = assistant.available_prompts(user)
expect(prompts.map { |p| p[:name] }).not_to include("illustrate_post")
end
@@ -293,6 +282,14 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
end
describe "#generate_and_send_prompt" do
+ fab!(:plain_text_agent) do
+ Fabricate(
+ :ai_agent,
+ response_format: nil,
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
+ end
+
context "when using a prompt that returns text" do
let(:mode) { described_class::TRANSLATE }
@@ -313,13 +310,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
context "when the agent is not using structured outputs" do
it "still works" do
- regular_agent =
- Fabricate(
- :ai_agent,
- response_format: nil,
- allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
- )
- SiteSetting.ai_helper_translator_agent = regular_agent.id
+ SiteSetting.ai_helper_translator_agent = plain_text_agent.id
response =
DiscourseAi::Completions::Llm.with_prepared_responses([english_text]) do
@@ -347,20 +338,12 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
end
it "returns an array with each title" do
- expected = [
- "The solitary horse",
- "The horse etched in gold",
- "A horse's infinite journey",
- "A horse lost in time",
- "A horse's last ride",
- ]
-
response =
DiscourseAi::Completions::Llm.with_prepared_responses([titles]) do
assistant.generate_and_send_prompt(mode, english_text, user)
end
- expect(response[:suggestions]).to contain_exactly(*expected)
+ expect(response[:suggestions]).to eq(titles[:output])
end
it "returns clean suggestions when the model streams the JSON across many partial chunks" do
@@ -399,19 +382,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
"\n",
"}",
]
-
- body =
- deltas
- .map { |delta| { choices: [{ index: 0, delta: { content: delta } }] } }
- .push({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })
- .map { |event| "data: #{event.to_json}\n\n" }
- .join
- body << "data: [DONE]\n\n"
-
- stub_request(:post, "https://api.openai.com/v1/chat/completions").to_return(
- status: 200,
- body: body,
- )
+ stub_streamed_completion(deltas)
response = streaming_assistant.generate_and_send_prompt(mode, english_text, user)
@@ -430,12 +401,6 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
end
it "returns no suggestions when the title agent replies with text instead of a list" do
- plain_text_agent =
- Fabricate(
- :ai_agent,
- response_format: nil,
- allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
- )
SiteSetting.ai_helper_title_suggestions_agent = plain_text_agent.id
response =
@@ -465,19 +430,7 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
"2-pass on every message.",
"\"}",
]
-
- body =
- deltas
- .map { |delta| { choices: [{ index: 0, delta: { content: delta } }] } }
- .push({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })
- .map { |event| "data: #{event.to_json}\n\n" }
- .join
- body << "data: [DONE]\n\n"
-
- stub_request(:post, "https://api.openai.com/v1/chat/completions").to_return(
- status: 200,
- body: body,
- )
+ stub_streamed_completion(deltas)
response = streaming_assistant.generate_and_send_prompt(mode, english_text, user)
diff --git a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
index 68694d915a6..ffc08c1211c 100644
--- a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb
@@ -11,6 +11,44 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
SiteSetting.ai_helper_enabled = true
end
+ shared_context "with an image-generating post illustrator" do
+ let(:upload) do
+ Fabricate(
+ :upload,
+ sha1: Upload.sha1_from_short_url("upload://test123"),
+ original_filename: "test.png",
+ )
+ end
+
+ let(:post_illustrator_agent) do
+ AiAgent.find_by(id: SiteSetting.ai_helper_post_illustrator_agent)
+ end
+
+ before do
+ image_tool =
+ Fabricate(
+ :ai_tool,
+ tool_name: "test_image_generator",
+ parameters: [{ name: "prompt", type: "string", required: true }],
+ script: <<~JS,
+ function invoke(params) {
+ const image = upload.create("test.png", "base64data");
+ chain.setCustomRaw(`![test](${image.short_url})`);
+ return { result: "success" };
+ }
+ JS
+ created_by_id: user.id,
+ is_image_generation_tool: true,
+ )
+ post_illustrator_agent.update_columns(system: false)
+ post_illustrator_agent.update!(tools: [["custom-#{image_tool.id}", nil, true]])
+
+ allow_any_instance_of(DiscourseAi::Agents::Bot).to receive(:reply) do |_bot, _context, &block|
+ block.call("", "![test](#{upload.short_url})", :partial_invoke)
+ end
+ end
+ end
+
describe "#stream_suggestion" do
before do
Jobs.run_immediately!
@@ -115,49 +153,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
context "when mode is illustrate_post" do
- let(:image_tool) do
- AiTool.create!(
- name: "Test Image Generator",
- tool_name: "test_image_generator",
- description: "Generates test images",
- summary: "Test image generation",
- parameters: [{ name: "prompt", type: "string", required: true }],
- script: <<~JS,
- function invoke(params) {
- const image = upload.create("test.png", "base64data");
- chain.setCustomRaw(`![test](${image.short_url})`);
- return { result: "success" };
- }
- JS
- created_by_id: user.id,
- enabled: true,
- is_image_generation_tool: true,
- )
- end
-
- let(:upload) do
- Fabricate(
- :upload,
- sha1: Upload.sha1_from_short_url("upload://test123"),
- original_filename: "test.png",
- )
- end
-
- let(:post_illustrator_agent) do
- AiAgent.find_by(id: SiteSetting.ai_helper_post_illustrator_agent)
- end
-
- before do
- image_tool
- post_illustrator_agent.update_columns(system: false)
- post_illustrator_agent.update!(tools: [["custom-#{image_tool.id}", nil, true]])
-
- allow_any_instance_of(DiscourseAi::Agents::Bot).to receive(
- :reply,
- ) do |_bot, _context, &block|
- block.call("", "![test](#{upload.short_url})", :partial_invoke)
- end
- end
+ include_context "with an image-generating post illustrator"
it "suggests thumbnails" do
sign_in(user)
@@ -519,49 +515,7 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
context "when PostIllustrator agent has image generation tool" do
- let(:image_tool) do
- AiTool.create!(
- name: "Test Image Generator",
- tool_name: "test_image_generator",
- description: "Generates test images",
- summary: "Test image generation",
- parameters: [{ name: "prompt", type: "string", required: true }],
- script: <<~JS,
- function invoke(params) {
- const image = upload.create("test.png", "base64data");
- chain.setCustomRaw(`![test](${image.short_url})`);
- return { result: "success" };
- }
- JS
- created_by_id: user.id,
- enabled: true,
- is_image_generation_tool: true,
- )
- end
-
- let(:upload) do
- Fabricate(
- :upload,
- sha1: Upload.sha1_from_short_url("upload://test123"),
- original_filename: "test.png",
- )
- end
-
- let(:post_illustrator_agent) do
- AiAgent.find_by(id: SiteSetting.ai_helper_post_illustrator_agent)
- end
-
- before do
- image_tool
- post_illustrator_agent.update_columns(system: false) # Allow editing for test
- post_illustrator_agent.update!(tools: [["custom-#{image_tool.id}", nil, true]])
-
- allow_any_instance_of(DiscourseAi::Agents::Bot).to receive(
- :reply,
- ) do |_bot, _context, &block|
- block.call("", "![test](#{upload.short_url})", :partial_invoke)
- end
- end
+ include_context "with an image-generating post illustrator"
it "successfully generates thumbnails" do
post "/discourse-ai/ai-helper/suggest",
@@ -642,22 +596,13 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
],
}
end
- let(:title_suggestions_array) do
- [
- "What are your favourite fruits?",
- "Love for fruits",
- "Fruits are amazing",
- "Favourite fruit list",
- "Fruit share topic",
- ]
- end
it "returns title suggestions based on all topic post context" do
DiscourseAi::Completions::Llm.with_prepared_responses([title_suggestions]) do
post "/discourse-ai/ai-helper/suggest_title", params: { topic_id: topic.id }
expect(response.status).to eq(200)
- expect(response.parsed_body["suggestions"]).to eq(title_suggestions_array)
+ expect(response.parsed_body["suggestions"]).to eq(title_suggestions[:output])
end
end
@@ -682,22 +627,13 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
],
}
end
- let(:title_suggestions_array) do
- [
- "Apples - the best fruit",
- "Why apples are great",
- "Apples are the best fruit",
- "My love for apples",
- "I love apples",
- ]
- end
it "returns title suggestions based on the input text" do
DiscourseAi::Completions::Llm.with_prepared_responses([title_suggestions]) do
post "/discourse-ai/ai-helper/suggest_title", params: { text: post_1.raw }
expect(response.status).to eq(200)
- expect(response.parsed_body["suggestions"]).to eq(title_suggestions_array)
+ expect(response.parsed_body["suggestions"]).to eq(title_suggestions[:output])
end
end
end