FIX: results will be nil for invalid queries (#74)
Previous to this change invalid searches would break the command.
This commit is contained in:
parent
92fb84e24d
commit
e0cf7b7d70
|
@ -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"
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue