49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import { summarizeMaterialUsage } from '../src/production/material-usage-summary';
|
|
import { materialUsageSchema } from '../src/production/material-usage.schemas';
|
|
import { randomUUID } from 'node:crypto';
|
|
|
|
describe('material usage arithmetic and input', () => {
|
|
const line = {
|
|
id: randomUUID(),
|
|
materialId: randomUUID(),
|
|
materialName: 'Wax',
|
|
materialCode: 'WAX',
|
|
unit: 'GRAM',
|
|
quantity: '125.000',
|
|
};
|
|
it('uses exact three-decimal arithmetic and shows negative variance', () => {
|
|
const summary = summarizeMaterialUsage(
|
|
[line],
|
|
[
|
|
{ bomLineId: line.id, kind: 'CONSUMED', quantity: '250.001' },
|
|
{ bomLineId: line.id, kind: 'WASTED', quantity: '0.125' },
|
|
],
|
|
2,
|
|
);
|
|
expect(summary[0]).toMatchObject({
|
|
plannedQuantity: '250.000',
|
|
consumedQuantity: '250.001',
|
|
wastedQuantity: '0.125',
|
|
varianceQuantity: '-0.126',
|
|
});
|
|
});
|
|
it('rejects zero, excessive precision and unknown fields', () => {
|
|
const input = {
|
|
bomLineId: line.id,
|
|
kind: 'CONSUMED',
|
|
quantity: '1.000',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
expect(materialUsageSchema.safeParse(input).success).toBe(true);
|
|
expect(
|
|
materialUsageSchema.safeParse({ ...input, quantity: '0.000' }).success,
|
|
).toBe(false);
|
|
expect(
|
|
materialUsageSchema.safeParse({ ...input, quantity: '1.0001' }).success,
|
|
).toBe(false);
|
|
expect(
|
|
materialUsageSchema.safeParse({ ...input, extra: true }).success,
|
|
).toBe(false);
|
|
});
|
|
});
|