import { readFileSync } from 'node:fs'; import path from 'node:path'; import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * 「直接选择已有商品」的候选接口(开发者 2026-10-11): * 库存入库除了拍照识别,还要能直接检索商品档案选中;选中后带出该商品**上次设置**的 * 成本价/存放区域/保质期/分类/产地/单位,**不带出生产日期**。 * * 这里钉住: * ① 检索口径:只出启用商品(status=1)+ 分公司范围,关键词命中编码或名称,limit 上限 50; * ② 回填**复用** `loadInboundBackfill`(与拍照识别同一份算法,不复制 SQL); * ③ 无分公司 / 无命中 ⇒ 空数组,不抛错; * ④ 返回值里**没有生产日期字段**; * ⑤ 路由权限与识别接口一致(inventory_manage | inspection_manage),且 SQL 不在路由文件里。 */ const connect = vi.hoisted(() => vi.fn()); const query = vi.hoisted(() => vi.fn()); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => query(...args), connect: (...args: unknown[]) => connect(...args) }, getRawPoolQuery: () => query, })); import { listInboundSkuOptions, listStorageZones } from '../src/services/stockInboundBatchService'; function makeClient() { const query = vi.fn(async (sql: string) => { const text = String(sql); // 回填:商品单位/分类(与主查询都含 `FROM skus s`,靠 JOIN 区分) if (text.includes('LEFT JOIN product_catalog pc')) { return { rows: [ { sku_code: 'SKU001', unit: '箱', origin: '山东', category_code: 'CAT-VEG', category_name: '蔬菜', }, ], }; } // 回填:最近一批 if (text.includes('DISTINCT ON (sku_code)')) { return { rows: [ { sku_code: 'SKU001', cost_price: '12.5', storage_zone: '冷库A', production_date: '2026-01-01', expiry_date: '2026-01-31', created_at: '20261001120000', }, ], }; } // 主查询:商品检索 if (text.includes('FROM skus s')) { return { rows: [{ sku_code: 'SKU001', sku_name: '土豆', spec: '5kg/箱', brand: '某某' }], }; } return { rows: [] }; }); return { query, release: vi.fn() }; } beforeEach(() => { connect.mockReset(); query.mockReset(); }); describe('listInboundSkuOptions', () => { it('返回商品档案 + 与拍照识别同一份回填(成本价/区域/保质期天数/分类/产地)', async () => { const client = makeClient(); connect.mockResolvedValue(client); const rows = await listInboundSkuOptions({ branchCode: 'BR002', search: '土豆', limit: 20 }); expect(rows).toHaveLength(1); expect(rows[0]).toMatchObject({ sku_code: 'SKU001', sku_name: '土豆', spec: '5kg/箱', brand: '某某', unit: '箱', origin: '山东', category_code: 'CAT-VEG', category_name: '蔬菜', }); expect(rows[0].backfill.last_cost_price).toBe(12.5); expect(rows[0].backfill.last_storage_zone).toBe('冷库A'); // 2026-01-01 → 2026-01-31 = 30 天 expect(rows[0].backfill.last_shelf_life_days).toBe(30); expect(client.release).toHaveBeenCalledTimes(1); }); it('检索参数:limit + 分公司 + 关键词(关键词命中编码或名称,由 SQL 负责)', async () => { const client = makeClient(); connect.mockResolvedValue(client); await listInboundSkuOptions({ branchCode: 'BR002', search: '土豆', limit: 20 }); const mainCall = client.query.mock.calls.find( (call) => String(call[0]).includes('FROM skus s') && !String(call[0]).includes('LEFT JOIN product_catalog') ); expect(mainCall).toBeTruthy(); expect(mainCall?.[1]).toEqual([20, 'BR002', '土豆']); expect(String(mainCall?.[0])).toContain('s.status = 1'); }); it('limit 上限 50(防一次拉全库)', async () => { const client = makeClient(); connect.mockResolvedValue(client); await listInboundSkuOptions({ branchCode: 'BR002', limit: 999 }); const mainCall = client.query.mock.calls.find( (call) => String(call[0]).includes('FROM skus s') && !String(call[0]).includes('LEFT JOIN product_catalog') ); expect(mainCall?.[1]?.[0]).toBe(50); }); it('无分公司 ⇒ 空数组且不碰数据库', async () => { const rows = await listInboundSkuOptions({ branchCode: '', search: '土豆' }); expect(rows).toEqual([]); expect(connect).not.toHaveBeenCalled(); }); it('无命中 ⇒ 空数组(不抛错)', async () => { const client = makeClient(); client.query.mockImplementation(async (sql: string) => { if (String(sql).includes('FROM skus s')) return { rows: [] }; return { rows: [] }; }); connect.mockResolvedValue(client); await expect(listInboundSkuOptions({ branchCode: 'BR002' })).resolves.toEqual([]); }); it('④ 返回值不含生产日期字段(生产日期由操作员按批手填)', async () => { const client = makeClient(); connect.mockResolvedValue(client); const rows = await listInboundSkuOptions({ branchCode: 'BR002' }); expect(Object.keys(rows[0])).not.toContain('production_date'); expect(Object.keys(rows[0].backfill)).not.toContain('production_date'); }); }); const routeSrc = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/inventoryRoutes.ts'), 'utf8' ); describe('路由接线(源码级)', () => { it('⑤ 权限与识别接口一致:inventory_manage | inspection_manage', () => { const start = routeSrc.indexOf("router.get('/inventory/sku-options'"); expect(start, '路由必须存在').toBeGreaterThan(-1); const body = routeSrc.slice(start, start + 700); expect(body).toContain("requireWorkflowPermission(ctx, ['inventory_manage', 'inspection_manage'])"); expect(body).toContain('listInboundSkuOptions'); }); it('SQL 不在路由文件里(SQL 归 service)', () => { const start = routeSrc.indexOf("router.get('/inventory/sku-options'"); const body = routeSrc.slice(start, start + 700); expect(body).not.toMatch(/SELECT|FROM skus/i); }); }); describe('存放区域候选(区域是自由文本,无独立表)', () => { it('listStorageZones 取既有区域去重(新增区域 = 首次入库落库后自动出现)', async () => { query.mockResolvedValueOnce({ rows: [{ storage_zone: '冷库A' }, { storage_zone: '常温区' }] }); await expect(listStorageZones()).resolves.toEqual(['冷库A', '常温区']); const sql = String(query.mock.calls[0]?.[0] || ''); expect(sql).toContain('DISTINCT storage_zone'); expect(sql).toContain('FROM inventory'); }); it('路由:/inventory/storage-zones 权限与识别/选品一致,SQL 不在路由文件里', () => { const start = routeSrc.indexOf("router.get('/inventory/storage-zones'"); expect(start, '路由必须存在').toBeGreaterThan(-1); const body = routeSrc.slice(start, start + 600); expect(body).toContain("requireWorkflowPermission(ctx, ['inventory_manage', 'inspection_manage'])"); expect(body).toContain('listStorageZones'); expect(body).not.toMatch(/SELECT|FROM inventory/i); }); });