queue.test.ts
TypeScript · 72 рядків
1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { compareQueue, dueSlot, horizonLimit, matchSupply, shouldInterrupt, takeFromInbound, type WaitingLine } from './queue.ts';
4
5function rng(seed: number) {
6 let s = seed >>> 0;
7 return () => {
8 let t = (s += 0x6d2b79f5);
9 t = Math.imul(t ^ (t >>> 15), t | 1);
10 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
11 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
12 };
13}
14
15test('слот: 11:00 і 11:20 — один слот (бал вирішує), 11:31 — наступний; термін не зсувається', () => {
16 assert.equal(dueSlot('2027-01-04T11:00:00Z'), dueSlot('2027-01-04T11:20:00Z'));
17 assert.notEqual(dueSlot('2027-01-04T11:20:00Z'), dueSlot('2027-01-04T11:31:00Z'));
18 const a = { class: 1, dueSlot: dueSlot('2027-01-04T11:20:00Z'), priority: 90, dueAt: '2027-01-04T11:20:00Z', id: 2 };
19 const b = { class: 1, dueSlot: dueSlot('2027-01-04T11:00:00Z'), priority: 10, dueAt: '2027-01-04T11:00:00Z', id: 1 };
20 assert.ok(compareQueue(a, b) < 0, 'в одному слоті вищий бал — першим');
21 const c = { ...a, dueSlot: dueSlot('2027-01-04T11:31:00Z'), dueAt: '2027-01-04T11:31:00Z' };
22 assert.ok(compareQueue(b, c) < 0, 'раніший слот — першим, незалежно від балу');
23});
24
25test('повернення (клас 9) — після будь-якої задачі класу 1', () => {
26 const ret = { class: 9, dueSlot: '2027-01-01T00:00:00.000Z', priority: 100, dueAt: '2027-01-01T00:00:00Z', id: 1 };
27 const late = { class: 1, dueSlot: '2027-12-01T00:00:00.000Z', priority: 0, dueAt: '2027-12-01T00:00:00Z', id: 2 };
28 assert.ok(compareQueue(late, ret) < 0);
29});
30
31test('горизонт «роль порожня»', () => {
32 assert.equal(horizonLimit('2027-01-04T09:00:00Z', 'none'), null);
33 assert.equal(horizonLimit('2027-01-04T09:00:00Z', '5h'), '2027-01-04T14:00:00.000Z');
34 assert.equal(horizonLimit('2027-01-04T09:00:00Z', 'today'), '2027-01-05T00:00:00.000Z');
35});
36
37test('переривання — лише заради термінової задачі з ранішим слотом', () => {
38 const at = '2027-01-04T10:00:00Z';
39 assert.equal(shouldInterrupt({ dueAt: '2027-01-05T08:00:00Z' }, { dueAt: '2027-01-04T10:20:00Z' }, at), true);
40 assert.equal(shouldInterrupt({ dueAt: '2027-01-05T08:00:00Z' }, { dueAt: '2027-01-04T13:00:00Z' }, at), false, 'голова не горить');
41 assert.equal(shouldInterrupt({ dueAt: '2027-01-04T10:10:00Z' }, { dueAt: '2027-01-04T10:20:00Z' }, at), false, 'той самий слот — не перериваємо');
42 assert.equal(shouldInterrupt({ dueAt: '2027-01-05T08:00:00Z' }, null, at), false);
43});
44
45test('matchSupply: властивості на 3 000 випадкових черг', () => {
46 for (let seed = 1; seed <= 3000; seed++) {
47 const r = rng(seed);
48 const int = (a: number, b: number) => a + Math.floor(r() * (b - a + 1));
49 const supply = new Map<number, number>();
50 for (let p = 1; p <= 4; p++) if (r() < 0.8) supply.set(p, int(0, 30));
51 const waiting: WaitingLine[] = Array.from({ length: int(0, 15) }, (_, i) => ({
52 customerLineId: i + 1, orderId: int(1, 6), productId: int(1, 5), need: int(1, 8),
53 dueAt: `2027-01-04T${String(int(8, 18)).padStart(2, '0')}:00:00Z`, priority: int(0, 100),
54 }));
55 const out = matchSupply(supply, waiting);
56 for (const [p, q] of supply) assert.ok(out.filter((a) => a.productId === p).reduce((s, a) => s + a.qty, 0) <= q, `seed=${seed}: не більше поставки`);
57 for (const a of out) assert.ok(a.qty <= waiting.find((w) => w.customerLineId === a.customerLineId)!.need, `seed=${seed}: не більше потреби`);
58 // немає інверсії: рядок пізнішої черги отримав, а раніший того ж товару — ні, хоча товар лишався
59 for (const w of waiting) {
60 const got = out.find((a) => a.customerLineId === w.customerLineId)?.qty ?? 0;
61 if (got >= w.need) continue;
62 const later = waiting.filter((x) => x.productId === w.productId && (x.dueAt > w.dueAt || (x.dueAt === w.dueAt && x.priority < w.priority)));
63 for (const x of later) assert.equal(out.find((a) => a.customerLineId === x.customerLineId)?.qty ?? 0, 0, `seed=${seed}: інверсія черги`);
64 }
65 }
66});
67
68test('takeFromInbound: бере з рядків по черзі, не більше потрібного', () => {
69 assert.deepEqual(takeFromInbound([{ lineId: 1, taskId: 7, qty: 5 }, { lineId: 2, taskId: 8, qty: 10 }], 8),
70 [{ lineId: 1, taskId: 7, qty: 5 }, { lineId: 2, taskId: 8, qty: 3 }]);
71});
72