From baaa3d199aa9c826f2ddbf7bf7e62f361e4ec073 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Thu, 19 Jun 2025 07:41:18 -0700 Subject: [PATCH] FIX: streaming related specs (#1448) ## :mag: Overview This update fixes an issue where message bus streaming related specs were not working correctly. To do so we pass the `last_id` when subscribing to `MessageBus` which allows us to unskip those broken tests. --------- Co-authored-by: Joffrey JAFFEUX --- .../discourse/components/modal/diff-modal.gjs | 10 +++++++--- lib/ai_helper/entry_point.rb | 6 ++++++ spec/system/ai_helper/ai_composer_helper_spec.rb | 6 +----- spec/system/ai_helper/ai_proofreading_spec.rb | 3 --- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/assets/javascripts/discourse/components/modal/diff-modal.gjs b/assets/javascripts/discourse/components/modal/diff-modal.gjs index 8eea2fd9..6d504aba 100644 --- a/assets/javascripts/discourse/components/modal/diff-modal.gjs +++ b/assets/javascripts/discourse/components/modal/diff-modal.gjs @@ -85,14 +85,18 @@ export default class ModalDiffModal extends Component { @bind subscribe() { - this.messageBus.subscribe(CHANNEL, this.updateResult); + this.messageBus.subscribe( + CHANNEL, + this.updateResult, + this.currentUser + ?.discourse_ai_helper_stream_composer_suggestion_last_message_bus_id + ); } @bind cleanup() { // stop all callbacks so it does not end up streaming pointlessly - this.smoothStreamer.resetStreaming(); - this.diffStreamer.reset(); + this.#resetState(); this.messageBus.unsubscribe(CHANNEL, this.updateResult); } diff --git a/lib/ai_helper/entry_point.rb b/lib/ai_helper/entry_point.rb index 41ce1780..729387e0 100644 --- a/lib/ai_helper/entry_point.rb +++ b/lib/ai_helper/entry_point.rb @@ -79,6 +79,12 @@ module DiscourseAi :discourse_ai_helper_stream_suggestion_last_message_bus_id, include_condition: -> { SiteSetting.ai_helper_enabled && scope.authenticated? }, ) { MessageBus.last_id("/discourse-ai/ai-helper/stream_suggestion/#{object.id}") } + + plugin.add_to_serializer( + :current_user, + :discourse_ai_helper_stream_composer_suggestion_last_message_bus_id, + include_condition: -> { SiteSetting.ai_helper_enabled && scope.authenticated? }, + ) { MessageBus.last_id("/discourse-ai/ai-helper/stream_composer_suggestion") } end end end diff --git a/spec/system/ai_helper/ai_composer_helper_spec.rb b/spec/system/ai_helper/ai_composer_helper_spec.rb index 6817cf16..b3539c17 100644 --- a/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -9,6 +9,7 @@ RSpec.describe "AI Composer helper", type: :system, js: true do Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_helper_model) SiteSetting.ai_helper_enabled = true + Jobs.run_immediately! sign_in(user) end @@ -80,7 +81,6 @@ RSpec.describe "AI Composer helper", type: :system, js: true do end it "replaces the composed message with AI generated content" do - skip("Message bus updates not appearing in tests") trigger_composer_helper(input) ai_helper_menu.fill_custom_prompt(custom_prompt_input) @@ -109,7 +109,6 @@ RSpec.describe "AI Composer helper", type: :system, js: true do let(:spanish_input) { "La lluvia en España se queda principalmente en el avión." } it "replaces the composed message with AI generated content" do - skip("Message bus updates not appearing in tests") trigger_composer_helper(spanish_input) DiscourseAi::Completions::Llm.with_prepared_responses([input]) do @@ -121,7 +120,6 @@ RSpec.describe "AI Composer helper", type: :system, js: true do end it "reverts results when Ctrl/Cmd + Z is pressed on the keyboard" do - skip("Message bus updates not appearing in tests") trigger_composer_helper(spanish_input) DiscourseAi::Completions::Llm.with_prepared_responses([input]) do @@ -134,7 +132,6 @@ RSpec.describe "AI Composer helper", type: :system, js: true do end it "shows the changes in a modal" do - skip("Message bus updates not appearing in tests") trigger_composer_helper(spanish_input) DiscourseAi::Completions::Llm.with_prepared_responses([input]) do @@ -168,7 +165,6 @@ RSpec.describe "AI Composer helper", type: :system, js: true do let(:proofread_text) { "The rain in Spain, stays mainly in the Plane." } it "replaces the composed message with AI generated content" do - skip("Message bus updates not appearing in tests") trigger_composer_helper(input) DiscourseAi::Completions::Llm.with_prepared_responses([proofread_text]) do diff --git a/spec/system/ai_helper/ai_proofreading_spec.rb b/spec/system/ai_helper/ai_proofreading_spec.rb index 1f06ea2a..b2e00733 100644 --- a/spec/system/ai_helper/ai_proofreading_spec.rb +++ b/spec/system/ai_helper/ai_proofreading_spec.rb @@ -23,7 +23,6 @@ RSpec.describe "AI Composer Proofreading Features", type: :system, js: true do context "when triggering via keyboard shortcut" do it "proofreads selected text" do - skip("Animation causing diff not to appear correctly in specs") visit "/new-topic" composer.fill_content("hello worldd !") @@ -38,7 +37,6 @@ RSpec.describe "AI Composer Proofreading Features", type: :system, js: true do end it "proofreads all text when nothing is selected" do - skip("Animation causing diff not to appear correctly in specs") visit "/new-topic" composer.fill_content("hello worrld") @@ -65,7 +63,6 @@ RSpec.describe "AI Composer Proofreading Features", type: :system, js: true do before { SiteSetting.rich_editor = true } it "proofreads selected text and replaces it" do - skip("Animation causing diff not to appear correctly in specs") visit "/new-topic" expect(composer).to be_opened composer.toggle_rich_editor