@forgehive/hive-sdk 0.1.5 → 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/README.md +318 -528
- package/dist/index.d.ts +0 -2
- package/dist/index.js +10 -66
- package/dist/test/metadata.test.js +123 -171
- package/dist/test/sendLog.test.js +199 -117
- package/package.json +3 -3
- package/src/index.ts +11 -78
- package/src/test/metadata.test.ts +143 -218
- package/src/test/sendLog.test.ts +237 -145
- package/src/test/sendLogByUuid.test.ts +0 -272
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import axios from 'axios'
|
|
2
|
-
import { HiveLogClient } from '../index'
|
|
3
|
-
|
|
4
|
-
// Mock axios
|
|
5
|
-
jest.mock('axios')
|
|
6
|
-
const mockedAxios = axios as jest.Mocked<typeof axios>
|
|
7
|
-
|
|
8
|
-
describe('HiveLogClient sendLogByUuid', () => {
|
|
9
|
-
const testConfig = {
|
|
10
|
-
projectName: 'test-project',
|
|
11
|
-
projectUuid: '550e8400-e29b-41d4-a716-446655440000',
|
|
12
|
-
apiKey: 'test-key',
|
|
13
|
-
apiSecret: 'test-secret',
|
|
14
|
-
host: 'https://test.example.com'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
jest.clearAllMocks()
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
describe('UUID Generation', () => {
|
|
22
|
-
it('should generate UUID v7 when execution record has no UUID', async () => {
|
|
23
|
-
mockedAxios.post.mockResolvedValueOnce({ data: { success: true } })
|
|
24
|
-
|
|
25
|
-
const client = new HiveLogClient(testConfig)
|
|
26
|
-
const executionRecord = {
|
|
27
|
-
input: { value: 'test-input' },
|
|
28
|
-
output: { result: 'test-output' },
|
|
29
|
-
taskName: 'test-task',
|
|
30
|
-
type: 'success' as const,
|
|
31
|
-
boundaries: {},
|
|
32
|
-
metadata: {}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
36
|
-
|
|
37
|
-
expect(result).toBe('success')
|
|
38
|
-
expect(mockedAxios.post).toHaveBeenCalledTimes(1)
|
|
39
|
-
|
|
40
|
-
// Check that the request was made with a UUID
|
|
41
|
-
const callArgs = mockedAxios.post.mock.calls[0]
|
|
42
|
-
const requestBody = callArgs[1] as { projectUuid: string; taskUuid: string; logItem: string }
|
|
43
|
-
const logItem = JSON.parse(requestBody.logItem)
|
|
44
|
-
|
|
45
|
-
expect(logItem.uuid).toBeDefined()
|
|
46
|
-
expect(typeof logItem.uuid).toBe('string')
|
|
47
|
-
expect(logItem.uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) // UUID v7 pattern
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('should preserve existing UUID when execution record already has one', async () => {
|
|
51
|
-
mockedAxios.post.mockResolvedValueOnce({ data: { success: true } })
|
|
52
|
-
|
|
53
|
-
const client = new HiveLogClient(testConfig)
|
|
54
|
-
const existingUuid = '01234567-89ab-7def-8123-456789abcdef'
|
|
55
|
-
const executionRecord = {
|
|
56
|
-
uuid: existingUuid,
|
|
57
|
-
input: { value: 'test-input' },
|
|
58
|
-
output: { result: 'test-output' },
|
|
59
|
-
taskName: 'test-task',
|
|
60
|
-
type: 'success' as const,
|
|
61
|
-
boundaries: {},
|
|
62
|
-
metadata: {}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
66
|
-
|
|
67
|
-
expect(result).toBe('success')
|
|
68
|
-
expect(mockedAxios.post).toHaveBeenCalledTimes(1)
|
|
69
|
-
|
|
70
|
-
// Check that the existing UUID was preserved
|
|
71
|
-
const callArgs = mockedAxios.post.mock.calls[0]
|
|
72
|
-
const requestBody = callArgs[1] as { projectUuid: string; taskUuid: string; logItem: string }
|
|
73
|
-
const logItem = JSON.parse(requestBody.logItem)
|
|
74
|
-
|
|
75
|
-
expect(logItem.uuid).toBe(existingUuid)
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('should generate UUID v7 when execution record has empty UUID', async () => {
|
|
79
|
-
mockedAxios.post.mockResolvedValueOnce({ data: { success: true } })
|
|
80
|
-
|
|
81
|
-
const client = new HiveLogClient(testConfig)
|
|
82
|
-
const executionRecord = {
|
|
83
|
-
uuid: '', // Empty string should trigger UUID generation
|
|
84
|
-
input: { value: 'test-input' },
|
|
85
|
-
output: { result: 'test-output' },
|
|
86
|
-
taskName: 'test-task',
|
|
87
|
-
type: 'success' as const,
|
|
88
|
-
boundaries: {},
|
|
89
|
-
metadata: {}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
93
|
-
|
|
94
|
-
expect(result).toBe('success')
|
|
95
|
-
expect(mockedAxios.post).toHaveBeenCalledTimes(1)
|
|
96
|
-
|
|
97
|
-
// Check that a new UUID was generated (not empty string)
|
|
98
|
-
const callArgs = mockedAxios.post.mock.calls[0]
|
|
99
|
-
const requestBody = callArgs[1] as { projectUuid: string; taskUuid: string; logItem: string }
|
|
100
|
-
const logItem = JSON.parse(requestBody.logItem)
|
|
101
|
-
|
|
102
|
-
expect(logItem.uuid).toBeDefined()
|
|
103
|
-
expect(logItem.uuid).not.toBe('')
|
|
104
|
-
expect(typeof logItem.uuid).toBe('string')
|
|
105
|
-
expect(logItem.uuid).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i) // UUID v7 pattern
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
describe('API Integration', () => {
|
|
110
|
-
it('should send correct request to log-ingest endpoint', async () => {
|
|
111
|
-
mockedAxios.post.mockResolvedValueOnce({ data: { success: true } })
|
|
112
|
-
|
|
113
|
-
const client = new HiveLogClient(testConfig)
|
|
114
|
-
const taskUuid = 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
|
|
115
|
-
const executionRecord = {
|
|
116
|
-
input: { userId: 123 },
|
|
117
|
-
output: { result: 'processed' },
|
|
118
|
-
taskName: 'process-user',
|
|
119
|
-
type: 'success' as const,
|
|
120
|
-
boundaries: {},
|
|
121
|
-
metadata: { session: 'abc123' }
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
await client.sendLogByUuid(executionRecord, taskUuid)
|
|
125
|
-
|
|
126
|
-
expect(mockedAxios.post).toHaveBeenCalledWith(
|
|
127
|
-
'https://test.example.com/api/log-ingest',
|
|
128
|
-
{
|
|
129
|
-
projectUuid: testConfig.projectUuid,
|
|
130
|
-
taskUuid: taskUuid,
|
|
131
|
-
logItem: expect.any(String)
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
headers: {
|
|
135
|
-
'Authorization': 'Bearer test-key:test-secret',
|
|
136
|
-
'Content-Type': 'application/json'
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
// Verify the logItem contains the execution record with UUID
|
|
142
|
-
const callArgs = mockedAxios.post.mock.calls[0]
|
|
143
|
-
const requestBody = callArgs[1] as { projectUuid: string; taskUuid: string; logItem: string }
|
|
144
|
-
const logItem = JSON.parse(requestBody.logItem)
|
|
145
|
-
|
|
146
|
-
expect(logItem.uuid).toBeDefined()
|
|
147
|
-
expect(logItem.input).toEqual({ userId: 123 })
|
|
148
|
-
expect(logItem.output).toEqual({ result: 'processed' })
|
|
149
|
-
expect(logItem.taskName).toBe('process-user')
|
|
150
|
-
expect(logItem.type).toBe('success')
|
|
151
|
-
expect(logItem.metadata).toEqual({ session: 'abc123' })
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it('should return "silent" when client is not initialized', async () => {
|
|
155
|
-
const client = new HiveLogClient({
|
|
156
|
-
projectName: 'test-project',
|
|
157
|
-
projectUuid: testConfig.projectUuid
|
|
158
|
-
// Missing apiKey and apiSecret
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
const executionRecord = {
|
|
162
|
-
input: { value: 'test' },
|
|
163
|
-
taskName: 'test-task',
|
|
164
|
-
type: 'success' as const,
|
|
165
|
-
boundaries: {},
|
|
166
|
-
metadata: {}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
170
|
-
|
|
171
|
-
expect(result).toBe('silent')
|
|
172
|
-
expect(mockedAxios.post).not.toHaveBeenCalled()
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
it('should return "error" when projectUuid is missing', async () => {
|
|
176
|
-
const client = new HiveLogClient({
|
|
177
|
-
projectName: 'test-project',
|
|
178
|
-
apiKey: 'test-key',
|
|
179
|
-
apiSecret: 'test-secret',
|
|
180
|
-
host: 'https://test.example.com'
|
|
181
|
-
// Missing projectUuid
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
const executionRecord = {
|
|
185
|
-
input: { value: 'test' },
|
|
186
|
-
taskName: 'test-task',
|
|
187
|
-
type: 'success' as const,
|
|
188
|
-
boundaries: {},
|
|
189
|
-
metadata: {}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
193
|
-
|
|
194
|
-
expect(result).toBe('error')
|
|
195
|
-
expect(mockedAxios.post).not.toHaveBeenCalled()
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
it('should handle API error responses', async () => {
|
|
199
|
-
mockedAxios.post.mockRejectedValueOnce(new Error('Network error'))
|
|
200
|
-
|
|
201
|
-
const client = new HiveLogClient(testConfig)
|
|
202
|
-
const executionRecord = {
|
|
203
|
-
input: { value: 'test' },
|
|
204
|
-
taskName: 'test-task',
|
|
205
|
-
type: 'success' as const,
|
|
206
|
-
boundaries: {},
|
|
207
|
-
metadata: {}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
211
|
-
|
|
212
|
-
expect(result).toBe('error')
|
|
213
|
-
expect(mockedAxios.post).toHaveBeenCalledTimes(1)
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
it('should return response data when API returns object with uuid', async () => {
|
|
217
|
-
const responseData = { uuid: 'log-uuid-123', success: true }
|
|
218
|
-
mockedAxios.post.mockResolvedValueOnce({ data: responseData })
|
|
219
|
-
|
|
220
|
-
const client = new HiveLogClient(testConfig)
|
|
221
|
-
const executionRecord = {
|
|
222
|
-
input: { value: 'test' },
|
|
223
|
-
taskName: 'test-task',
|
|
224
|
-
type: 'success' as const,
|
|
225
|
-
boundaries: {},
|
|
226
|
-
metadata: {}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const result = await client.sendLogByUuid(executionRecord, 'task-uuid-123')
|
|
230
|
-
|
|
231
|
-
expect(result).toEqual(responseData)
|
|
232
|
-
})
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
describe('Metadata Handling', () => {
|
|
236
|
-
it('should merge metadata correctly with UUID generation', async () => {
|
|
237
|
-
mockedAxios.post.mockResolvedValueOnce({ data: { success: true } })
|
|
238
|
-
|
|
239
|
-
const baseMetadata = { environment: 'test', version: '1.0' }
|
|
240
|
-
const client = new HiveLogClient({ ...testConfig, metadata: baseMetadata })
|
|
241
|
-
|
|
242
|
-
const recordMetadata = { session: 'abc123' }
|
|
243
|
-
const sendLogMetadata = { priority: 'high' }
|
|
244
|
-
|
|
245
|
-
const executionRecord = {
|
|
246
|
-
input: { value: 'test' },
|
|
247
|
-
taskName: 'test-task',
|
|
248
|
-
type: 'success' as const,
|
|
249
|
-
boundaries: {},
|
|
250
|
-
metadata: recordMetadata
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
await client.sendLogByUuid(executionRecord, 'task-uuid-123', sendLogMetadata)
|
|
254
|
-
|
|
255
|
-
const callArgs = mockedAxios.post.mock.calls[0]
|
|
256
|
-
const requestBody = callArgs[1] as { projectUuid: string; taskUuid: string; logItem: string }
|
|
257
|
-
const logItem = JSON.parse(requestBody.logItem)
|
|
258
|
-
|
|
259
|
-
// Should have UUID generated
|
|
260
|
-
expect(logItem.uuid).toBeDefined()
|
|
261
|
-
expect(typeof logItem.uuid).toBe('string')
|
|
262
|
-
|
|
263
|
-
// Should have merged metadata (sendLog > record > client)
|
|
264
|
-
expect(logItem.metadata).toEqual({
|
|
265
|
-
environment: 'test', // from client
|
|
266
|
-
version: '1.0', // from client
|
|
267
|
-
session: 'abc123', // from record
|
|
268
|
-
priority: 'high' // from sendLog (highest priority)
|
|
269
|
-
})
|
|
270
|
-
})
|
|
271
|
-
})
|
|
272
|
-
})
|