来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
175 lines
8.3 KiB
TypeScript
175 lines
8.3 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
assertPositiveSupplyCostPrice,
|
||
assertSupplyCostPrice,
|
||
isPositiveSupplyCostPrice,
|
||
} from '../src/services/supplyCostPrice';
|
||
import {
|
||
ALLOW_ZERO_SUPPLY_COST_KEY,
|
||
} from '../src/services/zeroSupplyCostConfigService';
|
||
|
||
/**
|
||
* 「允许成本价为 0」开关(2026-09-23)——分公司级、**默认关闭**。
|
||
*
|
||
* 覆盖:新校验函数的语义(关闭=与今天一致 / 开启=接受 0 但仍拒负数与 NaN)、
|
||
* 配置服务与路由的接线棘轮、以及**每一个放行点都必须接开关**(防止有人只改一半)。
|
||
* 设计:docs/design/20260923-allow-zero-supply-cost.md
|
||
*/
|
||
|
||
const read = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
||
|
||
describe('assertSupplyCostPrice(开关两态)', () => {
|
||
it('默认(不开开关)与既有函数逐字一致:0 / 负数 / NaN 都拒,文案不变', () => {
|
||
for (const bad of [0, -1, -0.01, Number.NaN, '', null, undefined, 'abc']) {
|
||
expect(() => assertSupplyCostPrice(bad, '采购价')).toThrow(/采购价必须大于 0/);
|
||
}
|
||
// 与旧实现的报错文案一致(前端/接口文案不变)
|
||
try {
|
||
assertPositiveSupplyCostPrice(0, '采购价');
|
||
} catch (error) {
|
||
expect(() => assertSupplyCostPrice(0, '采购价')).toThrow((error as Error).message);
|
||
}
|
||
});
|
||
|
||
it('开关开启:接受 0,仍拒负数与 NaN', () => {
|
||
expect(assertSupplyCostPrice(0, '采购价', { allowZero: true })).toBe(0);
|
||
expect(assertSupplyCostPrice(1.25, '采购价', { allowZero: true })).toBe(1.25);
|
||
expect(() => assertSupplyCostPrice(-0.01, '采购价', { allowZero: true })).toThrow(/不能为负数/);
|
||
// 非法输入(空/非数字)与负数分别提示,避免"空值"被说成"负数"
|
||
expect(() => assertSupplyCostPrice(Number.NaN, '采购价', { allowZero: true })).toThrow(/采购价无效/);
|
||
expect(() => assertSupplyCostPrice('', '采购价', { allowZero: true })).toThrow(/采购价无效/);
|
||
expect(() => assertSupplyCostPrice(null, '采购价', { allowZero: true })).toThrow(/采购价无效/);
|
||
});
|
||
|
||
it('错误码与状态码保持 400 / invalid_request(前端按码提示)', () => {
|
||
try {
|
||
assertSupplyCostPrice(0, '采购价');
|
||
throw new Error('应当抛错');
|
||
} catch (error) {
|
||
expect((error as { status?: number }).status).toBe(400);
|
||
expect((error as { code?: string }).code).toBe('invalid_request');
|
||
}
|
||
});
|
||
|
||
it('旧函数不动(发票金额、费用金额等非成本场景继续用它)', () => {
|
||
expect(isPositiveSupplyCostPrice(0)).toBe(false);
|
||
expect(assertPositiveSupplyCostPrice(2, '金额')).toBe(2);
|
||
});
|
||
});
|
||
|
||
describe('配置服务与路由接线棘轮', () => {
|
||
const service = read('src/services/zeroSupplyCostConfigService.ts');
|
||
const routes = read('src/modules/workflow/routes/purchaseRoutes.ts');
|
||
|
||
it('key 与默认值:allow_zero_supply_cost,默认关闭', () => {
|
||
expect(ALLOW_ZERO_SUPPLY_COST_KEY).toBe('allow_zero_supply_cost');
|
||
expect(service).toContain('const DEFAULT_CONFIG: AllowZeroSupplyCostConfig = { enabled: false };');
|
||
});
|
||
|
||
it('读取顺序:分公司行 → 全局行 → 代码默认(与既有开关同构)', () => {
|
||
expect(service).toContain('WHERE config_key = $1 AND branch_code = $2');
|
||
expect(service).toContain('WHERE config_key = $1 AND branch_code IS NULL');
|
||
expect(service).toContain("source: 'branch'");
|
||
expect(service).toContain("source: 'global'");
|
||
expect(service).toContain("source: 'default'");
|
||
});
|
||
|
||
it('写入:先 UPDATE 再 INSERT,config_group=purchase,并写审计', () => {
|
||
expect(service).toContain('UPDATE system_configs');
|
||
expect(service).toContain('INSERT INTO system_configs');
|
||
expect(service).toContain("config_group, description, branch_code, updated_at, updated_by");
|
||
expect(service).toContain("'purchase', $3, $4, NOW(), $5");
|
||
expect(service).toContain('writeAuditLog');
|
||
expect(service).toContain("operationType: 'config_update'");
|
||
});
|
||
|
||
it('路由:GET 只读 + PUT 仅分公司管理员(requireAdminRole 不认超管)', () => {
|
||
expect(routes).toContain("router.get('/purchase/allow-zero-cost-config'");
|
||
expect(routes).toContain("router.put('/purchase/allow-zero-cost-config'");
|
||
const put = routes.slice(routes.indexOf("router.put('/purchase/allow-zero-cost-config'"));
|
||
expect(put).toContain("requireAdminRole(ctx, '仅分公司管理员可修改该开关')");
|
||
expect(put).toContain('if (typeof req.body?.enabled !== \'boolean\')');
|
||
expect(put).toContain('setAllowZeroSupplyCostConfig(branchCode, req.body.enabled, req.userId || null)');
|
||
});
|
||
|
||
it('requireAdminRole 只认 admin 角色(超管不含 admin → 改不了)', () => {
|
||
const context = read('src/modules/workflow/context.ts');
|
||
expect(context).toContain("if (ctx.roles.includes('admin')) return;");
|
||
});
|
||
});
|
||
|
||
describe('放行点必须接开关(缺一处即失败)', () => {
|
||
const sites: Array<[string, string[]]> = [
|
||
['src/services/purchaseOperationService.ts', ['isZeroSupplyCostAllowed', 'assertSupplyCostPrice']],
|
||
['src/services/purchaseAmendService.ts', ['isZeroSupplyCostAllowed', 'assertSupplyCostPrice']],
|
||
[
|
||
'src/services/supplierPreparationStatusService.ts',
|
||
['isZeroSupplyCostAllowed', 'assertSupplyCostPrice', 'resolvePreparationBranchCode'],
|
||
],
|
||
['src/services/prepAmendRequestService.ts', ['isZeroSupplyCostAllowed', 'assertSupplyCostPrice']],
|
||
['src/services/stockInboundService.ts', ['isZeroSupplyCostAllowed', '成本价须大于 0']],
|
||
['src/services/independentInboundService.ts', ['isZeroSupplyCostAllowed', '成本价须大于 0']],
|
||
['src/services/selfPurchaseStockInboundService.ts', ['isZeroSupplyCostAllowed']],
|
||
['src/services/inspectionComplianceService.ts', ['isZeroSupplyCostAllowed']],
|
||
['src/routes/mobile/mobileAdminRoutes.ts', ['allow_zero_cost: await isZeroSupplyCostAllowed(ctx.branchCode)']],
|
||
];
|
||
|
||
for (const [file, needles] of sites) {
|
||
it(`${file} 已接开关`, () => {
|
||
const src = read(file);
|
||
for (const needle of needles) expect(src).toContain(needle);
|
||
});
|
||
}
|
||
|
||
it('自采侧保留"仓内加工成本必须为 0"的反向规则(开关不能把它放开成随便填)', () => {
|
||
const ops = read('src/services/purchaseOperationService.ts');
|
||
expect(ops).toContain('const allowZeroCost = selfProcessed || warehouseDemand;');
|
||
const amend = read('src/services/purchaseAmendService.ts');
|
||
expect(amend).toContain('if (!row.self_processed && nextPrice === 0) {');
|
||
});
|
||
|
||
it('API 层不再重复校验价(避免两处口径漂移),由服务层权威分流', () => {
|
||
for (const file of [
|
||
'src/modules/workflow/routes/preparationRoutes.ts',
|
||
'src/routes/mobile/mobileAdminRoutes.ts',
|
||
'src/routes/supplier/supplierBusinessRoutes.ts',
|
||
]) {
|
||
const src = read(file);
|
||
expect(src).not.toContain("error_description: '采购价必须大于 0'");
|
||
expect(src).toContain('updatePreparationPurchasePrice');
|
||
}
|
||
});
|
||
|
||
it('供应商端/小程序端无分公司上下文时,服务层用备货行 branch_code 兜底', () => {
|
||
const prep = read('src/services/supplierPreparationStatusService.ts');
|
||
expect(prep).toContain('resolvePreparationBranchCode(ids[0])');
|
||
expect(prep).toContain("existingRows.rows[0]?.branch_code || null");
|
||
});
|
||
});
|
||
|
||
describe('独立入库迁移棘轮', () => {
|
||
const sql = read('../migrations/20260924_allow_zero_cost_independent_inbound.sql');
|
||
|
||
it('把 unit_cost > 0 放宽为 >= 0(按定义定位旧约束,幂等)', () => {
|
||
expect(sql).toContain("pg_get_constraintdef(con.oid) ILIKE '%unit_cost%'");
|
||
expect(sql).toContain('self_purchase_stock_inbounds_unit_cost_nonneg CHECK (unit_cost >= 0)');
|
||
expect(sql).toContain('DROP CONSTRAINT %I');
|
||
// 反向断言:本迁移只改约束,不加列、不加默认值
|
||
expect(sql).not.toContain('ADD COLUMN');
|
||
});
|
||
|
||
it('不碰数量约束、不写任何数据', () => {
|
||
expect(sql).not.toContain('qty > 0)');
|
||
for (const forbidden of ['UPDATE ', 'INSERT INTO', 'DELETE FROM']) {
|
||
expect(sql).not.toContain(forbidden);
|
||
}
|
||
});
|
||
|
||
it('带自检:放宽后不应再存在 unit_cost > 0 的约束', () => {
|
||
expect(sql).toContain('RAISE EXCEPTION');
|
||
expect(sql).toContain("unit_cost\\s*>\\s*\\(?0");
|
||
});
|
||
});
|