87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
// MUST be first: points DATABASE_URL at a closed local port before db_client is
|
|
// evaluated (user_groups_db pulls in the DAL through watchers.ts), so importing
|
|
// the module under test can't reach production.
|
|
import "./setup_env";
|
|
|
|
import { test, expect, describe } from "bun:test";
|
|
import { sanitiseUserGroups, MAX_GROUPS, MAX_MEMBERS, MAX_NAME_LEN } from "../src/user_groups_db";
|
|
|
|
/**
|
|
* 只测这一层的纯逻辑:一条分组从「客户端塞过来的 JSON」变成「能拿去跟 watchers
|
|
* 比对的成员表」的那一步。这里错了不会报错,只会让筛选悄悄筛不出任何东西——
|
|
* 成员写成 `Alice`,任务上存的是 `alice@bytedance.com`,永远对不上。读写共用
|
|
* 同一个 sanitise,所以旧版本写进去的脏数据读出来也是干净的。
|
|
*/
|
|
describe("sanitiseUserGroups — 成员归一化", () => {
|
|
test("裸前缀 / 大写 / 别的域名,统一成公司邮箱", () => {
|
|
const [g] = sanitiseUserGroups([
|
|
{ id: "g1", name: "组", members: ["alice", "Bob@BYTEDANCE.com", "carol@gmail.com"] },
|
|
]);
|
|
expect(g.members).toEqual([
|
|
"alice@bytedance.com",
|
|
"bob@bytedance.com",
|
|
"carol@bytedance.com",
|
|
]);
|
|
});
|
|
|
|
test("飞书 id 原样透传,不当邮箱前缀处理", () => {
|
|
const [g] = sanitiseUserGroups([
|
|
{ id: "g1", name: "组", members: ["oc_abc", "ou_def", "on_ghi"] },
|
|
]);
|
|
expect(g.members).toEqual(["oc_abc", "ou_def", "on_ghi"]);
|
|
});
|
|
|
|
test("归一化之后才去重——同一个人的三种写法只留一条", () => {
|
|
const [g] = sanitiseUserGroups([
|
|
{ id: "g1", name: "组", members: ["alice", "ALICE@bytedance.com", "alice@other.com"] },
|
|
]);
|
|
expect(g.members).toEqual(["alice@bytedance.com"]);
|
|
});
|
|
|
|
test("非法成员被丢掉,而不是让整条分组作废", () => {
|
|
const [g] = sanitiseUserGroups([
|
|
{ id: "g1", name: "组", members: ["alice", "", " ", "-bad-", 42, null, "bob"] },
|
|
]);
|
|
expect(g.members).toEqual(["alice@bytedance.com", "bob@bytedance.com"]);
|
|
});
|
|
});
|
|
|
|
describe("sanitiseUserGroups — 结构与上限", () => {
|
|
test("没有 id 或没有名字的条目直接丢弃", () => {
|
|
expect(sanitiseUserGroups([
|
|
{ id: "", name: "组", members: ["alice"] },
|
|
{ id: "g2", name: " ", members: ["alice"] },
|
|
{ id: "g3", name: "留下", members: ["alice"] },
|
|
"not an object",
|
|
null,
|
|
]).map((g) => g.id)).toEqual(["g3"]);
|
|
});
|
|
|
|
test("id 重复只保留第一条", () => {
|
|
const out = sanitiseUserGroups([
|
|
{ id: "g1", name: "先来", members: ["alice"] },
|
|
{ id: "g1", name: "后到", members: ["bob"] },
|
|
]);
|
|
expect(out).toHaveLength(1);
|
|
expect(out[0].name).toBe("先来");
|
|
});
|
|
|
|
test("名字截断到上限,成员数和分组数各自封顶", () => {
|
|
const long = "名".repeat(MAX_NAME_LEN + 20);
|
|
const many = Array.from({ length: MAX_MEMBERS + 50 }, (_, i) => `u${i}`);
|
|
const [g] = sanitiseUserGroups([{ id: "g1", name: long, members: many }]);
|
|
expect(g.name).toHaveLength(MAX_NAME_LEN);
|
|
expect(g.members).toHaveLength(MAX_MEMBERS);
|
|
|
|
const flood = Array.from({ length: MAX_GROUPS + 10 }, (_, i) => ({
|
|
id: `g${i}`, name: `组${i}`, members: ["alice"],
|
|
}));
|
|
expect(sanitiseUserGroups(flood)).toHaveLength(MAX_GROUPS);
|
|
});
|
|
|
|
test("不是数组就当没有分组", () => {
|
|
expect(sanitiseUserGroups(null)).toEqual([]);
|
|
expect(sanitiseUserGroups({ g1: {} })).toEqual([]);
|
|
});
|
|
});
|