Initial import of streamline

This commit is contained in:
2026-11-16 17:26:44 -08:00
commit 8e4aec1855
1221 changed files with 274256 additions and 0 deletions
+222
View File
@@ -0,0 +1,222 @@
// MUST be first — pins env before anything that reads it at module load.
import "./setup_env";
import { test, expect, describe, beforeEach, afterAll } from "bun:test";
import {
createMergeRequest, findExistingMr, searchMrIdsBySourceBranch, resolveAgentMrUrl,
readMrInfoFields, mrStatePatchFromInfo,
} from "../src/bits";
const BRANCH = "feature/hide-profile-thought-creation-bubble";
const IOS_PROJECT = 114467;
const TARGET = "latest_but_unstable/dont_merge_unless_necessary";
/** One Bits MR payload, shaped like the real /merge_request/info reply. */
function mrInfo(over: Record<string, unknown> = {}) {
return {
code: 200,
data: {
id: 8327494, iid: 296301, project_id: IOS_PROJECT, group_name: "TikTok_iOS",
from_branch: BRANCH, target_branch: TARGET, state: "opened", merged_time: 0,
...over,
},
};
}
type Route = (url: string, init?: RequestInit) => unknown;
const realFetch = globalThis.fetch;
let routes: Route[] = [];
let calls: string[] = [];
/** Install a fetch stub; each route returns a JSON body or undefined to pass. */
function stubFetch(...rs: Route[]) {
routes = rs;
calls = [];
globalThis.fetch = (async (input: any, init?: RequestInit) => {
const url = typeof input === "string" ? input : input.url;
calls.push(url);
for (const r of routes) {
const body = r(url, init);
if (body !== undefined) {
return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } });
}
}
throw new Error(`unstubbed fetch: ${url}`);
}) as typeof fetch;
}
beforeEach(() => { routes = []; calls = []; });
afterAll(() => { globalThis.fetch = realFetch; });
const searchHit = (ids: number[]): Route => (url) =>
url.includes("/merge_request/search") ? { code: 200, data: ids } : undefined;
const infoFor = (payload: ReturnType<typeof mrInfo>): Route => (url) =>
url.includes("/merge_request/info") ? payload : undefined;
describe("searchMrIdsBySourceBranch", () => {
test("passes the branch through and parses the id list", async () => {
stubFetch(searchHit([8327494, 8300000]));
expect(await searchMrIdsBySourceBranch(BRANCH, TARGET)).toEqual([8327494, 8300000]);
expect(calls[0]).toContain(`source_branch=${encodeURIComponent(BRANCH)}`);
expect(calls[0]).toContain("target_branch=");
});
test("an empty branch never queries — a bare search returns the whole org", async () => {
stubFetch(searchHit([1, 2, 3]));
expect(await searchMrIdsBySourceBranch(" ")).toEqual([]);
expect(calls).toHaveLength(0);
});
});
describe("findExistingMr", () => {
test("adopts an open MR on the same branch in the same project", async () => {
stubFetch(searchHit([8327494]), infoFor(mrInfo()));
const found = await findExistingMr({ platform: "ios", sourceBranch: BRANCH, targetBranch: TARGET });
expect(found?.mrId).toBe(8327494);
expect(found?.mrUrl).toContain("/code/detail/8327494");
});
test("rejects a same-named branch in another project", async () => {
// Branch names DO collide across repos — this is the check that keeps us
// from adopting some other team's MR.
stubFetch(searchHit([8327494]), infoFor(mrInfo({ project_id: 113818 })));
expect(await findExistingMr({ platform: "ios", sourceBranch: BRANCH })).toBeNull();
});
test("skips merged / closed MRs", async () => {
stubFetch(searchHit([8327494]), infoFor(mrInfo({ state: "merged", merged_time: 123 })));
expect(await findExistingMr({ platform: "ios", sourceBranch: BRANCH })).toBeNull();
});
test("retries without the target filter when it comes back empty", async () => {
let searched = 0;
stubFetch(
(url) => {
if (!url.includes("/merge_request/search")) return undefined;
searched++;
return { code: 200, data: url.includes("target_branch") ? [] : [8327494] };
},
infoFor(mrInfo({ target_branch: "develop" })),
);
const found = await findExistingMr({ platform: "ios", sourceBranch: BRANCH, targetBranch: TARGET });
expect(searched).toBe(2);
expect(found?.mrId).toBe(8327494);
});
test("an iid match is definitive, whatever the MR's state", async () => {
stubFetch(searchHit([8327494]), infoFor(mrInfo({ state: "merged" })));
const found = await findExistingMr({ platform: "ios", sourceBranch: BRANCH, iid: 296301 });
expect(found?.mrId).toBe(8327494);
const miss = await findExistingMr({ platform: "ios", sourceBranch: BRANCH, iid: 999999 });
expect(miss).toBeNull();
});
});
describe("createMergeRequest", () => {
const created: Route = (url) =>
url.includes("/merge_request/create") ? { code: 200, data: { mr_id: 8400001 } } : undefined;
const duplicate: Route = (url) =>
url.includes("/merge_request/create")
? { code: 400, data: null, message: `MR has already existed for project_id(TikTok), source_branch(${BRANCH})` }
: undefined;
const input = {
platform: "ios" as const, sourceBranch: BRANCH, targetBranch: TARGET,
title: "(Streamline) x", mrType: "feature" as const,
};
test("the happy path does not go looking for an existing MR", async () => {
stubFetch(created);
const r = await createMergeRequest(input);
expect(r).toMatchObject({ ok: true, mrId: 8400001 });
expect(r.reused).toBeUndefined();
expect(calls.some((u) => u.includes("/search"))).toBe(false);
});
test('"MR has already existed" adopts the MR that is already there', async () => {
stubFetch(duplicate, searchHit([8327494]), infoFor(mrInfo()));
const r = await createMergeRequest(input);
expect(r.ok).toBe(true);
expect(r.reused).toBe(true);
expect(r.mrId).toBe(8327494);
// The original Bits complaint is kept for the step log.
expect(r.error).toContain("already existed");
});
test("a create that lands but replies without an mr_id still recovers", async () => {
// This is the shape that stranded feature 70b22319: Bits made the MR, our
// side never got an id for it, and every later round hit the duplicate wall.
stubFetch(
(url) => (url.includes("/merge_request/create") ? { code: 200, data: {} } : undefined),
searchHit([8327494]), infoFor(mrInfo()),
);
const r = await createMergeRequest(input);
expect(r).toMatchObject({ ok: true, reused: true, mrId: 8327494 });
});
test("a genuine failure with no MR to adopt stays a failure", async () => {
stubFetch(
(url) => (url.includes("/merge_request/create") ? { code: 400, data: null, message: "尚未登录" } : undefined),
searchHit([]),
);
const r = await createMergeRequest(input);
expect(r.ok).toBe(false);
expect(r.mrUrl).toBeNull();
expect(r.error).toContain("尚未登录");
});
});
describe("resolveAgentMrUrl", () => {
test("resolves a code.byted.org link to the canonical Bits URL via its iid", async () => {
stubFetch(searchHit([8327494]), infoFor(mrInfo()));
const r = await resolveAgentMrUrl("ios", "https://code.byted.org/ugc/TikTok/merge_requests/296301", BRANCH);
expect(r?.mrId).toBe(8327494);
expect(r?.mrUrl).toContain("/code/detail/8327494");
});
test("a Bits link is still verified against the task's branch", async () => {
stubFetch(infoFor(mrInfo({ from_branch: "someone/else" })));
const r = await resolveAgentMrUrl("ios", "https://bits.bytedance.net/devops/1500037378/code/detail/8327494", BRANCH);
expect(r).toBeNull();
});
test("ignores prose that carries no MR link", async () => {
stubFetch();
expect(await resolveAgentMrUrl("ios", "见 ImplReport", BRANCH)).toBeNull();
expect(calls).toHaveLength(0);
});
});
describe("readMrInfoFields / mrStatePatchFromInfo", () => {
test("create_time is unix seconds → ms; merged_time 0 is not a merge", () => {
const f = readMrInfoFields({
state: "opened", create_time: 1779960978, merged_time: 0, merge_commit_id: "",
});
expect(f).toMatchObject({ state: "opened", merged: false, mergedAt: null, createdAt: 1779960978000 });
});
test("merged_time seconds becomes mrMergedAt; Bits 'closed' after landing still counts as merged", () => {
const f = readMrInfoFields({
state: "closed", create_time: 1779960978, merged_time: 1780000000, merge_commit_id: "abc",
});
expect(f.merged).toBe(true);
expect(f.mergedAt).toBe(1780000000000);
const patch = mrStatePatchFromInfo({ mrState: "opened", mrMergedAt: null }, { ok: true, ...f });
expect(patch).toEqual({ mrState: "merged", mrMergedAt: 1780000000000 });
});
test("merged without merged_time falls back to create_time, never invents 'now'", () => {
const f = readMrInfoFields({ state: "merged", create_time: 1779960978, merged_time: 0 });
expect(f.merged).toBe(true);
expect(f.mergedAt).toBeNull();
const patch = mrStatePatchFromInfo({ mrState: null, mrMergedAt: null }, { ok: true, ...f });
expect(patch.mrState).toBe("merged");
expect(patch.mrMergedAt).toBe(1779960978000);
});
test("does not overwrite an existing mrMergedAt", () => {
const f = readMrInfoFields({ state: "merged", merged_time: 1780000000 });
const patch = mrStatePatchFromInfo({ mrState: "merged", mrMergedAt: 1 }, { ok: true, ...f });
expect(patch.mrMergedAt).toBeUndefined();
});
});