2021-04-04 22:31:02 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-04-04 22:31:02 +02:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2021-04-04 22:31:02 +02:00
|
|
|
|
|
|
|
const user_groups = zrequire("user_groups");
|
|
|
|
const user_group_pill = zrequire("user_group_pill");
|
2024-07-11 10:45:15 +02:00
|
|
|
const people = zrequire("people");
|
|
|
|
|
|
|
|
const user1 = {
|
|
|
|
user_id: 10,
|
|
|
|
email: "user1@example.com",
|
|
|
|
full_name: "User One",
|
|
|
|
};
|
|
|
|
people.add_active_user(user1);
|
|
|
|
|
|
|
|
const user2 = {
|
|
|
|
user_id: 20,
|
|
|
|
email: "user2@example.com",
|
|
|
|
full_name: "User Two",
|
|
|
|
};
|
|
|
|
people.add_active_user(user2);
|
|
|
|
|
|
|
|
const user3 = {
|
|
|
|
user_id: 30,
|
|
|
|
email: "user3@example.com",
|
|
|
|
full_name: "User Three",
|
|
|
|
};
|
|
|
|
people.add_active_user(user3);
|
|
|
|
|
|
|
|
const user4 = {
|
|
|
|
user_id: 40,
|
|
|
|
email: "user4@example.com",
|
|
|
|
full_name: "User Four",
|
|
|
|
};
|
|
|
|
people.add_active_user(user4);
|
|
|
|
|
|
|
|
const user5 = {
|
|
|
|
user_id: 50,
|
|
|
|
email: "user5@example.com",
|
|
|
|
full_name: "User Five",
|
|
|
|
};
|
|
|
|
people.add_active_user(user5);
|
2021-04-04 22:31:02 +02:00
|
|
|
|
|
|
|
const admins = {
|
|
|
|
name: "Admins",
|
|
|
|
description: "foo",
|
2022-02-22 15:54:14 +01:00
|
|
|
id: 101,
|
2021-04-04 22:31:02 +02:00
|
|
|
members: [10, 20],
|
|
|
|
};
|
|
|
|
const testers = {
|
|
|
|
name: "Testers",
|
|
|
|
description: "bar",
|
2022-02-22 15:54:14 +01:00
|
|
|
id: 102,
|
|
|
|
members: [20, 50, 30, 40],
|
2021-04-04 22:31:02 +02:00
|
|
|
};
|
2024-07-08 18:11:09 +02:00
|
|
|
const everyone = {
|
|
|
|
name: "role:everyone",
|
|
|
|
description: "Everyone",
|
|
|
|
id: 103,
|
|
|
|
members: [],
|
|
|
|
direct_subgroup_ids: [101, 102],
|
|
|
|
};
|
2021-04-04 22:31:02 +02:00
|
|
|
|
|
|
|
const admins_pill = {
|
2024-05-09 09:24:02 +02:00
|
|
|
group_id: admins.id,
|
2021-04-04 22:31:02 +02:00
|
|
|
group_name: admins.name,
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user_group",
|
2021-04-04 22:31:02 +02:00
|
|
|
};
|
|
|
|
const testers_pill = {
|
2024-05-09 09:24:02 +02:00
|
|
|
group_id: testers.id,
|
2021-04-04 22:31:02 +02:00
|
|
|
group_name: testers.name,
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user_group",
|
2021-04-04 22:31:02 +02:00
|
|
|
};
|
2024-07-08 18:11:09 +02:00
|
|
|
const everyone_pill = {
|
|
|
|
group_id: everyone.id,
|
|
|
|
group_name: everyone.name,
|
|
|
|
type: "user_group",
|
|
|
|
// While we can programmatically set the user count below,
|
|
|
|
// calculating it would almost mimic the entire display function
|
|
|
|
// here, reducing the usefulness of the test.
|
|
|
|
};
|
2021-04-04 22:31:02 +02:00
|
|
|
|
2024-07-08 18:11:09 +02:00
|
|
|
const groups = [admins, testers, everyone];
|
2021-04-04 22:31:02 +02:00
|
|
|
for (const group of groups) {
|
|
|
|
user_groups.add(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
run_test("create_item", () => {
|
|
|
|
function test_create_item(group_name, current_items, expected_item) {
|
|
|
|
const item = user_group_pill.create_item_from_group_name(group_name, current_items);
|
|
|
|
assert.deepEqual(item, expected_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_create_item(" admins ", [], admins_pill);
|
|
|
|
test_create_item("admins", [testers_pill], admins_pill);
|
|
|
|
test_create_item("admins", [admins_pill], undefined);
|
|
|
|
test_create_item("unknown", [], undefined);
|
2024-07-08 18:11:09 +02:00
|
|
|
test_create_item("role:everyone", [], everyone_pill);
|
2021-04-04 22:31:02 +02:00
|
|
|
});
|
|
|
|
|
2024-07-30 06:13:31 +02:00
|
|
|
run_test("display_value", () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
user_group_pill.display_pill(admins),
|
|
|
|
"translated HTML: " + admins.name + ": " + admins.members.length + " users",
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
user_group_pill.display_pill(testers),
|
|
|
|
"translated HTML: " + testers.name + ": " + testers.members.length + " users",
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
user_group_pill.display_pill(everyone),
|
|
|
|
"translated HTML: translated: Everyone: 5 users",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-04 22:31:02 +02:00
|
|
|
run_test("get_stream_id", () => {
|
|
|
|
assert.equal(user_group_pill.get_group_name_from_item(admins_pill), admins.name);
|
|
|
|
});
|
2022-02-22 15:54:14 +01:00
|
|
|
|
|
|
|
run_test("get_user_ids", () => {
|
2024-07-08 18:11:09 +02:00
|
|
|
let items = [admins_pill, testers_pill];
|
2022-02-22 15:54:14 +01:00
|
|
|
const widget = {items: () => items};
|
|
|
|
|
2024-07-08 18:11:09 +02:00
|
|
|
let user_ids = user_group_pill.get_user_ids(widget);
|
|
|
|
assert.deepEqual(user_ids, [10, 20, 30, 40, 50]);
|
|
|
|
|
|
|
|
// Test whether subgroup members are included or not.
|
|
|
|
items = [everyone_pill];
|
|
|
|
user_ids = user_group_pill.get_user_ids(widget);
|
2022-02-22 15:54:14 +01:00
|
|
|
assert.deepEqual(user_ids, [10, 20, 30, 40, 50]);
|
2024-07-11 10:45:15 +02:00
|
|
|
|
|
|
|
// Deactivated users should be excluded.
|
|
|
|
people.deactivate(user5);
|
|
|
|
user_ids = user_group_pill.get_user_ids(widget);
|
|
|
|
assert.deepEqual(user_ids, [10, 20, 30, 40]);
|
|
|
|
people.add_active_user(user5);
|
2022-02-22 15:54:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test("get_group_ids", () => {
|
2024-07-08 18:11:09 +02:00
|
|
|
const items = [admins_pill, everyone_pill];
|
2022-02-22 15:54:14 +01:00
|
|
|
const widget = {items: () => items};
|
|
|
|
|
2024-07-08 18:11:09 +02:00
|
|
|
// Subgroups should not be part of the results, we use `everyone_pill` to test that.
|
2022-02-22 15:54:14 +01:00
|
|
|
const group_ids = user_group_pill.get_group_ids(widget);
|
2024-07-08 18:11:09 +02:00
|
|
|
assert.deepEqual(group_ids, [101, 103]);
|
2022-02-22 15:54:14 +01:00
|
|
|
});
|
2023-07-15 19:16:44 +02:00
|
|
|
|
|
|
|
run_test("append_user_group", () => {
|
|
|
|
const items = [admins_pill];
|
|
|
|
const widget = {
|
|
|
|
appendValidatedData(group) {
|
|
|
|
assert.deepEqual(group, testers_pill);
|
|
|
|
items.push(testers_pill);
|
|
|
|
},
|
|
|
|
clear_text() {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const group = {
|
|
|
|
...testers,
|
|
|
|
members: new Set(testers.members),
|
|
|
|
};
|
|
|
|
user_group_pill.append_user_group(group, widget);
|
|
|
|
assert.deepEqual(items, [admins_pill, testers_pill]);
|
|
|
|
});
|