-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathversion-workflow.test.ts
More file actions
562 lines (512 loc) · 13.1 KB
/
version-workflow.test.ts
File metadata and controls
562 lines (512 loc) · 13.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import test from 'ava';
import { generateHash, parse } from '../../src/util/version';
import Project, { generateWorkflow } from '../../src';
import Workflow from '../../src/Workflow';
// this is an actual lightning workflow state, copied verbatim
// todo already out of data as the version will change soon
// next, update this
const example = {
id: '320157d2-260d-4e32-91c0-db935547c263',
name: 'Turtle Power',
edges: [
{
enabled: true,
id: 'ed3ebfbf-6fa3-4438-b21d-06f7eec216c1',
condition_type: 'always',
source_trigger_id: 'bf10f31a-cf51-45a2-95a4-756d0a25af53',
target_job_id: '4d18c46b-3bb4-4af1-81e2-07f9aee527fc',
},
{
enabled: true,
id: '253bf2d7-1a01-44c8-8e2e-ccf50de92dff',
condition_type: 'js_expression',
condition_label: 'always tbh',
condition_expression: 'state.data',
source_job_id: '4d18c46b-3bb4-4af1-81e2-07f9aee527fc',
target_job_id: '40b839bd-5ade-414e-8dde-ed3ae77239ea',
},
],
version_history: ['app:211291f6e6d5'],
inserted_at: '2025-12-19T15:26:49Z',
jobs: [
{
id: '4d18c46b-3bb4-4af1-81e2-07f9aee527fc',
name: 'Transform data',
body: 'fri',
adaptor: '@openfn/language-http@7.2.6',
project_credential_id: 'dd409089-5569-4157-8cf6-528ace283348',
},
{
id: '40b839bd-5ade-414e-8dde-ed3ae77239ea',
name: 'do something',
body: '// Check out the Job Writing Guide for help getting started:\n// https://docs.openfn.org/documentation/jobs/job-writing-guide\n',
adaptor: '@openfn/language-http@7.2.6',
project_credential_id: null,
},
],
triggers: [
{
enabled: false,
id: 'bf10f31a-cf51-45a2-95a4-756d0a25af53',
type: 'webhook',
},
],
updated_at: '2026-01-23T12:08:47Z',
lock_version: 34,
deleted_at: null,
concurrency: null,
};
test('match lightning version', async (t) => {
const [expected] = example.version_history;
// load the project from v1 state
const proj = await Project.from('state', {
workflows: [example],
});
const wf = proj.workflows[0];
const hash = wf.getVersionHash();
t.log(expected);
t.log(hash);
t.is(parse(hash).hash, parse(expected).hash);
});
test('generate an 12 character version hash for a basic workflow', (t) => {
const workflow = generateWorkflow(
`
@name a
@id some-id
webhook-transform_data(name="Transform data",expression="fn(s => s)")
`
);
const hash = workflow.getVersionHash();
t.is(hash, 'cli:72aed7c5f224');
});
test('ordering: generate version string with no steps', (t) => {
const workflow = generateWorkflow(
`
@name a
@id some-id
`
);
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:a');
});
test('ordering: generate version string with webook trigger and step', (t) => {
const workflow = generateWorkflow(
`
@name a
@id some-id
trigger(type=webhook)-x(adaptor=http,expression=fn,project_credential_id=abc)
`
);
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:awebhookhttpfnxabctruewebhook-x');
});
test('ordering: multiple steps are sorted alphabetically by name', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
z-x
a-x
m-x
`
);
// With step keys sorted: adaptor, body, keychain_credential_id, name, project_credential_id
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfamxztruea-xtruem-xtruez-x');
});
test('ordering: step names are sorted case-insensitively', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
Z-x
a-x
B-x
`
);
// Steps should appear in order: a, B, x, Z (case-insensitive sort)
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfaBxZtruea-xtrueB-xtrueZ-x');
});
test('ordering: step keys appear in sorted order', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
a-step(project_credential_id=cred,name=step,expression=code,adaptor=http)
`
);
// Step keys sorted: adaptor, body, keychain_credential_id, name, project_credential_id
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfahttpcodestepcredtruea-step');
});
test('ordering: multiple edges are sorted by edge name', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
z-a
a-b
m-n
`
);
// Edges sorted by "source-target" name: a-b, m-n, z-a
// Each edge has enabled=true and its generated name
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfabmnztruea-btruem-ntruez-a');
});
test('ordering: edge keys appear in sorted order', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
a-(label=lbl,condition=always,disabled=true)-b
`
);
// Edge keys sorted: condition_expression, condition_label, condition_type, enabled, label, name
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfablblalwaysfalsea-b');
});
test('ordering: trigger keys appear in sorted order', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
t(type=cron,cron_expression="* * *",enabled=false)-x(expression=code)
`
);
// Trigger keys sorted: cron_expression, enabled, type
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wf* * *falsecroncodextruecron-x');
});
test('ordering: complete workflow with all elements', (t) => {
const workflow = generateWorkflow(
`
@name complete
@id some-id
trigger(type=webhook)-step2(adaptor=http,expression=fn2,project_credential_id=c2)
step1(adaptor=common,expression=fn1,project_credential_id=c1)-step2
`
);
const hash = workflow.getVersionHash({ sha: false });
t.is(
hash,
'cli:completewebhookcommonfn1step1c1httpfn2step2c2truestep1-step2truewebhook-step2'
);
});
test('ordering: multiple edges from same source are sorted by target', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
a-z
a-m
a-b
`
);
// Edges: a-b, a-m, a-z (sorted by full edge name)
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfabmztruea-btruea-mtruea-z');
});
test('ordering: workflow with webhook trigger connected to step', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
trigger(type=webhook)-step
`
);
// Workflow name, trigger type, step name, edge (enabled + name)
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfwebhooksteptruewebhook-step');
});
test('ordering: steps with partial fields maintain sorted key order', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
a-step(name=step,adaptor=http)
`
);
// Step keys sorted: adaptor, body, keychain_credential_id, name, project_credential_id
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfahttpsteptruea-step');
});
test('ordering: edge with js_expression condition', (t) => {
const workflow = generateWorkflow(
`
@name wf
@id some-id
a-(condition="state.x > 5",label=check)-b
`
);
// Edge keys sorted: condition_expression, condition_label, condition_type, enabled, label, name
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:wfabstate.x > 5checkjs_expressiontruea-b');
});
test('ordering: undefined fields are omitted', (t) => {
const workflow1 = generateWorkflow(
`
@name wf
@id some-id
a-b(name=b)
`
);
const workflow2 = generateWorkflow(
`
@name wf
@id some-id
a-b(name=b)
`
);
// Both should produce the same hash
const hash1 = workflow1.getVersionHash({ sha: false });
const hash2 = workflow2.getVersionHash({ sha: false });
t.is(hash1, 'cli:wfabtruea-b');
t.is(hash1, hash2);
});
// TODO more ordering tests
test('unique hash but different steps order', (t) => {
const workflow1 = generateWorkflow(
`
@name same-workflow
@id id-one
a-b
a-c
a-d
`
);
// different order of nodes but should generate the same hash
const workflow2 = generateWorkflow(
`
@name same-workflow
@id id-two
a-d
a-c
a-b
`
);
// validate second step is actually different
t.is(workflow1.steps[1].name, 'b');
t.is(workflow2.steps[1].name, 'd');
// assert that hashes are the same
t.is(generateHash(workflow1), generateHash(workflow2));
});
/**
*
* different name/adaptor/credential/expression should generate different hash
* Other keys should be ignored
*
* key order doesn't matter (arbitrary order, sort, inverse sort)
*
* include a prefix
*/
test('hash changes when workflow name changes', (t) => {
const wf1 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b
b-c
`
);
const wf2 = generateWorkflow(
`
@name wf-2
@id workflow-id
a-b
b-c
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test('hash a trigger', (t) => {
// check that various changes on a trigger update the hash
const webhook = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=webhook)-x(expression=x)
`
);
const cron = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=cron)-x(expression=x)
`
);
t.not(generateHash(webhook), generateHash(cron));
const cronEnabled = generateWorkflow(
`@name wf-1
@id workflow-id
t(enabled=false)-x
`
);
t.not(generateHash(webhook), generateHash(cronEnabled));
const cronExpression = generateWorkflow(
`@name wf-1
@id workflow-id
t(cron_expression="1")-x
`
);
t.not(generateHash(webhook), generateHash(cronExpression));
});
test('hash changes when webhook_reply changes', (t) => {
const wf1 = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=webhook,webhook_reply=x)-x
`
);
const wf2 = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=webhook,webhook_reply=y)-x
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test('hash changes when cron_cursor_job_id changes', (t) => {
const wf1 = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=cron,cron_expression="1",cron_cursor_job_id=x)-x
`
);
const wf2 = generateWorkflow(
`@name wf-1
@id workflow-id
t(type=cron,cron_expression="1",cron_cursor_job_id=y)-x
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test('hash changes across an edge', (t) => {
const basicEdge = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b
`
);
const withLabel = generateWorkflow(
`
@name wf-1
@id workflow-id
a-(label=x)-b
`
);
t.not(generateHash(basicEdge), generateHash(withLabel));
const withCondition = generateWorkflow(
`
@name wf-1
@id workflow-id
a-(condition=always)-b
`
);
t.not(generateHash(basicEdge), generateHash(withCondition));
const withDisabled = generateWorkflow(
`
@name wf-1
@id workflow-id
a-(disabled=true)-b
`
);
t.not(generateHash(basicEdge), generateHash(withDisabled));
});
// TODO joe to think more about credential mapping (keychain and project cred keys)
// can't get credentials to work in the generator, need to fix that
test.skip('hash changes when credentials field changes', (t) => {
const wf1 = generateWorkflow(
`
@name wf-1
@credentials cred-1
@id workflow-id
a-b
b-c
`
);
const wf2 = generateWorkflow(
`
@name wf-1
@credentials cred-2
@id workflow-id
a-b
b-c
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test("hash changes when a step's adaptor changes", (t) => {
const wf1 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b(adaptor=http)
b-c
`
);
const wf2 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b(adaptor=common)
b-c
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test("hash changes when a step's expression changes", (t) => {
const wf1 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b
b-c(expression="x=1")
`
);
const wf2 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b
b-c(expression="x=2")
`
);
t.not(generateHash(wf1), generateHash(wf2));
});
test('ignored fields do not affect hash', (t) => {
const wf1 = generateWorkflow(
`
@name wf-1
@id workflow-id
a-b
b-c(expression="x=1")
`
);
const wf1_ignored = generateWorkflow(
`
@name wf-1
@id workflow-id
@unknownfield some-value
a-b
b-c(expression="x=1")
`
);
t.is(generateHash(wf1), generateHash(wf1_ignored));
});
// This test is important because when merging, the local workflow
// representation won't have UUIDs in it - and that should be fine, nothing should break
test('works without UUIDs', (t) => {
const workflow = generateWorkflow(
`
@name a
@id some-id
webhook-transform_data(name="Transform data",expression="fn(s => s)")
`,
{
openfnUuid: false,
}
);
const hash = workflow.getVersionHash({ sha: false });
t.is(hash, 'cli:awebhookfn(s => s)Transform datatruewebhook-Transform data');
});