forked from bsv-blockchain/ts-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskArcSSE.test.ts
More file actions
398 lines (349 loc) · 15.4 KB
/
Copy pathTaskArcSSE.test.ts
File metadata and controls
398 lines (349 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { TaskArcadeSSE } from '../TaskArcSSE'
import { ArcSSEEvent } from '../../../services/providers/ArcSSEClient'
import { EntityProvenTx } from '../../../storage/schema/entities'
import { TaskUnFail } from '../TaskUnFail'
// ── Fake EventSource ─────────────────────────────────────────────────────────
class FakeEventSource {
static instances: FakeEventSource[] = []
private listeners: Record<string, Array<(e: any) => void>> = {}
closed = false
constructor (
public url: string,
public opts: any
) {
FakeEventSource.instances.push(this)
}
addEventListener (type: string, fn: (e: any) => void): void {
if (this.listeners[type] == null) this.listeners[type] = []
this.listeners[type].push(fn)
}
emit (type: string, event: any = {}): void {
for (const fn of this.listeners[type] ?? []) fn(event)
}
close (): void {
this.closed = true
}
}
// ── Helpers ───────────────────────────────────────────────────────────────────
/** Build a minimal TableProvenTxReq API object that EntityProvenTxReq can parse */
function makeReqApi (status: string, txid = 'txid1'): any {
const now = new Date()
return {
provenTxReqId: 1,
created_at: now,
updated_at: now,
txid,
rawTx: [1, 2, 3],
status,
history: JSON.stringify({}),
notify: JSON.stringify({ transactionIds: [1] }),
attempts: 0,
notified: false
}
}
function makeStorageWithReqs (reqApis: any[]): any {
const sp = {
updateProvenTxReqDynamics: jest.fn().mockResolvedValue(undefined),
updateTransactionsStatus: jest.fn().mockResolvedValue(undefined),
updateProvenTxReqWithNewProvenTx: jest.fn().mockResolvedValue({
status: 'completed',
history: '{}',
provenTxId: 123
})
}
return {
isStorageProvider: jest.fn().mockReturnValue(false),
findProvenTxReqs: jest.fn().mockResolvedValue(reqApis),
runAsStorageProvider: jest.fn(async (fn: any) => fn(sp)),
sp
}
}
function makeEmptyStorage (): any {
return makeStorageWithReqs([])
}
/** Build a minimal Monitor stub */
function makeMonitor (
overrides: {
callbackToken?: string | null
arcadeUrl?: string
EventSourceClass?: any
loadLastSSEEventId?: () => Promise<string | undefined>
saveLastSSEEventId?: (id: string) => Promise<void>
storageOverride?: any
servicesOverride?: any
} = {}
): any {
const storage = overrides.storageOverride ?? makeEmptyStorage()
return {
options: {
callbackToken: overrides.callbackToken === null ? undefined : (overrides.callbackToken ?? 'test-token'),
EventSourceClass: overrides.EventSourceClass ?? FakeEventSource,
loadLastSSEEventId: overrides.loadLastSSEEventId,
saveLastSSEEventId: overrides.saveLastSSEEventId
},
services: overrides.servicesOverride ?? {
options: { arcadeUrl: overrides.arcadeUrl ?? 'https://arcade.example.com' },
getMerklePath: jest.fn()
},
chain: 'test',
storage,
logEvent: jest.fn().mockResolvedValue(undefined),
callOnTransactionStatusChanged: jest.fn(),
callOnProvenTransaction: jest.fn()
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
describe('TaskArcadeSSE', () => {
beforeEach(() => {
FakeEventSource.instances = []
jest.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
jest.restoreAllMocks()
})
// ── asyncSetup ─────────────────────────────────────────────────────────
describe('asyncSetup()', () => {
test('creates and connects SSE client when fully configured', async () => {
const task = new TaskArcadeSSE(makeMonitor())
await task.asyncSetup()
expect(task.sseClient).not.toBeNull()
expect(FakeEventSource.instances.length).toBe(1)
})
test('skips setup when callbackToken is absent', async () => {
const task = new TaskArcadeSSE(makeMonitor({ callbackToken: null }))
await task.asyncSetup()
expect(task.sseClient).toBeNull()
expect(FakeEventSource.instances.length).toBe(0)
})
test('skips setup when arcadeUrl is absent', async () => {
const monitor = makeMonitor({ arcadeUrl: '' })
monitor.services.options.arcadeUrl = ''
const task = new TaskArcadeSSE(monitor)
await task.asyncSetup()
expect(task.sseClient).toBeNull()
})
test('skips setup when EventSourceClass is absent', async () => {
const monitor = makeMonitor()
monitor.options.EventSourceClass = undefined
const task = new TaskArcadeSSE(monitor)
await task.asyncSetup()
expect(task.sseClient).toBeNull()
})
test('passes loadLastSSEEventId result as lastEventId to client', async () => {
const task = new TaskArcadeSSE(makeMonitor({ loadLastSSEEventId: async () => '77' }))
await task.asyncSetup()
expect(task.sseClient?.lastEventId).toBe('77')
})
test('continues setup when loadLastSSEEventId throws', async () => {
const task = new TaskArcadeSSE(
makeMonitor({
loadLastSSEEventId: async () => {
throw new Error('db error')
}
})
)
await expect(task.asyncSetup()).resolves.not.toThrow()
expect(task.sseClient).not.toBeNull()
})
})
// ── trigger ────────────────────────────────────────────────────────────
describe('trigger()', () => {
test('returns run=false when no pending events', () => {
const task = new TaskArcadeSSE(makeMonitor())
expect(task.trigger(Date.now()).run).toBe(false)
})
test('returns run=true after an SSE event is received', async () => {
const task = new TaskArcadeSSE(makeMonitor())
await task.asyncSetup()
const payload: ArcSSEEvent = { txid: 'aaaa', txStatus: 'MINED', timestamp: '' }
FakeEventSource.instances[0].emit('status', { data: JSON.stringify(payload) })
expect(task.trigger(Date.now()).run).toBe(true)
})
})
// ── runTask ────────────────────────────────────────────────────────────
describe('runTask()', () => {
test('returns empty string when there are no pending events', async () => {
const task = new TaskArcadeSSE(makeMonitor())
expect(await task.runTask()).toBe('')
})
test('drains pending events so trigger returns false afterward', async () => {
const task = new TaskArcadeSSE(makeMonitor())
await task.asyncSetup()
const payload: ArcSSEEvent = { txid: 'bbbb', txStatus: 'SEEN_ON_NETWORK', timestamp: '' }
FakeEventSource.instances[0].emit('status', { data: JSON.stringify(payload) })
FakeEventSource.instances[0].emit('status', { data: JSON.stringify(payload) })
expect(task.trigger(Date.now()).run).toBe(true)
await task.runTask()
expect(task.trigger(Date.now()).run).toBe(false)
})
test('calls callOnTransactionStatusChanged for each processed event', async () => {
const reqApi = makeReqApi('unsent', 'cccc')
const monitor = makeMonitor({ storageOverride: makeStorageWithReqs([reqApi]) })
const task = new TaskArcadeSSE(monitor)
await task.asyncSetup()
FakeEventSource.instances[0].emit('status', {
data: JSON.stringify({ txid: 'cccc', txStatus: 'SEEN_ON_NETWORK', timestamp: '' })
})
await task.runTask()
expect(monitor.callOnTransactionStatusChanged).toHaveBeenCalledWith('cccc', 'SEEN_ON_NETWORK')
})
test('logs "No matching ProvenTxReq" when storage returns empty', async () => {
const task = new TaskArcadeSSE(makeMonitor())
await task.asyncSetup()
FakeEventSource.instances[0].emit('status', {
data: JSON.stringify({ txid: 'dddd', txStatus: 'MINED', timestamp: '' })
})
const log = await task.runTask()
expect(log).toContain('No matching ProvenTxReq')
})
})
// ── SSE status → ProvenTxReq transitions ──────────────────────────────
describe('SSE status → ProvenTxReq transitions', () => {
async function runWithStatus (txStatus: string, reqStatus: string): Promise<{ log: string, monitor: any }> {
FakeEventSource.instances = []
const reqApi = makeReqApi(reqStatus)
const storage = makeStorageWithReqs([reqApi])
const monitor = makeMonitor({ storageOverride: storage })
const task = new TaskArcadeSSE(monitor)
await task.asyncSetup()
FakeEventSource.instances[0].emit('status', {
data: JSON.stringify({ txid: reqApi.txid, txStatus, timestamp: '' })
})
const log = await task.runTask()
return { log, monitor }
}
test('SEEN_ON_NETWORK advances unsent req to unmined', async () => {
const { log } = await runWithStatus('SEEN_ON_NETWORK', 'unsent')
expect(log).toContain('=> unmined')
})
test('ACCEPTED_BY_NETWORK advances sending req to unmined', async () => {
const { log } = await runWithStatus('ACCEPTED_BY_NETWORK', 'sending')
expect(log).toContain('=> unmined')
})
test('SENT_TO_NETWORK advances callback req to unmined', async () => {
const { log } = await runWithStatus('SENT_TO_NETWORK', 'callback')
expect(log).toContain('=> unmined')
})
test('SEEN_MULTIPLE_NODES is accepted without an unhandled warning', async () => {
const { log } = await runWithStatus('SEEN_MULTIPLE_NODES', 'unmined')
expect(log).not.toContain('unhandled status')
})
test('DOUBLE_SPEND_ATTEMPTED sets req to doubleSpend', async () => {
const { log } = await runWithStatus('DOUBLE_SPEND_ATTEMPTED', 'unmined')
expect(log).toContain('=> doubleSpend')
})
test('REJECTED records the event without releasing wallet inputs', async () => {
const { log, monitor } = await runWithStatus('REJECTED', 'unmined')
expect(log).toContain('rejection recorded; awaiting resolution')
expect(monitor.storage.sp.updateProvenTxReqDynamics).toHaveBeenCalledWith(
1,
expect.objectContaining({ status: 'unmined' }),
undefined
)
expect(monitor.storage.sp.updateTransactionsStatus).not.toHaveBeenCalled()
})
test('SEEN_MULTIPLE_NODES recovers a req previously marked invalid', async () => {
const unfailReq = jest.spyOn(TaskUnFail.prototype, 'unfailReq').mockResolvedValue('inputs re-reserved\n')
const { log, monitor } = await runWithStatus('SEEN_MULTIPLE_NODES', 'invalid')
expect(log).toContain('=> unmined')
expect(unfailReq).toHaveBeenCalledWith(expect.anything(), 4)
expect(monitor.storage.sp.updateProvenTxReqDynamics).toHaveBeenCalledWith(
1,
expect.objectContaining({ status: 'unmined', wasBroadcast: true }),
undefined
)
})
test('MINED recovers an invalid req before fetching its proof', async () => {
const unfailReq = jest.spyOn(TaskUnFail.prototype, 'unfailReq').mockResolvedValue('inputs re-reserved\n')
const { log, monitor } = await runWithStatus('MINED', 'invalid')
expect(log).not.toContain('already terminal')
expect(unfailReq).toHaveBeenCalledWith(expect.anything(), 4)
expect(monitor.storage.sp.updateProvenTxReqDynamics).toHaveBeenCalledWith(
1,
expect.objectContaining({ status: 'unmined', wasBroadcast: true }),
undefined
)
})
test('unknown status produces unhandled log entry', async () => {
const { log } = await runWithStatus('SOMETHING_NEW', 'unmined')
expect(log).toContain('unhandled status: SOMETHING_NEW')
})
test('does not process already-terminal reqs', async () => {
// A positive network event may recover invalid; the other terminal
// statuses remain immutable.
const terminalStatuses = ['completed', 'doubleSpend']
for (const s of terminalStatuses) {
const { log } = await runWithStatus('MINED', s)
expect(log).toContain(`already terminal: ${s}`)
}
})
test('MINED uses configured proof services and stores validated proof', async () => {
const reqApi = makeReqApi('unmined')
const storage = makeStorageWithReqs([reqApi])
const proof = { name: 'Arcade' } as any
const getMerklePath = jest.fn().mockResolvedValue(proof)
const fromReq = jest.spyOn(EntityProvenTx, 'fromReq').mockResolvedValue({
toApi: () => ({
index: 7,
height: 99,
blockHash: 'blockhash',
merklePath: [1, 2, 3],
merkleRoot: 'merkleroot'
})
} as any)
const monitor = makeMonitor({
storageOverride: storage,
servicesOverride: {
options: { arcadeUrl: 'https://arcade.example.com' },
getMerklePath
}
})
const task = new TaskArcadeSSE(monitor)
await task.asyncSetup()
FakeEventSource.instances[0].emit('status', {
data: JSON.stringify({ txid: reqApi.txid, txStatus: 'MINED', timestamp: '' })
})
const log = await task.runTask()
expect(getMerklePath).toHaveBeenCalledWith(reqApi.txid)
expect(fromReq).toHaveBeenCalledWith(expect.anything(), proof, false, expect.any(Number))
expect(storage.runAsStorageProvider).toHaveBeenCalled()
expect(storage.sp.updateProvenTxReqDynamics).toHaveBeenLastCalledWith(
reqApi.provenTxReqId,
expect.objectContaining({ notified: true }),
undefined
)
expect(monitor.callOnProvenTransaction).toHaveBeenCalledWith(expect.objectContaining({
txid: reqApi.txid,
txIndex: 7,
blockHeight: 99
}))
expect(log).toContain('proved by Arcade')
})
})
// ── fetchNow ───────────────────────────────────────────────────────────
describe('fetchNow()', () => {
test('returns 0 when sseClient is null', async () => {
const task = new TaskArcadeSSE(makeMonitor({ callbackToken: null }))
await task.asyncSetup()
expect(await task.fetchNow()).toBe(0)
})
test('returns 0 when sseClient is present', async () => {
const task = new TaskArcadeSSE(makeMonitor())
await task.asyncSetup()
expect(await task.fetchNow()).toBe(0)
})
})
// ── saveLastSSEEventId persistence ────────────────────────────────────
describe('saveLastSSEEventId', () => {
test('is called when lastEventId changes on an incoming event', async () => {
const saveLastSSEEventId = jest.fn().mockResolvedValue(undefined)
const task = new TaskArcadeSSE(makeMonitor({ saveLastSSEEventId }))
await task.asyncSetup()
FakeEventSource.instances[0].emit('status', {
data: JSON.stringify({ txid: 'eeee', txStatus: 'MINED', timestamp: '' }),
lastEventId: '55'
})
expect(saveLastSSEEventId).toHaveBeenCalledWith('55')
})
})
})