|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A screen flow ends on a result screen that links the records it created |
| 5 | + * (ruling B on the screen-flow result screen). |
| 6 | + * |
| 7 | + * The flow writes two records, then pauses on a screen of two |
| 8 | + * `type: 'record_link'` fields whose `defaultValue` envelopes read the created |
| 9 | + * records' ids. Pinned here, at the engine: |
| 10 | + * - the paused screen carries each field typed `record_link`, with its |
| 11 | + * `reference` and its `defaultValue` already evaluated to the id; |
| 12 | + * - a resume carrying a value under a link field's name is refused with |
| 13 | + * `INVALID_SCREEN_INPUT`, nothing after the screen runs, and the pause |
| 14 | + * survives; |
| 15 | + * - the resume that leaves the links out completes, and the flow variable the |
| 16 | + * id came from is the record the run created — the account link is named |
| 17 | + * after that very variable (`account`), so a bound refused value would have |
| 18 | + * replaced the record with a string and the audit write after the screen |
| 19 | + * would not read the created id. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect } from 'vitest'; |
| 23 | +import { AutomationEngine } from '../engine.js'; |
| 24 | +import { installBuiltinNodes } from './index.js'; |
| 25 | + |
| 26 | +function silentLogger(): any { |
| 27 | + const l: any = { info() {}, warn() {}, error() {}, debug() {} }; |
| 28 | + l.child = () => l; |
| 29 | + return l; |
| 30 | +} |
| 31 | + |
| 32 | +/** A data engine that mints `<object>_<n>` ids and records every insert. */ |
| 33 | +function fakeData() { |
| 34 | + const inserts: Array<{ obj: string; fields: Record<string, unknown> }> = []; |
| 35 | + const data: any = { |
| 36 | + async insert(obj: string, fields: Record<string, unknown>) { |
| 37 | + inserts.push({ obj, fields }); |
| 38 | + return { id: `${obj}_${inserts.length}`, ...fields }; |
| 39 | + }, |
| 40 | + }; |
| 41 | + return { data, inserts }; |
| 42 | +} |
| 43 | + |
| 44 | +const CEL = (source: string) => ({ dialect: 'cel', source }); |
| 45 | + |
| 46 | +/** start → create account → create contact → result screen (two links) → audit write → end. */ |
| 47 | +function convertFlow() { |
| 48 | + return { |
| 49 | + name: 'convert_with_result', |
| 50 | + label: 'Convert with result screen', |
| 51 | + type: 'screen', |
| 52 | + status: 'active', |
| 53 | + runAs: 'system', |
| 54 | + nodes: [ |
| 55 | + { id: 'start', type: 'start', label: 'Start' }, |
| 56 | + { id: 'acc', type: 'create_record', label: 'Account', config: { objectName: 'crm_account', fields: { name: 'Acme' }, outputVariable: 'account' } }, |
| 57 | + { id: 'con', type: 'create_record', label: 'Contact', config: { objectName: 'crm_contact', fields: { name: 'Ada' }, outputVariable: 'contact' } }, |
| 58 | + { |
| 59 | + id: 'result', type: 'screen', label: 'Created', |
| 60 | + config: { |
| 61 | + fields: [ |
| 62 | + { name: 'account', label: 'Account', type: 'record_link', reference: 'crm_account', defaultValue: CEL('account.id') }, |
| 63 | + { name: 'contact_link', label: 'Contact', type: 'record_link', reference: 'crm_contact', defaultValue: CEL('contact.id') }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + }, |
| 67 | + { id: 'audit', type: 'create_record', label: 'Audit', config: { objectName: 'audit', fields: { account: CEL('account.id') } } }, |
| 68 | + { id: 'end', type: 'end', label: 'End' }, |
| 69 | + ], |
| 70 | + edges: [ |
| 71 | + { id: 'e1', source: 'start', target: 'acc' }, |
| 72 | + { id: 'e2', source: 'acc', target: 'con' }, |
| 73 | + { id: 'e3', source: 'con', target: 'result' }, |
| 74 | + { id: 'e4', source: 'result', target: 'audit' }, |
| 75 | + { id: 'e5', source: 'audit', target: 'end' }, |
| 76 | + ], |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function makeEngine() { |
| 81 | + const { data, inserts } = fakeData(); |
| 82 | + const engine = new AutomationEngine(silentLogger()); |
| 83 | + installBuiltinNodes(engine, { logger: silentLogger(), getService: (n: string) => (n === 'data' ? data : undefined) } as any); |
| 84 | + engine.registerFlow('convert_with_result', convertFlow() as any); |
| 85 | + return { engine, inserts }; |
| 86 | +} |
| 87 | + |
| 88 | +describe('a screen flow ends on a screen that links the records it created', () => { |
| 89 | + it('pauses with both fields typed `record_link`, each carrying its object and the created id', async () => { |
| 90 | + const { engine, inserts } = makeEngine(); |
| 91 | + const started = await engine.execute('convert_with_result', {} as any); |
| 92 | + expect(started.status).toBe('paused'); |
| 93 | + expect(started.screen?.fields).toEqual([ |
| 94 | + expect.objectContaining({ name: 'account', type: 'record_link', reference: 'crm_account', defaultValue: 'crm_account_1' }), |
| 95 | + expect.objectContaining({ name: 'contact_link', type: 'record_link', reference: 'crm_contact', defaultValue: 'crm_contact_2' }), |
| 96 | + ]); |
| 97 | + expect(started.screen?.fields.every((f) => f.required === false)).toBe(true); |
| 98 | + expect(inserts.map((i) => i.obj)).toEqual(['crm_account', 'crm_contact']); |
| 99 | + }); |
| 100 | + |
| 101 | + it('refuses a resume carrying a link field\'s value, runs nothing after the screen, and keeps the pause', async () => { |
| 102 | + const { engine, inserts } = makeEngine(); |
| 103 | + const { runId } = await engine.execute('convert_with_result', {} as any); |
| 104 | + |
| 105 | + const refused = await engine.resume(runId!, { variables: { account: 'crm_account_999' } }); |
| 106 | + expect(refused.success).toBe(false); |
| 107 | + expect(refused.code).toBe('INVALID_SCREEN_INPUT'); |
| 108 | + expect(refused.error).toContain('Screen field "account" shows a record and takes no value'); |
| 109 | + expect(inserts.map((i) => i.obj)).toEqual(['crm_account', 'crm_contact']); |
| 110 | + |
| 111 | + // The refusal consumed nothing, and bound nothing: the clean resume |
| 112 | + // completes and the next node reads `account.id` — the record the run |
| 113 | + // created, not the string the refused resume sent under the same name. |
| 114 | + const resumed = await engine.resume(runId!, { variables: {} }); |
| 115 | + expect(resumed.success, resumed.error).toBe(true); |
| 116 | + expect(inserts[2]).toEqual({ obj: 'audit', fields: { account: 'crm_account_1' } }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments