@forgehive/task 0.2.0 → 0.2.1

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,28 +1,31 @@
1
- import { Task, type TaskRecord } from '../index'
2
- import { Schema, type InferSchema } from '@forgehive/schema'
3
-
4
- describe('Listener tests', () => {
5
- it('Should record one item', async () => {
6
- const tape: TaskRecord<{ value: number }, { value: number }>[] = []
7
- const task = new Task(function (_argv: { value: number }) {
8
- return _argv
1
+ import { Task, type ExecutionRecord } from '../index'
2
+
3
+ describe('Add listener', () => {
4
+ it('Should add a listener to the task', async () => {
5
+ const tape: ExecutionRecord[] = []
6
+ const task = new Task(async (argv: { value: number }) => {
7
+ return { value: argv.value }
9
8
  })
10
9
 
11
10
  task.addListener((record) => {
12
11
  tape.push(record)
13
12
  })
14
13
 
15
- await task.run({ value: 5 })
16
-
17
- expect(tape.length).toBe(1)
18
- expect(tape[0].input).toEqual({ value: 5 })
19
- expect(tape[0].output).toEqual({ value: 5 })
14
+ await task.run({ value: 1 })
15
+ expect(tape).toEqual([{
16
+ input: { value: 1 },
17
+ output: { value: 1 },
18
+ boundaries: {},
19
+ taskName: undefined,
20
+ metadata: {},
21
+ type: 'success'
22
+ }])
20
23
  })
21
24
 
22
- it('Should record execution error', async () => {
23
- const tape: TaskRecord<{ value: number }, never>[] = []
24
- const task = new Task(function (_argv: { value: number }) {
25
- throw new Error('This should happen')
25
+ it('Should add a listener to the task and catch error', async () => {
26
+ const tape: ExecutionRecord[] = []
27
+ const task = new Task(async (_argv: { value: number }) => {
28
+ throw new Error('Test error')
26
29
  })
27
30
 
28
31
  task.addListener((record) => {
@@ -30,83 +33,106 @@ describe('Listener tests', () => {
30
33
  })
31
34
 
32
35
  try {
33
- await task.run({ value: 5 })
34
- } catch (e) {
35
- // Error is expected
36
+ await task.run({ value: 1 })
37
+ } catch (error) {
38
+ // Expected error
36
39
  }
37
40
 
38
- expect(tape.length).toBe(1)
39
- expect(tape[0].input).toEqual({ value: 5 })
40
- expect(tape[0].error).toBe('This should happen')
41
+ expect(tape).toEqual([{
42
+ input: { value: 1 },
43
+ error: 'Test error',
44
+ boundaries: {},
45
+ taskName: undefined,
46
+ metadata: {},
47
+ type: 'error'
48
+ }])
41
49
  })
42
50
 
43
- it('Should record validation error', async () => {
44
- const tape: TaskRecord<{ value: number | null }, { value: number }>[] = []
45
- const schema = new Schema({
46
- value: Schema.number().min(5)
47
- })
48
-
49
- const task = new Task(function (_argv: InferSchema<typeof schema>) {
50
- return _argv
51
- }, {
52
- schema
51
+ it('Should add a listener to the task and catch error with null input', async () => {
52
+ const tape: ExecutionRecord[] = []
53
+ const task = new Task(async (argv: { value: number | null }) => {
54
+ if (argv.value === null) {
55
+ throw new Error('Value is null')
56
+ }
57
+ return { value: argv.value }
53
58
  })
54
59
 
55
- task.addListener<{ value: number | null }, { value: number }>((record) => {
60
+ task.addListener((record) => {
56
61
  tape.push(record)
57
62
  })
58
63
 
59
64
  try {
60
- await task.run({ value: 3 })
61
- } catch (e) {
62
- // Error is expected
65
+ await task.run({ value: null })
66
+ } catch (error) {
67
+ // Expected error
63
68
  }
64
69
 
65
- expect(tape.length).toBe(1)
66
- expect(tape[0].input).toEqual({ value: 3 })
67
- expect(tape[0].error).toContain('Invalid input on:')
68
- expect(tape[0].error).toContain('value:')
69
- expect(tape[0].error).toContain('Number must be greater than or equal to 5')
70
+ expect(tape).toEqual([{
71
+ input: { value: null },
72
+ error: 'Value is null',
73
+ boundaries: {},
74
+ taskName: undefined,
75
+ metadata: {},
76
+ type: 'error'
77
+ }])
70
78
  })
71
79
 
72
- it('Should record multiple records', async () => {
73
- const tape: TaskRecord<{ value: number }, { value: number }>[] = []
74
- const task = new Task(function (_argv: { value: number }) {
75
- return _argv
80
+ it('Should add a listener to the task and call twice', async () => {
81
+ const tape: ExecutionRecord[] = []
82
+ const task = new Task(async (argv: { value: number }) => {
83
+ return { value: argv.value }
76
84
  })
77
85
 
78
86
  task.addListener((record) => {
79
87
  tape.push(record)
80
88
  })
81
89
 
82
- await task.run({ value: 5 })
83
- await task.run({ value: 6 })
84
-
85
- expect(tape.length).toBe(2)
86
- expect(tape[0].input).toEqual({ value: 5 })
87
- expect(tape[0].output).toEqual({ value: 5 })
88
-
89
- expect(tape[1].input).toEqual({ value: 6 })
90
- expect(tape[1].output).toEqual({ value: 6 })
90
+ await task.run({ value: 1 })
91
+ await task.run({ value: 2 })
92
+
93
+ expect(tape).toEqual([
94
+ {
95
+ input: { value: 1 },
96
+ output: { value: 1 },
97
+ boundaries: {},
98
+ taskName: undefined,
99
+ metadata: {},
100
+ type: 'success'
101
+ },
102
+ {
103
+ input: { value: 2 },
104
+ output: { value: 2 },
105
+ boundaries: {},
106
+ taskName: undefined,
107
+ metadata: {},
108
+ type: 'success'
109
+ }
110
+ ])
91
111
  })
92
112
 
93
- it('Should stop recording', async () => {
94
- const tape: TaskRecord<{ value: number }, { value: number }>[] = []
95
- const task = new Task(function (_argv: { value: number }) {
96
- return _argv
113
+ it('Should add a listener and remove it', async () => {
114
+ const tape: ExecutionRecord[] = []
115
+ const task = new Task(async (argv: { value: number }) => {
116
+ return { value: argv.value }
97
117
  })
98
118
 
99
119
  task.addListener((record) => {
100
120
  tape.push(record)
101
121
  })
102
122
 
103
- await task.run({ value: 5 })
123
+ await task.run({ value: 1 })
104
124
 
105
125
  task.removeListener()
106
- await task.run({ value: 6 })
107
126
 
108
- expect(tape.length).toBe(1)
109
- expect(tape[0].input).toEqual({ value: 5 })
110
- expect(tape[0].output).toEqual({ value: 5 })
127
+ await task.run({ value: 2 })
128
+
129
+ expect(tape).toEqual([{
130
+ input: { value: 1 },
131
+ output: { value: 1 },
132
+ boundaries: {},
133
+ taskName: undefined,
134
+ metadata: {},
135
+ type: 'success'
136
+ }])
111
137
  })
112
138
  })
@@ -1,4 +1,4 @@
1
- import { createTask, Schema, TaskRecord, BoundaryTapeData } from '../index'
1
+ import { createTask, Schema, ExecutionRecord, BoundaryTapeData } from '../index'
2
2
 
3
3
  // Need to add proxy cache mode to the boundaries
4
4
  describe('Boundaries tasks tests', () => {
@@ -148,7 +148,7 @@ describe('Boundaries tasks tests', () => {
148
148
  }
149
149
 
150
150
  // Use the correct type definition for records
151
- const records: TaskRecord<{value: number}, Promise<number>>[] = []
151
+ const records: ExecutionRecord[] = []
152
152
 
153
153
  // Create a schema for the task that accepts a number
154
154
  const schema = new Schema({
@@ -192,7 +192,7 @@ describe('Boundaries tasks tests', () => {
192
192
  expect(records.length).toBe(3)
193
193
 
194
194
  // Sort records by input value for consistent testing
195
- const sortedRecords = [...records].sort((a, b) => a.input.value - b.input.value)
195
+ const sortedRecords = [...records].sort((a, b) => (a.input as {value: number}).value - (b.input as {value: number}).value)
196
196
 
197
197
  // Check record for first task (value: 2)
198
198
  expect(sortedRecords[0].input).toEqual({ value: 2 })