From 3aa5930191c6bd35b6be0217511ad702fdd42e75 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 6 Aug 2013 13:08:39 -0400 Subject: [PATCH] Streamline search suggestions when query has trailing space. If you entered "stream:Denmark " in the search box, we would show you two suggestions for "stream Denmark", despite our duplicate detection, because we didn't canonicalize the suggestion that is literally based off the user typed query, and so the other way of generating the "stream Denmark" suggestion created a duplicate. Now all the suggestions we generate are canonicalized, so the generalized duplicate detection can work. (imported from commit 52bf08ccf9bb2e2260ca8c20690169aead3732ab) --- static/js/search_suggestion.js | 8 ++++--- .../tests/frontend/node/search_suggestion.js | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/static/js/search_suggestion.js b/static/js/search_suggestion.js index 2362f2a0fb..da06ee9a4e 100644 --- a/static/js/search_suggestion.js +++ b/static/js/search_suggestion.js @@ -206,8 +206,10 @@ function get_person_suggestions(all_people, query, prefix, operator) { return objs; } -function get_suggestion_based_on_query(search_string, operators) { - // We expect caller to call narrow.parse to get operators from search_string. +function get_default_suggestion(operators) { + // Here we return the canonical suggestion for the full query that the + // user typed. (The caller passes us the parsed query as "operators".) + var search_string = narrow.unparse(operators); var description = describe(operators); description = Handlebars.Utils.escapeExpression(description); return {description: description, search_string: search_string}; @@ -394,7 +396,7 @@ exports.get_suggestions = function (query) { // Add an entry for narrow by operators. var operators = narrow.parse(query); - suggestion = get_suggestion_based_on_query(query, operators); + suggestion = get_default_suggestion(operators); result = [suggestion]; suggestions = get_special_filter_suggestions(query, operators); diff --git a/zerver/tests/frontend/node/search_suggestion.js b/zerver/tests/frontend/node/search_suggestion.js index 35dacfcb99..31edbcfbde 100644 --- a/zerver/tests/frontend/node/search_suggestion.js +++ b/zerver/tests/frontend/node/search_suggestion.js @@ -33,6 +33,8 @@ function set_up_dependencies() { global.recent_subjects = {}; + global.util = require('js/util.js'); + return search; } @@ -131,6 +133,27 @@ var search = set_up_dependencies(); }()); +(function test_whitespace_glitch() { + var query = 'stream:office '; // note trailing space + + global.subs.subscribed_streams = function () { + return ['office']; + }; + + global.narrow.stream = function () { + return; + }; + + global.recent_subjects = {}; + + var suggestions = search.get_suggestions(query); + + var expected = [ + "stream:office" + ]; + + assert.deepEqual(suggestions.strings, expected); +}()); (function test_people_suggestions() { var query = 'te';