189 lines
12 KiB
TypeScript
189 lines
12 KiB
TypeScript
import "./setup_env";
|
||
import { describe, test, expect, afterEach } from "bun:test";
|
||
import { PgDialect } from "drizzle-orm/pg-core";
|
||
import {
|
||
DEFAULT_PRODUCT_PROFILE_ID, defaultProductProfiles, validateProductProfiles, snapshotProductProfile,
|
||
workBaseBranch, profileTargetBranch, customBranchNote, resolvedCustomBranch, profileFillBranch,
|
||
validProductBranch, isCustomProductSnapshot, productPromptFor, appendProductPrompt, PRODUCT_PROMPT_MAX,
|
||
} from "@bbbug/shared/product_profiles";
|
||
import { repoByKey, targetBranchForRepo } from "@bbbug/shared/repos";
|
||
import { claimWhere } from "../src/db";
|
||
import { tasks, features, security } from "../src/schema";
|
||
import { submitMrForFix } from "../src/bits";
|
||
import { openApiProductProfilesResponse, withoutProductPrompts } from "../src/product_profiles";
|
||
|
||
function enabled() {
|
||
const c = defaultProductProfiles();
|
||
c.profiles.forEach((p) => { p.enabled = true; });
|
||
return c;
|
||
}
|
||
function profile(c: ReturnType<typeof enabled>, id: string) {
|
||
return c.profiles.find((p) => p.id === id)!;
|
||
}
|
||
const realFetch = globalThis.fetch;
|
||
afterEach(() => { globalThis.fetch = realFetch; });
|
||
|
||
describe("opt-in product profiles", () => {
|
||
test("TikTok is the enabled default; Lemon8/Notes stay drafts; legacy work is unchanged", () => {
|
||
const d = defaultProductProfiles();
|
||
expect(d.profiles[0]).toMatchObject({ id: DEFAULT_PRODUCT_PROFILE_ID, name: "TikTok", enabled: true });
|
||
expect(d.profiles.filter((p) => p.id !== "tiktok").every((p) => !p.enabled)).toBe(true);
|
||
expect(snapshotProductProfile(enabled(), null, "ios")).toBeNull();
|
||
expect(workBaseBranch({ platform: "ios" }, "node-default")).toBe("node-default");
|
||
expect(profileTargetBranch({ platform: "ios" }, "tiktok-ios", "control-default")).toBe("control-default");
|
||
expect(customBranchNote({ platform: "ios" } as { baseRef?: string | null }, "develop")).toBe("");
|
||
expect(resolvedCustomBranch(snapshotProductProfile(enabled(), "tiktok", "ios"), null)).toBeNull();
|
||
});
|
||
test("TikTok snapshots develop on both platforms", () => {
|
||
expect(snapshotProductProfile(enabled(), "tiktok", "ios")).toMatchObject({ repoPath: "ugc/TikTok", baseBranch: "develop", profileId: "tiktok" });
|
||
expect(snapshotProductProfile(enabled(), "tiktok", "android")).toMatchObject({ repoPath: "ugc-android/TikTok", baseBranch: "develop", profileId: "tiktok" });
|
||
expect(isCustomProductSnapshot(snapshotProductProfile(enabled(), "tiktok", "ios"))).toBe(false);
|
||
});
|
||
for (const [id, platform, base, path] of [
|
||
["lemon8", "ios", "lemon8/develop", "ugc/TikTok"],
|
||
["lemon8", "android", "develop", "ugc-android/TikTok"],
|
||
["notes", "ios", "notes/develop", "ugc/TikTok"],
|
||
["notes", "android", "notes/develop", "ugc-android/TikTok"],
|
||
] as const) test(`${id}/${platform} snapshots the correct repository and branches`, () => {
|
||
const snap = snapshotProductProfile(enabled(), id, platform);
|
||
expect(snap).toMatchObject({ repoPath: path, baseBranch: base, targetBranch: base, platform, profileId: id });
|
||
expect(isCustomProductSnapshot(snap)).toBe(true);
|
||
expect(resolvedCustomBranch(snap, null)).toBe(base);
|
||
expect(resolvedCustomBranch(snap, "feature/x")).toBe("feature/x");
|
||
});
|
||
test("selecting a non-default product fills custom branch once; explicit baseRef still wins", () => {
|
||
const c = enabled();
|
||
profile(c, "notes").platforms.ios!.targetBranch = "notes/release";
|
||
const productProfile = snapshotProductProfile(c, "notes", "ios")!;
|
||
const work = { platform: "ios" as const, productProfile, baseRef: null as string | null };
|
||
expect(workBaseBranch(work, "develop")).toBe("notes/develop");
|
||
expect(profileTargetBranch(work, "tiktok-ios", "develop")).toBe("notes/release");
|
||
expect(profileFillBranch(profile(c, "notes"), "ios")).toBe("notes/develop");
|
||
expect(profileFillBranch(profile(c, "tiktok"), "ios")).toBeNull();
|
||
expect(workBaseBranch({ ...work, baseRef: "feature/accept" }, "develop")).toBe("feature/accept");
|
||
expect(profileTargetBranch({ ...work, baseRef: "feature/accept" }, "tiktok-ios", "develop")).toBe("feature/accept");
|
||
expect(customBranchNote({ baseRef: "notes/develop" }, "develop")).toContain("notes/develop");
|
||
expect(customBranchNote({ baseRef: "develop" }, "develop")).toBe("");
|
||
});
|
||
test("missing, disabled, unsupported platforms and sidecar overrides fail closed", () => {
|
||
expect(() => snapshotProductProfile(enabled(), "missing", "ios")).toThrow();
|
||
expect(() => snapshotProductProfile(defaultProductProfiles(), "notes", "ios")).toThrow();
|
||
const c = enabled();
|
||
delete profile(c, "notes").platforms.android;
|
||
expect(() => snapshotProductProfile(c, "notes", "android")).toThrow();
|
||
const productProfile = snapshotProductProfile(c, "notes", "ios");
|
||
expect(() => profileTargetBranch({ platform: "ios", productProfile }, "tt-im-monorepo", "master")).toThrow();
|
||
expect(targetBranchForRepo(repoByKey("tt-im-monorepo")!, null)).toBe("master");
|
||
});
|
||
test("validation prevents branch injection and unregistered repositories", () => {
|
||
for (const branch of ["", "-delete", "HEAD", "x..y", "refs/heads/x", "x//y", "x.lock", "x/../y", "x\ny", "x:evil", "x;touch", "x@{1}"]) expect(validProductBranch(branch), branch).toBe(false);
|
||
expect(validProductBranch("notes/release-1.2")).toBe(true);
|
||
const c = enabled();
|
||
profile(c, "tiktok").platforms.ios!.repoKey = "tt-im-monorepo";
|
||
expect(() => validateProductProfiles(c.profiles)).toThrow();
|
||
expect(() => validateProductProfiles([enabled().profiles[0], enabled().profiles[0]])).toThrow();
|
||
});
|
||
test("old nodes are filtered before claim, on both tracks and both stages", () => {
|
||
const dialect = new PgDialect();
|
||
for (const table of [tasks, features]) for (const status of ["initializing", "awaiting_fix"]) {
|
||
const query = (version?: number) => dialect.sqlToQuery(claimWhere(table, status, "ios", "node", [], ["default"], { productProfilesVersion: version, steal: true })).sql;
|
||
expect(query()).toContain('"product_profile" is null');
|
||
expect(query()).toContain("profileId");
|
||
expect(query(1)).not.toContain('"product_profile" is null');
|
||
expect(query(2)).toContain('"product_profile" is null');
|
||
}
|
||
expect(dialect.sqlToQuery(claimWhere(security, "initializing", "ios", "node", [], ["default"])).sql).not.toContain("product_profile");
|
||
});
|
||
test("Bits receives the snapshot's requested target and registered project", async () => {
|
||
const calls: { url: string; body: any }[] = [];
|
||
globalThis.fetch = (async (url: any, init?: RequestInit) => {
|
||
calls.push({ url: String(url), body: init?.body ? JSON.parse(String(init.body)) : null });
|
||
return Response.json({ code: 200, data: { mr_id: 8327494 } });
|
||
}) as typeof fetch;
|
||
const c = enabled();
|
||
profile(c, "notes").platforms.ios!.targetBranch = "notes/release";
|
||
await submitMrForFix({ platform: "ios", sourceBranch: "feature/test-profile", productProfile: snapshotProductProfile(c, "notes", "ios"), title: "Test", mrType: "feature" });
|
||
expect(calls.find((c) => c.url.includes("/merge_request/create"))?.body.hosts).toEqual([
|
||
{ project_gitlab_id: 114467, source: "feature/test-profile", target: "notes/release" },
|
||
]);
|
||
});
|
||
});
|
||
|
||
describe("open-platform product profile options", () => {
|
||
test("lists enabled profiles only, in config order, with the default surfaced", () => {
|
||
const c = enabled();
|
||
profile(c, "notes").enabled = false;
|
||
const res = openApiProductProfilesResponse(c);
|
||
expect(res.defaultProfileId).toBe(DEFAULT_PRODUCT_PROFILE_ID);
|
||
expect(res.revision).toBe(c.revision);
|
||
expect(res.profiles.map((p) => p.id)).toEqual(["tiktok", "lemon8"]);
|
||
});
|
||
|
||
test("carries the repository path a caller cannot derive from the id alone", () => {
|
||
const res = openApiProductProfilesResponse(enabled());
|
||
expect(res.profiles.find((p) => p.id === "tiktok")).toEqual({
|
||
id: "tiktok",
|
||
name: "TikTok",
|
||
platforms: {
|
||
ios: { repoPath: "ugc/TikTok", baseBranch: "develop", targetBranch: "develop" },
|
||
android: { repoPath: "ugc-android/TikTok", baseBranch: "develop", targetBranch: "develop" },
|
||
},
|
||
});
|
||
// A profile limited to one platform does not invent the other one.
|
||
const c = enabled();
|
||
delete profile(c, "notes").platforms.android;
|
||
const notes = openApiProductProfilesResponse(c).profiles.find((p) => p.id === "notes")!;
|
||
expect(Object.keys(notes.platforms)).toEqual(["ios"]);
|
||
});
|
||
|
||
test("the list is the selectable set: a disabled/unknown id still fails at create time", () => {
|
||
const c = defaultProductProfiles(); // only tiktok enabled
|
||
expect(openApiProductProfilesResponse(c).profiles.map((p) => p.id)).toEqual(["tiktok"]);
|
||
expect(() => snapshotProductProfile(c, "notes", "ios")).toThrow();
|
||
expect(() => snapshotProductProfile(c, "nope", "ios")).toThrow();
|
||
});
|
||
});
|
||
|
||
describe("product prompt", () => {
|
||
function withPrompts() {
|
||
const c = enabled();
|
||
profile(c, "tiktok").platforms.android!.prompt = " 默认产品的安卓说明 ";
|
||
profile(c, "notes").platforms.ios!.prompt = "用 {{title}} 这个包";
|
||
return { ...c, profiles: validateProductProfiles(c.profiles) };
|
||
}
|
||
test("validation keeps a trimmed prompt, drops an empty one, rejects oversize / non-text", () => {
|
||
const c = withPrompts();
|
||
expect(profile(c, "tiktok").platforms.android!.prompt).toBe("默认产品的安卓说明");
|
||
expect("prompt" in profile(c, "tiktok").platforms.ios!).toBe(false);
|
||
const blank = enabled(); profile(blank, "tiktok").platforms.ios!.prompt = " ";
|
||
expect("prompt" in validateProductProfiles(blank.profiles)[0].platforms.ios!).toBe(false);
|
||
const big = enabled(); profile(big, "tiktok").platforms.ios!.prompt = "x".repeat(PRODUCT_PROMPT_MAX + 1);
|
||
expect(() => validateProductProfiles(big.profiles)).toThrow();
|
||
const bad = enabled(); (profile(bad, "tiktok").platforms.ios as unknown as { prompt: number }).prompt = 1;
|
||
expect(() => validateProductProfiles(bad.profiles)).toThrow();
|
||
});
|
||
test("never rides in the task snapshot or the non-admin views", () => {
|
||
const c = withPrompts();
|
||
expect("prompt" in snapshotProductProfile(c, "tiktok", "android")!).toBe(false);
|
||
expect(JSON.stringify(withoutProductPrompts(c))).not.toContain("默认产品的安卓说明");
|
||
expect(JSON.stringify(openApiProductProfilesResponse(c))).not.toContain("默认产品的安卓说明");
|
||
});
|
||
test("resolved from current settings by product × platform; unprofiled work uses the default product", () => {
|
||
const c = withPrompts();
|
||
expect(productPromptFor(c, { platform: "android" })).toMatchObject({ name: "TikTok", prompt: "默认产品的安卓说明" });
|
||
expect(productPromptFor(c, { platform: "android", productProfile: { profileId: "tiktok" } })?.prompt).toBe("默认产品的安卓说明");
|
||
expect(productPromptFor(c, { platform: "ios" })).toBeNull();
|
||
expect(productPromptFor(c, { platform: "ios", productProfile: { profileId: "notes" } })?.name).toBe("Notes");
|
||
expect(productPromptFor(c, { platform: "android", productProfile: { profileId: "notes" } })).toBeNull();
|
||
expect(productPromptFor(c, { platform: "android", productProfile: { profileId: "gone" } })).toBeNull();
|
||
const off = withPrompts(); profile(off, "notes").enabled = false;
|
||
expect(productPromptFor(off, { platform: "ios", productProfile: { profileId: "notes" } })).not.toBeNull();
|
||
});
|
||
test("appended after the template, with placeholders in the admin's text neutralised", () => {
|
||
const out = appendProductPrompt("主模板 {{title}}\n", productPromptFor(withPrompts(), { platform: "ios", productProfile: { profileId: "notes" } })!);
|
||
expect(out.startsWith("主模板 {{title}}\n\n## 产品定制说明(Notes · iOS)")).toBe(true);
|
||
expect(out).toContain("用 { {title} } 这个包");
|
||
expect(out.match(/\{\{title\}\}/g)?.length).toBe(1);
|
||
});
|
||
});
|