22
33const test = require ( 'brittle' )
44const { getConfigs } = require ( '../../../workers/lib/server/handlers/configs.handlers' )
5+ const { RPC_METHODS } = require ( '../../../workers/lib/constants' )
6+ const { createMockCtxWithOrks } = require ( '../helpers/mockHelpers' )
57
68test ( 'getConfigs - happy path' , async ( t ) => {
7- const mockCtx = {
8- conf : {
9- orks : [ { rpcPublicKey : 'key1' } ]
10- } ,
11- net_r0 : {
12- jRequest : async ( key , method , payload ) => {
13- if ( method === 'getConfigs' ) {
14- return [
15- { id : 'config1' , name : 'Pool Config 1' , url : 'stratum://pool1.example.com' } ,
16- { id : 'config2' , name : 'Pool Config 2' , url : 'stratum://pool2.example.com' }
17- ]
18- }
19- return [ ]
9+ const mockCtx = createMockCtxWithOrks (
10+ [ { rpcPublicKey : 'key1' } ] ,
11+ async ( key , method , payload ) => {
12+ if ( method === 'getConfigs' ) {
13+ return [
14+ { id : 'config1' , name : 'Pool Config 1' , url : 'stratum://pool1.example.com' } ,
15+ { id : 'config2' , name : 'Pool Config 2' , url : 'stratum://pool2.example.com' }
16+ ]
2017 }
18+ if ( method === RPC_METHODS . LIST_THINGS ) return [ ]
19+ return [ ]
2120 }
22- }
21+ )
2322
2423 const mockReq = {
2524 params : { type : 'pool' } ,
@@ -35,17 +34,15 @@ test('getConfigs - happy path', async (t) => {
3534
3635test ( 'getConfigs - with query filter' , async ( t ) => {
3736 let capturedPayload = null
38- const mockCtx = {
39- conf : {
40- orks : [ { rpcPublicKey : 'key1' } ]
41- } ,
42- net_r0 : {
43- jRequest : async ( key , method , payload ) => {
44- capturedPayload = payload
45- return [ { id : 'config1' , active : true } ]
46- }
37+ const mockCtx = createMockCtxWithOrks (
38+ [ { rpcPublicKey : 'key1' } ] ,
39+ async ( key , method , payload ) => {
40+ if ( method === 'getConfigs' ) capturedPayload = payload
41+ if ( method === 'getConfigs' ) return [ { id : 'config1' , active : true } ]
42+ if ( method === RPC_METHODS . LIST_THINGS ) return [ ]
43+ return [ ]
4744 }
48- }
45+ )
4946
5047 const mockReq = {
5148 params : { type : 'pool' } ,
@@ -60,17 +57,15 @@ test('getConfigs - with query filter', async (t) => {
6057
6158test ( 'getConfigs - with fields projection' , async ( t ) => {
6259 let capturedPayload = null
63- const mockCtx = {
64- conf : {
65- orks : [ { rpcPublicKey : 'key1' } ]
66- } ,
67- net_r0 : {
68- jRequest : async ( key , method , payload ) => {
69- capturedPayload = payload
70- return [ { name : 'Config 1' } ]
71- }
60+ const mockCtx = createMockCtxWithOrks (
61+ [ { rpcPublicKey : 'key1' } ] ,
62+ async ( key , method , payload ) => {
63+ if ( method === 'getConfigs' ) capturedPayload = payload
64+ if ( method === 'getConfigs' ) return [ { name : 'Config 1' } ]
65+ if ( method === RPC_METHODS . LIST_THINGS ) return [ ]
66+ return [ ]
7267 }
73- }
68+ )
7469
7570 const mockReq = {
7671 params : { type : 'pool' } ,
@@ -86,17 +81,15 @@ test('getConfigs - with fields projection', async (t) => {
8681
8782test ( 'getConfigs - with both query and fields' , async ( t ) => {
8883 let capturedPayload = null
89- const mockCtx = {
90- conf : {
91- orks : [ { rpcPublicKey : 'key1' } ]
92- } ,
93- net_r0 : {
94- jRequest : async ( key , method , payload ) => {
95- capturedPayload = payload
96- return [ { name : 'Config 1' } ]
97- }
84+ const mockCtx = createMockCtxWithOrks (
85+ [ { rpcPublicKey : 'key1' } ] ,
86+ async ( key , method , payload ) => {
87+ if ( method === 'getConfigs' ) capturedPayload = payload
88+ if ( method === 'getConfigs' ) return [ { name : 'Config 1' } ]
89+ if ( method === RPC_METHODS . LIST_THINGS ) return [ ]
90+ return [ ]
9891 }
99- }
92+ )
10093
10194 const mockReq = {
10295 params : { type : 'pool' } ,
@@ -114,10 +107,7 @@ test('getConfigs - with both query and fields', async (t) => {
114107} )
115108
116109test ( 'getConfigs - invalid config type throws' , async ( t ) => {
117- const mockCtx = {
118- conf : { orks : [ ] } ,
119- net_r0 : { jRequest : async ( ) => ( [ ] ) }
120- }
110+ const mockCtx = createMockCtxWithOrks ( [ ] , async ( ) => ( [ ] ) )
121111
122112 const mockReq = {
123113 params : { type : 'invalid_type' } ,
@@ -134,10 +124,7 @@ test('getConfigs - invalid config type throws', async (t) => {
134124} )
135125
136126test ( 'getConfigs - missing config type throws' , async ( t ) => {
137- const mockCtx = {
138- conf : { orks : [ ] } ,
139- net_r0 : { jRequest : async ( ) => ( [ ] ) }
140- }
127+ const mockCtx = createMockCtxWithOrks ( [ ] , async ( ) => ( [ ] ) )
141128
142129 const mockReq = {
143130 params : { } ,
@@ -154,10 +141,7 @@ test('getConfigs - missing config type throws', async (t) => {
154141} )
155142
156143test ( 'getConfigs - invalid query JSON throws' , async ( t ) => {
157- const mockCtx = {
158- conf : { orks : [ { rpcPublicKey : 'key1' } ] } ,
159- net_r0 : { jRequest : async ( ) => ( [ ] ) }
160- }
144+ const mockCtx = createMockCtxWithOrks ( [ { rpcPublicKey : 'key1' } ] , async ( ) => ( [ ] ) )
161145
162146 const mockReq = {
163147 params : { type : 'pool' } ,
@@ -174,10 +158,7 @@ test('getConfigs - invalid query JSON throws', async (t) => {
174158} )
175159
176160test ( 'getConfigs - invalid fields JSON throws' , async ( t ) => {
177- const mockCtx = {
178- conf : { orks : [ { rpcPublicKey : 'key1' } ] } ,
179- net_r0 : { jRequest : async ( ) => ( [ ] ) }
180- }
161+ const mockCtx = createMockCtxWithOrks ( [ { rpcPublicKey : 'key1' } ] , async ( ) => ( [ ] ) )
181162
182163 const mockReq = {
183164 params : { type : 'pool' } ,
@@ -194,10 +175,7 @@ test('getConfigs - invalid fields JSON throws', async (t) => {
194175} )
195176
196177test ( 'getConfigs - empty ork results' , async ( t ) => {
197- const mockCtx = {
198- conf : { orks : [ { rpcPublicKey : 'key1' } ] } ,
199- net_r0 : { jRequest : async ( ) => ( [ ] ) }
200- }
178+ const mockCtx = createMockCtxWithOrks ( [ { rpcPublicKey : 'key1' } ] , async ( ) => ( [ ] ) )
201179
202180 const mockReq = {
203181 params : { type : 'pool' } ,
@@ -211,12 +189,10 @@ test('getConfigs - empty ork results', async (t) => {
211189} )
212190
213191test ( 'getConfigs - handles error results from orks' , async ( t ) => {
214- const mockCtx = {
215- conf : { orks : [ { rpcPublicKey : 'key1' } ] } ,
216- net_r0 : {
217- jRequest : async ( ) => ( { error : 'timeout' } )
218- }
219- }
192+ const mockCtx = createMockCtxWithOrks (
193+ [ { rpcPublicKey : 'key1' } ] ,
194+ async ( ) => ( { error : 'timeout' } )
195+ )
220196
221197 const mockReq = {
222198 params : { type : 'pool' } ,
@@ -231,20 +207,15 @@ test('getConfigs - handles error results from orks', async (t) => {
231207
232208test ( 'getConfigs - aggregates results from multiple orks' , async ( t ) => {
233209 let callCount = 0
234- const mockCtx = {
235- conf : {
236- orks : [
237- { rpcPublicKey : 'key1' } ,
238- { rpcPublicKey : 'key2' }
239- ]
240- } ,
241- net_r0 : {
242- jRequest : async ( ) => {
243- callCount ++
244- return [ { id : `config${ callCount } ` , name : `Config ${ callCount } ` } ]
245- }
210+ const mockCtx = createMockCtxWithOrks (
211+ [ { rpcPublicKey : 'key1' } , { rpcPublicKey : 'key2' } ] ,
212+ async ( key , method ) => {
213+ callCount ++
214+ if ( method === 'getConfigs' ) return [ { id : `config${ callCount } ` , name : `Config ${ callCount } ` } ]
215+ if ( method === RPC_METHODS . LIST_THINGS ) return [ ]
216+ return [ ]
246217 }
247- }
218+ )
248219
249220 const mockReq = {
250221 params : { type : 'pool' } ,
0 commit comments