From 636b6c3a5a92664f6c8287afadb0807c5429e9e5 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 8 Aug 2019 10:47:39 +0800 Subject: [PATCH] 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`. --- .../discourse_narrative_bot/track_selector.rb | 2 +- .../track_selector_spec.rb | 42 +++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) 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 dad7dc1f95d..1f0ad28044f 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("@#{discobot_username} #{trigger}", 'i') + regexp = Regexp.new("@#{discobot_username} #{trigger}

", '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 69722148247..18466d156c9 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