transfer.test.ts
TypeScript · 37 рядків
1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { PACK, shape, stockOf, useLab } from '../../test/lab.ts';
4
5const lab = useLab();
6
7test('переміщення: коробка зі складу 1 → розсип на складі 2 з тим самим строком', async () => {
8 const p = await lab.product();
9 await lab.receive(p.id, { lines: [{ packType: PACK.box, packSize: 10, packCount: 2 }], expiryDate: '2028-01-31' });
10 const r = await lab.api.post('/api/transfers', { fromWarehouseId: 1, toWarehouseId: 2, toLocationId: 20101, lines: [{ productId: p.id, qty: 10 }] });
11 assert.equal(r.receivedUnits, 10);
12 const s = await lab.state(p.id);
13 assert.deepEqual([stockOf(s, 1).quantity, stockOf(s, 2).quantity], [10, 10]);
14 assert.deepEqual(shape(s, 2), ['0:1×10']);
15 assert.equal((s.items.find((i: any) => i.warehouse_id === 2) as any).expiry_date, '2028-01-31');
16 assert.deepEqual(s.transactions.map((t: any) => t.type).sort(), [1, 8, 9]);
17});
18
19test('переміщення на той самий склад — 422 TRANSFER_SAME_WAREHOUSE', async () => {
20 const p = await lab.product();
21 await assert.rejects(lab.api.post('/api/transfers', { fromWarehouseId: 1, toWarehouseId: 1, toLocationId: 10101, lines: [{ productId: p.id, qty: 1 }] }),
22 { status: 422, code: 'TRANSFER_SAME_WAREHOUSE' });
23});
24
25test('зустрічні переміщення 1→2 і 2→1 тих самих товарів — без взаємних блокувань', async () => {
26 const a = await lab.product();
27 const b = await lab.product();
28 for (const w of [1, 2]) for (const p of [a, b]) await lab.receive(p.id, { lines: [{ packType: PACK.unit, packSize: 1, packCount: 100 }] }, w);
29 const before = (await lab.api.get('/api/stats', { top: 1 })).database.deadlocks;
30 await Promise.all(Array.from({ length: 40 }, (_, i) => lab.api.post('/api/transfers', {
31 fromWarehouseId: i % 2 ? 1 : 2, toWarehouseId: i % 2 ? 2 : 1, toLocationId: 10101,
32 lines: i % 4 < 2 ? [{ productId: a.id, qty: 1 }, { productId: b.id, qty: 1 }] : [{ productId: b.id, qty: 1 }, { productId: a.id, qty: 1 }],
33 })));
34 const after = (await lab.api.get('/api/stats', { top: 1 })).database.deadlocks;
35 assert.equal(after - before, 0, 'взаємних блокувань не має бути: єдиний порядок (warehouse_id, product_id)');
36});
37