@forgehive/task 0.1.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.
- package/LICENSE +21 -0
- package/README.md +241 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +191 -0
- package/dist/index.js.map +1 -0
- package/dist/test/add-listener-with-boundaries.test.d.ts +2 -0
- package/dist/test/add-listener-with-boundaries.test.d.ts.map +1 -0
- package/dist/test/add-listener-with-boundaries.test.js +230 -0
- package/dist/test/add-listener-with-boundaries.test.js.map +1 -0
- package/dist/test/add-listener.test.d.ts +2 -0
- package/dist/test/add-listener.test.d.ts.map +1 -0
- package/dist/test/add-listener.test.js +92 -0
- package/dist/test/add-listener.test.js.map +1 -0
- package/dist/test/boundary-modes.test.d.ts +2 -0
- package/dist/test/boundary-modes.test.d.ts.map +1 -0
- package/dist/test/boundary-modes.test.js +184 -0
- package/dist/test/boundary-modes.test.js.map +1 -0
- package/dist/test/change-mode.test.d.ts +2 -0
- package/dist/test/change-mode.test.d.ts.map +1 -0
- package/dist/test/change-mode.test.js +39 -0
- package/dist/test/change-mode.test.js.map +1 -0
- package/dist/test/index.test.d.ts +2 -0
- package/dist/test/index.test.d.ts.map +1 -0
- package/dist/test/index.test.js +48 -0
- package/dist/test/index.test.js.map +1 -0
- package/dist/test/run-boundary.test.d.ts +2 -0
- package/dist/test/run-boundary.test.d.ts.map +1 -0
- package/dist/test/run-boundary.test.js +49 -0
- package/dist/test/run-boundary.test.js.map +1 -0
- package/dist/test/run-without-args.test.d.ts +2 -0
- package/dist/test/run-without-args.test.d.ts.map +1 -0
- package/dist/test/run-without-args.test.js +29 -0
- package/dist/test/run-without-args.test.js.map +1 -0
- package/dist/test/task-with-boundaries.test.d.ts +2 -0
- package/dist/test/task-with-boundaries.test.d.ts.map +1 -0
- package/dist/test/task-with-boundaries.test.js +102 -0
- package/dist/test/task-with-boundaries.test.js.map +1 -0
- package/dist/test/validation.test.d.ts +2 -0
- package/dist/test/validation.test.d.ts.map +1 -0
- package/dist/test/validation.test.js +166 -0
- package/dist/test/validation.test.js.map +1 -0
- package/dist/utils/boundary.d.ts +44 -0
- package/dist/utils/boundary.d.ts.map +1 -0
- package/dist/utils/boundary.js +145 -0
- package/dist/utils/boundary.js.map +1 -0
- package/jest.config.js +9 -0
- package/package.json +30 -0
- package/src/index.ts +344 -0
- package/src/test/add-listener-with-boundaries.test.ts +299 -0
- package/src/test/add-listener.test.ts +110 -0
- package/src/test/boundary-modes.test.ts +215 -0
- package/src/test/change-mode.test.ts +45 -0
- package/src/test/index.test.ts +62 -0
- package/src/test/run-boundary.test.ts +64 -0
- package/src/test/run-without-args.test.ts +33 -0
- package/src/test/task-with-boundaries.test.ts +137 -0
- package/src/test/validation.test.ts +194 -0
- package/src/utils/boundary.ts +178 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Task, type TaskRecord } from '../index'
|
|
2
|
+
import { Schema, type InferSchema } from '@shadow/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
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
task.addListener((record) => {
|
|
12
|
+
tape.push(record)
|
|
13
|
+
})
|
|
14
|
+
|
|
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 })
|
|
20
|
+
})
|
|
21
|
+
|
|
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')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
task.addListener((record) => {
|
|
29
|
+
tape.push(record)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
await task.run({ value: 5 })
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// Error is expected
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
expect(tape.length).toBe(1)
|
|
39
|
+
expect(tape[0].input).toEqual({ value: 5 })
|
|
40
|
+
expect(tape[0].error).toBe('This should happen')
|
|
41
|
+
})
|
|
42
|
+
|
|
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
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
task.addListener<{ value: number | null }, { value: number }>((record) => {
|
|
56
|
+
tape.push(record)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await task.run({ value: 3 })
|
|
61
|
+
} catch (e) {
|
|
62
|
+
// Error is expected
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
expect(tape.length).toBe(1)
|
|
66
|
+
expect(tape[0].input).toEqual({ value: 3 })
|
|
67
|
+
expect(tape[0].error).toBe('Invalid input')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('Should record multiple records', async () => {
|
|
71
|
+
const tape: TaskRecord<{ value: number }, { value: number }>[] = []
|
|
72
|
+
const task = new Task(function (_argv: { value: number }) {
|
|
73
|
+
return _argv
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
task.addListener((record) => {
|
|
77
|
+
tape.push(record)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
await task.run({ value: 5 })
|
|
81
|
+
await task.run({ value: 6 })
|
|
82
|
+
|
|
83
|
+
expect(tape.length).toBe(2)
|
|
84
|
+
expect(tape[0].input).toEqual({ value: 5 })
|
|
85
|
+
expect(tape[0].output).toEqual({ value: 5 })
|
|
86
|
+
|
|
87
|
+
expect(tape[1].input).toEqual({ value: 6 })
|
|
88
|
+
expect(tape[1].output).toEqual({ value: 6 })
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('Should stop recording', async () => {
|
|
92
|
+
const tape: TaskRecord<{ value: number }, { value: number }>[] = []
|
|
93
|
+
const task = new Task(function (_argv: { value: number }) {
|
|
94
|
+
return _argv
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
task.addListener((record) => {
|
|
98
|
+
tape.push(record)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
await task.run({ value: 5 })
|
|
102
|
+
|
|
103
|
+
task.removeListener()
|
|
104
|
+
await task.run({ value: 6 })
|
|
105
|
+
|
|
106
|
+
expect(tape.length).toBe(1)
|
|
107
|
+
expect(tape[0].input).toEqual({ value: 5 })
|
|
108
|
+
expect(tape[0].output).toEqual({ value: 5 })
|
|
109
|
+
})
|
|
110
|
+
})
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { Task } from '../index'
|
|
2
|
+
|
|
3
|
+
type FetchIncrementFn = (argv: { value: number }) => Promise<number>
|
|
4
|
+
|
|
5
|
+
interface TaskBoundaries {
|
|
6
|
+
fetchIncrement: FetchIncrementFn
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('Proxy mode', function () {
|
|
10
|
+
// Proxy: execute the function and records it
|
|
11
|
+
it('Should execute the boundary', async function () {
|
|
12
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
13
|
+
const increment: number = await fetchIncrement(argv)
|
|
14
|
+
|
|
15
|
+
return argv.value + increment
|
|
16
|
+
}, {
|
|
17
|
+
boundaries: {
|
|
18
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
19
|
+
return argv.value
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
mode: 'proxy'
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
26
|
+
const result = await add.run({ value: 5 })
|
|
27
|
+
|
|
28
|
+
expect(result).toBe(10)
|
|
29
|
+
expect(fetchIncrement.getMode()).toBe('proxy')
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('Proxy pass mode', function () {
|
|
34
|
+
// Proxy pass: review if the input exist, it it exist returns the previous value and if not execute the functions
|
|
35
|
+
it('Should return value from record', async function () {
|
|
36
|
+
let i = 0
|
|
37
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
38
|
+
const increment: number = await fetchIncrement(argv)
|
|
39
|
+
|
|
40
|
+
return argv.value + increment
|
|
41
|
+
}, {
|
|
42
|
+
boundaries: {
|
|
43
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
44
|
+
i++
|
|
45
|
+
return argv.value
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
boundariesData: {
|
|
49
|
+
fetchIncrement: [
|
|
50
|
+
{ input: [{ value: 5 }], output: 5 }
|
|
51
|
+
]
|
|
52
|
+
},
|
|
53
|
+
mode: 'proxy-pass'
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
57
|
+
const result = await add.run({ value: 5 })
|
|
58
|
+
|
|
59
|
+
expect(fetchIncrement.getMode()).toBe('proxy-pass')
|
|
60
|
+
expect(result).toBe(10)
|
|
61
|
+
expect(i).toBe(0)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('Should run boundary', async function () {
|
|
65
|
+
let i = 0
|
|
66
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
67
|
+
const increment: number = await fetchIncrement(argv)
|
|
68
|
+
|
|
69
|
+
return argv.value + increment
|
|
70
|
+
}, {
|
|
71
|
+
boundaries: {
|
|
72
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
73
|
+
i++
|
|
74
|
+
return argv.value
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
boundariesData: {
|
|
78
|
+
fetchIncrement: [
|
|
79
|
+
{ input: [{ value: 6 }], output: 5 }
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
mode: 'proxy-pass'
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
86
|
+
const result = await add.run({ value: 5 })
|
|
87
|
+
|
|
88
|
+
expect(fetchIncrement.getMode()).toBe('proxy-pass')
|
|
89
|
+
expect(result).toBe(10)
|
|
90
|
+
expect(i).toBe(1)
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
describe('Proxy catch mode', function () {
|
|
95
|
+
// Proxy-catch: executes the function and if it throws and error, it tries to use a previews output if it exists for the input.
|
|
96
|
+
it('Should run boundary', async function () {
|
|
97
|
+
let i = 0
|
|
98
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
99
|
+
const increment: number = await fetchIncrement(argv)
|
|
100
|
+
|
|
101
|
+
return argv.value + increment
|
|
102
|
+
}, {
|
|
103
|
+
boundaries: {
|
|
104
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
105
|
+
i++
|
|
106
|
+
return argv.value
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
boundariesData: {
|
|
110
|
+
fetchIncrement: [
|
|
111
|
+
{ input: [{ value: 5 }], output: 5 }
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
mode: 'proxy-catch'
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
118
|
+
const result = await add.run({ value: 5 })
|
|
119
|
+
|
|
120
|
+
expect(fetchIncrement.getMode()).toBe('proxy-catch')
|
|
121
|
+
expect(result).toBe(10)
|
|
122
|
+
expect(i).toBe(1)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('After boundary fails, should return value from record', async function () {
|
|
126
|
+
let i = 0
|
|
127
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
128
|
+
const increment: number = await fetchIncrement(argv)
|
|
129
|
+
|
|
130
|
+
return argv.value + increment
|
|
131
|
+
}, {
|
|
132
|
+
boundaries: {
|
|
133
|
+
fetchIncrement: async (_argv: { value: number }): Promise<number> => {
|
|
134
|
+
i++
|
|
135
|
+
throw new Error('Something')
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
boundariesData: {
|
|
139
|
+
fetchIncrement: [
|
|
140
|
+
{ input: [{ value: 5 }], output: 5 }
|
|
141
|
+
]
|
|
142
|
+
},
|
|
143
|
+
mode: 'proxy-catch'
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
147
|
+
const result = await add.run({ value: 5 })
|
|
148
|
+
|
|
149
|
+
expect(fetchIncrement.getMode()).toBe('proxy-catch')
|
|
150
|
+
expect(result).toBe(10)
|
|
151
|
+
expect(i).toBe(1)
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
describe('Replay mode', function () {
|
|
156
|
+
// Replay: review if the input exist and if it doesnt throws and error.
|
|
157
|
+
it('Should return value from record', async function () {
|
|
158
|
+
let i = 0
|
|
159
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
160
|
+
const increment: number = await fetchIncrement(argv)
|
|
161
|
+
|
|
162
|
+
return argv.value + increment
|
|
163
|
+
}, {
|
|
164
|
+
boundaries: {
|
|
165
|
+
fetchIncrement: async (_argv: { value: number }): Promise<number> => {
|
|
166
|
+
i++
|
|
167
|
+
return 0 // This should never be called in replay mode with existing data
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
boundariesData: {
|
|
171
|
+
fetchIncrement: [
|
|
172
|
+
{ input: [{ value: 5 }], output: 5 }
|
|
173
|
+
]
|
|
174
|
+
},
|
|
175
|
+
mode: 'replay'
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
179
|
+
const result = await add.run({ value: 5 })
|
|
180
|
+
|
|
181
|
+
expect(fetchIncrement.getMode()).toBe('replay')
|
|
182
|
+
expect(result).toBe(10)
|
|
183
|
+
expect(i).toBe(0)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('Should fail if the input its not present', async function () {
|
|
187
|
+
let i = 0
|
|
188
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }: TaskBoundaries) => {
|
|
189
|
+
const increment: number = await fetchIncrement(argv)
|
|
190
|
+
|
|
191
|
+
return argv.value + increment
|
|
192
|
+
}, {
|
|
193
|
+
boundaries: {
|
|
194
|
+
fetchIncrement: async (_argv: { value: number }): Promise<number> => {
|
|
195
|
+
i++
|
|
196
|
+
return 0 // This should never be called in replay mode
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
mode: 'replay'
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
203
|
+
|
|
204
|
+
let err: Error | undefined
|
|
205
|
+
try {
|
|
206
|
+
await add.run({ value: 5 })
|
|
207
|
+
} catch (e) {
|
|
208
|
+
err = e as Error
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
expect(fetchIncrement.getMode()).toBe('replay')
|
|
212
|
+
expect(err?.message).toBe('No tape value for this inputs')
|
|
213
|
+
expect(i).toBe(0)
|
|
214
|
+
})
|
|
215
|
+
})
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Task } from '../index'
|
|
2
|
+
|
|
3
|
+
describe('Change modes', () => {
|
|
4
|
+
it('On task start, boundaries should start in the same mode', async () => {
|
|
5
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }) => {
|
|
6
|
+
const increment: number = await fetchIncrement(argv)
|
|
7
|
+
|
|
8
|
+
return argv.value + increment
|
|
9
|
+
}, {
|
|
10
|
+
boundaries: {
|
|
11
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
12
|
+
return argv.value
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
mode: 'proxy-catch'
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const { fetchIncrement } = add.getBoundaries()
|
|
19
|
+
|
|
20
|
+
expect(fetchIncrement.getMode()).toBe('proxy-catch')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('On task mode change, boundaries should change to the same', async () => {
|
|
24
|
+
const add = new Task(async (argv: { value: number }, { fetchIncrement }) => {
|
|
25
|
+
const increment: number = await fetchIncrement(argv)
|
|
26
|
+
|
|
27
|
+
return argv.value + increment
|
|
28
|
+
}, {
|
|
29
|
+
boundaries: {
|
|
30
|
+
fetchIncrement: async (argv: { value: number }): Promise<number> => {
|
|
31
|
+
return argv.value
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
mode: 'proxy-catch'
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const { fetchIncrement: original } = add.getBoundaries()
|
|
38
|
+
expect(original.getMode()).toBe('proxy-catch')
|
|
39
|
+
|
|
40
|
+
add.setMode('replay')
|
|
41
|
+
|
|
42
|
+
const { fetchIncrement: updated } = add.getBoundaries()
|
|
43
|
+
expect(updated.getMode()).toBe('replay')
|
|
44
|
+
})
|
|
45
|
+
})
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Task } from '../index'
|
|
2
|
+
|
|
3
|
+
describe('Task class', () => {
|
|
4
|
+
it('Identity test', async () => {
|
|
5
|
+
const identity = new Task(function (argv) {
|
|
6
|
+
return argv
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
const { bar } = await identity.run({ bar: true })
|
|
10
|
+
const { foo } = await identity.run({ foo: true })
|
|
11
|
+
|
|
12
|
+
expect(bar).toBe(true)
|
|
13
|
+
expect(foo).toBe(true)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('Identity types test', async () => {
|
|
17
|
+
interface Identity {
|
|
18
|
+
bar?: boolean
|
|
19
|
+
foo?: boolean
|
|
20
|
+
}
|
|
21
|
+
const caller = async function (argv: Identity): Promise<Identity> {
|
|
22
|
+
return argv
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const identity = new Task(caller)
|
|
26
|
+
|
|
27
|
+
const { bar } = await identity.run({ bar: true })
|
|
28
|
+
const { foo } = await identity.run({ foo: true })
|
|
29
|
+
|
|
30
|
+
expect(bar).toBe(true)
|
|
31
|
+
expect(foo).toBe(true)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('Add test', async () => {
|
|
35
|
+
const add2 = new Task(function (int: number) {
|
|
36
|
+
return int + 2
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const six = await add2.run(4)
|
|
40
|
+
const seven = await add2.run(5)
|
|
41
|
+
|
|
42
|
+
expect(six).toBe(6)
|
|
43
|
+
expect(seven).toBe(7)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('getMode proxy test', async () => {
|
|
47
|
+
const proxy = new Task(function (int: number) {
|
|
48
|
+
return int + 2
|
|
49
|
+
}, {
|
|
50
|
+
mode: 'proxy'
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const proxyPass = new Task(function (int: number) {
|
|
54
|
+
return int + 2
|
|
55
|
+
}, {
|
|
56
|
+
mode: 'proxy-pass'
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
expect(proxy.getMode()).toBe('proxy')
|
|
60
|
+
expect(proxyPass.getMode()).toBe('proxy-pass')
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createBoundary } from '../utils/boundary'
|
|
2
|
+
|
|
3
|
+
describe('Run boundary tests', function () {
|
|
4
|
+
it('Should add a run record', async function () {
|
|
5
|
+
const identity = createBoundary(async function (argv) {
|
|
6
|
+
return argv
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
identity.startRun()
|
|
10
|
+
await identity({ value: 5 })
|
|
11
|
+
|
|
12
|
+
const runTape = identity.getRunData()
|
|
13
|
+
|
|
14
|
+
expect(runTape.length).toBe(1)
|
|
15
|
+
expect(runTape[0]).toEqual({ input: [{ value: 5 }], output: { value: 5 } })
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('Should only add a record if run is active', async function () {
|
|
19
|
+
const identity = createBoundary(async function (argv) {
|
|
20
|
+
return argv
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
await identity({ value: 5 })
|
|
24
|
+
|
|
25
|
+
const runTape = identity.getRunData()
|
|
26
|
+
|
|
27
|
+
expect(runTape.length).toBe(0)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('Shouldn\'t add after run stoped', async function () {
|
|
31
|
+
const identity = createBoundary(async function (argv) {
|
|
32
|
+
return argv
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
identity.startRun()
|
|
36
|
+
await identity({ value: 5 })
|
|
37
|
+
|
|
38
|
+
identity.stopRun()
|
|
39
|
+
await identity({ value: 5 })
|
|
40
|
+
|
|
41
|
+
const runTape = identity.getRunData()
|
|
42
|
+
|
|
43
|
+
expect(runTape.length).toBe(1)
|
|
44
|
+
expect(runTape[0]).toEqual({ input: [{ value: 5 }], output: { value: 5 } })
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('Should have run elements from this run', async function () {
|
|
48
|
+
const identity = createBoundary(async function (argv) {
|
|
49
|
+
return argv
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
identity.startRun()
|
|
53
|
+
await identity({ value: 4 })
|
|
54
|
+
identity.stopRun()
|
|
55
|
+
|
|
56
|
+
identity.startRun()
|
|
57
|
+
await identity({ value: 5 })
|
|
58
|
+
|
|
59
|
+
const runTape = identity.getRunData()
|
|
60
|
+
|
|
61
|
+
expect(runTape.length).toBe(1)
|
|
62
|
+
expect(runTape[0]).toEqual({ input: [{ value: 5 }], output: { value: 5 } })
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Task } from '../index'
|
|
2
|
+
|
|
3
|
+
describe('Task run without arguments', () => {
|
|
4
|
+
it('should run a task with no arguments', async () => {
|
|
5
|
+
// Create a task that doesn't require arguments
|
|
6
|
+
const noArgsTask = new Task(function () {
|
|
7
|
+
return 'success'
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// Call run without passing any arguments
|
|
11
|
+
const result = await noArgsTask.run()
|
|
12
|
+
|
|
13
|
+
// Verify the result
|
|
14
|
+
expect(result).toBe('success')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('should run a task with optional arguments', async () => {
|
|
18
|
+
// Create a task with optional arguments
|
|
19
|
+
const optionalArgsTask = new Task(function (argv?: { value?: string }) {
|
|
20
|
+
return argv?.value || 'default'
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
// Call run without passing any arguments
|
|
24
|
+
const defaultResult = await optionalArgsTask.run()
|
|
25
|
+
|
|
26
|
+
// Call run with arguments for comparison
|
|
27
|
+
const customResult = await optionalArgsTask.run({ value: 'custom' })
|
|
28
|
+
|
|
29
|
+
// Verify the results
|
|
30
|
+
expect(defaultResult).toBe('default')
|
|
31
|
+
expect(customResult).toBe('custom')
|
|
32
|
+
})
|
|
33
|
+
})
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { createTask, Schema } from '../index'
|
|
2
|
+
|
|
3
|
+
// Need to add proxy cache mode to the boundaries
|
|
4
|
+
describe('Boundaries tasks tests', () => {
|
|
5
|
+
it('Indentity + boundaries test', async () => {
|
|
6
|
+
// Create a schema for the task
|
|
7
|
+
const schema = new Schema({})
|
|
8
|
+
|
|
9
|
+
// Define the boundaries
|
|
10
|
+
const boundaries = {
|
|
11
|
+
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
12
|
+
return { foo: false }
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Create the task using createTask
|
|
17
|
+
const indentity = createTask(
|
|
18
|
+
schema,
|
|
19
|
+
boundaries,
|
|
20
|
+
async (argv, boundaries) => {
|
|
21
|
+
const externalData = await boundaries.fetchExternalData()
|
|
22
|
+
return { ...externalData, ...argv }
|
|
23
|
+
}
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const object = await indentity.run({ bar: true })
|
|
27
|
+
const { foo } = await indentity.run({ foo: true })
|
|
28
|
+
|
|
29
|
+
expect(object).toEqual({ bar: true, foo: false })
|
|
30
|
+
expect(foo).toBe(true)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('Indentity test with tape data', async () => {
|
|
34
|
+
// Create a schema for the task
|
|
35
|
+
const schema = new Schema({})
|
|
36
|
+
|
|
37
|
+
// Define the boundaries
|
|
38
|
+
const boundaries = {
|
|
39
|
+
fetchExternalData: async (): Promise<{ foo: boolean }> => {
|
|
40
|
+
// Return an empty implementation, the actual data will come from boundariesData
|
|
41
|
+
return { foo: false }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Create the task using createTask with boundariesData
|
|
46
|
+
const indentity = createTask(
|
|
47
|
+
schema,
|
|
48
|
+
boundaries,
|
|
49
|
+
async (argv, boundaries) => {
|
|
50
|
+
const externalData = await boundaries.fetchExternalData()
|
|
51
|
+
return { ...externalData, ...argv }
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
boundariesData: {
|
|
55
|
+
fetchExternalData: [
|
|
56
|
+
{ input: [], output: { foo: false } }
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
mode: 'proxy-pass'
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
const object = await indentity.run({ bar: true })
|
|
64
|
+
const { foo } = await indentity.run({ foo: true })
|
|
65
|
+
|
|
66
|
+
expect(object).toEqual({ bar: true, foo: false })
|
|
67
|
+
expect(foo).toBe(true)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('Add task with boundaries test', async () => {
|
|
71
|
+
// Create a schema for the task that accepts a number
|
|
72
|
+
const schema = new Schema({
|
|
73
|
+
value: Schema.number()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
// Define the boundaries
|
|
77
|
+
const boundaries = {
|
|
78
|
+
fetchExternalData: async (int: number): Promise<number> => {
|
|
79
|
+
return int * 2
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create the task using createTask
|
|
84
|
+
const add = createTask(
|
|
85
|
+
schema,
|
|
86
|
+
boundaries,
|
|
87
|
+
async function (argv, boundaries) {
|
|
88
|
+
const externalData: number = await boundaries.fetchExternalData(1)
|
|
89
|
+
return argv.value + externalData
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const six = await add.run({ value: 4 })
|
|
94
|
+
const seven = await add.run({ value: 5 })
|
|
95
|
+
|
|
96
|
+
expect(six).toBe(6)
|
|
97
|
+
expect(seven).toBe(7)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('Add task + boundaries + tape test', async () => {
|
|
101
|
+
// Create a schema for the task that accepts a number
|
|
102
|
+
const schema = new Schema({
|
|
103
|
+
value: Schema.number()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// Define the boundaries
|
|
107
|
+
const boundaries = {
|
|
108
|
+
fetchExternalData: async (int: number): Promise<number> => {
|
|
109
|
+
return int * 2
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Create the task using createTask with boundariesData
|
|
114
|
+
const add = createTask(
|
|
115
|
+
schema,
|
|
116
|
+
boundaries,
|
|
117
|
+
async function (argv, boundaries) {
|
|
118
|
+
const externalData: number = await boundaries.fetchExternalData(argv.value)
|
|
119
|
+
return argv.value + externalData
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
boundariesData: {
|
|
123
|
+
fetchExternalData: [
|
|
124
|
+
{ input: [4], output: 2 }
|
|
125
|
+
]
|
|
126
|
+
},
|
|
127
|
+
mode: 'proxy-pass'
|
|
128
|
+
}
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
const six = await add.run({ value: 4 }) // From tape data
|
|
132
|
+
const fifteen = await add.run({ value: 5 })
|
|
133
|
+
|
|
134
|
+
expect(six).toBe(6)
|
|
135
|
+
expect(fifteen).toBe(15)
|
|
136
|
+
})
|
|
137
|
+
})
|