|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A screen asks for a string and the node after it writes it (#31491). |
| 5 | + * |
| 6 | + * The reproduction: the showcase reassign wizard (`screen` → `update_record`). |
| 7 | + * A resume that sent an object for its `text` field passed the screen-input |
| 8 | + * door, the resume consumed the suspension (#13937's shape-4 order), and the |
| 9 | + * record write refused the object — the run ended `failed`, and a corrected |
| 10 | + * resume answered `RUN_NOT_FOUND`. A number was stored as text instead. |
| 11 | + * |
| 12 | + * Ruling A+ (maintainer, 2026-11-08): `text`, `textarea`, `email`, `url` and |
| 13 | + * `phone` are string value domains at the door. Pinned here, at the engine, |
| 14 | + * for each kind: |
| 15 | + * - an object and a number are refused `INVALID_SCREEN_INPUT` before anything |
| 16 | + * is written, and the pause survives; |
| 17 | + * - a corrected resume then completes the run and writes the string; |
| 18 | + * - a missing value is refused as `required`, as before, and the pause survives. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect } from 'vitest'; |
| 22 | +import { AutomationEngine } from '../engine.js'; |
| 23 | +import { installBuiltinNodes } from './index.js'; |
| 24 | + |
| 25 | +function silentLogger(): any { |
| 26 | + const l: any = { info() {}, warn() {}, error() {}, debug() {} }; |
| 27 | + l.child = () => l; |
| 28 | + return l; |
| 29 | +} |
| 30 | + |
| 31 | +/** A data engine that records every insert's object and fields. */ |
| 32 | +function fakeData() { |
| 33 | + const inserts: Array<{ obj: string; fields: Record<string, unknown> }> = []; |
| 34 | + const data: any = { |
| 35 | + async insert(obj: string, fields: Record<string, unknown>) { |
| 36 | + inserts.push({ obj, fields }); |
| 37 | + return { id: `${obj}_1` }; |
| 38 | + }, |
| 39 | + }; |
| 40 | + return { data, inserts }; |
| 41 | +} |
| 42 | + |
| 43 | +const CEL = (source: string) => ({ dialect: 'cel', source }); |
| 44 | + |
| 45 | +/** start → screen (one required field of `type`) → create_record (writes it) → end. */ |
| 46 | +function askFlow(type: string) { |
| 47 | + return { |
| 48 | + name: 'ask_string', |
| 49 | + label: 'Ask a string', |
| 50 | + type: 'screen', |
| 51 | + status: 'active', |
| 52 | + runAs: 'system', |
| 53 | + nodes: [ |
| 54 | + { id: 'start', type: 'start', label: 'Start' }, |
| 55 | + { |
| 56 | + id: 'ask', type: 'screen', label: 'Ask', |
| 57 | + config: { fields: [{ name: 'answer', label: 'Answer', type, required: true }] }, |
| 58 | + }, |
| 59 | + { |
| 60 | + id: 'write', type: 'create_record', label: 'Write', |
| 61 | + config: { objectName: 'reply', fields: { answer: CEL('answer') } }, |
| 62 | + }, |
| 63 | + { id: 'end', type: 'end', label: 'End' }, |
| 64 | + ], |
| 65 | + edges: [ |
| 66 | + { id: 'e1', source: 'start', target: 'ask' }, |
| 67 | + { id: 'e2', source: 'ask', target: 'write' }, |
| 68 | + { id: 'e3', source: 'write', target: 'end' }, |
| 69 | + ], |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +function makeEngine(type: string) { |
| 74 | + const { data, inserts } = fakeData(); |
| 75 | + const engine = new AutomationEngine(silentLogger()); |
| 76 | + installBuiltinNodes(engine, { logger: silentLogger(), getService: (n: string) => (n === 'data' ? data : undefined) } as any); |
| 77 | + engine.registerFlow('ask_string', askFlow(type) as any); |
| 78 | + return { engine, inserts }; |
| 79 | +} |
| 80 | + |
| 81 | +describe('a screen string field refuses a non-string before the pause is consumed (#31491, ruling A+)', () => { |
| 82 | + for (const type of ['text', 'textarea', 'email', 'url', 'phone']) { |
| 83 | + it(`\`${type}\`: an object and a number are refused, the pause survives, and a corrected resume completes`, async () => { |
| 84 | + const { engine, inserts } = makeEngine(type); |
| 85 | + const started = await engine.execute('ask_string', {} as any); |
| 86 | + expect(started.status).toBe('paused'); |
| 87 | + const runId = started.runId!; |
| 88 | + |
| 89 | + for (const bad of [{ x: 1 }, 42]) { |
| 90 | + const refused = await engine.resume(runId, { variables: { answer: bad } }); |
| 91 | + expect(refused.success, JSON.stringify(bad)).toBe(false); |
| 92 | + expect(refused.code).toBe('INVALID_SCREEN_INPUT'); |
| 93 | + expect(refused.error).toContain('Screen field "answer" must be a string'); |
| 94 | + expect(inserts).toEqual([]); |
| 95 | + expect(await engine.hasSuspendedRun(runId)).toBe(true); |
| 96 | + } |
| 97 | + |
| 98 | + const resumed = await engine.resume(runId, { variables: { answer: 'alice' } }); |
| 99 | + expect(resumed.success, resumed.error).toBe(true); |
| 100 | + expect(inserts).toEqual([{ obj: 'reply', fields: { answer: 'alice' } }]); |
| 101 | + expect(await engine.hasSuspendedRun(runId)).toBe(false); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + it('a missing value is refused as `required`, as before, and the pause survives', async () => { |
| 106 | + const { engine, inserts } = makeEngine('text'); |
| 107 | + const { runId } = await engine.execute('ask_string', {} as any); |
| 108 | + const refused = await engine.resume(runId!, { variables: {} }); |
| 109 | + expect(refused.code).toBe('INVALID_SCREEN_INPUT'); |
| 110 | + expect(refused.error).toContain('Screen field "answer" is required'); |
| 111 | + expect(inserts).toEqual([]); |
| 112 | + expect(await engine.hasSuspendedRun(runId!)).toBe(true); |
| 113 | + }); |
| 114 | +}); |
0 commit comments