@@ -2,43 +2,56 @@ import { Store } from '../../../src/store';
22import GrpcAction from '../../../src/action/grpc' ;
33import NotificationAction from '../../../src/action/notification' ;
44import AppStorage from '../../../src/action/app-storage' ;
5+ import InfoAction from '../../../src/action/info' ;
56import AtplAction from '../../../src/action/autopilot' ;
67import * as logger from '../../../src/action/log' ;
8+ import nock from 'nock' ;
9+ import 'isomorphic-fetch' ;
710
811describe ( 'Action Autopilot Unit Test' , ( ) => {
912 let store ;
1013 let db ;
1114 let grpc ;
1215 let notify ;
16+ let info ;
1317 let autopilot ;
1418 let sandbox ;
1519
1620 beforeEach ( ( ) => {
1721 sandbox = sinon . createSandbox ( { } ) ;
1822 sandbox . stub ( logger ) ;
1923 store = new Store ( ) ;
24+ require ( '../../../src/config' ) . ATPL_DELAY = 1 ;
2025 grpc = sinon . createStubInstance ( GrpcAction ) ;
2126 db = sinon . createStubInstance ( AppStorage ) ;
2227 notify = sinon . createStubInstance ( NotificationAction ) ;
23- autopilot = new AtplAction ( store , grpc , db , notify ) ;
28+ info = sinon . createStubInstance ( InfoAction ) ;
29+ autopilot = new AtplAction ( store , grpc , info , db , notify ) ;
2430 } ) ;
2531
2632 afterEach ( ( ) => {
2733 sandbox . restore ( ) ;
2834 } ) ;
2935
3036 describe ( 'init()' , ( ) => {
31- it ( 'should enable autopilot by default' , async ( ) => {
37+ beforeEach ( ( ) => {
38+ sandbox . stub ( autopilot , 'updateNodeScores' ) . resolves ( ) ;
39+ autopilot . updateNodeScores . onThirdCall ( ) . resolves ( true ) ;
40+ } ) ;
41+
42+ it ( 'should enable autopilot and fetch scores by default' , async ( ) => {
3243 await autopilot . init ( ) ;
3344 expect ( grpc . sendAutopilotCommand , 'was called with' , 'modifyStatus' , {
3445 enable : true ,
3546 } ) ;
47+ expect ( autopilot . updateNodeScores , 'was called thrice' ) ;
3648 } ) ;
3749
38- it ( 'should not enable autopilot if disabled' , async ( ) => {
50+ it ( 'should not enable autopilot if disabled but fetch scores ' , async ( ) => {
3951 store . settings . autopilot = false ;
4052 await autopilot . init ( ) ;
4153 expect ( grpc . sendAutopilotCommand , 'was not called' ) ;
54+ expect ( autopilot . updateNodeScores , 'was called thrice' ) ;
4255 } ) ;
4356 } ) ;
4457
@@ -59,4 +72,110 @@ describe('Action Autopilot Unit Test', () => {
5972 expect ( db . save , 'was not called' ) ;
6073 } ) ;
6174 } ) ;
75+
76+ describe ( 'updateNodeScores()' , async ( ) => {
77+ let scoresJson ;
78+
79+ beforeEach ( ( ) => {
80+ scoresJson = {
81+ last_updated : '2019-03-21T04:39:41.031Z' ,
82+ scores : [
83+ {
84+ alias : 'some-alias' ,
85+ public_key : 'some-pubkey' ,
86+ score : 14035087 ,
87+ } ,
88+ ] ,
89+ } ;
90+ info . getInfo . resolves ( ) ;
91+ store . network = 'testnet' ;
92+ } ) ;
93+
94+ it ( 'should fail if network cannot be read' , async ( ) => {
95+ store . network = null ;
96+ await autopilot . updateNodeScores ( ) ;
97+ expect ( logger . error , 'was called once' ) ;
98+ expect ( grpc . sendAutopilotCommand , 'was not called' ) ;
99+ expect ( db . save , 'was not called' ) ;
100+ expect ( store . settings . nodeScores , 'to equal' , { } ) ;
101+ } ) ;
102+
103+ it ( 'should read scores for testnet from empty cache' , async ( ) => {
104+ nock ( 'https://nodes.lightning.computer' )
105+ . get ( '/availability/v1/btctestnet.json' )
106+ . reply ( 500 , 'Boom!' ) ;
107+
108+ await autopilot . updateNodeScores ( ) ;
109+ expect ( grpc . sendAutopilotCommand , 'was not called' ) ;
110+ expect ( logger . error , 'was called twice' ) ;
111+ expect ( db . save , 'was not called' ) ;
112+ expect ( store . settings . nodeScores , 'to equal' , { } ) ;
113+ } ) ;
114+
115+ it ( 'should handle invalid score format' , async ( ) => {
116+ delete scoresJson . scores [ 0 ] . public_key ;
117+ nock ( 'https://nodes.lightning.computer' )
118+ . get ( '/availability/v1/btctestnet.json' )
119+ . reply ( 200 , scoresJson ) ;
120+
121+ await autopilot . updateNodeScores ( ) ;
122+ expect ( logger . error , 'was called twice' ) ;
123+ expect ( grpc . sendAutopilotCommand , 'was not called' ) ;
124+ expect ( db . save , 'was not called' ) ;
125+ expect ( store . settings . nodeScores , 'to equal' , { } ) ;
126+ } ) ;
127+
128+ it ( 'should read scores for testnet from cache' , async ( ) => {
129+ nock ( 'https://nodes.lightning.computer' )
130+ . get ( '/availability/v1/btctestnet.json' )
131+ . reply ( 500 , 'Boom!' ) ;
132+ store . settings . nodeScores = {
133+ testnet : { 'some-pubkey' : 0.14035087 } ,
134+ } ;
135+
136+ await autopilot . updateNodeScores ( ) ;
137+ expect ( grpc . sendAutopilotCommand , 'was called with' , 'setScores' , {
138+ heuristic : 'externalscore' ,
139+ scores : { 'some-pubkey' : 0.14035087 } ,
140+ } ) ;
141+ expect ( logger . error , 'was called once' ) ;
142+ expect ( db . save , 'was not called' ) ;
143+ expect ( store . settings . nodeScores , 'to equal' , {
144+ testnet : { 'some-pubkey' : 0.14035087 } ,
145+ } ) ;
146+ } ) ;
147+
148+ it ( 'should set scores for testnet' , async ( ) => {
149+ nock ( 'https://nodes.lightning.computer' )
150+ . get ( '/availability/v1/btctestnet.json' )
151+ . reply ( 200 , scoresJson ) ;
152+
153+ await autopilot . updateNodeScores ( ) ;
154+ expect ( grpc . sendAutopilotCommand , 'was called with' , 'setScores' , {
155+ heuristic : 'externalscore' ,
156+ scores : { 'some-pubkey' : 0.14035087 } ,
157+ } ) ;
158+ expect ( db . save , 'was called once' ) ;
159+ expect ( store . settings . nodeScores , 'to equal' , {
160+ testnet : { 'some-pubkey' : 0.14035087 } ,
161+ } ) ;
162+ } ) ;
163+
164+ it ( 'should set scores for mainnet' , async ( ) => {
165+ store . network = 'mainnet' ;
166+ nock ( 'https://nodes.lightning.computer' )
167+ . get ( '/availability/v1/btc.json' )
168+ . reply ( 200 , scoresJson ) ;
169+
170+ await autopilot . updateNodeScores ( ) ;
171+ expect ( grpc . sendAutopilotCommand , 'was called with' , 'setScores' , {
172+ heuristic : 'externalscore' ,
173+ scores : { 'some-pubkey' : 0.14035087 } ,
174+ } ) ;
175+ expect ( db . save , 'was called once' ) ;
176+ expect ( store . settings . nodeScores , 'to equal' , {
177+ mainnet : { 'some-pubkey' : 0.14035087 } ,
178+ } ) ;
179+ } ) ;
180+ } ) ;
62181} ) ;
0 commit comments