@@ -283,3 +283,134 @@ func newTestApp(t *testing.T) *Avalanche {
283283 Log : logging.NoLog {},
284284 }
285285}
286+
287+ func TestExtractBlockchainIDFromEndpoint (t * testing.T ) {
288+ tests := []struct {
289+ name string
290+ endpoint string
291+ expected string
292+ }{
293+ {
294+ name : "valid RPC endpoint" ,
295+ endpoint : "http://127.0.0.1:55067/ext/bc/2ATnxvq9GwPrEPHyULDJqpanFNJecvzSGZ3w5jMzfZAWSmXC6u/rpc" ,
296+ expected : "2ATnxvq9GwPrEPHyULDJqpanFNJecvzSGZ3w5jMzfZAWSmXC6u" ,
297+ },
298+ {
299+ name : "valid WS endpoint" ,
300+ endpoint : "ws://127.0.0.1:55067/ext/bc/X2oDj86zGjCRCy6vd8Cca4FDStMeFAHWc7SUUygPCCfYf9sHh/ws" ,
301+ expected : "X2oDj86zGjCRCy6vd8Cca4FDStMeFAHWc7SUUygPCCfYf9sHh" ,
302+ },
303+ {
304+ name : "endpoint without blockchain ID" ,
305+ endpoint : "http://127.0.0.1:9650/ext/info" ,
306+ expected : "" ,
307+ },
308+ {
309+ name : "endpoint with no path after blockchain ID" ,
310+ endpoint : "http://127.0.0.1:9650/ext/bc/someblockchainid" ,
311+ expected : "" ,
312+ },
313+ {
314+ name : "empty endpoint" ,
315+ endpoint : "" ,
316+ expected : "" ,
317+ },
318+ }
319+
320+ for _ , tt := range tests {
321+ t .Run (tt .name , func (t * testing.T ) {
322+ require := require .New (t )
323+ result := extractBlockchainIDFromEndpoint (tt .endpoint )
324+ require .Equal (tt .expected , result )
325+ })
326+ }
327+ }
328+
329+ func TestFilterOutdatedBlockchainEndpoints (t * testing.T ) {
330+ currentBlockchainID := "X2oDj86zGjCRCy6vd8Cca4FDStMeFAHWc7SUUygPCCfYf9sHh"
331+ oldBlockchainID := "2ATnxvq9GwPrEPHyULDJqpanFNJecvzSGZ3w5jMzfZAWSmXC6u"
332+
333+ tests := []struct {
334+ name string
335+ endpoints []string
336+ currentBlockchainID string
337+ expected []string
338+ }{
339+ {
340+ name : "filter out old blockchain ID endpoints" ,
341+ endpoints : []string {
342+ "http://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/rpc" ,
343+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
344+ },
345+ currentBlockchainID : currentBlockchainID ,
346+ expected : []string {
347+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
348+ },
349+ },
350+ {
351+ name : "preserve endpoints without blockchain ID" ,
352+ endpoints : []string {
353+ "http://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/rpc" ,
354+ "http://127.0.0.1:9650/ext/info" ,
355+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
356+ },
357+ currentBlockchainID : currentBlockchainID ,
358+ expected : []string {
359+ "http://127.0.0.1:9650/ext/info" ,
360+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
361+ },
362+ },
363+ {
364+ name : "keep all current blockchain endpoints" ,
365+ endpoints : []string {
366+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
367+ "http://127.0.0.1:60646/ext/bc/" + currentBlockchainID + "/rpc" ,
368+ },
369+ currentBlockchainID : currentBlockchainID ,
370+ expected : []string {
371+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
372+ "http://127.0.0.1:60646/ext/bc/" + currentBlockchainID + "/rpc" ,
373+ },
374+ },
375+ {
376+ name : "empty current blockchain ID returns all endpoints" ,
377+ endpoints : []string {
378+ "http://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/rpc" ,
379+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
380+ },
381+ currentBlockchainID : "" ,
382+ expected : []string {
383+ "http://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/rpc" ,
384+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
385+ },
386+ },
387+ {
388+ name : "empty endpoints list" ,
389+ endpoints : []string {},
390+ currentBlockchainID : currentBlockchainID ,
391+ expected : []string {},
392+ },
393+ {
394+ name : "mixed WS and RPC endpoints" ,
395+ endpoints : []string {
396+ "ws://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/ws" ,
397+ "http://127.0.0.1:55067/ext/bc/" + oldBlockchainID + "/rpc" ,
398+ "ws://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/ws" ,
399+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
400+ },
401+ currentBlockchainID : currentBlockchainID ,
402+ expected : []string {
403+ "ws://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/ws" ,
404+ "http://127.0.0.1:60645/ext/bc/" + currentBlockchainID + "/rpc" ,
405+ },
406+ },
407+ }
408+
409+ for _ , tt := range tests {
410+ t .Run (tt .name , func (t * testing.T ) {
411+ require := require .New (t )
412+ result := filterOutdatedBlockchainEndpoints (tt .endpoints , tt .currentBlockchainID )
413+ require .ElementsMatch (tt .expected , result )
414+ })
415+ }
416+ }
0 commit comments