|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateReadonlyFlowWrites, |
| 6 | + FLOW_UPDATE_READONLY_FIELD, |
| 7 | + FLOW_UPDATE_READONLY_WHEN_FIELD, |
| 8 | +} from './validate-readonly-flow-writes.js'; |
| 9 | + |
| 10 | +// Target object: a static-readonly field, a conditional readonlyWhen field, and |
| 11 | +// a plain writable field. Map-shaped `fields` (the common authoring form). |
| 12 | +const opportunityObject = { |
| 13 | + name: 'crm_opportunity', |
| 14 | + label: 'Opportunity', |
| 15 | + fields: { |
| 16 | + approval_status: { type: 'text', readonly: true }, |
| 17 | + amount: { type: 'currency', readonlyWhen: "record.stage == 'closed_won'" }, |
| 18 | + notes: { type: 'text' }, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +/** A flow with a single `update_record` node (nodes[1]) writing `fields`. */ |
| 23 | +function flowWith( |
| 24 | + fields: unknown, |
| 25 | + flowOverrides: Record<string, unknown> = {}, |
| 26 | + nodeConfigOverrides: Record<string, unknown> = {}, |
| 27 | +) { |
| 28 | + return { |
| 29 | + name: 'stamp_approval', |
| 30 | + type: 'record_change', |
| 31 | + nodes: [ |
| 32 | + { id: 'start', type: 'start', config: {} }, |
| 33 | + { |
| 34 | + id: 'stamp', |
| 35 | + type: 'update_record', |
| 36 | + label: 'Stamp approval', |
| 37 | + config: { objectName: 'crm_opportunity', filter: { id: '{recordId}' }, fields, ...nodeConfigOverrides }, |
| 38 | + }, |
| 39 | + { id: 'end', type: 'end' }, |
| 40 | + ], |
| 41 | + edges: [], |
| 42 | + ...flowOverrides, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +describe('validateReadonlyFlowWrites', () => { |
| 47 | + // ── static readonly → ERROR ────────────────────────────────────────── |
| 48 | + it('errors when a runAs:user update_record writes a static-readonly field', () => { |
| 49 | + const findings = validateReadonlyFlowWrites({ |
| 50 | + objects: [opportunityObject], |
| 51 | + flows: [flowWith({ approval_status: 'approved' }, { runAs: 'user' })], |
| 52 | + }); |
| 53 | + expect(findings).toHaveLength(1); |
| 54 | + expect(findings[0].severity).toBe('error'); |
| 55 | + expect(findings[0].rule).toBe(FLOW_UPDATE_READONLY_FIELD); |
| 56 | + expect(findings[0].path).toBe('flows[0].nodes[1].config.fields.approval_status'); |
| 57 | + expect(findings[0].message).toContain('approval_status'); |
| 58 | + expect(findings[0].message).toContain('crm_opportunity'); |
| 59 | + expect(findings[0].message).toContain('#2948'); |
| 60 | + expect(findings[0].where).toBe('flow "stamp_approval" › node "Stamp approval"'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('errors when runAs is unauthored (defaults to user)', () => { |
| 64 | + const findings = validateReadonlyFlowWrites({ |
| 65 | + objects: [opportunityObject], |
| 66 | + flows: [flowWith({ approval_status: 'approved' })], // no runAs |
| 67 | + }); |
| 68 | + expect(findings).toHaveLength(1); |
| 69 | + expect(findings[0].severity).toBe('error'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('resolves the target object via the `object` alias', () => { |
| 73 | + const findings = validateReadonlyFlowWrites({ |
| 74 | + objects: [opportunityObject], |
| 75 | + flows: [flowWith({ approval_status: 'approved' }, { runAs: 'user' }, { objectName: undefined, object: 'crm_opportunity' })], |
| 76 | + }); |
| 77 | + expect(findings).toHaveLength(1); |
| 78 | + expect(findings[0].rule).toBe(FLOW_UPDATE_READONLY_FIELD); |
| 79 | + }); |
| 80 | + |
| 81 | + it('flags each readonly field written in one node', () => { |
| 82 | + const twoReadonly = { |
| 83 | + name: 'crm_case', |
| 84 | + fields: { |
| 85 | + is_sla_violated: { type: 'boolean', readonly: true }, |
| 86 | + closed_at: { type: 'datetime', readonly: true }, |
| 87 | + subject: { type: 'text' }, |
| 88 | + }, |
| 89 | + }; |
| 90 | + const flow = { |
| 91 | + name: 'close_case', |
| 92 | + type: 'record_change', |
| 93 | + runAs: 'user', |
| 94 | + nodes: [ |
| 95 | + { id: 'start', type: 'start', config: {} }, |
| 96 | + { |
| 97 | + id: 'u', |
| 98 | + type: 'update_record', |
| 99 | + label: 'Close', |
| 100 | + config: { objectName: 'crm_case', fields: { is_sla_violated: true, closed_at: '{now}', subject: 'x' } }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + edges: [], |
| 104 | + }; |
| 105 | + const findings = validateReadonlyFlowWrites({ objects: [twoReadonly], flows: [flow] }); |
| 106 | + expect(findings).toHaveLength(2); |
| 107 | + expect(findings.every((f) => f.severity === 'error')).toBe(true); |
| 108 | + expect(findings.map((f) => f.path)).toEqual([ |
| 109 | + 'flows[0].nodes[1].config.fields.is_sla_violated', |
| 110 | + 'flows[0].nodes[1].config.fields.closed_at', |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it('handles array-shaped object.fields', () => { |
| 115 | + const arrObject = { |
| 116 | + name: 'crm_lead', |
| 117 | + fields: [ |
| 118 | + { name: 'converted_account', type: 'lookup', readonly: true }, |
| 119 | + { name: 'company', type: 'text' }, |
| 120 | + ], |
| 121 | + }; |
| 122 | + const flow = { |
| 123 | + name: 'convert', |
| 124 | + type: 'record_change', |
| 125 | + runAs: 'user', |
| 126 | + nodes: [ |
| 127 | + { id: 'start', type: 'start', config: {} }, |
| 128 | + { id: 'u', type: 'update_record', label: 'Convert', config: { objectName: 'crm_lead', fields: { converted_account: '{acct}' } } }, |
| 129 | + ], |
| 130 | + edges: [], |
| 131 | + }; |
| 132 | + const findings = validateReadonlyFlowWrites({ objects: [arrObject], flows: [flow] }); |
| 133 | + expect(findings).toHaveLength(1); |
| 134 | + expect(findings[0].rule).toBe(FLOW_UPDATE_READONLY_FIELD); |
| 135 | + }); |
| 136 | + |
| 137 | + // ── readonlyWhen → WARNING ─────────────────────────────────────────── |
| 138 | + it('warns (not errors) when writing a readonlyWhen field', () => { |
| 139 | + const findings = validateReadonlyFlowWrites({ |
| 140 | + objects: [opportunityObject], |
| 141 | + flows: [flowWith({ amount: 5000 }, { runAs: 'user' })], |
| 142 | + }); |
| 143 | + expect(findings).toHaveLength(1); |
| 144 | + expect(findings[0].severity).toBe('warning'); |
| 145 | + expect(findings[0].rule).toBe(FLOW_UPDATE_READONLY_WHEN_FIELD); |
| 146 | + expect(findings[0].message).toContain('#3042'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('separates readonly (error) + readonlyWhen (warning) + plain (clean) in one node', () => { |
| 150 | + const findings = validateReadonlyFlowWrites({ |
| 151 | + objects: [opportunityObject], |
| 152 | + flows: [flowWith({ approval_status: 'approved', amount: 1, notes: 'hi' }, { runAs: 'user' })], |
| 153 | + }); |
| 154 | + expect(findings).toHaveLength(2); |
| 155 | + expect(findings.find((f) => f.severity === 'error')?.path).toBe('flows[0].nodes[1].config.fields.approval_status'); |
| 156 | + expect(findings.find((f) => f.severity === 'warning')?.path).toBe('flows[0].nodes[1].config.fields.amount'); |
| 157 | + }); |
| 158 | + |
| 159 | + // ── clean: runAs:system is the intended maintenance channel ─────────── |
| 160 | + it('does NOT flag a runAs:system flow (elevated writer bypasses the strip)', () => { |
| 161 | + const findings = validateReadonlyFlowWrites({ |
| 162 | + objects: [opportunityObject], |
| 163 | + flows: [flowWith({ approval_status: 'approved' }, { runAs: 'system' })], |
| 164 | + }); |
| 165 | + expect(findings).toEqual([]); |
| 166 | + }); |
| 167 | + |
| 168 | + // ── clean: create_record is engine-exempt from the readonly strip ───── |
| 169 | + it('does NOT flag create_record writing a readonly field', () => { |
| 170 | + const flow = { |
| 171 | + name: 'seed_opp', |
| 172 | + type: 'record_change', |
| 173 | + runAs: 'user', |
| 174 | + nodes: [ |
| 175 | + { id: 'start', type: 'start', config: {} }, |
| 176 | + { id: 'c', type: 'create_record', label: 'Create', config: { objectName: 'crm_opportunity', fields: { approval_status: 'approved' } } }, |
| 177 | + ], |
| 178 | + edges: [], |
| 179 | + }; |
| 180 | + const findings = validateReadonlyFlowWrites({ objects: [opportunityObject], flows: [flow] }); |
| 181 | + expect(findings).toEqual([]); |
| 182 | + }); |
| 183 | + |
| 184 | + // ── clean: plain writable field ────────────────────────────────────── |
| 185 | + it('does NOT flag writes to a plain writable field', () => { |
| 186 | + const findings = validateReadonlyFlowWrites({ |
| 187 | + objects: [opportunityObject], |
| 188 | + flows: [flowWith({ notes: 'updated' }, { runAs: 'user' })], |
| 189 | + }); |
| 190 | + expect(findings).toEqual([]); |
| 191 | + }); |
| 192 | + |
| 193 | + // ── clean: not statically knowable ─────────────────────────────────── |
| 194 | + it('skips a templated objectName (dynamic target)', () => { |
| 195 | + const findings = validateReadonlyFlowWrites({ |
| 196 | + objects: [opportunityObject], |
| 197 | + flows: [flowWith({ approval_status: 'approved' }, { runAs: 'user' }, { objectName: '{targetObject}' })], |
| 198 | + }); |
| 199 | + expect(findings).toEqual([]); |
| 200 | + }); |
| 201 | + |
| 202 | + it('skips a non-literal fields map (dynamic write payload)', () => { |
| 203 | + const findings = validateReadonlyFlowWrites({ |
| 204 | + objects: [opportunityObject], |
| 205 | + flows: [flowWith('{allFields}', { runAs: 'user' })], |
| 206 | + }); |
| 207 | + expect(findings).toEqual([]); |
| 208 | + }); |
| 209 | + |
| 210 | + it('skips an object not defined in this stack (another package)', () => { |
| 211 | + const findings = validateReadonlyFlowWrites({ |
| 212 | + objects: [], // crm_opportunity not present |
| 213 | + flows: [flowWith({ approval_status: 'approved' }, { runAs: 'user' })], |
| 214 | + }); |
| 215 | + expect(findings).toEqual([]); |
| 216 | + }); |
| 217 | + |
| 218 | + it('does NOT flag an unknown field (not this rule’s concern)', () => { |
| 219 | + const findings = validateReadonlyFlowWrites({ |
| 220 | + objects: [opportunityObject], |
| 221 | + flows: [flowWith({ nonexistent_field: 'x' }, { runAs: 'user' })], |
| 222 | + }); |
| 223 | + expect(findings).toEqual([]); |
| 224 | + }); |
| 225 | + |
| 226 | + // ── shape robustness ───────────────────────────────────────────────── |
| 227 | + it('returns [] for a stack with no flows', () => { |
| 228 | + expect(validateReadonlyFlowWrites({ objects: [opportunityObject] })).toEqual([]); |
| 229 | + expect(validateReadonlyFlowWrites({})).toEqual([]); |
| 230 | + }); |
| 231 | + |
| 232 | + it('falls back to node id then index for the location label', () => { |
| 233 | + const flow = { |
| 234 | + name: 'f', |
| 235 | + runAs: 'user', |
| 236 | + nodes: [{ id: 'my_node', type: 'update_record', config: { objectName: 'crm_opportunity', fields: { approval_status: 'x' } } }], |
| 237 | + edges: [], |
| 238 | + }; |
| 239 | + const findings = validateReadonlyFlowWrites({ objects: [opportunityObject], flows: [flow] }); |
| 240 | + expect(findings[0].where).toBe('flow "f" › node "my_node"'); |
| 241 | + expect(findings[0].path).toBe('flows[0].nodes[0].config.fields.approval_status'); |
| 242 | + }); |
| 243 | +}); |
0 commit comments