|
10 | 10 | * the next run reading the original. |
11 | 11 | * - **The trigger record.** `record` and `$record` start as the same record; |
12 | 12 | * a `record.x` write is the run's own value, so `$record.x` and the |
13 | | - * trigger's object keep what they were handed. |
| 13 | + * trigger's object keep what they were handed. The lookups a start node |
| 14 | + * declares are read by every attempt of a retried run and by a dry run, |
| 15 | + * and the caller's record, a class instance included, stays as handed. |
14 | 16 | * - **A paused run.** With no durable store, the paused run is held in |
15 | 17 | * memory; its resume works on its own copy, as a stored run's does, so |
16 | 18 | * nothing it writes reaches a value the run was handed. |
@@ -219,6 +221,73 @@ describe('the trigger record is the run\'s own copy', () => { |
219 | 221 | expect(trigger).toEqual({ id: 'd1', account: 'a1' }); |
220 | 222 | }); |
221 | 223 |
|
| 224 | + it('a start node that declares expand, under the retry strategy: every attempt reads the expanded relation, and the caller\'s record object is unchanged', async () => { |
| 225 | + const engine = makeEngine(); |
| 226 | + engine.setRecordExpander(async (_object, id) => ({ id, account: { id: 'a1', name: 'Acme' } })); |
| 227 | + // Fails on its first call and succeeds after; each call notes the |
| 228 | + // relation name its attempt reads. |
| 229 | + const seenByAttempt: unknown[] = []; |
| 230 | + engine.registerNodeExecutor({ |
| 231 | + type: 'flaky', |
| 232 | + descriptor: defineActionDescriptor({ type: 'flaky', version: '1.0.0', name: 'flaky' }), |
| 233 | + async execute(_node: any, variables: Map<string, unknown>) { |
| 234 | + const account = (variables.get('record') as Record<string, any> | undefined)?.account; |
| 235 | + seenByAttempt.push(account && typeof account === 'object' ? account.name : account); |
| 236 | + return seenByAttempt.length === 1 ? { success: false, error: 'first attempt fails' } : { success: true }; |
| 237 | + }, |
| 238 | + } as never); |
| 239 | + const flow = chain('iso_expand_retry', [ |
| 240 | + { type: 'flaky' }, |
| 241 | + assign({ seen: cel('record.account.name') }), |
| 242 | + ], [{ name: 'seen', type: 'text', isOutput: true }]); |
| 243 | + flow.runAs = 'system'; |
| 244 | + flow.nodes[0].config = { expand: ['account'] }; |
| 245 | + flow.errorHandling = { strategy: 'retry', maxRetries: 2, backoffMs: 0 }; |
| 246 | + engine.registerFlow('iso_expand_retry', flow); |
| 247 | + const trigger = { id: 'd1', account: 'a1' }; |
| 248 | + const run = await engine.execute('iso_expand_retry', { object: 'deal', record: trigger } as any); |
| 249 | + expect(run.success, JSON.stringify(run)).toBe(true); |
| 250 | + expect(seenByAttempt).toEqual(['Acme', 'Acme']); |
| 251 | + expect((run.output as any).seen).toBe('Acme'); |
| 252 | + expect(trigger).toEqual({ id: 'd1', account: 'a1' }); |
| 253 | + }); |
| 254 | + |
| 255 | + it('a start node that declares expand, in a dry run: the run reads the expanded relation, and the caller\'s record object is unchanged', async () => { |
| 256 | + const engine = makeEngine(); |
| 257 | + engine.setRecordExpander(async (_object, id) => ({ id, account: { id: 'a1', name: 'Acme' } })); |
| 258 | + const flow = chain('iso_expand_dry', [ |
| 259 | + assign({ seen: cel('record.account.name') }), |
| 260 | + ], [{ name: 'seen', type: 'text', isOutput: true }]); |
| 261 | + flow.nodes[0].config = { expand: ['account'] }; |
| 262 | + const trigger = { id: 'd1', account: 'a1' }; |
| 263 | + const result = await engine.dryRun(flow, { userId: 'usr_maker', object: 'deal', record: trigger }); |
| 264 | + expect(result.status, JSON.stringify(result)).toBe('completed'); |
| 265 | + expect(result.steps.find((s) => s.nodeId === 'n1')).toMatchObject({ kind: 'assignment', values: { seen: 'Acme' } }); |
| 266 | + expect(trigger).toEqual({ id: 'd1', account: 'a1' }); |
| 267 | + }); |
| 268 | + |
| 269 | + it('a start node that declares expand, with a record that is not a plain object: the run reads the expanded relation, and the caller\'s instance is unchanged', async () => { |
| 270 | + const engine = makeEngine(); |
| 271 | + engine.setRecordExpander(async (_object, id) => ({ id, account: { id: 'a1', name: 'Acme' } })); |
| 272 | + const flow = chain('iso_expand_instance', [ |
| 273 | + assign({ seen: cel('record.account.name') }), |
| 274 | + ], [{ name: 'seen', type: 'text', isOutput: true }]); |
| 275 | + flow.runAs = 'system'; |
| 276 | + flow.nodes[0].config = { expand: ['account'] }; |
| 277 | + engine.registerFlow('iso_expand_instance', flow); |
| 278 | + class DealRow { |
| 279 | + id = 'd1'; |
| 280 | + account: unknown = 'a1'; |
| 281 | + } |
| 282 | + const trigger = new DealRow(); |
| 283 | + const run = await engine.execute('iso_expand_instance', { object: 'deal', record: trigger } as any); |
| 284 | + expect(trigger).toBeInstanceOf(DealRow); |
| 285 | + expect(trigger.account).toBe('a1'); |
| 286 | + expect({ ...trigger }).toEqual({ id: 'd1', account: 'a1' }); |
| 287 | + expect(run.success, JSON.stringify(run)).toBe(true); |
| 288 | + expect((run.output as any).seen).toBe('Acme'); |
| 289 | + }); |
| 290 | + |
222 | 291 | it('a node that changes previous in place leaves the caller\'s previous object as handed', async () => { |
223 | 292 | const engine = makeEngine(); |
224 | 293 | engine.registerFlow('iso_previous', chain('iso_previous', [ |
|
0 commit comments