Files
SupplierSystem/server/tests/miniprogramZeroCostDisplay.test.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

133 lines
6.0 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.
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* 「允许成本价为 0」的**小程序端契约**(2026-09-24)。
*
* 背景(实测):Web 端「已完成列改 0 不生效」修完之后,小程序端还有同族问题:
* ① `purchase-today-panel` 独立自采入库(SPI)与「全部完成」批量**硬编码** `<= 0` 拦截
* (批量那处更隐蔽:把整组**静默跳过**,只报"跳过 N(缺数量/单价)");
* ② `admin-purchase-panel`(小程序版采购管理)保存实采价同样硬拦 0,且**没读开关**;
* ③ 展示/预填用 `actual_price || purchase_price || suggested_cost_price` 把 0 当缺失 ——
* 实测用户那一行(`PUR17901252345361a7c691`,actual 0 / pp 0 / total 0、订单行 unit_cost=6)
* 预填显示 **6.00**,历史列表金额显示「—」。
*
* 服务端早已按开关放行 0(`selfPurchaseStockInboundService`、`purchaseOperationService`、
* `purchaseAmendService` 都查的是服务端自己的开关),所以这三处纯粹是前端拦路。
*
* ⚠️ 反面(勿动):`actual_price` 为 **NULL** 的行仍必须回退下单价 —— 本地实测 81 行
* (32 加菜凭证 + 49 待采购)都是 NULL,不是 0;把"未填"一并当 0 会把它们的金额抹成 0。
*/
const SRC = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
const purchaseToday = require('../../miniprogram/utils/purchaseToday.js') as {
pickCostPriceHint: (row: Record<string, unknown>) => number | '';
decorateHistoryRow: (row: Record<string, unknown>) => {
display_price: number;
total_price_text: string;
total_price: number | null;
};
};
describe('小程序采购映射:0 是合法成本价,NULL 才是未填', () => {
// 本地库 tenant_br004 实测行(用户改 0 的那张单)
const userRow = {
actual_qty: 5,
purchase_qty: 5,
actual_price: 0,
purchase_price: 0,
total_amount: 0,
suggested_price: 6,
};
it('实采价 0 不得回退成建议价(改前预填 6)', () => {
expect(purchaseToday.pickCostPriceHint(userRow)).toBe(0);
});
it('0 成本行的金额是 0.00,不是「—」', () => {
const row = purchaseToday.decorateHistoryRow(userRow);
expect(row.display_price).toBe(0);
expect(row.total_price_text).toBe('0.00');
});
it('未填(NULL)仍回退下单价/建议价 —— 加菜凭证与待采购行依赖这个回退', () => {
expect(purchaseToday.pickCostPriceHint({ actual_price: null, purchase_price: 12 })).toBe(12);
expect(
purchaseToday.pickCostPriceHint({ actual_price: null, purchase_price: null, suggested_price: 6 })
).toBe(6);
const extraDish = purchaseToday.decorateHistoryRow({
actual_qty: null,
purchase_qty: 5,
actual_price: null,
purchase_price: 12,
total_amount: null,
});
expect(extraDish.display_price).toBe(12);
expect(extraDish.total_price_text).toBe('60.00');
});
it('完全没价格的行仍显示「—」(不得把"没填价"说成 0 元)', () => {
const row = purchaseToday.decorateHistoryRow({
purchase_qty: 5,
actual_price: null,
purchase_price: null,
total_amount: null,
});
expect(row.total_price_text).toBe('—');
});
});
describe('小程序:开关接线(改前是硬编码拦截)', () => {
const todayPanel = SRC('../miniprogram/components/purchase-today-panel/index.js');
it('独立自采入库(SPI)按开关放行 0', () => {
expect(todayPanel).toContain(
'(this.data.allowZeroCost ? unitCost < 0 : unitCost <= 0)'
);
expect(todayPanel).not.toContain('if (!Number.isFinite(unitCost) || unitCost <= 0) {');
});
it('「全部完成」批量不得把 0 价组静默跳过(单行 + 两处批量共 3 处同口径)', () => {
const wired =
todayPanel.match(/this\.data\.allowZeroCost \? actualPrice < 0 : actualPrice <= 0/g)?.length ?? 0;
expect(wired, '单行 updateStatus 与两处批量完成都要接线').toBe(3);
expect(todayPanel).not.toContain('!Number.isFinite(actualPrice) || actualPrice <= 0) {');
});
const adminPanel = SRC('../miniprogram/components/admin-purchase-panel/index.js');
it('管理端采购面板保存实采价:按开关放行 0,NaN 仍拦', () => {
expect(adminPanel).toContain('(this.data.allowZeroCost ? !(price >= 0) : !(price > 0))');
expect(adminPanel).not.toContain('if (!(qty > 0) || !(price > 0)) {');
});
it('管理端采购面板:开关来自列表接口,默认 false(保守拦截)', () => {
expect(adminPanel).toContain('allowZeroCost: false,');
expect(adminPanel).toContain('allowZeroCost: result && result.allow_zero_cost === true,');
});
it('管理端采购面板:禁止 `actual_price || purchase_price` 把 0 当缺失', () => {
expect(adminPanel).not.toContain('Number(row.actual_price || row.purchase_price) || 0');
expect(adminPanel).not.toContain('money(row.actual_price || row.purchase_price)');
expect(adminPanel.match(/pickStoredPrice\(/g)?.length ?? 0).toBeGreaterThanOrEqual(3);
});
it('小程序统一用 pickStoredPrice(0 也算填了)', () => {
const src = SRC('../miniprogram/utils/purchaseToday.js');
expect(src).toContain('function pickStoredPrice(...values)');
expect(src).toContain("value !== undefined && value !== null && String(value).trim() !== ''");
// 旧的「> 0 才算填」判据不得回退
expect(src).not.toContain('if (Number.isFinite(actual) && actual > 0) return actual;');
});
it('服务端 /admin/purchase/orders 下发 allow_zero_cost(与兄弟接口同口径)', () => {
const src = SRC('src/routes/mobile/mobileAdminRoutes.ts');
const start = src.indexOf("router.get('/admin/purchase/orders'");
expect(start).toBeGreaterThan(-1);
const route = src.slice(start, src.indexOf("router.get('/admin/skus'", start));
expect(route).toContain('allow_zero_cost: await isZeroSupplyCostAllowed(ctx.branchCode),');
});
});