zulip/frontend_tests/node_tests/presence.js

214 lines
5.7 KiB
JavaScript
Raw Normal View History

zrequire('people');
zrequire('presence');
2017-05-24 17:55:31 +02:00
const return_false = function () { return false; };
set_global('server_events', {});
set_global('reload_state', {
is_in_progress: return_false,
});
const OFFLINE_THRESHOLD_SECS = 140;
2017-05-24 17:55:31 +02:00
const me = {
2017-05-24 17:55:31 +02:00
email: 'me@zulip.com',
user_id: 999,
full_name: 'Me Myself',
};
const alice = {
2017-05-24 17:55:31 +02:00
email: 'alice@zulip.com',
user_id: 1,
full_name: 'Alice Smith',
};
const fred = {
2017-05-24 17:55:31 +02:00
email: 'fred@zulip.com',
user_id: 2,
full_name: "Fred Flintstone",
};
const zoe = {
2017-05-24 17:55:31 +02:00
email: 'zoe@example.com',
user_id: 6,
full_name: 'Zoe Yang',
};
const bot = {
email: 'bot@zulip.com',
user_id: 7,
full_name: 'The Bot',
is_bot: true,
};
people.add(me);
people.add(alice);
people.add(fred);
people.add(zoe);
people.add(bot);
2017-05-24 17:55:31 +02:00
people.initialize_current_user(me.user_id);
run_test('my user', () => {
assert.equal(presence.get_status(me.user_id), 'active');
});
run_test('status_from_timestamp', () => {
const status_from_timestamp = presence._status_from_timestamp;
2017-05-24 17:55:31 +02:00
const base_time = 500;
const info = {
2017-05-24 17:55:31 +02:00
website: {
status: "active",
timestamp: base_time,
},
};
let status = status_from_timestamp(
2017-05-24 17:55:31 +02:00
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
info.random_client = {
2017-05-24 17:55:31 +02:00
status: "active",
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
pushable: false,
};
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS, info);
assert.equal(status.status, "active");
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
assert.equal(status.status, "active");
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
assert.equal(status.status, "offline");
info.random_client = {
2017-05-24 17:55:31 +02:00
status: "idle",
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
pushable: true,
};
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS, info);
assert.equal(status.status, "idle");
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
assert.equal(status.status, "active");
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
assert.equal(status.status, "offline");
info.random_client = {
status: "offline",
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
pushable: true,
};
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS, info);
assert.equal(status.status, "offline");
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
assert.equal(status.status, "active"); // website
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS * 2, info);
assert.equal(status.status, "offline");
info.random_client = {
status: "unknown",
timestamp: base_time + OFFLINE_THRESHOLD_SECS / 2,
pushable: true,
};
blueslip.expect('error', 'Unexpected status');
status = status_from_timestamp(
base_time + OFFLINE_THRESHOLD_SECS - 1, info);
blueslip.reset();
assert.equal(status.status, "active"); // website
});
2017-05-24 17:55:31 +02:00
run_test('set_presence_info', () => {
const presences = {};
const base_time = 500;
2017-05-24 17:55:31 +02:00
presences[alice.user_id.toString()] = {
2017-05-24 17:55:31 +02:00
website: {
status: 'active',
timestamp: base_time,
},
};
presences[fred.user_id.toString()] = {
2017-05-24 17:55:31 +02:00
website: {
status: 'idle',
timestamp: base_time,
},
};
presences[me.user_id.toString()] = {
website: {
status: 'active',
timestamp: base_time,
},
};
const params = {};
params.presences = presences;
params.initial_servertime = base_time;
presence.initialize(params);
2017-05-24 17:55:31 +02:00
assert.deepEqual(presence.presence_info.get(alice.user_id),
{ status: 'active', last_active: 500}
2017-05-24 17:55:31 +02:00
);
assert.deepEqual(presence.presence_info.get(fred.user_id),
{ status: 'idle', last_active: 500}
2017-05-24 17:55:31 +02:00
);
assert.deepEqual(presence.presence_info.get(me.user_id),
{ status: 'active', last_active: 500}
);
assert.deepEqual(presence.presence_info.get(zoe.user_id),
{ status: 'offline', last_active: undefined}
2017-05-24 17:55:31 +02:00
);
assert(!presence.presence_info.has(bot.user_id));
// Make it seem like realm has a lot of people
const get_active_human_count = people.get_active_human_count;
people.get_active_human_count = function () { return 1000; };
assert.equal(presence.set_info(presences, base_time), undefined);
people.get_active_human_count = get_active_human_count;
});
2017-05-24 17:55:31 +02:00
run_test('last_active_date', () => {
const unknown_id = 42;
presence.presence_info.clear();
presence.presence_info.set(alice.user_id, { last_active: 500 });
presence.presence_info.set(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});
});
presence: Prep for upcoming changes to server data. In the next commit we're going to change what the server sends for the following: - page_params - server responses to /json/users/me/presence We will **not** yet be changing the format of the data that we get in events when users update their presence. It's also just a bit in flux what our final formats will be for various presence payloads, and different optimizations may lead us to use different data structures in different payloads. So for now we decouple these two things: raw_info: this is intended to represent a snapshot of the latest data from the server, including some data like timestamps that are only used in downstream calculations and not user-facing exports.presence_info: this is calculated info for modules like buddy_data that just need to know active vs. idle and last_active_date Another change that happens here is we rename set_info_for_user to update_info_for_event, which just makes it clear that the function expects data in the "event" format (as opposed to the format for page_params or server responses). As of now keeping the intermediate raw_info data around feels slightly awkward, because we just immediately calculate presence_info for any kind of update. This may be sorta surprising if you just skim the code and see the various timeout constants. You would think we might be automatically expiring "active" statuses in the client due to the simple passage of time, but in fact the precise places we do this are all triggered by new data from the server and we re-calculate statuses immediately. (There are indirect ways that clients have timing logic, since they ask the server for new data at various intervals, but a smarter client could simply expire users on its own, or at least with a more efficient transfer of info between it and the server. One of the thing that complicates client-side logic is that server and client clocks may be out of sync. Also, it's not inherently super expensive to get updates from the server.)
2020-02-07 17:19:03 +01:00
run_test('update_info_from_event', () => {
const server_time = 500;
const info = {
website: {
status: "active",
timestamp: server_time,
},
};
presence.presence_info.delete(alice.user_id);
presence: Prep for upcoming changes to server data. In the next commit we're going to change what the server sends for the following: - page_params - server responses to /json/users/me/presence We will **not** yet be changing the format of the data that we get in events when users update their presence. It's also just a bit in flux what our final formats will be for various presence payloads, and different optimizations may lead us to use different data structures in different payloads. So for now we decouple these two things: raw_info: this is intended to represent a snapshot of the latest data from the server, including some data like timestamps that are only used in downstream calculations and not user-facing exports.presence_info: this is calculated info for modules like buddy_data that just need to know active vs. idle and last_active_date Another change that happens here is we rename set_info_for_user to update_info_for_event, which just makes it clear that the function expects data in the "event" format (as opposed to the format for page_params or server responses). As of now keeping the intermediate raw_info data around feels slightly awkward, because we just immediately calculate presence_info for any kind of update. This may be sorta surprising if you just skim the code and see the various timeout constants. You would think we might be automatically expiring "active" statuses in the client due to the simple passage of time, but in fact the precise places we do this are all triggered by new data from the server and we re-calculate statuses immediately. (There are indirect ways that clients have timing logic, since they ask the server for new data at various intervals, but a smarter client could simply expire users on its own, or at least with a more efficient transfer of info between it and the server. One of the thing that complicates client-side logic is that server and client clocks may be out of sync. Also, it's not inherently super expensive to get updates from the server.)
2020-02-07 17:19:03 +01:00
presence.update_info_from_event(alice.user_id, info, server_time);
const expected = { status: 'active', last_active: 500 };
assert.deepEqual(presence.presence_info.get(alice.user_id), expected);
});