// MUST be first — pins env before anything that reads it at module load. import "./setup_env"; import { test, expect, describe } from "bun:test"; import { interpretLegacyRepoKey, isSidecarOnlyDelivery, parseRepoList, repoByKey, repoForMr, sidecarChannelOfUrl, splitRepoList, } from "@bbbug/shared/repos"; import { applyRepoReportToPatch } from "../src/code_sidecars"; describe("parseRepoList / splitRepoList", () => { test("去重、丢掉非字符串", () => { expect(parseRepoList(["tiktok-android", "tiktok-android", 1, "tt-im-monorepo", ""])).toEqual([ "tiktok-android", "tt-im-monorepo", ]); expect(parseRepoList(null)).toEqual([]); }); test("android 涉及集拆成交付 hit + 附加仓", () => { expect(splitRepoList("android", ["tiktok-android", "tt-im-monorepo"])).toEqual({ deliveryHit: true, sidecars: ["tt-im-monorepo"], }); expect(splitRepoList("android", ["tt-im-monorepo"])).toEqual({ deliveryHit: false, sidecars: ["tt-im-monorepo"], }); expect(splitRepoList("ios", ["tiktok-android", "tt-im-monorepo"]).deliveryHit).toBe(false); }); }); describe("interpretLegacyRepoKey", () => { test("sidecar key 不得进交付列,记成 unpushed + sidecar", () => { expect(interpretLegacyRepoKey("android", "tt-im-monorepo")).toEqual({ deliveryKey: "unpushed", sidecar: "tt-im-monorepo", }); }); test("交付仓 / unpushed / 空 原样", () => { expect(interpretLegacyRepoKey("android", "tiktok-android")).toEqual({ deliveryKey: "tiktok-android", sidecar: null, }); expect(interpretLegacyRepoKey("android", "unpushed")).toEqual({ deliveryKey: "unpushed", sidecar: null, }); expect(interpretLegacyRepoKey("android", null)).toEqual({ deliveryKey: null, sidecar: null, }); }); }); describe("applyRepoReportToPatch", () => { test("repoList 同时命中客户端和 FE → 交付仓 key + sidecar 列表,丢掉 repoList", () => { const patch: Record = { repoList: ["tiktok-android", "tt-im-monorepo"], repoKey: "tt-im-monorepo", }; const { sidecarKeys } = applyRepoReportToPatch("android", patch); expect(patch.repoKey).toBe("tiktok-android"); expect(patch.repoList).toBeUndefined(); expect(sidecarKeys).toEqual(["tt-im-monorepo"]); }); test("只有 FE → 交付列 unpushed,附加仓进 sidecar", () => { const patch: Record = { repoList: ["tt-im-monorepo"] }; const { sidecarKeys } = applyRepoReportToPatch("android", patch); expect(patch.repoKey).toBe("unpushed"); expect(sidecarKeys).toEqual(["tt-im-monorepo"]); }); test("空 repoList → unpushed,没有 sidecar", () => { const patch: Record = { repoList: [] }; const { sidecarKeys } = applyRepoReportToPatch("android", patch); expect(patch.repoKey).toBe("unpushed"); expect(sidecarKeys).toEqual([]); }); test("旧卫星只报 sidecar repoKey", () => { const patch: Record = { repoKey: "tt-im-monorepo" }; const { sidecarKeys } = applyRepoReportToPatch("android", patch); expect(patch.repoKey).toBe("unpushed"); expect(sidecarKeys).toEqual(["tt-im-monorepo"]); }); }); describe("repoForMr 钳制", () => { test("面板/核心路径拿不到 FE 仓", () => { expect(repoForMr("android", "tt-im-monorepo").key).toBe("tiktok-android"); }); }); describe("主仓无改动(改动只在附加仓)", () => { /** 生产 ce9bf567:只改了 tt_im_monorepo,交付仓那一行却报「MR 提交失败:没有推到交付仓」。 */ test("交付仓远端没有分支、附加仓有命中 ⇒ 主仓不需要 MR", () => { const patch: Record = { repoList: ["tt-im-monorepo"] }; const { sidecarKeys } = applyRepoReportToPatch("ios", patch); expect(isSidecarOnlyDelivery(patch.repoKey as string, sidecarKeys.length)).toBe(true); }); test("哪儿都没推上去 ⇒ 不算(那是真的没推,交付仓的失败要报)", () => { expect(isSidecarOnlyDelivery("unpushed", 0)).toBe(false); }); test("交付仓也命中 ⇒ 不算", () => { const patch: Record = { repoList: ["tiktok-ios", "tt-im-monorepo"] }; const { sidecarKeys } = applyRepoReportToPatch("ios", patch); expect(isSidecarOnlyDelivery(patch.repoKey as string, sidecarKeys.length)).toBe(false); }); test("探测没跑 / 查不动(repoKey 为空)⇒ 不算,照老路去建交付仓 MR", () => { expect(isSidecarOnlyDelivery(null, 1)).toBe(false); expect(isSidecarOnlyDelivery(undefined, 1)).toBe(false); }); }); describe("tt_im_monorepo 的 MR 通道", () => { test("走 code.byted.org MR(gitlab 通道),自动建,不再要 Bits 开发任务配置", () => { const repo = repoByKey("tt-im-monorepo")!; expect(repo.mrChannel).toBe("gitlab"); expect(repo.mrTrigger).toBe("auto"); expect(repo.codebaseRepoPath).toBe("tiktok/tt_im_monorepo"); expect(repo.bitsDevTask).toBeNull(); expect(repo.defaultTargetBranch).toBe("master"); }); }); describe("附加仓行上的链接是哪条通道建的 —— 按链接认,不按建行时写的 channel 列", () => { /** 生产 ce9bf567:行是开发任务时代建的,MR 是改走 code.byted.org 之后建的;按列画就把 MR 吞了。 */ test("code.byted.org 的 MR ⇒ gitlab", () => { expect(sidecarChannelOfUrl("https://code.byted.org/tiktok/tt_im_monorepo/merge_requests/733")).toBe("gitlab"); }); test("Bits 开发任务详情 ⇒ bits-dev-task(9 月那些老行照旧按开发任务画)", () => { expect(sidecarChannelOfUrl("https://bits.bytedance.net/devops/39151416834/develop/detail/2882215?devops_space_type=server_fe")) .toBe("bits-dev-task"); }); test("流水线链接、非 https、乱填的都不认 ⇒ null,调用方退回列值", () => { expect(sidecarChannelOfUrl("https://bits.bytedance.net/devops/39151416834/pipeline/detail/1189494497026")).toBeNull(); expect(sidecarChannelOfUrl("http://code.byted.org/tiktok/tt_im_monorepo/merge_requests/733")).toBeNull(); expect(sidecarChannelOfUrl("javascript:alert(1)")).toBeNull(); expect(sidecarChannelOfUrl(null)).toBeNull(); }); });