Files
streamline/control-server/test/bits_tokens.test.ts
T
2026-11-16 17:26:44 -08:00

154 lines
8.0 KiB
TypeScript
Raw 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.
// MUST be first: points DATABASE_URL at a closed local port before db_client is
// evaluated, so importing the module under test can't reach production.
import "./setup_env";
import { readFileSync } from "node:fs";
import { test, expect, describe } from "bun:test";
import { sanitiseBitsToken, normBitsPrefix } from "../src/bits_tokens_db";
import { BITS_FIXED_OPERATOR, BUG_AUTO_MR_NONE_CREATED, bitsPersonalTokenSubjects, bugAutoMrBlocksWalk, orderedBugMrTokenSubjects, bugMrOwnerEmails, classifyBitsTokenProbe, classifyBitsCreateProbe, bitsCreateProbeBranch, bitsCreateProbeRepos } from "../src/bits";
describe("sanitiseBitsToken", () => {
test("strips Bearer prefix and whitespace", () => {
expect(sanitiseBitsToken(" Bearer abcdef0123456789 ")).toBe("abcdef0123456789");
expect(sanitiseBitsToken("bearer ABCDEF0123456789")).toBe("ABCDEF0123456789");
});
test("rejects empty / too short / too long / junk charset", () => {
expect(sanitiseBitsToken("")).toBeNull();
expect(sanitiseBitsToken("abc")).toBeNull();
expect(sanitiseBitsToken("not a token!!!")).toBeNull();
expect(sanitiseBitsToken(null)).toBeNull();
expect(sanitiseBitsToken("x".repeat(300))).toBeNull();
});
});
describe("classifyBitsTokenProbe", () => {
test("Bits 明说 Token 坏了才算 invalid", () => {
expect(classifyBitsTokenProbe({ code: 403, data: null, message: "Invalid token" })).toBe("invalid");
expect(classifyBitsTokenProbe({ code: 401, data: null, message: "Lack of token, If you have any questions..." })).toBe("invalid");
});
test("成功算 valid;看不到探针 MR、解析失败都放行", () => {
expect(classifyBitsTokenProbe({ code: 200, data: {} })).toBe("valid");
expect(classifyBitsTokenProbe({ code: 403, message: "no permission" })).toBe("unknown");
expect(classifyBitsTokenProbe({ code: 404, message: "merge request not found" })).toBe("unknown");
expect(classifyBitsTokenProbe(null)).toBe("unknown");
});
});
describe("classifyBitsCreateProbe", () => {
test("走到 GitLab 才因分支不存在失败 = create 通", () => {
expect(classifyBitsCreateProbe({ code: 400, message: 'Gitlab Error: TikTok {"message":"input: createMergeRequest resource Branch is not found: merge request source branch not found\\n"}' })).toBe("ok");
});
test("info 认、create 回 Invalid token = Bits 空间未放行", () => {
expect(classifyBitsCreateProbe({ code: 403, data: null, message: "Invalid token" })).toBe("create_blocked");
});
test("权限不足与未知", () => {
expect(classifyBitsCreateProbe({ code: 400, message: "TikTok 你可能没有权限访问这个仓库." })).toBe("no_permission");
expect(classifyBitsCreateProbe({ code: 500, message: "internal error" })).toBe("unknown");
expect(classifyBitsCreateProbe(null)).toBe("unknown");
});
test("探针分支每次都不同,且探的是 Android / iOS 两个交付仓", () => {
expect(bitsCreateProbeBranch()).not.toBe(bitsCreateProbeBranch());
expect(bitsCreateProbeBranch()).toStartWith("streamline-token-probe/nonexistent-");
expect(bitsCreateProbeRepos().map((r) => r.key)).toEqual(["tiktok-android", "tiktok-ios"]);
});
});
describe("normBitsPrefix", () => {
test("email → local-part", () => {
expect(normBitsPrefix("Jiaopengwen@bytedance.com")).toBe("jiaopengwen");
expect(normBitsPrefix("jiaopengwen")).toBe("jiaopengwen");
});
});
describe("bitsPersonalTokenSubjects", () => {
test("只认点击者,负责人不是候选", () => {
expect(bitsPersonalTokenSubjects("JohnLee@bytedance.com")).toEqual(["johnlee"]);
expect(bitsPersonalTokenSubjects("johnlee")).toEqual(["johnlee"]);
expect(bitsPersonalTokenSubjects(undefined)).toEqual([]);
expect(bitsPersonalTokenSubjects(null)).toEqual([]);
expect(bitsPersonalTokenSubjects(" ")).toEqual([]);
});
});
describe("bugfix 自动建 MR 的候选人", () => {
test("经办人在前,watcher 在后,同一个人只出现一次,不兜底到固定操作人", () => {
expect(orderedBugMrTokenSubjects(
["XinZhao.Li@bytedance.com", "majing.21@bytedance.com"],
["jingyuan.yi@bytedance.com", "xinzhao.li@bytedance.com", "oc_chat", "ou_open"],
)).toEqual(["xinzhao.li", "majing.21", "jingyuan.yi"]);
expect(orderedBugMrTokenSubjects([], [])).toEqual([]);
expect(orderedBugMrTokenSubjects([], ["oc_only"])).not.toContain(BITS_FIXED_OPERATOR);
});
test("非 Meego 单的负责人是建单人,排在最前;Meego 单读不到经办人仍不落到导入人", () => {
// UI 走查转出的单:没有 Meego,watchers = [建单人, 群]
const owners = bugMrOwnerEmails(null, [], "ruyi.liu@bytedance.com");
expect(owners).toEqual(["ruyi.liu@bytedance.com"]);
expect(orderedBugMrTokenSubjects(owners, ["ruyi.liu@bytedance.com", "qa.qiu@bytedance.com", "oc_chat"], "ruyi.liu@bytedance.com"))
.toEqual(["ruyi.liu", "qa.qiu"]);
expect(bugMrOwnerEmails(null, [], null)).toEqual([]);
// Meego 单:只认经办人,读不到就是空,导入人照旧剔除
expect(bugMrOwnerEmails("7355180707", ["rd.li@bytedance.com", null], "importer@bytedance.com")).toEqual(["rd.li@bytedance.com"]);
expect(bugMrOwnerEmails("7355180707", [null], "importer@bytedance.com")).toEqual([]);
});
test("导入人不是候选人:经办人都建不成时不落到批量提交人名下", () => {
// 批量导入:watchers = [导入人, 导入时的经办人, 群]
expect(orderedBugMrTokenSubjects(
["wusong.1228@bytedance.com"],
["jingyuan.yi@bytedance.com", "wusong.1228@bytedance.com", "oc_chat"],
"jingyuan.yi@bytedance.com",
)).toEqual(["wusong.1228"]);
// 其他 watcher 照常顺延
expect(orderedBugMrTokenSubjects(
["huayipeng@bytedance.com"],
["Jingyuan.Yi@bytedance.com", "leon.liu@bytedance.com"],
"jingyuan.yi@bytedance.com",
)).toEqual(["huayipeng", "leon.liu"]);
// 导入人本人就是经办人时仍然排在经办人里
expect(orderedBugMrTokenSubjects(
["jingyuan.yi@bytedance.com"],
["jingyuan.yi@bytedance.com"],
"jingyuan.yi@bytedance.com",
)).toEqual(["jingyuan.yi"]);
});
test("创建过或没有分支就停;没绑 Token、权限不足继续顺延", () => {
expect(bugAutoMrBlocksWalk("create_mr: MR has already existed for project_id")).toBe(true);
expect(bugAutoMrBlocksWalk("create_mr: This MR is during the creating process")).toBe(true);
expect(bugAutoMrBlocksWalk("preflight: 分支 bugfix/x 没有推到交付仓 TikTok (Android) 的远端")).toBe(true);
expect(bugAutoMrBlocksWalk("Gitlab Error: Branch is not found")).toBe(true);
expect(bugAutoMrBlocksWalk("create_mr: Invalid token")).toBe(false);
expect(bugAutoMrBlocksWalk("create_mr: TikTok 你可能没有权限访问这个仓库")).toBe(false);
expect(bugAutoMrBlocksWalk("HTTP 0: The operation timed out")).toBe(false);
expect(BUG_AUTO_MR_NONE_CREATED).toBe("none of watcher could successfully create MR");
});
});
describe("mr_sync Bits reader", () => {
test("后台读走 BITS_FIXED_OPERATOR 的面板 Token,不碰 BITS_TOKEN", () => {
expect(BITS_FIXED_OPERATOR).toBe("jiaopengwen");
const sync = readFileSync(new URL("../src/mr_sync.ts", import.meta.url), "utf8");
const auto = readFileSync(new URL("../src/mr_auto_confirm.ts", import.meta.url), "utf8");
expect(sync).toContain("getMrInfo(mrId, null, BITS_FIXED_OPERATOR)");
expect(auto).toContain("getMrInfo(mrId, null, BITS_FIXED_OPERATOR)");
expect(sync).not.toMatch(/getMrInfo\(mrId\)/);
expect(auto).not.toMatch(/getMrInfo\(mrId\)/);
});
});
describe("mr-comments Bits reader", () => {
test("拉评论走 BITS_FIXED_OPERATOR 的面板 Token,不跟点击者、不碰 BITS_TOKEN", () => {
expect(BITS_FIXED_OPERATOR).toBe("jiaopengwen");
const routes = readFileSync(new URL("../src/routes.ts", import.meta.url), "utf8");
expect(routes).toContain("tokenUser: BITS_FIXED_OPERATOR");
expect(routes).not.toContain("bitsTokenUser");
expect(routes.match(/tokenUser: BITS_FIXED_OPERATOR/g)?.length).toBe(2);
});
});