-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidate-flow-trigger-readiness.test.ts
More file actions
251 lines (231 loc) · 9.45 KB
/
Copy pathvalidate-flow-trigger-readiness.test.ts
File metadata and controls
251 lines (231 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect } from 'vitest';
import {
validateFlowTriggerReadiness,
FLOW_TRIGGER_UNKNOWN_OBJECT,
FLOW_DRAFT_STATUS_AMBIGUOUS,
FLOW_TRIGGER_UNKNOWN_EVENT,
} from './validate-flow-trigger-readiness.js';
function recordFlow(overrides: Record<string, unknown> = {}) {
return {
name: 'candidate_hired',
type: 'autolaunched',
nodes: [
{
id: 'start',
type: 'start',
config: {
objectName: 'app_candidate',
triggerType: 'record-after-update',
condition: 'stage == "hired"',
},
},
{ id: 'end', type: 'end' },
],
edges: [{ id: 'e1', source: 'start', target: 'end' }],
...overrides,
};
}
const candidateObject = { name: 'app_candidate', label: 'Candidate', fields: {} };
describe('validateFlowTriggerReadiness', () => {
it('passes a correctly wired, explicitly active record flow', () => {
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [recordFlow({ status: 'active' })],
});
expect(findings).toEqual([]);
});
it('warns when the target object is not defined in the stack (the silent-miss)', () => {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.objectName = 'candidate';
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [flow],
});
expect(findings).toHaveLength(1);
expect(findings[0].rule).toBe(FLOW_TRIGGER_UNKNOWN_OBJECT);
expect(findings[0].severity).toBe('warning');
expect(findings[0].message).toContain("'candidate'");
expect(findings[0].path).toBe('flows[0].nodes[0].config.objectName');
});
it('does not flag sys_* platform objects as unknown', () => {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.objectName = 'sys_user';
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [flow],
});
expect(findings).toEqual([]);
});
it('warns when an auto-triggered flow has no explicit status (defaults to draft)', () => {
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [recordFlow()],
});
expect(findings).toHaveLength(1);
expect(findings[0].rule).toBe(FLOW_DRAFT_STATUS_AMBIGUOUS);
expect(findings[0].message).toContain("'draft'");
expect(findings[0].message).toMatch(/still fire/i);
});
it('warns on an explicit draft too — defineFlow fills the default before lint runs', () => {
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [recordFlow({ status: 'draft' })],
});
expect(findings.map((f) => f.rule)).toEqual([FLOW_DRAFT_STATUS_AMBIGUOUS]);
});
it('stays quiet on obsolete (deliberately disabled) auto-triggered flows', () => {
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [recordFlow({ status: 'obsolete' })],
});
expect(findings).toEqual([]);
});
it('does not require a status on manual/screen flows (no arming semantics)', () => {
const findings = validateFlowTriggerReadiness({
objects: [candidateObject],
flows: [
{
name: 'wizard',
type: 'screen',
nodes: [{ id: 'start', type: 'start' }, { id: 'end', type: 'end' }],
edges: [{ id: 'e1', source: 'start', target: 'end' }],
},
],
});
expect(findings).toEqual([]);
});
it('flags schedule and api flows for missing status too', () => {
const findings = validateFlowTriggerReadiness({
objects: [],
flows: [
{
name: 'digest',
type: 'schedule',
nodes: [
{ id: 'start', type: 'start', config: { schedule: { type: 'interval', intervalMs: 60000 } } },
{ id: 'end', type: 'end' },
],
edges: [{ id: 'e1', source: 'start', target: 'end' }],
},
],
});
expect(findings.map((f) => f.rule)).toEqual([FLOW_DRAFT_STATUS_AMBIGUOUS]);
});
it('treats a time-relative flow (config.timeRelative) as auto-triggered — flags missing status', () => {
const findings = validateFlowTriggerReadiness({
objects: [{ name: 'contracts', label: 'Contracts', fields: {} }],
flows: [
{
name: 'renewal_alert',
type: 'schedule',
nodes: [
{
id: 'start',
type: 'start',
config: {
timeRelative: { object: 'contracts', dateField: 'end_date', offsetDays: [60, 30, 7] },
},
},
{ id: 'end', type: 'end' },
],
edges: [{ id: 'e1', source: 'start', target: 'end' }],
},
],
});
expect(findings.map((f) => f.rule)).toEqual([FLOW_DRAFT_STATUS_AMBIGUOUS]);
});
it('warns when a time-relative flow sweeps an object the stack does not define', () => {
const findings = validateFlowTriggerReadiness({
objects: [{ name: 'contracts', label: 'Contracts', fields: {} }],
flows: [
{
name: 'renewal_alert',
type: 'schedule',
status: 'active',
nodes: [
{
id: 'start',
type: 'start',
config: {
timeRelative: { object: 'contract', dateField: 'end_date', withinDays: 60 },
},
},
{ id: 'end', type: 'end' },
],
edges: [{ id: 'e1', source: 'start', target: 'end' }],
},
],
});
expect(findings).toHaveLength(1);
expect(findings[0].rule).toBe(FLOW_TRIGGER_UNKNOWN_OBJECT);
expect(findings[0].message).toContain("'contract'");
expect(findings[0].path).toBe('flows[0].nodes[0].config.timeRelative.object');
});
it('passes the record-after-write (create-OR-update) token (#3427)', () => {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = 'record-after-write';
const findings = validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
expect(findings).toEqual([]);
});
it('flags a record-lifecycle-shaped token with a typo op that never fires', () => {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = 'record-after-updated';
const findings = validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
expect(findings).toHaveLength(1);
expect(findings[0].rule).toBe(FLOW_TRIGGER_UNKNOWN_EVENT);
expect(findings[0].severity).toBe('warning');
expect(findings[0].message).toContain("'updated'");
expect(findings[0].message).toMatch(/never fires/i);
expect(findings[0].path).toBe('flows[0].nodes[0].config.triggerType');
});
it('flags any invalid op on either phase (before/after)', () => {
const mk = (tt: string) => {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = tt;
return validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
};
expect(mk('record-before-frobnicate').map((f) => f.rule)).toEqual([FLOW_TRIGGER_UNKNOWN_EVENT]);
expect(mk('record-after-writes').map((f) => f.rule)).toEqual([FLOW_TRIGGER_UNKNOWN_EVENT]);
});
it('does not flag the canonical firing tokens (incl. insert synonym)', () => {
for (const tt of [
'record-after-create',
'record-after-insert',
'record-after-update',
'record-before-update',
'record-after-delete',
'record-after-write',
'record-before-write',
]) {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = tt;
const findings = validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
expect(findings, `${tt} should not be flagged`).toEqual([]);
}
});
it('does not flag bare record-<noun> shapes (e.g. record-change) with this rule', () => {
// `record-change` lacks a before/after phase, so it is out of this rule's
// scope (a separate concern); the UNKNOWN_EVENT rule must stay silent on it.
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = 'record-change';
const findings = validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
expect(findings.some((f) => f.rule === FLOW_TRIGGER_UNKNOWN_EVENT)).toBe(false);
});
it('does not flag non-record triggerTypes (schedule/api/manual)', () => {
for (const tt of ['schedule', 'api', 'manual']) {
const flow = recordFlow({ status: 'active' });
(flow.nodes[0] as { config: Record<string, unknown> }).config.triggerType = tt;
const findings = validateFlowTriggerReadiness({ objects: [candidateObject], flows: [flow] });
expect(findings.some((f) => f.rule === FLOW_TRIGGER_UNKNOWN_EVENT)).toBe(false);
}
});
it('handles map-keyed flows/objects and stacks with no flows', () => {
expect(validateFlowTriggerReadiness({})).toEqual([]);
const findings = validateFlowTriggerReadiness({
objects: { app_candidate: { label: 'C', fields: {} } },
flows: { hired: recordFlow({ name: undefined, status: 'active' }) },
});
expect(findings).toEqual([]);
});
});