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

130 lines
6.9 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 { classifyMrFailure } from "../src/lib/mrFailure";
/**
* 分类错了的代价不对称:
* - 把「没授权」认成 unknown,用户只是少一段引导,还能看到原文;
* - 把别的错认成「没授权」,用户会被指去 Bits 点一圈,回来发现照样失败 —— 更糟。
* 所以用例主体是**生产上真实出现过的原文**(下面每条都抄自面板记录),外加一组
* 「不许误伤」的反例。
*/
const REAL = {
notAuthorized: 'MR 提交失败(as srj.5):create_mr: HTTP 200: {"code":400,"data":null,"message":"尚未登录, 或会话已过期"}',
mrExists: 'MR 提交失败(as tanzhehao):create_mr: HTTP 200: {"code":400,"data":null,"message":"MR has already existed for project_id(TikTok), source_branch(feature/bb-skin-guide-inline-message) and target_branch(latest_but_untested)"}',
branchMissing: 'MR 提交失败(as tanzhehao):create_mr: HTTP 200: {"code":400,"data":null,"message":"Gitlab Error: TikTok {\\"message\\":\\"input: createMergeRequest resource Branch is not found: merge request source branch not found\\"}"}',
legacyBranch: "MR 创建失败(已重试 3 次): 远程分支 bugfix/default-system-font-text-shadow 不存在且本地重推失败: branch bugfix/default-system-font-text-shadow not found in local checkout /Users/fei/ai_workspace/TikTok",
threw: "MR 提交异常:The operation timed out.",
// 控制面自己写的预检失败(bits.ts::unconfiguredRepoError / unpushedBranchError)。
repoUnconfigured: "MR 提交失败(as johnlee):preflight: 分支 feature/lynx-im-page-ui-tree-audit 推在 tt_im_monorepo,"
+ "但这个仓库没有配置 GitLab project id,控制面建不了它的 MR(见 shared/src/repos.ts 的 tt-im-monorepo)。"
+ "请手动建:https://code.byted.org/tiktok/tt_im_monorepo/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Flynx-im-page-ui-tree-audit",
clickRequired: "MR 提交失败(as johnlee):preflight: 分支 feature/lynx-im-page-ui-tree-audit 推在 tt_im_monorepo,"
+ "这个仓的 MR 不自动建,需要在面板上点「创建 MR」发起。也可以直接自己建:"
+ "https://code.byted.org/tiktok/tt_im_monorepo/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Flynx-im-page-ui-tree-audit",
devTaskClickRequired: "MR 提交失败(as johnlee):preflight: 分支 feature/lynx-im-page-ui-tree-audit 推在 tt_im_monorepo,"
+ "这个仓的开发任务不自动建,需要在面板上点「创建开发任务」发起。也可以直接自己创建:"
+ "https://bits.bytedance.net/devops/39151416834/develop/update?devops_space_type=server_fe",
neverPushed: "MR 提交失败(as johnlee):preflight: 分支 feature/x 没有推到交付仓 TikTok (Android) 的远端,"
+ "无法建 MR —— 这不是重试能解决的,需要让任务重跑并把分支推上去。",
};
describe("classifyMrFailure", () => {
test("经办人和 watcher 都没绑 Token,仍走绑定后重试", () => {
const d = classifyMrFailure("MR 提交失败:none of watcher could successfully create MR")!;
expect(d.kind).toBe("not_authorized");
expect(d.retryable).toBe(true);
expect(d.bitsUser).toBeNull();
});
test("失效的个人 JWT 归为授权问题", () => {
const d = classifyMrFailure("MR 提交失败(as johnlee):create_dev_task: 当前登录用户 johnlee 保存的 Bits 个人 Token 已失效(Invalid JWT token)");
expect(d?.kind).toBe("not_authorized");
expect(d?.bitsUser).toBe("johnlee");
});
test("Bits 不认这个身份 —— 唯一一种「用户自己能修」的失败", () => {
const d = classifyMrFailure(REAL.notAuthorized)!;
expect(d.kind).toBe("not_authorized");
expect(d.bitsUser).toBe("srj.5");
expect(d.retryable).toBe(true);
});
test("分支上已有 MR —— 重试会去认领它", () => {
expect(classifyMrFailure(REAL.mrExists)!.kind).toBe("mr_exists");
});
test("远端分支不见了 —— 重试没用,只能重跑", () => {
for (const r of [REAL.branchMissing, REAL.legacyBranch]) {
const d = classifyMrFailure(r)!;
expect(d.kind).toBe("branch_missing");
expect(d.retryable).toBe(false);
}
});
test("分支推在没接入的仓库 —— 重试没用,给的是手动建 MR 的直达链接", () => {
const d = classifyMrFailure(REAL.repoUnconfigured)!;
expect(d.kind).toBe("repo_unconfigured");
expect(d.retryable).toBe(false);
expect(d.manualMrUrl).toBe(
"https://code.byted.org/tiktok/tt_im_monorepo/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Flynx-im-page-ui-tree-audit",
);
});
test("等人点一下建 MR —— 是待办不是失败,而且必须可重试(那个按钮就是它)", () => {
const d = classifyMrFailure(REAL.clickRequired)!;
expect(d.kind).toBe("mr_click_required");
expect(d.retryable).toBe(true);
expect(d.manualMrUrl).toContain("/merge_requests/new?");
});
test("开发任务待创建使用同一待办分类,并提取 Bits 创建入口", () => {
const d = classifyMrFailure(REAL.devTaskClickRequired)!;
expect(d.kind).toBe("mr_click_required");
expect(d.retryable).toBe(true);
expect(d.manualMrUrl).toBe(
"https://bits.bytedance.net/devops/39151416834/develop/update?devops_space_type=server_fe",
);
});
test("哪个仓都没推上去 —— 与「远端分支没了」同一类,都得重跑", () => {
const d = classifyMrFailure(REAL.neverPushed)!;
expect(d.kind).toBe("branch_missing");
expect(d.retryable).toBe(false);
});
test("没有手动链接时不瞎给", () => {
expect(classifyMrFailure(REAL.branchMissing)!.manualMrUrl).toBeNull();
});
test("超时算网络类,可以直接重试", () => {
const d = classifyMrFailure(REAL.threw)!;
expect(d.kind).toBe("network");
expect(d.retryable).toBe(true);
});
test("没见过的形态落到 unknown,但仍然给重试 + 原文", () => {
const d = classifyMrFailure("MR 提交失败(as janice.wang):reviewers_rd: HTTP 500: gateway blew up")!;
expect(d.kind).toBe("unknown");
expect(d.bitsUser).toBe("janice.wang");
expect(d.raw).toContain("gateway blew up");
});
test("空原因不接管,交给「提交 MR」", () => {
expect(classifyMrFailure(null)).toBeNull();
expect(classifyMrFailure("")).toBeNull();
expect(classifyMrFailure(" ")).toBeNull();
});
test("任意非空 failureReason 都走黄卡,认不出的形态落到 unknown", () => {
const d = classifyMrFailure("Fix-Critic 判定证据不足:缺少 MR diff 对照")!;
expect(d.kind).toBe("unknown");
expect(d.raw).toContain("Fix-Critic");
expect(classifyMrFailure("实现失败:编译不过")!.kind).toBe("unknown");
});
test("取不到 as 身份时不瞎猜", () => {
expect(classifyMrFailure("MR 提交失败:create_mr: HTTP 0: fetch failed")!.bitsUser).toBeNull();
});
});