import { beforeEach, describe, expect, it, vi } from 'vitest'; import { readFileSync } from 'fs'; import path from 'path'; /** * 订单详情「报告」列显示**批次报告**(2026-10-12)。 * * 设计:`docs/design/20261012-订单详情显示批次报告.md` * * 用户报告「我这边没有看到报告,主系统那边也没有」——主系统侧已由第九批推送修好, * 我方侧此前只显示**质检报告**(`quality_reports`),不显示**批次报告** * (`inventory_batch_reports`)⇒ 抄手皮那几批明明绑了报告,订单行仍是空的。 * * 这里钉住三件做反就要返工的事: * ① **口径不许漂**:批次判据必须与推送侧 `collectReportPushCandidates` 同一套两来源 * (`picking_items.outbound_batches` ∪ `inventory_logs` 的 `picking_out`/`delivery_loading_out`), * 否则会出现"我方看得到、主系统收不到"(或反之); * ② **只显示已人工确认的报告**(`status = 30`),草稿/待确认不许出现在订单详情; * ③ 路由只做鉴权 + 委派(与既有 `quality-report-files` 同权限集合),不写 SQL。 */ const query = vi.hoisted(() => vi.fn()); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => query(...args) }, })); import { listOrderBatchReportFiles } from '../src/services/orderBatchReportFilesService'; const serviceSrc = readFileSync( path.join(__dirname, '../src/services/orderBatchReportFilesService.ts'), 'utf8' ); const pushSrc = readFileSync( path.join(__dirname, '../src/services/mainSystemReportPushService.ts'), 'utf8' ); const routeSrc = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'), 'utf8' ); /** 取出 GET /orders/:orderCode/batch-reports 这一个 handler 的源码片段 */ function batchReportRouteBlock(): string { const start = routeSrc.indexOf("router.get('/orders/:orderCode/batch-reports'"); expect(start, '路由必须存在').toBeGreaterThan(-1); const end = routeSrc.indexOf('router.get(', start + 10); return routeSrc.slice(start, end > start ? end : start + 2000); } describe('订单详情批次报告(我方侧可见性)', () => { beforeEach(() => { query.mockReset(); }); it('① 口径守卫:批次判据与推送侧同为「分拣选定 ∪ 实际出库流水」两来源', () => { for (const src of [serviceSrc, pushSrc]) { expect(src).toContain('outbound_batches'); expect(src).toContain("'picking_out'"); expect(src).toContain("'delivery_loading_out'"); expect(src).toContain('related_order_code'); } // 实际出库流水必须两者都认(只看 picking_out 会漏发车兜底出库的单) expect(serviceSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i); expect(pushSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i); }); it('② 只显示已人工确认的批次报告(status = 30)', () => { expect(serviceSrc).toMatch(/r\.status\s*=\s*30/); expect(serviceSrc).toContain('inventory_batch_report_matches'); expect(serviceSrc).toContain('inventory_batch_reports'); }); it('③ 路由只做鉴权 + 委派,且与既有质检报告列同权限集合', () => { const block = batchReportRouteBlock(); expect(block).toContain('authMiddleware'); for (const perm of ['order_manage', 'order_entry', 'order_status_update', 'allocation_manage']) { expect(block).toContain(`'${perm}'`); } expect(block).toContain('listOrderBatchReportFiles'); expect(block, '路由里不许写 SQL').not.toMatch(/SELECT\s+/i); }); it('④ 空单号直接返回空,不打扰数据库', async () => { await expect(listOrderBatchReportFiles(' ')).resolves.toEqual({ items: [] }); expect(query).not.toHaveBeenCalled(); }); it('⑤ 组装:按行 × 批次给出报告与签名文件(pdf/image 分类)', async () => { query.mockResolvedValue({ rowCount: 2, rows: [ { our_order_item_id: '887', sku_code: 'SKU62634064428', item_name: '抄手皮', batch_no: 'STK20261011HHFCUN', report_id: '42', report_no: 'HZ-2026-1011', produced_at: '2026-10-08', testing_org: '红河质安检验检测有限公司', product_names: ['抄手皮'], file_paths: ['抄手皮/2026/报告.pdf', '抄手皮/2026/照片.jpg', '', null], }, ], }); const { items } = await listOrderBatchReportFiles('ORD_X'); expect(query).toHaveBeenCalledTimes(1); expect(query.mock.calls[0][1]).toEqual(['ORD_X']); expect(items).toHaveLength(1); expect(items[0]).toMatchObject({ our_order_item_id: '887', sku_code: 'SKU62634064428', item_name: '抄手皮', batch_no: 'STK20261011HHFCUN', report_id: '42', report_no: 'HZ-2026-1011', produced_at: '2026-10-08', testing_org: '红河质安检验检测有限公司', product_names: ['抄手皮'], }); // 空文件名被滤掉,剩下两个;URL 走 quality-reports 同一套签名桶 expect(items[0].files.map((f) => f.kind)).toEqual(['pdf', 'image']); expect(items[0].files[0].url).toContain('/api/storage/quality-reports/public/'); expect(items[0].files[0].url).toContain('sig='); }); it('⑥ 字段可空(无报告号/无检测机构/无产品名)不炸', async () => { query.mockResolvedValue({ rowCount: 1, rows: [ { our_order_item_id: '1', sku_code: null, item_name: null, batch_no: 'STK1', report_id: '9', report_no: null, produced_at: null, testing_org: null, product_names: null, file_paths: null, }, ], }); const { items } = await listOrderBatchReportFiles('ORD_Y'); expect(items[0]).toMatchObject({ sku_code: null, item_name: null, report_no: null, produced_at: null, testing_org: null, product_names: [], files: [], }); }); });