import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 订单入库「一键上传批次报告 → 提取生产日期 → 随入库落库」(2026-10-16 开发者需求/批准)。 * 设计 docs/design/20261016-订单入库批量上传批次报告并落生产日期.md。 * * 只钉**接线与优先级**(真实 OCR 与入库往返需真库,见设计 §6): * ① 验收入参新增逐行 `production_date`(**优先于** 预包装 `production_date_month`)与 `batch_report_id`; * ② 入库 COMMIT **之后**把报告关联到新建库存批次(best-effort,失败不阻断); * ③ 移动端映射两个字段 + 新增 `POST /mobile/inspection/batch-reports`(复用既有 OCR 服务); * ④ 小程序:按钮 + 核对表 + 两个提交路径都带上这两个字段。 */ const readServer = (rel: string) => readFileSync(new URL(`../${rel}`, import.meta.url), 'utf8'); const readMp = (rel: string) => readFileSync(new URL(`../../miniprogram/${rel}`, import.meta.url), 'utf8'); const svc = readServer('src/services/inspectionComplianceService.ts'); const mobileRoutes = readServer('src/routes/mobile/mobileWarehouseRoutes.ts'); const mpPanelJs = readMp('components/inspection-panel/index.js'); const mpPanelWxml = readMp('components/inspection-panel/index.wxml'); const mpPanelWxss = readMp('components/inspection-panel/index.wxss'); const mpRequest = readMp('utils/request.js'); describe('验收:逐行生产日期优先(不改 INSERT,只改取值)', () => { it('入参新增 production_date / batch_report_id', () => { expect(svc).toContain('production_date?: string | null;'); expect(svc).toContain('batch_report_id?: string | null;'); }); it('取值:逐行 production_date > 预包装 production_date_month > null(仍兜底今天)', () => { const start = svc.indexOf('const productionDate ='); expect(start).toBeGreaterThanOrEqual(0); const block = svc.slice(start, start + 320); expect(block).toContain('input.production_date'); expect(block).toContain('input.production_date_month'); expect(block.indexOf('input.production_date')).toBeLessThan( block.indexOf('input.production_date_month') ); // 仍是「YYYY-MM」→「-01」的既有预包装口径 expect(block).toContain('-01'); }); it('INSERT 的列与 COALESCE 兜底未被改(不新增列、不改写入方)', () => { expect(svc).toContain("COALESCE($8::date, (NOW() AT TIME ZONE 'Asia/Shanghai')::date)"); }); }); describe('验收:入库成功后把批次报告关联到新建库存批次(best-effort)', () => { it('调用既有 confirmInventoryBatchReportMatches,且以 inboundInventoryId 为主键', () => { expect(svc).toContain("await import('./inventoryBatchReportService')"); expect(svc).toContain('confirmInventoryBatchReportMatches({'); expect(svc).toMatch(/matches: \[\{ inventoryId: inboundInventoryId \}\]/); expect(svc).toMatch(/if \(!isRejected && inboundInventoryId && input\.batch_report_id\)/); }); it('放在 COMMIT 之后、失败只记日志不抛(日期已入库,不因关联失败回滚)', () => { const linkIdx = svc.indexOf('confirmInventoryBatchReportMatches({'); expect(linkIdx).toBeGreaterThanOrEqual(0); // 取该关联点**之前**最近的一次 COMMIT:两者很近 ⇒ 确实在提交后的收尾段 const commitIdx = svc.slice(0, linkIdx).lastIndexOf("await client.query('COMMIT')"); expect(commitIdx).toBeGreaterThanOrEqual(0); expect(linkIdx - commitIdx).toBeLessThan(3000); const tail = svc.slice(linkIdx, linkIdx + 700); expect(tail).toContain('catch (err)'); expect(tail).toContain('console.error'); }); }); describe('移动端路由', () => { it('complete 映射 production_date / batch_report_id', () => { expect(mobileRoutes).toMatch( /production_date: req\.body\?\.production_date \? String\(req\.body\.production_date\) : null/ ); expect(mobileRoutes).toMatch( /batch_report_id: req\.body\?\.batch_report_id \? String\(req\.body\.batch_report_id\) : null/ ); }); it('新增 POST /mobile/inspection/batch-reports 且复用既有 OCR 服务', () => { expect(mobileRoutes).toContain("router.post('/inspection/batch-reports'"); expect(mobileRoutes).toContain('createInventoryBatchReports({'); expect(mobileRoutes).toContain("from '../../services/inventoryBatchReportService'"); expect(mobileRoutes).toContain('requireInspectionManageRole'); }); it('后端不新增表/不碰 quality_reports(复用既有独立表与服务)', () => { expect(mobileRoutes).toContain('createInventoryBatchReports'); // 端点内只上调服务,不直接写 inventory const start = mobileRoutes.indexOf("router.post('/inspection/batch-reports'"); const block = mobileRoutes.slice(start, start + 1400); expect(block).not.toContain('INSERT INTO inventory'); }); }); describe('小程序:按钮 / 核对表 / 两个提交路径都带上字段', () => { it('request 有 createInspectionBatchReports(复用 quality-reports 桶上传)', () => { expect(mpRequest).toContain('function createInspectionBatchReports('); expect(mpRequest).toContain("'/mobile/inspection/batch-reports'"); expect(mpRequest).toContain('createInspectionBatchReports,'); expect(mpPanelJs).toContain('uploadQualityReportStorageFile'); }); it('按钮在「一键全部合格入库」下方,且核对表存在', () => { const iBulk = mpPanelWxml.indexOf('一键全部合格入库'); const iReport = mpPanelWxml.indexOf('一键上传批次报告'); expect(iBulk).toBeGreaterThanOrEqual(0); expect(iReport).toBeGreaterThan(iBulk); expect(mpPanelWxml).toContain('bindtap="onUploadBatchReports"'); expect(mpPanelWxml).toContain('wx:if="{{reportReview}}"'); expect(mpPanelWxml).toContain('catchtap="onReportConfirm"'); expect(mpPanelWxml).toContain('bindchange="onReportDateChange"'); }); it('两个按钮**竖排**(微信 button[size=mini] 默认 inline-block,容器须强制 column)', () => { const wrap = mpPanelWxss.slice( mpPanelWxss.indexOf('.batch-btn-wrap'), mpPanelWxss.indexOf('.batch-btn {') ); expect(wrap).toContain('flex-direction: column'); expect(wrap).toContain('display: flex'); }); it('上传入口:拍照 / 相册 / 聊天文件(PDF)三项分开,不合并', () => { expect(mpPanelJs).toContain("itemList: ['拍照', '相册', '聊天文件(PDF)']"); expect(mpPanelJs).not.toContain('拍照/相册'); expect(mpPanelJs).toContain("this.pickReportImages(['camera'])"); expect(mpPanelJs).toContain("this.pickReportImages(['album'])"); }); it('核对表:按商品名匹配 + 日期可改 + 确定才写入行', () => { expect(mpPanelJs).toContain('buildReportReview(reports)'); expect(mpPanelJs).toContain('match_label'); expect(mpPanelJs).toContain('onReportDateChange(e)'); expect(mpPanelJs).toMatch(/onReportConfirm\(\)[\s\S]{0,600}this\.patchRow\(item\.row_id, patch\)/); }); it('逐行入库与一键入库都带 production_date / batch_report_id', () => { const occurrences = mpPanelJs.match(/batch_report_id: String\(row/g) || []; expect(occurrences.length).toBeGreaterThanOrEqual(2); }); it('刷新不丢:loadRows 从 prev 保留 production_date / batch_report_id', () => { expect(mpPanelJs).toContain('prev.production_date !== undefined'); expect(mpPanelJs).toContain('prev.batch_report_id !== undefined'); }); });