|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #29026 — the null-guard gate on a record-triggered flow's start, edge and |
| 4 | +// `decision` branch conditions. `validate-null-guards.test.ts` pins the |
| 5 | +// decision procedure; this file pins the wiring: which flows, which slots, |
| 6 | +// which bare names, and what the start condition proves downstream. The |
| 7 | +// ledger row in `validate-null-guards.ts` carries the runtime measurements. |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | + |
| 11 | +import { validateStackExpressions } from './validate-expressions.js'; |
| 12 | + |
| 13 | +// `budget` and `owner` are optional, as on `showcase_project`; `name` is |
| 14 | +// required and `spent` has a default, so neither may ever be flagged. |
| 15 | +const project = { |
| 16 | + name: 'showcase_project', |
| 17 | + fields: { |
| 18 | + name: { type: 'text', required: true }, |
| 19 | + owner: { type: 'text' }, |
| 20 | + health: { type: 'select', options: [{ value: 'green', default: true }, { value: 'red' }] }, |
| 21 | + budget: { type: 'currency' }, |
| 22 | + spent: { type: 'currency', defaultValue: 0 }, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +type Rec = Record<string, unknown>; |
| 27 | + |
| 28 | +const start = (config: Rec = {}): Rec => ({ |
| 29 | + id: 'start', |
| 30 | + type: 'start', |
| 31 | + config: { objectName: 'showcase_project', triggerType: 'record-after-update', ...config }, |
| 32 | +}); |
| 33 | + |
| 34 | +const flow = (overrides: Rec): Rec => ({ |
| 35 | + name: 'escalate', |
| 36 | + label: 'Escalate', |
| 37 | + type: 'autolaunched', |
| 38 | + nodes: [start(), { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }], |
| 39 | + edges: [ |
| 40 | + { id: 'e1', source: 'start', target: 'triage' }, |
| 41 | + { id: 'e2', source: 'triage', target: 'alert', condition: 'budget > 200000' }, |
| 42 | + { id: 'e3', source: 'triage', target: 'calm', isDefault: true }, |
| 43 | + ], |
| 44 | + ...overrides, |
| 45 | +}); |
| 46 | + |
| 47 | +/** The null-guard findings alone — other passes judge other things about these flows. */ |
| 48 | +const nullGuardIssues = (flows: Rec[]) => |
| 49 | + validateStackExpressions({ objects: [project], flows }).filter((i) => i.message.includes('declares as nullable')); |
| 50 | + |
| 51 | +describe('flow conditions under the null-guard gate (#29026)', () => { |
| 52 | + it('rejects the bare unguarded comparison that failed the showcase escalation run', () => { |
| 53 | + const issues = nullGuardIssues([flow({})]); |
| 54 | + expect(issues).toHaveLength(1); |
| 55 | + const [issue] = issues; |
| 56 | + expect(issue!.severity).toBe('error'); |
| 57 | + expect(issue!.where).toContain("edge 'e2'"); |
| 58 | + expect(issue!.path).toBe('flows[0].edges[1].condition'); |
| 59 | + expect(issue!.message).toContain('`budget`'); |
| 60 | + expect(issue!.message).toContain('the flow run fails at this condition'); |
| 61 | + expect(issue!.message).toContain("Guard it with '!= null'"); |
| 62 | + }); |
| 63 | + |
| 64 | + it('rejects the `record.` spelling too, and accepts either guarded spelling', () => { |
| 65 | + const edges = (condition: string) => [ |
| 66 | + { id: 'e1', source: 'start', target: 'triage' }, |
| 67 | + { id: 'e2', source: 'triage', target: 'alert', condition }, |
| 68 | + { id: 'e3', source: 'triage', target: 'calm', isDefault: true }, |
| 69 | + ]; |
| 70 | + expect(nullGuardIssues([flow({ edges: edges('record.budget > 200000') })])).toHaveLength(1); |
| 71 | + expect(nullGuardIssues([flow({ edges: edges('record.budget != null && record.budget > 200000') })])).toEqual([]); |
| 72 | + expect(nullGuardIssues([flow({ edges: edges('budget != null && budget > 200000') })])).toEqual([]); |
| 73 | + // `has()` is true on the materialized null, so it is no guard here either. |
| 74 | + const hasOnly = nullGuardIssues([flow({ edges: edges('has(record.budget) && record.budget > 200000') })]); |
| 75 | + expect(hasOnly).toHaveLength(1); |
| 76 | + expect(hasOnly[0]!.message).toContain('`has(record.budget)` does not guard it'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('judges the start condition, and presumes what it proves at every edge downstream', () => { |
| 80 | + const unguardedStart = nullGuardIssues([flow({ |
| 81 | + nodes: [start({ condition: 'health == "red" && budget > 100000' }), { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }], |
| 82 | + edges: [{ id: 'e1', source: 'start', target: 'triage' }], |
| 83 | + })]); |
| 84 | + expect(unguardedStart).toHaveLength(1); |
| 85 | + expect(unguardedStart[0]!.where).toContain("node 'start' (start) condition"); |
| 86 | + expect(unguardedStart[0]!.path).toBe('flows[0].nodes[0].config.condition'); |
| 87 | + |
| 88 | + // The start condition gates every run, so its guard holds at e2. |
| 89 | + expect(nullGuardIssues([flow({ |
| 90 | + nodes: [start({ condition: 'budget != null && budget > 100000' }), { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }], |
| 91 | + })])).toEqual([]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('judges a `decision` branch expression', () => { |
| 95 | + const issues = nullGuardIssues([flow({ |
| 96 | + nodes: [ |
| 97 | + start(), |
| 98 | + { id: 'triage', type: 'decision', config: { conditions: [{ label: 'critical', expression: 'record.budget > 200000' }, { label: 'normal', expression: 'true' }] } }, |
| 99 | + { id: 'alert', type: 'end' }, |
| 100 | + { id: 'calm', type: 'end' }, |
| 101 | + ], |
| 102 | + edges: [{ id: 'e1', source: 'start', target: 'triage' }], |
| 103 | + })]); |
| 104 | + expect(issues).toHaveLength(1); |
| 105 | + expect(issues[0]!.where).toContain('conditions[0].expression'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('judges an edge inside a container region', () => { |
| 109 | + const issues = nullGuardIssues([flow({ |
| 110 | + nodes: [ |
| 111 | + start(), |
| 112 | + { |
| 113 | + id: 'fan', |
| 114 | + type: 'parallel', |
| 115 | + config: { |
| 116 | + branches: [{ |
| 117 | + name: 'Owner', |
| 118 | + nodes: [{ id: 'gate', type: 'decision' }, { id: 'ping', type: 'end' }], |
| 119 | + edges: [{ id: 'b1', source: 'gate', target: 'ping', condition: 'record.budget * 2 > 10' }], |
| 120 | + }], |
| 121 | + }, |
| 122 | + }, |
| 123 | + ], |
| 124 | + edges: [{ id: 'e1', source: 'start', target: 'fan' }], |
| 125 | + })]); |
| 126 | + expect(issues).toHaveLength(1); |
| 127 | + expect(issues[0]!.where).toContain("edge 'b1'"); |
| 128 | + }); |
| 129 | + |
| 130 | + it('leaves a flow with no record trigger alone: nothing makes its record total', () => { |
| 131 | + const noTrigger = flow({ nodes: [{ id: 'start', type: 'start', config: { objectName: 'showcase_project' } }, { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }] }); |
| 132 | + expect(nullGuardIssues([noTrigger])).toEqual([]); |
| 133 | + const api = flow({ type: 'api', nodes: [{ id: 'start', type: 'start', config: { objectName: 'showcase_project', triggerType: 'api' } }, { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }] }); |
| 134 | + expect(nullGuardIssues([api])).toEqual([]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('leaves a bare name a declared variable holds alone — the variable wins — and still judges `record.`', () => { |
| 138 | + const variables = [{ name: 'budget', type: 'number', defaultValue: 5 }]; |
| 139 | + expect(nullGuardIssues([flow({ variables })])).toEqual([]); |
| 140 | + const issues = nullGuardIssues([flow({ |
| 141 | + variables, |
| 142 | + edges: [ |
| 143 | + { id: 'e1', source: 'start', target: 'triage' }, |
| 144 | + { id: 'e2', source: 'triage', target: 'alert', condition: 'record.budget > 200000' }, |
| 145 | + { id: 'e3', source: 'triage', target: 'calm', isDefault: true }, |
| 146 | + ], |
| 147 | + })]); |
| 148 | + expect(issues).toHaveLength(1); |
| 149 | + }); |
| 150 | + |
| 151 | + it('leaves bare names alone on a flow whose nodes can bind names nobody declared', () => { |
| 152 | + const withScript = (condition: string) => flow({ |
| 153 | + nodes: [start(), { id: 'calc', type: 'script', config: { function: 'calc' } }, { id: 'triage', type: 'decision' }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }], |
| 154 | + edges: [ |
| 155 | + { id: 'e0', source: 'start', target: 'calc' }, |
| 156 | + { id: 'e1', source: 'calc', target: 'triage' }, |
| 157 | + { id: 'e2', source: 'triage', target: 'alert', condition }, |
| 158 | + { id: 'e3', source: 'triage', target: 'calm', isDefault: true }, |
| 159 | + ], |
| 160 | + }); |
| 161 | + expect(nullGuardIssues([withScript('budget > 200000')])).toEqual([]); |
| 162 | + expect(nullGuardIssues([withScript('record.budget > 200000')])).toHaveLength(1); |
| 163 | + }); |
| 164 | + |
| 165 | + it('does not judge an inert `config.condition` on a non-start node', () => { |
| 166 | + expect(nullGuardIssues([flow({ |
| 167 | + nodes: [start(), { id: 'triage', type: 'decision', config: { condition: 'record.budget > 1' } }, { id: 'alert', type: 'end' }, { id: 'calm', type: 'end' }], |
| 168 | + edges: [{ id: 'e1', source: 'start', target: 'triage' }], |
| 169 | + })])).toEqual([]); |
| 170 | + }); |
| 171 | + |
| 172 | + it('treats a `count` / `sum` roll-up as always valued, and a `max` one as nullable', () => { |
| 173 | + // Zero children is zero for `count` / `sum` (objectql's |
| 174 | + // `summaryEmptySetValue`); `min` / `max` / `avg` over no rows is `null`. |
| 175 | + const report = { |
| 176 | + name: 'expense_report', |
| 177 | + fields: { |
| 178 | + status: { type: 'select', options: [{ value: 'draft', default: true }, { value: 'submitted' }] }, |
| 179 | + total_amount: { type: 'summary', summaryOperations: { object: 'expense_line', field: 'amount', function: 'sum' } }, |
| 180 | + line_count: { type: 'summary', summaryOperations: { object: 'expense_line', function: 'count' } }, |
| 181 | + largest_line: { type: 'summary', summaryOperations: { object: 'expense_line', field: 'amount', function: 'max' } }, |
| 182 | + }, |
| 183 | + }; |
| 184 | + const quorum = (condition: string): Rec => ({ |
| 185 | + name: 'quorum', |
| 186 | + label: 'Quorum', |
| 187 | + type: 'autolaunched', |
| 188 | + nodes: [{ id: 'start', type: 'start', config: { objectName: 'expense_report', triggerType: 'record-after-update', condition } }, { id: 'end', type: 'end' }], |
| 189 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 190 | + }); |
| 191 | + const judge = (condition: string) => |
| 192 | + validateStackExpressions({ objects: [report], flows: [quorum(condition)] }) |
| 193 | + .filter((i) => i.message.includes('declares as nullable')); |
| 194 | + expect(judge('status == "submitted" && total_amount >= 5000 && line_count > 1')).toEqual([]); |
| 195 | + expect(judge('largest_line > 1000').map((i) => i.message)).toEqual([expect.stringContaining('`largest_line`')]); |
| 196 | + }); |
| 197 | + |
| 198 | + it('never flags a non-nullable field', () => { |
| 199 | + expect(nullGuardIssues([flow({ |
| 200 | + edges: [{ id: 'e1', source: 'start', target: 'triage' }, { id: 'e2', source: 'triage', target: 'alert', condition: 'spent > 5 && size(name) > 1' }], |
| 201 | + })])).toEqual([]); |
| 202 | + }); |
| 203 | +}); |
0 commit comments