Files
SupplierSystem/server/tests/unitChangeSalePriceSourceOrder.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

111 lines
4.7 KiB
TypeScript

import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it, vi } from 'vitest';
import {
resolveSkuPriceForCustomer,
resolveSkuSalePriceForUnitChange,
} from '../src/services/orderPricingService';
/**
* 「供货单位变更后重取客户销售价」必须与录单预填同口径:**生效报价单销售价优先**。
*
* 背景(2026-10-07 实测):新报价单表与旧版本表并存,而完成自采/备货的自动调价、
* 完成回写、录单回写三类写入**只写新表**。若单位变更这条路径仍以旧版本表优先,
* 单位一变更就会拿旧价把刚调好的销售价冲回订单行(实测报价单 6.00、旧表 5.00 → 订单行被改回 5.00)。
* 见 docs/design/20261007-quote-sale-price-priority-and-writeback.md
*/
type QueryResult = { rows: Array<Record<string, unknown>>; rowCount: number };
function makeDb(handlers: {
sheet?: Array<Record<string, unknown>>;
legacyByUnit?: Array<Record<string, unknown>>;
legacyAnyUnit?: Array<Record<string, unknown>>;
discountRate?: unknown;
}) {
const sqls: string[] = [];
const db = {
query: vi.fn(async (sql: string) => {
sqls.push(String(sql));
const s = String(sql);
let rows: Array<Record<string, unknown>> = [];
if (s.includes('FROM customers')) rows = [{ branch_code: 'BR002' }];
else if (s.includes('FROM customer_quote_sheets')) rows = [{ quote_sheet_code: 'QS1' }];
else if (s.includes('FROM customer_quote_sheet_items')) rows = handlers.sheet ?? [];
else if (s.includes('FROM customer_quote_versions')) {
rows = s.includes('DISTINCT ON')
? handlers.legacyAnyUnit ?? []
: handlers.legacyByUnit ?? [];
} else if (s.includes('FROM customer_skus')) {
rows = handlers.discountRate === undefined ? [] : [{ discount_rate: handlers.discountRate }];
}
return { rows, rowCount: rows.length } as QueryResult;
}),
};
return { db, sqls };
}
describe('单位变更重取客户销售价:报价单优先', () => {
it('报价单有价时直接采用报价单价,不再查询旧版本表', async () => {
const { db, sqls } = makeDb({
sheet: [{ sku_code: 'SKU1', sale_price: 6.24 }],
legacyByUnit: [{ price: 5, unit: '斤' }],
legacyAnyUnit: [{ sku_code: 'SKU1', price: 5 }],
});
const price = await resolveSkuSalePriceForUnitChange('C1', 'SKU1', '斤', db as never);
expect(price).toBe(6.24);
// 关键:旧版本表一次都不能被读到,否则就会用旧价覆盖刚调好的价
expect(sqls.some((s) => s.includes('customer_quote_versions'))).toBe(false);
});
it('报价单无价时回退到旧版本表「单位精确」价(兜底仍在)', async () => {
const { db } = makeDb({
sheet: [],
legacyByUnit: [{ price: 5, unit: '斤' }],
});
const price = await resolveSkuSalePriceForUnitChange('C1', 'SKU1', '斤', db as never);
expect(price).toBe(5);
});
it('报价单与单位精确价都没有时,仍按「任意单位 × 客户折扣」兜底', async () => {
const anyUnitOnly = makeDb({
sheet: [],
legacyByUnit: [],
legacyAnyUnit: [{ sku_code: 'SKU1', price: 5 }],
discountRate: 90,
});
expect(await resolveSkuSalePriceForUnitChange('C1', 'SKU1', '斤', anyUnitOnly.db as never)).toBe(
4.5
);
});
it('两条路都以报价单为一号价源(源码顺序钉住,防再次漂移)', () => {
const src = readFileSync(
path.join(__dirname, '../src/services/orderPricingService.ts'),
'utf8'
);
const fnStart = src.indexOf('export async function resolveSkuSalePriceForUnitChange');
const fnEnd = src.indexOf('export async function enrichPrefillRowsWithPrices');
expect(fnStart).toBeGreaterThan(-1);
const body = src.slice(fnStart, fnEnd);
const sheetAt = body.indexOf('resolveActiveQuoteSheetSalePrices');
const byUnitAt = body.indexOf('resolveSkuPriceForCustomer');
const anyUnitAt = body.indexOf('resolveSkuPricesAnyUnitForCustomer');
expect(sheetAt).toBeGreaterThan(-1);
expect(sheetAt).toBeLessThan(byUnitAt);
expect(byUnitAt).toBeLessThan(anyUnitAt);
// 录单预填同样:报价单销售价先于历史成交价
const enrichStart = src.indexOf('export async function enrichOrderItemsWithPrices');
const enrichBody = src.slice(enrichStart);
expect(enrichBody.indexOf('if (sheetSale > 0)')).toBeLessThan(
enrichBody.indexOf('else if (lastSale != null')
);
});
it('单位精确取价本身仍保留(无报价单时给 fallback 用)', async () => {
const { db } = makeDb({ legacyByUnit: [{ price: 7, unit: '斤' }] });
expect(await resolveSkuPriceForCustomer('C1', 'SKU1', '斤', db as never)).toBe(7);
});
});