github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/channels/errors_test.go (about)

     1  package channels
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  )
    11  
    12  // TestInvalidTopicErrRoundTrip ensures correct error formatting for InvalidTopicErr.
    13  func TestInvalidTopicErrRoundTrip(t *testing.T) {
    14  	topic := Topic("invalid-topic")
    15  	wrapErr := fmt.Errorf("this err should be wrapped with topic to add context")
    16  	err := NewInvalidTopicErr(topic, wrapErr)
    17  
    18  	// tests the error message formatting.
    19  	expectedErrMsg := fmt.Errorf("invalid topic %s: %w", topic, wrapErr).Error()
    20  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
    21  
    22  	// tests the IsErrActiveClusterIDsNotSet function.
    23  	assert.True(t, IsInvalidTopicErr(err), "IsInvalidTopicErr should return true for InvalidTopicErr error")
    24  
    25  	// test IsErrActiveClusterIDsNotSet with a different error type.
    26  	dummyErr := fmt.Errorf("dummy error")
    27  	assert.False(t, IsInvalidTopicErr(dummyErr), "IsInvalidTopicErr should return false for non-IsInvalidTopicErr error")
    28  }
    29  
    30  // TestUnknownClusterIDErrRoundTrip ensures correct error formatting for UnknownClusterIDErr.
    31  func TestUnknownClusterIDErrRoundTrip(t *testing.T) {
    32  	clusterId := flow.ChainID("cluster-id")
    33  	activeClusterIds := flow.ChainIDList{"active", "cluster", "ids"}
    34  	err := NewUnknownClusterIdErr(clusterId, activeClusterIds)
    35  
    36  	// tests the error message formatting.
    37  	expectedErrMsg := fmt.Errorf("cluster ID %s not found in active cluster IDs list %s", clusterId, activeClusterIds).Error()
    38  	assert.Equal(t, expectedErrMsg, err.Error(), "the error message should be correctly formatted")
    39  
    40  	// tests the IsErrActiveClusterIDsNotSet function.
    41  	assert.True(t, IsUnknownClusterIDErr(err), "IsUnknownClusterIDErr should return true for UnknownClusterIDErr error")
    42  
    43  	// test IsErrActiveClusterIDsNotSet with a different error type.
    44  	dummyErr := fmt.Errorf("dummy error")
    45  	assert.False(t, IsUnknownClusterIDErr(dummyErr), "IsUnknownClusterIDErr should return false for non-UnknownClusterIDErr error")
    46  }