|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateFlowTemplatePaths, |
| 6 | + FLOW_TEMPLATE_UNKNOWN_FIELD, |
| 7 | + FLOW_TEMPLATE_LOOKUP_TRAVERSAL, |
| 8 | +} from './validate-flow-template-paths.js'; |
| 9 | + |
| 10 | +type AnyRec = Record<string, unknown>; |
| 11 | + |
| 12 | +/** A crm_lead object with a scalar, a formula, a lookup, and a multi-lookup. */ |
| 13 | +const LEAD_OBJECT: AnyRec = { |
| 14 | + name: 'crm_lead', |
| 15 | + fields: { |
| 16 | + first_name: { name: 'first_name', type: 'text' }, |
| 17 | + last_name: { name: 'last_name', type: 'text' }, |
| 18 | + company: { name: 'company', type: 'text' }, |
| 19 | + full_name: { name: 'full_name', type: 'formula' }, |
| 20 | + crm_account: { name: 'crm_account', type: 'lookup', reference_to: 'crm_account' }, |
| 21 | + target_channels: { name: 'target_channels', type: 'lookup', reference_to: 'channel', multiple: true }, |
| 22 | + payload: { name: 'payload', type: 'json' }, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +/** Build a record-change flow with one notify node carrying the given templates. */ |
| 27 | +function flowWith(notify: AnyRec, objectName = 'crm_lead'): AnyRec { |
| 28 | + return { |
| 29 | + objects: [LEAD_OBJECT], |
| 30 | + flows: [ |
| 31 | + { |
| 32 | + name: 'notify_lead', |
| 33 | + type: 'record_change', |
| 34 | + nodes: [ |
| 35 | + { id: 'start', type: 'start', config: { objectName, triggerType: 'record-created' } }, |
| 36 | + { id: 'n1', type: 'notify', notify }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + ], |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +describe('validateFlowTemplatePaths', () => { |
| 44 | + it('flags an unknown field in a {record.<x>} template (typo)', () => { |
| 45 | + const findings = validateFlowTemplatePaths( |
| 46 | + flowWith({ title: 'New lead: {record.full_naem}', body: 'x' }), |
| 47 | + ); |
| 48 | + expect(findings).toHaveLength(1); |
| 49 | + expect(findings[0].rule).toBe(FLOW_TEMPLATE_UNKNOWN_FIELD); |
| 50 | + expect(findings[0].severity).toBe('warning'); |
| 51 | + expect(findings[0].message).toContain('full_naem'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('flags a lookup cross-object hop {record.<lookup>.<field>}', () => { |
| 55 | + const findings = validateFlowTemplatePaths( |
| 56 | + flowWith({ title: 'From {record.crm_account.name}', body: 'x' }), |
| 57 | + ); |
| 58 | + expect(findings).toHaveLength(1); |
| 59 | + expect(findings[0].rule).toBe(FLOW_TEMPLATE_LOOKUP_TRAVERSAL); |
| 60 | + expect(findings[0].message).toContain('crm_account.name'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does NOT flag a formula field (valid, hydrated since #3445)', () => { |
| 64 | + const findings = validateFlowTemplatePaths( |
| 65 | + flowWith({ title: 'New lead: {record.full_name}', body: '{record.company}' }), |
| 66 | + ); |
| 67 | + expect(findings).toHaveLength(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it('does NOT flag a plain scalar field', () => { |
| 71 | + const findings = validateFlowTemplatePaths( |
| 72 | + flowWith({ title: '{record.first_name} {record.last_name}', body: '{record.company}' }), |
| 73 | + ); |
| 74 | + expect(findings).toHaveLength(0); |
| 75 | + }); |
| 76 | + |
| 77 | + it('does NOT flag a bare lookup id (no sub-path)', () => { |
| 78 | + const findings = validateFlowTemplatePaths( |
| 79 | + flowWith({ title: 'acct {record.crm_account}', body: 'x' }), |
| 80 | + ); |
| 81 | + expect(findings).toHaveLength(0); |
| 82 | + }); |
| 83 | + |
| 84 | + it('does NOT flag a numeric index into a multiple lookup (#1872)', () => { |
| 85 | + const findings = validateFlowTemplatePaths( |
| 86 | + flowWith({ title: 'ch {record.target_channels.0}', body: 'x' }), |
| 87 | + ); |
| 88 | + expect(findings).toHaveLength(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does NOT flag a sub-path into a json field', () => { |
| 92 | + const findings = validateFlowTemplatePaths( |
| 93 | + flowWith({ title: '{record.payload.foo}', body: 'x' }), |
| 94 | + ); |
| 95 | + expect(findings).toHaveLength(0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('does NOT flag system/audit columns', () => { |
| 99 | + const findings = validateFlowTemplatePaths( |
| 100 | + flowWith({ title: '{record.id} {record.created_at} {record.owner}', body: 'x' }), |
| 101 | + ); |
| 102 | + expect(findings).toHaveLength(0); |
| 103 | + }); |
| 104 | + |
| 105 | + it('ignores non-record tokens (flow vars, NOW(), $User)', () => { |
| 106 | + const findings = validateFlowTemplatePaths( |
| 107 | + flowWith({ title: '{some_var.field} {NOW()} {$User.Email}', body: 'x' }), |
| 108 | + ); |
| 109 | + expect(findings).toHaveLength(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('skips a flow whose object is not defined in this stack', () => { |
| 113 | + const findings = validateFlowTemplatePaths({ |
| 114 | + objects: [LEAD_OBJECT], |
| 115 | + flows: [ |
| 116 | + { |
| 117 | + name: 'external', |
| 118 | + type: 'record_change', |
| 119 | + nodes: [ |
| 120 | + { id: 'start', type: 'start', config: { objectName: 'sys_user', triggerType: 'record-created' } }, |
| 121 | + { id: 'n1', type: 'notify', notify: { title: '{record.anything.deep}', body: 'x' } }, |
| 122 | + ], |
| 123 | + }, |
| 124 | + ], |
| 125 | + }); |
| 126 | + expect(findings).toHaveLength(0); |
| 127 | + }); |
| 128 | + |
| 129 | + it('skips non-record-triggered flows', () => { |
| 130 | + const findings = validateFlowTemplatePaths({ |
| 131 | + objects: [LEAD_OBJECT], |
| 132 | + flows: [ |
| 133 | + { |
| 134 | + name: 'manual', |
| 135 | + type: 'screen', |
| 136 | + nodes: [ |
| 137 | + { id: 'start', type: 'start', config: {} }, |
| 138 | + { id: 'n1', type: 'notify', notify: { title: '{record.full_naem}', body: 'x' } }, |
| 139 | + ], |
| 140 | + }, |
| 141 | + ], |
| 142 | + }); |
| 143 | + expect(findings).toHaveLength(0); |
| 144 | + }); |
| 145 | + |
| 146 | + it('dedupes a repeated bad reference to one finding per node', () => { |
| 147 | + const findings = validateFlowTemplatePaths( |
| 148 | + flowWith({ title: '{record.full_naem}', body: 'again {record.full_naem}' }), |
| 149 | + ); |
| 150 | + expect(findings).toHaveLength(1); |
| 151 | + }); |
| 152 | + |
| 153 | + it('resolves objectName from the typed start block too', () => { |
| 154 | + const findings = validateFlowTemplatePaths({ |
| 155 | + objects: [LEAD_OBJECT], |
| 156 | + flows: [ |
| 157 | + { |
| 158 | + name: 'typed_start', |
| 159 | + type: 'record_change', |
| 160 | + nodes: [ |
| 161 | + { id: 'start', type: 'start', start: { objectName: 'crm_lead', triggerType: 'record-created' } }, |
| 162 | + { id: 'n1', type: 'notify', notify: { title: '{record.crm_account.name}', body: 'x' } }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + ], |
| 166 | + }); |
| 167 | + expect(findings).toHaveLength(1); |
| 168 | + expect(findings[0].rule).toBe(FLOW_TEMPLATE_LOOKUP_TRAVERSAL); |
| 169 | + }); |
| 170 | + |
| 171 | + it('detects references in freeform config and other node types (http url)', () => { |
| 172 | + const findings = validateFlowTemplatePaths({ |
| 173 | + objects: [LEAD_OBJECT], |
| 174 | + flows: [ |
| 175 | + { |
| 176 | + name: 'webhook', |
| 177 | + type: 'record_change', |
| 178 | + nodes: [ |
| 179 | + { id: 'start', type: 'start', config: { objectName: 'crm_lead', triggerType: 'record-created' } }, |
| 180 | + { id: 'h1', type: 'http', http: { url: 'https://x.test/{record.full_naem}', method: 'GET' } }, |
| 181 | + ], |
| 182 | + }, |
| 183 | + ], |
| 184 | + }); |
| 185 | + expect(findings).toHaveLength(1); |
| 186 | + expect(findings[0].rule).toBe(FLOW_TEMPLATE_UNKNOWN_FIELD); |
| 187 | + }); |
| 188 | + |
| 189 | + it('returns empty when there are no flows', () => { |
| 190 | + expect(validateFlowTemplatePaths({ objects: [LEAD_OBJECT] })).toHaveLength(0); |
| 191 | + }); |
| 192 | +}); |
0 commit comments