Files
streamline/control-server/test/eddy_export.test.ts
T
2026-11-16 17:26:44 -08:00

106 lines
7.4 KiB
TypeScript

import "./setup_env";
import { describe, expect, test } from "bun:test";
import { createHash, createHmac } from "node:crypto";
import { activeExport, advanceExport, checkExportResult, exportBlockReason, exportPeers, productForBaseline } from "@bbbug/shared/eddy_export";
import { signS3Request } from "../src/s3_sign";
import { exportObjectKey, signExportUpload, verifyExportUpload } from "../src/eddy_export_tos";
const commit = "a".repeat(40);
const bytes = new Uint8Array([1,2,3]);
const sha256 = createHash("sha256").update(bytes).digest("hex");
const result = { baselineId: "tiktokm-47-470000-12345678", commit, eddyVersion: "v2.8", devicesimSha256: "d".repeat(64),
artifacts: { app: {file: "baseline-tiktokm-47-470000-12345678.zip", bytes: 3, sha256 },
source: {file:"baseline-tiktokm-47-470000-12345678-source.zip", bytes:3, sha256} } };
describe("feature Eddy exports", () => {
test("eligibility requires an implemented iOS task with a known delivery branch", () => {
const f = {platform:"ios",status:"manual_review",repoKey:"tiktok-ios",branchName:"feature/demo",mrUrl:"https://example.invalid/mr/1"};
expect(exportBlockReason(f)).toBeNull();
for (const patch of [{platform:"android"}, {status:"implementing"}, {repoKey:"unpushed"}, {mrUrl:null}]) expect(exportBlockReason({...f,...patch})).not.toBeNull();
});
test("an offline executor's export goes to live iOS peers in its clusters, idle ones first", () => {
const node = (id: string, patch: Record<string, unknown> = {}) =>
({ id, platform: "ios", live: true, clusters: ["default"], fixInFlight: 0, fixCapacity: 1, ...patch });
const nodes = [
node("home", { live: false }), node("busy", { fixInFlight: 1 }), node("idle-b"), node("idle-a"),
node("half", { fixInFlight: 1, fixCapacity: 2 }), node("offline", { live: false }), node("android", { platform: "android" }),
node("lab", { clusters: ["manual-only", "default"] }), node("lemon", { clusters: ["lemon"] }), node("parked", { clusters: [] }),
];
expect(exportPeers(nodes, { executorNodeId: "home" }).map(n => n.id)).toEqual(["idle-a", "idle-b", "half", "busy"]);
expect(exportPeers(nodes, { executorNodeId: "home", clusters: ["lemon"] }).map(n => n.id)).toEqual(["lemon"]);
expect(exportPeers(nodes, { executorNodeId: "idle-a" }).map(n => n.id)).not.toContain("idle-a");
});
test("source commit and exact installable ZIP names are enforced", () => {
expect(checkExportResult(result, commit)).toEqual(result);
expect(() => checkExportResult(result, "b".repeat(40))).toThrow();
expect(() => checkExportResult({...result,artifacts:{...result.artifacts,app:{...result.artifacts.app,file:"../secret"}}},commit)).toThrow();
expect(() => checkExportResult({...result,artifacts:{...result.artifacts,source:null}},commit)).toThrow();
});
test("stages cannot skip native validation or resurrect a terminal job", () => {
expect(advanceExport("building","packaging")).toBe(true);
expect(advanceExport("building","ready")).toBe(false);
expect(advanceExport("ready","building")).toBe(false);
expect(advanceExport("verifying","failed")).toBe(true);
expect(activeExport("failed")).toBe(false);
});
test("streamed signing is identical to signing the full bytes", () => {
const spec = {method:"PUT",endpoint:"https://tos.invalid",path:"/bucket/key",region:"test",accessKey:"key",secretKey:"secret",date:new Date("2026-09-18T00:00:00Z")};
expect(signS3Request({...spec,payload:bytes})).toEqual(signS3Request({...spec,payload:new Uint8Array(),payloadSha256:sha256}));
});
test("readback checks content and length of both download artifacts", async () => {
let calls=0;
await verifyExportUpload("a".repeat(36),result,(async()=>{calls++;return new Response(bytes)}) as typeof fetch);
expect(calls).toBe(2);
await expect(verifyExportUpload("a".repeat(36),result,(async()=>new Response(new Uint8Array([1,2,4]))) as typeof fetch)).rejects.toThrow("完整性");
await expect(verifyExportUpload("a".repeat(36),result,(async()=>new Response(new Uint8Array([1,2,3,4]))) as typeof fetch)).rejects.toThrow("大小");
expect(()=>exportObjectKey("../escape",result.artifacts.app)).toThrow();
});
test("satellite uploads are signed with a TOS temporary signature on the bucket's internal subdomain", () => {
const env = { ...process.env };
process.env.TOS_ACCESS_KEY = "bucket-access-key"; process.env.TOS_SECRET_KEY = "secret";
try {
const id = "a".repeat(36), now = Date.UTC(2026, 9, 15, 12), expiration = String(now / 1000 + 7200);
const signed = signExportUpload(id, result.artifacts.app, now), url = new URL(signed.url);
// Office DNS resolves <bucket>.tos-cn-north.byted.org, not the S3 endpoint or the bare domain.
expect(url.host).toBe("streamline.tos-cn-north.byted.org");
expect(url.pathname).toBe("/" + exportObjectKey(id, result.artifacts.app));
// TOS docs, 「TOS 临时签名」: base64url(hmac-sha256(accesskey, Method\nPath\nSigName\nExpiredAt)), first 30 characters.
const expected = createHmac("sha256", "bucket-access-key")
.update(["PUT", "/streamline/" + exportObjectKey(id, result.artifacts.app), "streamline-eddy-export", expiration].join("\n"))
.digest("base64url").slice(0, 30);
expect(Object.fromEntries(url.searchParams)).toEqual({ "tos-algorithm": "TOS-HMAC-SHA256", "tos-expiration": expiration,
"tos-signame": "streamline-eddy-export", "tos-signature": expected });
expect(signed.url).not.toContain("bucket-access-key");
expect(signed.headers).toEqual({ "content-type": "application/zip", "content-length": "3" });
expect(signExportUpload(id, result.artifacts.source, now).url).not.toContain(expected);
} finally { process.env = env; }
});
test("node endpoints follow the cluster secret: open when unset, checked when set", async () => {
const { eddyExportsApi: api } = await import("../src/routes_eddy_exports");
const { CLUSTER_SECRET_HEADER } = await import("@bbbug/shared/protocol");
// An empty body passes auth and stops at validation (400) before any database call.
const claim = (headers: Record<string, string> = {}) => api.request("/internal/eddy-exports/claim",
{ method: "POST", headers: { "content-type": "application/json", ...headers }, body: "{}" });
const env = { enabled: process.env.EDDY_EXPORT_ENABLED, secret: process.env.CLUSTER_SECRET };
process.env.EDDY_EXPORT_ENABLED = "1";
try {
delete process.env.CLUSTER_SECRET;
expect((await claim()).status).toBe(400);
process.env.CLUSTER_SECRET = "fixture-secret";
expect((await claim()).status).toBe(401);
expect((await claim({ [CLUSTER_SECRET_HEADER]: "wrong" })).status).toBe(401);
expect((await claim({ [CLUSTER_SECRET_HEADER]: "fixture-secret" })).status).toBe(400);
} finally {
for (const [key, value] of [["EDDY_EXPORT_ENABLED", env.enabled], ["CLUSTER_SECRET", env.secret]] as const)
if (value === undefined) delete process.env[key]; else process.env[key] = value;
}
});
});
test("a handoff's baseline id names the product whose recipe builds it", () => {
expect(productForBaseline("tiktokm-47.4.0-474000-5e813df6")).toBe("tiktok");
expect(productForBaseline("notes-15.1.0-151000-2e05a02f")).toBe("notes");
expect(productForBaseline("whee-1.0.0-1-abcdef01")).toBe("whee");
for (const b of ["lemon8-1.0", "tiktok-47.4.0", "", undefined, 42]) expect(productForBaseline(b)).toBeNull();
});