import { describe, expect, it } from 'vitest'; import type { CategoryRow } from '../src/services/categoryCatalogService'; import { categoryInExpandedSet, resolveCategoryInviteSuppliers, } from '../src/services/bidRoundService'; function cat( category_code: string, parent_code: string | null, level: number, category_name = category_code ): CategoryRow { return { category_code, parent_code, category_name, level, sort_order: 0, loss_threshold: null, is_fresh: false, merge_same_day_fulfillment: false, status: 1, }; } describe('bid invite category match', () => { const rows: CategoryRow[] = [ cat('BR001_VEG', null, 1, '蔬菜'), cat('BR001_LEAF', 'BR001_VEG', 2, '叶菜'), cat('BR001_GANZA', null, 1, '干杂'), cat('BR001_EGG', null, 1, '蛋类'), ]; it('invites supplier bound to parent when item is leaf', () => { const invited = resolveCategoryInviteSuppliers( ['BR001_LEAF'], rows, [ { supplier_code: 'S_PARENT', category_code: 'BR001_VEG' }, { supplier_code: 'S_OTHER', category_code: 'BR001_MEAT' }, ], 'BR001' ); expect(invited).toContain('S_PARENT'); expect(invited).not.toContain('S_OTHER'); }); it('invites exact leaf binding and bare/prefix variants', () => { const invited = resolveCategoryInviteSuppliers( ['LEAF'], rows, [{ supplier_code: 'S_LEAF', category_code: 'BR001_LEAF' }], 'BR001' ); expect(invited).toContain('S_LEAF'); }); it('categoryInExpandedSet matches branch prefix variants', () => { const expanded = new Set(['BR001_VEG', 'BR001_LEAF']); expect(categoryInExpandedSet('LEAF', expanded, 'BR001')).toBe(true); expect(categoryInExpandedSet('BR001_LEAF', expanded, 'BR001')).toBe(true); expect(categoryInExpandedSet('MEAT', expanded, 'BR001')).toBe(false); }); it('does not invite egg supplier after rebind away from 干杂', () => { const invited = resolveCategoryInviteSuppliers( ['BR001_GANZA'], rows, [ { supplier_code: 'S_EGG', category_code: 'BR001_EGG' }, { supplier_code: 'S_GANZA', category_code: 'BR001_GANZA' }, ], 'BR001' ); expect(invited).toContain('S_GANZA'); expect(invited).not.toContain('S_EGG'); }); it('category-only invite set is driven by live supplier_categories bindings', () => { // 模拟:sku_suppliers 仍挂着蛋商,但经营分类已改绑蛋类 —— 分类邀请不应包含蛋商 const liveBindings = [{ supplier_code: 'S_EGG', category_code: 'BR001_EGG' }]; const invited = resolveCategoryInviteSuppliers(['GANZA'], rows, liveBindings, 'BR001'); expect(invited).not.toContain('S_EGG'); expect(invited).toEqual([]); }); it('经营品类闸门:单供应商绑定下,跨分类商品不可报价', () => { // 报价侧闸门(resolveSupplierQuotableSkus)就是拿该供应商自己的绑定跑这个函数: // 只报自己品类内的商品,跨分类的返回空集 → 拒绝报价。 const ganzaBindings = [{ supplier_code: 'S_GANZA', category_code: 'BR001_GANZA' }]; expect(resolveCategoryInviteSuppliers(['BR001_EGG'], rows, ganzaBindings, 'BR001')).toEqual([]); expect(resolveCategoryInviteSuppliers(['BR001_GANZA'], rows, ganzaBindings, 'BR001')).toContain( 'S_GANZA' ); }); it('经营品类闸门:绑定父类时子类商品仍可报价', () => { const parentBindings = [{ supplier_code: 'S_PARENT', category_code: 'BR001_VEG' }]; expect(resolveCategoryInviteSuppliers(['BR001_LEAF'], rows, parentBindings, 'BR001')).toContain( 'S_PARENT' ); }); });