import { NfcManufacturingService, ManufacturingBatchStatus, CreateManufacturingBatchDto, calculateBatchYieldRate, canTransitionBatchStatus, } from '../../../src/modules/nfc/service/manufacturing'; describe('Point 45: 国际实体卡供应链生产批次工单生命周期与良率检验', () => { let service: NfcManufacturingService; beforeEach(() => { service = new NfcManufacturingService(); }); describe('1. 生产工单状态机流转规则 (canTransitionBatchStatus)', () => { it('应允许合法正向生命周期流转: draft -> ordered -> manufacturing -> qa_inspected -> in_stock', () => { expect(canTransitionBatchStatus('draft', 'ordered')).toBe(true); expect(canTransitionBatchStatus('ordered', 'manufacturing')).toBe(true); expect(canTransitionBatchStatus('manufacturing', 'qa_inspected')).toBe(true); expect(canTransitionBatchStatus('qa_inspected', 'in_stock')).toBe(true); }); it('质检不合格可流转为 rejected 废品或返工', () => { expect(canTransitionBatchStatus('qa_inspected', 'rejected')).toBe(true); expect(canTransitionBatchStatus('manufacturing', 'cancelled')).toBe(true); expect(canTransitionBatchStatus('draft', 'cancelled')).toBe(true); }); it('逆向或跨阶段非法流转应坚决拒绝', () => { expect(canTransitionBatchStatus('in_stock', 'draft')).toBe(false); expect(canTransitionBatchStatus('draft', 'in_stock')).toBe(false); expect(canTransitionBatchStatus('rejected', 'in_stock')).toBe(false); }); }); describe('2. calculateBatchYieldRate (出厂质检良率与损耗统计)', () => { it('应精确计算生产良品率 (Yield Rate)', () => { // 计划生产 1000 张,实际合格 985 张,报废 15 张 const result = calculateBatchYieldRate(1000, 985, 15); expect(result.yieldRate).toBe(0.985); expect(result.defectRate).toBe(0.015); expect(result.isQualified).toBe(true); // 默认合格线 >= 95% }); it('良率低于阈值 (如 95%) 时应标记为未达标', () => { const result = calculateBatchYieldRate(1000, 920, 80, 0.95); expect(result.yieldRate).toBe(0.92); expect(result.isQualified).toBe(false); expect(result.defectCount).toBe(80); }); it('非法入参处理 (如数量为负或超过总数) 应安全抛出异常', () => { expect(() => calculateBatchYieldRate(0, 0, 0)).toThrow(); expect(() => calculateBatchYieldRate(100, 105, 0)).toThrow(); expect(() => calculateBatchYieldRate(100, -5, 0)).toThrow(); }); }); describe('3. NfcManufacturingService 批次工单管理', () => { it('应能创建生产批次并初始化为 draft 状态', () => { const batchDto: CreateManufacturingBatchDto = { batchNo: 'MFG-2026-Q4-001', materialType: 'MATTE_BLACK_METAL', // 磨砂黑金属卡 targetQuantity: 500, factoryName: 'Shenzhen Smart Chip Technology Co.', chipModel: 'NTAG216', cardFinishing: 'GOLD_LASER_ENGRAVING', // 激光金雕 }; const batch = service.createBatch(batchDto); expect(batch.id).toBeDefined(); expect(batch.batchNo).toBe('MFG-2026-Q4-001'); expect(batch.status).toBe('draft'); expect(batch.targetQuantity).toBe(500); expect(batch.qualifiedQuantity).toBe(0); }); it('更新生产状态并在达到 qa_inspected 时记录质检指标', () => { const batch = service.createBatch({ batchNo: 'MFG-2026-Q4-002', materialType: 'ECO_BAMBOO_WOOD', // 环保竹木卡 targetQuantity: 200, factoryName: 'EcoCard Global Ltd.', chipModel: 'NTAG213', }); // 流转为 ordered -> manufacturing service.updateBatchStatus(batch.batchNo, 'ordered'); service.updateBatchStatus(batch.batchNo, 'manufacturing'); // 质检录入 const updated = service.recordBatchInspection(batch.batchNo, 196, 4); expect(updated.status).toBe('qa_inspected'); expect(updated.qualifiedQuantity).toBe(196); expect(updated.defectQuantity).toBe(4); expect(updated.yieldRate).toBe(0.98); // 入库 const inStock = service.updateBatchStatus(batch.batchNo, 'in_stock'); expect(inStock.status).toBe('in_stock'); }); }); });