来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
221 lines
6.5 KiB
TypeScript
221 lines
6.5 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import {
|
||
DEFAULT_ALLOCATION_RULES,
|
||
resolveItemAllocation,
|
||
} from '../src/services/allocationRules';
|
||
|
||
describe('resolveItemAllocation sku_supplier preferred', () => {
|
||
const suppliers = [
|
||
{ supplier_code: 'SUP_A', company_name: '供应商A', rating: 'A', status: 10 },
|
||
{ supplier_code: 'SUP_B', company_name: '供应商B', rating: 'B', status: 10 },
|
||
];
|
||
|
||
const baseItem = {
|
||
id: 'item-1',
|
||
sku_code: 'SKU001',
|
||
sku_name: '土豆',
|
||
category_code: 'CAT_VEG',
|
||
category_name: '蔬菜',
|
||
quantity: 10,
|
||
price: 3,
|
||
unit: '斤',
|
||
};
|
||
|
||
it('falls back to preferred sku_suppliers when category has no binding', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: baseItem,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [],
|
||
suppliers,
|
||
skuSuppliers: [
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_B',
|
||
supply_price: 2.5,
|
||
is_preferred: false,
|
||
status: 1,
|
||
},
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_A',
|
||
supply_price: 2.2,
|
||
is_preferred: true,
|
||
status: 1,
|
||
},
|
||
],
|
||
});
|
||
|
||
expect(resolved).not.toBeNull();
|
||
expect(resolved?.target).toBe('supplier');
|
||
expect(resolved?.allocation_source).toBe('sku_supplier');
|
||
expect(resolved?.supplier_code).toBe('SUP_A');
|
||
expect(resolved?.supply_price).toBe(2.2);
|
||
expect(resolved?.reason).toContain('商品指定供应商');
|
||
});
|
||
|
||
it('goes to purchaser when category and sku_suppliers both miss', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: baseItem,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [],
|
||
suppliers,
|
||
skuSuppliers: [],
|
||
});
|
||
|
||
expect(resolved?.target).toBe('purchaser');
|
||
expect(resolved?.allocation_source).toBe('purchaser');
|
||
});
|
||
|
||
it('prefers sku_suppliers over category binding', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: baseItem,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [{ supplier_code: 'SUP_B', category_code: 'CAT_VEG', is_primary: true }],
|
||
suppliers,
|
||
skuSuppliers: [
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_A',
|
||
supply_price: 1,
|
||
is_preferred: true,
|
||
status: 1,
|
||
},
|
||
],
|
||
});
|
||
|
||
expect(resolved?.target).toBe('supplier');
|
||
expect(resolved?.allocation_source).toBe('sku_supplier');
|
||
expect(resolved?.supplier_code).toBe('SUP_A');
|
||
});
|
||
|
||
it('uses category binding when sku_suppliers is empty', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: baseItem,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [{ supplier_code: 'SUP_B', category_code: 'CAT_VEG', is_primary: true }],
|
||
suppliers,
|
||
skuSuppliers: [],
|
||
});
|
||
|
||
expect(resolved?.target).toBe('supplier');
|
||
expect(resolved?.allocation_source).toBe('category');
|
||
expect(resolved?.supplier_code).toBe('SUP_B');
|
||
});
|
||
|
||
it('uses preferred sku_suppliers when item has no category', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: { ...baseItem, category_code: '', category_name: '' },
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [{ supplier_code: 'SUP_B', category_code: 'CAT_VEG', is_primary: true }],
|
||
suppliers,
|
||
skuSuppliers: [
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_A',
|
||
supply_price: 2.2,
|
||
is_preferred: true,
|
||
status: 1,
|
||
},
|
||
],
|
||
});
|
||
expect(resolved?.allocation_source).toBe('sku_supplier');
|
||
expect(resolved?.supplier_code).toBe('SUP_A');
|
||
});
|
||
|
||
it('ignores unavailable sku_suppliers and falls to purchaser', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: baseItem,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [],
|
||
suppliers: [{ supplier_code: 'SUP_A', company_name: '供应商A', rating: 'D', status: 10 }],
|
||
skuSuppliers: [
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_A',
|
||
supply_price: 2,
|
||
is_preferred: true,
|
||
status: 1,
|
||
},
|
||
],
|
||
});
|
||
|
||
expect(resolved?.target).toBe('purchaser');
|
||
});
|
||
|
||
it('商品直送强制自采,忽略分类供应商', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item: { ...baseItem, direct_delivery: true },
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [{ supplier_code: 'SUP_B', category_code: 'CAT_VEG', is_primary: true }],
|
||
suppliers,
|
||
skuSuppliers: [
|
||
{
|
||
sku_code: 'SKU001',
|
||
supplier_code: 'SUP_A',
|
||
supply_price: 2.2,
|
||
is_preferred: true,
|
||
status: 1,
|
||
},
|
||
],
|
||
});
|
||
expect(resolved?.target).toBe('purchaser');
|
||
expect(resolved?.supplier_code).toBe('INTERNAL');
|
||
expect(resolved?.reason).toContain('商品直送');
|
||
});
|
||
});
|
||
|
||
describe('缺货率/评分纳入分配降权(fulfillmentBySupplier)', () => {
|
||
const item = {
|
||
id: 'item-1',
|
||
sku_code: 'SKU001',
|
||
sku_name: '土豆',
|
||
category_code: 'CAT_VEG',
|
||
category_name: '蔬菜',
|
||
quantity: 10,
|
||
price: 3,
|
||
unit: '斤',
|
||
};
|
||
|
||
it('同组候选中履约分低(缺货率高)的供应商被降权', () => {
|
||
const resolved = resolveItemAllocation({
|
||
orderCode: 'ORD1',
|
||
item,
|
||
rules: DEFAULT_ALLOCATION_RULES,
|
||
supplierCategories: [
|
||
{ supplier_code: 'SUP_A', category_code: 'CAT_VEG', is_primary: false },
|
||
{ supplier_code: 'SUP_B', category_code: 'CAT_VEG', is_primary: false },
|
||
],
|
||
suppliers: [
|
||
{ supplier_code: 'SUP_A', company_name: '供应商A', rating: 'A', status: 10 },
|
||
{ supplier_code: 'SUP_B', company_name: '供应商B', rating: 'B', status: 10 },
|
||
],
|
||
skuSuppliers: [],
|
||
fulfillmentBySupplier: {
|
||
SUP_A: {
|
||
supplier_code: 'SUP_A',
|
||
fulfillment_score: 40,
|
||
risk_level: 'high',
|
||
suggest_backup: true,
|
||
recommendation: '近30天缺货率偏高',
|
||
},
|
||
SUP_B: {
|
||
supplier_code: 'SUP_B',
|
||
fulfillment_score: 90,
|
||
risk_level: 'low',
|
||
suggest_backup: false,
|
||
},
|
||
},
|
||
});
|
||
|
||
// SUP_A 评级更高但履约分(含缺货率)低 → 应选择 SUP_B
|
||
expect(resolved?.supplier_code).toBe('SUP_B');
|
||
});
|
||
});
|