From e0cf7b7d70e0b4789110dc6c41fe7faa49ce162f Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 May 2023 15:14:26 +1000 Subject: [PATCH] FIX: results will be nil for invalid queries (#74) Previous to this change invalid searches would break the command. --- lib/modules/ai_bot/commands/search_command.rb | 4 ++-- spec/lib/modules/ai_bot/commands/search_command_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/modules/ai_bot/commands/search_command.rb b/lib/modules/ai_bot/commands/search_command.rb index 04b38901..64f043b5 100644 --- a/lib/modules/ai_bot/commands/search_command.rb +++ b/lib/modules/ai_bot/commands/search_command.rb @@ -95,10 +95,10 @@ module DiscourseAi::AiBot::Commands results = Search.execute(search_string.to_s, search_type: :full_page, guardian: Guardian.new()) - posts = results.posts + posts = results&.posts || [] posts = posts[0..limit - 1] if limit - @last_num_results = results.posts.length + @last_num_results = posts.length if posts.blank? "No results found" diff --git a/spec/lib/modules/ai_bot/commands/search_command_spec.rb b/spec/lib/modules/ai_bot/commands/search_command_spec.rb index 37717400..a47cbdb0 100644 --- a/spec/lib/modules/ai_bot/commands/search_command_spec.rb +++ b/spec/lib/modules/ai_bot/commands/search_command_spec.rb @@ -9,6 +9,14 @@ RSpec.describe DiscourseAi::AiBot::Commands::SearchCommand do after { SearchIndexer.disable } describe "#process" do + it "can handle no results" do + post1 = Fabricate(:post) + search = described_class.new(bot_user, post1) + + results = search.process("order:fake") + expect(results).to eq("No results found") + end + it "can handle limits" do post1 = Fabricate(:post) _post2 = Fabricate(:post, user: post1.user)