@@ -14,6 +14,8 @@ import {
1414import {
1515 performMainNetworkSync ,
1616 startNetworkSyncing ,
17+ calculateAvailableSpaceToAdd ,
18+ getBoundedNetworksToAdd ,
1719} from './controller-integration' ;
1820import * as ControllerIntegrationModule from './controller-integration' ;
1921import * as ServicesModule from './services' ;
@@ -40,6 +42,69 @@ const storageOpts: UserStorageBaseOptions = {
4042 storageKey : MOCK_STORAGE_KEY ,
4143} ;
4244
45+ describe ( 'network-syncing/controller-integration - calculateAvailableSpaceToAdd()' , ( ) => {
46+ it ( 'returns available space to add' , ( ) => {
47+ expect ( calculateAvailableSpaceToAdd ( 5 , 10 ) ) . toBe ( 5 ) ;
48+ expect ( calculateAvailableSpaceToAdd ( 9 , 10 ) ) . toBe ( 1 ) ;
49+ } ) ;
50+ it ( 'returns 0 if there is no available space to add' , ( ) => {
51+ expect ( calculateAvailableSpaceToAdd ( 5 , 5 ) ) . toBe ( 0 ) ;
52+ expect ( calculateAvailableSpaceToAdd ( 10 , 5 ) ) . toBe ( 0 ) ;
53+ } ) ;
54+ } ) ;
55+
56+ describe ( 'network-syncing/controller-integration - getBoundedNetworksToAdd()' , ( ) => {
57+ it ( 'returns networks to add if within bounds' , ( ) => {
58+ const originalNetworks = arrangeTestNetworks ( [ '0x1' , '0x2' ] ) ;
59+ const networksToAdd = arrangeTestNetworks ( [ '0x3' , '0x4' ] ) ;
60+ const result = getBoundedNetworksToAdd ( originalNetworks , networksToAdd ) ;
61+ expect ( result ) . toHaveLength ( 2 ) ; // we can all networks
62+ } ) ;
63+
64+ it ( 'returns a max size of networks to add if larger than max bounds' , ( ) => {
65+ const originalNetworks = arrangeTestNetworks ( [ '0x1' , '0x2' ] ) ;
66+ const networksToAdd = arrangeTestNetworks ( [ '0x3' , '0x4' ] ) ;
67+ const result = getBoundedNetworksToAdd ( originalNetworks , networksToAdd , 3 ) ; // max size set to 3
68+ expect ( result ) . toHaveLength ( 1 ) ; // we can only add 1 network
69+ } ) ;
70+
71+ it ( 'returns an empty array if there is not available space to add networks' , ( ) => {
72+ const originalNetworks = arrangeTestNetworks ( [ '0x1' , '0x2' ] ) ;
73+ const networksToAdd = arrangeTestNetworks ( [ '0x3' , '0x4' ] ) ;
74+
75+ const result2 = getBoundedNetworksToAdd ( originalNetworks , networksToAdd , 2 ) ; // max size is set to 2
76+ expect ( result2 ) . toHaveLength ( 0 ) ; // we've used up all the available space, so no networks can be added
77+
78+ const result3 = getBoundedNetworksToAdd ( originalNetworks , networksToAdd , 1 ) ; // max size is set to 1
79+ expect ( result3 ) . toHaveLength ( 0 ) ; // we've used up all the available space, so no networks can be added
80+ } ) ;
81+
82+ it ( 'returns a list of networks ordered by chainId to add' , ( ) => {
83+ const originalNetworks = arrangeTestNetworks ( [ '0x1' , '0x2' ] ) ;
84+ const networksToAdd = arrangeTestNetworks ( [ '0x3' , '0x4' , '0x33' ] ) ;
85+
86+ const result = getBoundedNetworksToAdd ( originalNetworks , networksToAdd , 4 ) ; // Max size is set to 4
87+ expect ( result ) . toHaveLength ( 2 ) ; // We can only add 2 of the 3 networks to add
88+
89+ // we are only adding 0x3 and 0x33 since the list was ordered
90+ // 0x4 was dropped as we ran out of available space
91+ expect ( result . map ( ( n ) => n . chainId ) ) . toStrictEqual ( [ '0x3' , '0x33' ] ) ;
92+ } ) ;
93+
94+ /**
95+ * Test Utility - creates an array of network configurations
96+ * @param chains - list of chains to create
97+ * @returns array of mock network configurations
98+ */
99+ function arrangeTestNetworks ( chains : `0x${string } `[ ] ) {
100+ return chains . map ( ( chainId ) => {
101+ const n = createMockNetworkConfiguration ( ) ;
102+ n . chainId = chainId ;
103+ return n ;
104+ } ) ;
105+ }
106+ } ) ;
107+
43108describe ( 'network-syncing/controller-integration - startNetworkSyncing()' , ( ) => {
44109 it ( `should successfully sync when NetworkController:networkRemoved is emitted` , async ( ) => {
45110 const { baseMessenger, props, deleteNetworkMock } = arrangeMocks ( ) ;
@@ -237,6 +302,33 @@ describe('network-syncing/controller-integration - performMainSync()', () => {
237302 expect ( mockCalls . mockNetworkControllerRemoveNetwork ) . not . toHaveBeenCalled ( ) ;
238303 } ) ;
239304
305+ it ( 'should not add missing local networks if there is no available space' , async ( ) => {
306+ const { messenger, getStorageConfig, mockSync, mockServices, mockCalls } =
307+ arrangeMocks ( ) ;
308+ mockSync . findNetworksToUpdate . mockReturnValue ( {
309+ remoteNetworksToUpdate : [ ] ,
310+ missingLocalNetworks : [ createMockNetworkConfiguration ( ) ] ,
311+ localNetworksToUpdate : [ ] ,
312+ localNetworksToRemove : [ ] ,
313+ } ) ;
314+
315+ const mockAddCallback = jest . fn ( ) ;
316+ await performMainNetworkSync ( {
317+ messenger,
318+ getStorageConfig,
319+ onNetworkAdded : mockAddCallback ,
320+ maxNetworksToAdd : 0 , // mocking that there is no available space
321+ } ) ;
322+
323+ expect ( mockServices . mockBatchUpdateNetworks ) . not . toHaveBeenCalled ( ) ;
324+ expect ( mockCalls . mockNetworkControllerAddNetwork ) . not . toHaveBeenCalled ( ) ;
325+ expect ( mockAddCallback ) . not . toHaveBeenCalled ( ) ;
326+ expect (
327+ mockCalls . mockNetworkControllerDangerouslySetNetworkConfiguration ,
328+ ) . not . toHaveBeenCalled ( ) ;
329+ expect ( mockCalls . mockNetworkControllerRemoveNetwork ) . not . toHaveBeenCalled ( ) ;
330+ } ) ;
331+
240332 it ( 'should update local networks' , async ( ) => {
241333 const { messenger, getStorageConfig, mockSync, mockServices, mockCalls } =
242334 arrangeMocks ( ) ;
0 commit comments