import { describe, expect, test } from "bun:test"; import { mrPackageCacheUsable, mrPackageStateFrom } from "../src/mr_package_state"; const status = (o: Record = {}) => ({ last_commit_id: "abcdef1234567890", pipeline_status: "running", state: "opened", ...o }); describe("mrPackageStateFrom", () => { test("latest commit not built yet → building", () => { const s = mrPackageStateFrom(status(), [{ commit_id: "0000000", packages: [{ package_name: "old.ipa", package_url: "https://x/old.ipa" }] }], 1); expect(s.state).toBe("building"); expect(s.packages).toEqual([]); }); test("package on the latest commit → ready, install_url preferred, platform inferred", () => { const s = mrPackageStateFrom(status({ pipeline_status: "success" }), [{ commit_id: "abcdef12", packages: [ { package_name: "TikTok-iphoneos.ipa", package_url: "https://x/a.ipa", install_url: "itms-services://?url=https://x/a.plist" }, { package_name: "app-local_test-release.apk", package_url: "https://x/b.apk" }, ], }]); expect(s.state).toBe("ready"); expect(s.packages.map((p) => [p.url, p.platform])).toEqual([ ["itms-services://?url=https://x/a.plist", "ios"], ["https://x/b.apk", "android"], ]); }); test("pipeline failed with no package → failed", () => { expect(mrPackageStateFrom(status({ pipeline_status: "failed" }), []).state).toBe("failed"); }); test("closed MR → closed", () => { expect(mrPackageStateFrom(status({ state: "closed" }), []).state).toBe("closed"); }); }); // 多轮提交:Bits 的真实形状(MR 8592680,2026-11-11)—— 按时间倒序,每组一个提交,每组带两个包。 describe("multiple commits on one MR → only the head commit's packages", () => { const groups = (head: string, old: string) => [ { id: 27386714, commit_id: head, create_time: 1794425967, packages: [ { commit_id: head, package_name: "TikTokInhouseRelease", package_url: `https://v/${head}/TikTok.ipa`, install_url: `https://v/${head}/TikTok.ipa` }, { commit_id: head, package_name: "MusicallyInhouseRelease", package_url: `https://v/${head}/Musically.ipa`, install_url: `https://v/${head}/Musically.ipa` }, ] }, { id: 27386503, commit_id: old, create_time: 1794422902, packages: [ { commit_id: old, package_name: "MusicallyInhouseRelease", package_url: `https://v/${old}/Musically.ipa` }, ] }, ]; const HEAD = "883e9a10aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; const OLD = "d4a9724cbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; test("head built → its packages only, never the older commit's", () => { const s = mrPackageStateFrom(status({ last_commit_id: HEAD, pipeline_status: "success" }), groups(HEAD, OLD)); expect(s.state).toBe("ready"); expect(s.commit).toBe(HEAD); expect(s.packages.map((p) => p.url)).toEqual([`https://v/${HEAD}/TikTok.ipa`, `https://v/${HEAD}/Musically.ipa`]); expect(s.packages.every((p) => p.platform === "ios")).toBe(true); }); test("new commit pushed, not built yet → building, the previous QR is not shown", () => { const NEW = "5b399b55cccccccccccccccccccccccccccccccc"; const s = mrPackageStateFrom(status({ last_commit_id: NEW, pipeline_status: "running" }), groups(HEAD, OLD)); expect(s.state).toBe("building"); expect(s.packages).toEqual([]); }); test("new commit's pipeline failed, older commits have packages → failed, not an old QR", () => { // MR 8350646:头提交 3535b142 没包,更早四个提交都有。 const s = mrPackageStateFrom(status({ last_commit_id: "3535b142dddd", pipeline_status: "failed" }), groups(HEAD, OLD)); expect(s.state).toBe("failed"); expect(s.packages).toEqual([]); }); }); describe("mrPackageCacheUsable", () => { const hit = { state: "ready" as const, commit: "a", pipelineStatus: "success", packages: [], checkedAt: 1_000_000 }; test("fresh and the item has not moved since → use it", () => { expect(mrPackageCacheUsable(hit, 900_000, 1_030_000, 60_000)).toBe(true); expect(mrPackageCacheUsable(hit, null, 1_030_000, 60_000)).toBe(true); }); test("older than the TTL → refetch", () => { expect(mrPackageCacheUsable(hit, 900_000, 1_060_000, 60_000)).toBe(false); }); test("item changed status after the lookup (new round landed) → refetch even inside the TTL", () => { expect(mrPackageCacheUsable(hit, 1_010_000, 1_030_000, 60_000)).toBe(false); }); test("nothing cached → refetch", () => { expect(mrPackageCacheUsable(undefined, null, 1_030_000, 60_000)).toBe(false); }); }); // 每个产品 × 端只给一个包。包名照抄 2026-11-11 的真实 MR。 describe("one package per product × platform", () => { const SHA = "d01960b008c7bbf2a34675fd805bba285ca0ce08"; const pk = (name: string, url: string) => ({ commit_id: SHA, package_name: name, package_url: url, install_url: url }); const head = status({ last_commit_id: SHA, pipeline_status: "success" }); // MR 8592561(feature 4707a03b):最新提交 4 个包,只有 M 是要装的。 const tiktokAndroid = [{ commit_id: SHA, packages: [ pk("ROW-aab-ttlite-d01960-googleplay-release", "https://v/x/ROW-aab-ttlite-d01960-googleplay-release.aab"), pk("ROW-64bit-ttlite-d01960-googleplay-release", "https://v/x/64bit/ROW-64bit-ttlite-d01960-googleplay-release.apk"), pk("ROW-32bit-ttlite-d01960-googleplay-release", "https://v/x/32bit/ROW-32bit-ttlite-d01960-googleplay-release.apk"), pk("M-d01960-local_test-release", "https://v/x/musicallyI18n/release/M-d01960-local_test-release.apk"), ] }]; test("TikTok Android: only M, Lite ignored", () => { const s = mrPackageStateFrom(head, tiktokAndroid, 1, { product: "tiktok", platform: "android" }); expect(s.state).toBe("ready"); expect(s.packages.map((p) => p.name)).toEqual(["M-d01960-local_test-release"]); }); test("TikTok Android: only Lite out so far → still building, no Lite QR", () => { const liteOnly = [{ commit_id: SHA, packages: tiktokAndroid[0]!.packages.slice(0, 3) }]; const s = mrPackageStateFrom(status({ last_commit_id: SHA, pipeline_status: "running" }), liteOnly, 1, { product: "tiktok", platform: "android" }); expect(s.state).toBe("building"); expect(s.packages).toEqual([]); }); test("TikTok iOS: Musically, not the TikTok (T) build", () => { const g = [{ commit_id: SHA, packages: [pk("TikTokInhouseRelease", "https://v/t.ipa"), pk("MusicallyInhouseRelease", "https://v/m.ipa")] }]; const s = mrPackageStateFrom(head, g, 1, { product: "tiktok", platform: "ios" }); expect(s.packages.map((p) => p.name)).toEqual(["MusicallyInhouseRelease"]); }); test("Lemon8 Android: the local_test private build, deduped, not the debug apk", () => { // MR 8354122:同一文件列了两条,外加一个 debug 包。 const url = "https://v/build_feature/903505878/app-l8/build/outputs/apk/release/Lemon8_Private-v14.3.0-local_test-2026_1015_062155-a3e7fbdc.apk"; const g = [{ commit_id: SHA, packages: [ pk("build:feature-Lemon8_Private-v14.3.0-local_test-2026_1015_062155-a3e7fbdc.apk", url), pk("Lemon8_Private-v14.3.0-local_test-2026_1015_062155-a3e7fbdc", url), pk("build:feature debug-app-l8-debug.apk", "https://v/246754918/app-l8-debug.apk"), ] }]; const s = mrPackageStateFrom(head, g, 1, { product: "lemon8", platform: "android" }); expect(s.packages.map((p) => p.url)).toEqual([url]); }); test("Lemon8 iOS: Lemon8InhouseRelease", () => { const g = [{ commit_id: SHA, packages: [pk("Lemon8InhouseRelease", "itms-services://?action=download-manifest&url=https://bits/x")] }]; const s = mrPackageStateFrom(head, g, 1, { product: "lemon8", platform: "ios" }); expect(s.packages.map((p) => p.name)).toEqual(["Lemon8InhouseRelease"]); }); test("product without a rule: everything installable, aab dropped", () => { const s = mrPackageStateFrom(head, tiktokAndroid, 1, { product: "notes", platform: "android" }); expect(s.packages.length).toBe(3); expect(s.packages.some((p) => p.url.endsWith(".aab"))).toBe(false); }); });