import { AppError } from '../src/common/errors/app-error'; import { calculateBatchCost } from '../src/costing/batch-cost-calculation'; const decimal = (value: string) => ({ toString: () => value, toFixed: (digits: number) => Number(value).toFixed(digits), }); describe('batch cost arithmetic', () => { it('sums production and packaging quantities, then prices each line exactly', () => { const production = { id: 'wax-line', bomId: 'production-bom', materialId: 'wax', }; const packaging = { id: 'box-line', bomId: 'packaging-bom', materialId: 'box', }; const report = calculateBatchCost( [production], [ { bomLineId: production.id, kind: 'CONSUMED', quantity: decimal('200.000'), }, { bomLineId: production.id, kind: 'WASTED', quantity: decimal('10.000'), }, ], [ { bomLineId: packaging.id, bomLine: packaging, usedQuantity: decimal('19.000'), wastedQuantity: decimal('1.000'), }, ], new Map([ ['wax', { id: 'wax-rate', unitCost: decimal('0.1000') }], ['box', { id: 'box-rate', unitCost: decimal('2.0000') }], ]), ); expect(report).toMatchObject({ productionTotal: '21.00', packagingTotal: '40.00', total: '61.00', }); expect(report.lines[0]).toMatchObject({ usedQuantity: '200.000', wastedQuantity: '10.000', lineCost: '21.00', }); }); it('requires usage for every production line', () => { expect(() => calculateBatchCost( [{ id: 'wax-line', bomId: 'bom', materialId: 'wax' }], [], [], new Map(), ), ).toThrow(AppError); }); });