import { randomUUID } from 'node:crypto'; import { identityApp, type IdentityApp } from './helpers/identity-app'; import { secondActor } from './helpers/commerce'; import { createBatch as makeBatch, seedActiveBom } from './helpers/production'; describe('production batch API', () => { let ctx: IdentityApp; const auth = { type: 'bearer' as const }; const createActiveBom = () => seedActiveBom(ctx); const createBatch = (bomId: string, plannedQuantity = 20) => makeBatch(ctx, bomId, plannedQuantity); beforeAll(async () => { ctx = await identityApp(); }, 60000); afterAll(async () => { await ctx?.close(); }); beforeEach(async () => { await ctx.clearLimits(); }); it('tracks a batch from plan through completion with a stable BOM and exact yield', async () => { const { bomId, variantId } = await createActiveBom(); const created = await createBatch(bomId).expect(201); expect(created.body).toMatchObject({ status: 'DRAFT', plannedQuantity: 20, }); expect(created.body.plannedMaterials[0].plannedQuantity).toBe('250.000'); const started = await ctx .api() .post(`/api/v1/production-batches/${created.body.id}/start`) .auth(ctx.token, auth) .expect(201); expect(started.body.status).toBe('IN_PROGRESS'); const completed = await ctx .api() .post(`/api/v1/production-batches/${created.body.id}/complete`) .auth(ctx.token, auth) .send({ goodQuantity: 18, rejectedQuantity: 2, rejectionReason: 'Surface defects', }) .expect(201); expect(completed.body.completion).toMatchObject({ goodQuantity: 18, rejectedQuantity: 2, }); const listed = await ctx .api() .get(`/api/v1/production-batches?status=COMPLETED&variantId=${variantId}`) .auth(ctx.token, auth) .expect(200); expect(listed.body.map((row: { id: string }) => row.id)).toContain( created.body.id, ); const repeated = await ctx .api() .post(`/api/v1/production-batches/${created.body.id}/complete`) .auth(ctx.token, auth) .send({ goodQuantity: 20, rejectedQuantity: 0 }) .expect(409); expect(repeated.body.code).toBe('BATCH_COMPLETE_STATE'); expect( await ctx.db.auditEvent.count({ where: { organizationId: ctx.owner.organizationId, action: 'batch.completed', }, }), ).toBe(1); }); it('enforces reference uniqueness, BOM yield and active BOM requirements', async () => { const { bomId } = await createActiveBom(); const mismatch = await createBatch(bomId, 11).expect(409); expect(mismatch.body.code).toBe('BATCH_YIELD_MISMATCH'); const reference = `B-${randomUUID().slice(0, 8)}`; const input = { bomId, reference, plannedQuantity: 10 }; await ctx .api() .post('/api/v1/production-batches') .auth(ctx.token, auth) .send(input) .expect(201); const duplicate = await ctx .api() .post('/api/v1/production-batches') .auth(ctx.token, auth) .send(input) .expect(409); expect(duplicate.body.code).toBe('BATCH_REFERENCE_CONFLICT'); const missing = await createBatch(randomUUID()).expect(404); expect(missing.body.code).toBe('BOM_NOT_FOUND'); }); it('reconciles completion and records cancellable work without stock movement', async () => { const { bomId } = await createActiveBom(); const first = await createBatch(bomId).expect(201); const early = await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/complete`) .auth(ctx.token, auth) .send({ goodQuantity: 20, rejectedQuantity: 0 }) .expect(409); expect(early.body.code).toBe('BATCH_COMPLETE_STATE'); await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/start`) .auth(ctx.token, auth) .expect(201); const mismatch = await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/complete`) .auth(ctx.token, auth) .send({ goodQuantity: 19, rejectedQuantity: 0 }) .expect(409); expect(mismatch.body.code).toBe('BATCH_QUANTITY_MISMATCH'); const missingReason = await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/complete`) .auth(ctx.token, auth) .send({ goodQuantity: 19, rejectedQuantity: 1 }) .expect(400); expect(missingReason.body.code).toBe('BATCH_REJECTION_REASON_REQUIRED'); const cancelled = await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/cancel`) .auth(ctx.token, auth) .send({ reason: 'Production halted' }) .expect(201); expect(cancelled.body).toMatchObject({ status: 'CANCELLED', cancellation: { reason: 'Production halted' }, }); const repeated = await ctx .api() .post(`/api/v1/production-batches/${first.body.id}/cancel`) .auth(ctx.token, auth) .send({ reason: 'Again' }) .expect(409); expect(repeated.body.code).toBe('BATCH_CANCEL_STATE'); }); it('separates read/manage permission and organization scope', async () => { const { bomId } = await createActiveBom(); const batch = await createBatch(bomId).expect(201); const reader = await secondActor(ctx, true, ['production.read']); await ctx .api() .get(`/api/v1/production-batches/${batch.body.id}`) .auth(reader.token, auth) .expect(200); await ctx .api() .post(`/api/v1/production-batches/${batch.body.id}/start`) .auth(reader.token, auth) .expect(403); const outsider = await secondActor(ctx, false, [ 'production.read', 'production.manage', ]); const hidden = await ctx .api() .get(`/api/v1/production-batches/${batch.body.id}`) .auth(outsider.token, auth) .expect(404); expect(hidden.body.code).toBe('BATCH_NOT_FOUND'); }); it('enforces the rejection reason in PostgreSQL as well as the API', async () => { const { bomId } = await createActiveBom(); const batch = await createBatch(bomId).expect(201); await ctx .api() .post(`/api/v1/production-batches/${batch.body.id}/start`) .auth(ctx.token, auth) .expect(201); await ctx.db.productionBatch.update({ where: { id: batch.body.id }, data: { status: 'COMPLETED' }, }); await expect( ctx.db.productionBatchCompletion.create({ data: { batchId: batch.body.id, organizationId: ctx.owner.organizationId, completedById: ctx.owner.userId, goodQuantity: 19, rejectedQuantity: 1, }, }), ).rejects.toThrow(); }); });