Files
SupplierSystem/server/tests/inventoryShelfLife.test.ts
T
雨天andCursor e3ae7ee6e7
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
feat(库存入库): 按行入库 + 保质期年/月/日选择 + 弹层抬层
小程序识别结果逐行入库;保质期拆数值与单位(AI 三选一或人手选)落库原文;选择商品弹层 root-portal 防 tab 遮挡,并预填 AI 品名。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-15 10:43:47 +08:00

115 lines
5.2 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 {
composeShelfLifeText,
deriveExpiryDate,
normalizeRecognizedDate,
parseShelfLife,
} from '../src/utils/shelfLife';
/**
* 保质期「存原文 + 解析推算有效期」的口径(2026-10-14 开发者指令):
* 「修改的应该是保质期 而不是有效期」「保质期可能是年月日(三种单位),存储的应该是一个字符串然后解析」。
*
* 钉住四件事:
* ① 解析支持 天 / 月 / 年;② 月 = 30 天、年 = 360 天;③ 推不出返回 null(不瞎写);
* ④ 库存管理「修改」弹窗把手填「有效期」换成「保质期」,有效期由服务端派生。
*/
const batchEditSrc = readFileSync(
new URL('../src/services/inventoryBatchEditService.ts', import.meta.url),
'utf8'
);
const routeSrc = readFileSync(
new URL('../src/modules/workflow/routes/inventoryRoutes.ts', import.meta.url),
'utf8'
);
const inboundSrc = readFileSync(
new URL('../src/services/stockInboundService.ts', import.meta.url),
'utf8'
);
const migrationSrc = readFileSync(
new URL('../../migrations/20261042_inventory_shelf_life.sql', import.meta.url),
'utf8'
);
const modalSrc = readFileSync(
new URL('../../src/pages/inventory/InventoryManagement.tsx', import.meta.url),
'utf8'
);
describe('保质期解析与有效期推算', () => {
it('parseShelfLife:天 / 月 / 年 三种单位(含带前缀文案)', () => {
expect(parseShelfLife('7天')).toEqual({ unit: 'day', n: 7 });
expect(parseShelfLife('180日')).toEqual({ unit: 'day', n: 180 });
expect(parseShelfLife('6个月')).toEqual({ unit: 'month', n: 6 });
expect(parseShelfLife('1年')).toEqual({ unit: 'year', n: 1 });
expect(parseShelfLife('常温12个月')).toEqual({ unit: 'month', n: 12 });
expect(parseShelfLife('')).toBeNull();
expect(parseShelfLife('新鲜')).toBeNull();
});
it('deriveExpiryDate:生产日期 + 保质期(月=30 天、年=360 天)', () => {
expect(deriveExpiryDate('2026-01-01', '30天')).toBe('2026-01-31');
expect(deriveExpiryDate('2026-01-01', '6个月')).toBe('2026-06-30');
expect(deriveExpiryDate('2026-01-01', '1年')).toBe('2026-12-27');
});
it('deriveExpiryDate:保质期本身是截止日期 ⇒ 直接用;推不出 ⇒ null(不瞎写)', () => {
expect(deriveExpiryDate(null, '2026-03-01')).toBe('2026-03-01');
expect(deriveExpiryDate('2026-01-01', '')).toBeNull();
expect(deriveExpiryDate(null, '7天')).toBeNull();
expect(deriveExpiryDate('2026-01-01', '新鲜')).toBeNull();
});
it('normalizeRecognizedDate:只有年月补 01;兼容纯数字 / 中文', () => {
expect(normalizeRecognizedDate('2026-03')).toBe('2026-03-01');
expect(normalizeRecognizedDate('2026-03-05')).toBe('2026-03-05');
expect(normalizeRecognizedDate('20261014')).toBe('2026-10-14');
expect(normalizeRecognizedDate(20261014)).toBe('2026-10-14');
expect(normalizeRecognizedDate('202610')).toBe('2026-10-01');
expect(normalizeRecognizedDate('2026年10月14日')).toBe('2026-10-14');
expect(normalizeRecognizedDate('')).toBeNull();
});
it('composeShelfLifeText:年/月/日 → 落库原文', () => {
expect(composeShelfLifeText(6, '月')).toBe('6个月');
expect(composeShelfLifeText(180, '日')).toBe('180天');
expect(composeShelfLifeText(1, '年')).toBe('1年');
expect(composeShelfLifeText(12, '天')).toBe('12天');
expect(composeShelfLifeText('6个月')).toBe('6个月');
expect(composeShelfLifeText(6, null)).toBeNull();
expect(composeShelfLifeText(null, '月')).toBeNull();
});
});
describe('库存管理「修改」:存保质期原文 + 有效期派生(源码契约)', () => {
it('服务层:收 shelf_life、不再收手填 expiryDate,并用 deriveExpiryDate 重算', () => {
expect(batchEditSrc).toContain('shelfLife?: string | null;');
expect(batchEditSrc).not.toContain('expiryDate?: string | null;');
expect(batchEditSrc).toContain('deriveExpiryDate(nextProductionDate, nextShelfLife)');
expect(batchEditSrc).toMatch(/shelf_life = \$/);
});
it('路由:入参由 expiry_date 改为 shelf_life', () => {
expect(routeSrc).toContain("shelfLife: 'shelf_life' in body ? body.shelf_life : undefined,");
expect(routeSrc).not.toContain("expiryDate: 'expiry_date' in body");
});
it('入库链路:保质期原文随行落库(writeStockImportRow 写 shelf_life)', () => {
expect(inboundSrc).toContain('shelfLife?: string | null;');
expect(inboundSrc).toContain('deriveExpiryDate(productionDate, shelfLife)');
expect(inboundSrc).toContain('shelf_life');
});
it('迁移:所有 schema 加 inventory.shelf_life 列 + 响亮失败守卫', () => {
expect(migrationSrc).toContain('ADD COLUMN IF NOT EXISTS shelf_life VARCHAR(32)');
expect(migrationSrc).toContain('tenant_template');
expect(migrationSrc).toContain('RAISE EXCEPTION');
});
it('弹窗:字段换成「保质期」,不再手填有效期', () => {
expect(modalSrc).toContain("t('inventory.adjust.shelfLife')");
expect(modalSrc).not.toContain("t('inventory.adjust.expiry')");
expect(modalSrc).toContain('payload.shelf_life = adjustShelfLife;');
});
});