r23-gaps.test.ts
TypeScript · 92 рядків
1// R-23 §11: P7 — екран помилки за замовчуванням; P3 — літерали в мапінгу; P4 — значення за замовчуванням у моделях;
2// P5 — батьківські дані (parent.*) у пакетній задачі.
3import test, { after, before } from 'node:test'
4import assert from 'node:assert/strict'
5import { buildMappedData, deriveMappingSchema, validateMapping } from '../shared/mapping.ts'
6import { validateTaskConfig, withDefaults } from '../shared/task.ts'
7import { validateModel } from '../server/models.ts'
8import { validateWorkflow } from '../server/validation.ts'
9import { demoModels } from '../server/demo.ts'
10import { edge, node, publishWorkflow, testServer } from './helpers.ts'
11
12test('P3: { "$value": … } is a constant in a mapping, with a literal type', () => {
13 const mapping = { type: { $value: 'SUPPLY_RECEIVED' }, blank: { $value: { sku: '', qty: 0 } }, id: 'data.id' }
14 assert.deepEqual(validateMapping(mapping, { id: 'string' }).errors, [])
15 assert.deepEqual(buildMappedData(mapping, { id: 'A' }), { type: 'SUPPLY_RECEIVED', blank: { sku: '', qty: 0 }, id: 'A' })
16 assert.deepEqual(deriveMappingSchema(mapping, { id: 'string' }), { type: 'string', blank: { sku: 'string', qty: 'integer' }, id: 'string' })
17})
18
19test('P4: model defaults are checked against the schema and fill a task context the mapping does not give', () => {
20 const models = demoModels()
21 assert.deepEqual(validateModel({ name: 'Ctx', schema: { id: 'string', qty: 'integer' }, defaults: { qty: 0 } }, models).errors, [])
22 const bad = validateModel({ name: 'Ctx', schema: { id: 'string', qty: 'integer' }, defaults: { qty: 'many', extra: 1 } }, models).errors
23 assert.ok(bad.some((e) => e.includes('qty must be an integer')), bad.join('\n'))
24 assert.ok(bad.some((e) => e.includes('defaults.extra: no such field')), bad.join('\n'))
25 assert.deepEqual(withDefaults({ id: 'A' }, { qty: 0, id: 'X' }), { id: 'A', qty: 0 })
26 const config = { assigneeType: 'group', assignee: 'g', taskType: 'T', title: 't', payloadMapping: { id: 'data.id' } }
27 const context = { id: 'string', qty: 'integer' }
28 assert.ok(validateTaskConfig(config, { id: 'string' }, false, {}, context).errors.some((e) => e.includes('data.qty')))
29 assert.deepEqual(validateTaskConfig(config, { id: 'string' }, false, {}, context, { qty: 0 }).errors, [])
30})
31
32test('P5: a batch task sees the item as data.* and the surrounding flow data as parent.*', () => {
33 const config = {
34 assigneeType: 'group', assignee: 'g', taskType: 'T', itemsPath: 'data.pallets', entityIdPath: 'data.id',
35 title: 'Pallet {{data.id}} of {{parent.orderId}}', payloadMapping: { orderId: 'parent.orderId', id: 'data.id' },
36 }
37 const schema = { orderId: 'string', pallets: [{ id: 'string' }] }
38 assert.deepEqual(validateTaskConfig(config, schema, false, {}, { orderId: 'string', id: 'string' }).errors, [])
39 const wrong = validateTaskConfig({ ...config, payloadMapping: { orderId: 'parent.nope', id: 'data.id' } }, schema).errors
40 assert.ok(wrong.some((e) => e.includes('parent.nope')), wrong.join('\n'))
41})
42
43test('P7: an API_REQUEST without a connected error output is a warning, not a publishing blocker', () => {
44 const workflow = {
45 id: 'w', name: 'W',
46 nodes: [
47 node('start', 'EVENT', { event: 'GO', activation: 'START', modelId: 'demo-warehouse-event' }, ['next'], []),
48 node('api', 'API_REQUEST', { method: 'GET', url: '/x' }, ['ok', 'error']),
49 node('screen', 'SCREEN', { kind: 'k', text: 'done', expect: 'none', tone: 'ok' }, []),
50 ],
51 edges: [edge('start', 'next', 'api'), edge('api', 'ok', 'screen')],
52 }
53 const { issues } = validateWorkflow(workflow, demoModels(), [workflow])
54 const warning = issues.find((issue) => issue.nodeId === 'api')
55 assert.equal(warning?.severity, 'warning')
56 assert.match(warning.message, /default error screen/)
57})
58
59let t: Awaited<ReturnType<typeof testServer>>
60before(async () => { t = await testServer('r23gaps') })
61after(async () => { await t.stop() })
62
63test('P7: a failed pass of a scanner workflow answers with the error screen and leaves the task unchanged', async () => {
64 await publishWorkflow(t.call, {
65 id: 'scan-handler', name: 'SCAN HANDLER', settings: { errorScreen: { text: 'Failed at {{data.error.block}}: {{data.error.message}}' } },
66 nodes: [
67 node('scan', 'EVENT', { event: 'SCAN', activation: 'TASK', modelId: 'demo-warehouse-event' }, ['next'], []),
68 node('mark', 'ACTION', { set: { touched: true } }),
69 { ...node('wms', 'API_REQUEST', { method: 'POST', url: '/stub/wms/containers/close', requestMapping: {} }, ['ok', 'error']), name: 'WMS CLOSE' },
70 node('ok', 'SCREEN', { kind: 'done', text: 'closed', expect: 'none', tone: 'ok' }, []),
71 ],
72 edges: [edge('scan', 'next', 'mark'), edge('mark', 'next', 'wms'), edge('wms', 'ok', 'ok')],
73 })
74 await publishWorkflow(t.call, {
75 name: 'START',
76 nodes: [
77 node('go', 'EVENT', { event: 'GO', activation: 'START', modelId: 'demo-warehouse-event' }, ['next'], []),
78 node('task', 'TASK', { handlerWorkflowId: 'scan-handler', entityType: 'box', entityIdPath: 'data.entityId', assigneeType: 'group', assignee: 'g', taskType: 'T', title: 't', payloadMapping: { $copy: 'data' } }, ['next', 'task'], ['data']),
79 ],
80 edges: [edge('go', 'next', 'task', 'data')],
81 })
82 await t.call('POST', '/api/events?responseMode=sync', { event: 'GO', entityId: 'B-9' })
83 const r = await t.call('POST', '/api/events?responseMode=sync', { event: 'SCAN', entityType: 'box', entityId: 'B-9' })
84 assert.equal(r.status, 200)
85 assert.equal(r.body.screen.tone, 'error')
86 assert.equal(r.body.screen.text, 'Failed at WMS CLOSE: API 409: container undefined is not open')
87 assert.equal(r.body.error.code, 'HTTP_409')
88 // контекст невдалого проходу не зберігається — повтор дії почнеться з того самого стану
89 const [task] = await t.sql.query<{ data: any }>(`SELECT data FROM workflow_tasks WHERE entity_id = 'B-9'`)
90 assert.equal(task.data.context.touched, undefined)
91})
92