Files
SupplierSystem/server/tests/branchUiContract.test.ts
T
Ubuntu de14fb01f0
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat: 供应商系统 2026-11 阶段成果(分公司模板/真还原预览、商品远程搜索、主系统联调、加工幂等修复)
- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页)
- 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具
- 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页)
- 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发
- 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard)
- 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数
- 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
2026-11-07 20:10:48 +08:00

121 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 分公司类型模板:契约 / 棘轮测试(设计 v2 §8.1 / §8.3 / §14 后端契约)。
*
* 钉住四件事:
* ① `branchUiMenuKeys.ts` 白名单与前端 `MENU_CATALOG` 的 menuKey 集合一致(fs 读文件);
* ② 新路由文件 0 裸 SQL(routeLayerBoundary 棘轮),SQL 全在 service 且只用 `pool.query` + `public.`;
* ③ 写接口都在 `mountPlatformWorkflowRoutes` 挂载、且用 `requireSuperAdminRole`(reopen 用 requireAdminRole);
* ④ 4 类写 + 分公司重开都进了 `operationAuditMiddleware.isExactBusinessAuditedPath` 精确白名单。
*/
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { BRANCH_UI_MENU_KEYS } from '../src/services/branchUiMenuKeys';
import { isExactBusinessAuditedPath } from '../src/middleware/operationAuditMiddleware';
const SRC = path.join(__dirname, '..', 'src');
const REPO_ROOT = path.join(__dirname, '..', '..');
function readSrc(relativePath: string): string {
return readFileSync(path.join(SRC, relativePath), 'utf8');
}
describe('契约:branchUiMenuKeys 与前端 MENU_CATALOG 一致', () => {
it('键集合完全一致(新增/删除菜单项必须两边同步)', () => {
const catalogSource = readFileSync(path.join(REPO_ROOT, 'src/config/menuCatalog.ts'), 'utf8');
const catalogKeys = [...catalogSource.matchAll(/menuKey:\s*'([^']+)'/g)].map((match) => match[1]);
expect(catalogKeys.length).toBeGreaterThan(0);
expect([...BRANCH_UI_MENU_KEYS].sort()).toEqual([...catalogKeys].sort());
});
it('白名单无重复项', () => {
expect(new Set(BRANCH_UI_MENU_KEYS).size).toBe(BRANCH_UI_MENU_KEYS.length);
});
});
describe('棘轮:新路由 0 裸 SQL + service 只用 pool.query/public.', () => {
const SQL_RE = /\b(SELECT\s|INSERT\s+INTO\s|UPDATE\s+\w+\s+SET\s|DELETE\s+FROM\s)/g;
const routeSource = readSrc('modules/workflow/routes/branchUiRoutes.ts');
const serviceSource = readSrc('services/branchUiConfigService.ts');
it('路由文件内没有任何 SQL 语句(SQL 必须下沉 service)', () => {
const hits = routeSource.match(SQL_RE) || [];
expect(hits, `路由层出现裸 SQL:${hits.join(', ')}`).toEqual([]);
});
it('service 不用 getRawPoolQuery / getRawPoolConnect(routeLayerBoundary 白名单棘轮)', () => {
expect(serviceSource).not.toMatch(/getRawPoolQuery/);
expect(serviceSource).not.toMatch(/getRawPoolConnect/);
});
it('service 对 system_configs 的访问全部 public. 限定', () => {
// 禁止出现未限定的 FROM/INTO/UPDATE/DELETE system_configs
expect(serviceSource).not.toMatch(/\b(?:FROM|INTO|UPDATE|DELETE\s+FROM)\s+system_configs\b/);
expect(serviceSource.match(/public\.system_configs/g)?.length || 0).toBeGreaterThanOrEqual(6);
});
});
describe('路由注册与权限守卫', () => {
const routeSource = readSrc('modules/workflow/routes/branchUiRoutes.ts');
const businessModulesSource = readSrc('modules/businessModules.ts');
it('在 mountPlatformWorkflowRoutes 中挂载', () => {
expect(businessModulesSource).toContain(
"import { mountBranchUiWorkflowRoutes } from './workflow/routes/branchUiRoutes'"
);
expect(businessModulesSource).toContain('mountBranchUiWorkflowRoutes(router)');
});
it('7 个接口路径全部注册(§8.2,PUT/DELETE types 同前缀)', () => {
for (const routePath of [
'/platform/branch-ui/effective',
'/platform/branch-ui/admin-config',
'/platform/branch-ui/types',
'/platform/branch-ui/templates/:typeCode',
'/platform/branch-ui/branches/:branchCode',
'/platform/branch-ui/types/:typeCode',
'/platform/branch-ui/reopen',
]) {
expect(routeSource, `缺少路由 ${routePath}`).toContain(`'${routePath}'`);
}
});
it('写接口用 requireSuperAdminRole(超管 ctx helper),重开用 requireAdminRole', () => {
expect(routeSource).toContain('requireSuperAdminRole');
expect(routeSource).toContain('requireAdminRole');
});
});
describe('审计:写接口进入精确白名单(§8.5,避免通用审计重复 + 空 branchCode 扇出)', () => {
it('4 类超管写 + 分公司重开路径返回 true', () => {
expect(isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/types')).toBe(true);
expect(
isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/templates/retail')
).toBe(true);
expect(
isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/branches/SUP0001')
).toBe(true);
expect(
isExactBusinessAuditedPath('DELETE', '/api/workflow/platform/branch-ui/types/retail')
).toBe(true);
expect(isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/reopen')).toBe(true);
});
it('只精确匹配,不放大到同前缀其它路径', () => {
expect(isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/types-extra')).toBe(false);
expect(
isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/templates/retail/extra')
).toBe(false);
expect(isExactBusinessAuditedPath('POST', '/api/workflow/platform/branch-ui/types')).toBe(false);
expect(isExactBusinessAuditedPath('PUT', '/api/workflow/platform/branch-ui/unknown')).toBe(false);
});
it('路由内显式 writeAuditLog 且 branchCode 恒定(平台写=BR001 / 分公司写=目标分公司)', () => {
const routeSource = readSrc('modules/workflow/routes/branchUiRoutes.ts');
expect(routeSource).toContain('writeAuditLog');
expect(routeSource).toContain('PLATFORM_BRANCH_CODE');
expect(routeSource).toContain("operationType: BRANCH_UI_AUDIT_OPERATION_TYPE");
expect(routeSource).toContain("actionCategory: BRANCH_UI_AUDIT_ACTION_CATEGORY");
});
});