@manablox/workflows 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,129 @@
1
+ import { ManabloxError, type WorkflowStep } from '@manablox/core';
2
+ import { describe, expect, it } from 'vitest';
3
+ import { validateWorkflow, type WorkflowInput } from '../src/validate.js';
4
+
5
+ const env = { typeExists: (id: string) => id === 'page' };
6
+
7
+ const email: WorkflowStep = {
8
+ id: '',
9
+ name: '',
10
+ enabled: true,
11
+ continueOnError: false,
12
+ type: 'email',
13
+ to: ['x@example.com'],
14
+ toRoles: [],
15
+ subject: 'Hi',
16
+ body: '',
17
+ html: false,
18
+ };
19
+
20
+ const valid: WorkflowInput = {
21
+ name: 'Notify',
22
+ trigger: { kind: 'event', events: ['content.saved'], typeIds: ['page'], locales: [] },
23
+ steps: [email],
24
+ };
25
+
26
+ function problems(input: WorkflowInput): string[] {
27
+ try {
28
+ validateWorkflow(input, env);
29
+ return [];
30
+ } catch (error) {
31
+ if (!ManabloxError.is(error)) throw error;
32
+ return error.details.map((detail) => `${detail.key}@${(detail.path ?? []).join('.')}`);
33
+ }
34
+ }
35
+
36
+ describe('validateWorkflow', () => {
37
+ it('passes a sound workflow and assigns step ids', () => {
38
+ const out = validateWorkflow(valid, env);
39
+ expect(out.steps[0]?.id).toMatch(/^[0-9a-f-]{36}$/);
40
+ expect(out.enabled).toBe(false);
41
+ });
42
+
43
+ it('reports every problem with its path', () => {
44
+ expect(
45
+ problems({
46
+ name: ' ',
47
+ trigger: { kind: 'event', events: [], typeIds: ['nope'], locales: [] },
48
+ steps: [],
49
+ }),
50
+ ).toEqual([
51
+ 'workflow.name.required@name',
52
+ 'workflow.trigger.eventsRequired@trigger.events',
53
+ 'workflow.trigger.typeNotFound@trigger.typeIds.0',
54
+ 'workflow.steps.required@steps',
55
+ ]);
56
+ });
57
+
58
+ it('checks a schedule', () => {
59
+ expect(
60
+ problems({
61
+ ...valid,
62
+ trigger: {
63
+ kind: 'schedule',
64
+ cron: '* * *',
65
+ timezone: 'Nowhere/Land',
66
+ selection: null,
67
+ perDocument: false,
68
+ },
69
+ }),
70
+ ).toEqual([
71
+ 'workflow.trigger.cronInvalid@trigger.cron',
72
+ 'workflow.trigger.timezoneInvalid@trigger.timezone',
73
+ ]);
74
+ });
75
+
76
+ it('checks each step type', () => {
77
+ expect(problems({ ...valid, steps: [{ ...email, to: [], subject: '' }] })).toEqual([
78
+ 'workflow.step.email.recipientRequired@steps.0.to',
79
+ 'workflow.step.email.subjectRequired@steps.0.subject',
80
+ ]);
81
+ expect(problems({ ...valid, steps: [{ ...email, to: ['not-an-address'] }] })).toEqual([
82
+ 'workflow.step.email.recipientInvalid@steps.0.to.0',
83
+ ]);
84
+ expect(problems({ ...valid, steps: [{ ...email, to: ['{{ actor.email }}'] }] })).toEqual([]);
85
+ expect(
86
+ problems({
87
+ ...valid,
88
+ steps: [
89
+ {
90
+ id: '',
91
+ name: '',
92
+ enabled: true,
93
+ continueOnError: false,
94
+ type: 'http',
95
+ method: 'POST',
96
+ url: 'ftp://x',
97
+ headers: [{ name: 'bad header', value: 'x' }],
98
+ body: { mode: 'event', template: '' },
99
+ secret: null,
100
+ timeoutMs: 10_000,
101
+ },
102
+ ],
103
+ }),
104
+ ).toEqual([
105
+ 'workflow.step.http.urlInvalid@steps.0.url',
106
+ 'workflow.step.http.headerNameInvalid@steps.0.headers.0.name',
107
+ ]);
108
+ expect(
109
+ problems({
110
+ ...valid,
111
+ steps: [
112
+ {
113
+ id: '',
114
+ name: '',
115
+ enabled: true,
116
+ continueOnError: false,
117
+ type: 'condition',
118
+ match: 'all',
119
+ rules: [],
120
+ },
121
+ { id: '', name: '', enabled: true, continueOnError: false, type: 'delay', minutes: 0 },
122
+ ],
123
+ }),
124
+ ).toEqual([
125
+ 'workflow.step.condition.rulesRequired@steps.0.rules',
126
+ 'workflow.step.delay.minutesInvalid@steps.1.minutes',
127
+ ]);
128
+ });
129
+ });
package/tsconfig.json ADDED
@@ -0,0 +1 @@
1
+ { "extends": "@manablox/config-typescript/library.json", "include": ["src", "test"] }
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ export default defineConfig({
3
+ test: {
4
+ environment: 'node',
5
+ include: ['test/**/*.test.ts'],
6
+ testTimeout: 60_000,
7
+ hookTimeout: 60_000,
8
+ },
9
+ });