techtools
Головна / research / lab / stock-lab / src / domain / lot.test.ts

lot.test.ts

TypeScript · 54 рядків

1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { PackType } from './codes.ts';
4import { DomainError } from './errors.ts';
5import { formatLocation, parseLocation } from './location.ts';
6import { planLot } from './lot.ts';
7
8const box = (packSize: number, packCount: number) => ({ packType: PackType.box, packSize, packCount });
9const unit = (packCount: number) => ({ packType: PackType.unit, packSize: 1, packCount });
10const code = (f: () => unknown) => {
11  try {
12    f();
13  } catch (e) {
14    return (e as DomainError).code;
15  }
16  return 'OK';
17};
18
19test('пакування + розсип: розсип іде в постійний запис', () => {
20  const p = planLot({ lines: [box(10, 2), unit(3), unit(4)] });
21  assert.equal(p.total, 27);
22  assert.equal(p.looseUnits, 7, 'два рядки розсипу зводяться в один');
23  assert.deepEqual(p.packs, [box(10, 2)]);
24});
25
26test('спайка × 3: розсип усередині — рядок вмісту, не постійний розсип', () => {
27  const p = planLot({ lines: [box(100, 9), { packType: PackType.bag, packSize: 10, packCount: 10 }, unit(5)], container: { packType: PackType.bundle, count: 3 } });
28  assert.equal(p.container?.perContainer, 1005);
29  assert.equal(p.total, 3015);
30  assert.equal(p.looseUnits, 0);
31  assert.deepEqual(p.packs.at(-1), unit(5));
32});
33
34test('серійні: кількість номерів = штук розсипом', () => {
35  assert.deepEqual(planLot({ lines: [unit(2)], serials: ['A1', 'A2'] }).serials, ['A1', 'A2']);
36  assert.equal(code(() => planLot({ lines: [unit(3)], serials: ['A1', 'A2'] })), 'LOT_SERIALS_COUNT');
37  assert.equal(code(() => planLot({ lines: [unit(2)], serials: ['A1', 'A1'] })), 'LOT_SERIALS_DUPLICATE');
38  assert.equal(code(() => planLot({ lines: [unit(1)], serials: ['A1'], container: { packType: PackType.bundle, count: 1 } })), 'LOT_SERIALS_IN_CONTAINER');
39});
40
41test('порушення правил складу партії мають власні коди', () => {
42  assert.equal(code(() => planLot({ lines: [] })), 'LOT_EMPTY');
43  assert.equal(code(() => planLot({ lines: [{ packType: PackType.bundle, packSize: 10, packCount: 1 }] })), 'LOT_PACK_TYPE');
44  assert.equal(code(() => planLot({ lines: [{ packType: PackType.unit, packSize: 5, packCount: 1 }] })), 'LOT_UNIT_SIZE');
45  assert.equal(code(() => planLot({ lines: [box(10, 1)], container: { packType: PackType.box, count: 1 } })), 'LOT_CONTAINER_TYPE');
46});
47
48test('адреса A-01-03 ↔ 10103', () => {
49  assert.equal(parseLocation('a-01-03'), 10103);
50  assert.equal(formatLocation(10103), 'A-01-03');
51  assert.equal(formatLocation(parseLocation('Z-99-99')), 'Z-99-99');
52  assert.equal(code(() => parseLocation('A1')), 'LOCATION_FORMAT');
53});
54