gitlab.com/SkynetLabs/skyd@v1.6.9/cmd/skyc/rentercmd_helpers_test.go (about)

     1  package main
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/andreyvit/diff"
     8  	"gitlab.com/NebulousLabs/errors"
     9  	"gitlab.com/SkynetLabs/skyd/skymodules"
    10  	"go.sia.tech/siad/types"
    11  )
    12  
    13  // TestParseLSArgs probes the parseLSArgs function
    14  func TestParseLSArgs(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	// Define Tests
    18  	var tests = []struct {
    19  		args []string
    20  		sp   skymodules.SiaPath
    21  		err  error
    22  	}{
    23  		// Valid Cases
    24  		{nil, skymodules.RootSiaPath(), nil},
    25  		{[]string{}, skymodules.RootSiaPath(), nil},
    26  		{[]string{""}, skymodules.RootSiaPath(), nil},
    27  		{[]string{"."}, skymodules.RootSiaPath(), nil},
    28  		{[]string{"/"}, skymodules.RootSiaPath(), nil},
    29  		{[]string{"path"}, skymodules.SiaPath{Path: "path"}, nil},
    30  
    31  		// Invalid Cases
    32  		{[]string{"path", "extra"}, skymodules.SiaPath{}, errIncorrectNumArgs},
    33  		{[]string{"path", "extra", "extra"}, skymodules.SiaPath{}, errIncorrectNumArgs},
    34  		{[]string{"...//////badd....////path"}, skymodules.SiaPath{}, skymodules.ErrInvalidSiaPath},
    35  	}
    36  	// Execute Tests
    37  	for _, test := range tests {
    38  		sp, err := parseLSArgs(test.args)
    39  		if !sp.Equals(test.sp) {
    40  			t.Log("Expected:", test.sp)
    41  			t.Log("Actual:", sp)
    42  			t.Error("unexpected siapath")
    43  		}
    44  		if !errors.Contains(err, test.err) && err != test.err {
    45  			t.Log("Expected:", test.err)
    46  			t.Log("Actual:", err)
    47  			t.Error("unexpected error")
    48  		}
    49  	}
    50  }
    51  
    52  // TestCurrentPeriodSpending is a small unit test that verifies the output of
    53  // the currentperiodspending helper
    54  func TestCurrentPeriodSpending(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	fm := skymodules.FinancialMetrics{
    58  		ContractorSpending: skymodules.ContractorSpending{
    59  			DownloadSpending: types.SiacoinPrecision.Mul64(1),
    60  			Fees: skymodules.Fees{
    61  				ContractFees:    types.SiacoinPrecision.Mul64(2),
    62  				SiafundFees:     types.SiacoinPrecision.Mul64(3),
    63  				TransactionFees: types.SiacoinPrecision.Mul64(4),
    64  			},
    65  			FundAccountSpending: types.SiacoinPrecision.Mul64(5),
    66  			MaintenanceSpending: skymodules.MaintenanceSpending{
    67  				AccountBalanceCost:   types.SiacoinPrecision.Mul64(6),
    68  				FundAccountCost:      types.SiacoinPrecision.Mul64(7),
    69  				UpdatePriceTableCost: types.SiacoinPrecision.Mul64(8),
    70  			},
    71  			StorageSpending:            types.SiacoinPrecision.Mul64(9),
    72  			TotalAllocated:             types.SiacoinPrecision.Mul64(10),
    73  			UploadSpending:             types.SiacoinPrecision.Mul64(11),
    74  			Unspent:                    types.SiacoinPrecision.Mul64(12),
    75  			ContractSpendingDeprecated: types.SiacoinPrecision.Mul64(13),
    76  			WithheldFunds:              types.SiacoinPrecision.Mul64(14),
    77  			ReleaseBlock:               types.BlockHeight(1),
    78  			PreviousSpending:           types.SiacoinPrecision.Mul64(15),
    79  		},
    80  		EphemeralAccountSpending: []skymodules.EphemeralAccountSpending{
    81  			{
    82  				AccountSpending: skymodules.AccountSpending{
    83  					AccountBalanceCost:    types.SiacoinPrecision.Mul64(16),
    84  					DownloadsCost:         types.SiacoinPrecision.Mul64(17),
    85  					RegistryReadsCost:     types.SiacoinPrecision.Mul64(18),
    86  					RegistryWritesCost:    types.SiacoinPrecision.Mul64(19),
    87  					RepairDownloadsCost:   types.SiacoinPrecision.Mul64(20),
    88  					RepairUploadsCost:     types.SiacoinPrecision.Mul64(21),
    89  					SnapshotDownloadsCost: types.SiacoinPrecision.Mul64(22),
    90  					SnapshotUploadsCost:   types.SiacoinPrecision.Mul64(23),
    91  					SubscriptionsCost:     types.SiacoinPrecision.Mul64(24),
    92  					UpdatePriceTableCost:  types.SiacoinPrecision.Mul64(25),
    93  					UploadsCost:           types.SiacoinPrecision.Mul64(26),
    94  
    95  					Balance:      types.SiacoinPrecision.Mul64(27),
    96  					BalanceDrift: *big.NewInt(-28),
    97  					Residue:      types.SiacoinPrecision.Mul64(29),
    98  				},
    99  				HostKey: types.SiaPublicKey{},
   100  			},
   101  		},
   102  		SkynetFee: types.SiacoinPrecision.Mul64(28),
   103  	}
   104  
   105  	expected := `
   106      Spent Funds:               56 SC
   107        Storage:                 9 SC
   108        Upload:                  11 SC
   109        Download:                1 SC
   110        FundAccount:             5 SC (+29 SC residue)
   111          AccountBalanceCost:    16 SC
   112          Balance:               27 SC (-28 H drift)
   113          DownloadsCost:         17 SC
   114          MiscCost:              45 SC
   115          RegistryReadsCost:     18 SC
   116          RegistryWritesCost:    19 SC
   117          RepairsCost:           41 SC
   118          SubscriptionsCost:     24 SC
   119          UpdatePriceTableCost:  25 SC
   120          UploadsCost:           26 SC
   121          Drift:                 -282 SC
   122        Maintenance:             21 SC
   123          AccountBalanceCost:    6 SC
   124          FundAccountCost:       7 SC
   125          UpdatePriceTableCost:  8 SC
   126        Fees:                    9 SC
   127          ContractFees:          2 SC
   128          SiafundFees:           3 SC
   129          TransactionFees:       4 SC
   130      Unspent Funds:             12 SC
   131        Allocated:               0 H
   132        Unallocated:             12 SC
   133      Skynet Fee:                28 SC
   134  `
   135  	actual := currentperiodspending(fm, nil)
   136  	if actual != expected {
   137  		t.Fatal(actual, expected, diff.LineDiff(expected, actual))
   138  	}
   139  }