86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { ConflictException, NotFoundException } 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 actorId = "admin-1";
|
|
|
|
function createService(task: Record<string, unknown> | null): AdminMiniAppReleaseService {
|
|
const prisma = {
|
|
miniAppReleaseTask: {
|
|
findFirst: async () => task,
|
|
update: async ({ data }: { data: Record<string, unknown> }) => ({ ...task, ...data }),
|
|
},
|
|
$transaction: async (callback: (tx: unknown) => unknown) =>
|
|
callback({
|
|
miniAppReleaseTask: {
|
|
findFirst: async () => task,
|
|
update: async ({ data }: { data: Record<string, unknown> }) => ({ ...task, ...data }),
|
|
},
|
|
}),
|
|
} as unknown as PrismaService;
|
|
return new AdminMiniAppReleaseService(prisma);
|
|
}
|
|
|
|
function builtTask(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: "task-1",
|
|
brandId,
|
|
version: 3,
|
|
status: "built",
|
|
buildStatus: "idle",
|
|
error: null,
|
|
buildLogs: [],
|
|
audit: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test("构建进度更新不触发发布状态机的顺序校验", async () => {
|
|
const release = await createService(builtTask()).updateBuild(brandId, actorId, "task-1", {
|
|
buildLogs: [{ message: "开始构建", stream: "stdout" }],
|
|
buildStatus: "building",
|
|
});
|
|
assert.equal((release as { status: string }).status, "built");
|
|
assert.equal((release as { buildStatus: string }).buildStatus, "building");
|
|
});
|
|
|
|
test("构建进度更新按品牌隔离,任务不存在时抛出 404", async () => {
|
|
await assert.rejects(
|
|
() => createService(null).updateBuild(brandId, actorId, "task-1", { buildStatus: "building" }),
|
|
NotFoundException,
|
|
);
|
|
});
|
|
|
|
test("重新构建时可以清除上一次遗留的错误信息", async () => {
|
|
const release = await createService(
|
|
builtTask({ buildStatus: "failed", error: "小程序构建产物缺少 app.json" }),
|
|
).updateBuild(brandId, actorId, "task-1", { buildStatus: "building", error: null });
|
|
assert.equal((release as { error: string | null }).error, null);
|
|
assert.equal((release as { buildStatus: string }).buildStatus, "building");
|
|
});
|
|
|
|
test("发布状态机仍拒绝越级变更并允许合法上传流转", async () => {
|
|
await assert.rejects(
|
|
() =>
|
|
createService(builtTask()).updateStatus(brandId, actorId, "task-1", { status: "published" }),
|
|
ConflictException,
|
|
);
|
|
const uploaded = await createService(builtTask()).updateStatus(brandId, actorId, "task-1", {
|
|
status: "uploaded",
|
|
weappUploadId: "test-upload",
|
|
});
|
|
assert.equal((uploaded as { status: string }).status, "uploaded");
|
|
assert.equal((uploaded as { weappUploadId: string }).weappUploadId, "test-upload");
|
|
});
|
|
|
|
test("失败任务重试时可回到 built 并清除错误", async () => {
|
|
const release = await createService(
|
|
builtTask({ status: "failed", buildStatus: "failed", error: "上传失败" }),
|
|
).updateStatus(brandId, actorId, "task-1", { status: "built", buildStatus: "building" });
|
|
assert.equal((release as { status: string }).status, "built");
|
|
assert.equal((release as { error: string | null }).error, null);
|
|
});
|