import { describe, expect, it } from 'vitest'; import { expandWhitespaceJoinedItems, type ParseMatchContext, } from '../src/services/orderParseService'; /** * 录单文本「空格分隔的多个商品名」兜底拆行。 * * 对齐主系统提交 93d33142(fix(order-parse): 空格分隔且精确命中商品时拆行): * 前端一直原样传递空格文本;后端本地拆条原先只按换行/中文标点切分,导致 * 「西瓜 白菜 土豆」被当成一项。新增兜底:仅当每个空格分段都精确命中 * 客户商品库/别名/SKU 时才拆行;含空格商品名与带数量文本保持原结果。 */ function ctxWith(names: string[]): ParseMatchContext { return { customerSkus: names.map((name, i) => ({ name, skuCode: `SKU${i}`, skuName: name, unit: '斤', processingMethod: '', usageCount: 1, categoryCode: 'CAT', })), customerAliases: [], skus: [], customerSkuCodes: new Set(names.map((_, i) => `SKU${i}`)), customerAliasKeys: new Set(), customerProcessingAliases: [], skuCategoryByCode: new Map(), catalogParentByCode: new Map(), smartRules: [], }; } const item = (name: string) => ({ originalName: name, quantity: 1, unit: '个' }); describe('录单文本空格拆行', () => { it('三个名称都精确命中商品库时拆成三行', () => { const out = expandWhitespaceJoinedItems( '西瓜 白菜 土豆', [item('土豆')], ctxWith(['西瓜', '白菜', '土豆']) ); expect(out.map((row) => row.originalName)).toEqual(['西瓜', '白菜', '土豆']); }); it('含空格但整体才是商品名时保持一行', () => { const out = expandWhitespaceJoinedItems( '醋 大瓶', [item('醋 大瓶')], ctxWith(['醋 大瓶']) ); expect(out).toHaveLength(1); expect(out[0].originalName).toBe('醋 大瓶'); }); it('商品库完全没命中时,纯名称空格串也按兜底拆行(解决模型丢无数量清单)', () => { const out = expandWhitespaceJoinedItems( '土豆 白菜 西红柿', [item('土豆 白菜 西红柿')], ctxWith([]) ); expect(out.map((row) => row.originalName)).toEqual(['土豆', '白菜', '西红柿']); }); it('品名自带单位字(条/根/只)也要拆:小白菜 苹果 二荆条', () => { const out = expandWhitespaceJoinedItems( '小白菜 苹果 二荆条', [item('小白菜 苹果 二荆条')], ctxWith(['小白菜', '苹果']) ); expect(out.map((row) => row.originalName)).toEqual(['小白菜', '苹果', '二荆条']); }); it('「油条」「粉条」这类品名不被当成数量单位', () => { const out = expandWhitespaceJoinedItems('油条 粉条', [item('油条 粉条')], ctxWith([])); expect(out.map((row) => row.originalName)).toEqual(['油条', '粉条']); }); it('纯数量词仍不拆:土豆 两斤 白菜', () => { const out = expandWhitespaceJoinedItems( '土豆 两斤 白菜', [item('土豆 两斤 白菜')], ctxWith(['土豆', '白菜']) ); expect(out).toHaveLength(1); }); it('菜名/菜单词不拆(无数量菜单说明仍按原规则忽略成一条)', () => { const out = expandWhitespaceJoinedItems( '青豆炒碎肉 肥肠烧土豆', [item('青豆炒碎肉 肥肠烧土豆')], ctxWith([]) ); expect(out).toHaveLength(1); }); it('带数量/单位或非精确命中时不拆', () => { const out = expandWhitespaceJoinedItems( '西瓜 2斤 土豆', [item('土豆')], ctxWith(['西瓜', '土豆']) ); expect(out).toHaveLength(1); expect(out[0].originalName).toBe('土豆'); }); });