Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search pills: Don't show invalid terms in search suggestions.
evykassirer5463f641e41e
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 8e5065d8999..054ce448e35 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -830,6 +830,11 @@ function suggestion_search_string(suggestion_line: SuggestionLine): string {const search_strings = [];for (const suggestion of suggestion_line) {if (suggestion.search_string !== "") {+ // This is rendered as "Direct messages" and we want to make sure+ // that we don't add another suggestion for "is:dm" in parallel.+ if (suggestion.search_string === "is:private") {+ suggestion.search_string = "is:dm";+ }search_strings.push(suggestion.search_string);}}@@ -967,10 +972,7 @@ export function get_search_result(query_from_pills: string, query_from_text: strconst base = get_default_suggestion_line(base_terms);const attacher = new Attacher(base);- // Display the default first- // `has` and `is` operators work only on predefined categories. Default suggestion- // is not displayed in that case. e.g. `messages that contain abc` as- // a suggestion for `has:abc` does not make sense.+ // Display the default first, unless it has invalid terms.if (last.operator === "search") {suggestion_line = [{@@ -982,7 +984,10 @@ export function get_search_result(query_from_pills: string, query_from_text: str},];attacher.push([...attacher.base, ...suggestion_line]);- } else if (all_search_terms.length > 0 && last.operator !== "has" && last.operator !== "is") {+ } else if (+ all_search_terms.length > 0 &&+ all_search_terms.every((term) => Filter.is_valid_search_term(term))+ ) {suggestion_line = get_default_suggestion_line(all_search_terms);attacher.push(suggestion_line);}diff --git a/web/tests/search_suggestion.test.js b/web/tests/search_suggestion.test.jsindex b44b9d02bc3..b17efff4e1a 100644--- a/web/tests/search_suggestion.test.js+++ b/web/tests/search_suggestion.test.js@@ -173,12 +173,12 @@ test("dm_suggestions", ({override, mock_template}) => {query = "dm:t";suggestions = get_suggestions(query);- expected = ["dm:t", "dm:[email protected]"];+ expected = ["dm:[email protected]"];assert.deepEqual(suggestions.strings, expected);query = "-dm:t";suggestions = get_suggestions(query);- expected = ["-dm:t", "is:dm -dm:[email protected]"];+ expected = ["is:dm -dm:[email protected]"];assert.deepEqual(suggestions.strings, expected);query = "dm:[email protected]";@@ -188,17 +188,17 @@ test("dm_suggestions", ({override, mock_template}) => {query = "sender:ted";suggestions = get_suggestions(query);- expected = ["sender:ted", "sender:[email protected]"];+ expected = ["sender:[email protected]"];assert.deepEqual(suggestions.strings, expected);query = "sender:te";suggestions = get_suggestions(query);- expected = ["sender:te", "sender:[email protected]"];+ expected = ["sender:[email protected]"];assert.deepEqual(suggestions.strings, expected);query = "-sender:te";suggestions = get_suggestions(query);- expected = ["-sender:te", "-sender:[email protected]"];+ expected = ["-sender:[email protected]"];assert.deepEqual(suggestions.strings, expected);query = "sender:[email protected]";@@ -208,7 +208,7 @@ test("dm_suggestions", ({override, mock_template}) => {query = "is:unread from:ted";suggestions = get_suggestions(query);- expected = ["is:unread sender:ted", "is:unread sender:[email protected]", "is:unread"];+ expected = ["is:unread sender:[email protected]", "is:unread"];assert.deepEqual(suggestions.strings, expected);// Users can enter bizarre queries, and if they do, we want to@@ -243,15 +243,9 @@ test("dm_suggestions", ({override, mock_template}) => {];assert.deepEqual(suggestions.strings, expected);- // Make sure it handles past context correctly- query = "stream:Denmark dm:";+ query = "from:[email protected]";suggestions = get_suggestions(query);- expected = ["channel:Denmark dm:", "channel:Denmark"];- assert.deepEqual(suggestions.strings, expected);-- query = "from:[email protected] from:";- suggestions = get_suggestions(query);- expected = ["sender:[email protected] sender:", "sender:[email protected]"];+ expected = ["sender:[email protected]"];assert.deepEqual(suggestions.strings, expected);// "pm-with" operator returns search result@@ -264,7 +258,7 @@ test("dm_suggestions", ({override, mock_template}) => {query = "pm-with:t";suggestions = get_suggestions(query);- expected = ["dm:t", "dm:[email protected]"];+ expected = ["dm:[email protected]"];assert.deepEqual(suggestions.strings, expected);});@@ -304,15 +298,7 @@ test("group_suggestions", ({mock_template}) => {query = "-dm:[email protected]";suggestions = get_suggestions(query);- expected = [- "-dm:[email protected]",- "is:dm -dm:[email protected]",- ];- assert.deepEqual(suggestions.strings, expected);-- query = "-dm:[email protected],red";- suggestions = get_suggestions(query);- expected = ["-dm:[email protected],red"];+ expected = ["is:dm -dm:[email protected]"];assert.deepEqual(suggestions.strings, expected);// If user types "pm-with" operator, show suggestions for@@ -344,21 +330,17 @@ test("group_suggestions", ({mock_template}) => {];assert.deepEqual(suggestions.strings, expected);- // Doesn't show ted@ because it's invalid in combination+ // Doesn't show dms because it's invalid in combination// with a channel.query = "channel:Denmark has:link dm:[email protected],Smit";suggestions = get_suggestions(query);- expected = [- "channel:Denmark has:link dm:[email protected],Smit",- "channel:Denmark has:link",- "channel:Denmark",- ];+ expected = ["channel:Denmark has:link", "channel:Denmark"];assert.deepEqual(suggestions.strings, expected);// Invalid emails don't give suggestionsquery = "has:link dm:[email protected],Smit";suggestions = get_suggestions(query);- expected = ["has:link dm:[email protected],Smit", "has:link"];+ expected = ["has:link"];assert.deepEqual(suggestions.strings, expected);});@@ -635,12 +617,7 @@ test("sent_by_me_suggestions", ({override, mock_template}) => {query = "is:starred sender:m";suggestions = get_suggestions(query);- expected = ["is:starred sender:m", "is:starred sender:[email protected]", "is:starred"];- assert.deepEqual(suggestions.strings, expected);-- query = "sender:[email protected] sender:";- suggestions = get_suggestions(query);- expected = ["sender:[email protected] sender:", "sender:[email protected]"];+ expected = ["is:starred sender:[email protected]", "is:starred"];assert.deepEqual(suggestions.strings, expected);});@@ -785,9 +762,7 @@ test("whitespace_glitch", ({override, mock_template}) => {assert.deepEqual(suggestions.strings, expected);});-test("channel_completion", ({override, mock_template}) => {- mock_template("search_description.hbs", true, (_data, html) => html);-+test("channel_completion", ({override}) => {stream_data.add_sub({stream_id: 77, name: "office", subscribed: true});stream_data.add_sub({stream_id: 88, name: "dev help", subscribed: true});@@ -795,12 +770,12 @@ test("channel_completion", ({override, mock_template}) => {let query = "channel:of";let suggestions = get_suggestions(query);- let expected = ["channel:of", "channel:office"];+ let expected = ["channel:office"];assert.deepEqual(suggestions.strings, expected);query = "-channel:of";suggestions = get_suggestions(query);- expected = ["-channel:of", "-channel:office"];+ expected = ["-channel:office"];assert.deepEqual(suggestions.strings, expected);query = "hel";@@ -967,7 +942,7 @@ test("people_suggestions", ({override, mock_template}) => {assert.deepEqual(suggestions.strings, expected);query = "sender:ted sm";- expected = ["sender:ted+sm", "sender:[email protected]"];+ expected = ["sender:[email protected]"];suggestions = get_suggestions(query);assert.deepEqual(suggestions.strings, expected);@@ -977,7 +952,7 @@ test("people_suggestions", ({override, mock_template}) => {assert.deepEqual(suggestions.strings, expected);query = "sender:[email protected] new";- expected = ["sender:[email protected]+new"];+ expected = []; // Invalid emailsuggestions = get_suggestions(query);assert.deepEqual(suggestions.strings, expected);});@@ -990,7 +965,7 @@ test("operator_suggestions", ({override, mock_template}) => {// Completed operator should return nothinglet query = "channel:";let suggestions = get_suggestions(query);- let expected = ["channel:"];+ let expected = [];assert.deepEqual(suggestions.strings, expected);query = "ch";@@ -1015,27 +990,25 @@ test("operator_suggestions", ({override, mock_template}) => {assert.deepEqual(suggestions.strings, expected);});-test("queries_with_spaces", ({mock_template}) => {- mock_template("search_description.hbs", true, (_data, html) => html);-+test("queries_with_spaces", () => {stream_data.add_sub({stream_id: 77, name: "office", subscribed: true});stream_data.add_sub({stream_id: 88, name: "dev help", subscribed: true});// test allowing spaces with quotes surrounding operandlet query = 'channel:"dev he"';let suggestions = get_suggestions(query);- let expected = ["channel:dev+he", "channel:dev+help"];+ let expected = ["channel:dev+help"];assert.deepEqual(suggestions.strings, expected);// test mismatched quotequery = 'channel:"dev h';suggestions = get_suggestions(query);- expected = ["channel:dev+h", "channel:dev+help"];+ expected = ["channel:dev+help"];assert.deepEqual(suggestions.strings, expected);// test extra space after operator still worksquery = "channel: offi";suggestions = get_suggestions(query);- expected = ["channel:offi", "channel:office"];+ expected = ["channel:office"];assert.deepEqual(suggestions.strings, expected);});