2018-05-02 15:31:36 +02:00
|
|
|
zrequire('Filter', 'js/filter');
|
|
|
|
zrequire('people');
|
|
|
|
zrequire('stream_data');
|
|
|
|
zrequire('unread');
|
|
|
|
zrequire('util');
|
|
|
|
set_global('blueslip', global.make_zblueslip());
|
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
set_global('message_store', {});
|
|
|
|
set_global('page_params', {});
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
set_global('muting', {
|
|
|
|
is_topic_muted: () => false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// The main code we are testing lives here.
|
|
|
|
zrequire('narrow_state');
|
|
|
|
|
|
|
|
const alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 11,
|
|
|
|
full_name: 'Alice',
|
|
|
|
};
|
|
|
|
|
|
|
|
people.init();
|
|
|
|
people.add(alice);
|
|
|
|
people.is_my_user_id = () => false;
|
|
|
|
|
|
|
|
function set_filter(terms) {
|
|
|
|
const filter = new Filter(terms);
|
|
|
|
narrow_state.set_current_filter(filter);
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:58:00 +02:00
|
|
|
function assert_unread_info(expected) {
|
|
|
|
assert.deepEqual(narrow_state.get_first_unread_info(), expected);
|
|
|
|
}
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
function candidate_ids() {
|
|
|
|
return narrow_state._possible_unread_message_ids();
|
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_unread_ids', () => {
|
2018-05-02 15:31:36 +02:00
|
|
|
var unread_ids;
|
|
|
|
var terms;
|
|
|
|
|
|
|
|
const sub = {
|
|
|
|
name: 'My Stream',
|
|
|
|
stream_id: 55,
|
|
|
|
};
|
2018-05-09 03:28:29 +02:00
|
|
|
|
|
|
|
const stream_msg = {
|
|
|
|
id: 101,
|
|
|
|
type: 'stream',
|
|
|
|
stream_id: sub.stream_id,
|
2018-12-23 16:49:14 +01:00
|
|
|
topic: 'my topic',
|
2018-05-09 03:28:29 +02:00
|
|
|
unread: true,
|
|
|
|
mentioned: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const private_msg = {
|
|
|
|
id: 102,
|
|
|
|
type: 'private',
|
|
|
|
unread: true,
|
|
|
|
display_recipient: [
|
|
|
|
{user_id: alice.user_id},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
stream_data.add_sub(sub.name, sub);
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.equal(unread_ids, undefined);
|
|
|
|
|
|
|
|
terms = [
|
2018-05-31 16:46:39 +02:00
|
|
|
{operator: 'search', operand: 'whatever'},
|
2018-05-02 15:31:36 +02:00
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.equal(unread_ids, undefined);
|
2018-05-03 18:58:00 +02:00
|
|
|
assert_unread_info({flavor: 'cannot_compute'});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-31 16:46:39 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'bogus_operator', operand: 'me@example.com'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
assert_unread_info({flavor: 'not_found'});
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'stream', operand: 'bogus'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'stream', operand: sub.name},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2018-05-03 18:58:00 +02:00
|
|
|
assert_unread_info({flavor: 'not_found'});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-09 03:28:29 +02:00
|
|
|
unread.process_loaded_messages([stream_msg]);
|
2018-05-09 00:08:24 +02:00
|
|
|
message_store.get = (msg_id) => {
|
|
|
|
assert.equal(msg_id, stream_msg.id);
|
|
|
|
return stream_msg;
|
|
|
|
};
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-03 18:58:00 +02:00
|
|
|
assert_unread_info({
|
|
|
|
flavor: 'found',
|
2018-05-09 03:28:29 +02:00
|
|
|
msg_id: stream_msg.id,
|
2018-05-03 18:58:00 +02:00
|
|
|
});
|
2018-05-02 15:31:36 +02:00
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'stream', operand: 'bogus'},
|
|
|
|
{operator: 'topic', operand: 'my topic'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'stream', operand: sub.name},
|
|
|
|
{operator: 'topic', operand: 'my topic'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'is', operand: 'mentioned'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-10 13:55:56 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'sender', operand: 'me@example.com'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
// note that our candidate ids are just "all" ids now
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id]);
|
|
|
|
|
|
|
|
// this actually does filtering
|
|
|
|
assert_unread_info({flavor: 'not_found'});
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'pm-with', operand: 'alice@example.com'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
|
|
|
|
2018-05-09 03:28:29 +02:00
|
|
|
unread.process_loaded_messages([private_msg]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
message_store.get = (msg_id) => {
|
|
|
|
assert.equal(msg_id, private_msg.id);
|
|
|
|
return private_msg;
|
|
|
|
};
|
|
|
|
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [private_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-09 00:08:24 +02:00
|
|
|
assert_unread_info({
|
|
|
|
flavor: 'found',
|
|
|
|
msg_id: private_msg.id,
|
|
|
|
});
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-09 03:28:29 +02:00
|
|
|
assert.deepEqual(unread_ids, [private_msg.id]);
|
2018-05-02 15:31:36 +02:00
|
|
|
|
2018-05-31 16:46:39 +02:00
|
|
|
// For a negated search, our candidate ids will be all
|
|
|
|
// unread messages, even ones that don't pass the filter.
|
|
|
|
terms = [
|
|
|
|
{operator: 'is', operand: 'private', negated: true},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
unread_ids = candidate_ids();
|
|
|
|
assert.deepEqual(unread_ids, [stream_msg.id, private_msg.id]);
|
|
|
|
|
2018-05-02 15:31:36 +02:00
|
|
|
terms = [
|
|
|
|
{operator: 'pm-with', operand: 'bob@example.com'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
|
|
|
|
blueslip.set_test_data('warn', 'Unknown emails: bob@example.com');
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-02 15:31:36 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2018-05-03 22:57:07 +02:00
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'is', operand: 'starred'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
2018-05-08 23:06:00 +02:00
|
|
|
unread_ids = candidate_ids();
|
2018-05-03 22:57:07 +02:00
|
|
|
assert.deepEqual(unread_ids, []);
|
2018-05-09 00:08:24 +02:00
|
|
|
|
|
|
|
terms = [
|
|
|
|
{operator: 'search', operand: 'needle'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
|
2018-05-31 12:55:19 +02:00
|
|
|
assert_unread_info({
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
});
|
|
|
|
|
|
|
|
narrow_state.reset_current_filter();
|
2018-05-09 00:08:24 +02:00
|
|
|
blueslip.set_test_data('error', 'unexpected call to get_first_unread_info');
|
|
|
|
assert_unread_info({
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-05-31 16:46:39 +02:00
|
|
|
|
|
|
|
run_test('defensive code', () => {
|
|
|
|
// Test defensive code. We actually avoid calling
|
|
|
|
// _possible_unread_message_ids for any case where we
|
|
|
|
// couldn't compute the unread message ids, but that
|
|
|
|
// invariant is hard to future-proof.
|
|
|
|
narrow_state._possible_unread_message_ids = () => undefined;
|
|
|
|
var terms = [
|
|
|
|
{operator: 'some-unhandled-case', operand: 'whatever'},
|
|
|
|
];
|
|
|
|
set_filter(terms);
|
|
|
|
assert_unread_info({
|
|
|
|
flavor: 'cannot_compute',
|
|
|
|
});
|
|
|
|
});
|