Files
SupplierSystem/server/tests/inventoryShelfLife.test.ts
T
雨天 33f2f7b75a
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(库存): 修改弹窗改录保质期,有效期由生产日期+保质期派生
新增 inventory.shelf_life VARCHAR(32)(迁移 20261042,遍历 public/tenant_template/所有 tenant_*),存保质期原文(7天/6个月/1年)。

库存管理「修改」不再手填有效期:只要动了保质期或生产日期,就用 deriveExpiryDate 重算 expiry_date(推不出则清空)。解析与推算提到 server/src/utils/shelfLife.ts 与入库链路共用(月=30天、年=360天)。

入库链路(batchStockInboundRows)随行落 shelf_life 原文(原先算完即弃);列表行映射补 shelf_life;弹窗字段与三语文案同步。不做历史回填。
2026-10-13 13:42:15 +08:00

100 lines
4.4 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 {
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('')).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;');
});
});