manicanldes-backend/test/costing.spec.ts

143 lines
4.7 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
import { seedActiveBom } from './helpers/production';
describe('material costing API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const rate = (materialId: string, currency: string, unitCost: string) =>
ctx
.api()
.post(`/api/v1/materials/${materialId}/cost-rates`)
.auth(ctx.token, auth)
.send({ currency, unitCost });
const estimate = (bomId: string, currency = 'INR', kind = 'boms') =>
ctx
.api()
.get(`/api/v1/${kind}/${bomId}/cost-estimate?currency=${currency}`)
.auth(ctx.token, auth);
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('revises rates per currency and estimates production BOMs exactly', async () => {
const bom = await seedActiveBom(ctx);
const missing = await estimate(bom.bomId).expect(409);
expect(missing.body.code).toBe('COST_RATE_MISSING');
const first = await rate(bom.materialId, 'INR', '0.1000').expect(201);
expect(first.body.revision).toBe(1);
const initial = await estimate(bom.bomId).expect(200);
expect(initial.body).toMatchObject({
kind: 'PRODUCTION',
currency: 'INR',
estimatedTotal: '12.50',
});
expect(initial.body.lines[0].rateId).toBe(first.body.id);
const next = await rate(bom.materialId, 'INR', '0.2000').expect(201);
expect(next.body.revision).toBe(2);
const revised = await estimate(bom.bomId).expect(200);
expect(revised.body.estimatedTotal).toBe('25.00');
expect(revised.body.lines[0].rateRevision).toBe(2);
await rate(bom.materialId, 'USD', '0.0100').expect(201);
const usd = await estimate(bom.bomId, 'USD').expect(200);
expect(usd.body.estimatedTotal).toBe('1.25');
const history = await ctx
.api()
.get(
`/api/v1/materials/${bom.materialId}/cost-rates?currency=INR&limit=1`,
)
.auth(ctx.token, auth)
.expect(200);
expect(history.body).toHaveLength(1);
expect(history.body[0].revision).toBe(2);
});
it('keeps packaging and production estimates separate', async () => {
const production = await seedActiveBom(ctx);
const box = await ctx
.api()
.post('/api/v1/materials')
.auth(ctx.token, auth)
.send({
code: `P-${randomUUID().slice(0, 8)}`,
name: 'Box',
kind: 'PACKAGING',
unit: 'PIECE',
})
.expect(201);
const packaging = await ctx
.api()
.post(`/api/v1/variants/${production.variantId}/packaging-boms`)
.auth(ctx.token, auth)
.send({
outputQuantity: 10,
lines: [{ materialId: box.body.id, quantity: '10.000' }],
})
.expect(201);
await rate(box.body.id, 'INR', '2.5000').expect(201);
const result = await estimate(
packaging.body.id,
'INR',
'packaging-boms',
).expect(200);
expect(result.body).toMatchObject({
kind: 'PACKAGING',
estimatedTotal: '25.00',
});
const wrongKind = await estimate(packaging.body.id).expect(404);
expect(wrongKind.body.code).toBe('BOM_NOT_FOUND');
});
it('enforces validation, permissions and organization scope', async () => {
const bom = await seedActiveBom(ctx);
await rate(bom.materialId, 'INR', '0.0000').expect(400);
await rate(bom.materialId, 'XYZ', '1.0000').expect(400);
const reader = await secondActor(ctx, true, ['cost.read']);
await ctx
.api()
.post(`/api/v1/materials/${bom.materialId}/cost-rates`)
.auth(reader.token, auth)
.send({ currency: 'INR', unitCost: '1.0000' })
.expect(403);
await rate(bom.materialId, 'INR', '1.0000').expect(201);
await ctx
.api()
.get(`/api/v1/boms/${bom.bomId}/cost-estimate?currency=INR`)
.auth(reader.token, auth)
.expect(200);
const outsider = await secondActor(ctx, false, [
'cost.read',
'cost.manage',
]);
await ctx
.api()
.get(`/api/v1/boms/${bom.bomId}/cost-estimate?currency=INR`)
.auth(outsider.token, auth)
.expect(404);
await ctx
.api()
.get(`/api/v1/materials/${bom.materialId}/cost-rates?currency=INR`)
.auth(outsider.token, auth)
.expect(404);
});
it('keeps rate revisions immutable in PostgreSQL', async () => {
const bom = await seedActiveBom(ctx);
const created = await rate(bom.materialId, 'INR', '1.0000').expect(201);
await expect(
ctx.db.materialCostRate.update({
where: { id: created.body.id },
data: { unitCost: '2.0000' },
}),
).rejects.toThrow();
});
});