FIX: results will be nil for invalid queries (#74)

Previous to this change invalid searches would break the command.
This commit is contained in:
Sam 2023-05-22 15:14:26 +10:00 committed by GitHub
parent 92fb84e24d
commit e0cf7b7d70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -95,10 +95,10 @@ module DiscourseAi::AiBot::Commands
results = results =
Search.execute(search_string.to_s, search_type: :full_page, guardian: Guardian.new()) 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 posts = posts[0..limit - 1] if limit
@last_num_results = results.posts.length @last_num_results = posts.length
if posts.blank? if posts.blank?
"No results found" "No results found"

View File

@ -9,6 +9,14 @@ RSpec.describe DiscourseAi::AiBot::Commands::SearchCommand do
after { SearchIndexer.disable } after { SearchIndexer.disable }
describe "#process" do 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 it "can handle limits" do
post1 = Fabricate(:post) post1 = Fabricate(:post)
_post2 = Fabricate(:post, user: post1.user) _post2 = Fabricate(:post, user: post1.user)