manicanldes-backend/test/material-stock-adjustments....

202 lines
6.2 KiB
TypeScript

import { randomUUID } from 'node:crypto';
import { identityApp, type IdentityApp } from './helpers/identity-app';
import { secondActor } from './helpers/commerce';
describe('manual material stock adjustments', () => {
let ctx: IdentityApp;
const auth = { type: 'bearer' as const };
const post = (body: object) =>
ctx
.api()
.post('/api/v1/inventory/material-adjustments')
.auth(ctx.token, auth)
.send(body);
const ready = async () => {
const code = randomUUID().slice(0, 8);
const material = await ctx
.api()
.post('/api/v1/materials')
.auth(ctx.token, auth)
.send({
code: `ADJ-${code}`,
name: `Adjustment wax ${code}`,
kind: 'WAX',
unit: 'GRAM',
})
.expect(201);
const warehouse = await ctx
.api()
.post('/api/v1/inventory/warehouses')
.auth(ctx.token, auth)
.send({ name: `Adjustment ${code}` })
.expect(201);
return {
materialId: material.body.id as string,
warehouseId: warehouse.body.id as string,
};
};
const request = (data: Awaited<ReturnType<typeof ready>>, delta: string) => ({
...data,
delta,
reason: 'Counted physical inventory',
idempotencyKey: randomUUID(),
});
beforeAll(async () => {
ctx = await identityApp();
}, 60000);
afterAll(async () => {
await ctx?.close();
});
beforeEach(async () => {
await ctx.clearLimits();
});
it('creates positive stock, deducts a later correction and keeps signed ledger entries', async () => {
const data = await ready();
const add = request(data, '1.250');
const first = await post(add).expect(201);
expect(first.body).toMatchObject({
materialId: data.materialId,
warehouseId: data.warehouseId,
delta: '1.250',
reason: add.reason,
});
expect((await post(add).expect(201)).body.id).toBe(first.body.id);
const second = await post(request(data, '-0.250')).expect(201);
const stock = await ctx.db.materialStockItem.findUniqueOrThrow({
where: { id: first.body.stockItemId },
});
expect(stock.onHand.toFixed(3)).toBe('1.000');
const entries = await ctx.db.materialStockLedger.findMany({
where: {
stockItemId: stock.id,
kind: { in: ['ADJUSTMENT_IN', 'ADJUSTMENT_OUT'] },
},
orderBy: { kind: 'asc' },
});
expect(
entries.map((entry) => [entry.kind, entry.delta.toFixed(3)]),
).toEqual([
['ADJUSTMENT_IN', '1.250'],
['ADJUSTMENT_OUT', '-0.250'],
]);
expect(
(
await ctx
.api()
.get(`/api/v1/inventory/material-adjustments/${second.body.id}`)
.auth(ctx.token, auth)
.expect(200)
).body.ledgerEntryId,
).toBe(entries[1].id);
const listing = await ctx
.api()
.get(
`/api/v1/inventory/material-adjustments?materialId=${data.materialId}`,
)
.auth(ctx.token, auth)
.expect(200);
expect(listing.body).toHaveLength(2);
});
it('rejects missing stock, insufficient stock and capacity overflow without posting', async () => {
const data = await ready();
expect((await post(request(data, '-0.001')).expect(404)).body.code).toBe(
'MATERIAL_ADJUSTMENT_STOCK_NOT_FOUND',
);
await post(request(data, '1.000')).expect(201);
expect((await post(request(data, '-1.001')).expect(409)).body.code).toBe(
'MATERIAL_ADJUSTMENT_INSUFFICIENT',
);
const stock = await ctx.db.materialStockItem.findFirstOrThrow({
where: { materialId: data.materialId, warehouseId: data.warehouseId },
});
await ctx.db.materialStockItem.update({
where: { id: stock.id },
data: { onHand: '999999999.999' },
});
expect((await post(request(data, '0.001')).expect(409)).body.code).toBe(
'MATERIAL_ADJUSTMENT_CAPACITY',
);
expect(
await ctx.db.materialStockAdjustment.count({
where: { materialId: data.materialId },
}),
).toBe(1);
expect(
(
await ctx.db.materialStockItem.findUniqueOrThrow({
where: { id: stock.id },
})
).onHand.toFixed(3),
).toBe('999999999.999');
});
it('requires a reason and nonzero quantity and rejects a changed retry', async () => {
const data = await ready();
await post(request(data, '0.000')).expect(400);
await post(request(data, '-0.000')).expect(400);
await post({ ...request(data, '1.000'), reason: ' ' }).expect(400);
const body = request(data, '1.000');
await post(body).expect(201);
expect(
(await post({ ...body, reason: 'Different stock count' }).expect(409))
.body.code,
).toBe('MATERIAL_ADJUSTMENT_REPLAY_CONFLICT');
});
it('enforces adjustment permission and hides another organization', async () => {
const data = await ready();
const body = request(data, '1.000');
const reader = await secondActor(ctx, true, [
'inventory.read',
'inventory.manage',
]);
await ctx
.api()
.post('/api/v1/inventory/material-adjustments')
.auth(reader.token, auth)
.send(body)
.expect(403);
const adjustment = await post(body).expect(201);
const outsider = await secondActor(ctx, false, [
'inventory.read',
'inventory.adjust',
]);
await ctx
.api()
.get(`/api/v1/inventory/material-adjustments/${adjustment.body.id}`)
.auth(outsider.token, auth)
.expect(404);
await ctx
.api()
.get('/api/v1/inventory/material-adjustments')
.auth(outsider.token, auth)
.expect(200, []);
await ctx
.api()
.post('/api/v1/inventory/material-adjustments')
.auth(outsider.token, auth)
.send(request(data, '1.000'))
.expect(404);
});
it('keeps adjustment records and linked ledger entries immutable', async () => {
const data = await ready();
const adjustment = await post(request(data, '1.000')).expect(201);
await expect(
ctx.db.materialStockAdjustment.update({
where: { id: adjustment.body.id },
data: { reason: 'Changed reason' },
}),
).rejects.toThrow();
await expect(
ctx.db.materialStockLedger.update({
where: { id: adjustment.body.ledgerEntryId },
data: { delta: '2.000' },
}),
).rejects.toThrow();
});
});