feat: 收口小程序构建上传阶段
This commit is contained in:
1 parent
6420b85019
commit
ec16fff70e
23 files changed
+1185
-104
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user