@forgehive/record-tape 0.1.6 → 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.
- package/dist/index.d.ts +2 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +28 -78
- package/dist/index.js.map +1 -1
- package/dist/tests/index.test.js +3 -3
- package/dist/tests/index.test.js.map +1 -1
- package/dist/tests/log-format.test.js +18 -27
- package/dist/tests/log-format.test.js.map +1 -1
- package/dist/tests/mode.test.js +66 -54
- package/dist/tests/mode.test.js.map +1 -1
- package/dist/tests/safe-run.test.js +50 -21
- package/dist/tests/safe-run.test.js.map +1 -1
- package/dist/tests/save.test.js +13 -13
- package/dist/tests/save.test.js.map +1 -1
- package/dist/tests/task-listener.test.js +43 -29
- package/dist/tests/task-listener.test.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +43 -105
- package/src/tests/index.test.ts +3 -3
- package/src/tests/log-format.test.ts +20 -32
- package/src/tests/mode.test.ts +75 -62
- package/src/tests/safe-run.test.ts +35 -22
- package/src/tests/save.test.ts +19 -19
- package/src/tests/task-listener.test.ts +36 -30
package/src/tests/save.test.ts
CHANGED
|
@@ -2,11 +2,11 @@ import fs from 'fs'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
import { RecordTape } from '../index'
|
|
4
4
|
|
|
5
|
-
const logFileData = '{"name":"name","
|
|
5
|
+
const logFileData = '{"name":"name","input":true,"output":true,"boundaries":{},"type":"success"}\n{"name":"name","input":true,"error":"invalid data","boundaries":{},"type":"error"}\n'
|
|
6
6
|
|
|
7
7
|
describe('Save to file async', () => {
|
|
8
8
|
it('Save async to existing file(should add new logs)', async () => {
|
|
9
|
-
type InputType = boolean
|
|
9
|
+
type InputType = boolean
|
|
10
10
|
type OutputType = boolean
|
|
11
11
|
|
|
12
12
|
const tapeFilePath = path.resolve(__dirname, './fixtures/save')
|
|
@@ -19,8 +19,8 @@ describe('Save to file async', () => {
|
|
|
19
19
|
|
|
20
20
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
21
21
|
await tape.load()
|
|
22
|
-
tape.
|
|
23
|
-
tape.
|
|
22
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
23
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
24
24
|
await tape.save()
|
|
25
25
|
|
|
26
26
|
const content = await fs.promises.readFile(tapeFilePath + '.log', 'utf8')
|
|
@@ -31,20 +31,20 @@ describe('Save to file async', () => {
|
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
it('Save async to a path of invalid folder', async () => {
|
|
34
|
-
type InputType = boolean
|
|
34
|
+
type InputType = boolean
|
|
35
35
|
type OutputType = boolean
|
|
36
36
|
|
|
37
37
|
const tapeFilePath = path.resolve(__dirname, './nowhere/nop')
|
|
38
38
|
|
|
39
39
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
40
|
-
tape.
|
|
41
|
-
tape.
|
|
40
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
41
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
42
42
|
|
|
43
43
|
await expect(tape.save()).rejects.toThrow('Folder doesn\'t exists')
|
|
44
44
|
})
|
|
45
45
|
|
|
46
46
|
it('Save async to a log file that doesnt exist', async () => {
|
|
47
|
-
type InputType = boolean
|
|
47
|
+
type InputType = boolean
|
|
48
48
|
type OutputType = boolean
|
|
49
49
|
|
|
50
50
|
const tapeFilePath = path.resolve(__dirname, './fixtures/nop')
|
|
@@ -56,8 +56,8 @@ describe('Save to file async', () => {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
59
|
-
tape.
|
|
60
|
-
tape.
|
|
59
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
60
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
61
61
|
await tape.save()
|
|
62
62
|
|
|
63
63
|
const content = await fs.promises.readFile(tapeFilePath + '.log', 'utf8')
|
|
@@ -68,7 +68,7 @@ describe('Save to file async', () => {
|
|
|
68
68
|
|
|
69
69
|
describe('Save to file sync', () => {
|
|
70
70
|
it('Save sync', () => {
|
|
71
|
-
type InputType = boolean
|
|
71
|
+
type InputType = boolean
|
|
72
72
|
type OutputType = boolean
|
|
73
73
|
|
|
74
74
|
const tapeFilePath = path.resolve(__dirname, './fixtures/save')
|
|
@@ -80,8 +80,8 @@ describe('Save to file sync', () => {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
83
|
-
tape.
|
|
84
|
-
tape.
|
|
83
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
84
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
85
85
|
tape.saveSync()
|
|
86
86
|
|
|
87
87
|
const content = fs.readFileSync(tapeFilePath + '.log', 'utf8')
|
|
@@ -90,20 +90,20 @@ describe('Save to file sync', () => {
|
|
|
90
90
|
})
|
|
91
91
|
|
|
92
92
|
it('Save sync to a path of invalid folder', () => {
|
|
93
|
-
type InputType = boolean
|
|
93
|
+
type InputType = boolean
|
|
94
94
|
type OutputType = boolean
|
|
95
95
|
|
|
96
96
|
const tapeFilePath = path.resolve(__dirname, './nowhere/nop')
|
|
97
97
|
|
|
98
98
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
99
|
-
tape.
|
|
100
|
-
tape.
|
|
99
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
100
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
101
101
|
|
|
102
102
|
expect(() => tape.saveSync()).toThrow('Folder doesn\'t exists')
|
|
103
103
|
})
|
|
104
104
|
|
|
105
105
|
it('Save sync to a log file that doesnt exist', () => {
|
|
106
|
-
type InputType = boolean
|
|
106
|
+
type InputType = boolean
|
|
107
107
|
type OutputType = boolean
|
|
108
108
|
|
|
109
109
|
const tapeFilePath = path.resolve(__dirname, './fixtures/nop')
|
|
@@ -116,8 +116,8 @@ describe('Save to file sync', () => {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
const tape = new RecordTape<InputType, OutputType>({ path: tapeFilePath })
|
|
119
|
-
tape.
|
|
120
|
-
tape.
|
|
119
|
+
tape.addLogRecord({ name: 'name', input: true, output: true, boundaries: {}, type: 'success' })
|
|
120
|
+
tape.addLogRecord({ name: 'name', input: true, error: 'invalid data', boundaries: {}, type: 'error' })
|
|
121
121
|
tape.saveSync()
|
|
122
122
|
|
|
123
123
|
const content = fs.readFileSync(tapeFilePath + '.log', 'utf8')
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Task, createTask, Schema } from '@forgehive/task'
|
|
2
|
-
import { RecordTape
|
|
2
|
+
import { RecordTape } from '../index'
|
|
3
3
|
|
|
4
4
|
describe('Task listener', () => {
|
|
5
5
|
it('Should listen to task events', async () => {
|
|
@@ -14,17 +14,18 @@ describe('Task listener', () => {
|
|
|
14
14
|
)
|
|
15
15
|
|
|
16
16
|
task.addListener<InputType, OutputType>((record) => {
|
|
17
|
-
const
|
|
18
|
-
? { input: record.input, error: record.error, boundaries: record.boundaries }
|
|
19
|
-
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries }
|
|
17
|
+
const executionRecord = record.error
|
|
18
|
+
? { input: record.input, error: record.error, boundaries: record.boundaries || {}, type: 'error' as const }
|
|
19
|
+
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries || {}, type: 'success' as const }
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
tape.push('test', executionRecord as any)
|
|
22
23
|
})
|
|
23
24
|
|
|
24
25
|
await task.run({})
|
|
25
26
|
|
|
26
27
|
expect(tape.getLog()).toEqual([
|
|
27
|
-
{ name: 'test', type: 'success', input: {}, output: { value: 1, foo: true }, boundaries: {} }
|
|
28
|
+
{ name: 'test', type: 'success', input: {}, output: { value: 1, foo: true }, boundaries: {}, metadata: {} }
|
|
28
29
|
])
|
|
29
30
|
})
|
|
30
31
|
|
|
@@ -42,11 +43,12 @@ describe('Task listener', () => {
|
|
|
42
43
|
const tape = new RecordTape<{ value: number }, { result: number }>({})
|
|
43
44
|
|
|
44
45
|
task.addListener<{ value: number }, { result: number }>((record) => {
|
|
45
|
-
const
|
|
46
|
-
? { input: record.input, error: record.error, boundaries: record.boundaries }
|
|
47
|
-
: { input: record.input, output: record.output as { result: number }, boundaries: record.boundaries }
|
|
46
|
+
const executionRecord = record.error
|
|
47
|
+
? { input: record.input, error: record.error, boundaries: record.boundaries || {}, type: 'error' as const }
|
|
48
|
+
: { input: record.input, output: record.output as { result: number }, boundaries: record.boundaries || {}, type: 'success' as const }
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
tape.push('test', executionRecord as any)
|
|
50
52
|
})
|
|
51
53
|
|
|
52
54
|
try {
|
|
@@ -60,8 +62,8 @@ describe('Task listener', () => {
|
|
|
60
62
|
const log = tape.getLog()
|
|
61
63
|
|
|
62
64
|
expect(log).toEqual([
|
|
63
|
-
{ name: 'test', type: 'error', input: { value: 5 }, error: 'Value is not between 10 and 20', boundaries: {} },
|
|
64
|
-
{ name: 'test', type: 'success', input: { value: 15 }, output: { result: 30 }, boundaries: {} }
|
|
65
|
+
{ name: 'test', type: 'error', input: { value: 5 }, error: 'Value is not between 10 and 20', boundaries: {}, metadata: {}, output: undefined },
|
|
66
|
+
{ name: 'test', type: 'success', input: { value: 15 }, output: { result: 30 }, boundaries: {}, metadata: {} }
|
|
65
67
|
])
|
|
66
68
|
})
|
|
67
69
|
|
|
@@ -79,14 +81,14 @@ describe('Task listener', () => {
|
|
|
79
81
|
}
|
|
80
82
|
|
|
81
83
|
// Create the task using createTask
|
|
82
|
-
const task = createTask(
|
|
84
|
+
const task = createTask({
|
|
83
85
|
schema,
|
|
84
86
|
boundaries,
|
|
85
|
-
async (input, { multiply }) => {
|
|
87
|
+
fn: async (input, { multiply }) => {
|
|
86
88
|
const result = await multiply(input.value)
|
|
87
89
|
return { result }
|
|
88
90
|
}
|
|
89
|
-
)
|
|
91
|
+
})
|
|
90
92
|
|
|
91
93
|
type InputType = typeof schema
|
|
92
94
|
type OutputType = Awaited<ReturnType<typeof task.run>>
|
|
@@ -94,11 +96,12 @@ describe('Task listener', () => {
|
|
|
94
96
|
const tape = new RecordTape<InputType, OutputType>({})
|
|
95
97
|
|
|
96
98
|
task.addListener<InputType, OutputType>((record) => {
|
|
97
|
-
const
|
|
98
|
-
? { input: record.input, error: record.error, boundaries: record.boundaries }
|
|
99
|
-
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries }
|
|
99
|
+
const executionRecord = record.error
|
|
100
|
+
? { input: record.input, error: record.error, boundaries: record.boundaries || {}, type: 'error' as const }
|
|
101
|
+
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries || {}, type: 'success' as const }
|
|
100
102
|
|
|
101
|
-
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
104
|
+
tape.push('test', executionRecord as any)
|
|
102
105
|
})
|
|
103
106
|
|
|
104
107
|
await task.run({ value: 5 })
|
|
@@ -110,8 +113,9 @@ describe('Task listener', () => {
|
|
|
110
113
|
input: { value: 5 },
|
|
111
114
|
output: { result: 10 },
|
|
112
115
|
boundaries: {
|
|
113
|
-
multiply: [{ input: [5], output: 10 }]
|
|
114
|
-
}
|
|
116
|
+
multiply: [{ input: [5], output: 10, error: null }]
|
|
117
|
+
},
|
|
118
|
+
metadata: {}
|
|
115
119
|
}
|
|
116
120
|
])
|
|
117
121
|
})
|
|
@@ -135,21 +139,22 @@ describe('Task listener', () => {
|
|
|
135
139
|
}
|
|
136
140
|
|
|
137
141
|
// Create the task using createTask
|
|
138
|
-
const task = createTask(
|
|
142
|
+
const task = createTask({
|
|
139
143
|
schema,
|
|
140
144
|
boundaries,
|
|
141
|
-
async (input, { multiply }) => {
|
|
145
|
+
fn: async (input, { multiply }) => {
|
|
142
146
|
const result = await multiply(input.value)
|
|
143
147
|
return { result }
|
|
144
148
|
}
|
|
145
|
-
)
|
|
149
|
+
})
|
|
146
150
|
|
|
147
151
|
task.addListener<InputType, OutputType>((record) => {
|
|
148
|
-
const
|
|
149
|
-
? { input: record.input, error: record.error, boundaries: record.boundaries }
|
|
150
|
-
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries }
|
|
152
|
+
const executionRecord = record.error
|
|
153
|
+
? { input: record.input, error: record.error, boundaries: record.boundaries || {}, type: 'error' as const }
|
|
154
|
+
: { input: record.input, output: record.output as OutputType, boundaries: record.boundaries || {}, type: 'success' as const }
|
|
151
155
|
|
|
152
|
-
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
157
|
+
tape.push('test', executionRecord as any)
|
|
153
158
|
})
|
|
154
159
|
|
|
155
160
|
await task.run({ value: 5 })
|
|
@@ -161,8 +166,9 @@ describe('Task listener', () => {
|
|
|
161
166
|
input: { value: 5 },
|
|
162
167
|
output: { result: 10 },
|
|
163
168
|
boundaries: {
|
|
164
|
-
multiply: [{ input: [5], output: 10 }]
|
|
165
|
-
}
|
|
169
|
+
multiply: [{ input: [5], output: 10, error: null }]
|
|
170
|
+
},
|
|
171
|
+
metadata: {}
|
|
166
172
|
}
|
|
167
173
|
])
|
|
168
174
|
})
|