@forgehive/task 0.2.0 → 0.2.2
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.
- package/README.md +164 -10
- package/dist/index.d.ts +20 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -6
- package/dist/index.js.map +1 -1
- package/dist/test/add-listener-with-boundaries.test.js +175 -200
- package/dist/test/add-listener-with-boundaries.test.js.map +1 -1
- package/dist/test/add-listener.test.js +80 -52
- package/dist/test/add-listener.test.js.map +1 -1
- package/dist/test/execution-record-boundaries.test.d.ts +2 -0
- package/dist/test/execution-record-boundaries.test.d.ts.map +1 -0
- package/dist/test/execution-record-boundaries.test.js +220 -0
- package/dist/test/execution-record-boundaries.test.js.map +1 -0
- package/dist/test/task-with-boundaries.test.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +78 -28
- package/src/test/add-listener-with-boundaries.test.ts +186 -242
- package/src/test/add-listener.test.ts +90 -64
- package/src/test/execution-record-boundaries.test.ts +266 -0
- package/src/test/task-with-boundaries.test.ts +3 -3
|
@@ -1,305 +1,249 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ExecutionRecord, createTask, Schema } from '../index'
|
|
2
2
|
|
|
3
|
-
describe('Listener with boundaries
|
|
4
|
-
it('Should
|
|
5
|
-
const tape:
|
|
6
|
-
|
|
7
|
-
// Create a schema for the task
|
|
8
|
-
const schema = new Schema({
|
|
9
|
-
value: Schema.number()
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
// Define the boundaries
|
|
13
|
-
const boundaries = {
|
|
14
|
-
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
15
|
-
return { foo: false }
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// Create the task using createTask
|
|
3
|
+
describe('Listener with boundaries', () => {
|
|
4
|
+
it('Should add a listener to the task and capture boundaries', async () => {
|
|
5
|
+
const tape: ExecutionRecord[] = []
|
|
20
6
|
const task = createTask({
|
|
21
|
-
name: '
|
|
22
|
-
schema
|
|
23
|
-
|
|
7
|
+
name: 'test',
|
|
8
|
+
schema: new Schema({
|
|
9
|
+
value: Schema.number()
|
|
10
|
+
}),
|
|
11
|
+
boundaries: {
|
|
12
|
+
getTen: async () => {
|
|
13
|
+
return 10
|
|
14
|
+
}
|
|
15
|
+
},
|
|
24
16
|
fn: async (argv, boundaries) => {
|
|
25
|
-
const
|
|
26
|
-
return {
|
|
17
|
+
const ten = await boundaries.getTen()
|
|
18
|
+
return { value: argv.value, foo: ten > 5 }
|
|
27
19
|
}
|
|
28
20
|
})
|
|
29
21
|
|
|
30
|
-
task.addListener
|
|
22
|
+
task.addListener((record) => {
|
|
31
23
|
tape.push(record)
|
|
32
24
|
})
|
|
33
25
|
|
|
34
26
|
await task.run({ value: 5 })
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
expect(tape).toEqual([{
|
|
28
|
+
input: { value: 5 },
|
|
29
|
+
output: { value: 5, foo: true },
|
|
30
|
+
boundaries: {
|
|
31
|
+
getTen: [{
|
|
32
|
+
input: [],
|
|
33
|
+
output: 10
|
|
34
|
+
}]
|
|
35
|
+
},
|
|
36
|
+
taskName: 'test',
|
|
37
|
+
metadata: {},
|
|
38
|
+
type: 'success'
|
|
39
|
+
}])
|
|
44
40
|
})
|
|
45
41
|
|
|
46
|
-
it('Should
|
|
47
|
-
const tape:
|
|
48
|
-
|
|
49
|
-
// Create a schema for the task
|
|
50
|
-
const schema = new Schema({
|
|
51
|
-
value: Schema.number()
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
// Define the boundaries
|
|
55
|
-
const boundaries = {
|
|
56
|
-
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
57
|
-
return { foo: false }
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Create the task using createTask
|
|
42
|
+
it('Should add a listener to the task and capture boundaries with error', async () => {
|
|
43
|
+
const tape: ExecutionRecord[] = []
|
|
62
44
|
const task = createTask({
|
|
63
|
-
name: '
|
|
64
|
-
schema
|
|
65
|
-
|
|
45
|
+
name: 'test',
|
|
46
|
+
schema: new Schema({
|
|
47
|
+
value: Schema.number()
|
|
48
|
+
}),
|
|
49
|
+
boundaries: {
|
|
50
|
+
getTen: async () => {
|
|
51
|
+
throw new Error('Network error')
|
|
52
|
+
}
|
|
53
|
+
},
|
|
66
54
|
fn: async (argv, boundaries) => {
|
|
67
|
-
const
|
|
68
|
-
return {
|
|
55
|
+
const ten = await boundaries.getTen()
|
|
56
|
+
return { value: argv.value, foo: ten > 5 }
|
|
69
57
|
}
|
|
70
58
|
})
|
|
71
59
|
|
|
72
|
-
task.addListener
|
|
60
|
+
task.addListener((record) => {
|
|
73
61
|
tape.push(record)
|
|
74
62
|
})
|
|
75
63
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
expect(tape[0].input).toEqual({ value: 5 })
|
|
82
|
-
expect(tape[0].output).toEqual({ value: 5, foo: false })
|
|
83
|
-
expect(tape[0].boundaries).toEqual({
|
|
84
|
-
fetchExternalData: [
|
|
85
|
-
{ input: [], output: { foo: false } }
|
|
86
|
-
]
|
|
87
|
-
})
|
|
64
|
+
try {
|
|
65
|
+
await task.run({ value: 5 })
|
|
66
|
+
} catch (error) {
|
|
67
|
+
// Expected error
|
|
68
|
+
}
|
|
88
69
|
|
|
89
|
-
expect(tape
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
70
|
+
expect(tape).toEqual([{
|
|
71
|
+
input: { value: 5 },
|
|
72
|
+
error: 'Network error',
|
|
73
|
+
boundaries: {
|
|
74
|
+
getTen: [{
|
|
75
|
+
input: [],
|
|
76
|
+
error: 'Network error'
|
|
77
|
+
}]
|
|
78
|
+
},
|
|
79
|
+
taskName: 'test',
|
|
80
|
+
metadata: {},
|
|
81
|
+
type: 'error'
|
|
82
|
+
}])
|
|
96
83
|
})
|
|
97
84
|
|
|
98
|
-
it('Should
|
|
99
|
-
const tape:
|
|
100
|
-
|
|
101
|
-
// Create a schema for the task
|
|
102
|
-
const schema = new Schema({
|
|
103
|
-
value: Schema.number().optional()
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
// Define the boundaries
|
|
107
|
-
const boundaries = {
|
|
108
|
-
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
109
|
-
return { foo: false }
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Create the task using createTask
|
|
85
|
+
it('Should add a listener to the task and capture boundaries with dynamic parameters', async () => {
|
|
86
|
+
const tape: ExecutionRecord[] = []
|
|
114
87
|
const task = createTask({
|
|
115
|
-
name: '
|
|
116
|
-
schema,
|
|
117
|
-
boundaries
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (typeof argv.value === 'undefined') {
|
|
121
|
-
throw new Error('Value is required')
|
|
88
|
+
name: 'test',
|
|
89
|
+
schema: new Schema({}),
|
|
90
|
+
boundaries: {
|
|
91
|
+
addNumbers: async (a: number, b: number) => {
|
|
92
|
+
return a + b
|
|
122
93
|
}
|
|
123
|
-
|
|
124
|
-
|
|
94
|
+
},
|
|
95
|
+
fn: async (argv, boundaries) => {
|
|
96
|
+
const sum = await boundaries.addNumbers(3, 7)
|
|
97
|
+
return { value: sum, foo: sum > 5 }
|
|
125
98
|
}
|
|
126
99
|
})
|
|
127
100
|
|
|
128
|
-
task.addListener
|
|
101
|
+
task.addListener((record) => {
|
|
129
102
|
tape.push(record)
|
|
130
103
|
})
|
|
131
104
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
})
|
|
105
|
+
await task.run({})
|
|
106
|
+
expect(tape).toEqual([{
|
|
107
|
+
input: {},
|
|
108
|
+
output: { value: 10, foo: true },
|
|
109
|
+
boundaries: {
|
|
110
|
+
addNumbers: [{
|
|
111
|
+
input: [3, 7],
|
|
112
|
+
output: 10
|
|
113
|
+
}]
|
|
114
|
+
},
|
|
115
|
+
taskName: 'test',
|
|
116
|
+
metadata: {},
|
|
117
|
+
type: 'success'
|
|
118
|
+
}])
|
|
146
119
|
})
|
|
147
120
|
|
|
148
|
-
it('Should
|
|
149
|
-
const tape:
|
|
150
|
-
|
|
151
|
-
// Create a schema for the task
|
|
152
|
-
const schema = new Schema({
|
|
153
|
-
value: Schema.number().optional()
|
|
154
|
-
})
|
|
155
|
-
|
|
156
|
-
// Define the boundaries
|
|
157
|
-
const boundaries = {
|
|
158
|
-
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
159
|
-
return { foo: false }
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Create the task using createTask
|
|
121
|
+
it('Should add a listener to the task and capture boundaries with optional parameters', async () => {
|
|
122
|
+
const tape: ExecutionRecord[] = []
|
|
164
123
|
const task = createTask({
|
|
165
|
-
name: '
|
|
166
|
-
schema
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
124
|
+
name: 'test',
|
|
125
|
+
schema: new Schema({
|
|
126
|
+
value: Schema.number().optional()
|
|
127
|
+
}),
|
|
128
|
+
boundaries: {
|
|
129
|
+
processValue: async (val?: number) => {
|
|
130
|
+
return val || 0
|
|
172
131
|
}
|
|
173
|
-
|
|
174
|
-
|
|
132
|
+
},
|
|
133
|
+
fn: async (argv, boundaries) => {
|
|
134
|
+
const processed = await boundaries.processValue(argv.value)
|
|
135
|
+
return { value: processed, foo: processed > 5 }
|
|
175
136
|
}
|
|
176
137
|
})
|
|
177
138
|
|
|
178
|
-
task.addListener
|
|
139
|
+
task.addListener((record) => {
|
|
179
140
|
tape.push(record)
|
|
180
141
|
})
|
|
181
142
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
expect(tape[1].input).toEqual({ value: 5 })
|
|
199
|
-
expect(tape[1].output).toEqual({ value: 5, foo: false })
|
|
200
|
-
expect(tape[1].boundaries).toEqual({
|
|
201
|
-
fetchExternalData: [
|
|
202
|
-
{ input: [], output: { foo: false } }
|
|
203
|
-
]
|
|
204
|
-
})
|
|
143
|
+
await task.run({ value: undefined })
|
|
144
|
+
expect(tape).toEqual([{
|
|
145
|
+
input: { value: undefined },
|
|
146
|
+
output: { value: 0, foo: false },
|
|
147
|
+
boundaries: {
|
|
148
|
+
processValue: [{
|
|
149
|
+
input: [undefined],
|
|
150
|
+
output: 0
|
|
151
|
+
}]
|
|
152
|
+
},
|
|
153
|
+
taskName: 'test',
|
|
154
|
+
metadata: {},
|
|
155
|
+
type: 'success'
|
|
156
|
+
}])
|
|
205
157
|
})
|
|
206
158
|
|
|
207
|
-
it('Should
|
|
208
|
-
const tape:
|
|
209
|
-
|
|
210
|
-
// Create a schema for the task
|
|
211
|
-
const schema = new Schema({
|
|
212
|
-
value: Schema.number()
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
// Define the boundaries
|
|
216
|
-
const boundaries = {
|
|
217
|
-
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
218
|
-
return { foo: false }
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Create the task using createTask
|
|
159
|
+
it('Should add a listener to the task and capture boundaries with multiple calls', async () => {
|
|
160
|
+
const tape: ExecutionRecord[] = []
|
|
223
161
|
const task = createTask({
|
|
224
|
-
name: '
|
|
225
|
-
schema
|
|
226
|
-
|
|
162
|
+
name: 'test',
|
|
163
|
+
schema: new Schema({
|
|
164
|
+
value: Schema.number()
|
|
165
|
+
}),
|
|
166
|
+
boundaries: {
|
|
167
|
+
multiplyByTwo: async (num: number) => {
|
|
168
|
+
return num * 2
|
|
169
|
+
}
|
|
170
|
+
},
|
|
227
171
|
fn: async (argv, boundaries) => {
|
|
228
|
-
await boundaries.
|
|
229
|
-
await boundaries.
|
|
230
|
-
|
|
231
|
-
return { foo: true }
|
|
172
|
+
const doubled = await boundaries.multiplyByTwo(argv.value)
|
|
173
|
+
const quadrupled = await boundaries.multiplyByTwo(doubled)
|
|
174
|
+
return { foo: quadrupled > 10 }
|
|
232
175
|
}
|
|
233
176
|
})
|
|
234
177
|
|
|
235
|
-
task.addListener
|
|
178
|
+
task.addListener((record) => {
|
|
236
179
|
tape.push(record)
|
|
237
180
|
})
|
|
238
181
|
|
|
239
|
-
await task.run({ value:
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
// Create a schema for the task
|
|
256
|
-
const schema = new Schema({
|
|
257
|
-
value: Schema.number()
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
// Define the boundaries
|
|
261
|
-
const boundaries = {
|
|
262
|
-
add: async (value: number): Promise<number> => {
|
|
263
|
-
return value + 1
|
|
182
|
+
await task.run({ value: 3 })
|
|
183
|
+
expect(tape).toEqual([{
|
|
184
|
+
input: { value: 3 },
|
|
185
|
+
output: { foo: true },
|
|
186
|
+
boundaries: {
|
|
187
|
+
multiplyByTwo: [
|
|
188
|
+
{
|
|
189
|
+
input: [3],
|
|
190
|
+
output: 6
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
input: [6],
|
|
194
|
+
output: 12
|
|
195
|
+
}
|
|
196
|
+
]
|
|
264
197
|
},
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
198
|
+
taskName: 'test',
|
|
199
|
+
metadata: {},
|
|
200
|
+
type: 'success'
|
|
201
|
+
}])
|
|
202
|
+
})
|
|
269
203
|
|
|
270
|
-
|
|
204
|
+
it('Should add a listener to the task and capture boundaries with mixed results', async () => {
|
|
205
|
+
const tape: ExecutionRecord[] = []
|
|
271
206
|
const task = createTask({
|
|
272
|
-
name: '
|
|
273
|
-
schema
|
|
274
|
-
|
|
207
|
+
name: 'test',
|
|
208
|
+
schema: new Schema({
|
|
209
|
+
value: Schema.number()
|
|
210
|
+
}),
|
|
211
|
+
boundaries: {
|
|
212
|
+
getResult: async (num: number) => {
|
|
213
|
+
if (num > 5) {
|
|
214
|
+
return num * 2
|
|
215
|
+
}
|
|
216
|
+
throw new Error('Number too small')
|
|
217
|
+
}
|
|
218
|
+
},
|
|
275
219
|
fn: async (argv, boundaries) => {
|
|
276
|
-
let
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return
|
|
220
|
+
let result = 0
|
|
221
|
+
try {
|
|
222
|
+
result = await boundaries.getResult(argv.value)
|
|
223
|
+
} catch (error) {
|
|
224
|
+
// Continue with default
|
|
225
|
+
}
|
|
226
|
+
return result
|
|
283
227
|
}
|
|
284
228
|
})
|
|
285
229
|
|
|
286
|
-
task.addListener
|
|
230
|
+
task.addListener((record) => {
|
|
287
231
|
tape.push(record)
|
|
288
232
|
})
|
|
289
233
|
|
|
290
|
-
await task.run({ value:
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
})
|
|
234
|
+
await task.run({ value: 3 })
|
|
235
|
+
expect(tape).toEqual([{
|
|
236
|
+
input: { value: 3 },
|
|
237
|
+
output: 0,
|
|
238
|
+
boundaries: {
|
|
239
|
+
getResult: [{
|
|
240
|
+
input: [3],
|
|
241
|
+
error: 'Number too small'
|
|
242
|
+
}]
|
|
243
|
+
},
|
|
244
|
+
taskName: 'test',
|
|
245
|
+
metadata: {},
|
|
246
|
+
type: 'success'
|
|
247
|
+
}])
|
|
304
248
|
})
|
|
305
249
|
})
|