@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.
@@ -1,305 +1,249 @@
1
- import { type TaskRecord, createTask, Schema } from '../index'
1
+ import { type ExecutionRecord, createTask, Schema } from '../index'
2
2
 
3
- describe('Listener with boundaries tests', () => {
4
- it('Should record one item and its boundaries tape', async () => {
5
- const tape: TaskRecord<{ value: number }, { value: number, foo: boolean }>[] = []
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: 'task',
22
- schema,
23
- boundaries,
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 externalData = await boundaries.fetchExternalData()
26
- return { ...externalData, ...argv }
17
+ const ten = await boundaries.getTen()
18
+ return { value: argv.value, foo: ten > 5 }
27
19
  }
28
20
  })
29
21
 
30
- task.addListener<{ value: number }, { value: number, foo: boolean }>((record) => {
22
+ task.addListener((record) => {
31
23
  tape.push(record)
32
24
  })
33
25
 
34
26
  await task.run({ value: 5 })
35
-
36
- expect(tape.length).toBe(1)
37
- expect(tape[0].input).toEqual({ value: 5 })
38
- expect(tape[0].output).toEqual({ value: 5, foo: false })
39
- expect(tape[0].boundaries).toEqual({
40
- fetchExternalData: [
41
- { input: [], output: { foo: false } }
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 record multiple items and their boundaries tape', async () => {
47
- const tape: TaskRecord<{ value: number }, { value: number, foo: boolean }>[] = []
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: 'task',
64
- schema,
65
- boundaries,
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 externalData = await boundaries.fetchExternalData()
68
- return { ...externalData, ...argv }
55
+ const ten = await boundaries.getTen()
56
+ return { value: argv.value, foo: ten > 5 }
69
57
  }
70
58
  })
71
59
 
72
- task.addListener<{ value: number }, { value: number, foo: boolean }>((record) => {
60
+ task.addListener((record) => {
73
61
  tape.push(record)
74
62
  })
75
63
 
76
- await task.run({ value: 5 })
77
- await task.run({ value: 6 })
78
-
79
- expect(tape.length).toBe(2)
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[1].input).toEqual({ value: 6 })
90
- expect(tape[1].output).toEqual({ value: 6, foo: false })
91
- expect(tape[1].boundaries).toEqual({
92
- fetchExternalData: [
93
- { input: [], output: { foo: false } }
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 record error and its boundaries tape', async () => {
99
- const tape: TaskRecord<Record<string, unknown>, { value: number, foo: boolean }>[] = []
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: 'task',
116
- schema,
117
- boundaries,
118
- fn: async (argv, boundaries) => {
119
- const externalData = await boundaries.fetchExternalData()
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
- return { ...externalData, ...argv as { value: number } }
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<Record<string, unknown>, { value: number, foo: boolean }>((record) => {
101
+ task.addListener((record) => {
129
102
  tape.push(record)
130
103
  })
131
104
 
132
- try {
133
- await task.run({})
134
- } catch (e) {
135
- // Error is expected
136
- }
137
-
138
- expect(tape.length).toBe(1)
139
- expect(tape[0].input).toEqual({})
140
- expect(tape[0].error).toBe('Value is required')
141
- expect(tape[0].boundaries).toEqual({
142
- fetchExternalData: [
143
- { input: [], output: { foo: false } }
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 record error + success and their boundaries tape', async () => {
149
- const tape: TaskRecord<{ value?: number }, { value: number, foo: boolean }>[] = []
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: 'task',
166
- schema,
167
- boundaries,
168
- fn: async (argv, boundaries) => {
169
- const externalData = await boundaries.fetchExternalData()
170
- if (typeof argv.value === 'undefined') {
171
- throw new Error('Value is required')
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
- return { ...externalData, ...argv as { value: number } }
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<{ value?: number }, { value: number, foo: boolean }>((record) => {
139
+ task.addListener((record) => {
179
140
  tape.push(record)
180
141
  })
181
142
 
182
- try {
183
- await task.run({})
184
- } catch (e) {
185
- // Error is expected
186
- }
187
- await task.run({ value: 5 })
188
-
189
- expect(tape.length).toBe(2)
190
- expect(tape[0].input).toEqual({})
191
- expect(tape[0].error).toBe('Value is required')
192
- expect(tape[0].boundaries).toEqual({
193
- fetchExternalData: [
194
- { input: [], output: { foo: false } }
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 record 2 run logs if boundary called twice', async () => {
208
- const tape: TaskRecord<{ value: number }, { foo: boolean }>[] = []
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: 'task',
225
- schema,
226
- boundaries,
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.fetchExternalData()
229
- await boundaries.fetchExternalData()
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<{ value: number }, { foo: boolean }>((record) => {
178
+ task.addListener((record) => {
236
179
  tape.push(record)
237
180
  })
238
181
 
239
- await task.run({ value: 5 })
240
-
241
- expect(tape.length).toBe(1)
242
- expect(tape[0].input).toEqual({ value: 5 })
243
- expect(tape[0].output).toEqual({ foo: true })
244
- expect(tape[0].boundaries).toEqual({
245
- fetchExternalData: [
246
- { input: [], output: { foo: false } },
247
- { input: [], output: { foo: false } }
248
- ]
249
- })
250
- })
251
-
252
- it('Should record 2 boundary logs', async () => {
253
- const tape: TaskRecord<{ value: number }, number>[] = []
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
- subtract: async (value: number): Promise<number> => {
266
- return value - 1
267
- }
268
- }
198
+ taskName: 'test',
199
+ metadata: {},
200
+ type: 'success'
201
+ }])
202
+ })
269
203
 
270
- // Create the task using createTask
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: 'task',
273
- schema,
274
- boundaries,
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 counter = argv.value
277
-
278
- counter = await boundaries.add(counter)
279
- counter = await boundaries.subtract(counter)
280
- counter = await boundaries.subtract(counter)
281
-
282
- return counter
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<{ value: number }, number>((record) => {
230
+ task.addListener((record) => {
287
231
  tape.push(record)
288
232
  })
289
233
 
290
- await task.run({ value: 5 })
291
-
292
- expect(tape.length).toBe(1)
293
- expect(tape[0].input).toEqual({ value: 5 })
294
- expect(tape[0].output).toBe(4)
295
- expect(tape[0].boundaries).toEqual({
296
- add: [
297
- { input: [5], output: 6 }
298
- ],
299
- subtract: [
300
- { input: [6], output: 5 },
301
- { input: [5], output: 4 }
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
  })