Files
SupplierSystem/src/utils/shelfLife.parity.test.ts
T
雨天 3bc82d669c
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
Web库存入库: 保质期改为数值+年/月/日
面板保质期格由纯文本改为「数值输入框 + 年/月/日 下拉」,
与小程序订单入库、Web 验收弹窗同形态。两控件改动即时经
composeShelfLifeText 组回原文写入 shelf_life,提交与预填路径未变
(inventory.shelf_life 仍存原文,有效期仍由服务端 deriveExpiryDate 推算)。
预填/AI/多图合并后由面板层 helper withShelfLifeFields 拆回两控件
(只补空),不改 applyInboundBackfill 预填规则;同格内联不破 5 列
grid;零新增文案(复用 phShelf 与 inspection.qc.shelf*)。
2026-10-16 17:53:51 +08:00

146 lines
5.5 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.
/**
* 入库识别「日期两列」的前端口径测试。
*
* 背景(2026-10-15 开发者指令):AI 现在返回**数字 + 年/月/日**(`shelf_life_value` + `shelf_life_unit`),
* 小程序已改,Web 没改 ⇒ Web 保质期空白、生产日期因 `<input type="date">` 只认 `YYYY-MM-DD` 而显示空。
*
* 本文件钉住两件事:
* ① 前端 `src/utils/shelfLife.ts` 与 `server/src/utils/shelfLife.ts` **逐值一致**(同一批边界输入);
* ② 入库面板确实用了这套归一(且 DTO 已补 `shelf_life_value`/`shelf_life_unit`)。
*/
import { readFileSync } from 'fs';
import { describe, expect, it } from 'vitest';
import {
composeShelfLifeText,
normalizeRecognizedDate,
parseShelfLife,
resolveRecognizedDateFields,
} from './shelfLife';
import {
composeShelfLifeText as serverCompose,
normalizeRecognizedDate as serverNormalize,
parseShelfLife as serverParse,
} from '../../server/src/utils/shelfLife';
const panelSrc = readFileSync(
new URL('../pages/inventory/StockInboundPanel.tsx', import.meta.url),
'utf8'
);
const apiSrc = readFileSync(new URL('./workflowApi.ts', import.meta.url), 'utf8');
describe('与 server/src/utils/shelfLife.ts 逐值对拍', () => {
it('normalizeRecognizedDate:ISO / 斜杠 / 点 / 纯数字 / 中文 / 脏数据', () => {
const cases: unknown[] = [
'2026-10-14',
'2026/10/14',
'2026.10.14',
'2026-10-5',
'2026-10',
'2026/10',
'20261014',
'202610',
'2026年10月14日',
'2026年10月',
'2026年10月5日',
'20261314', // 月份非法 ⇒ null
'2026-13-01', // 月份非法(正则不校验月)⇒ 与原样保留,逐值对拍只要求两侧一致
'',
null,
undefined,
'abc',
20261014,
];
for (const value of cases) {
expect(normalizeRecognizedDate(value), `normalize(${String(value)})`).toBe(
serverNormalize(value)
);
}
});
it('composeShelfLifeText:数字 + 年/月/日(含天/个月别名)', () => {
const cases: Array<[unknown, unknown]> = [
[6, '月'],
[6, '个月'],
[180, '日'],
[7, '天'],
[1, '年'],
['6', '月'],
[6, ''],
[0, '年'],
[-3, '年'],
['abc', '年'],
['12个月', undefined], // 旧模型整串
['180天', '月'],
['', '年'],
];
for (const [value, unit] of cases) {
expect(composeShelfLifeText(value, unit), `compose(${String(value)},${String(unit)})`).toBe(
serverCompose(value, unit)
);
}
});
it('parseShelfLife:原文 → 数值 + 单位', () => {
const cases: unknown[] = ['7天', '180日', '6个月', '24个月', '1年', '常温12个月', '半年', '', 'abc'];
for (const value of cases) {
expect(parseShelfLife(value), `parse(${String(value)})`).toEqual(serverParse(value));
}
});
});
describe('AI 识别结果 → 页面两个日期字段', () => {
it('新字段(数字 + 年/月/日)⇒ 组装成落库原文 + 生产日期归一', () => {
expect(
resolveRecognizedDateFields({
production_date: '2026年10月15日',
shelf_life_value: 6,
shelf_life_unit: '个月',
})
).toEqual({ production_date: '2026-10-15', shelf_life: '6个月' });
expect(
resolveRecognizedDateFields({ production_date: '20261001', shelf_life_value: 180, shelf_life_unit: '日' })
).toEqual({ production_date: '2026-10-01', shelf_life: '180天' });
expect(
resolveRecognizedDateFields({ production_date: '2026-10', shelf_life_value: 1, shelf_life_unit: '年' })
).toEqual({ production_date: '2026-10-01', shelf_life: '1年' });
});
it('旧字段整串文本仍兼容(「6个月」/「180天」/「1年」)', () => {
expect(resolveRecognizedDateFields({ production_date: null, shelf_life: '6个月' })).toEqual({
production_date: '',
shelf_life: '6个月',
});
expect(resolveRecognizedDateFields({ shelf_life: '180天' }).shelf_life).toBe('180天');
expect(resolveRecognizedDateFields({ shelf_life: '1年' }).shelf_life).toBe('1年');
});
it('只有数字没单位 ⇒ 填数字留人补单位;空/垃圾 ⇒ 一律留空(不猜)', () => {
expect(resolveRecognizedDateFields({ shelf_life_value: 12 }).shelf_life).toBe('12');
expect(resolveRecognizedDateFields({ shelf_life_value: 'null', shelf_life_unit: 'null' })).toEqual({
production_date: '',
shelf_life: '',
});
expect(resolveRecognizedDateFields(null)).toEqual({ production_date: '', shelf_life: '' });
expect(resolveRecognizedDateFields({ production_date: '不是日期', shelf_life: 'abc' }).production_date).toBe('');
});
});
describe('接线与字段定义', () => {
it('入库面板用同一个工具解析日期两列(不再直接塞 AI 原文)', () => {
// 面板从同一工具导入(2026-10-16 起为多行导入,同时带入 compose/parse 以支撑「数值+单位」格)
expect(panelSrc).toContain("from '../../utils/shelfLife';");
expect(panelSrc).toContain('resolveRecognizedDateFields,');
expect(panelSrc).toContain('composeShelfLifeText,');
expect(panelSrc).toContain('parseShelfLife,');
expect(panelSrc).toContain('...resolveRecognizedDateFields(item),');
expect(panelSrc).not.toContain("production_date: item.production_date || '',");
});
it('识别结果 DTO 补了 shelf_life_value / shelf_life_unit', () => {
expect(apiSrc).toContain('shelf_life_value: number | null;');
expect(apiSrc).toContain('shelf_life_unit: string | null;');
});
});