|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * FlowNodeInspector — a `connector_action` node's input values are value slots |
| 5 | + * (objectstack#28164). |
| 6 | + * |
| 7 | + * The spec's expression ledger roots `connector_action` `input.*` in the node's |
| 8 | + * `connectorConfig` sibling block, and `registerFlow` refuses a `{…}` token |
| 9 | + * there. The form gives each input value what every other value slot has: the |
| 10 | + * text / CEL-expression toggle, and the spec's refusal beside the value as it |
| 11 | + * is typed, read from the same judge the publish door calls. Driven through the |
| 12 | + * real render path, for both editors the Input section uses: a typed field |
| 13 | + * (the action declares an `inputSchema`) and the key/value repeater (it |
| 14 | + * declares none). |
| 15 | + */ |
| 16 | + |
| 17 | +import * as React from 'react'; |
| 18 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 19 | +import { render, screen, cleanup, fireEvent, waitFor, within } from '@testing-library/react'; |
| 20 | +import { flowNodeValueTemplateRefusals } from '@objectstack/spec/automation'; |
| 21 | + |
| 22 | +vi.mock('../previews/useFlowNodePalette', async (orig) => ({ |
| 23 | + ...(await orig<Record<string, unknown>>()), |
| 24 | + useActionConfigSchemas: () => ({}), |
| 25 | + useFlowNodePalette: () => [], |
| 26 | +})); |
| 27 | +vi.mock('../previews/useObjectFields', () => ({ |
| 28 | + useObjectFields: () => ({ fields: [], loading: false, error: null }), |
| 29 | +})); |
| 30 | + |
| 31 | +import { FlowNodeInspector } from './FlowNodeInspector'; |
| 32 | +import type { MetadataSelection } from '../preview-registry'; |
| 33 | + |
| 34 | +afterEach(() => { |
| 35 | + cleanup(); |
| 36 | + vi.unstubAllGlobals(); |
| 37 | +}); |
| 38 | + |
| 39 | +const TOGGLE = 'Write as a CEL expression'; |
| 40 | + |
| 41 | +const SLACK_POST_MESSAGE = { |
| 42 | + type: 'object', |
| 43 | + required: ['channel'], |
| 44 | + properties: { |
| 45 | + channel: { type: 'string', description: 'Channel id, user id, or #name' }, |
| 46 | + text: { type: 'string', description: 'Message text' }, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +function mockRegistry(actions: unknown[]) { |
| 51 | + vi.stubGlobal( |
| 52 | + 'fetch', |
| 53 | + vi.fn(async () => |
| 54 | + new Response( |
| 55 | + JSON.stringify({ |
| 56 | + success: true, |
| 57 | + data: { connectors: [{ name: 'slack', label: 'Slack', type: 'saas', origin: 'plugin', state: 'ready', actions }] }, |
| 58 | + }), |
| 59 | + { status: 200, headers: { 'Content-Type': 'application/json' } }, |
| 60 | + ), |
| 61 | + ), |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function connectorNode(input: Record<string, unknown>, actionId = 'chat.postMessage') { |
| 66 | + return { id: 'post', type: 'connector_action', label: 'Post to Slack', connectorConfig: { connectorId: 'slack', actionId, input } }; |
| 67 | +} |
| 68 | + |
| 69 | +const SELECTION: MetadataSelection = { kind: 'node', id: 'post' }; |
| 70 | + |
| 71 | +/** The inspector over a draft that takes each patch, as the designer host does. */ |
| 72 | +function Host({ initial, onPatch }: { initial: Record<string, unknown>; onPatch: (patch: Record<string, unknown>) => void }) { |
| 73 | + const [draft, setDraft] = React.useState(initial); |
| 74 | + return ( |
| 75 | + <FlowNodeInspector |
| 76 | + type="flow" |
| 77 | + name="notify_flow" |
| 78 | + selection={SELECTION} |
| 79 | + draft={draft} |
| 80 | + onPatch={(patch: Record<string, unknown>) => { |
| 81 | + onPatch(patch); |
| 82 | + setDraft((d) => ({ ...d, ...patch })); |
| 83 | + }} |
| 84 | + onClearSelection={vi.fn()} |
| 85 | + locale="en-US" |
| 86 | + readOnly={false} |
| 87 | + /> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function renderInspector(input: Record<string, unknown>, actionId?: string) { |
| 92 | + const onPatch = vi.fn(); |
| 93 | + const initial = { nodes: [{ id: 'start', type: 'start' }, connectorNode(input, actionId)], edges: [{ source: 'start', target: 'post' }] }; |
| 94 | + render(<Host initial={initial} onPatch={onPatch} />); |
| 95 | + return { onPatch }; |
| 96 | +} |
| 97 | + |
| 98 | +/** The spec's refusal of `input` on the node, at `connectorConfig.input.KEY`. */ |
| 99 | +function specRefusal(input: Record<string, unknown>, key: string): string { |
| 100 | + const node = connectorNode(input); |
| 101 | + const found = flowNodeValueTemplateRefusals('connector_action', {}, node).find( |
| 102 | + (r) => r.block === 'connectorConfig' && r.path === `input.${key}`, |
| 103 | + ); |
| 104 | + expect(found, `the spec refuses connectorConfig.input.${key}`).toBeDefined(); |
| 105 | + return found!.message; |
| 106 | +} |
| 107 | + |
| 108 | +/** The control group a typed field's label heads. */ |
| 109 | +function typedField(labelText: string): HTMLElement { |
| 110 | + const group = screen.getByText(labelText, { selector: 'label' }).parentElement; |
| 111 | + if (!group) throw new Error(`no group under the "${labelText}" label`); |
| 112 | + return group; |
| 113 | +} |
| 114 | + |
| 115 | +function lastInput(onPatch: ReturnType<typeof vi.fn>): unknown { |
| 116 | + expect(onPatch).toHaveBeenCalled(); |
| 117 | + const patch = onPatch.mock.calls.at(-1)![0] as { nodes?: Array<Record<string, unknown>> }; |
| 118 | + const node = (patch.nodes ?? []).find((n) => n.id === 'post')!; |
| 119 | + return (node.connectorConfig as Record<string, unknown>).input; |
| 120 | +} |
| 121 | + |
| 122 | +describe('#28164 — a typed connector input is a value slot', () => { |
| 123 | + it('shows the spec’s refusal in the form when a `{…}` token is typed into the value', async () => { |
| 124 | + mockRegistry([{ key: 'chat.postMessage', label: 'Post Message', inputSchema: SLACK_POST_MESSAGE }]); |
| 125 | + const { onPatch } = renderInspector({ channel: 'C123' }); |
| 126 | + |
| 127 | + await screen.findByText('Text', { selector: 'label' }); |
| 128 | + const field = typedField('Text'); |
| 129 | + expect(within(field).queryByRole('alert')).toBeNull(); |
| 130 | + fireEvent.change(within(field).getByRole('textbox'), { target: { value: 'Hello {record.name}' } }); |
| 131 | + |
| 132 | + expect(lastInput(onPatch)).toEqual({ channel: 'C123', text: 'Hello {record.name}' }); |
| 133 | + const alert = await within(typedField('Text')).findByRole('alert'); |
| 134 | + expect(alert.textContent).toContain(specRefusal({ channel: 'C123', text: 'Hello {record.name}' }, 'text')); |
| 135 | + }); |
| 136 | + |
| 137 | + it('shows the refusal for a stored token, and none for a plain literal', async () => { |
| 138 | + mockRegistry([{ key: 'chat.postMessage', label: 'Post Message', inputSchema: SLACK_POST_MESSAGE }]); |
| 139 | + const stored = { channel: 'C123', text: 'Hi {name}' }; |
| 140 | + renderInspector(stored); |
| 141 | + |
| 142 | + await screen.findByText('Text', { selector: 'label' }); |
| 143 | + expect(within(typedField('Text')).getByRole('alert').textContent).toContain(specRefusal(stored, 'text')); |
| 144 | + expect(within(typedField('Channel')).queryByRole('alert')).toBeNull(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('offers the expression toggle, which stores a CEL value envelope at the same key', async () => { |
| 148 | + mockRegistry([{ key: 'chat.postMessage', label: 'Post Message', inputSchema: SLACK_POST_MESSAGE }]); |
| 149 | + const { onPatch } = renderInspector({ channel: 'C123' }); |
| 150 | + |
| 151 | + await screen.findByText('Text', { selector: 'label' }); |
| 152 | + const field = typedField('Text'); |
| 153 | + fireEvent.change(within(field).getByRole('textbox'), { target: { value: "'Hello ' + record.name" } }); |
| 154 | + fireEvent.click(within(field).getByRole('button', { name: TOGGLE })); |
| 155 | + |
| 156 | + expect(lastInput(onPatch)).toEqual({ channel: 'C123', text: { dialect: 'cel', source: "'Hello ' + record.name" } }); |
| 157 | + expect(within(typedField('Text')).queryByRole('alert')).toBeNull(); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe('#28164 — the Input repeater’s values are value slots', () => { |
| 162 | + it('shows the spec’s refusal under a repeater value holding a `{…}` token, and offers the toggle', async () => { |
| 163 | + mockRegistry([{ key: 'plain', label: 'Plain' }]); |
| 164 | + const stored = { channel: 'C123', note: 'Hi {name}' }; |
| 165 | + renderInspector(stored, 'plain'); |
| 166 | + |
| 167 | + await screen.findByDisplayValue('note'); |
| 168 | + const alert = await screen.findByRole('alert'); |
| 169 | + expect(alert.textContent).toContain(specRefusal(stored, 'note')); |
| 170 | + expect(screen.getAllByRole('button', { name: TOGGLE })).toHaveLength(2); |
| 171 | + }); |
| 172 | + |
| 173 | + it('a token typed into a repeater value is refused in place', async () => { |
| 174 | + mockRegistry([{ key: 'plain', label: 'Plain' }]); |
| 175 | + renderInspector({ channel: 'C123' }, 'plain'); |
| 176 | + |
| 177 | + const cell = await screen.findByDisplayValue('C123'); |
| 178 | + expect(screen.queryByRole('alert')).toBeNull(); |
| 179 | + fireEvent.change(cell, { target: { value: '{record.channel}' } }); |
| 180 | + fireEvent.blur(cell); |
| 181 | + |
| 182 | + await waitFor(() => |
| 183 | + expect(screen.getByRole('alert').textContent).toContain(specRefusal({ channel: '{record.channel}' }, 'channel')), |
| 184 | + ); |
| 185 | + }); |
| 186 | +}); |
0 commit comments