import { readFileSync } from 'fs'; import { describe, expect, it } from 'vitest'; import { itemDisplayNameSql, itemDisplayNameJoinSql } from '../src/services/itemDisplayNameSql'; import { deliveryLoadingItemNameSql } from '../src/services/deliveryLoadingService'; /** * 分拣 / 核货 / 标签打印的「商品名」都取**商品档案当前名**(2026-10-21 开发者指定)。 * * 本地真数据佐证:12 条分拣行里有 8 条 `picking_items.item_name` 与 `skus.sku_name` 不一致 * (瘦肉↔精瘦肉、苹果↔苹果666)⇒ 不改就会一直显示旧快照名。 */ const pickingSrc = readFileSync(new URL('../src/services/pickingService.ts', import.meta.url), 'utf8'); const printSrc = readFileSync(new URL('../src/services/printJobService.ts', import.meta.url), 'utf8'); describe('itemDisplayNameSql:核货/分拣共用同一份取值链', () => { it('档案名 → 订单行名 → 分拣行名', () => { const sql = itemDisplayNameSql({ skuName: 's.sku_name', orderItemName: 'oi.item_name', pickingItemName: 'pi.item_name', }); expect(sql).toBe( "COALESCE(NULLIF(TRIM(s.sku_name), ''), NULLIF(TRIM(oi.item_name), ''), NULLIF(TRIM(pi.item_name), ''))" ); expect(sql.indexOf('s.sku_name')).toBeLessThan(sql.indexOf('oi.item_name')); expect(sql.indexOf('oi.item_name')).toBeLessThan(sql.indexOf('pi.item_name')); }); it('缺列跳过、全空返回 NULL', () => { expect(itemDisplayNameSql({ orderItemName: 'oi.item_name' })).toBe( "COALESCE(NULLIF(TRIM(oi.item_name), ''))" ); expect(itemDisplayNameSql({})).toBe('NULL'); }); it('JOIN 片段按 sku_code 关联(改名必须同时补 JOIN)', () => { expect(itemDisplayNameJoinSql()).toContain('LEFT JOIN skus s ON s.sku_code ='); }); it('核货侧函数委托给同一实现(不再各写一份)', () => { expect(deliveryLoadingItemNameSql({ skuName: 'sk.sku_name', orderItemName: 'oi.item_name' })).toBe( itemDisplayNameSql({ skuName: 'sk.sku_name', orderItemName: 'oi.item_name' }) ); }); }); describe('分拣列表 / 标签打印:展示名档案优先', () => { it('分拣列表 SQL 用取值链并另存快照列', () => { // 只在「分拣列表」这条 SQL 内断言:service 里还有**完成校验**等其它查询也用 pi.item_name(非展示),不该被这条测试牵连 const start = pickingSrc.indexOf('export const PICKING_ITEMS_ENRICHED_SQL'); expect(start).toBeGreaterThan(-1); const sqlBlock = pickingSrc.slice(start, pickingSrc.indexOf('`;', start)); expect(sqlBlock).toContain('AS item_name'); expect(sqlBlock).toContain('pi.item_name AS item_name_snapshot'); // 旧写法(裸选 pi.item_name 作为展示名)必须已从这条 SQL 消失 expect(sqlBlock).not.toContain('pi.order_item_id, pi.item_name, pi.required_qty'); }); it('分拣标签打印 payload 同样档案优先', () => { expect(printSrc).toContain('AS item_name'); expect(printSrc).toContain('pi.item_name AS item_name_snapshot'); expect(printSrc).not.toMatch(/pi\.pick_code,\s*\n\s*pi\.item_name,/); }); });