// MUST be first — pins env before anything that reads it at module load. import "./setup_env"; import { test, expect, describe, afterAll } from "bun:test"; import { bugMeegoBindings, submitMrForFix, type MeegoBinding } from "../src/bits"; import { featureMeegoBindings, featureMeegoLinks, featureMrChoice } from "../src/feature_importer"; /** * 回归:按 Feature 导入 Meego 后,提的 MR 不绑工单,要人手动绑(feature 116c3310 / MR 8549489)。 * 需求单在 Bits 的 task_type 是 issue,不是 bug:7382040553 只有 issue 查得到绑定的 MR。 */ describe("featureMeegoBindings", () => { const story = "https://meego.larkoffice.com/tiktok/story/detail/7382040553"; test("需求单 ⇒ Bits 的 issue,缺陷单 ⇒ Bits 的 bug", () => { expect(featureMeegoBindings(story)).toEqual([{ taskId: "7382040553", taskType: "issue", projectKey: "tiktok" }]); expect(featureMeegoBindings("Bug ticket: https://meego.larkoffice.com/tiktok/issue/detail/7365664146")).toEqual([ { taskId: "7365664146", taskType: "bug", projectKey: "tiktok" }, ]); }); test("链接前后贴着文字、带查询参数也认(线上真实写法)", () => { for (const text of [ `meego:${story}`, `meego=${story}?parentUrl=%2Ftiktok%2FstoryView%2Fx`, `验证一下这个需求${story}`, `${story}?tabKey=detail`, ]) { expect(featureMeegoBindings(text).map((b) => b.taskId)).toEqual(["7382040553"]); } }); test("展开块里提到的别的工单不算;同一张只绑一次", () => { const description = [ story, "", `===${story} 实际内容===`, "父需求 https://meego.larkoffice.com/tiktok/story/detail/7000000001", `===${story} 实际内容===`, ].join("\n"); expect(featureMeegoBindings(description).map((b) => b.taskId)).toEqual(["7382040553"]); expect(featureMeegoBindings(`${story} 以及 ${story}`)).toHaveLength(1); }); test("描述里写了两张工单 ⇒ 都绑", () => { expect(featureMeegoBindings(`${story} https://meego.larkoffice.com/tiktok/story/detail/7000000002`).map((b) => b.taskId)) .toEqual(["7382040553", "7000000002"]); }); test("不是工单的链接不算", () => { expect(featureMeegoBindings(null)).toEqual([]); expect(featureMeegoBindings("https://www.figma.com/design/abc/detail/123")).toEqual([]); expect(featureMeegoBindings("https://bits.bytedance.net/devops/1500037378/code/detail/8549489")).toEqual([]); expect(featureMeegoBindings("https://meego.larkoffice.com/tiktok/issueView/Rrn5z70Dg")).toEqual([]); expect(featureMeegoBindings("https://meego.larkoffice.com/tiktok/sub_task/detail/7000000003")).toEqual([]); }); }); /** 面板「提交 MR」第一步:产品需求 = Feature MR + 必绑一张 Meego 单;技术优化 = Optimize MR、不绑单。 */ describe("featureMrChoice", () => { const story = "https://meego.larkoffice.com/tiktok/story/detail/7382040553"; test("产品需求:绑填进来的那一张,按工单类型换成 Bits 的叫法", () => { expect(featureMrChoice("feature", `${story}?tabKey=detail`)).toEqual({ mrType: "feature", meegoWorkItems: [{ taskId: "7382040553", taskType: "issue", projectKey: "tiktok" }], }); expect(featureMrChoice("feature", "https://meego.larkoffice.com/tiktok/issue/detail/7365664146")).toEqual({ mrType: "feature", meegoWorkItems: [{ taskId: "7365664146", taskType: "bug", projectKey: "tiktok" }], }); }); test("产品需求:没填 / 认不出的链接 ⇒ 拒,不建没绑单的 Feature MR", () => { for (const url of [undefined, "", " ", "7382040553", "https://meego.larkoffice.com/tiktok/issueView/Rrn5z70Dg"]) { expect("error" in featureMrChoice("feature", url)).toBe(true); } }); test("产品需求:贴了两张只绑第一张", () => { const r = featureMrChoice("feature", `${story} https://meego.larkoffice.com/tiktok/story/detail/7000000002`); expect("error" in r ? [] : r.meegoWorkItems.map((b) => b.taskId)).toEqual(["7382040553"]); }); test("技术优化:Optimize MR,不绑单(填了也不绑)", () => { expect(featureMrChoice("optimize", undefined)).toEqual({ mrType: "optimize", meegoWorkItems: [] }); expect(featureMrChoice("optimize", story)).toEqual({ mrType: "optimize", meegoWorkItems: [] }); }); test("预填用的链接:只取建单人写的、去掉查询参数", () => { const description = [`meego:${story}?tabKey=detail`, "", `===${story} 实际内容===`, "父需求 https://meego.larkoffice.com/tiktok/story/detail/7000000001", `===${story} 实际内容===`].join("\n"); expect(featureMeegoLinks(description)).toEqual([story]); expect(featureMeegoLinks(null)).toEqual([]); }); }); test("bugMeegoBindings:按 Bug 导入的需求单也按需求绑;没工单不绑", () => { expect(bugMeegoBindings("7388011095", "https://meego.larkoffice.com/tiktok/issue/detail/7388011095")[0].taskType).toBe("bug"); expect(bugMeegoBindings("7383586503", "https://meego.larkoffice.com/tiktok/story/detail/7383586503?x=1")[0].taskType).toBe("issue"); expect(bugMeegoBindings(null, null)).toEqual([]); }); describe("submitMrForFix 绑工单", () => { const MR_ID = 8549489; const realFetch = globalThis.fetch; afterAll(() => { globalThis.fetch = realFetch; }); function stub(boundUnderType?: string) { const calls: { url: string; body?: any }[] = []; globalThis.fetch = (async (input: any, init?: any) => { const url = typeof input === "string" ? input : input.url; calls.push({ url, body: init?.body ? JSON.parse(init.body) : undefined }); if (url.includes("/openapi/feature/mr_info")) { const taskType = new URL(url).searchParams.get("task_type"); return Response.json({ code: 200, data: taskType === boundUnderType ? [{ mr_id: MR_ID }] : [] }); } return Response.json({ code: 200, data: { mr_id: MR_ID } }); }) as typeof fetch; return calls; } const items: MeegoBinding[] = [ { taskId: "7382040553", taskType: "issue", projectKey: "tiktok" }, { taskId: "7000000002", taskType: "issue", projectKey: "lemon8" }, ]; const submit = (existingMrId?: number, meegoWorkItems: MeegoBinding[] = items) => submitMrForFix({ platform: "ios", sourceBranch: "feature/x", title: "t", mrType: "feature", meegoWorkItems, bitsUser: "someone", tokenUser: "someone", existingMrId, }); const binds = (calls: { url: string; body?: any }[]) => calls.filter((c) => c.url.includes("/openapi/feature/bind/mr")).map((c) => c.body); test("新建的 MR:每张工单按自己的类型和项目绑", async () => { const calls = stub(); const r = await submit(); expect(r.ok).toBe(true); expect(binds(calls)).toMatchObject([ { task_id: "7382040553", task_type: "issue", project_id: "tiktok", platform: "meego", mr_id: MR_ID }, { task_id: "7000000002", task_type: "issue", project_id: "lemon8", platform: "meego", mr_id: MR_ID }, ]); }); test("认领的 MR:查重要按需求单的类型查,查到就不再绑", async () => { const calls = stub("issue"); await submit(MR_ID, items.slice(0, 1)); expect(binds(calls)).toHaveLength(0); }); test("没有工单 ⇒ 不调绑定接口", async () => { const calls = stub(); expect((await submit(undefined, [])).ok).toBe(true); expect(binds(calls)).toHaveLength(0); }); });