Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search: Add support for date operator.
apoorvapendse41480799c379
diff --git a/web/src/filter.ts b/web/src/filter.tsindex 28f4bdd266e..031eab47384 100644--- a/web/src/filter.ts+++ b/web/src/filter.ts@@ -3,6 +3,7 @@ import assert from "minimalistic-assert";import render_search_description from "../templates/search_description.hbs";+import * as date_util from "./date_util.ts";import * as filter_util from "./filter_util.ts";import * as hash_parser from "./hash_parser.ts";import {$t} from "./i18n.ts";@@ -221,6 +222,7 @@ function build_term_predicate(term: NarrowCanonicalTerm): ((message: Message) =>// Operators that don't filter messages on the client.case "near":case "with":+ case "date":case "":return null;@@ -356,6 +358,8 @@ export class Filter {// unlikely to cause any problems and is probably what the user wants.narrow_term.operand = narrow_term.operand.replaceAll(/[\u201C\u201D]/g, '"');break;+ case "date":+ break;default:narrow_term.operand = narrow_term.operand.toLowerCase();}@@ -684,6 +688,13 @@ export class Filter {case "search":case "":return true;+ case "date": {+ if (term.negated) {+ return false;+ }+ const date = date_util.maybe_get_parsed_iso_8601_date(term.operand);+ return date_util.is_date_valid(date);+ }default:// istanbul ignore next// It should never reach here because of operator validation.@@ -806,6 +817,10 @@ export class Filter {return verb + "messages in a specific channel";case "channels":return verb + "channel type";++ case "date":+ return "messages sent around a specific date";+case "near":return verb + "messages around";@@ -944,6 +959,14 @@ export class Filter {// Assume the operand is a partially formed name and return// the operator as the channel name in the next block.}+ if (term.operator === "date") {+ return {+ type: "prefix_for_operator",+ prefix_for_operator:+ term.operand === "" ? prefix_for_operator : "messages sent around",+ operand: date_util.convert_date_str_to_description_date(term.operand),+ };+ }if (term.operator === "topic" && !is_operator_suggestion) {return {type: "prefix_for_operator",@@ -1129,18 +1152,23 @@ export class Filter {}public_terms(): NarrowTerm[] {- const safe_to_return = this._terms.filter(+ const safe_to_return = this._terms.filter((term) => {+ if (term.operator === "date") {+ // We don't send the date as a part of the narrow+ // while fetching messages or during other+ // server interactions.+ return false;+ }+ // TODO(stream_id): Ideally we have `page_params.narrow_stream_id`+ if (page_params.narrow_stream === undefined || term.operator !== "channel") {+ return true;+ }+// Filter out the embedded narrow (if any).- (term) => {- // TODO(stream_id): Ideally we have `page_params.narrow_stream_id`- if (page_params.narrow_stream === undefined || term.operator !== "channel") {- return true;- }- const narrow_stream = stream_data.get_sub_by_name(page_params.narrow_stream);- assert(narrow_stream !== undefined);- return Number.parseInt(term.operand, 10) !== narrow_stream.stream_id;- },- );+ const narrow_stream = stream_data.get_sub_by_name(page_params.narrow_stream);+ assert(narrow_stream !== undefined);+ return Number.parseInt(term.operand, 10) !== narrow_stream.stream_id;+ });return safe_to_return;}@@ -1252,6 +1280,7 @@ export class Filter {"not-channels-web-public","near","with",+ "date",]);for (const term of term_types) {@@ -2101,17 +2130,26 @@ export class Filter {return false;}+ terms_for_server_query(): NarrowCanonicalTerm[] {+ // We work off `_terms` rather than `public_terms()` because the+ // embedded-narrow stripping in `public_terms()` is specific to+ // the client-side filter; the server query should include those+ // terms. We do still drop `date`, since it travels to the server+ // as the `anchor`/`anchor_date` parameters instead.+ return this._terms.filter((term) => term.operator !== "date");+ }+get_stringified_narrow_for_server_query(): string {- return JSON.stringify(- this._terms.map((term) => {- if (term.operator === "channel") {- return {- ...term,- operand: Number.parseInt(term.operand, 10),- };- }- return term;- }),- );+ const terms = this.terms_for_server_query().map((term) => {+ if (term.operator === "channel") {+ return {+ ...term,+ operand: Number.parseInt(term.operand, 10),+ };+ }+ return term;+ });++ return JSON.stringify(terms);}}diff --git a/web/src/hash_parser.ts b/web/src/hash_parser.tsindex 84d980cac6a..0806b60e73a 100644--- a/web/src/hash_parser.ts+++ b/web/src/hash_parser.ts@@ -135,6 +135,9 @@ export function is_an_allowed_web_public_narrow(export const allowed_web_public_narrow_operators = ["channels","channel",+ // This is sent as an anchor to the server rather+ // than a narrow operator, but is part of the fragment.+ "date","streams","stream","topic",diff --git a/web/src/message_events.ts b/web/src/message_events.tsindex b76fbb2d9c5..f4eaa404cab 100644--- a/web/src/message_events.ts+++ b/web/src/message_events.ts@@ -201,7 +201,7 @@ export let update_views_filtered_on_message_property = (url: "/json/messages",data: {message_ids: JSON.stringify(message_ids),- narrow: JSON.stringify(filter.terms()),+ narrow: filter.get_stringified_narrow_for_server_query(),allow_empty_topic_name: true,},success(data) {diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex 6f6c276d0b8..8e7bc542abe 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -511,8 +511,9 @@ export function load_messages(opts: MessageFetchOptions, attempt = 1): void {export function load_messages_for_narrow(opts: {anchor: string | number;+ anchor_date?: string | undefined;msg_list: MessageList;- cont: () => void;+ cont: (data: MessageFetchResponse) => void;validate_filter_topic_post_fetch?: boolean | undefined;}): void {load_messages({@@ -522,6 +523,7 @@ export function load_messages_for_narrow(opts: {msg_list: opts.msg_list,msg_list_data: opts.msg_list.data,cont: opts.cont,+ anchor_date: opts.anchor_date,validate_filter_topic_post_fetch: opts.validate_filter_topic_post_fetch,});}diff --git a/web/src/message_view.ts b/web/src/message_view.tsindex 5f5fdcb90a0..a6a67093e1a 100644--- a/web/src/message_view.ts+++ b/web/src/message_view.ts@@ -16,6 +16,7 @@ import * as compose_notifications from "./compose_notifications.ts";import * as compose_recipient from "./compose_recipient.ts";import * as compose_state from "./compose_state.ts";import * as condense from "./condense.ts";+import * as date_util from "./date_util.ts";import * as feedback_widget from "./feedback_widget.ts";import {Filter} from "./filter.ts";import * as hash_parser from "./hash_parser.ts";@@ -298,6 +299,7 @@ function handle_post_message_list_change(select_immediately: boolean,select_opts: SelectIdOpts,then_select_offset: number | undefined,+ anchored_for_date?: boolean,): void {// Important: We need to consider opening the compose box// before calling render_message_list_with_selected_message, so that the logic in@@ -307,12 +309,20 @@ function handle_post_message_list_change(compose_actions.on_narrow(opts);if (select_immediately) {- render_message_list_with_selected_message({+ const render_opts: Parameters<typeof render_message_list_with_selected_message>[0] = {id_info,select_offset: then_select_offset,msg_list: message_lists.current,select_opts,- });+ };++ if (anchored_for_date) {+ // We go through this codepath only when the message closest+ // to the date is present locally in the current message list.+ render_opts.anchor_for_date = true;+ id_info.final_select_id = id_info.local_select_id!;+ }+ render_message_list_with_selected_message(render_opts);}handle_post_view_change(msg_list, opts);@@ -324,11 +334,22 @@ function handle_post_message_list_change(compose_recipient.handle_middle_pane_transition();}-function get_selected_message_top_offset(): number {+function get_selected_message_top_offset(is_first_message_in_narrow = false): number {const navbar_height = $("#navbar-fixed-container").height()!;- // 30px height + 10px top margin.- const sticky_header_outer_height = 40;- return navbar_height + sticky_header_outer_height;+ let final_offset =+ navbar_height ++ // Falls back to 30px height + 10px top margin if no sticky header is present.+ ($(".message_header").outerHeight(true) ?? 40);++ // These extra offsets are only needed for the first message in the feed when using+ // the date search operator. Using this otherwise causes too much of a gap between+ // the top of the message and the sticky header.+ if (is_first_message_in_narrow) {+ final_offset +=+ ($("#message_feed_errors_container").outerHeight(true) ?? 0) ++ ($(".top-messages-logo").outerHeight(true) ?? 0);+ }+ return final_offset;}export function try_rendering_locally_for_same_narrow(@@ -779,15 +800,25 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi{let anchor;+ let anchor_date;// Either we're trying to center the narrow around a// particular message ID (which could be max_int), or we're// asking the server to figure out for us what the first// unread message is, and center the narrow around that.switch (id_info.final_select_id) {- case undefined:+ case undefined: {anchor = "first_unread";+ const date_term = filter+ .terms()+ .find((raw_term) => raw_term.operator === "date");+ const date_anchor =+ date_term && date_util.maybe_get_date_anchor(date_term.operand);+ if (date_anchor) {+ ({anchor, anchor_date} = date_anchor);+ }break;+ }case -1:// This case should never happen in this code path; it's// here in case we choose to extract this as an@@ -802,9 +833,10 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi}message_fetch.load_messages_for_narrow({anchor,+ anchor_date,validate_filter_topic_post_fetch:filter.requires_adjustment_for_moved_with_target,- cont() {+ cont(data) {if (message_lists.current !== msg_list) {return;}@@ -832,12 +864,18 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi);filter.narrow_requires_hash_change = false;}+ // If the server request was made with a "date" anchor,+ // we select the message id that the server returned.+ if (anchor === "date") {+ id_info.final_select_id = data.anchor;+ }if (!select_immediately) {render_message_list_with_selected_message({id_info,select_offset: then_select_offset,msg_list,select_opts,+ anchor_for_date: anchor === "date",});}},@@ -848,6 +886,7 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voiassert(select_opts !== undefined);assert(select_immediately !== undefined);+ const anchored_for_date = filter.has_operator("date");handle_post_message_list_change(id_info,msg_list,@@ -855,6 +894,7 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voiselect_immediately,select_opts,then_select_offset,+ anchored_for_date,);if (id_info.first_unread_msg_id_pending_server_verification &&@@ -1148,6 +1188,79 @@ export function maybe_add_local_messages(opts: {const filter = msg_data.filter;const unread_info = narrow_state.get_first_unread_info(filter);+ // The `date` operator behaves as a slicing mechanism rather than a+ // true filter: we just need to pick the message to select, namely+ // the one closest to (on or after) the searched date. The local+ // superset cache is not an authoritative, contiguous view of the+ // narrow (see message_list_data_cache.get_superset_datasets), so we+ // can resolve the anchor locally only when the answer doesn't depend+ // on the cache being complete: when every local message is older than+ // the date and we've found the newest, nothing matches on/after the+ // date and the closest match is the newest message we have. In every+ // other case the cache may be missing a matching message, so we leave+ // `final_select_id` undefined and defer to the server's anchor.+ if (filter.has_operator("date")) {+ if (+ unread_info.flavor === "cannot_compute" ||+ !load_local_messages(msg_data, superset_data)+ ) {+ // We surely don't have the earliest matching message+ // and should fetch messages from the server+ // for the most accurate anchor.+ // This is particularly applicable during a hashchange or+ // when the filter has a search operator.+ id_info.final_select_id = undefined;+ return;+ }++ const date_str = filter.terms_with_operator("date")[0]!.operand;+ const unix_seconds_for_date_local_midnight =+ date_util.get_unix_seconds_for_local_midnight(date_str);+ if (unix_seconds_for_date_local_midnight === null) {+ return;+ }++ const first_message_in_filtered_data = msg_data.first();+ assert(first_message_in_filtered_data !== undefined);+ const last_message_in_filtered_data = msg_data.last();+ assert(last_message_in_filtered_data !== undefined);++ id_info.final_select_id = undefined;+ if (first_message_in_filtered_data.timestamp >= unix_seconds_for_date_local_midnight) {+ // The earliest message we have locally is already on or after the+ // searched date, so the anchor would be our first message. But we+ // cannot confirm locally that it's the first message in the narrow:+ // superset_data's fetch status reflects only messages with+ // UserMessage rows, so for a channel narrow that includes history+ // it cannot tell us whether older matching messages exist on the+ // server (see #32150). We therefore defer to the server, which+ // returns both the accurate anchor and the correct oldest-message+ // fetch status that render_message_list_with_selected_message+ // relies on to add the top-of-feed offsets when the anchor is the+ // first message in the narrow.+ return;+ }++ if (last_message_in_filtered_data.timestamp < unix_seconds_for_date_local_midnight) {+ if (superset_data.fetch_status.has_found_newest()) {+ // No messages on/after the date exist at all so select last message+ // locally.+ id_info.final_select_id = last_message_in_filtered_data.id;+ id_info.local_select_id = id_info.final_select_id;+ return;+ }+ // There may be matching messages on/after the date that we+ // haven't fetched yet, so defer to the server.+ return;+ }+ // The date falls within the local range, but the cache need not+ // be contiguous, so the first local message on/after the date may+ // not be the narrow's true first such message (an unfetched+ // matching message could lie between the date and it). Defer to+ // the server for the accurate anchor.+ return;+ }+// If we don't have a specific message we're hoping to select// (i.e. no `target_id`) and the narrow's filter doesn't// allow_use_first_unread_when_narrowing, we want to just render@@ -1305,6 +1418,7 @@ export function render_message_list_with_selected_message(opts: {id_info: TargetMessageIdInfo;select_offset: number | undefined;select_opts: SelectIdOpts;+ anchor_for_date?: boolean;}): void {if (message_lists.current !== undefined && message_lists.current !== opts.msg_list) {// If we navigated away from a view while we were fetching@@ -1323,6 +1437,7 @@ export function render_message_list_with_selected_message(opts: {const select_offset = opts.select_offset;const msg_id = id_info.final_select_id ?? message_lists.current.first_unread_message_id();+// There should be something since it's not visibly empty.assert(msg_id !== undefined);@@ -1349,6 +1464,17 @@ export function render_message_list_with_selected_message(opts: {message_lists.current.view.set_message_offset(select_offset);}message_lists.current.view.update_sticky_recipient_headers();+ if (opts.anchor_for_date) {+ // The aim is to keep the selected message at the top, when using+ // the date anchor.+ const is_first_message_in_narrow =+ message_lists.current.first()?.id === msg_id &&+ message_lists.current.data.fetch_status.has_found_oldest();+ message_lists.current.view.set_message_offset(+ get_selected_message_top_offset(is_first_message_in_narrow),+ );+ }+// For /near/ views, check whether reading can be resumed before// processing visibility, so that messages are correctly marked// as read on the initial render if appropriate.diff --git a/web/src/search_pill.ts b/web/src/search_pill.tsindex 74bf66d7e97..64dfa123253 100644--- a/web/src/search_pill.ts+++ b/web/src/search_pill.ts@@ -5,6 +5,7 @@ import render_input_pill from "../templates/input_pill.hbs";import render_search_list_item from "../templates/search_list_item.hbs";import render_search_user_pill from "../templates/search_user_pill.hbs";+import * as date_util from "./date_util.ts";import {Filter} from "./filter.ts";import {$t} from "./i18n.ts";import * as input_pill from "./input_pill.ts";@@ -83,6 +84,9 @@ export function get_search_string_from_item(item: SearchPill): string {case "channel":operand = stream_data.get_valid_sub_by_id_string(item.operand).name;break;+ case "date":+ operand = date_util.get_search_pill_value(item.operand);+ break;default:operand = item.operand;}diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 46b420fdaac..a7ae4287149 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -3,6 +3,7 @@ import assert from "minimalistic-assert";import {MAX_ITEMS} from "./bootstrap_typeahead.ts";import * as common from "./common.ts";+import * as date_util from "./date_util.ts";import * as direct_message_group_data from "./direct_message_group_data.ts";import {Filter} from "./filter.ts";import * as filter_util from "./filter_util.ts";@@ -154,7 +155,11 @@ const incompatible_patterns: Record<SearchFilter, TermPattern[]> = {"has:image": [{operator: "has", operand: "image"}],"has:attachment": [{operator: "has", operand: "attachment"}],"has:reaction": [{operator: "has", operand: "reaction"}],- near: [],+ // `date` and `near` combination is made incompatible to avoid confusing the user.+ // Having both operators only takes `date` into account while narrowing.+ // Details: https://github.com/zulip/zulip/pull/38486#issuecomment-4310019929+ date: [{operator: "date"}, {operator: "near"}],+ near: [{operator: "date"}],// These below are not currently looked up.has: [],in: [],@@ -524,6 +529,21 @@ function ignore_resolved_topic_prefix(entry: ChannelTopicEntry, case_insensitivereturn topic_name;}+function get_date_suggestions(+ last: NarrowCanonicalTermSuggestion,+ terms: NarrowCanonicalTerm[],+): Suggestion[] {+ if (!check_validity(last.operator, terms, ["date", "search"], incompatible_patterns.date)) {+ return [];+ }++ const negated = last.negated === true;+ if (negated) {+ return [];+ }+ return date_util.get_matching_default_date_suggestions(last.operand);+}+function get_topic_suggestions(last: NarrowCanonicalTermSuggestion,terms: NarrowCanonicalTerm[],@@ -886,6 +906,11 @@ function get_operator_suggestions(legacy_operator_choices = ["from", "pm-with", "streams", "stream"];}+ if (!negated) {+ // We don't support excluding a date.+ canonicalized_operator_choices.push("date");+ }+// We remove suggestion choice if its incompatible_pattern matches// that of current search terms.canonicalized_operator_choices = canonicalized_operator_choices.filter((choice) => {@@ -1205,6 +1230,7 @@ export let get_suggestions = function (get_people("mentions"),get_topic_suggestions,get_has_filter_suggestions,+ get_date_suggestions,];if (page_params.is_spectator) {@@ -1216,6 +1242,7 @@ export let get_suggestions = function (get_people("sender"),get_topic_suggestions,get_has_filter_suggestions,+ get_date_suggestions,];}diff --git a/web/src/state_data.ts b/web/src/state_data.tsindex 52eb50bfb06..60cd1104f48 100644--- a/web/src/state_data.ts+++ b/web/src/state_data.ts@@ -44,6 +44,7 @@ export const narrow_canonical_operator_schema = z.enum(["", // Used for search suggestions."channel","channels",+ "date","dm","dm-including","has",@@ -150,6 +151,11 @@ export const narrow_canonical_term_schema = z.discriminatedUnion("operator", [operand: z.array(z.number()),negated: z.optional(z.boolean()),}),+ z.object({+ operator: z.literal("date"),+ operand: z.string(),+ negated: z.optional(z.boolean()),+ }),]);export type NarrowCanonicalTerm = z.output<typeof narrow_canonical_term_schema>;diff --git a/web/src/unread_ops.ts b/web/src/unread_ops.tsindex caa323ff0b6..9e1f0baf603 100644--- a/web/src/unread_ops.ts+++ b/web/src/unread_ops.ts@@ -829,7 +829,7 @@ export function mark_all_as_read(modal_id?: string): void {export function mark_narrow_as_read(filter: Filter): void {bulk_update_read_flags_for_narrow(- [{operator: "is", operand: "unread", negated: false}, ...filter.terms()],+ [{operator: "is", operand: "unread", negated: false}, ...filter.terms_for_server_query()],"add",);}diff --git a/web/templates/search_operators.hbs b/web/templates/search_operators.hbsindex 421d335f9f1..a8e3580519b 100644--- a/web/templates/search_operators.hbs+++ b/web/templates/search_operators.hbs@@ -51,6 +51,15 @@{{/tr}}</td></tr>+ <tr>+ <td class="operator">date:<span class="operator_value">date</span></td>+ <td class="definition">+ {{#tr}}+ Narrow to messages around <z-value></z-value>.+ {{#*inline "z-value"}}<span class="operator_value">date</span>{{/inline}}+ {{/tr}}+ </td>+ </tr><tr><td class="operator">dm-including:<span class="operator_value">user</span></td><td class="definition">diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjsindex 8aec6492326..40d5db209fd 100644--- a/web/tests/filter.test.cjs+++ b/web/tests/filter.test.cjs@@ -7,7 +7,7 @@ const {parseOneAddress} = require("email-addresses");const {make_realm} = require("./lib/example_realm.cjs");const {make_stream} = require("./lib/example_stream.cjs");const {make_user, Role} = require("./lib/example_user.cjs");-const {mock_esm, with_overrides, zrequire} = require("./lib/namespace.cjs");+const {mock_esm, with_overrides, zrequire, clock} = require("./lib/namespace.cjs");const {run_test} = require("./lib/test.cjs");const blueslip = require("./lib/zblueslip.cjs");const $ = require("./lib/zjquery.cjs");@@ -1050,6 +1050,7 @@ test("public_terms", ({override}) => {{operator: "channel", operand: some_channel_id.toString()},{operator: "in", operand: "all"},{operator: "topic", operand: "bar"},+ {operator: "date", operand: "2025-04-01"},];let filter = new Filter(terms);const expected_terms = [@@ -1136,6 +1137,10 @@ test("canonicalization", () => {assert.equal(term.operator, "search");assert.equal(term.operand, 'abc "xyz"');+ term = Filter.canonicalize_term({operator: "date", operand: "2022-06-10"});+ assert.equal(term.operator, "date");+ assert.equal(term.operand, "2022-06-10");+term = Filter.canonicalize_term({operator: "has", operand: "attachments"});assert.equal(term.operator, "has");assert.equal(term.operand, "attachment");@@ -1829,6 +1834,20 @@ test("parse", () => {string = "https://www.google.com";terms = [{operator: "search", operand: "https://www.google.com"}];_test();++ string = "date:a week ago";+ terms = [+ {operator: "date", operand: "a"},+ {operator: "search", operand: "week ago"},+ ];+ _test();++ string = "date:2025-03-12 hello";+ terms = [+ {operator: "date", operand: "2025-03-12"},+ {operator: "search", operand: "hello"},+ ];+ _test();});test("unparse", () => {@@ -1930,8 +1949,17 @@ test("describe", ({mock_template, override}) => {assert.equal(Filter.search_description_as_html(narrow, false), string);page_params.is_spectator = false;+ narrow = [{operator: "date", operand: ""}];+ string = "messages sent around a specific date";+ assert.equal(Filter.search_description_as_html(narrow, true), string);++ narrow = [{operator: "date", operand: "2026-03-15"}];+ string = "messages sent around March 15, 2026";+ assert.equal(Filter.search_description_as_html(narrow, false), string);+const devel_decorated = `<span class="decorated-channel-name-wrapper inline-decorated-channel-name"><span class="channel-privacy-type-icon"><i class="zulip-icon zulip-icon-hashtag" aria-hidden="true"></i></span><span class="decorated-channel-name">devel</span></span>`;const river_decorated = `<span class="decorated-channel-name-wrapper inline-decorated-channel-name"><span class="channel-privacy-type-icon"><i class="zulip-icon zulip-icon-hashtag" aria-hidden="true"></i></span><span class="decorated-channel-name">river</span></span>`;+const devel_id = new_stream_id();make_sub("devel", devel_id);@@ -2381,7 +2409,17 @@ test("convert_suggestion_to_term", () => {true,{operator: "dm", operand: [sam_two.user_id]},],- ];+ [`-date:2022-01-01`, false],+ [`date:2022-01-01`, true],+ // Not a valid date string.+ [`date:2022-01-01abcd`, false],+ // In the past+ [`date:1500-01-06`, false],+ // Not in the `yyyy-MM-dd` format+ [`date:2020-`, false],+ ];+ const today = new Date(2024, 0, 15, 2, 0, 0);+ clock.setSystemTime(today.getTime());for (const [search_term_string, expected_is_valid, expected_term] of test_data) {const term = Filter.convert_suggestion_to_term(Filter.parse(search_term_string)[0]);assert.equal(term !== undefined, expected_is_valid);@@ -2389,6 +2427,7 @@ test("convert_suggestion_to_term", () => {assert.deepEqual(term, {negated: false, ...expected_term});}}+ clock.reset();// Invalid operator.assert.equal(@@ -3428,6 +3467,20 @@ run_test("can_newly_match_moved_messages", () => {assert.deepEqual(filter.can_newly_match_moved_messages("general", "test"), true);});+run_test("terms_for_server_query", () => {+ // Returns the raw terms unchanged (channel operand stays a+ // string), only dropping the client-side-only `date` operator.+ const filter = new Filter([+ {operator: "channel", operand: "1"},+ {operator: "date", operand: "2025-03-01"},+ {operator: "topic", operand: "bar"},+ ]);+ assert.deepEqual(filter.terms_for_server_query(), [+ {operator: "channel", operand: "1", negated: false},+ {operator: "topic", operand: "bar", negated: false},+ ]);+});+run_test("get_stringified_narrow_for_server_query", () => {const filter = new Filter([{operator: "channel", operand: "1"},@@ -3438,4 +3491,16 @@ run_test("get_stringified_narrow_for_server_query", () => {narrow,'[{"operator":"channel","operand":1,"negated":false},{"operator":"topic","operand":"bar","negated":false}]',);++ // Excludes date term if it is present in the filter.+ const filter2 = new Filter([+ {operator: "channel", operand: "1"},+ {operator: "date", operand: "2025-03-01"},+ {operator: "topic", operand: "bar"},+ ]);+ const narrow2 = filter2.get_stringified_narrow_for_server_query();+ assert.equal(+ narrow2,+ '[{"operator":"channel","operand":1,"negated":false},{"operator":"topic","operand":"bar","negated":false}]',+ );});diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjsindex ad41a28811b..2e410296584 100644--- a/web/tests/lib/zjquery_element.cjs+++ b/web/tests/lib/zjquery_element.cjs@@ -611,9 +611,17 @@ exports.FakeJQuery = class extends RejectMissing {},);}- outerHeight(...args) {- assert.equal(args.length, 0, "zjquery does not support this outerHeight() call");- return 0 in this ? this[0].offsetHeight : undefined;+ outerHeight() {+ if (!(0 in this)) {+ return undefined;+ }+ const state = fake_element_state.get(this[0]);+ const height = state.computed_style.getPropertyValue("height");+ if (height === "") {+ return undefined;+ }+ assert.ok(height.endsWith("px"));+ return Number(height.slice(0, -2));}parent(selector = "*") {return new exports.FakeJQuery(diff --git a/web/tests/message_view.test.cjs b/web/tests/message_view.test.cjsindex 35440a627dd..79bad0f0ae4 100644--- a/web/tests/message_view.test.cjs+++ b/web/tests/message_view.test.cjs@@ -1101,14 +1101,16 @@ run_test("fast_track_current_msg_list_to_anchor date", ({override}) => {};message_lists.current = list;$("#navbar-fixed-container").set_height(50);+ $(".message_header").set_height(30);// Date jumps should place the selected message below the sticky- // message header, increasing the target scroll offset by 40px.+ // message header, increasing the target scroll offset by the+ // header's measured height (30px here).const in_range = new Date(150 * 1000).toISOString();message_view.fast_track_current_msg_list_to_anchor("date", in_range);assert.deepEqual(selected, {id: 102,- opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 90},+ opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 80},});list.data.fetch_status.finish_older_batch({@@ -1120,7 +1122,7 @@ run_test("fast_track_current_msg_list_to_anchor date", ({override}) => {message_view.fast_track_current_msg_list_to_anchor("date", before_range);assert.deepEqual(selected, {id: 101,- opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 90},+ opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 80},});// If we have not found the oldest message, and the anchor timestamp is@@ -1160,7 +1162,7 @@ run_test("fast_track_current_msg_list_to_anchor date", ({override}) => {then_scroll: true,from_scroll: false,force_rerender: true,- target_scroll_offset: 90,+ target_scroll_offset: 80,},});@@ -1193,7 +1195,7 @@ run_test("fast_track_current_msg_list_to_anchor date", ({override}) => {then_scroll: true,from_scroll: false,force_rerender: true,- target_scroll_offset: 90,+ target_scroll_offset: 80,},});@@ -1208,7 +1210,7 @@ run_test("fast_track_current_msg_list_to_anchor date", ({override}) => {message_view.fast_track_current_msg_list_to_anchor("date", future_range);assert.deepEqual(selected, {id: 104,- opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 90},+ opts: {then_scroll: true, from_scroll: false, target_scroll_offset: 80},});assert.equal(load_messages_calls, 0);diff --git a/web/tests/narrow_local.test.cjs b/web/tests/narrow_local.test.cjsindex b4aca7fb2f7..3c3afb095d8 100644--- a/web/tests/narrow_local.test.cjs+++ b/web/tests/narrow_local.test.cjs@@ -431,3 +431,101 @@ test_fixture("final corner case", {},expected_msg_ids: [425],});++// The `date` operator is a no-op predicate (see filter.ts), so msg_data+// holds every local message; the operator just slices where we anchor.+// maybe_add_local_messages resolves that anchor locally only when it can+// be certain of the result. 1717977600 is the Unix time for midnight on+// 2024-06-10 (tests run in UTC).+const date_operand = "2024-06-10";+const date_midnight = 1717977600;++test_fixture("date with all local messages before it and newest found", {+ // Every local message predates the date and we've found the newest,+ // so nothing matches on/after the date; select the newest we have.+ filter_terms: [{operator: "date", operand: date_operand}],+ target_id: undefined,+ unread_info: {+ flavor: "not_found",+ },+ has_found_newest: true,+ visibly_empty: false,+ all_messages: [+ {id: 10, topic: "whatever", timestamp: date_midnight - 200000},+ {id: 11, topic: "whatever", timestamp: date_midnight - 100000},+ ],+ expected_id_info: {+ target_id: undefined,+ final_select_id: 11,+ local_select_id: 11,+ },+ expected_msg_ids: [10, 11],+});++test_fixture("date with all local messages before it but newest not found", {+ // Matching messages on/after the date may exist on the server, so we+ // leave final_select_id undefined and defer to the server's anchor.+ filter_terms: [{operator: "date", operand: date_operand}],+ target_id: undefined,+ unread_info: {+ flavor: "not_found",+ },+ has_found_newest: false,+ visibly_empty: false,+ all_messages: [+ {id: 20, topic: "whatever", timestamp: date_midnight - 200000},+ {id: 21, topic: "whatever", timestamp: date_midnight - 100000},+ ],+ expected_id_info: {+ target_id: undefined,+ final_select_id: undefined,+ local_select_id: undefined,+ },+ expected_msg_ids: [20, 21],+});++test_fixture("date with earliest local message already on/after it", {+ // We can't rule out older matching messages on the server (#32150),+ // so we defer to the server's anchor.+ filter_terms: [{operator: "date", operand: date_operand}],+ target_id: undefined,+ unread_info: {+ flavor: "not_found",+ },+ has_found_newest: true,+ visibly_empty: false,+ all_messages: [+ {id: 30, topic: "whatever", timestamp: date_midnight + 100},+ {id: 31, topic: "whatever", timestamp: date_midnight + 100000},+ ],+ expected_id_info: {+ target_id: undefined,+ final_select_id: undefined,+ local_select_id: undefined,+ },+ expected_msg_ids: [30, 31],+});++test_fixture("date falling within the locally-loaded range", {+ // We have local messages both before and on/after the date, but the+ // superset cache need not be contiguous, so the first local message+ // on/after the date may not be the narrow's true first such message;+ // we defer to the server for the accurate anchor.+ filter_terms: [{operator: "date", operand: date_operand}],+ target_id: undefined,+ unread_info: {+ flavor: "not_found",+ },+ has_found_newest: true,+ visibly_empty: false,+ all_messages: [+ {id: 40, topic: "whatever", timestamp: date_midnight - 100000},+ {id: 41, topic: "whatever", timestamp: date_midnight + 100000},+ ],+ expected_id_info: {+ target_id: undefined,+ final_select_id: undefined,+ local_select_id: undefined,+ },+ expected_msg_ids: [40, 41],+});diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjsindex 68f25e93aef..d38e1f8cb5f 100644--- a/web/tests/search_suggestion.test.cjs+++ b/web/tests/search_suggestion.test.cjs@@ -152,6 +152,7 @@ test("basic_get_suggestions_for_spectator", () => {assert.deepEqual(suggestions, ["channels:","channel:",+ "date:","is:resolved","-is:resolved","has:link",@@ -470,6 +471,7 @@ test("empty_query_suggestions", () => {const expected = ["channels:","channel:",+ "date:","is:dm","is:starred","is:mentioned",