import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import { hasSuspiciousOrderAllFiveNine, mapRawItemToExtracted, normalizeOrderQtyUnit, type ExtractedItem, } from '../src/services/orderParseService'; /** 手写送货单样例:单位列 35/25/30、数量列「斤」;另有单位列 9;共 17 个商品。 */ function sample17(): any[][] { return [ ['草鱼', '斤', '35'], ['二刀肉', '斤', '25'], ['腿子牛肉', '斤', '30'], ['蒜苔', '12', '9'], ['小青椒', '18', '斤'], ['青黄瓜', '20', '斤'], ['空心菜', '22', '斤'], ['酸菜', '10', '袋'], ['夹子肉', '7', '斤'], ['水面', '斤', '26'], ['米块', '9', '斤'], ['圣女果', '15', '斤'], ['米糯玉米棒', '13', '斤'], ['鲜海带丝', '斤', '28'], ['苹果', '斤', '32'], ['抄手皮', '8', '袋'], ['精瘦肉', '斤', '19'], ]; } describe('送货单图片录单 · 手写单数量/单位', () => { it('17 个商品:单位列 35/25/30、数量列「斤」交换,单位 9 按斤,数量均为数字', () => { const items = sample17() .map((row) => mapRawItemToExtracted(row)) .filter(Boolean) as ExtractedItem[]; expect(items).toHaveLength(17); expect(items[0]).toMatchObject({ originalName: '草鱼', quantity: 35, unit: '斤' }); expect(items[1]).toMatchObject({ originalName: '二刀肉', quantity: 25, unit: '斤' }); expect(items[2]).toMatchObject({ originalName: '腿子牛肉', quantity: 30, unit: '斤' }); expect(items[3]).toMatchObject({ originalName: '蒜苔', quantity: 12, unit: '斤' }); expect(items[9]).toMatchObject({ originalName: '水面', quantity: 26, unit: '斤' }); for (const item of items) { expect(typeof item.quantity).toBe('number'); expect(String(item.unit)).not.toMatch(/^\d+$/); } }); it('数量字段「35斤」拆为数量35/单位斤', () => { expect(normalizeOrderQtyUnit('35斤', '')).toEqual({ quantity: 35, unit: '斤' }); expect(normalizeOrderQtyUnit('斤', '35')).toEqual({ quantity: 35, unit: '斤' }); expect(normalizeOrderQtyUnit('12', '9')).toEqual({ quantity: 12, unit: '斤' }); }); it('3 行以上数量全为 5/9 标记为可疑,供纠正重识别/人工', () => { const rows = [5, 9, 5].map((quantity) => ({ originalName: `品${quantity}`, quantity, unit: '斤' })) as ExtractedItem[]; expect(hasSuspiciousOrderAllFiveNine(rows)).toBe(true); expect(hasSuspiciousOrderAllFiveNine([...rows.slice(0, 2), { ...rows[2], quantity: 7 }])).toBe(false); }); it('order.parse.image 提示词包含手写分流/完整扫描/3行5/9/数量单位规则,且保留7字段', () => { const source = readFileSync(new URL('../src/prompts/defaults.ts', import.meta.url), 'utf8'); for (const text of [ '先判断单据类型', '手写明细', '右半幅', '页边', '3 行以上数量为 5 或 9', '数量=数字、单位=单位', '单位列出现 9', '第7规格', ]) { expect(source).toContain(text); } }); });