import "./setup_env"; import { expect, test } from "bun:test"; /** * 开放平台 v1:建单带产品(工程)配置 + 图片附件上传的端到端契约。 * * 只连显式给出的本地库:TEST_DATABASE_URL 必须指向 127.0.0.1 / localhost。 * 没有库时整组跳过(普通 `bun test` 不依赖数据库)。**建议指向一次性的本地库** * (例如 streamline_profiles_test),因为测试会临时改写产品配置、并留下两三条任务; * 产品配置会在 finally 里恢复原样,任务不删(本地库本来就是可丢弃的)。 * * TEST_DATABASE_URL=postgres://…@127.0.0.1:55438/… bun test \ * control-server/test/openapi_product_attachments.integration.test.ts * * 外部网络一律不许出去:TOS 的 PUT 截到内存里,其它请求直接让测试失败。 */ const url = process.env.TEST_DATABASE_URL; const local = !!url && ["127.0.0.1", "localhost"].includes(new URL(url).hostname); const WHEE = { id: "whee", name: "Whee", enabled: true, platforms: { ios: { repoKey: "tiktok-ios", baseBranch: "whee/develop", targetBranch: "whee/develop" }, android: { repoKey: "tiktok-android", baseBranch: "whee/develop", targetBranch: "whee/develop" }, }, } as const; test.skipIf(!local)("产品配置选项、建单带 productProfileId、图片上传 → description", async () => { const realFetch = globalThis.fetch; const tosPuts: string[] = []; const savedEnv = { TOS_ACCESS_KEY: process.env.TOS_ACCESS_KEY, TOS_SECRET_KEY: process.env.TOS_SECRET_KEY, TOS_S3_ENDPOINT: process.env.TOS_S3_ENDPOINT, }; // 配置先于 import routes:tos.ts 在模块加载时读凭据(若已被别的测试先加载过, // 本地 .env 里的真凭据也只会发到下面这个 stub 上,不会有真流量)。 process.env.TOS_ACCESS_KEY = "test-ak"; process.env.TOS_SECRET_KEY = "test-sk"; process.env.TOS_S3_ENDPOINT = "http://tos.test"; globalThis.fetch = (async (input: any, init?: RequestInit) => { const href = String(input); if ((init?.method || "GET").toUpperCase() === "PUT" && href.includes("/img_upload/")) { tosPuts.push(href); return new Response("", { status: 200 }); } throw new Error(`integration test forbids outbound request: ${init?.method || "GET"} ${href}`); }) as typeof fetch; const keyIds: string[] = []; // 产品配置不是测试可以永久改写的状态:跑完恢复成进来时的样子。 let originalProfiles: { revision: number; profiles: unknown[] } | null = null; let restoreProfiles: ((profiles: unknown[], revision: number) => Promise) | null = null; let revokeKey: ((id: string) => Promise) | null = null; try { const { api } = await import("../src/routes"); const { createOpenPlatformKey, revokeOpenPlatformKey } = await import("../src/openplatform_keys"); const { readProductProfiles, saveProductProfiles } = await import("../src/product_profiles"); const { getTask } = await import("../src/db"); const { isTosConfigured } = await import("../src/tos"); revokeKey = revokeOpenPlatformKey; restoreProfiles = saveProductProfiles; // 路由确实挂在 /openapi/v1 下;未带 Bearer 时先 401,不会掉进别的鉴权口。 const registered = api.routes.map((r) => `${r.method} ${r.path}`); expect(registered).toContain("GET /openapi/v1/product-profiles"); expect(registered).toContain("POST /openapi/v1/uploads/image"); expect(registered).toContain("POST /openapi/v1/features"); expect(registered).toContain("GET /openapi/v1/features"); expect(registered).toContain("GET /openapi/v1/features/:featureId"); expect(registered).toContain("POST /openapi/v1/features/:featureId/execute"); expect((await api.request("/openapi/v1/features")).status).toBe(401); expect((await api.request("/openapi/v1/product-profiles")).status).toBe(401); expect((await api.request("/openapi/v1/uploads/image", { method: "POST", body: new FormData() })).status).toBe(401); // 确保有 whee(测试要验非默认产品)。只动 whee 一条,不把开发库里的产品全启用; // 原配置留到 finally 恢复。 const current = await readProductProfiles(); originalProfiles = { revision: current.revision, profiles: current.profiles }; const profiles = current.profiles.filter((p) => p.id !== WHEE.id); profiles.push({ ...WHEE, enabled: true, platforms: { ...WHEE.platforms } }); await saveProductProfiles(profiles, current.revision); const mine = await createOpenPlatformKey({ label: "attachment fixture", source: "local-test", createdBy: "local-test@bytedance.com" }); keyIds.push(mine.key.id); const key = { Authorization: `Bearer ${mine.secret}` }; const req = (path: string, init: { method?: string; body?: unknown; headers?: Record } = {}) => { const multipart = init.body instanceof FormData; return api.request(path, { method: init.method ?? (init.body === undefined ? "GET" : "POST"), headers: { ...(init.body === undefined || multipart ? {} : { "content-type": "application/json" }), ...init.headers, }, body: init.body === undefined ? undefined : multipart ? (init.body as FormData) : JSON.stringify(init.body), }); }; // ── 1. 选项:和面板同源的启用产品,默认值也在响应里 ──────────────────── const listed = await req("/openapi/v1/product-profiles", { headers: key }); expect(listed.status).toBe(200); const options = await listed.json() as any; expect(options.defaultProfileId).toBe("tiktok"); expect(options.profiles.map((p: any) => p.id)).toContain("whee"); const whee = options.profiles.find((p: any) => p.id === "whee"); expect(whee).toMatchObject({ id: "whee", name: "Whee", platforms: { android: { repoPath: "ugc-android/TikTok", baseBranch: "whee/develop", targetBranch: "whee/develop" } }, }); expect((await req("/openapi/v1/product-profiles")).status).toBe(401); // ── 2. 建单带 productProfileId:快照 + 基线都落到任务上 ───────────────── const created = await req("/openapi/v1/tasks", { headers: key, body: { title: "Whee 产品配置链路", description: "复现步骤见下", platform: "android", productProfileId: "whee" }, }); expect(created.status).toBe(200); const taskId = (await created.json() as any).taskId as string; const stored = await getTask(taskId); expect(stored?.productProfile).toMatchObject({ profileId: "whee", profileName: "Whee", baseBranch: "whee/develop" }); // 非默认产品把配置的基线写进 baseRef,执行侧才会检出正确的分支。 expect(stored?.baseRef).toBe("whee/develop"); const view = await (await req(`/openapi/v1/tasks/${taskId}`, { headers: key })).json() as any; expect(view.productProfileId).toBe("whee"); expect(view.productProfileName).toBe("Whee"); // 不可用的产品 id 是 400(不是静默退回默认产品)。 const bad = await req("/openapi/v1/tasks", { headers: key, body: { title: "bad profile", description: "x", platform: "android", productProfileId: "does-not-exist" }, }); expect(bad.status).toBe(400); // ── 2b. 一句话需求:建单 / 去重 / 归属闸 / 执行前置 ────────────────────── const { getFeature } = await import("../src/db"); const createdFeature = await req("/openapi/v1/features", { headers: key, body: { description: "把收藏页的排序改成按最近收藏", platform: "ios", productProfileId: "whee", externalId: "PRD-1" }, }); expect(createdFeature.status).toBe(200); const createdBody = await createdFeature.json() as any; expect(createdBody.status).toBe("created"); const featureId = createdBody.featureId as string; const storedFeature = await getFeature(featureId); // 和 tasks 同一套产品快照语义:feature 上也有。 expect(storedFeature?.productProfile).toMatchObject({ profileId: "whee", profileName: "Whee" }); expect(storedFeature?.baseRef).toBe("whee/develop"); expect(storedFeature?.createdViaKeyId).toBeTruthy(); expect(storedFeature?.externalId).toBe("PRD-1"); // 一句话需求:title 从 description 的第一条非空行推导。 expect(storedFeature?.title).toContain("收藏页"); // 需求单和 bug 不同:Key 的申请人自动进 watchers(要人看方案、确认、复核)。 expect(storedFeature?.watchers).toContain("local-test@bytedance.com"); // externalId 按 Key 去重:第二次回已有任务,不改已有配置。 const dedupedFeature = await req("/openapi/v1/features", { headers: key, body: { description: "换个说法", platform: "android", externalId: "PRD-1" }, }); expect(dedupedFeature.status).toBe(200); expect(await dedupedFeature.json() as any).toMatchObject({ featureId, status: "exists" }); const featureView = await req(`/openapi/v1/features/${featureId}`, { headers: key }); expect(featureView.status).toBe(200); expect(await featureView.json() as any).toMatchObject({ featureId, externalId: "PRD-1", platform: "ios", productProfileId: "whee", // 与任务视图对齐:评估中时没有打回原因、也没有待补项。 infoRequired: null, needsInput: [], }); const listedFeatures = await (await req("/openapi/v1/features?limit=5", { headers: key })).json() as any; expect(listedFeatures.features.map((f: any) => f.featureId)).toContain(featureId); // 归属闸:别的 Key 查不到、也执行不了;归属先于状态,403 里不带 featureStatus。 const otherKeyMake = await createOpenPlatformKey({ label: "attachment fixture 2", source: "local-test", createdBy: "local-test@bytedance.com" }); keyIds.push(otherKeyMake.key.id); const other = { Authorization: `Bearer ${otherKeyMake.secret}` }; const foreign = await req(`/openapi/v1/features/${featureId}`, { headers: other }); expect(foreign.status).toBe(403); const foreignBody = await foreign.json() as any; expect(foreignBody.featureStatus).toBeUndefined(); const foreignExec = await req(`/openapi/v1/features/${featureId}/execute`, { method: "POST", headers: other }); expect(foreignExec.status).toBe(403); expect((await foreignExec.json() as any).featureStatus).toBeUndefined(); // 执行前置:没有卫星在跑评估,状态停在 initializing —— 不是待确认,409 并带回状态。 const earlyExec = await req(`/openapi/v1/features/${featureId}/execute`, { method: "POST", headers: key }); expect(earlyExec.status).toBe(409); expect(await earlyExec.json() as any).toMatchObject({ code: "wrong_status", featureStatus: "initializing" }); // ── 2c. 写路径:CAS 确认执行(面板和 API 共用同一次迁移)───────────────── // 真实评估由卫星跑;这里直接把状态/方案写成「已起草完」的样子。 const { updateFeature, confirmFeatureIntoQueue } = await import("../src/db"); const { startFeatureExecution } = await import("../src/feature_execution"); await updateFeature(featureId, { status: "pending_confirmation", executionPlan: "## 实现方案\n1. 加开关" }); // 归属先于状态:别的 Key 推不动别人建的需求。 expect(await startFeatureExecution(featureId, { requireCreatedViaKeyId: otherKeyMake.key.id })) .toMatchObject({ ok: false, reason: "not_owner" }); // 本 Key 推进:pending_confirmation → awaiting_implementation,方案落库。 const ownExec = await startFeatureExecution(featureId, { requireCreatedViaKeyId: mine.key.id }); expect(ownExec.ok).toBe(true); if (ownExec.ok) expect(ownExec.feature.status).toBe("awaiting_implementation"); expect((await getFeature(featureId))?.status).toBe("awaiting_implementation"); expect((await getFeature(featureId))?.executionPlan).toContain("加开关"); // 重复推进、以及面板那侧直接走 CAS 的二次确认,都必须落空 —— 不能把方案覆盖成第二份 // (面板 confirm 路由调的就是 startFeatureExecution / confirmFeatureIntoQueue 这一对)。 expect(await startFeatureExecution(featureId, { requireCreatedViaKeyId: mine.key.id })) .toMatchObject({ ok: false, reason: "wrong_status" }); expect(await confirmFeatureIntoQueue(featureId, "## 另一份方案")).toBe(false); expect((await getFeature(featureId))?.executionPlan).toContain("加开关"); // on-wire 复核:已是 awaiting_implementation 时 409 带 featureStatus。 const lateExec = await req(`/openapi/v1/features/${featureId}/execute`, { method: "POST", headers: key }); expect(lateExec.status).toBe(409); expect(await lateExec.json() as any).toMatchObject({ code: "wrong_status", featureStatus: "awaiting_implementation" }); // 不存在的需求 / 非法平台 / 不可用产品各回各的状态码。 expect((await req("/openapi/v1/features/does-not-exist", { headers: key })).status).toBe(404); expect((await req("/openapi/v1/features/does-not-exist/execute", { method: "POST", headers: key })).status).toBe(404); expect((await req("/openapi/v1/features", { headers: key, body: { description: "x", platform: "web" } })).status).toBe(400); // platform 必填:不落环境默认值。 const noPlatform = await req("/openapi/v1/features", { headers: key, body: { description: "x" } }); expect(noPlatform.status).toBe(400); expect((await noPlatform.json() as any).error).toContain("platform is required"); // larkReply 和 bug 同一套建单前校验:形状不对直接 400,不建单。 const badReply = await req("/openapi/v1/features", { headers: key, body: { description: "x", platform: "ios", larkReply: "om_x" } }); expect(badReply.status).toBe(400); expect((await badReply.json() as any).code).toBe("invalid_lark_reply"); expect((await req("/openapi/v1/features", { headers: key, body: { description: "x", platform: "ios", productProfileId: "does-not-exist" } })).status).toBe(400); // ── 3. 图片上传 → markdown → 建单 description ─────────────────────────── // tos.ts 若在本进程里已被别的测试文件先加载且没配凭据,isTosConfigured 会是 // false;这时跳过上传断言而不是让整条测试失败(其余断言仍然有效)。 if (!isTosConfigured()) { console.warn("[openapi-attach-test] TOS not configured in this process — skipping upload assertions"); return; } const form = new FormData(); form.append("file", new File([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], "shot.png", { type: "image/png" })); const uploaded = await req("/openapi/v1/uploads/image", { method: "POST", body: form, headers: key }); expect(uploaded.status).toBe(200); const image = await uploaded.json() as any; expect(image).toMatchObject({ ext: "png", name: "shot.png" }); expect(image.url).toContain("/img_upload/"); expect(image.markdown).toBe(`![shot.png](${image.url})`); // 存到 TOS 的对象名就是 .,卫星按 URL 里的文件名落盘。 expect(tosPuts.some((u) => u.endsWith(`/${image.uuid}.png`))).toBe(true); const withImage = await req("/openapi/v1/tasks", { headers: key, body: { title: "带截图的通用建单", description: `现象如下:\n\n${image.markdown}`, platform: "ios" }, }); expect(withImage.status).toBe(200); const imageTask = await getTask((await withImage.json() as any).taskId as string); expect(imageTask?.description).toContain(image.markdown); // 不传产品 → 不落快照(执行走节点/控制面主干默认;生产默认配置下等价 TikTok/develop)。 expect(imageTask?.productProfile ?? null).toBeNull(); // 参数错误各回各的状态码。 const noFile = await req("/openapi/v1/uploads/image", { method: "POST", body: new FormData(), headers: key }); expect(noFile.status).toBe(400); const textFile = new FormData(); textFile.append("file", new File([new Uint8Array([1])], "log.txt", { type: "text/plain" })); expect((await req("/openapi/v1/uploads/image", { method: "POST", body: textFile, headers: key })).status).toBe(415); } finally { globalThis.fetch = realFetch; for (const [k, v] of Object.entries(savedEnv)) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } for (const id of keyIds) { try { await revokeKey?.(id); } catch { /* best-effort cleanup */ } } if (originalProfiles && restoreProfiles) { try { const { readProductProfiles } = await import("../src/product_profiles"); const now = await readProductProfiles(); await restoreProfiles(originalProfiles.profiles, now.revision); } catch (err) { console.warn("[openapi-attach-test] failed to restore product profiles:", (err as Error).message); } } } }, 60_000);