@bsv/overlay-express 2.4.2 → 2.4.9

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.
Files changed (135) hide show
  1. package/LICENSE.txt +57 -27
  2. package/README.md +141 -56
  3. package/dist/cjs/mod.d.ts +11 -0
  4. package/dist/cjs/mod.d.ts.map +1 -0
  5. package/dist/cjs/mod.js.map +1 -1
  6. package/dist/cjs/package.json +26 -13
  7. package/dist/cjs/src/ArcadeProvider.d.ts +37 -0
  8. package/dist/cjs/src/ArcadeProvider.d.ts.map +1 -0
  9. package/dist/cjs/src/ArcadeProvider.js.map +1 -1
  10. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts +36 -0
  11. package/dist/cjs/src/BanAwareDiscoveryStorage.d.ts.map +1 -0
  12. package/dist/cjs/src/BanAwareDiscoveryStorage.js +5 -4
  13. package/dist/cjs/src/BanAwareDiscoveryStorage.js.map +1 -1
  14. package/dist/cjs/src/BanAwareLookupWrapper.d.ts +31 -0
  15. package/dist/cjs/src/BanAwareLookupWrapper.d.ts.map +1 -0
  16. package/dist/cjs/src/BanAwareLookupWrapper.js +3 -2
  17. package/dist/cjs/src/BanAwareLookupWrapper.js.map +1 -1
  18. package/dist/cjs/src/BanAwareTopicManager.d.ts +28 -0
  19. package/dist/cjs/src/BanAwareTopicManager.d.ts.map +1 -0
  20. package/dist/cjs/src/BanAwareTopicManager.js +3 -2
  21. package/dist/cjs/src/BanAwareTopicManager.js.map +1 -1
  22. package/dist/cjs/src/BanService.d.ts +81 -0
  23. package/dist/cjs/src/BanService.d.ts.map +1 -0
  24. package/dist/cjs/src/BanService.js.map +1 -1
  25. package/dist/cjs/src/ChaintracksProvider.d.ts +29 -0
  26. package/dist/cjs/src/ChaintracksProvider.d.ts.map +1 -0
  27. package/dist/cjs/src/ChaintracksProvider.js +1 -1
  28. package/dist/cjs/src/ChaintracksProvider.js.map +1 -1
  29. package/dist/cjs/src/JanitorService.d.ts +128 -0
  30. package/dist/cjs/src/JanitorService.d.ts.map +1 -0
  31. package/dist/cjs/src/JanitorService.js +108 -37
  32. package/dist/cjs/src/JanitorService.js.map +1 -1
  33. package/dist/cjs/src/OverlayExpress.d.ts +466 -0
  34. package/dist/cjs/src/OverlayExpress.d.ts.map +1 -0
  35. package/dist/cjs/src/OverlayExpress.js +307 -148
  36. package/dist/cjs/src/OverlayExpress.js.map +1 -1
  37. package/dist/cjs/src/OverlayMonitor.d.ts +197 -0
  38. package/dist/cjs/src/OverlayMonitor.d.ts.map +1 -0
  39. package/dist/cjs/src/OverlayMonitor.js +6 -4
  40. package/dist/cjs/src/OverlayMonitor.js.map +1 -1
  41. package/dist/cjs/src/ProviderChainBroadcaster.d.ts +18 -0
  42. package/dist/cjs/src/ProviderChainBroadcaster.d.ts.map +1 -0
  43. package/dist/cjs/src/ProviderChainBroadcaster.js +7 -3
  44. package/dist/cjs/src/ProviderChainBroadcaster.js.map +1 -1
  45. package/dist/cjs/src/ReorgStream.d.ts +74 -0
  46. package/dist/cjs/src/ReorgStream.d.ts.map +1 -0
  47. package/dist/cjs/src/ReorgStream.js +14 -6
  48. package/dist/cjs/src/ReorgStream.js.map +1 -1
  49. package/dist/cjs/src/generalGuide.md.d.ts +3 -0
  50. package/dist/cjs/src/generalGuide.md.d.ts.map +1 -0
  51. package/dist/cjs/src/generalGuide.md.js.map +1 -1
  52. package/dist/cjs/src/makeUserInterface.d.ts +22 -0
  53. package/dist/cjs/src/makeUserInterface.d.ts.map +1 -0
  54. package/dist/cjs/src/makeUserInterface.js +4 -4
  55. package/dist/cjs/src/makeUserInterface.js.map +1 -1
  56. package/dist/cjs/src/security/edgePolicy.d.ts +57 -0
  57. package/dist/cjs/src/security/edgePolicy.d.ts.map +1 -0
  58. package/dist/cjs/src/security/edgePolicy.js +379 -0
  59. package/dist/cjs/src/security/edgePolicy.js.map +1 -0
  60. package/dist/esm/mod.js.map +1 -1
  61. package/dist/esm/src/ArcadeProvider.js.map +1 -1
  62. package/dist/esm/src/BanAwareDiscoveryStorage.js +5 -4
  63. package/dist/esm/src/BanAwareDiscoveryStorage.js.map +1 -1
  64. package/dist/esm/src/BanAwareLookupWrapper.js +3 -2
  65. package/dist/esm/src/BanAwareLookupWrapper.js.map +1 -1
  66. package/dist/esm/src/BanAwareTopicManager.js +3 -2
  67. package/dist/esm/src/BanAwareTopicManager.js.map +1 -1
  68. package/dist/esm/src/ChaintracksProvider.js +1 -1
  69. package/dist/esm/src/ChaintracksProvider.js.map +1 -1
  70. package/dist/esm/src/JanitorService.js +84 -8
  71. package/dist/esm/src/JanitorService.js.map +1 -1
  72. package/dist/esm/src/OverlayExpress.js +290 -134
  73. package/dist/esm/src/OverlayExpress.js.map +1 -1
  74. package/dist/esm/src/ProviderChainBroadcaster.js +7 -3
  75. package/dist/esm/src/ProviderChainBroadcaster.js.map +1 -1
  76. package/dist/esm/src/ReorgStream.js +14 -6
  77. package/dist/esm/src/ReorgStream.js.map +1 -1
  78. package/dist/esm/src/makeUserInterface.js +3 -4
  79. package/dist/esm/src/makeUserInterface.js.map +1 -1
  80. package/dist/esm/src/security/edgePolicy.js +377 -0
  81. package/dist/esm/src/security/edgePolicy.js.map +1 -0
  82. package/dist/types/mod.d.ts +1 -1
  83. package/dist/types/mod.d.ts.map +1 -1
  84. package/dist/types/src/ArcadeProvider.d.ts.map +1 -1
  85. package/dist/types/src/BanAwareDiscoveryStorage.d.ts.map +1 -1
  86. package/dist/types/src/BanAwareLookupWrapper.d.ts.map +1 -1
  87. package/dist/types/src/BanAwareTopicManager.d.ts.map +1 -1
  88. package/dist/types/src/BanService.d.ts.map +1 -1
  89. package/dist/types/src/ChaintracksProvider.d.ts.map +1 -1
  90. package/dist/types/src/JanitorService.d.ts +14 -1
  91. package/dist/types/src/JanitorService.d.ts.map +1 -1
  92. package/dist/types/src/OverlayExpress.d.ts +48 -2
  93. package/dist/types/src/OverlayExpress.d.ts.map +1 -1
  94. package/dist/types/src/OverlayMonitor.d.ts.map +1 -1
  95. package/dist/types/src/ProviderChainBroadcaster.d.ts.map +1 -1
  96. package/dist/types/src/ReorgStream.d.ts.map +1 -1
  97. package/dist/types/src/generalGuide.md.d.ts +1 -1
  98. package/dist/types/src/generalGuide.md.d.ts.map +1 -1
  99. package/dist/types/src/makeUserInterface.d.ts +1 -2
  100. package/dist/types/src/makeUserInterface.d.ts.map +1 -1
  101. package/dist/types/src/security/edgePolicy.d.ts +57 -0
  102. package/dist/types/src/security/edgePolicy.d.ts.map +1 -0
  103. package/package.json +47 -25
  104. package/dist/cjs/tsconfig.cjs.tsbuildinfo +0 -1
  105. package/dist/esm/tsconfig.esm.tsbuildinfo +0 -1
  106. package/dist/types/tsconfig.types.tsbuildinfo +0 -1
  107. package/mod.ts +0 -47
  108. package/src/ArcadeProvider.ts +0 -232
  109. package/src/BanAwareDiscoveryStorage.ts +0 -91
  110. package/src/BanAwareLookupWrapper.ts +0 -95
  111. package/src/BanAwareTopicManager.ts +0 -87
  112. package/src/BanService.ts +0 -154
  113. package/src/ChaintracksProvider.ts +0 -79
  114. package/src/JanitorService.ts +0 -423
  115. package/src/OverlayExpress.ts +0 -2479
  116. package/src/OverlayMonitor.ts +0 -817
  117. package/src/ProviderChainBroadcaster.ts +0 -108
  118. package/src/ReorgStream.ts +0 -221
  119. package/src/__tests__/BanAwareDiscoveryStorage.test.ts +0 -131
  120. package/src/__tests__/BanAwareLookupWrapper.test.ts +0 -255
  121. package/src/__tests__/BanAwareTopicManager.test.ts +0 -132
  122. package/src/__tests__/BanService.test.ts +0 -310
  123. package/src/__tests__/InMemoryMigrationSource.test.ts +0 -11
  124. package/src/__tests__/JanitorService.test.ts +0 -941
  125. package/src/__tests__/OverlayExpress.test.ts +0 -1300
  126. package/src/__tests__/OverlayMonitor.test.ts +0 -255
  127. package/src/__tests__/OverlayProviders.live.test.ts +0 -43
  128. package/src/__tests__/OverlayProviders.test.ts +0 -123
  129. package/src/__tests__/README.md +0 -105
  130. package/src/__tests__/ReorgStream.test.ts +0 -66
  131. package/src/__tests__/makeUserInterface.test.ts +0 -193
  132. package/src/__tests__/setup.ts +0 -36
  133. package/src/auth-express-middleware.d.ts +0 -16
  134. package/src/generalGuide.md.ts +0 -74
  135. package/src/makeUserInterface.ts +0 -1224
@@ -1,941 +0,0 @@
1
- import { describe, it, expect, jest, beforeEach } from '@jest/globals'
2
- import { JanitorService } from '../JanitorService.js'
3
- import { Db } from 'mongodb'
4
-
5
- // Mock fetch globally
6
- global.fetch = jest.fn() as any
7
-
8
- describe('JanitorService', () => {
9
- let mockDb: Db
10
- let mockCollection: any
11
- let mockLogger: any
12
-
13
- beforeEach(() => {
14
- jest.clearAllMocks()
15
-
16
- mockCollection = {
17
- find: jest.fn().mockReturnValue({
18
- toArray: jest.fn<any>().mockResolvedValue([] as any)
19
- }),
20
- updateOne: jest.fn<any>().mockResolvedValue({} as any),
21
- deleteOne: jest.fn<any>().mockResolvedValue({} as any)
22
- }
23
-
24
- mockDb = {
25
- collection: jest.fn().mockReturnValue(mockCollection)
26
- } as unknown as Db
27
-
28
- mockLogger = {
29
- log: jest.fn(),
30
- error: jest.fn()
31
- }
32
- })
33
-
34
- describe('constructor', () => {
35
- it('should create instance with required config', () => {
36
- const janitor = new JanitorService({
37
- mongoDb: mockDb
38
- })
39
-
40
- expect(janitor).toBeInstanceOf(JanitorService)
41
- })
42
-
43
- it('should use default values when not provided', () => {
44
- const janitor = new JanitorService({
45
- mongoDb: mockDb
46
- })
47
-
48
- expect(janitor).toBeDefined()
49
- })
50
-
51
- it('should use custom logger when provided', () => {
52
- const janitor = new JanitorService({
53
- mongoDb: mockDb,
54
- logger: mockLogger
55
- })
56
-
57
- expect(janitor).toBeDefined()
58
- })
59
-
60
- it('should use custom timeout and revoke score', () => {
61
- const janitor = new JanitorService({
62
- mongoDb: mockDb,
63
- requestTimeoutMs: 5000,
64
- hostDownRevokeScore: 5
65
- })
66
-
67
- expect(janitor).toBeDefined()
68
- })
69
- })
70
-
71
- describe('run', () => {
72
- it('should check both SHIP and SLAP collections', async () => {
73
- const janitor = new JanitorService({
74
- mongoDb: mockDb,
75
- logger: mockLogger
76
- })
77
-
78
- await janitor.run()
79
-
80
- expect(mockDb.collection).toHaveBeenCalledWith('shipRecords')
81
- expect(mockDb.collection).toHaveBeenCalledWith('slapRecords')
82
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('Running janitor'))
83
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('completed'))
84
- })
85
-
86
- it('should handle errors during health checks', async () => {
87
- (mockDb as any).collection = jest.fn().mockImplementation(() => {
88
- throw new Error('Database error')
89
- })
90
-
91
- const janitor = new JanitorService({
92
- mongoDb: mockDb,
93
- logger: mockLogger
94
- })
95
-
96
- await janitor.run()
97
- expect(mockLogger.error).toHaveBeenCalled()
98
- })
99
-
100
- it('should process empty collections without errors', async () => {
101
- const janitor = new JanitorService({
102
- mongoDb: mockDb,
103
- logger: mockLogger
104
- })
105
-
106
- await janitor.run()
107
-
108
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('completed'))
109
- })
110
- })
111
-
112
- describe('output processing', () => {
113
- it('should process output with domain field', async () => {
114
- const mockOutput = {
115
- _id: '123',
116
- txid: 'abc123',
117
- outputIndex: 0,
118
- domain: 'https://example.com',
119
- down: 0
120
- }
121
-
122
- mockCollection.find.mockReturnValue({
123
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
124
- })
125
-
126
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
127
- ok: true,
128
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
129
- })
130
-
131
- const janitor = new JanitorService({
132
- mongoDb: mockDb,
133
- logger: mockLogger
134
- })
135
-
136
- await janitor.run()
137
-
138
- expect(global.fetch).toHaveBeenCalled()
139
- })
140
-
141
- it('should process output with url field', async () => {
142
- const mockOutput = {
143
- _id: '123',
144
- txid: 'abc123',
145
- outputIndex: 0,
146
- url: 'https://example.com',
147
- down: 0
148
- }
149
-
150
- mockCollection.find.mockReturnValue({
151
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
152
- })
153
-
154
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
155
- ok: true,
156
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
157
- })
158
-
159
- const janitor = new JanitorService({
160
- mongoDb: mockDb,
161
- logger: mockLogger
162
- })
163
-
164
- await janitor.run()
165
-
166
- expect(global.fetch).toHaveBeenCalled()
167
- })
168
-
169
- it('should process output with serviceURL field', async () => {
170
- const mockOutput = {
171
- _id: '123',
172
- txid: 'abc123',
173
- outputIndex: 0,
174
- serviceURL: 'https://example.com',
175
- down: 0
176
- }
177
-
178
- mockCollection.find.mockReturnValue({
179
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
180
- })
181
-
182
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
183
- ok: true,
184
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
185
- })
186
-
187
- const janitor = new JanitorService({
188
- mongoDb: mockDb,
189
- logger: mockLogger
190
- })
191
-
192
- await janitor.run()
193
-
194
- expect(global.fetch).toHaveBeenCalled()
195
- })
196
-
197
- it('should process output with protocols array', async () => {
198
- const mockOutput = {
199
- _id: '123',
200
- txid: 'abc123',
201
- outputIndex: 0,
202
- protocols: ['https://example.com', 'http://other.com'],
203
- down: 0
204
- }
205
-
206
- mockCollection.find.mockReturnValue({
207
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
208
- })
209
-
210
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
211
- ok: true,
212
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
213
- })
214
-
215
- const janitor = new JanitorService({
216
- mongoDb: mockDb,
217
- logger: mockLogger
218
- })
219
-
220
- await janitor.run()
221
-
222
- expect(global.fetch).toHaveBeenCalled()
223
- })
224
-
225
- it('should skip output without valid URL', async () => {
226
- const mockOutput = {
227
- _id: '123',
228
- txid: 'abc123',
229
- outputIndex: 0,
230
- down: 0
231
- }
232
-
233
- mockCollection.find.mockReturnValue({
234
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
235
- })
236
-
237
- const janitor = new JanitorService({
238
- mongoDb: mockDb,
239
- logger: mockLogger
240
- })
241
-
242
- await janitor.run()
243
-
244
- expect(global.fetch).not.toHaveBeenCalled()
245
- })
246
- })
247
-
248
- describe('domain validation', () => {
249
- it('should accept valid domain', async () => {
250
- const mockOutput = {
251
- _id: '123',
252
- txid: 'abc123',
253
- outputIndex: 0,
254
- domain: 'https://valid-domain.com',
255
- down: 0
256
- }
257
-
258
- mockCollection.find.mockReturnValue({
259
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
260
- })
261
-
262
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
263
- ok: true,
264
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
265
- })
266
-
267
- const janitor = new JanitorService({
268
- mongoDb: mockDb,
269
- logger: mockLogger
270
- })
271
-
272
- await janitor.run()
273
-
274
- expect(global.fetch).toHaveBeenCalled()
275
- })
276
-
277
- it('should reject invalid domain and increment down counter', async () => {
278
- const mockOutput = {
279
- _id: '123',
280
- txid: 'abc123',
281
- outputIndex: 0,
282
- domain: 'invalid domain!@#',
283
- down: 0
284
- }
285
-
286
- mockCollection.find.mockReturnValue({
287
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
288
- })
289
-
290
- const janitor = new JanitorService({
291
- mongoDb: mockDb,
292
- logger: mockLogger
293
- })
294
-
295
- await janitor.run()
296
-
297
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
298
- { _id: '123' },
299
- { $inc: { down: 1 } }
300
- )
301
- })
302
-
303
- it('should accept localhost', async () => {
304
- const mockOutput = {
305
- _id: '123',
306
- txid: 'abc123',
307
- outputIndex: 0,
308
- domain: 'http://localhost:3000',
309
- down: 0
310
- }
311
-
312
- mockCollection.find.mockReturnValue({
313
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
314
- })
315
-
316
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
317
- ok: true,
318
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
319
- })
320
-
321
- const janitor = new JanitorService({
322
- mongoDb: mockDb,
323
- logger: mockLogger
324
- })
325
-
326
- await janitor.run()
327
-
328
- expect(global.fetch).toHaveBeenCalled()
329
- })
330
-
331
- it('should accept IP address', async () => {
332
- const mockOutput = {
333
- _id: '123',
334
- txid: 'abc123',
335
- outputIndex: 0,
336
- domain: 'http://192.168.1.1',
337
- down: 0
338
- }
339
-
340
- mockCollection.find.mockReturnValue({
341
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
342
- })
343
-
344
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
345
- ok: true,
346
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
347
- })
348
-
349
- const janitor = new JanitorService({
350
- mongoDb: mockDb,
351
- logger: mockLogger
352
- })
353
-
354
- await janitor.run()
355
-
356
- expect(global.fetch).toHaveBeenCalled()
357
- })
358
- })
359
-
360
- describe('health check', () => {
361
- it('should mark output as healthy when health check passes', async () => {
362
- const mockOutput = {
363
- _id: '123',
364
- txid: 'abc123',
365
- outputIndex: 0,
366
- domain: 'https://example.com',
367
- down: 2
368
- }
369
-
370
- mockCollection.find.mockReturnValue({
371
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
372
- })
373
-
374
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
375
- ok: true,
376
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
377
- })
378
-
379
- const janitor = new JanitorService({
380
- mongoDb: mockDb,
381
- logger: mockLogger
382
- })
383
-
384
- await janitor.run()
385
-
386
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
387
- { _id: '123' },
388
- { $inc: { down: -1 } }
389
- )
390
- })
391
-
392
- it('should not decrement when already at 0', async () => {
393
- const mockOutput = {
394
- _id: '123',
395
- txid: 'abc123',
396
- outputIndex: 0,
397
- domain: 'https://example.com',
398
- down: 0
399
- }
400
-
401
- mockCollection.find.mockReturnValue({
402
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
403
- })
404
-
405
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
406
- ok: true,
407
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
408
- })
409
-
410
- const janitor = new JanitorService({
411
- mongoDb: mockDb,
412
- logger: mockLogger
413
- })
414
-
415
- await janitor.run()
416
-
417
- expect(mockCollection.updateOne).not.toHaveBeenCalled()
418
- })
419
-
420
- it('should increment down counter when health check fails', async () => {
421
- const mockOutput = {
422
- _id: '123',
423
- txid: 'abc123',
424
- outputIndex: 0,
425
- domain: 'https://example.com',
426
- down: 0
427
- }
428
-
429
- mockCollection.find.mockReturnValue({
430
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
431
- })
432
-
433
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
434
- ok: false
435
- })
436
-
437
- const janitor = new JanitorService({
438
- mongoDb: mockDb,
439
- logger: mockLogger
440
- })
441
-
442
- await janitor.run()
443
-
444
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
445
- { _id: '123' },
446
- { $inc: { down: 1 } }
447
- )
448
- })
449
-
450
- it('should delete output when down count reaches threshold', async () => {
451
- const mockOutput = {
452
- _id: '123',
453
- txid: 'abc123',
454
- outputIndex: 0,
455
- domain: 'https://example.com',
456
- down: 2
457
- }
458
-
459
- mockCollection.find.mockReturnValue({
460
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
461
- })
462
-
463
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
464
- ok: false
465
- })
466
-
467
- const janitor = new JanitorService({
468
- mongoDb: mockDb,
469
- logger: mockLogger,
470
- hostDownRevokeScore: 3
471
- })
472
-
473
- await janitor.run()
474
-
475
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
476
- expect(mockLogger.log).toHaveBeenCalledWith(expect.stringContaining('Removing output'))
477
- })
478
-
479
- it('should handle fetch timeout', async () => {
480
- const mockOutput = {
481
- _id: '123',
482
- txid: 'abc123',
483
- outputIndex: 0,
484
- domain: 'https://example.com',
485
- down: 0
486
- }
487
-
488
- mockCollection.find.mockReturnValue({
489
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
490
- })
491
-
492
- const abortError = new Error('Aborted')
493
- abortError.name = 'AbortError'
494
- ;(global.fetch as jest.Mock<any>).mockRejectedValue(abortError)
495
-
496
- const janitor = new JanitorService({
497
- mongoDb: mockDb,
498
- logger: mockLogger,
499
- requestTimeoutMs: 1000
500
- })
501
-
502
- await janitor.run()
503
-
504
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
505
- { _id: '123' },
506
- { $inc: { down: 1 } }
507
- )
508
- })
509
-
510
- it('should handle fetch errors', async () => {
511
- const mockOutput = {
512
- _id: '123',
513
- txid: 'abc123',
514
- outputIndex: 0,
515
- domain: 'https://example.com',
516
- down: 0
517
- }
518
-
519
- mockCollection.find.mockReturnValue({
520
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
521
- })
522
-
523
- ;(global.fetch as jest.Mock<any>).mockRejectedValue(new Error('Network error'))
524
-
525
- const janitor = new JanitorService({
526
- mongoDb: mockDb,
527
- logger: mockLogger
528
- })
529
-
530
- await janitor.run()
531
-
532
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
533
- { _id: '123' },
534
- { $inc: { down: 1 } }
535
- )
536
- })
537
-
538
- it('should handle invalid JSON response', async () => {
539
- const mockOutput = {
540
- _id: '123',
541
- txid: 'abc123',
542
- outputIndex: 0,
543
- domain: 'https://example.com',
544
- down: 0
545
- }
546
-
547
- mockCollection.find.mockReturnValue({
548
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
549
- })
550
-
551
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
552
- ok: true,
553
- json: jest.fn<any>().mockRejectedValue(new Error('Invalid JSON'))
554
- })
555
-
556
- const janitor = new JanitorService({
557
- mongoDb: mockDb,
558
- logger: mockLogger
559
- })
560
-
561
- await janitor.run()
562
-
563
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
564
- { _id: '123' },
565
- { $inc: { down: 1 } }
566
- )
567
- })
568
-
569
- it('should verify health endpoint returns status: ok', async () => {
570
- const mockOutput = {
571
- _id: '123',
572
- txid: 'abc123',
573
- outputIndex: 0,
574
- domain: 'https://example.com',
575
- down: 1
576
- }
577
-
578
- mockCollection.find.mockReturnValue({
579
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
580
- })
581
-
582
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
583
- ok: true,
584
- json: jest.fn<any>().mockResolvedValue({ status: 'error' })
585
- })
586
-
587
- const janitor = new JanitorService({
588
- mongoDb: mockDb,
589
- logger: mockLogger
590
- })
591
-
592
- await janitor.run()
593
-
594
- expect(mockCollection.updateOne).toHaveBeenCalledWith(
595
- { _id: '123' },
596
- { $inc: { down: 1 } }
597
- )
598
- })
599
- })
600
-
601
- describe('checkHost', () => {
602
- it('should return healthy when health endpoint returns ok', async () => {
603
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
604
- ok: true,
605
- status: 200,
606
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
607
- })
608
-
609
- const janitor = new JanitorService({
610
- mongoDb: mockDb,
611
- logger: mockLogger
612
- })
613
-
614
- const result = await janitor.checkHost('https://example.com')
615
-
616
- expect(result.healthy).toBe(true)
617
- expect(result.statusCode).toBe(200)
618
- expect(result.responseTimeMs).toBeGreaterThanOrEqual(0)
619
- })
620
-
621
- it('should return unhealthy for invalid domain', async () => {
622
- const janitor = new JanitorService({
623
- mongoDb: mockDb,
624
- logger: mockLogger
625
- })
626
-
627
- const result = await janitor.checkHost('invalid domain!@#')
628
-
629
- expect(result.healthy).toBe(false)
630
- expect(result.error).toBe('Invalid domain')
631
- })
632
-
633
- it('should return unhealthy when health endpoint returns non-ok status', async () => {
634
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
635
- ok: false,
636
- status: 503
637
- })
638
-
639
- const janitor = new JanitorService({
640
- mongoDb: mockDb,
641
- logger: mockLogger
642
- })
643
-
644
- const result = await janitor.checkHost('https://example.com')
645
-
646
- expect(result.healthy).toBe(false)
647
- expect(result.statusCode).toBe(503)
648
- expect(result.error).toBe('HTTP 503')
649
- })
650
-
651
- it('should return unhealthy when response does not have status ok', async () => {
652
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
653
- ok: true,
654
- status: 200,
655
- json: jest.fn<any>().mockResolvedValue({ status: 'error' })
656
- })
657
-
658
- const janitor = new JanitorService({
659
- mongoDb: mockDb,
660
- logger: mockLogger
661
- })
662
-
663
- const result = await janitor.checkHost('https://example.com')
664
-
665
- expect(result.healthy).toBe(false)
666
- expect(result.error).toBe('Unexpected response')
667
- })
668
-
669
- it('should handle timeout (AbortError)', async () => {
670
- const abortError = new Error('Aborted')
671
- abortError.name = 'AbortError'
672
- ;(global.fetch as jest.Mock<any>).mockRejectedValue(abortError)
673
-
674
- const janitor = new JanitorService({
675
- mongoDb: mockDb,
676
- logger: mockLogger,
677
- requestTimeoutMs: 100
678
- })
679
-
680
- const result = await janitor.checkHost('https://example.com')
681
-
682
- expect(result.healthy).toBe(false)
683
- expect(result.error).toBe('Timeout')
684
- })
685
-
686
- it('should handle network errors', async () => {
687
- ;(global.fetch as jest.Mock<any>).mockRejectedValue(new Error('ECONNREFUSED'))
688
-
689
- const janitor = new JanitorService({
690
- mongoDb: mockDb,
691
- logger: mockLogger
692
- })
693
-
694
- const result = await janitor.checkHost('https://example.com')
695
-
696
- expect(result.healthy).toBe(false)
697
- expect(result.error).toBe('ECONNREFUSED')
698
- })
699
-
700
- it('should prepend https:// to domains without protocol', async () => {
701
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
702
- ok: true,
703
- status: 200,
704
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
705
- })
706
-
707
- const janitor = new JanitorService({
708
- mongoDb: mockDb,
709
- logger: mockLogger
710
- })
711
-
712
- await janitor.checkHost('example.com')
713
-
714
- expect(global.fetch).toHaveBeenCalledWith(
715
- 'https://example.com/health',
716
- expect.any(Object)
717
- )
718
- })
719
- })
720
-
721
- describe('getHealthStatus', () => {
722
- it('should return health status for ship and slap records', async () => {
723
- const shipOutput = {
724
- txid: 'ship-tx',
725
- outputIndex: 0,
726
- domain: 'https://ship.example.com',
727
- topic: 'tm_ship',
728
- identityKey: 'key1',
729
- createdAt: new Date(),
730
- down: 1
731
- }
732
- const slapOutput = {
733
- txid: 'slap-tx',
734
- outputIndex: 1,
735
- domain: 'https://slap.example.com',
736
- service: 'ls_slap',
737
- identityKey: 'key2',
738
- createdAt: new Date(),
739
- down: 0
740
- }
741
-
742
- // First call returns ship records, second returns slap records
743
- let callCount = 0
744
- mockCollection.find.mockImplementation(() => ({
745
- toArray: jest.fn<any>().mockResolvedValue(callCount++ === 0 ? [shipOutput] : [slapOutput])
746
- }))
747
-
748
- const janitor = new JanitorService({
749
- mongoDb: mockDb,
750
- logger: mockLogger
751
- })
752
-
753
- const result = await janitor.getHealthStatus()
754
-
755
- expect(result.ship).toHaveLength(1)
756
- expect(result.ship[0].txid).toBe('ship-tx')
757
- expect(result.ship[0].downCount).toBe(1)
758
- expect(result.slap).toHaveLength(1)
759
- expect(result.slap[0].txid).toBe('slap-tx')
760
- expect(result.slap[0].downCount).toBe(0)
761
- })
762
-
763
- it('should return empty arrays when no records exist', async () => {
764
- const janitor = new JanitorService({
765
- mongoDb: mockDb,
766
- logger: mockLogger
767
- })
768
-
769
- const result = await janitor.getHealthStatus()
770
-
771
- expect(result.ship).toEqual([])
772
- expect(result.slap).toEqual([])
773
- })
774
- })
775
-
776
- describe('ban service integration', () => {
777
- it('should auto-ban domain and outpoint when threshold is reached', async () => {
778
- const mockBanService = {
779
- banDomain: jest.fn<any>().mockResolvedValue(undefined),
780
- banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
781
- }
782
-
783
- const mockOutput = {
784
- _id: '123',
785
- txid: 'abc123',
786
- outputIndex: 0,
787
- domain: 'https://dead-host.com',
788
- down: 2
789
- }
790
-
791
- mockCollection.find.mockReturnValue({
792
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
793
- })
794
-
795
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
796
-
797
- const janitor = new JanitorService({
798
- mongoDb: mockDb,
799
- logger: mockLogger,
800
- hostDownRevokeScore: 3,
801
- banService: mockBanService as any
802
- })
803
-
804
- await janitor.run()
805
-
806
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
807
- expect(mockBanService.banOutpoint).toHaveBeenCalledWith(
808
- 'abc123',
809
- 0,
810
- expect.stringContaining('Auto-banned'),
811
- 'https://dead-host.com'
812
- )
813
- expect(mockBanService.banDomain).toHaveBeenCalledWith(
814
- 'https://dead-host.com',
815
- expect.stringContaining('Auto-banned')
816
- )
817
- })
818
-
819
- it('should not auto-ban when autoBanOnRemoval is false', async () => {
820
- const mockBanService = {
821
- banDomain: jest.fn<any>().mockResolvedValue(undefined),
822
- banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
823
- }
824
-
825
- const mockOutput = {
826
- _id: '123',
827
- txid: 'abc123',
828
- outputIndex: 0,
829
- domain: 'https://dead-host.com',
830
- down: 2
831
- }
832
-
833
- mockCollection.find.mockReturnValue({
834
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
835
- })
836
-
837
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
838
-
839
- const janitor = new JanitorService({
840
- mongoDb: mockDb,
841
- logger: mockLogger,
842
- hostDownRevokeScore: 3,
843
- banService: mockBanService as any,
844
- autoBanOnRemoval: false
845
- })
846
-
847
- await janitor.run()
848
-
849
- expect(mockCollection.deleteOne).toHaveBeenCalledWith({ _id: '123' })
850
- expect(mockBanService.banOutpoint).not.toHaveBeenCalled()
851
- expect(mockBanService.banDomain).not.toHaveBeenCalled()
852
- })
853
-
854
- it('should not ban domain when domain is unknown', async () => {
855
- const mockBanService = {
856
- banDomain: jest.fn<any>().mockResolvedValue(undefined),
857
- banOutpoint: jest.fn<any>().mockResolvedValue(undefined)
858
- }
859
-
860
- const mockOutput = {
861
- _id: '123',
862
- txid: 'abc123',
863
- outputIndex: 0,
864
- down: 2
865
- // no domain field
866
- }
867
-
868
- mockCollection.find.mockReturnValue({
869
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
870
- })
871
-
872
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({ ok: false })
873
-
874
- const janitor = new JanitorService({
875
- mongoDb: mockDb,
876
- logger: mockLogger,
877
- hostDownRevokeScore: 3,
878
- banService: mockBanService as any
879
- })
880
-
881
- await janitor.run()
882
-
883
- expect(mockBanService.banOutpoint).toHaveBeenCalled()
884
- expect(mockBanService.banDomain).not.toHaveBeenCalled()
885
- })
886
- })
887
-
888
- describe('run report', () => {
889
- it('should return a JanitorReport with summary', async () => {
890
- const mockOutput = {
891
- _id: '123',
892
- txid: 'abc123',
893
- outputIndex: 0,
894
- domain: 'https://example.com',
895
- down: 0
896
- }
897
-
898
- mockCollection.find.mockReturnValue({
899
- toArray: jest.fn<any>().mockResolvedValue([mockOutput])
900
- })
901
-
902
- ;(global.fetch as jest.Mock<any>).mockResolvedValue({
903
- ok: true,
904
- status: 200,
905
- json: jest.fn<any>().mockResolvedValue({ status: 'ok' })
906
- })
907
-
908
- const janitor = new JanitorService({
909
- mongoDb: mockDb,
910
- logger: mockLogger
911
- })
912
-
913
- const report = await janitor.run()
914
-
915
- expect(report.startedAt).toBeInstanceOf(Date)
916
- expect(report.completedAt).toBeInstanceOf(Date)
917
- expect(report.durationMs).toBeGreaterThanOrEqual(0)
918
- expect(report.summary.totalChecked).toBeGreaterThan(0)
919
- expect(report.summary.removed).toBe(0)
920
- expect(report.summary.banned).toBe(0)
921
- })
922
-
923
- it('should handle collection errors gracefully and return empty report', async () => {
924
- ;(mockDb as any).collection = jest.fn().mockImplementation(() => {
925
- throw new Error('Database error')
926
- })
927
-
928
- const janitor = new JanitorService({
929
- mongoDb: mockDb,
930
- logger: mockLogger
931
- })
932
-
933
- // checkTopicOutputs catches errors internally, so run() returns an empty report
934
- const report = await janitor.run()
935
- expect(report.summary.totalChecked).toBe(0)
936
- expect(report.shipResults).toEqual([])
937
- expect(report.slapResults).toEqual([])
938
- expect(mockLogger.error).toHaveBeenCalled()
939
- })
940
- })
941
- })