FIX: Wrong discobot tutorial started for certain locales.

FIX: Wrong discobot tutorial started for certain locales.

If a locale has triggers that start with the same word, our regexp will always end up matching the first trigger. For example,

start tutorial and start tutorial advanced

To support the change, we have to make the match on triggers more restrictive. @discobot quote here will no longer work like @discobot quote.

diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb
index dad7dc1..1f0ad28 100644
--- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb
+++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/track_selector.rb
@@ -227,7 +227,7 @@ module DiscourseNarrativeBot
 
     def match_trigger?(trigger)
       discobot_username = self.discobot_user.username
-      regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i')
+      regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}</p>", 'i')
       match = @post.cooked.match(regexp)
 
       if @is_pm_to_bot
diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
index 6972214..18466d1 100644
--- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
+++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb
@@ -580,16 +580,14 @@ describe DiscourseNarrativeBot::TrackSelector do
             stub_request(:get, "http://api.forismatic.com/api/1.0/?format=json&lang=en&method=getQuote").
               to_return(status: 200, body: "{\"quoteText\":\"Be Like Water\",\"quoteAuthor\":\"Bruce Lee\"}")
 
-            ['@discobot quote', 'hello @discobot quote there'].each do |raw|
-              post.update!(raw: raw)
-              described_class.new(:reply, user, post_id: post.id).select
-              new_post = Post.last
+            post.update!(raw: "@discobot quote")
+            described_class.new(:reply, user, post_id: post.id).select
+            new_post = Post.last
 
-              expect(new_post.raw).to eq(
-                I18n.t("discourse_narrative_bot.quote.results",
-                quote: "Be Like Water", author: "Bruce Lee"
-              ))
-            end
+            expect(new_post.raw).to eq(
+              I18n.t("discourse_narrative_bot.quote.results",
+              quote: "Be Like Water", author: "Bruce Lee"
+            ))
           end
 
           describe 'when quote is requested incorrectly' do
@@ -661,6 +659,32 @@ describe DiscourseNarrativeBot::TrackSelector do
               end.to_not change { Post.count }
             end
           end
+
+          describe "when new and advanced user triggers overlap" do
+            before do
+              @overrides = []
+
+              @overrides << TranslationOverride.upsert!(
+                I18n.locale, 'discourse_narrative_bot.new_user_narrative.reset_trigger', 'tutorial'
+              )
+
+              @overrides << TranslationOverride.upsert!(
+                I18n.locale, 'discourse_narrative_bot.advanced_user_narrative.reset_trigger', 'tutorial advanced'
+              )
+            end
+
+            after do
+              @overrides.each(&:destroy!)
+            end
+
+            it "should start the right track" do
+              post.update!(raw: "@discobot #{I18n.t('discourse_narrative_bot.track_selector.reset_trigger')} #{DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
+
+              expect do
+                described_class.new(:reply, user, post_id: post.id).select
+              end.to change { Post.count }.by(2)
+            end
+          end
         end
       end
     end

GitHub sha: 636b6c3a

1 Like