decision-screen.test.ts
TypeScript · 82 рядків
1// DECISION (таблиця рішень) і SCREEN (екран ТСД) — R-23 §10.
2import test from 'node:test'
3import assert from 'node:assert/strict'
4import { decisionOutputSchema, decisionPorts, runDecision } from '../shared/decision.ts'
5import { screenBody, validateScreen } from '../shared/screen.ts'
6import { validateWorkflowDataFlow } from '../server/data-flow.ts'
7import { outputSchema } from '../src/dataFlow.js'
8import { demoModels } from '../server/demo.ts'
9import { edge, node } from './helpers.ts'
10
11const table = {
12 resultPath: 'dest',
13 rows: [
14 { label: 'extra', when: 'data.found == false', port: 'extra', set: { destination: 'none', target: '' } },
15 { label: 'reserve', when: 'data.reserve != ""', set: { destination: 'crossdock', target: '=data.reserve' } },
16 { label: 'otherwise', when: '', set: { destination: 'storage', target: 'normal' } },
17 ],
18}
19
20test('DECISION: first matching row writes its values and rule name; ports follow the rows', () => {
21 assert.deepEqual(decisionPorts(table), ['extra', 'next'])
22 const data: any = { found: true, reserve: 'C-501' }
23 assert.deepEqual(runDecision(table, data), { port: 'next', row: 1 })
24 assert.deepEqual(data.dest, { destination: 'crossdock', target: 'C-501', rule: 'reserve' })
25 const other: any = { found: true, reserve: '' }
26 assert.equal(runDecision(table, other).row, 2)
27 assert.equal(other.dest.rule, 'otherwise')
28 const extra: any = { found: false, reserve: 'C-501' }
29 assert.equal(runDecision(table, extra).port, 'extra')
30})
31
32test('DECISION without an "otherwise" row gets a none output', () => {
33 const config = { resultPath: 'r', rows: [{ when: 'data.x > 1', set: { v: 1 } }] }
34 assert.deepEqual(decisionPorts(config), ['next', 'none'])
35 assert.deepEqual(runDecision(config, { x: 0 }), { port: 'none', row: -1 })
36})
37
38test('DECISION validation: result structure, unknown fields, missing values, type conflicts, fallback position', () => {
39 const ok = decisionOutputSchema(table, { found: 'boolean', reserve: 'string' })
40 assert.deepEqual(ok.errors, [])
41 assert.deepEqual(ok.schema.dest, { destination: 'string', target: 'string', rule: 'string' })
42 const bad = decisionOutputSchema({
43 resultPath: 'dest',
44 rows: [
45 { label: 'a', when: '', set: { v: 1 } },
46 { label: 'b', when: 'data.missing == 1', set: { v: 'text', w: 2 } },
47 ],
48 }, { x: 'integer' })
49 assert.ok(bad.errors.some((e) => e.includes('must be last')), bad.errors.join('\n'))
50 assert.ok(bad.errors.some((e) => e.includes('Unknown field: data.missing')), bad.errors.join('\n'))
51 assert.ok(bad.errors.some((e) => e.includes('row a: no value for "w"')), bad.errors.join('\n'))
52 assert.ok(bad.errors.some((e) => e.includes('column "v"')), bad.errors.join('\n'))
53})
54
55test('DECISION in a workflow: later blocks see data.<resultPath>; editor derives the same; ports must match rows', () => {
56 const models = [...demoModels(), { id: 'facts', name: 'Facts', schema: { event: 'string', found: 'boolean', reserve: 'string' } }]
57 const workflow = {
58 nodes: [
59 node('start', 'EVENT', { event: 'GO', activation: 'START', modelId: 'facts' }, ['next'], []),
60 node('dest', 'DECISION', table, ['extra', 'next']),
61 node('use', 'CONDITION', { routes: { cross: 'data.dest.destination == "crossdock" && data.dest.rule != ""', other: '' } }, ['cross', 'other']),
62 ],
63 edges: [edge('start', 'next', 'dest'), edge('dest', 'next', 'use')],
64 }
65 assert.deepEqual(validateWorkflowDataFlow(workflow, models, [workflow]), [])
66 assert.equal(outputSchema(workflow, 'dest', 'next', models).dest.target, 'string')
67 const wrongPorts = { ...workflow, nodes: workflow.nodes.map((n: any) => n.id === 'dest' ? { ...n, outputs: [{ id: 'next', name: 'next' }] } : n) }
68 assert.ok(validateWorkflowDataFlow(wrongPorts, models, [wrongPorts]).some((e) => e.includes('output ports must match the rows')))
69})
70
71test('SCREEN: template and expression text, qty / value, buttons; unknown template fields are errors', () => {
72 const config = { kind: 'put', text: '{{data.name}}: put {{data.qty}} into {{data.box}}', expect: 'qty', tone: 'info', qty: '=data.qty', value: '=data.box', buttons: ['Damaged'] }
73 assert.deepEqual(screenBody(config, { data: { name: 'Disc', qty: 4, box: 'B-1' } }), {
74 screen: { kind: 'put', text: 'Disc: put 4 into B-1', expect: 'qty', tone: 'info', qty: 4, value: 'B-1', buttons: ['Damaged'] },
75 })
76 assert.equal(screenBody({ ...config, text: '="Left: " + (data.qty - 1)' }, { data: { qty: 4, name: '', box: '' } }).screen.text, 'Left: 3')
77 assert.deepEqual(validateScreen(config, { data: { name: 'string', qty: 'integer', box: 'string' } }), [])
78 const errors = validateScreen({ ...config, text: 'put {{data.nope}}', expect: 'anything' }, { data: { name: 'string', qty: 'integer', box: 'string' } })
79 assert.ok(errors.some((e) => e.includes('{{data.nope}}')), errors.join('\n'))
80 assert.ok(errors.some((e) => e.includes('expect must be one of')), errors.join('\n'))
81})
82