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

397 lines
14 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { applicationsFromRepoMetadata, changedFilesOnBranch, projectsFromRepoMetadata, renderPipelineUrl } from "../src/dev_task_projects";
const PROJECTS_META = {
"apps/social-shaped-card": {
bits: { projectType: "跨端", projectName: "tiktok_social_shaped_card" },
},
"apps/business-inbox": {
bits: { projectType: "跨端", projectName: "tiktok_im_business_inbox" },
},
"apps/business-page": {
bits: { projectType: "跨端", projectName: "tiktok_im_business_page" },
},
};
const EDEN = {
scene: {
scm: {
"tiktok/im/business_inbox": { entries: ["business-inbox"] },
"tiktok/im/business_page": { entries: ["business-page"] },
"tiktok/social/shaped_card": { entries: ["social-shaped-card"] },
"tiktok/im/popup_page_lynx": { entries: ["im-popup-page-lynx"] },
"tiktok/im/ai_chat_bot": { entries: ["ai-chat-bot"] },
},
},
};
const PACKAGE_NAMES = {
"apps/social-shaped-card": "social-shaped-card",
"apps/business-inbox": "business-inbox",
"apps/business-page": "business-page",
"apps/im-popup-page-lynx": "im-popup-page-lynx",
"apps/ai-chat-bot": "ai-chat-bot",
};
const WRAPPED_META = {
schemaVersion: 1,
bitsConsole: {
spaceId: "39151416834",
pipelineUrlTemplate:
"https://bits.bytedance.net/devops/{spaceId}/pipeline/detail/{pipelineId}?pipelineID={pipelineId}&enterType=all&activeTab=0&devops_space_type=server_fe",
},
projects: {
"apps/social-shaped-card": {
channel: "tiktok_social_shaped_card",
deploy: {
scmRepo: "tiktok/social/shaped_card",
test: {
method: "bits-dev-task",
bits: { projectUniqueId: "69b9657f968778c27a32433b" },
},
},
},
"apps/business-page": {
channel: "tiktok_im_business_page_lynx",
deploy: {
scmRepo: "tiktok/im/business_page",
test: {
method: "bits-dev-task",
bits: { projectUniqueId: "68d38ce6003b041ba643fcf3" },
},
},
},
"apps/im-popup-page-lynx": {
channel: "tiktok_im_popup_page_lynx",
deploy: {
scmRepo: "tiktok/im/popup_page_lynx",
test: {
method: "bits-pipeline",
pipeline: {
pipelineId: "1189494497026",
pipelineName: "[Internal] IM Popup Page Lynx-V2",
branchVar: "revision",
laneVar: "ppe_lane",
},
},
},
},
"apps/ai-chat-bot": {
channel: "dm_ai_chat_bot",
deploy: {
test: { method: "none" },
},
},
},
};
describe("Bits 应用始终从 tt_im_monorepo 元数据解析", () => {
test("当前失败任务的 business-page 修复解析到唯一跨端应用和 SCM", () => {
const result = applicationsFromRepoMetadata(
PROJECTS_META,
EDEN,
PACKAGE_NAMES,
"唯一改动:apps/business-page/src/components/MessageInputModal/index.tsx",
);
expect(result).toEqual([{
sourceRoot: "apps/business-page",
projectName: "tiktok_im_business_page",
scmName: "tiktok/im/business_page",
}]);
});
test("同一修复可选中多个真实子项目,顺序跟 projects-meta 一致", () => {
const result = applicationsFromRepoMetadata(
PROJECTS_META,
EDEN,
PACKAGE_NAMES,
"apps/social-shaped-card/src/a.ts\napps/business-inbox/src/b.ts",
);
expect(result.map((item) => item.projectName)).toEqual([
"tiktok_social_shaped_card",
"tiktok_im_business_inbox",
]);
});
test("路径边界不会把 business-page-old 误认为 business-page", () => {
expect(applicationsFromRepoMetadata(
PROJECTS_META,
EDEN,
PACKAGE_NAMES,
"apps/business-page-old/src/index.ts",
)).toEqual([]);
});
test("新项目只需更新仓库元数据,Streamline 无静态名单", () => {
const projectsMeta = {
...PROJECTS_META,
"apps/new-lynx-app": { bits: { projectType: "跨端", projectName: "new_bits_app" } },
};
const eden = {
scene: { scm: { ...EDEN.scene.scm, "tiktok/im/new_lynx_app": { entries: ["new-lynx-app"] } } },
};
expect(applicationsFromRepoMetadata(
projectsMeta,
eden,
{ ...PACKAGE_NAMES, "apps/new-lynx-app": "new-lynx-app" },
"apps/new-lynx-app/src/index.tsx",
)).toEqual([{
sourceRoot: "apps/new-lynx-app",
projectName: "new_bits_app",
scmName: "tiktok/im/new_lynx_app",
}]);
});
test("跨端类型或 Eden SCM 映射不合法时 fail closed", () => {
expect(() => applicationsFromRepoMetadata(
{ "apps/business-page": { bits: { projectType: "Web", projectName: "wrong" } } },
EDEN,
PACKAGE_NAMES,
"apps/business-page/src/index.tsx",
)).toThrow("未在 projects-meta.json 配置为 Bits「跨端」项目");
expect(() => applicationsFromRepoMetadata(
PROJECTS_META,
{ scene: { scm: {} } },
PACKAGE_NAMES,
"apps/business-page/src/index.tsx",
)).toThrow("对应 0 个 SCM");
});
test("Eden entry 来自项目 package name,不假设等于目录 basename", () => {
const projectsMeta = {
"apps/directory-name": { bits: { projectType: "跨端", projectName: "bits_name" } },
};
const eden = { scene: { scm: { "tiktok/im/scm_name": { entries: ["actual-package-name"] } } } };
expect(applicationsFromRepoMetadata(
projectsMeta,
eden,
{ "apps/directory-name": "actual-package-name" },
"apps/directory-name/src/index.tsx",
)).toEqual([{
sourceRoot: "apps/directory-name",
projectName: "bits_name",
scmName: "tiktok/im/scm_name",
}]);
});
});
describe("现行 projects-meta:按 deploy.test.method 分叉", () => {
test("bits-dev-task 带上仓内 projectUniqueId,不再搜 projectName", () => {
const result = projectsFromRepoMetadata(
WRAPPED_META,
EDEN,
PACKAGE_NAMES,
"apps/business-page/src/index.tsx",
);
expect(result.applications).toEqual([{
sourceRoot: "apps/business-page",
projectName: "tiktok_im_business_page_lynx",
scmName: "tiktok/im/business_page",
projectUniqueId: "68d38ce6003b041ba643fcf3",
}]);
expect(result.pipelines).toEqual([]);
});
test("bits-pipeline 不进 create 列表,只出控制台链接和变量名", () => {
const result = projectsFromRepoMetadata(
WRAPPED_META,
EDEN,
PACKAGE_NAMES,
"apps/im-popup-page-lynx/src/pages/index.tsx",
);
expect(result.applications).toEqual([]);
expect(result.pipelines).toEqual([{
sourceRoot: "apps/im-popup-page-lynx",
pipelineId: "1189494497026",
pipelineName: "[Internal] IM Popup Page Lynx-V2",
url: "https://bits.bytedance.net/devops/39151416834/pipeline/detail/1189494497026?pipelineID=1189494497026&enterType=all&activeTab=0&devops_space_type=server_fe",
branchVar: "revision",
laneVar: "ppe_lane",
}]);
});
test("两种都改到时拆开,none 的路径跳过而不是失败", () => {
const result = projectsFromRepoMetadata(
WRAPPED_META,
EDEN,
PACKAGE_NAMES,
[
"apps/social-shaped-card/src/a.ts",
"apps/im-popup-page-lynx/src/b.ts",
"apps/ai-chat-bot/src/c.ts",
].join("\n"),
);
expect(result.applications.map((a) => a.projectUniqueId)).toEqual(["69b9657f968778c27a32433b"]);
expect(result.pipelines.map((p) => p.pipelineId)).toEqual(["1189494497026"]);
});
test("bits-dev-task 缺 uniqueId 时 fail closed", () => {
expect(() => projectsFromRepoMetadata(
{
schemaVersion: 1,
bitsConsole: WRAPPED_META.bitsConsole,
projects: {
"apps/business-page": {
deploy: { test: { method: "bits-dev-task", bits: {} } },
},
},
},
EDEN,
PACKAGE_NAMES,
"apps/business-page/src/index.tsx",
)).toThrow("缺少 deploy.test.bits.projectUniqueId");
});
test("流水线 URL 用仓内模板,不拼 CLI 回显那种坏链接", () => {
expect(renderPipelineUrl(
WRAPPED_META.bitsConsole.pipelineUrlTemplate,
"39151416834",
"1189494497026",
)).toBe(
"https://bits.bytedance.net/devops/39151416834/pipeline/detail/1189494497026?pipelineID=1189494497026&enterType=all&activeTab=0&devops_space_type=server_fe",
);
expect(renderPipelineUrl(
WRAPPED_META.bitsConsole.pipelineUrlTemplate,
"39151416834",
"1189494497026",
)).not.toContain("/pipeline/1189494497026?");
});
test("javascript: 流水线模板不会进卡片,也不挡同一次解析里的开发任务", () => {
const result = projectsFromRepoMetadata(
{
schemaVersion: 1,
bitsConsole: {
spaceId: "39151416834",
pipelineUrlTemplate: "javascript:alert({pipelineId})",
},
projects: {
"apps/business-page": WRAPPED_META.projects["apps/business-page"],
"apps/im-popup-page-lynx": WRAPPED_META.projects["apps/im-popup-page-lynx"],
},
},
EDEN,
PACKAGE_NAMES,
"apps/business-page/src/a.ts\napps/im-popup-page-lynx/src/b.ts",
);
expect(result.applications.map((a) => a.projectUniqueId)).toEqual(["68d38ce6003b041ba643fcf3"]);
expect(result.pipelines).toEqual([]);
});
test("http 和非 bytedance.net 的流水线链接丢掉", () => {
const result = projectsFromRepoMetadata(
{
schemaVersion: 1,
bitsConsole: {
spaceId: "39151416834",
pipelineUrlTemplate: "https://evil.example/p/{pipelineId}",
},
projects: {
"apps/im-popup-page-lynx": WRAPPED_META.projects["apps/im-popup-page-lynx"],
},
},
EDEN,
PACKAGE_NAMES,
"apps/im-popup-page-lynx/src/b.ts",
);
expect(result.pipelines).toEqual([]);
});
});
describe("按分支 diff 选应用,不按正文提及", () => {
/**
* 生产 7c34c632:方案里顺带写了 apps/streak-pet-lynx,按正文搜就给它建了开发任务,而它一行没改。
* 生产 ce9bf567:真正改的是 apps/im-popup-page-lynx 一个文件。
*/
test("只认真正改到的 source root", () => {
const result = projectsFromRepoMetadata(WRAPPED_META, EDEN, PACKAGE_NAMES, {
changedFiles: ["apps/im-popup-page-lynx/src/pages/streak_milestone/index.tsx"],
});
expect(result.applications).toEqual([]);
expect(result.pipelines.map((p) => p.sourceRoot)).toEqual(["apps/im-popup-page-lynx"]);
});
test("同样的路径只出现在正文里、diff 没碰 ⇒ 什么都不选", () => {
const result = projectsFromRepoMetadata(WRAPPED_META, EDEN, PACKAGE_NAMES, { changedFiles: ["README.md"] });
expect(result.applications).toEqual([]);
expect(result.pipelines).toEqual([]);
});
test("目录边界:business-page-old 下的改动不算 business-page", () => {
const result = projectsFromRepoMetadata(WRAPPED_META, EDEN, PACKAGE_NAMES, {
changedFiles: ["apps/business-page-old/src/index.tsx"],
});
expect(result.applications).toEqual([]);
});
test("diff 同时改到开发任务应用和流水线应用 ⇒ 拆开", () => {
const result = projectsFromRepoMetadata(WRAPPED_META, EDEN, PACKAGE_NAMES, {
changedFiles: ["apps/social-shaped-card/src/a.ts", "apps/im-popup-page-lynx/src/b.ts", "apps/ai-chat-bot/c.ts"],
});
expect(result.applications.map((a) => a.projectUniqueId)).toEqual(["69b9657f968778c27a32433b"]);
expect(result.pipelines.map((p) => p.pipelineId)).toEqual(["1189494497026"]);
});
});
describe("changedFilesOnBranch:分支相对主干的三点 diff", () => {
let root = "";
let work = "";
const git = (cwd: string, ...args: string[]) => {
const r = Bun.spawnSync({
cmd: ["git", "-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false", ...args],
cwd,
env: { ...process.env, GIT_CONFIG_NOSYSTEM: "1", HOME: root },
});
if (r.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr.toString()}`);
return r.stdout.toString();
};
const write = (rel: string, body: string) => {
mkdirSync(path.dirname(path.join(work, rel)), { recursive: true });
writeFileSync(path.join(work, rel), body);
};
beforeAll(() => {
root = mkdtempSync(path.join(tmpdir(), "dev-task-diff-"));
const origin = path.join(root, "origin.git");
work = path.join(root, "work");
git(root, "init", "--bare", "-b", "master", origin);
git(root, "clone", origin, work);
write("apps/im-popup-page-lynx/src/pages/streak_milestone/index.tsx", "export const h = 48;\n");
write("apps/streak-pet-lynx/src/index.tsx", "export const x = 1;\n");
git(work, "add", "-A");
git(work, "commit", "-m", "init");
git(work, "push", "-u", "origin", "master");
// agent 的分支:只改 im-popup-page-lynx。
git(work, "checkout", "-b", "bugfix/streak-milestone-button-height");
write("apps/im-popup-page-lynx/src/pages/streak_milestone/index.tsx", "export const h = 44;\n");
git(work, "commit", "-am", "fix");
// 主干在分叉之后又往前走了,改的是别的应用 —— 三点 diff 不该把它算进来。
git(work, "checkout", "master");
write("apps/streak-pet-lynx/src/index.tsx", "export const x = 2;\n");
git(work, "commit", "-am", "unrelated");
git(work, "push", "origin", "master");
});
afterAll(() => { if (root) rmSync(root, { recursive: true, force: true }); });
test("只列分支自己的改动,主干后来的提交不算", async () => {
expect(await changedFilesOnBranch(work, "bugfix/streak-milestone-button-height", "master"))
.toEqual(["apps/im-popup-page-lynx/src/pages/streak_milestone/index.tsx"]);
});
test("检出里没有这条分支 ⇒ 报错,不猜", async () => {
await expect(changedFilesOnBranch(work, "bugfix/nope", "master")).rejects.toThrow("找不到分支");
});
test("没有主干的远端引用 ⇒ 报错", async () => {
await expect(changedFilesOnBranch(work, "bugfix/streak-milestone-button-height", "develop")).rejects.toThrow("origin/develop");
});
test("分支名不合法 ⇒ 报错,不把它交给 git", async () => {
await expect(changedFilesOnBranch(work, "--output=/tmp/x", "master")).rejects.toThrow("不合法");
await expect(changedFilesOnBranch(work, "a..b", "master")).rejects.toThrow("不合法");
});
});