github.com/ethersphere/bee/v2@v2.2.0/pkg/settlement/swap/headers/utilities_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package swap_test
     6  
     7  import (
     8  	"math/big"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/p2p"
    13  	swap "github.com/ethersphere/bee/v2/pkg/settlement/swap/headers"
    14  )
    15  
    16  func TestParseSettlementResponseHeaders(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	headers := p2p.Headers{
    20  		swap.ExchangeRateFieldName: []byte{10},
    21  		swap.DeductionFieldName:    []byte{20},
    22  	}
    23  
    24  	exchange, deduction, err := swap.ParseSettlementResponseHeaders(headers)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  
    29  	if exchange.Cmp(big.NewInt(10)) != 0 {
    30  		t.Fatalf("Exchange rate mismatch, got %v, want %v", exchange, 10)
    31  	}
    32  
    33  	if deduction.Cmp(big.NewInt(20)) != 0 {
    34  		t.Fatalf("Deduction mismatch, got %v, want %v", deduction, 20)
    35  	}
    36  }
    37  
    38  func TestMakeSettlementHeaders(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	makeHeaders := swap.MakeSettlementHeaders(big.NewInt(906000), big.NewInt(5348))
    42  
    43  	expectedHeaders := p2p.Headers{
    44  		swap.ExchangeRateFieldName: []byte{13, 211, 16},
    45  		swap.DeductionFieldName:    []byte{20, 228},
    46  	}
    47  
    48  	if !reflect.DeepEqual(makeHeaders, expectedHeaders) {
    49  		t.Fatalf("Made headers not as expected, got %+v, want %+v", makeHeaders, expectedHeaders)
    50  	}
    51  }
    52  
    53  func TestParseExchangeHeader(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	toReadHeaders := p2p.Headers{
    57  		swap.ExchangeRateFieldName: []byte{13, 211, 16},
    58  	}
    59  
    60  	parsedExchange, err := swap.ParseExchangeHeader(toReadHeaders)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if parsedExchange.Cmp(big.NewInt(906000)) != 0 {
    66  		t.Fatalf("Allowance mismatch, got %v, want %v", parsedExchange, big.NewInt(906000))
    67  	}
    68  
    69  }
    70  
    71  func TestParseDeductionHeader(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	toReadHeaders := p2p.Headers{
    75  		swap.DeductionFieldName: []byte{20, 228},
    76  	}
    77  
    78  	parsedDeduction, err := swap.ParseDeductionHeader(toReadHeaders)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	if parsedDeduction.Cmp(big.NewInt(5348)) != 0 {
    84  		t.Fatalf("Allowance mismatch, got %v, want %v", parsedDeduction, big.NewInt(5348))
    85  	}
    86  
    87  }