r23-receiving.test.ts
TypeScript · 80 рядків
1// R-23 §9: приймання від А до Я, зібране з наявних блоків, і виправлення, які воно виявило — перевірка графа має
2// бачити дані так само, як їх бачить рушій.
3import test, { after, before } from 'node:test'
4import assert from 'node:assert/strict'
5import { validateWorkflowDataFlow } from '../server/data-flow.ts'
6import { outputSchema } from '../src/dataFlow.js'
7import { demoModels } from '../server/demo.ts'
8import { actionOutputSchema } from '../shared/action.ts'
9import { load } from '../scripts/r23/receiving.ts'
10import { edge, node, testServer } from './helpers.ts'
11
12const models = [
13 ...demoModels(),
14 { id: 'shelf', name: 'Shelf', schema: { code: 'string', free: 'integer' } },
15 { id: 'scan', name: 'Scan', schema: { event: 'string', data: { code: 'string', qty: 'integer' } } },
16]
17
18test('API_REQUEST response is checked at data.<resultPath>, where the engine puts it', () => {
19 const workflow = {
20 nodes: [
21 node('start', 'EVENT', { event: 'GO', activation: 'START', modelId: 'demo-warehouse-event' }, ['next'], []),
22 node('api', 'API_REQUEST', { method: 'GET', url: '/shelf', modelId: 'shelf', resultPath: 'putaway.shelf' }, ['ok', 'error']),
23 node('use', 'CONDITION', { routes: { free: 'data.putaway.shelf.free > 0', full: '' } }, ['free', 'full']),
24 node('root', 'CONDITION', { routes: { wrong: 'data.free > 0', other: '' } }, ['wrong', 'other']),
25 ],
26 edges: [edge('start', 'next', 'api'), edge('api', 'ok', 'use'), edge('api', 'ok', 'root')],
27 }
28 const issues = validateWorkflowDataFlow(workflow, models, [workflow])
29 assert.ok(!issues.some((issue) => issue.includes('data.putaway.shelf.free')), issues.join('\n'))
30 assert.ok(issues.some((issue) => issue.includes('Unknown field: data.free')), issues.join('\n'))
31 // редактор показує ту саму форму
32 assert.deepEqual(outputSchema(workflow, 'api', 'ok', models).putaway.shelf, { code: 'string', free: 'integer' })
33})
34
35test('START event with a data envelope adds event.data fields to data; system event fields are always in event.*', () => {
36 const workflow = {
37 nodes: [
38 node('start', 'EVENT', { event: 'SCAN', activation: 'START', modelId: 'scan' }, ['next'], []),
39 node('route', 'CONDITION', { routes: { many: 'data.qty > 1 && event.data.code != "" && event.eventId != ""', one: '' } }, ['many', 'one']),
40 ],
41 edges: [edge('start', 'next', 'route')],
42 }
43 assert.deepEqual(validateWorkflowDataFlow(workflow, models, [workflow]), [])
44 assert.equal(outputSchema(workflow, 'start', 'next', models).code, 'string')
45})
46
47test('ACTION object literal keeps its shape for later blocks and task contracts', () => {
48 const { schema, errors } = actionOutputSchema({ set: { blank: { sku: '', qty: 0, price: 1.5, tags: [] } } }, {})
49 assert.deepEqual(errors, [])
50 assert.deepEqual(schema.blank, { sku: 'string', qty: 'integer', price: 'number', tags: 'any[]' })
51})
52
53let t: Awaited<ReturnType<typeof testServer>>
54before(async () => { t = await testServer('r23') })
55after(async () => { await t.stop() })
56
57test('receiving processes publish; the engine sends an idempotency key, so a repeated scan does not receive twice', async () => {
58 assert.deepEqual(await load(t.call), [])
59 const fact = await t.call('POST', '/stub/erp/supply-order', {
60 orderId: 'SO-1', truck: 'T1', pallets: [{ palletId: 'P-1', lines: [{ sku: 'BD-FEBI-01', qty: 4 }] }],
61 })
62 assert.equal(fact.status, 200)
63 // факт WMS іде в чергу (async); без диспетчера обробляємо його тут
64 for (let i = 0; i < 10 && await t.flow.processAccepted(); i += 1);
65 const scan = await t.call('POST', '/api/events?responseMode=sync', { event: 'ITEM_SCANNED', eventId: 's1', entityType: 'pallet', entityId: 'P-1', data: { code: '4820000004011' } })
66 assert.equal(scan.body.screen.kind, 'put')
67 assert.match(scan.body.screen.text, /4 шт у B-001/)
68 const qty = { event: 'QTY_CONFIRMED', eventId: 'q1', entityType: 'pallet', entityId: 'P-1', data: { qty: 4 } }
69 const first = await t.call('POST', '/api/events?responseMode=sync', qty)
70 const repeat = await t.call('POST', '/api/events?responseMode=sync', qty)
71 assert.deepEqual(repeat.body, first.body)
72 // ключ ставить рушій: подія + блок + відвідування; у мапінгах процесу ключа немає
73 const keys = (await t.sql.query<{ key: string }>(`SELECT key FROM wms_stub.ops WHERE key LIKE 'q1:%'`)).map((r) => r.key)
74 assert.deepEqual(keys, ['q1:receive:1'])
75 const state = (await t.call('GET', '/stub/state')).body
76 assert.equal(state.lines[0].received, 4)
77 assert.deepEqual(state.containers[0].items, [{ sku: 'BD-FEBI-01', qty: 4, placed: 0 }])
78 assert.equal(state.containers[0].destination, 'crossdock')
79})
80