import { expect, test } from "bun:test"; import { handoverStart, isEddyRelayTarget } from "@bbbug/shared/handover"; import { resolveRelayStart, resolveExport } from "../src/eddy_export_config"; import type { GitResult } from "../src/git_exec"; const item = { platform: "ios" as const, status: "manual_review", branchName: "feature/example", repoKey: "tiktok-ios", baseRef: "develop" }; const sha = "a".repeat(40); test("relay uses the task branch before merge and the configured base after merge", () => { expect(handoverStart(item).mode).toBe("branch"); expect(handoverStart({ ...item, status: "completed" }).mode).toBe("branch-or-base"); expect(handoverStart({ ...item, status: "completed", mrMergedAt: 1 })).toEqual({ mode: "base", branch: item.branchName, baseBranch: "develop", baseBranchKnown: true }); // Without a baseRef the base branch is only a fallback: Eddy still resolves against it // (and fails loudly if it is wrong), while the Markdown relay must print a placeholder. const guessed = handoverStart({ ...item, status: "completed", mrMergedAt: 1, baseRef: null }); expect(guessed.baseBranch).toBe(process.env.BASE_BRANCH || "develop"); expect(guessed.baseBranchKnown).toBe(false); }); test("only iOS features expose Eddy; Android and other task kinds keep ordinary relay", async () => { expect(isEddyRelayTarget({ kind: "feature", platform: "ios" })).toBe(true); for (const target of [{ kind: "feature", platform: "android" }, { kind: "bug", platform: "ios" }, { kind: "prototype", platform: "ios" }]) { expect(isEddyRelayTarget(target)).toBe(false); } await expect(resolveExport({ platform: "android", product: "tiktok", repoKey: "tiktok-android", start: handoverStart(item) })).rejects.toThrow("iOS"); }); test("missing branch falls back only for completed/unknown merge; network failures never fall back", async () => { const start = handoverStart({ ...item, status: "completed" }); const calls: string[] = []; const runner = async (argv: string[]): Promise => { const ref = argv.at(-1)!; calls.push(ref); return ref.endsWith("develop") ? { ok: true, exitCode: 0, stdout: `${sha}\t${ref}\n`, stderr: "" } : { ok: false, exitCode: 2, stdout: "", stderr: "" }; }; expect(await resolveRelayStart("/fixture", start, runner)).toEqual({ commit: sha, branch: "develop", from: "base" }); expect(calls).toHaveLength(2); calls.length = 0; await expect(resolveRelayStart("/fixture", handoverStart(item), runner)).rejects.toThrow("任务分支不存在"); expect(calls).toHaveLength(1); await expect(resolveRelayStart("/fixture", start, async () => ({ ok: false, exitCode: 128, stdout: "", stderr: "network unavailable" }))).rejects.toThrow("网络"); }); test("known merged task queries only the latest base even when its old branch still exists", async () => { const start = handoverStart({ ...item, status: "completed", mrMergedAt: 1 }); const calls: string[] = []; const result = await resolveRelayStart("/fixture", start, async argv => { calls.push(argv.at(-1)!); return { ok: true, exitCode: 0, stdout: `${sha}\t${argv.at(-1)}\n`, stderr: "" }; }); expect(calls).toEqual(["refs/heads/develop"]); expect(result.from).toBe("base"); });