Files
streamline/satellite/test/repo_probe.test.ts
T
2026-11-16 17:26:44 -08:00

116 lines
5.0 KiB
TypeScript

import { test, expect, describe, beforeAll, afterAll } from "bun:test";
import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
/**
* 分支到底推到哪个仓了 —— 用真的 git 仓库测,不打桩。
*
* 打桩在这里没有意义:这段代码的全部风险就在 `git ls-remote` 的退出码和输出形态上
* (没有 remote / 分支不存在 / 查不动 分别长什么样),桩子只会把这三种情况按我以为的
* 样子铺开,然后测我自己的假设。
*
* 这段代码的底线也在这儿:探测不出结论时必须返回 null(退回老行为),**绝不能**把
* 「我查不动」说成「分支哪儿都没有」—— 后者会让一条本来能建的 MR 建不成。
*/
let root = "";
let originalWorkspace: string | undefined;
let probeBranchRepo: typeof import("../src/repo_probe").probeBranchRepo;
let restoreImLocalPaths: (() => void) | null = null;
const git = (cwd: string, ...args: string[]) => {
const p = Bun.spawnSync({
cmd: ["git", "-c", "user.email=t@t", "-c", "user.name=t", "-c", "commit.gpgsign=false", ...args],
cwd,
});
if (p.exitCode !== 0) {
throw new Error(`git ${args.join(" ")} failed: ${p.stderr?.toString() || ""}`);
}
};
beforeAll(async () => {
root = mkdtempSync(path.join(tmpdir(), "repo-probe-"));
// 「远端」就是一个 bare 仓 —— ls-remote 对它和对 code.byted.org 没有区别。
// 目录名带上 TikTok:注册表按 remote URL 认仓(remoteHints),不是按本地路径。
const origin = path.join(root, "TikTok.git");
mkdirSync(origin);
git(origin, "init", "--bare", "-q");
const work = path.join(root, "TikTok");
git(root, "clone", "-q", origin, "TikTok");
writeFileSync(path.join(work, "README.md"), "hi\n");
git(work, "add", "README.md");
git(work, "commit", "-q", "-m", "init");
git(work, "branch", "-M", "develop");
git(work, "push", "-q", "-u", "origin", "develop");
git(work, "checkout", "-q", "-b", "feature/pushed-branch");
git(work, "push", "-q", "-u", "origin", "feature/pushed-branch");
// 本地建了但没推 —— 正是把人带偏的那种分支。
git(work, "checkout", "-q", "-b", "feature/local-only");
originalWorkspace = process.env.AI_WORKSPACE;
process.env.AI_WORKSPACE = root; // 注册表里的 `$AI_WORKSPACE/TikTok` 指到这儿
// 这个用例只能看到自己创建的临时仓。开发机如果刚好有
// `~/code/tt_im_monorepo`,它的真实远端状态不应把“本地分支未 push”测试变成 null。
const { CODE_REPOS } = await import("@bbbug/shared/repos");
const imRepo = CODE_REPOS.find((repo) => repo.key === "tt-im-monorepo");
if (imRepo) {
const original = [...imRepo.localPaths];
imRepo.localPaths = ["$AI_WORKSPACE/tt_im_monorepo"];
restoreImLocalPaths = () => { imRepo.localPaths = original; };
}
({ probeBranchRepo } = await import("../src/repo_probe"));
});
afterAll(() => {
if (originalWorkspace === undefined) delete process.env.AI_WORKSPACE;
else process.env.AI_WORKSPACE = originalWorkspace;
restoreImLocalPaths?.();
if (root) rmSync(root, { recursive: true, force: true });
});
describe("probeBranchRepo", () => {
test("远端有这条分支 ⇒ 认出是 platform 默认仓", async () => {
const r = await probeBranchRepo("android", "feature/pushed-branch");
expect(r.repoKey).toBe("tiktok-android");
expect(r.repoList).toEqual(["tiktok-android"]);
});
test("只在本地建过的分支 ⇒ unpushed(而不是默认仓)", async () => {
// 这正是那次事故的形状:TikTok 仓里有个同名本地分支、零改动、从没 push。
const r = await probeBranchRepo("android", "feature/local-only");
expect(r.repoKey).toBe("unpushed");
expect(r.repoList).toEqual([]);
});
test("分支名为空 ⇒ null,不下任何结论", async () => {
expect(await probeBranchRepo("android", " ")).toEqual({
repoKey: null, repoList: [], detail: "no branch to probe",
});
});
test("检出在、但 origin 指向别的仓 ⇒ 不算这个仓的分支(路径是线索,remote 才是身份)", async () => {
const work = path.join(root, "TikTok");
git(work, "remote", "set-url", "origin", path.join(root, "someone-elses.git"));
try {
// 认不出仓 ⇒ 这条路径当作「没有这份检出」,而不是把分支算到 TikTok 头上。
expect((await probeBranchRepo("android", "feature/pushed-branch")).repoKey).toBeNull();
} finally {
git(work, "remote", "set-url", "origin", path.join(root, "TikTok.git"));
}
});
test("检出在、但远端查不动 ⇒ null,退回老行为(绝不说成「哪儿都没有」)", async () => {
const work = path.join(root, "TikTok");
git(work, "remote", "rename", "origin", "origin-parked");
try {
const r = await probeBranchRepo("android", "feature/pushed-branch");
expect(r.repoKey).toBeNull();
expect(r.detail).toContain("inconclusive");
} finally {
git(work, "remote", "rename", "origin-parked", "origin");
}
});
});