manicanldes-backend/test/packaging-run.spec.ts

188 lines
6.1 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
import { createBatch, seedActiveBom } from './helpers/production';
describe('packaging execution API', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const seed = async () => {
const production = await seedActiveBom(ctx);
const material = await ctx
.api()
.post('/api/v1/materials')
.auth(ctx.token, auth)
.send({
code: `P-${randomUUID().slice(0, 8)}`,
name: 'Gift box',
kind: 'PACKAGING',
unit: 'PIECE',
})
.expect(201);
const bom = await ctx
.api()
.post(`/api/v1/variants/${production.variantId}/packaging-boms`)
.auth(ctx.token, auth)
.send({
outputQuantity: 10,
lines: [{ materialId: material.body.id, quantity: '10.000' }],
})
.expect(201);
await ctx
.api()
.post(`/api/v1/packaging-boms/${bom.body.id}/activate`)
.auth(ctx.token, auth)
.expect(201);
const batch = await createBatch(ctx, production.bomId).expect(201);
return {
batchId: batch.body.id as string,
bomId: bom.body.id as string,
lineId: bom.body.lines[0].id as string,
};
};
const complete = async (batchId: string, goodQuantity = 20) => {
await ctx
.api()
.post(`/api/v1/production-batches/${batchId}/start`)
.auth(ctx.token, auth)
.expect(201);
await ctx
.api()
.post(`/api/v1/production-batches/${batchId}/complete`)
.auth(ctx.token, auth)
.send({
goodQuantity,
rejectedQuantity: 20 - goodQuantity,
...(goodQuantity < 20 ? { rejectionReason: 'Damaged' } : {}),
})
.expect(201);
};
const input = (bomId: string, lineId: string) => ({
bomId,
idempotencyKey: randomUUID(),
lines: [
{
bomLineId: lineId,
usedQuantity: '19.000',
wastedQuantity: '1.000',
wasteReason: 'Torn',
},
],
});
const record = (batchId: string, body: object) =>
ctx
.api()
.post(`/api/v1/production-batches/${batchId}/packaging`)
.auth(ctx.token, auth)
.send(body);
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('records immutable packaging use and exact plan variance', async () => {
const { batchId, bomId, lineId } = await seed();
await complete(batchId);
const body = input(bomId, lineId);
const first = await record(batchId, body).expect(201);
expect(first.body.lines[0]).toMatchObject({
plannedQuantity: '20.000',
usedQuantity: '19.000',
wastedQuantity: '1.000',
varianceQuantity: '0.000',
});
const retry = await record(batchId, body).expect(201);
expect(retry.body.id).toBe(first.body.id);
const found = await ctx
.api()
.get(`/api/v1/production-batches/${batchId}/packaging`)
.auth(ctx.token, auth)
.expect(200);
expect(found.body.id).toBe(first.body.id);
});
it('rejects wrong state, mismatched BOM, missing lines and waste reason', async () => {
const { batchId, bomId, lineId } = await seed();
const draft = await record(batchId, input(bomId, lineId)).expect(409);
expect(draft.body.code).toBe('PACKAGING_BATCH_STATE');
await complete(batchId);
const wrongBom = await record(batchId, input(randomUUID(), lineId)).expect(
409,
);
expect(wrongBom.body.code).toBe('PACKAGING_BOM_MISMATCH');
const wrongLines = await record(batchId, {
...input(bomId, lineId),
lines: [
{
bomLineId: randomUUID(),
usedQuantity: '1.000',
wastedQuantity: '0.000',
},
],
}).expect(400);
expect(wrongLines.body.code).toBe('PACKAGING_LINES_MISMATCH');
const noReason = await record(batchId, {
...input(bomId, lineId),
lines: [
{ bomLineId: lineId, usedQuantity: '1.000', wastedQuantity: '1.000' },
],
}).expect(400);
expect(noReason.body.code).toBe('PACKAGING_WASTE_REASON_REQUIRED');
});
it('rejects output that cannot be scaled to the packaging BOM yield', async () => {
const { batchId, bomId, lineId } = await seed();
await complete(batchId, 19);
const response = await record(batchId, input(bomId, lineId)).expect(409);
expect(response.body.code).toBe('PACKAGING_YIELD_MISMATCH');
});
it('enforces one record, idempotency payload, permission and organization scope', async () => {
const { batchId, bomId, lineId } = await seed();
await complete(batchId);
const reader = await secondActor(ctx, true, ['packaging.read']);
await ctx
.api()
.post(`/api/v1/production-batches/${batchId}/packaging`)
.auth(reader.token, auth)
.send(input(bomId, lineId))
.expect(403);
const outsider = await secondActor(ctx, false, ['packaging.read']);
await ctx
.api()
.get(`/api/v1/production-batches/${batchId}/packaging`)
.auth(outsider.token, auth)
.expect(404);
const body = input(bomId, lineId);
await record(batchId, body).expect(201);
const changed = await record(batchId, {
...body,
lines: [{ ...body.lines[0], usedQuantity: '18.000' }],
}).expect(409);
expect(changed.body.code).toBe('PACKAGING_REPLAY_CONFLICT');
const duplicate = await record(batchId, input(bomId, lineId)).expect(409);
expect(duplicate.body.code).toBe('PACKAGING_ALREADY_RECORDED');
});
it('keeps packaging records immutable in PostgreSQL', async () => {
const { batchId, bomId, lineId } = await seed();
await complete(batchId);
const run = await record(batchId, input(bomId, lineId)).expect(201);
const line = await ctx.db.packagingRunLine.findFirstOrThrow({
where: { runId: run.body.id },
});
await expect(
ctx.db.packagingRunLine.update({
where: { id: line.id },
data: { usedQuantity: '1.000' },
}),
).rejects.toThrow();
});
});