manicanldes-backend/test/batch-cost.spec.ts

186 lines
5.8 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
import {
completeCostBatch,
packageCostBatch,
prepareCostBatch,
recordCostUsage,
setCostRate,
startCostBatch,
} from './helpers/batch-cost';
describe('batch cost snapshots API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const post = (batchId: string, body: object) =>
ctx
.api()
.post(`/api/v1/production-batches/${batchId}/cost-snapshot`)
.auth(ctx.token, auth)
.send(body);
const get = (batchId: string) =>
ctx
.api()
.get(`/api/v1/production-batches/${batchId}/cost-snapshot`)
.auth(ctx.token, auth);
const ready = async () => {
const batch = await prepareCostBatch(ctx);
await startCostBatch(ctx, batch.batchId);
await recordCostUsage(
ctx,
batch.batchId,
batch.productionLineId,
'CONSUMED',
'200.000',
);
await recordCostUsage(
ctx,
batch.batchId,
batch.productionLineId,
'WASTED',
'10.000',
);
await completeCostBatch(ctx, batch.batchId);
await packageCostBatch(
ctx,
batch.batchId,
batch.packagingBomId,
batch.packagingLineId,
);
await setCostRate(ctx, batch.productionMaterialId, '0.1000');
await setCostRate(ctx, batch.packagingMaterialId, '2.0000');
return batch;
};
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('freezes actual production and packaging cost at selected rate revisions', async () => {
const batch = await ready();
const body = { currency: 'INR', idempotencyKey: randomUUID() };
const created = await post(batch.batchId, body).expect(201);
expect(created.body).toMatchObject({
productionTotal: '21.00',
packagingTotal: '40.00',
total: '61.00',
});
expect(created.body.lines).toHaveLength(2);
await setCostRate(ctx, batch.productionMaterialId, '0.2000');
const saved = await get(batch.batchId).expect(200);
expect(saved.body.total).toBe('61.00');
expect(
saved.body.lines.find(
(line: { kind: string }) => line.kind === 'PRODUCTION',
),
).toMatchObject({
rateRevision: 1,
usedQuantity: '200.000',
wastedQuantity: '10.000',
});
const retry = await post(batch.batchId, body).expect(201);
expect(retry.body.id).toBe(created.body.id);
});
it('requires completed usage, packaging and cost rates', async () => {
const batch = await prepareCostBatch(ctx);
const body = { currency: 'INR', idempotencyKey: randomUUID() };
const draft = await post(batch.batchId, body).expect(409);
expect(draft.body.code).toBe('BATCH_COST_STATE');
await startCostBatch(ctx, batch.batchId);
await completeCostBatch(ctx, batch.batchId);
const missingPackage = await post(batch.batchId, body).expect(409);
expect(missingPackage.body.code).toBe('BATCH_COST_PACKAGING_MISSING');
await packageCostBatch(
ctx,
batch.batchId,
batch.packagingBomId,
batch.packagingLineId,
);
const missingUsage = await post(batch.batchId, body).expect(409);
expect(missingUsage.body.code).toBe('BATCH_COST_USAGE_INCOMPLETE');
});
it('rejects missing rates, changed retries, duplicate snapshots and other organizations', async () => {
const batch = await ready();
const body = { currency: 'USD', idempotencyKey: randomUUID() };
const missing = await post(batch.batchId, body).expect(409);
expect(missing.body.code).toBe('COST_RATE_MISSING');
const reader = await secondActor(ctx, true, ['cost.read']);
await ctx
.api()
.post(`/api/v1/production-batches/${batch.batchId}/cost-snapshot`)
.auth(reader.token, auth)
.send({ currency: 'INR', idempotencyKey: randomUUID() })
.expect(403);
const created = await post(batch.batchId, {
...body,
currency: 'INR',
}).expect(201);
const changed = await post(batch.batchId, body).expect(409);
expect(changed.body.code).toBe('BATCH_COST_REPLAY_CONFLICT');
const duplicate = await post(batch.batchId, {
currency: 'INR',
idempotencyKey: randomUUID(),
}).expect(409);
expect(duplicate.body.code).toBe('BATCH_COST_ALREADY_RECORDED');
const outsider = await secondActor(ctx, false, [
'cost.read',
'cost.manage',
]);
await ctx
.api()
.get(`/api/v1/production-batches/${batch.batchId}/cost-snapshot`)
.auth(outsider.token, auth)
.expect(404);
expect((await get(batch.batchId).expect(200)).body.id).toBe(
created.body.id,
);
});
it('costs a fully rejected batch without a packaging run', async () => {
const batch = await prepareCostBatch(ctx);
await startCostBatch(ctx, batch.batchId);
await recordCostUsage(
ctx,
batch.batchId,
batch.productionLineId,
'WASTED',
'50.000',
);
await completeCostBatch(ctx, batch.batchId, 0);
await setCostRate(ctx, batch.productionMaterialId, '0.1000');
const snapshot = await post(batch.batchId, {
currency: 'INR',
idempotencyKey: randomUUID(),
}).expect(201);
expect(snapshot.body).toMatchObject({
goodQuantity: 0,
productionTotal: '5.00',
packagingTotal: '0.00',
total: '5.00',
});
});
it('keeps snapshots and lines immutable in PostgreSQL', async () => {
const batch = await ready();
const snapshot = await post(batch.batchId, {
currency: 'INR',
idempotencyKey: randomUUID(),
}).expect(201);
await expect(
ctx.db.batchCostSnapshot.update({
where: { id: snapshot.body.id },
data: { total: '1.00' },
}),
).rejects.toThrow();
});
});