feat: 收口小程序构建上传阶段

This commit is contained in:
que01 committed 2026-10-17 18:01:38 +08:00
1 parent 6420b85019
commit ec16fff70e
23 files changed
+1185 -104

No files matched your search

@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { AdminMiniAppBuildService } from "../src/modules/admin/admin-miniapp-build.service.js";
function fakePnpm(script: string) {
const directory = mkdtempSync(join(tmpdir(), "miniapp-build-worker-"));
const executable = join(directory, "pnpm");
writeFileSync(executable, `#!${process.execPath}\n${script}\n`);
chmodSync(executable, 0o755);
return { directory, executable };
}
test("构建 worker 异常退出时返回明确非零结果", async () => {
const worker = fakePnpm("process.exit(23);");
const previousPath = process.env.PATH;
process.env.PATH = worker.directory;
try {
const result = await new AdminMiniAppBuildService().run(
"template-key",
{ apiBaseUrl: "https://api.example.com", appId: "wx-test", outputRoot: "dist/test" },
() => undefined,
);
assert.deepEqual(result, { exitCode: 23 });
} finally {
if (previousPath === undefined) delete process.env.PATH;
else process.env.PATH = previousPath;
rmSync(worker.directory, { recursive: true, force: true });
}
});
test("构建 worker watchdog 超时会终止子进程并返回 timedOut", async () => {
const worker = fakePnpm("setTimeout(() => {}, 30_000);");
const previousPath = process.env.PATH;
const previousTimeout = process.env.MINIAPP_BUILD_TIMEOUT_MS;
process.env.PATH = worker.directory;
process.env.MINIAPP_BUILD_TIMEOUT_MS = "30";
try {
const messages: string[] = [];
const result = await new AdminMiniAppBuildService().run(
"template-key",
{ apiBaseUrl: "https://api.example.com", appId: "wx-test", outputRoot: "dist/test" },
(message) => messages.push(message),
);
assert.deepEqual(result, { exitCode: 124, timedOut: true });
assert.ok(messages.some((message) => message.includes("终止构建进程")));
} finally {
if (previousPath === undefined) delete process.env.PATH;
else process.env.PATH = previousPath;
if (previousTimeout === undefined) delete process.env.MINIAPP_BUILD_TIMEOUT_MS;
else process.env.MINIAPP_BUILD_TIMEOUT_MS = previousTimeout;
rmSync(worker.directory, { recursive: true, force: true });
}
});
@@ -2,9 +2,10 @@ import assert from "node:assert/strict";
import { generateKeyPairSync } from "node:crypto";
import { test } from "node:test";
import { BadGatewayException, BadRequestException, ConflictException } from "@nestjs/common";
import { templatePackageRegistry } from "@personal-brand/config";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { join, resolve } from "node:path";
import { AdminMiniAppBuildService } from "../src/modules/admin/admin-miniapp-build.service.js";
import { AdminMiniAppUploadService } from "../src/modules/admin/admin-miniapp-upload.service.js";
import type { AdminMiniAppReleaseService } from "../src/modules/admin/admin-miniapp-release.service.js";
@@ -55,6 +56,7 @@ function createHarness(
: mkdtempSync(join(tmpdir(), "miniapp-upload-"));
if (options.artifact !== false) writeFileSync(join(artifactRoot, "app.json"), "{}");
const updates: Record<string, unknown>[] = [];
const buildConfigs: Array<{ apiBaseUrl: string; appId: string; outputRoot: string }> = [];
const releases = {
uploadContext: async () => ({
task: task(options.task),
@@ -63,6 +65,27 @@ function createHarness(
miniAppSettings: options.settings ?? validBrandSettings(),
},
}),
acquireAttempt: async (
_brandId: string,
_actorId: string,
_taskId: string,
phase: "build" | "upload",
) => ({
id: `${phase}-attempt`,
leaseToken: `${phase}-lease`,
attempt: 1,
leaseExpiresAt: new Date(Date.now() + 60_000),
}),
heartbeatAttempt: async () => ({}),
finishAttempt: async (
_brandId: string,
attemptId: string,
_leaseToken: string,
input: object,
) => {
updates.push({ kind: "attempt", attemptId, input });
return input;
},
updateBuild: async (_brandId: string, _actorId: string, _taskId: string, input: object) => {
updates.push({ kind: "build", input });
return input;
@@ -75,15 +98,16 @@ function createHarness(
const builds = {
run: async (
_templateKey: string,
_config: { apiBaseUrl: string; appId: string },
config: { apiBaseUrl: string; appId: string; outputRoot: string },
onLog: (message: string, stream: "stdout" | "stderr") => void,
) => {
buildConfigs.push(config);
onLog("build log", "stdout");
return { exitCode: options.exitCode ?? 0 };
},
} as unknown as AdminMiniAppBuildService;
const service = new AdminMiniAppUploadService(releases, builds);
return { service, updates, artifactRoot };
return { service, updates, artifactRoot, buildConfigs };
}
function createHarnessWithCredential(
@@ -99,6 +123,7 @@ function createHarnessWithCredential(
: mkdtempSync(join(tmpdir(), "miniapp-upload-"));
if (options.artifact !== false) writeFileSync(join(artifactRoot, "app.json"), "{}");
const updates: Record<string, unknown>[] = [];
const buildConfigs: Array<{ apiBaseUrl: string; appId: string; outputRoot: string }> = [];
const releases = {
uploadContext: async () => ({
task: task(options.task),
@@ -110,6 +135,27 @@ function createHarnessWithCredential(
},
},
}),
acquireAttempt: async (
_brandId: string,
_actorId: string,
_taskId: string,
phase: "build" | "upload",
) => ({
id: `${phase}-attempt`,
leaseToken: `${phase}-lease`,
attempt: 1,
leaseExpiresAt: new Date(Date.now() + 60_000),
}),
heartbeatAttempt: async () => ({}),
finishAttempt: async (
_brandId: string,
attemptId: string,
_leaseToken: string,
input: object,
) => {
updates.push({ kind: "attempt", attemptId, input });
return input;
},
updateBuild: async (_brandId: string, _actorId: string, _taskId: string, input: object) => {
updates.push({ kind: "build", input });
return input;
@@ -122,14 +168,20 @@ function createHarnessWithCredential(
const builds = {
run: async (
_templateKey: string,
_config: { apiBaseUrl: string; appId: string },
config: { apiBaseUrl: string; appId: string; outputRoot: string },
onLog: (message: string, stream: "stdout" | "stderr") => void,
) => {
buildConfigs.push(config);
onLog("build log", "stdout");
return { exitCode: options.exitCode ?? 0 };
},
} as unknown as AdminMiniAppBuildService;
return { service: new AdminMiniAppUploadService(releases, builds), updates, artifactRoot };
return {
service: new AdminMiniAppUploadService(releases, builds),
updates,
artifactRoot,
buildConfigs,
};
}
test("upload blocks a currently building task before credentials are read", async () => {
@@ -159,17 +211,23 @@ test("failed build records logs and build failure state", async () => {
() => service.run(brandId, actorId, "task-1", {}),
(error: unknown) => {
assert.ok(error instanceof BadRequestException);
const logs = (updates.at(-1)?.input as { buildLogs: Array<{ message: string }> }).buildLogs;
const failedAttempt = updates.find(
(entry) => entry.kind === "attempt" && entry.attemptId === "build-attempt",
);
const logs = (failedAttempt?.input as { logs: Array<{ message: string }> }).logs;
assert.ok(logs.some((entry) => entry.message.includes("build log")));
assert.ok(logs.some((entry) => entry.message.includes("退出码 2")));
return true;
},
);
assert.equal((updates.at(-1)?.input as { buildStatus: string }).buildStatus, "failed");
assert.equal(
(updates.find((entry) => entry.kind === "attempt")?.input as { status: string }).status,
"failed",
);
});
test("missing artifact records build failure without invoking upload", async () => {
const { service, updates } = createHarnessWithCredential({ artifact: false });
const { service, updates, buildConfigs } = createHarnessWithCredential({ artifact: false });
const previousProjectPath = process.env.MINIAPP_ARTIFACT_ROOT;
process.env.MINIAPP_ARTIFACT_ROOT = join(tmpdir(), "missing-miniapp-artifacts");
try {
@@ -181,7 +239,21 @@ test("missing artifact records build failure without invoking upload", async ()
return true;
},
);
assert.equal((updates.at(-1)?.input as { buildStatus: string }).buildStatus, "failed");
assert.equal(
(updates.find((entry) => entry.kind === "attempt")?.input as { status: string }).status,
"failed",
);
assert.equal(
resolve(process.cwd(), "../taro", buildConfigs[0].outputRoot),
resolve(
process.env.MINIAPP_ARTIFACT_ROOT,
"releases",
brandId,
`acheng-campus-food@${templatePackageRegistry.find((item) => item.supportedIndustryTemplates.includes("food"))?.version}`,
"build-attempt",
"weapp",
),
);
} finally {
if (previousProjectPath === undefined) delete process.env.MINIAPP_ARTIFACT_ROOT;
else process.env.MINIAPP_ARTIFACT_ROOT = previousProjectPath;
+73
View File
@@ -10,6 +10,7 @@ import { getVisualTemplateManifest } from "@personal-brand/config";
import { createApp } from "../dist/main.js";
import { readIdentity, signIdentity } from "../dist/auth.js";
import { WechatAuthService } from "../dist/wechat-auth.js";
import { AdminMiniAppReleaseService } from "../dist/modules/admin/admin-miniapp-release.service.js";
let app;
const prisma = new PrismaClient();
@@ -571,6 +572,21 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
});
assert.equal(created.statusCode, 201);
assert.equal(JSON.parse(created.body).version, current.version);
await prisma.miniAppReleaseAttempt.create({
data: {
taskId: JSON.parse(created.body).id,
brandId,
actorId: "integration-test",
phase: "build",
attempt: 1,
leaseToken: "integration-secret-lease-token",
leaseExpiresAt: new Date(Date.now() + 60_000),
},
});
const listedAttempts = await request("/admin/miniapp-releases", { headers });
assert.equal(listedAttempts.statusCode, 200);
assert.equal(listedAttempts.body.includes("integration-secret-lease-token"), false);
assert.equal(JSON.parse(listedAttempts.body)[0].attempts[0].phase, "build");
const repeated = await request("/admin/miniapp-releases", {
method: "POST",
headers,
@@ -591,18 +607,21 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
});
assert.equal(uploaded.statusCode, 200);
assert.equal(JSON.parse(uploaded.body).status, "uploaded");
assert.equal(JSON.parse(uploaded.body).uploadStatus, "uploaded");
const underReview = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
payload: { status: "under_review" },
});
assert.equal(JSON.parse(underReview.body).status, "under_review");
assert.equal(JSON.parse(underReview.body).reviewStatus, "under_review");
const approved = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
payload: { status: "approved" },
});
assert.equal(JSON.parse(approved.body).status, "approved");
assert.equal(JSON.parse(approved.body).reviewStatus, "approved");
const published = await request(`/admin/miniapp-releases/${taskId}/status`, {
method: "PATCH",
headers,
@@ -628,6 +647,60 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
await prisma.miniAppReleaseTask.delete({ where: { id: buildingTask.id } });
});
test("API restart recovers an expired persisted miniapp build attempt", async () => {
const task = await prisma.miniAppReleaseTask.create({
data: {
brandId,
version: 900002,
actorId: "integration-restart-test",
status: "built",
buildStatus: "idle",
},
});
try {
const runningApi = app.get(AdminMiniAppReleaseService);
const first = await runningApi.acquireAttempt(
brandId,
"integration-restart-test",
task.id,
"build",
);
assert.equal(
(await prisma.miniAppReleaseAttempt.findUniqueOrThrow({ where: { id: first.id } })).status,
"running",
);
await app.close();
await prisma.miniAppReleaseAttempt.update({
where: { id: first.id },
data: { leaseExpiresAt: new Date(Date.now() - 1_000) },
});
app = await createApp();
await app.init();
const restartedApi = app.get(AdminMiniAppReleaseService);
const recovered = await restartedApi.acquireAttempt(
brandId,
"integration-restart-test",
task.id,
"build",
);
const attempts = await prisma.miniAppReleaseAttempt.findMany({
where: { taskId: task.id, phase: "build" },
orderBy: { attempt: "asc" },
});
assert.equal(recovered.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(attempts[1].status, "running");
assert.equal(
(await prisma.miniAppReleaseTask.findUniqueOrThrow({ where: { id: task.id } })).buildStatus,
"building",
);
} finally {
await prisma.miniAppReleaseTask.delete({ where: { id: task.id } });
}
});
test("miniapp release settings never expose or persist plaintext private keys", async () => {
const login = await request("/admin/session/login", {
method: "POST",
+21
View File
@@ -5,6 +5,8 @@ import { test } from "node:test";
import {
miniappArtifactRoot,
miniappProjectPath,
miniappReleaseProjectPath,
miniappReleaseOutputRoot,
REPOSITORY_ROOT,
} from "../src/modules/admin/miniapp-artifact.js";
@@ -40,3 +42,22 @@ test("显式配置的产物根目录按仓库根目录解析相对路径", () =>
);
assert.equal(miniappArtifactRoot("/srv/miniapp"), resolve("/srv/miniapp"));
});
test("发布产物路径隔离品牌、模板版本和 attempt", () => {
const root = miniappReleaseOutputRoot("brand-a", "linjing-editorial", "1.0.4", "attempt-1");
assert.equal(
root,
resolve(REPOSITORY_ROOT, "apps/taro/dist/releases/brand-a/linjing-editorial@1.0.4/attempt-1"),
);
assert.equal(
miniappReleaseProjectPath("brand-a", "linjing-editorial", "1.0.4", "attempt-1"),
`${root}/weapp`,
);
});
test("发布产物路径拒绝可能越界的品牌或模板字段", () => {
assert.throws(
() => miniappReleaseOutputRoot("../brand", "food", "1.0.0", "attempt-1"),
/Invalid brand slug/,
);
});
+14 -4
View File
@@ -1,6 +1,10 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { buildingStaleMs, isStaleBuilding } from "../src/modules/admin/miniapp-build-status.js";
import {
buildingStaleMs,
isStaleBuilding,
miniappBuildTimeoutMs,
} from "../src/modules/admin/miniapp-build-status.js";
const now = Date.UTC(2026, 9, 13, 12, 0, 0);
@@ -10,7 +14,8 @@ function task(buildStatus: string, minutesAgo: number) {
test("构建中的任务在超过阈值未更新时视为已中断", () => {
assert.equal(isStaleBuilding(task("building", 1), now), false);
assert.equal(isStaleBuilding(task("building", 16), now), true);
assert.equal(isStaleBuilding(task("building", 16), now), false);
assert.equal(isStaleBuilding(task("building", 31), now), true);
assert.equal(isStaleBuilding(task("building", 24 * 60), now), true);
});
@@ -24,9 +29,14 @@ test("时间戳异常时按未中断处理,宁可挡住重试也不允许并
assert.equal(isStaleBuilding({ buildStatus: "building", updatedAt: "not-a-date" }, now), false);
});
test("阈值可通过环境变量覆盖,非法值回退到默认 15 分钟", () => {
test("阈值可通过环境变量覆盖,非法值回退到默认 30 分钟", () => {
assert.equal(buildingStaleMs("60000"), 60_000);
for (const invalid of [undefined, "0", "-1", "abc"]) {
assert.equal(buildingStaleMs(invalid), 15 * 60 * 1000);
assert.equal(buildingStaleMs(invalid), 30 * 60 * 1000);
}
});
test("构建超时阈值支持环境变量并有独立默认值", () => {
assert.equal(miniappBuildTimeoutMs("60000"), 60_000);
assert.equal(miniappBuildTimeoutMs("0"), 20 * 60 * 1000);
});
@@ -0,0 +1,239 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { ConflictException } from "@nestjs/common";
import { AdminMiniAppReleaseService } from "../src/modules/admin/admin-miniapp-release.service.js";
import type { PrismaService } from "../src/prisma.service.js";
const brandId = "brand-1";
const taskId = "task-1";
const actorId = "admin-1";
function createHarness(initialAttempts: Array<Record<string, unknown>> = []) {
const task = {
id: taskId,
brandId,
status: "built",
buildStatus: "idle",
uploadStatus: "idle",
reviewStatus: "not_submitted",
};
const attempts = [...initialAttempts];
const findAttempt = (where: Record<string, unknown>) =>
attempts
.filter((item) => Object.entries(where).every(([key, value]) => item[key] === value))
.sort((left, right) => Number(right.attempt) - Number(left.attempt))[0] ?? null;
let sequence = attempts.length + 1;
const tx = {
miniAppReleaseTask: {
findFirst: async () => task,
update: async ({ data }: { data: Record<string, unknown> }) => {
Object.assign(task, data);
return task;
},
},
miniAppReleaseAttempt: {
findFirst: async ({ where }: { where: Record<string, unknown> }) => findAttempt(where),
create: async ({ data }: { data: Record<string, unknown> }) => {
const created = { id: `attempt-${sequence++}`, status: "running", ...data };
attempts.push(created);
return created;
},
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => {
const item = attempts.find((candidate) => candidate.id === where.id);
if (!item) throw new Error("attempt missing");
Object.assign(item, data);
return item;
},
updateMany: async ({
where,
data,
}: {
where: {
id: string;
status: string;
leaseToken: string;
leaseExpiresAt?: { gt: Date };
};
data: Record<string, unknown>;
}) => {
const item = attempts.find(
(candidate) =>
candidate.id === where.id &&
candidate.status === where.status &&
candidate.leaseToken === where.leaseToken &&
(!where.leaseExpiresAt || (candidate.leaseExpiresAt as Date) > where.leaseExpiresAt.gt),
);
if (!item) return { count: 0 };
Object.assign(item, data);
return { count: 1 };
},
},
};
const prisma = {
$transaction: async (callback: (value: typeof tx) => unknown) => callback(tx),
miniAppReleaseAttempt: tx.miniAppReleaseAttempt,
miniAppReleaseTask: tx.miniAppReleaseTask,
} as unknown as PrismaService;
return { service: new AdminMiniAppReleaseService(prisma), attempts, task };
}
test("同一任务同一阶段的有效 lease 拒绝并发操作", async () => {
const { service } = createHarness();
const first = await service.acquireAttempt(brandId, actorId, taskId, "build");
assert.equal(first.attempt, 1);
await assert.rejects(
() => service.acquireAttempt(brandId, actorId, taskId, "build"),
ConflictException,
);
});
test("过期 lease 会先收敛为 timed_out,再允许新的 attempt 接管", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { service, attempts } = createHarness([
{
id: "expired-attempt",
taskId,
brandId,
phase: "upload",
attempt: 1,
status: "running",
leaseToken: "expired-token",
leaseExpiresAt: expiredAt,
},
]);
const next = await service.acquireAttempt(brandId, actorId, taskId, "upload");
assert.equal(next.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(attempts[0].finishedAt instanceof Date, true);
});
test("服务进程重建后从持久化记录恢复,并接管已过期的 build attempt", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { attempts } = createHarness([
{
id: "worker-before-restart",
taskId,
brandId,
phase: "build",
attempt: 1,
status: "running",
leaseToken: "old-worker-token",
leaseExpiresAt: expiredAt,
},
]);
const persistentTask = {
id: taskId,
brandId,
status: "built",
buildStatus: "building",
uploadStatus: "idle",
reviewStatus: "not_submitted",
};
const persistentTx = {
miniAppReleaseTask: {
findFirst: async () => persistentTask,
update: async ({ data }: { data: Record<string, unknown> }) => {
Object.assign(persistentTask, data);
return persistentTask;
},
},
miniAppReleaseAttempt: {
findFirst: async ({ where }: { where: Record<string, unknown> }) =>
attempts
.filter((item) => Object.entries(where).every(([key, value]) => item[key] === value))
.sort((left, right) => Number(right.attempt) - Number(left.attempt))[0] ?? null,
create: async ({ data }: { data: Record<string, unknown> }) => {
const created = {
id: "worker-after-restart",
status: "running",
...data,
};
attempts.push(created);
return created;
},
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => {
const item = attempts.find((candidate) => candidate.id === where.id);
if (!item) throw new Error("attempt missing");
Object.assign(item, data);
return item;
},
},
};
const prisma = {
$transaction: async (callback: (value: typeof persistentTx) => unknown) =>
callback(persistentTx),
...persistentTx,
} as unknown as PrismaService;
const restartedService = new AdminMiniAppReleaseService(prisma);
const recovered = await restartedService.acquireAttempt(brandId, actorId, taskId, "build");
assert.equal(recovered.attempt, 2);
assert.equal(attempts[0].status, "timed_out");
assert.equal(persistentTask.buildStatus, "building");
});
test("heartbeat 遇到过期上传 lease 时将任务阶段收敛为 timed_out", async () => {
const expiredAt = new Date(Date.now() - 1_000);
const { service, attempts, task } = createHarness([
{
id: "expired-attempt",
taskId,
brandId,
phase: "upload",
attempt: 1,
status: "running",
leaseToken: "expired-token",
leaseExpiresAt: expiredAt,
},
]);
task.uploadStatus = "uploading";
await assert.rejects(
() => service.heartbeatAttempt(brandId, "expired-attempt", "expired-token", {}),
ConflictException,
);
assert.equal(attempts[0].status, "timed_out");
assert.equal(task.uploadStatus, "timed_out");
});
test("错误 lease 不能结束操作,正确 lease 结束上传后更新阶段状态", async () => {
const { service, task, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "upload");
await assert.rejects(
() => service.finishAttempt(brandId, attempt.id, "wrong-token", { status: "succeeded" }),
ConflictException,
);
await service.finishAttempt(brandId, attempt.id, attempt.leaseToken, {
status: "succeeded",
externalId: "wechat-upload-1",
});
assert.equal(attempts[0].status, "succeeded");
assert.equal(task.uploadStatus, "uploaded");
assert.equal(task.status, "uploaded");
});
test("heartbeat 续租并更新日志,结束后的 attempt 拒绝迟到 heartbeat", async () => {
const { service, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "build");
await service.heartbeatAttempt(brandId, attempt.id, attempt.leaseToken, {
logs: [{ message: "compile" }],
});
assert.deepEqual(attempts[0].logs, [{ message: "compile" }]);
await service.finishAttempt(brandId, attempt.id, attempt.leaseToken, { status: "succeeded" });
await assert.rejects(
() => service.heartbeatAttempt(brandId, attempt.id, attempt.leaseToken, { logs: [] }),
ConflictException,
);
assert.equal(attempts[0].status, "succeeded");
});
test("执行结束时 lease 已过期会持久化 timed_out,而不是回滚为 running", async () => {
const { service, attempts } = createHarness();
const attempt = await service.acquireAttempt(brandId, actorId, taskId, "upload");
attempts[0].leaseExpiresAt = new Date(Date.now() - 1);
await assert.rejects(
() => service.finishAttempt(brandId, attempt.id, attempt.leaseToken, { status: "succeeded" }),
ConflictException,
);
assert.equal(attempts[0].status, "timed_out");
});