2023-10-23 10:41:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
RSpec.describe "AI Post helper", type: :system, js: true do
|
2023-10-23 10:41:36 -04:00
|
|
|
fab!(:user) { Fabricate(:admin) }
|
|
|
|
fab!(:non_member_group) { Fabricate(:group) }
|
2024-03-05 10:48:28 -05:00
|
|
|
fab!(:topic)
|
2023-10-23 10:41:36 -04:00
|
|
|
fab!(:post) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
raw:
|
|
|
|
"I like to eat pie. It is a very good dessert. Some people are wasteful by throwing pie at others but I do not do that. I always eat the pie.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
fab!(:post_2) do
|
2023-11-27 07:33:31 -05:00
|
|
|
Fabricate(:post, topic: topic, raw: "La lluvia en España se queda principalmente en el avión.")
|
2023-10-23 10:41:36 -04:00
|
|
|
end
|
2023-12-14 22:30:52 -05:00
|
|
|
fab!(:post_3) do
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
raw: "The Toyota Supra delivrs 382 horsepwr makin it a very farst car.",
|
|
|
|
)
|
|
|
|
end
|
2023-10-23 10:41:36 -04:00
|
|
|
let(:topic_page) { PageObjects::Pages::Topic.new }
|
2024-07-10 14:01:05 -04:00
|
|
|
let(:post_ai_helper) { PageObjects::Components::AiPostHelperMenu.new }
|
2023-12-14 22:30:52 -05:00
|
|
|
let(:fast_editor) { PageObjects::Components::FastEditor.new }
|
2023-10-23 10:41:36 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user)
|
2024-06-19 17:01:35 -04:00
|
|
|
assign_fake_provider_to(:ai_helper_model)
|
2024-08-12 18:40:23 -04:00
|
|
|
SiteSetting.ai_helper_enabled = true
|
2023-10-23 10:41:36 -04:00
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_post_text(selected_post)
|
|
|
|
topic_page.visit_topic(topic)
|
|
|
|
page.execute_script(
|
|
|
|
"var element = document.querySelector('#{topic_page.post_by_number_selector(selected_post.post_number)} .cooked p'); " +
|
|
|
|
"var range = document.createRange(); " + "range.selectNodeContents(element); " +
|
|
|
|
"var selection = window.getSelection(); " + "selection.removeAllRanges(); " +
|
|
|
|
"selection.addRange(range);",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when triggering AI helper in post" do
|
|
|
|
it "shows the Ask AI button in the post selection toolbar" do
|
|
|
|
select_post_text(post)
|
|
|
|
expect(post_ai_helper).to have_post_selection_toolbar
|
|
|
|
expect(post_ai_helper).to have_post_ai_helper
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shows AI helper options after clicking the AI button" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
expect(post_ai_helper).to have_no_post_selection_primary_buttons
|
|
|
|
expect(post_ai_helper).to have_post_ai_helper_options
|
|
|
|
end
|
|
|
|
|
2024-07-10 14:01:05 -04:00
|
|
|
it "should not have the mobile post AI helper" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
expect(post_ai_helper).to have_no_mobile_post_ai_helper
|
|
|
|
end
|
|
|
|
|
2024-04-04 14:35:01 -04:00
|
|
|
it "highlights the selected text after clicking the AI button and removes after closing" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
expect(post_ai_helper).to have_highlighted_text
|
|
|
|
find("article[data-post-id='#{post.id}']").click
|
|
|
|
expect(post_ai_helper).to have_no_highlighted_text
|
|
|
|
end
|
|
|
|
|
2024-05-21 16:58:37 -04:00
|
|
|
it "allows post control buttons to still be functional after clicking the AI button" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
topic_page.click_like_reaction_for(post)
|
|
|
|
wait_for { post.reload.like_count == 1 }
|
|
|
|
expect(post.like_count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2023-10-23 10:41:36 -04:00
|
|
|
context "when using explain mode" do
|
2023-12-12 12:28:39 -05:00
|
|
|
let(:mode) { CompletionPrompt::EXPLAIN }
|
|
|
|
|
|
|
|
let(:explain_response) { <<~STRING }
|
2024-05-03 14:53:17 -04:00
|
|
|
In this context, pie refers to a baked dessert typically consisting of a pastry crust and filling.
|
2023-12-12 12:28:39 -05:00
|
|
|
The person states they enjoy eating pie, considering it a good dessert. They note that some people wastefully
|
2024-05-03 14:53:17 -04:00
|
|
|
throw pie at others, but the person themselves chooses to eat the pie rather than throwing it. Overall, pie
|
2023-12-12 12:28:39 -05:00
|
|
|
is being used to refer the the baked dessert food item.
|
|
|
|
STRING
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2024-05-03 14:53:17 -04:00
|
|
|
skip "TODO: Streaming causing timing issue in test" do
|
2023-10-23 10:41:36 -04:00
|
|
|
it "shows an explanation of the selected text" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
|
2023-11-28 23:17:46 -05:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([explain_response]) do
|
2024-05-03 14:53:17 -04:00
|
|
|
expected_value = explain_response.gsub(/"/, "").strip
|
|
|
|
|
|
|
|
post_ai_helper.select_helper_model(mode)
|
|
|
|
Jobs.run_immediately!
|
|
|
|
|
|
|
|
wait_for(timeout: 10) do
|
|
|
|
post_ai_helper.suggestion_value.gsub(/"/, "").strip == expected_value
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(post_ai_helper.suggestion_value.gsub(/"/, "").strip).to eq(expected_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds explained text as footnote to post" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([explain_response]) do
|
|
|
|
expected_value = explain_response.gsub(/"/, "").strip
|
|
|
|
|
2023-11-27 07:33:31 -05:00
|
|
|
post_ai_helper.select_helper_model(mode)
|
2024-05-03 14:53:17 -04:00
|
|
|
Jobs.run_immediately!
|
|
|
|
|
|
|
|
wait_for(timeout: 10) do
|
|
|
|
post_ai_helper.suggestion_value.gsub(/"/, "").strip == expected_value
|
|
|
|
end
|
|
|
|
|
|
|
|
post_ai_helper.click_add_footnote
|
|
|
|
expect(page.has_css?(".expand-footnote")).to eq(true)
|
2023-11-27 07:33:31 -05:00
|
|
|
end
|
2023-10-23 10:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when using translate mode" do
|
2023-12-12 12:28:39 -05:00
|
|
|
let(:mode) { CompletionPrompt::TRANSLATE }
|
2023-11-27 07:33:31 -05:00
|
|
|
|
2023-12-12 12:28:39 -05:00
|
|
|
let(:translated_input) { "The rain in Spain, stays mainly in the Plane." }
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
skip "TODO: Streaming causing timing issue in test" do
|
|
|
|
it "shows a translation of the selected text" do
|
|
|
|
select_post_text(post_2)
|
|
|
|
post_ai_helper.click_ai_button
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([translated_input]) do
|
|
|
|
post_ai_helper.select_helper_model(mode)
|
2023-10-23 10:41:36 -04:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
wait_for { post_ai_helper.suggestion_value == translated_input }
|
2023-11-27 07:33:31 -05:00
|
|
|
|
2024-08-08 14:32:39 -04:00
|
|
|
expect(post_ai_helper.suggestion_value).to eq(translated_input)
|
|
|
|
end
|
2023-12-14 22:30:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when using proofread mode" do
|
|
|
|
let(:mode) { CompletionPrompt::PROOFREAD }
|
|
|
|
let(:proofread_response) do
|
|
|
|
"The Toyota Supra delivers 382 horsepower making it a very fast car."
|
|
|
|
end
|
|
|
|
|
|
|
|
it "pre-fills fast edit with proofread text" do
|
|
|
|
skip("Test is flaky in CI, possibly some timing issue?") if ENV["CI"]
|
|
|
|
select_post_text(post_3)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([proofread_response]) do
|
|
|
|
post_ai_helper.select_helper_model(mode)
|
|
|
|
wait_for { fast_editor.has_content?(proofread_response) }
|
|
|
|
expect(fast_editor).to have_content(proofread_response)
|
2023-10-23 10:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when AI helper is disabled" do
|
2024-08-12 18:40:23 -04:00
|
|
|
before { SiteSetting.ai_helper_enabled = false }
|
2023-10-23 10:41:36 -04:00
|
|
|
|
|
|
|
it "does not show the Ask AI button in the post selection toolbar" do
|
|
|
|
select_post_text(post)
|
|
|
|
expect(post_ai_helper).to have_post_selection_toolbar
|
|
|
|
expect(post_ai_helper).to have_no_post_ai_helper
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is not a member of the post AI helper allowed group" do
|
|
|
|
before do
|
2024-08-12 18:40:23 -04:00
|
|
|
SiteSetting.ai_helper_enabled = true
|
2023-10-23 10:41:36 -04:00
|
|
|
SiteSetting.post_ai_helper_allowed_groups = non_member_group.id.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show the Ask AI button in the post selection toolbar" do
|
|
|
|
select_post_text(post)
|
|
|
|
expect(post_ai_helper).to have_post_selection_toolbar
|
|
|
|
expect(post_ai_helper).to have_no_post_ai_helper
|
|
|
|
end
|
|
|
|
end
|
2023-12-14 22:30:52 -05:00
|
|
|
|
|
|
|
context "when triggering AI proofread through edit button" do
|
|
|
|
let(:proofread_response) do
|
|
|
|
"The Toyota Supra delivers 382 horsepower making it a very fast car."
|
|
|
|
end
|
|
|
|
|
|
|
|
it "pre-fills fast edit with proofread text" do
|
|
|
|
skip("Test is flaky in CI, possibly some timing issue?") if ENV["CI"]
|
|
|
|
select_post_text(post_3)
|
|
|
|
find(".quote-edit-label").click
|
|
|
|
DiscourseAi::Completions::Llm.with_prepared_responses([proofread_response]) do
|
|
|
|
find(".btn-ai-suggest-edit", visible: :all).click
|
|
|
|
wait_for { fast_editor.has_content?(proofread_response) }
|
|
|
|
expect(fast_editor).to have_content(proofread_response)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-07-10 14:01:05 -04:00
|
|
|
|
|
|
|
context "when triggering post AI helper on mobile", mobile: true do
|
|
|
|
it "should use the bottom modal instead of the popup menu" do
|
|
|
|
select_post_text(post)
|
|
|
|
post_ai_helper.click_ai_button
|
|
|
|
expect(post_ai_helper).to have_mobile_post_ai_helper
|
|
|
|
end
|
|
|
|
end
|
2023-10-23 10:41:36 -04:00
|
|
|
end
|