来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
76 lines
2.7 KiB
TypeScript
76 lines
2.7 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { resolve } from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
computeFinanceNetProfit,
|
|
NET_PROFIT_FORMULA_ID,
|
|
NET_PROFIT_FORMULA_LABEL,
|
|
PERIOD_FIXED_COST_SQL_PREDICATE,
|
|
} from '../src/services/financeNetProfitFormula';
|
|
|
|
/**
|
|
* 锁定净利润公式:改口径须同步改 financeNetProfitFormula.ts + 本文件断言。
|
|
* 若只在 marginQueryService 内联改算术/期间固定条件,本套件应失败。
|
|
*/
|
|
describe('financeNetProfitFormula (locked)', () => {
|
|
it('keeps formula id and human label frozen', () => {
|
|
expect(NET_PROFIT_FORMULA_ID).toBe('finance_overview_net_profit_v20260810');
|
|
expect(NET_PROFIT_FORMULA_LABEL).toBe('销售额 − 采购成本 − 期间固定 − 期间其他');
|
|
});
|
|
|
|
it('computes net profit only as revenue - product - fixed - free', () => {
|
|
const r = computeFinanceNetProfit({
|
|
revenue: 1000,
|
|
productCost: 600,
|
|
periodFixedCost: 100,
|
|
periodFreeCost: 50,
|
|
});
|
|
expect(r.formulaId).toBe(NET_PROFIT_FORMULA_ID);
|
|
expect(r.grossProfit).toBe(400);
|
|
expect(r.netProfit).toBe(250);
|
|
expect(r.netProfitMarginPct).toBe(25);
|
|
});
|
|
|
|
it('does not treat week_cost / receivable as inputs (arity lock)', () => {
|
|
const fn = computeFinanceNetProfit as unknown as (input: Record<string, number>) => {
|
|
netProfit: number;
|
|
};
|
|
// 即使多传无关字段,结果仍只看四项
|
|
const r = fn({
|
|
revenue: 200,
|
|
productCost: 80,
|
|
periodFixedCost: 20,
|
|
periodFreeCost: 10,
|
|
week_cost: 999,
|
|
receivable_total: 999,
|
|
period_all_cost: 999,
|
|
});
|
|
expect(r.netProfit).toBe(90);
|
|
});
|
|
|
|
it('period-fixed SQL includes fixed_cost rows even when order_code is set', () => {
|
|
const sql = PERIOD_FIXED_COST_SQL_PREDICATE;
|
|
expect(sql).toContain("cd.reference_type = 'fixed_cost'");
|
|
expect(sql).toContain("cd.cost_type IS DISTINCT FROM 'free_overhead'");
|
|
// 挂单分摊必须计入:不能只靠 order_code 为空
|
|
expect(sql).not.toMatch(
|
|
/^\s*\(\s*cd\.order_code IS NULL[\s\S]*reference_type = 'fixed_cost'/
|
|
);
|
|
});
|
|
|
|
it('marginQueryService must call locked formula (source contract)', () => {
|
|
const src = readFileSync(
|
|
resolve(__dirname, '../src/services/marginQueryService.ts'),
|
|
'utf8'
|
|
);
|
|
expect(src).toContain("from './financeNetProfitFormula'");
|
|
expect(src).toContain('computeFinanceNetProfit');
|
|
expect(src).toContain('PERIOD_FIXED_COST_SQL_PREDICATE');
|
|
// 禁止在概览服务内联净利润四则运算主路径
|
|
expect(src).not.toMatch(
|
|
/weekRevenue\s*-\s*weekProductCost\s*-\s*periodFixedCost\s*-\s*freeCostTotal/
|
|
);
|
|
expect(src).not.toContain("cd.reference_type = 'fixed_cost'\n OR (");
|
|
});
|
|
});
|