Files
persionalBrand/apps/api/test/miniapp-build-config.test.ts
que01 44fe7a0142 fix: 打通小程序代码上传链路
小程序上传此前连续失败,根因分散在状态机、产物路径、模块导入、私钥解析和进程隔离上,逐项修复:

- 构建进度改用 updateBuild 只写 buildStatus/buildLogs,不再用 updateStatus 记录进度,避免状态机拒绝自我迁移而返回 409
- 产物目录锚定仓库根目录,不再随 API 进程工作目录变成 apps/api/apps/taro/dist
- miniprogram-ci 为 CommonJS 包,动态导入后从 default 归一化取 Project/upload,替换原有的类型断言
- 上传私钥统一还原成合法 PEM;输入框曾把换行吞成单行,导致 OpenSSL 解码失败,现保存时校验、上传时自动修复存量数据
- 上传改在独立子进程执行,miniprogram-ci 崩溃或超时不再终止 API 进程,失败返回 502 并保留微信原始错误
- 构建超过 MINIAPP_BUILD_STALE_MS 无进度即视为中断,允许重新上传,避免任务永久卡在 building
- 构建前校验请求地址与 AppID,请求地址支持在品牌配置中维护,环境变量仅作回落,避免再打出指向 localhost 的包
- 小程序 /media/ 按请求地址的 origin 解析,不再依赖网关剥离 /api 前缀

新增对应单测与集成断言,并更新部署说明。
2026-10-13 21:58:48 +08:00

75 lines
2.7 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import {
miniappApiBaseUrl,
validateApiBaseUrl,
weappAppId,
} from "../src/modules/admin/miniapp-build-config.js";
test("后台配置的请求地址优先于环境变量,改域名不需要动服务端配置", () => {
const env = { MINIAPP_API_BASE_URL: "https://env.example.com/api" };
assert.equal(
miniappApiBaseUrl("https://stored.example.com/api/", env),
"https://stored.example.com/api",
);
assert.equal(miniappApiBaseUrl("", env), "https://env.example.com/api");
assert.equal(miniappApiBaseUrl(undefined, env), "https://env.example.com/api");
});
test("后台地址允许带路径前缀,保存前会去掉结尾斜杠", () => {
assert.equal(
validateApiBaseUrl("https://wechat.app.que01.top/api/"),
"https://wechat.app.que01.top/api",
);
assert.throws(() => validateApiBaseUrl("http://wechat.app.que01.top/api"), /https/);
});
test("优先使用 MINIAPP_API_BASE_URL,去掉结尾斜杠", () => {
assert.equal(
miniappApiBaseUrl(undefined, { MINIAPP_API_BASE_URL: "https://wechat.app.que01.top/api/" }),
"https://wechat.app.que01.top/api",
);
});
test("没有显式配置时由 PUBLIC_BASE_URL 加默认 /api 推导", () => {
assert.equal(
miniappApiBaseUrl(undefined, { PUBLIC_BASE_URL: "https://wechat.app.que01.top" }),
"https://wechat.app.que01.top/api",
);
assert.equal(
miniappApiBaseUrl(undefined, {
PUBLIC_BASE_URL: "https://wechat.app.que01.top/",
MINIAPP_API_PATH: "/v2",
}),
"https://wechat.app.que01.top/v2",
);
});
test("非 https 的公网地址必须被拒绝,避免再次打出 localhost 包", () => {
assert.throws(
() => miniappApiBaseUrl(undefined, { MINIAPP_API_BASE_URL: "http://wechat.app.que01.top/api" }),
/https/,
);
assert.throws(() => miniappApiBaseUrl(undefined, { MINIAPP_API_BASE_URL: "https://" }), /URL/);
assert.throws(() => miniappApiBaseUrl(undefined, {}), /小程序请求地址/);
});
test("本地调试地址允许 http,方便开发环境构建", () => {
assert.equal(
miniappApiBaseUrl(undefined, { MINIAPP_API_BASE_URL: "http://localhost:3001" }),
"http://localhost:3001",
);
assert.equal(
miniappApiBaseUrl(undefined, { MINIAPP_API_BASE_URL: "http://127.0.0.1:3001" }),
"http://127.0.0.1:3001",
);
});
test("AppID 取品牌配置优先,回落 WEAPP_APP_ID,并拒绝占位值", () => {
assert.equal(weappAppId("wxd75d946d6978d183", {}), "wxd75d946d6978d183");
assert.equal(weappAppId(null, { WEAPP_APP_ID: "wxfallback" }), "wxfallback");
assert.throws(() => weappAppId("undefined", {}), /AppID/);
assert.throws(() => weappAppId("touristappid", {}), /AppID/);
assert.throws(() => weappAppId("", {}), /AppID/);
});