From 2b98e84acf11a1fa2ae16790ac879c8266962a8f Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 27 May 2024 12:07:57 -0700 Subject: [PATCH] FIX: Reply broken when auto caption is enabled --- .../initializers/ai-image-caption.js | 12 +++---- .../system/ai_helper/ai_image_caption_spec.rb | 31 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/assets/javascripts/initializers/ai-image-caption.js b/assets/javascripts/initializers/ai-image-caption.js index f0bb50fb..0a6fd47f 100644 --- a/assets/javascripts/initializers/ai-image-caption.js +++ b/assets/javascripts/initializers/ai-image-caption.js @@ -161,6 +161,11 @@ export default apiInitializer("1.25.0", (api) => { const imageUploads = composer.model.reply.match(IMAGE_MARKDOWN_REGEX); const hasImageUploads = imageUploads?.length > 0; + + if (!hasImageUploads) { + resolve(); + } + const imagesToCaption = imageUploads.filter((image) => { const caption = image .substring(image.indexOf("[") + 1, image.indexOf("]")) @@ -181,12 +186,7 @@ export default apiInitializer("1.25.0", (api) => { const autoCaptionPromptKey = "ai-auto-caption-seen"; const seenAutoCaptionPrompt = keyValueStore.getItem(autoCaptionPromptKey); - if ( - autoCaptionEnabled || - !hasImageUploads || - !needsBetterCaptions || - seenAutoCaptionPrompt - ) { + if (autoCaptionEnabled || !needsBetterCaptions || seenAutoCaptionPrompt) { return resolve(); } diff --git a/spec/system/ai_helper/ai_image_caption_spec.rb b/spec/system/ai_helper/ai_image_caption_spec.rb index 47eace7d..5adc3319 100644 --- a/spec/system/ai_helper/ai_image_caption_spec.rb +++ b/spec/system/ai_helper/ai_image_caption_spec.rb @@ -4,6 +4,9 @@ RSpec.describe "AI image caption", type: :system, js: true do fab!(:user) { Fabricate(:admin, refresh_auto_groups: true) } fab!(:non_member_group) { Fabricate(:group) } let(:user_preferences_ai_page) { PageObjects::Pages::UserPreferencesAi.new } + let(:topic_page) { PageObjects::Pages::Topic.new } + fab!(:topic) + fab!(:post) { Fabricate(:post, topic: topic) } let(:composer) { PageObjects::Components::Composer.new } let(:popup) { PageObjects::Components::AiCaptionPopup.new } let(:dialog) { PageObjects::Components::Dialog.new } @@ -150,5 +153,33 @@ RSpec.describe "AI image caption", type: :system, js: true do end end end + + context "when a post has no uploads" do + before { user.user_option.update!(auto_image_caption: true) } + + it "should create the topic without triggering auto caption" do + title = "I love using Discourse! It is my favorite forum software" + visit("/latest") + page.find("#create-topic").click + composer.fill_title(title) + composer.fill_content( + "Culpa labore velit cupidatat commodo magna esse et minim consequat veniam dolore eiusmod. Labore eu elit in nulla ipsum elit consectetur sunt consectetur enim. Cillum ex ex velit eiusmod labore ullamco ut ad. Dolore dolor commodo nisi fugiat esse quis anim officia quis. Pariatur cillum pariatur irure cupidatat nostrud ullamco labore id aliqua ut nostrud. Eu adipisicing ut laboris.", + ) + composer.create + expect(dialog).to be_closed + expect(topic_page).to have_topic_title(title) + end + + it "should create a reply to a post without triggering auto caption" do + visit("/t/-/#{topic.id}") + topic_page.click_footer_reply + composer.fill_content( + "Culpa labore velit cupidatat commodo magna esse et minim consequat veniam dolore eiusmod. Labore eu elit in nulla ipsum elit consectetur sunt consectetur enim. Cillum ex ex velit eiusmod labore ullamco ut ad. Dolore dolor commodo nisi fugiat esse quis anim officia quis. Pariatur cillum pariatur irure cupidatat nostrud ullamco labore id aliqua ut nostrud. Eu adipisicing ut laboris.", + ) + composer.create + expect(dialog).to be_closed + expect(topic_page).to have_post_number(post.post_number) + end + end end end