github.com/sunriselayer/sunrise-da@v0.13.1-sr3/cmd/sunrise-da/cmd_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/sunriselayer/sunrise-da/header"
    13  )
    14  
    15  func TestCompletionHelpString(t *testing.T) {
    16  	type TestFields struct {
    17  		NoInputOneOutput        func(context.Context) (*header.ExtendedHeader, error)
    18  		TwoInputsOneOutputArray func(
    19  			context.Context,
    20  			*header.ExtendedHeader,
    21  			uint64,
    22  		) ([]*header.ExtendedHeader, error)
    23  		OneInputOneOutput  func(context.Context, uint64) (*header.ExtendedHeader, error)
    24  		NoInputsNoOutputs  func(ctx context.Context) error
    25  		NoInputsChanOutput func(ctx context.Context) (<-chan *header.ExtendedHeader, error)
    26  	}
    27  	testOutputs := []string{
    28  		"() -> (*header.ExtendedHeader)",
    29  		"(*header.ExtendedHeader, uint64) -> ([]*header.ExtendedHeader)",
    30  		"(uint64) -> (*header.ExtendedHeader)",
    31  		"() -> ()",
    32  		"() -> (<-chan *header.ExtendedHeader)",
    33  	}
    34  	methods := reflect.VisibleFields(reflect.TypeOf(TestFields{}))
    35  	for i, method := range methods {
    36  		require.Equal(t, testOutputs[i], parseSignatureForHelpString(method))
    37  	}
    38  }
    39  
    40  func TestLight(t *testing.T) {
    41  	// Run the tests in a temporary directory
    42  	tmpDir := t.TempDir()
    43  	testDir, err := os.Getwd()
    44  	require.NoError(t, err, "error getting the current working directory")
    45  	err = os.Chdir(tmpDir)
    46  	require.NoError(t, err, "error changing to the temporary test directory")
    47  
    48  	t.Run("init", func(t *testing.T) {
    49  		output := &bytes.Buffer{}
    50  		rootCmd.SetOut(output)
    51  		rootCmd.SetArgs([]string{
    52  			"bridge",
    53  			"--node.store", ".sunrise-light",
    54  			"init",
    55  		})
    56  		err := rootCmd.ExecuteContext(context.Background())
    57  		require.NoError(t, err)
    58  	})
    59  
    60  	t.Cleanup(func() {
    61  		if err := os.Chdir(testDir); err != nil {
    62  			t.Error("error resetting:", err)
    63  		}
    64  	})
    65  
    66  	// TODO @jbowen93: Commented out until a dry-run option can be implemented
    67  	/*
    68  			t.Run("start", func(t *testing.T) {
    69  				output := &bytes.Buffer{}
    70  				rootCmd.SetOut(output)
    71  				rootCmd.SetArgs([]string{
    72  					"light",
    73  					"--node.store", ".celestia-light",
    74  					"start",
    75  					"--headers.trusted-peer",
    76  		            "/ip4/192.167.10.6/tcp/2121/p2p/12D3KooWL8z3KARAYJcmExhDsGwKbjChKeGaJpFPENyADdxmEHzw",
    77  		            "--headers.trusted-hash",
    78  		            "54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
    79  				})
    80  				err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
    81  				require.NoError(t, err)
    82  			})
    83  	*/
    84  }
    85  
    86  func TestBridge(t *testing.T) {
    87  	// Run the tests in a temporary directory
    88  	tmpDir := t.TempDir()
    89  	testDir, err := os.Getwd()
    90  	require.NoError(t, err, "error getting the current working directory")
    91  	err = os.Chdir(tmpDir)
    92  	require.NoError(t, err, "error changing to the temporary test directory")
    93  
    94  	t.Run("init", func(t *testing.T) {
    95  		output := &bytes.Buffer{}
    96  		rootCmd.SetOut(output)
    97  		rootCmd.SetArgs([]string{
    98  			"bridge",
    99  			"--node.store", ".sunrise-bridge",
   100  			"init",
   101  		})
   102  		err := rootCmd.ExecuteContext(context.Background())
   103  		require.NoError(t, err)
   104  	})
   105  
   106  	t.Cleanup(func() {
   107  		if err := os.Chdir(testDir); err != nil {
   108  			t.Error("error resetting:", err)
   109  		}
   110  	})
   111  
   112  	// TODO @jbowen93: Commented out until a dry-run option can be implemented
   113  	/*
   114  			t.Run("start", func(t *testing.T) {
   115  				output := &bytes.Buffer{}
   116  				rootCmd.SetOut(output)
   117  				rootCmd.SetArgs([]string{
   118  					"bridge",
   119  					"--node.store", ".celestia-bridge",
   120  					"start",
   121  					"--core.remote",
   122  		            "tcp://192.167.10.2:26657",
   123  					"--headers.trusted-hash",
   124  					"54A8B66D2BEF13850D67C8D474E196BD7485FE5A79989E31B17169371B0A9C96",
   125  				})
   126  				err := rootCmd.ExecuteContext(cmdnode.WithEnv(context.Background()))
   127  				require.NoError(t, err)
   128  			})
   129  	*/
   130  }
   131  
   132  func parseSignatureForHelpString(methodSig reflect.StructField) string {
   133  	simplifiedSignature := "("
   134  	in, out := methodSig.Type.NumIn(), methodSig.Type.NumOut()
   135  	for i := 1; i < in; i++ {
   136  		simplifiedSignature += methodSig.Type.In(i).String()
   137  		if i != in-1 {
   138  			simplifiedSignature += ", "
   139  		}
   140  	}
   141  	simplifiedSignature += ") -> ("
   142  	for i := 0; i < out-1; i++ {
   143  		simplifiedSignature += methodSig.Type.Out(i).String()
   144  		if i != out-2 {
   145  			simplifiedSignature += ", "
   146  		}
   147  	}
   148  	simplifiedSignature += ")"
   149  	return simplifiedSignature
   150  }