2017-11-08 18:38:52 +01:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('presence');
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2018-07-31 07:17:46 +02:00
|
|
|
var return_false = function () { return false; };
|
|
|
|
|
2017-06-16 01:53:12 +02:00
|
|
|
set_global('server_events', {});
|
|
|
|
set_global('blueslip', {});
|
2018-08-04 15:40:25 +02:00
|
|
|
set_global('reload_state', {
|
2018-07-31 07:17:46 +02:00
|
|
|
is_in_progress: return_false,
|
|
|
|
});
|
2017-06-16 01:53:12 +02:00
|
|
|
|
2017-05-24 17:55:31 +02:00
|
|
|
var OFFLINE_THRESHOLD_SECS = 140;
|
|
|
|
|
|
|
|
var me = {
|
|
|
|
email: 'me@zulip.com',
|
|
|
|
user_id: 999,
|
|
|
|
full_name: 'Me Myself',
|
|
|
|
};
|
|
|
|
|
|
|
|
var alice = {
|
|
|
|
email: 'alice@zulip.com',
|
|
|
|
user_id: 1,
|
|
|
|
full_name: 'Alice Smith',
|
|
|
|
};
|
|
|
|
|
|
|
|
var fred = {
|
|
|
|
email: 'fred@zulip.com',
|
|
|
|
user_id: 2,
|
|
|
|
full_name: "Fred Flintstone",
|
|
|
|
};
|
|
|
|
|
|
|
|
var zoe = {
|
|
|
|
email: 'zoe@example.com',
|
|
|
|
user_id: 6,
|
|
|
|
full_name: 'Zoe Yang',
|
|
|
|
};
|
|
|
|
|
2017-06-07 18:36:26 +02:00
|
|
|
var bot = {
|
|
|
|
email: 'bot@zulip.com',
|
|
|
|
user_id: 7,
|
|
|
|
full_name: 'The Bot',
|
|
|
|
is_bot: true,
|
|
|
|
};
|
|
|
|
|
2017-05-24 17:55:31 +02:00
|
|
|
people.add_in_realm(me);
|
|
|
|
people.add_in_realm(alice);
|
|
|
|
people.add_in_realm(fred);
|
|
|
|
people.add_in_realm(zoe);
|
2017-06-07 18:36:26 +02:00
|
|
|
people.add_in_realm(bot);
|
2017-05-24 17:55:31 +02:00
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2019-01-03 16:44:06 +01:00
|
|
|
run_test('my user', () => {
|
|
|
|
assert.equal(presence.get_status(me.user_id), 'active');
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('on_mobile_property', () => {
|
2017-05-24 17:55:31 +02:00
|
|
|
// TODO: move this test to a new test module directly testing presence.js
|
|
|
|
var status_from_timestamp = presence._status_from_timestamp;
|
|
|
|
|
|
|
|
var base_time = 500;
|
|
|
|
var info = {
|
|
|
|
website: {
|
|
|
|
status: "active",
|
|
|
|
timestamp: base_time,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
var status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
|
|
|
|
info.Android = {
|
|
|
|
status: "active",
|
|
|
|
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
|
|
|
|
pushable: false,
|
|
|
|
};
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS, info);
|
|
|
|
assert.equal(status.mobile, true);
|
|
|
|
assert.equal(status.status, "active");
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
assert.equal(status.status, "active");
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
assert.equal(status.status, "offline");
|
|
|
|
|
|
|
|
info.Android = {
|
|
|
|
status: "idle",
|
|
|
|
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
|
|
|
|
pushable: true,
|
|
|
|
};
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS, info);
|
|
|
|
assert.equal(status.mobile, true);
|
|
|
|
assert.equal(status.status, "idle");
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
assert.equal(status.status, "active");
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
|
|
|
|
assert.equal(status.mobile, true);
|
|
|
|
assert.equal(status.status, "offline");
|
|
|
|
|
2017-06-17 00:04:27 +02:00
|
|
|
info.Android = {
|
|
|
|
status: "offline",
|
|
|
|
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
|
|
|
|
pushable: true,
|
|
|
|
};
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS, info);
|
|
|
|
assert.equal(status.mobile, true);
|
|
|
|
assert.equal(status.status, "offline");
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
assert.equal(status.status, "active"); // website
|
|
|
|
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
|
|
|
|
assert.equal(status.mobile, true);
|
|
|
|
assert.equal(status.status, "offline");
|
|
|
|
|
|
|
|
info.Android = {
|
|
|
|
status: "unknown",
|
|
|
|
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
|
|
|
|
pushable: true,
|
|
|
|
};
|
|
|
|
var called = false;
|
|
|
|
blueslip.error = function () {
|
|
|
|
assert.equal(arguments[0], 'Unexpected status');
|
|
|
|
assert.deepEqual(arguments[1].presence_object, info.Android);
|
|
|
|
assert.equal(arguments[2], undefined);
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
status = status_from_timestamp(
|
|
|
|
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
|
|
|
|
assert.equal(status.mobile, false);
|
|
|
|
assert.equal(status.status, "active"); // website
|
|
|
|
assert(called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('set_presence_info', () => {
|
2017-05-24 17:55:31 +02:00
|
|
|
var presences = {};
|
|
|
|
var base_time = 500;
|
|
|
|
|
|
|
|
presences[alice.email] = {
|
|
|
|
website: {
|
|
|
|
status: 'active',
|
|
|
|
timestamp: base_time,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
presences[fred.email] = {
|
|
|
|
website: {
|
|
|
|
status: 'idle',
|
|
|
|
timestamp: base_time,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-09-08 14:25:06 +02:00
|
|
|
presences[me.email] = {
|
|
|
|
website: {
|
|
|
|
status: 'active',
|
|
|
|
timestamp: base_time,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-05-24 17:55:31 +02:00
|
|
|
presence.set_info(presences, base_time);
|
|
|
|
|
|
|
|
assert.deepEqual(presence.presence_info[alice.user_id],
|
2018-05-07 03:30:13 +02:00
|
|
|
{ status: 'active', mobile: false, last_active: 500}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(presence.presence_info[fred.user_id],
|
2018-05-07 03:30:13 +02:00
|
|
|
{ status: 'idle', mobile: false, last_active: 500}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
|
|
|
|
2018-09-08 14:41:41 +02:00
|
|
|
assert.deepEqual(presence.presence_info[me.user_id],
|
|
|
|
{ status: 'active', mobile: false, last_active: 500}
|
|
|
|
);
|
|
|
|
|
2017-05-24 17:55:31 +02:00
|
|
|
assert.deepEqual(presence.presence_info[zoe.user_id],
|
2018-05-07 03:30:13 +02:00
|
|
|
{ status: 'offline', mobile: false, last_active: undefined}
|
2017-05-24 17:55:31 +02:00
|
|
|
);
|
2017-06-07 18:36:26 +02:00
|
|
|
|
|
|
|
assert(!presence.presence_info[bot.user_id]);
|
2017-06-16 01:53:12 +02:00
|
|
|
|
2017-06-16 02:18:34 +02:00
|
|
|
// Make it seem like realm has a lot of people
|
|
|
|
var get_realm_count = people.get_realm_count;
|
|
|
|
people.get_realm_count = function () { return 1000; };
|
|
|
|
assert.equal(presence.set_info(presences, base_time), undefined);
|
|
|
|
people.get_realm_count = get_realm_count;
|
|
|
|
|
2017-06-16 01:53:12 +02:00
|
|
|
var unknown = {
|
|
|
|
email: 'unknown@zulip.com',
|
|
|
|
user_id: 42,
|
|
|
|
full_name: 'Unknown Name',
|
|
|
|
};
|
|
|
|
presences[unknown.email] = {};
|
|
|
|
|
|
|
|
server_events.suspect_offline = false;
|
|
|
|
blueslip.error = function (msg) {
|
|
|
|
assert.equal(msg, 'Unknown email in presence data: unknown@zulip.com');
|
|
|
|
};
|
|
|
|
presence.set_info(presences, base_time);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-24 17:55:31 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('last_active_date', () => {
|
2017-06-16 01:54:49 +02:00
|
|
|
var unknown_id = 42;
|
|
|
|
presence.presence_info = {
|
|
|
|
1: { last_active: 500 }, // alice.user_id
|
|
|
|
2: {}, // fred.user_id
|
|
|
|
};
|
|
|
|
set_global('XDate', function (ms) { return {seconds: ms}; });
|
|
|
|
|
|
|
|
assert.equal(presence.last_active_date(unknown_id), undefined);
|
|
|
|
assert.equal(presence.last_active_date(fred.user_id), undefined);
|
|
|
|
assert.deepEqual(presence.last_active_date(alice.user_id), {seconds: 500000});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-16 01:54:49 +02:00
|
|
|
|
2018-12-18 18:30:40 +01:00
|
|
|
run_test('set_info_for_user', () => {
|
2017-06-17 00:06:21 +02:00
|
|
|
var server_time = 500;
|
|
|
|
var info = {
|
|
|
|
website: {
|
|
|
|
status: "active",
|
|
|
|
timestamp: server_time,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
presence.presence_info[alice.user_id] = undefined;
|
2018-12-18 18:30:40 +01:00
|
|
|
presence.set_info_for_user(alice.user_id, info, server_time);
|
2017-06-17 00:06:21 +02:00
|
|
|
|
|
|
|
var expected = { status: 'active', mobile: false, last_active: 500 };
|
|
|
|
assert.deepEqual(presence.presence_info[alice.user_id], expected);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|