import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import { parseProductionDateJson } from '../src/utils/shelfLife'; /** * 分拣 · 拍包装照读**生产日期** → 匹配出库批次(AI-30,2026-10-15 开发者需求)。 * * 钉住三件事: * ① 解析纯函数边界(围栏 JSON / 裸对象 / 字符串 "null" / JSON null / 空 / 中文日期 / 无日期)—— * 看不清必须 null、**禁止猜**(猜测会扣错批次); * ② 后端契约:路由 + 服务 + 提示词都在,且**未配多模态时不阻断**(返回 recognized:false); * ③ 小程序契约:复用「拍照」按钮 → 调接口 → 打开**同一个**「调整批次」弹窗, * **唯一命中**才预选并高亮,多命中/无命中回退 FIFO。 */ const serviceSrc = readFileSync( new URL('../src/services/pickingBatchDateService.ts', import.meta.url), 'utf8' ); const routeSrc = readFileSync( new URL('../src/routes/mobile/mobileWarehouseRoutes.ts', import.meta.url), 'utf8' ); const defaultsSrc = readFileSync(new URL('../src/prompts/defaults.ts', import.meta.url), 'utf8'); const mpPanelSrc = readFileSync( new URL('../../miniprogram/components/picking-panel/index.js', import.meta.url), 'utf8' ); const mpPanelWxml = readFileSync( new URL('../../miniprogram/components/picking-panel/index.wxml', import.meta.url), 'utf8' ); const mpRequestSrc = readFileSync( new URL('../../miniprogram/utils/request.js', import.meta.url), 'utf8' ); describe('parseProductionDateJson 边界(看不清必须 null,禁止猜)', () => { it('围栏 JSON / 裸对象 → YYYY-MM-DD', () => { expect(parseProductionDateJson('```json\n{"production_date":"2026-10-15"}\n```')).toBe('2026-10-15'); expect(parseProductionDateJson('{"production_date":"2026-10-15"}')).toBe('2026-10-15'); expect(parseProductionDateJson('好的,结果是 {"production_date": "2026-10-15"} 请查收')).toBe('2026-10-15'); }); it('只有年月补日为 01;中文日期归一', () => { expect(parseProductionDateJson('{"production_date":"2026-10"}')).toBe('2026-10-01'); expect(parseProductionDateJson('{"production_date":"2026年10月15日"}')).toBe('2026-10-15'); }); it('JSON null / 字符串 "null" / 空 / 无日期 ⇒ null', () => { expect(parseProductionDateJson('{"production_date":null}')).toBeNull(); expect(parseProductionDateJson('{"production_date":"null"}')).toBeNull(); expect(parseProductionDateJson('')).toBeNull(); expect(parseProductionDateJson('null')).toBeNull(); expect(parseProductionDateJson('看不清')).toBeNull(); expect(parseProductionDateJson(undefined)).toBeNull(); }); it('裸日期不加围栏也能兜底(正则),但不把批号数字当日期', () => { expect(parseProductionDateJson('2026-10-15')).toBe('2026-10-15'); expect(parseProductionDateJson('批号 IN20261015XJ2HOX')).toBeNull(); }); }); describe('后端契约', () => { it('提示词 picking.batch.production_date 存在且只输出生产日期', () => { expect(defaultsSrc).toContain("featureKey: 'picking.batch.production_date'"); expect(defaultsSrc).toContain('production_date'); }); it('服务经多模态链路,且未配多模态时不报错(recognized:false)', () => { expect(serviceSrc).toContain('hasRuntimeConfig'); expect(serviceSrc).toContain('callWithImage'); expect(serviceSrc).toContain('recognized: false'); expect(serviceSrc).toContain('parseProductionDateJson'); }); it('路由 POST /picking/recognize-production-date 挂了分拣访问守卫', () => { expect(routeSrc).toContain("'/picking/recognize-production-date'"); expect(routeSrc).toContain('recognizeProductionDateFromImage'); expect(routeSrc).toContain('requirePickingAccess'); }); }); describe('小程序契约(复用拍照 + 复用同一批次弹窗)', () => { it('request 有 recognizePickingProductionDate 且指向新路由', () => { expect(mpRequestSrc).toContain('recognizePickingProductionDate'); expect(mpRequestSrc).toContain('/mobile/picking/recognize-production-date'); }); it('拍照后调 AI 并打开同一个批次弹窗(buildEditorState 共用)', () => { expect(mpPanelSrc).toContain('applyBatchAiFromPhoto'); expect(mpPanelSrc).toContain('prepareOrderImageBase64'); // 复用同一套弹窗构造:既被 openBatchEditor 用,也被拍照 AI 用 expect(mpPanelSrc).toContain('buildEditorState'); expect(mpPanelSrc).toContain('this.buildEditorState(id, basePlan)'); }); it('只认唯一命中,多命中/无命中不猜(回退 FIFO)', () => { expect(mpPanelSrc).toContain('function matchBatchByProductionDate'); expect(mpPanelSrc).toContain("String((b && b.production_date) || '').trim() === target"); expect(mpPanelSrc).toContain('hits.length === 1 ? hits[0] : null'); expect(mpPanelSrc).toContain('function buildBatchBasePlan'); expect(mpPanelSrc).toContain('allocateBatchesFifo(batches, needQty).plan'); }); it('AI 命中行只做高亮(零新增文案)', () => { expect(mpPanelWxml).toContain("{{item.ai ? 'batch-row-ai' : ''}}"); }); });