Files
SupplierSystem/server/tests/pickingBatchDate.test.ts
T
雨天 57ce3b64a1
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
分拣: 拍包装读生产日期匹配出库批次(AI-30)
复用行内「拍照」按钮,上传出库照后用第 1 张照片让 AI 只读生产日期(不认商品名),按 production_date 匹配本行候选批次:唯一命中则先取该批次、余量按 FIFO 补,并在同一个「调整批次」弹窗里高亮该行;多命中/无命中/日期为 null 回退 FIFO。无论是否匹配都打开同一弹窗,未配多模态或识别失败静默降级为手动选批次,不阻断分拣。

- 后端: 提示词 picking.batch.production_date、解析纯函数 parseProductionDateJson、服务 pickingBatchDateService、路由 POST /mobile/picking/recognize-production-date
- 小程序: request.recognizePickingProductionDate;抽出 buildEditorState 与 openBatchEditor 共用;新增 .batch-row-ai 高亮
- 零新增界面文案;登记 AI-30;补设计文档与契约测试
2026-10-16 13:48:30 +08:00

110 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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' : ''}}");
});
});