来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
127 lines
6.0 KiB
TypeScript
127 lines
6.0 KiB
TypeScript
/**
|
||
* 业务模块注册表一致性契约 —— **以前端 `src/modules/businessModules.ts` 为权威**。
|
||
*
|
||
* 起因(2026-09-15):注册表与前端/文档命名漂移 ——
|
||
* - inspection 后端叫「验收入库」
|
||
* - platform 后端叫「平台配置」
|
||
* - ops(运营)、dashboard(数据看板)只存在于前端
|
||
* - inventory 的 workflowApiPrefix 被填成 '/trace'(那是溯源路由)
|
||
* 用户裁定:命名**以前端为准**,故本测试直接读前端注册表比对,前端改名即会让后端测试变红。
|
||
*
|
||
* 注意两点:
|
||
* 1. 单查「声明的前缀是否真实存在」抓不到第 4 条 —— '/trace' 本身是真实前缀,
|
||
* 所以必须与模块绑定比对(见下方 MODULE_PREFIX)。
|
||
* 2. docs/modules 下的文件名是**描述性长名**(如 12-平台与系统.md),与前端短名(系统)
|
||
* 有意不同;这里只校验「编号对应的文档存在」,不强制同名。
|
||
*/
|
||
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import { ALL_BUSINESS_MODULES } from '../src/modules/businessModules';
|
||
import { getModuleManifest } from '../src/modules/registry';
|
||
|
||
const REPO_ROOT = path.join(__dirname, '../..');
|
||
const DOCS_DIR = path.join(REPO_ROOT, 'docs/modules');
|
||
const FRONTEND_MODULES_SRC = path.join(REPO_ROOT, 'src/modules/businessModules.ts');
|
||
const ROUTES_DIR = path.join(__dirname, '../src/modules/workflow/routes');
|
||
|
||
/** 顺序与 workflowApiPrefix 的约定(编号取自 docs/modules/NN-*.md;名称以前端为准) */
|
||
const MODULE_ORDER: Array<{ no: string; id: string; prefix?: string }> = [
|
||
{ no: '01', id: 'order', prefix: '/orders' },
|
||
{ no: '02', id: 'purchase', prefix: '/purchase' },
|
||
{ no: '03', id: 'inspection', prefix: '/inspection' },
|
||
{ no: '04', id: 'picking', prefix: '/picking' },
|
||
{ no: '05', id: 'delivery', prefix: '/delivery' },
|
||
{ no: '06', id: 'finance', prefix: '/finance' },
|
||
{ no: '07', id: 'product' },
|
||
{ no: '08', id: 'customer' },
|
||
{ no: '09', id: 'supplier' },
|
||
{ no: '10', id: 'inventory', prefix: '/inventory' },
|
||
{ no: '11', id: 'ops' },
|
||
{ no: '12', id: 'platform', prefix: '/system' },
|
||
{ no: '13', id: 'dashboard' },
|
||
];
|
||
|
||
/** 解析前端注册表的 id -> name(允许 id 与 name 之间夹注释) */
|
||
function frontendModuleNames(): Record<string, string> {
|
||
const src = readFileSync(FRONTEND_MODULES_SRC, 'utf8');
|
||
const out: Record<string, string> = {};
|
||
for (const part of src.split(/\bid:\s*'/).slice(1)) {
|
||
const id = part.slice(0, part.indexOf("'"));
|
||
if (!/^[a-z_]+$/.test(id)) continue;
|
||
const nm = /name:\s*'([^']+)'/.exec(part.slice(0, 400));
|
||
if (nm) out[id] = nm[1];
|
||
}
|
||
return out;
|
||
}
|
||
|
||
/** 把 workflow/routes/*.ts 里真实出现的一级路径段抽出来 */
|
||
function realRoutePrefixes(): Set<string> {
|
||
const found = new Set<string>();
|
||
for (const file of readdirSync(ROUTES_DIR)) {
|
||
if (!file.endsWith('.ts')) continue;
|
||
const src = readFileSync(path.join(ROUTES_DIR, file), 'utf8');
|
||
const re = /router\.(?:get|post|put|patch|delete)\(\s*['"`]\/([a-z0-9-]+)/g;
|
||
let m: RegExpExecArray | null;
|
||
while ((m = re.exec(src))) found.add(`/${m[1]}`);
|
||
}
|
||
return found;
|
||
}
|
||
|
||
describe('业务模块注册表一致性(以前端为准)', () => {
|
||
it('模块 id 与顺序与 docs/modules 编号一致', () => {
|
||
expect(ALL_BUSINESS_MODULES.map((m) => m.id)).toEqual(MODULE_ORDER.map((d) => d.id));
|
||
});
|
||
|
||
it('模块名与前端 src/modules/businessModules.ts 完全一致', () => {
|
||
const frontend = frontendModuleNames();
|
||
expect(Object.keys(frontend).length).toBeGreaterThan(0);
|
||
const mismatches = ALL_BUSINESS_MODULES.filter((m) => frontend[m.id] !== m.name).map(
|
||
(m) => `${m.id}: 后端「${m.name}」≠ 前端「${frontend[m.id] ?? '(前端无此模块)'}」`
|
||
);
|
||
expect(mismatches, `后端模块名须跟前端一致:\n${mismatches.join('\n')}`).toEqual([]);
|
||
});
|
||
|
||
it('前端与后端的模块 id 集合一致(无一方缺失)', () => {
|
||
const frontendIds = Object.keys(frontendModuleNames()).sort();
|
||
const backendIds = ALL_BUSINESS_MODULES.map((m) => m.id).sort();
|
||
expect(backendIds).toEqual(frontendIds);
|
||
});
|
||
|
||
it('docs/modules 下存在与编号对应的文档', () => {
|
||
const files = readdirSync(DOCS_DIR);
|
||
const missing = MODULE_ORDER.filter((d) => !files.some((f) => f.startsWith(`${d.no}-`))).map(
|
||
(d) => `${d.no}-*.md`
|
||
);
|
||
expect(missing, `缺少编号对应的文档:${missing.join(', ')}`).toEqual([]);
|
||
});
|
||
|
||
it('workflowApiPrefix 与约定一致(含 inventory 不得为 /trace)', () => {
|
||
const mismatches = ALL_BUSINESS_MODULES.map((m, i) => ({ m, d: MODULE_ORDER[i] }))
|
||
.filter(({ m, d }) => (m.workflowApiPrefix ?? undefined) !== (d.prefix ?? undefined))
|
||
.map(
|
||
({ m, d }) =>
|
||
`${d.id}: 注册表 '${m.workflowApiPrefix ?? '(未声明)'}' ≠ 约定 '${d.prefix ?? '(不应声明)'}'`
|
||
);
|
||
expect(mismatches, mismatches.join('\n')).toEqual([]);
|
||
});
|
||
|
||
it('声明的前缀必须是真实挂载到 /api/workflow 的前缀', () => {
|
||
const real = realRoutePrefixes();
|
||
const unknown = ALL_BUSINESS_MODULES.map((m) => m.workflowApiPrefix)
|
||
.filter((p): p is string => Boolean(p))
|
||
.filter((p) => !real.has(p));
|
||
expect(unknown, `这些前缀在 workflow/routes 下找不到对应路由:${unknown.join(', ')}`).toEqual([]);
|
||
});
|
||
|
||
it('GET /api/modules/manifest 的输出与前端一致(含新增的 ops / dashboard)', () => {
|
||
const frontend = frontendModuleNames();
|
||
const manifest = getModuleManifest();
|
||
expect(manifest.map((m) => m.id)).toEqual(MODULE_ORDER.map((d) => d.id));
|
||
expect(manifest.map((m) => m.name)).toEqual(MODULE_ORDER.map((d) => frontend[d.id]));
|
||
expect(manifest.find((m) => m.id === 'ops')?.name).toBe(frontend.ops);
|
||
expect(manifest.find((m) => m.id === 'dashboard')?.name).toBe(frontend.dashboard);
|
||
expect(manifest.find((m) => m.id === 'inventory')?.workflowApiPrefix).toBe('/inventory');
|
||
});
|
||
});
|