github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/wallet/parser_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  type candidateTestCase struct {
    10  	in  string
    11  	out []ChatTxCandidate
    12  }
    13  
    14  func TestFindCandidates(t *testing.T) {
    15  	alfa := "alfa"
    16  	bravo := "bravo"
    17  	charlie := "charlie"
    18  	alfaTx := ChatTxCandidate{Amount: "124.005", CurrencyCode: "XLM", Username: &alfa, Full: "+124.005XLM@alfa"}
    19  	bravoTx := ChatTxCandidate{Amount: ".005", CurrencyCode: "USD", Username: &bravo, Full: "+.005USD@bravo"}
    20  	charlieTx := ChatTxCandidate{Amount: "5.", CurrencyCode: "HKD", Username: &charlie, Full: "+5.HKD@charlie"}
    21  	anonTx := ChatTxCandidate{Amount: "25", CurrencyCode: "EUR", Username: nil, Full: "+25EUR"}
    22  	testCases := []candidateTestCase{
    23  		{"+124.005XLM@alfa", []ChatTxCandidate{alfaTx}},
    24  		{"   +124.005XLM@alfa   ", []ChatTxCandidate{alfaTx}},
    25  		{"   +124.005XLM@alfa", []ChatTxCandidate{alfaTx}},
    26  		{"+124.005XLM@alfa      ", []ChatTxCandidate{alfaTx}},
    27  		{"   `+124.`005XLM@alfa   ", []ChatTxCandidate{}},
    28  		{"   `+124.005XLM@alfa`   ", []ChatTxCandidate{}},
    29  		{"   ```   `+124.005XLM@alfa```   ", []ChatTxCandidate{}},
    30  		{"   ```   `+124.005XLM@alfa``   ", []ChatTxCandidate{}},
    31  		{"   ```   ` +124.005XLM@alfa``   ", []ChatTxCandidate{}},
    32  		{"   ```   `+124.005XLM@alfa``   ", []ChatTxCandidate{}},
    33  		{"   ```   ` +124.005XLM@alfa``   ", []ChatTxCandidate{}},
    34  		{"  +124.005XLM@alfa\n+.005USD@bravo   \n+5.HKD@charlie", []ChatTxCandidate{alfaTx, bravoTx, charlieTx}},
    35  		{"  +124.005XLM@alfa\n-.005USD@bravo   \n 5.HKD@charlie", []ChatTxCandidate{alfaTx}},
    36  		{"  +124.005X@alfa   ", []ChatTxCandidate{}},
    37  		{"  +124.005XLM@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa   ", []ChatTxCandidate{}},
    38  		{"  +.XLM@alfa   ", []ChatTxCandidate{}},
    39  		{"  +XLM@alfa   ", []ChatTxCandidate{}},
    40  		{"  +XLM+XLM@alfa   ", []ChatTxCandidate{}},
    41  		{"  +124.005XLM@alfa+.005USD@bravo   ", []ChatTxCandidate{}},
    42  		{"  +124.005XLM@alfa +.005USD@bravo   ", []ChatTxCandidate{alfaTx, bravoTx}},
    43  		{"  +124.005XLM@alfa +.005USD@bravo   ", []ChatTxCandidate{alfaTx, bravoTx}},
    44  		{"+124.005XLM@alfa +.005USD@bravo", []ChatTxCandidate{alfaTx, bravoTx}},
    45  		{"(+124.005XLM@alfa, +.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
    46  		{"(+124.005XLM@alfa,+.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
    47  		{"(+124.005XLM@alfa$+.005USD@bravo)", []ChatTxCandidate{}},
    48  		{"(+124.005XLM@alfa@+.005USD@bravo)", []ChatTxCandidate{}},
    49  		{"\t(+124.005XLM@alfa, +.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
    50  		{"\t(+124.005XLM@alfa,+.005USD@bravo)", []ChatTxCandidate{alfaTx, bravoTx}},
    51  		{"\t(+124.005XLM@alfa$+.005USD@bravo)", []ChatTxCandidate{}},
    52  		{"\t(+124.005XLM@alfa@+.005USD@bravo)", []ChatTxCandidate{}},
    53  		{"{+124.005XLM@alfa,+.005USD@bravo}", []ChatTxCandidate{alfaTx, bravoTx}},
    54  		{" {+124.005XLM@alfa,+.005USD@bravo}", []ChatTxCandidate{alfaTx, bravoTx}},
    55  		{"[+124.005XLM@alfa,+.005USD@bravo]", []ChatTxCandidate{alfaTx, bravoTx}},
    56  		{" [+124.005XLM@alfa,+.005USD@bravo]", []ChatTxCandidate{alfaTx, bravoTx}},
    57  		{"+124.005XLM@alfa +124.005XLM@alfa", []ChatTxCandidate{alfaTx, alfaTx}},
    58  		{"+124.005XLM@alfa +124.005XLM@alfa +124.005XLM@alfa", []ChatTxCandidate{alfaTx, alfaTx, alfaTx}},
    59  		{"+1xlm@patrick+1xlm@mikem", []ChatTxCandidate{}},
    60  
    61  		// direct message txs
    62  		{"thanks friend, +25eur!", []ChatTxCandidate{anonTx}},
    63  		{"thanks friend, +25eur for you!", []ChatTxCandidate{anonTx}},
    64  		{"thanks friend,+25eur for you!", []ChatTxCandidate{anonTx}},
    65  		{"thanks friend,+25eur@ for you!", []ChatTxCandidate{}},
    66  		{"thanks friend,+25eur", []ChatTxCandidate{anonTx}},
    67  		{"thanks friend,+25eur\nnewline", []ChatTxCandidate{anonTx}},
    68  		{"thanks friend,+25eur\ttabbed", []ChatTxCandidate{anonTx}},
    69  		{"thanks friend (+25eur)\ttabbed", []ChatTxCandidate{anonTx}},
    70  		{"+25eur", []ChatTxCandidate{anonTx}},
    71  		{"+25eur  +25eur 1", []ChatTxCandidate{anonTx, anonTx}},
    72  		{"+25eur +25eur 2", []ChatTxCandidate{anonTx, anonTx}},
    73  		{"+25eur +25eur +25eur", []ChatTxCandidate{anonTx, anonTx, anonTx}},
    74  		{"+25eur +25eur +25eur +25eur", []ChatTxCandidate{anonTx, anonTx, anonTx, anonTx}},
    75  		{"+25eur +25eur +25eur +25eur +25eur", []ChatTxCandidate{anonTx, anonTx, anonTx, anonTx, anonTx}},
    76  		{"+1xlm+1xlm", []ChatTxCandidate{}},
    77  
    78  		// some extra checks
    79  		{"+0.05XLM", []ChatTxCandidate{{Amount: "0.05", CurrencyCode: "XLM", Username: nil, Full: "+0.05XLM"}}},
    80  		{"+.05XLM", []ChatTxCandidate{{Amount: ".05", CurrencyCode: "XLM", Username: nil, Full: "+.05XLM"}}},
    81  		{"+0.5xlm", []ChatTxCandidate{{Amount: "0.5", CurrencyCode: "XLM", Username: nil, Full: "+0.5XLM"}}},
    82  		{"+0xlm", []ChatTxCandidate{}},
    83  
    84  		// misc rejected
    85  		{"hello `in my code +0.5XLM blah` ok?", []ChatTxCandidate{}},
    86  		{"hello ```in my code +0.5XLM blah``` ok?", []ChatTxCandidate{}},
    87  		{"> quoted pay me +0.5XLM ok?", []ChatTxCandidate{}},
    88  		{"", []ChatTxCandidate{}},
    89  		{"+..1XLM", []ChatTxCandidate{}},
    90  		{"02y8fasjof+10XLMsireu1-39ijqeri", []ChatTxCandidate{}},
    91  		{`BPJ+5lV/+2d`, []ChatTxCandidate{}},
    92  		{`BPJ+5LV/+2d`, []ChatTxCandidate{}},
    93  		{`BPJ+5lv/+2d`, []ChatTxCandidate{}},
    94  		{`BPJ+5lvl/+2d`, []ChatTxCandidate{}},
    95  		{`BPJ+5LVL@bob/+2d`, []ChatTxCandidate{}},
    96  		{` _+5LVL-+4LVL/+3LVL-+2LVL#+1LVL\\+6LVL`, []ChatTxCandidate{}},
    97  		{`g+10ish`, []ChatTxCandidate{}},
    98  	}
    99  
   100  	for i, testCase := range testCases {
   101  		ret := FindChatTxCandidates(testCase.in)
   102  		// blow away positions, we test them in decorate tests
   103  		filtered := []ChatTxCandidate{}
   104  		for _, r := range ret {
   105  			r.Position = nil
   106  			filtered = append(filtered, r)
   107  		}
   108  		require.Equal(t, testCase.out, filtered, "unit %v %q", i, testCase.in)
   109  	}
   110  
   111  	require.True(t, true)
   112  }