node tests: Clean up buddy_data tests.

We mostly add the test() wrapper to give
each test a clean slate.
This commit is contained in:
Steve Howell 2021-03-14 09:18:07 +00:00 committed by Steve Howell
parent 85bd196bfc
commit 569a68a057
1 changed files with 48 additions and 39 deletions

View File

@ -79,35 +79,34 @@ const bot_with_owner = {
bot_owner_full_name: "Human Myself", bot_owner_full_name: "Human Myself",
}; };
function make_people() { function add_canned_users() {
// sanity check people.add_active_user(alice);
assert.equal(mark.user_id, 1005);
for (const i of _.range(mark.user_id + 1, 2000)) {
const person = {
user_id: i,
full_name: `Human ${i}`,
email: `person${i}@example.com`,
};
people.add_active_user(person);
}
people.add_active_user(bot); people.add_active_user(bot);
people.add_active_user(bot_with_owner); people.add_active_user(bot_with_owner);
people.add_active_user(fred);
people.add_active_user(jill);
people.add_active_user(mark);
people.add_active_user(old_user);
people.add_active_user(selma);
}
function test(label, f) {
run_test(label, (override) => {
user_status.initialize({user_status: {}});
presence.presence_info.clear();
people.init();
people.add_active_user(me);
people.initialize_current_user(me.user_id);
f(override);
});
}
test("huddle_fraction_present", () => {
people.add_active_user(alice); people.add_active_user(alice);
people.add_active_user(fred); people.add_active_user(fred);
people.add_active_user(jill); people.add_active_user(jill);
people.add_active_user(mark); people.add_active_user(mark);
people.add_active_user(selma);
people.add_active_user(me);
people.add_active_user(old_user);
people.initialize_current_user(me.user_id);
}
make_people();
run_test("huddle_fraction_present", () => {
let huddle = "alice@zulip.com,fred@zulip.com,jill@zulip.com,mark@zulip.com"; let huddle = "alice@zulip.com,fred@zulip.com,jill@zulip.com,mark@zulip.com";
huddle = people.emails_strings_to_user_ids_string(huddle); huddle = people.emails_strings_to_user_ids_string(huddle);
@ -147,8 +146,9 @@ function set_presence(user_id, status) {
}); });
} }
run_test("user_circle", () => { test("user_circle", () => {
presence.presence_info.clear(); add_canned_users();
set_presence(selma.user_id, "active"); set_presence(selma.user_id, "active");
set_presence(me.user_id, "active"); set_presence(me.user_id, "active");
@ -165,8 +165,7 @@ run_test("user_circle", () => {
assert.equal(buddy_data.get_user_circle_class(me.user_id), "user_circle_green"); assert.equal(buddy_data.get_user_circle_class(me.user_id), "user_circle_green");
}); });
run_test("buddy_status", () => { test("buddy_status", () => {
presence.presence_info.clear();
set_presence(selma.user_id, "active"); set_presence(selma.user_id, "active");
set_presence(me.user_id, "active"); set_presence(me.user_id, "active");
@ -183,7 +182,9 @@ run_test("buddy_status", () => {
assert.equal(buddy_data.buddy_status(me.user_id), "active"); assert.equal(buddy_data.buddy_status(me.user_id), "active");
}); });
run_test("title_data", () => { test("title_data", () => {
add_canned_users();
// Groups // Groups
let is_group = true; let is_group = true;
const user_ids_string = "9999,1000"; const user_ids_string = "9999,1000";
@ -236,8 +237,8 @@ run_test("title_data", () => {
assert.deepEqual(buddy_data.get_title_data(old_user.user_id, is_group), expected_data); assert.deepEqual(buddy_data.get_title_data(old_user.user_id, is_group), expected_data);
}); });
run_test("simple search", () => { test("simple search", () => {
presence.presence_info.clear(); add_canned_users();
set_presence(selma.user_id, "active"); set_presence(selma.user_id, "active");
set_presence(me.user_id, "active"); set_presence(me.user_id, "active");
@ -247,8 +248,19 @@ run_test("simple search", () => {
assert.deepEqual(user_ids, [selma.user_id]); assert.deepEqual(user_ids, [selma.user_id]);
}); });
run_test("bulk_data_hacks", () => { test("bulk_data_hacks", () => {
presence.presence_info.clear(); // sanity check
assert.equal(mark.user_id, 1005);
for (const i of _.range(mark.user_id + 1, 2000)) {
const person = {
user_id: i,
full_name: `Human ${i}`,
email: `person${i}@example.com`,
};
people.add_active_user(person);
}
add_canned_users();
// Make 400 of the users active // Make 400 of the users active
set_presence(selma.user_id, "active"); set_presence(selma.user_id, "active");
@ -295,8 +307,7 @@ run_test("bulk_data_hacks", () => {
assert.equal(user_ids.length, 700); assert.equal(user_ids.length, 700);
}); });
run_test("user_status", () => { test("user_status", () => {
presence.presence_info.clear();
user_status.initialize({user_status: []}); user_status.initialize({user_status: []});
set_presence(me.user_id, "active"); set_presence(me.user_id, "active");
assert.equal(buddy_data.get_my_user_status(me.user_id), "translated: (you)"); assert.equal(buddy_data.get_my_user_status(me.user_id), "translated: (you)");
@ -306,8 +317,8 @@ run_test("user_status", () => {
assert.equal(buddy_data.get_my_user_status(me.user_id), "translated: (you)"); assert.equal(buddy_data.get_my_user_status(me.user_id), "translated: (you)");
}); });
run_test("level", () => { test("level", () => {
presence.presence_info.clear(); add_canned_users();
assert.equal(buddy_data.level(me.user_id), 0); assert.equal(buddy_data.level(me.user_id), 0);
assert.equal(buddy_data.level(selma.user_id), 3); assert.equal(buddy_data.level(selma.user_id), 3);
@ -333,9 +344,7 @@ run_test("level", () => {
assert.equal(buddy_data.level(selma.user_id), 3); assert.equal(buddy_data.level(selma.user_id), 3);
}); });
run_test("user_last_seen_time_status", (override) => { test("user_last_seen_time_status", (override) => {
presence.presence_info.clear();
set_presence(selma.user_id, "active"); set_presence(selma.user_id, "active");
set_presence(me.user_id, "active"); set_presence(me.user_id, "active");
@ -362,7 +371,7 @@ run_test("user_last_seen_time_status", (override) => {
assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id), "May 12"); assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id), "May 12");
}); });
run_test("error handling", (override) => { test("error handling", (override) => {
override(presence, "get_user_ids", () => [42]); override(presence, "get_user_ids", () => [42]);
blueslip.expect("error", "Unknown user_id in get_by_user_id: 42"); blueslip.expect("error", "Unknown user_id in get_by_user_id: 42");
blueslip.expect("warn", "Got user_id in presence but not people: 42"); blueslip.expect("warn", "Got user_id in presence but not people: 42");