Files
2026-11-16 17:26:44 -08:00

73 lines
2.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect, describe } from "bun:test";
import { normaliseWatcherKey, parseWatcherList, watcherKind } from "../src/lib/watcherNames";
/**
* 前端这份归一化必须跟服务端 watchers.ts 的规则逐字一致:分组成员是拿去跟任务
* 的 watchers 直接比字符串的,差一个大小写或者少个域名,筛选就永远是空板——
* 而且不报错。粘贴导入还要负责把"人名打错了"和"这只是分隔符"区分开。
*/
describe("normaliseWatcherKey", () => {
test("裸前缀补上公司域名并转小写", () => {
expect(normaliseWatcherKey("Alice")).toBe("alice@bytedance.com");
expect(normaliseWatcherKey(" bob ")).toBe("bob@bytedance.com");
});
test("别的域名被换成公司域名(跟服务端一样只认前缀)", () => {
expect(normaliseWatcherKey("carol@gmail.com")).toBe("carol@bytedance.com");
});
test("飞书 id 原样返回", () => {
expect(normaliseWatcherKey("oc_abc")).toBe("oc_abc");
expect(normaliseWatcherKey("ou_def")).toBe("ou_def");
});
test("空值和非法 local-part 返回 null", () => {
expect(normaliseWatcherKey("")).toBeNull();
expect(normaliseWatcherKey(" ")).toBeNull();
expect(normaliseWatcherKey("-alice")).toBeNull();
expect(normaliseWatcherKey("alice-")).toBeNull();
expect(normaliseWatcherKey("张三")).toBeNull();
});
});
describe("parseWatcherList — 粘贴导入", () => {
test("换行 / 逗号 / 空格 / 中文标点都算分隔符", () => {
const r = parseWatcherList("alice\nbob, carol;dave、eve,frank;grace");
expect(r.keys).toEqual(
["alice", "bob", "carol", "dave", "eve", "frank", "grace"].map((n) => `${n}@bytedance.com`),
);
expect(r.added).toBe(7);
});
test("已在组里的人算重复,不重复加入", () => {
const r = parseWatcherList("alice, bob", ["alice@bytedance.com"]);
expect(r.keys).toEqual(["bob@bytedance.com"]);
expect(r.dup).toBe(1);
expect(r.added).toBe(1);
});
test("同一次粘贴里的重复也只算一次", () => {
const r = parseWatcherList("alice ALICE@bytedance.com alice@other.com");
expect(r.keys).toEqual(["alice@bytedance.com"]);
expect(r.dup).toBe(2);
});
test("认不出来的 token 被计数而不是被静默吃掉", () => {
const r = parseWatcherList("alice 张三 -bad-");
expect(r.keys).toEqual(["alice@bytedance.com"]);
expect(r.bad).toBe(2);
});
test("空文本不产生任何计数", () => {
expect(parseWatcherList(" \n ")).toEqual({ keys: [], added: 0, dup: 0, bad: 0 });
});
});
describe("watcherKind", () => {
test("邮箱 / 群 / open_id 三分", () => {
expect(watcherKind("alice@bytedance.com")).toBe("email");
expect(watcherKind("oc_abc")).toBe("chat");
expect(watcherKind("ou_abc")).toBe("user");
});
});