Initial import of streamline
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// 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 { submitMrForFix } from "../src/bits";
|
||||
import { withMrSubmitLock } from "../src/mr_submit_lock";
|
||||
|
||||
/**
|
||||
* 回归:Bits 的 /openapi/feature/bind/mr 不幂等。线上 MR 8553164 / 8547413 / 8547028 都是两路提交
|
||||
* 同时跑,后到的认领同一条 MR 后又绑了一次,meego_features 里出现两条相同工单。
|
||||
*/
|
||||
const MEEGO = "7388011095";
|
||||
const MR_ID = 8553164;
|
||||
|
||||
const realFetch = globalThis.fetch;
|
||||
let calls: string[] = [];
|
||||
|
||||
function stubBits(opts: { boundRows?: unknown[] | "fail" }) {
|
||||
calls = [];
|
||||
globalThis.fetch = (async (input: any) => {
|
||||
const url = typeof input === "string" ? input : input.url;
|
||||
calls.push(url);
|
||||
if (url.includes("/openapi/feature/mr_info")) {
|
||||
if (opts.boundRows === "fail") return new Response("upstream", { status: 502 });
|
||||
return Response.json({ code: 200, data: opts.boundRows ?? [] });
|
||||
}
|
||||
return Response.json({ code: 200, data: { mr_id: MR_ID } });
|
||||
}) as typeof fetch;
|
||||
}
|
||||
|
||||
beforeEach(() => { calls = []; });
|
||||
afterAll(() => { globalThis.fetch = realFetch; });
|
||||
|
||||
const binds = () => calls.filter((u) => u.includes("/openapi/feature/bind/mr"));
|
||||
const lookups = () => calls.filter((u) => u.includes("/openapi/feature/mr_info"));
|
||||
|
||||
function submit(existingMrId?: number) {
|
||||
return submitMrForFix({
|
||||
platform: "android", sourceBranch: "bugfix/x", title: "t", mrType: "bug",
|
||||
meegoWorkItems: [{ taskId: MEEGO, taskType: "bug", projectKey: "tiktok" }], bitsUser: "someone", tokenUser: "someone", existingMrId,
|
||||
});
|
||||
}
|
||||
|
||||
describe("绑 Meego", () => {
|
||||
test("新建的 MR 直接绑,不用先查", async () => {
|
||||
stubBits({});
|
||||
const r = await submit();
|
||||
expect(r.ok).toBe(true);
|
||||
expect(lookups()).toHaveLength(0);
|
||||
expect(binds()).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("认领的 MR 已绑过这张工单 ⇒ 不再绑", async () => {
|
||||
stubBits({ boundRows: [{ mr_id: MR_ID, mr_title: "t" }] });
|
||||
const r = await submit(MR_ID);
|
||||
expect(binds()).toHaveLength(0);
|
||||
expect(r.steps.find((s) => s.step === "bind_meego")).toMatchObject({ ok: true, error: "already bound — skipped" });
|
||||
});
|
||||
|
||||
test("认领的 MR 还没绑(工单只绑了别的 MR)⇒ 照常绑", async () => {
|
||||
stubBits({ boundRows: [{ mr_id: 1, mr_title: "other" }] });
|
||||
await submit(MR_ID);
|
||||
expect(binds()).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("查询失败 ⇒ 仍然绑:重复只是难看,漏绑会断掉关联", async () => {
|
||||
stubBits({ boundRows: "fail" });
|
||||
await submit(MR_ID);
|
||||
expect(binds()).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("withMrSubmitLock", () => {
|
||||
const gate = () => {
|
||||
let open!: () => void;
|
||||
const opened = new Promise<void>((r) => { open = r; });
|
||||
return { open, opened };
|
||||
};
|
||||
|
||||
test("同一张单排队跑,后到的看得见前一路的结果", async () => {
|
||||
const g = gate();
|
||||
const order: string[] = [];
|
||||
let mrUrl: string | null = null;
|
||||
const first = withMrSubmitLock("bug", "t1", async () => {
|
||||
order.push("first:start");
|
||||
await g.opened;
|
||||
mrUrl = "mr";
|
||||
order.push("first:end");
|
||||
});
|
||||
const second = withMrSubmitLock("bug", "t1", async () => {
|
||||
order.push("second:start");
|
||||
return mrUrl;
|
||||
});
|
||||
await Promise.resolve();
|
||||
g.open();
|
||||
await first;
|
||||
expect(await second).toBe("mr");
|
||||
expect(order).toEqual(["first:start", "first:end", "second:start"]);
|
||||
});
|
||||
|
||||
test("不同的单、不同的类型互不阻塞", async () => {
|
||||
const g = gate();
|
||||
const held = withMrSubmitLock("bug", "t2", () => g.opened);
|
||||
expect(await withMrSubmitLock("bug", "t3", async () => "other id")).toBe("other id");
|
||||
expect(await withMrSubmitLock("feature", "t2", async () => "other kind")).toBe("other kind");
|
||||
g.open();
|
||||
await held;
|
||||
});
|
||||
|
||||
test("前一路抛错:错误还给它自己,后一路照常跑", async () => {
|
||||
const first = withMrSubmitLock("bug", "t4", async () => { throw new Error("boom"); });
|
||||
const second = withMrSubmitLock("bug", "t4", async () => "ran");
|
||||
await expect(first).rejects.toThrow("boom");
|
||||
expect(await second).toBe("ran");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user