@@ -28,19 +28,76 @@ import (
2828)
2929
3030func TestWaspAuth (t * testing.T ) {
31- t .Skip ("TODO: fix test" )
3231 w := newWaspCLITest (t , waspClusterOpts {
3332 modifyConfig : func (nodeIndex int , configParams cluster.WaspConfigParams ) cluster.WaspConfigParams {
3433 configParams .AuthScheme = "jwt"
3534 return configParams
3635 },
3736 })
38- _ , err := w .Run ("chain" , "list" , "--node=0" , "--node=0 " )
37+ _ , err := w .Run ("chain" , "info " )
3938 require .Error (t , err )
40- out := w .MustRun ("auth" , "login" , "--node=0" , "-u=wasp" , "-p=wasp" )
41- require .Equal (t , "Successfully authenticated" , out [1 ])
42- out = w .MustRun ("chain" , "list" , "--node=0" , "--node=0" )
43- require .Contains (t , out [0 ], "Total 0 chain(s)" )
39+
40+ t .Run ("table format output" , func (t * testing.T ) {
41+ //t.Skip()
42+ out := w .MustRun ("auth" , "login" , "--node=0" , "-u=wasp" , "-p=wasp" )
43+ // Check for table output format with SUCCESS status
44+ found := false
45+ for _ , line := range out {
46+ if strings .Contains (line , "success" ) && strings .Contains (line , "wasp" ) {
47+ found = true
48+ break
49+ }
50+ // Check for the table format
51+ if strings .Contains (line , "| success" ) {
52+ found = true
53+ break
54+ }
55+ }
56+ require .True (t , found , "Expected to find SUCCESS status in table output, got: %v" , out )
57+ })
58+
59+ t .Run ("json format output" , func (t * testing.T ) {
60+ out := w .MustRun ("auth" , "login" , "--node=0" , "-u=wasp" , "-p=wasp" , "--json" )
61+
62+ // Join all output lines to get the complete JSON
63+ jsonOutput := strings .Join (out , "" )
64+
65+ // Parse the JSON output to verify it's valid JSON
66+ var authResult map [string ]interface {}
67+ err := json .Unmarshal ([]byte (jsonOutput ), & authResult )
68+ require .NoError (t , err , "Expected valid JSON output, got: %v" , jsonOutput )
69+
70+ // Verify the standardized JSON structure contains required top-level fields
71+ require .Contains (t , authResult , "type" , "JSON output should contain 'type' field" )
72+ require .Contains (t , authResult , "status" , "JSON output should contain 'status' field" )
73+ require .Contains (t , authResult , "timestamp" , "JSON output should contain 'timestamp' field" )
74+ require .Contains (t , authResult , "data" , "JSON output should contain 'data' field" )
75+
76+ // Verify top-level field values
77+ require .Equal (t , "auth" , authResult ["type" ], "Expected type to be 'auth'" )
78+ require .Equal (t , "success" , authResult ["status" ], "Expected status to be 'success'" )
79+ require .NotEmpty (t , authResult ["timestamp" ], "Timestamp should not be empty" )
80+
81+ // Verify the data structure contains auth-specific fields
82+ data , ok := authResult ["data" ].(map [string ]interface {})
83+ require .True (t , ok , "Data field should be an object" )
84+ require .Contains (t , data , "node" , "Auth data should contain 'node' field" )
85+ require .Contains (t , data , "username" , "Auth data should contain 'username' field" )
86+
87+ // Verify the auth data values are correct
88+ require .Equal (t , "0" , data ["node" ], "Expected node to be '0'" )
89+ require .Equal (t , "wasp" , data ["username" ], "Expected username to be 'wasp'" )
90+
91+ // Check if the message field exists in data (it's optional)
92+ if message , exists := data ["message" ]; exists {
93+ require .NotEmpty (t , message , "Message field should not be empty if present" )
94+ }
95+
96+ // Validate timestamp format (should be ISO 8601)
97+ timestamp , ok := authResult ["timestamp" ].(string )
98+ require .True (t , ok , "Timestamp should be a string" )
99+ require .Regexp (t , `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?$` , timestamp , "Timestamp should be in ISO 8601 format" )
100+ })
44101}
45102
46103func TestZeroGasFee (t * testing.T ) {
@@ -57,7 +114,7 @@ func TestZeroGasFee(t *testing.T) {
57114 w .ActivateChainOnAllNodes (chainName , 0 )
58115
59116 w .MustRun ("wallet" , "address" )
60- alternativeAddress := getAddress (w .MustRun ("wallet" , "address" ))
117+ alternativeAddress := getAddressFromJSON (w .MustRun ("wallet" , "address" , "--json " ))
61118 w .MustRun ("chain" , "deposit" , alternativeAddress , "base|2000000" , "--node=0" )
62119 w .MustRun ("chain" , "balance" , alternativeAddress , "--node=0" )
63120 outs , err := w .Run ("chain" , "info" , "--node=0" , "--node=0" )
@@ -75,7 +132,7 @@ func TestZeroGasFee(t *testing.T) {
75132 })
76133
77134 t .Run ("deposit directly to EVM" , func (t * testing.T ) {
78- alternativeAddress := getAddress (w .MustRun ("wallet" , "address" , "--address-index=1" ))
135+ alternativeAddress := getAddressFromJSON (w .MustRun ("wallet" , "address" , "--address-index=1" , "--json " ))
79136 w .MustRun ("wallet" , "send-funds" , "-s" , alternativeAddress , "base|1000000" )
80137 outs := w .MustRun ("wallet" , "balance" , "--address-index=1" )
81138 _ , eth := newEthereumAccount ()
@@ -85,15 +142,59 @@ func TestZeroGasFee(t *testing.T) {
85142 })
86143}
87144
88- func checkL1Balance (t * testing.T , out []string , expected int ) {
145+ // checkL1BalanceJSON checks the balance using JSON output format
146+ func checkL1BalanceJSON (t * testing.T , out []string , expected int ) {
89147 t .Helper ()
90- r := regexp .MustCompile (`- 0x[0]{0,63}2+::iota::IOTA: (\d+)` ).FindStringSubmatch (strings .Join (out , "" ))
91- if r == nil {
92- panic ("couldn't check balance" )
148+
149+ // Join all output lines to get the complete JSON
150+ jsonOutput := strings .Join (out , "" )
151+
152+ // Parse the JSON output
153+ var balanceResult map [string ]interface {}
154+ err := json .Unmarshal ([]byte (jsonOutput ), & balanceResult )
155+ require .NoError (t , err , "Expected valid JSON output, got: %v" , jsonOutput )
156+
157+ // Verify the JSON structure
158+ require .Contains (t , balanceResult , "type" , "JSON output should contain 'type' field" )
159+ require .Contains (t , balanceResult , "status" , "JSON output should contain 'status' field" )
160+ require .Contains (t , balanceResult , "data" , "JSON output should contain 'data' field" )
161+
162+ // Verify type and status
163+ require .Equal (t , "wallet_balance" , balanceResult ["type" ], "Expected type to be 'wallet_balance'" )
164+ require .Equal (t , "success" , balanceResult ["status" ], "Expected status to be 'success'" )
165+
166+ // Extract the data section
167+ data , ok := balanceResult ["data" ].(map [string ]interface {})
168+ require .True (t , ok , "Data field should be an object" )
169+ require .Contains (t , data , "balances" , "Data should contain 'balances' field" )
170+
171+ // Extract balances array
172+ balances , ok := data ["balances" ].([]interface {})
173+ require .True (t , ok , "Balances should be an array" )
174+
175+ // Find the IOTA balance
176+ var iotaBalance uint64
177+ found := false
178+ for _ , balanceItem := range balances {
179+ balance , ok := balanceItem .(map [string ]interface {})
180+ require .True (t , ok , "Each balance item should be an object" )
181+ coinType , ok := balance ["coinType" ].(string )
182+ require .True (t , ok , "coinType should be a string" )
183+
184+ // Look for IOTA coin type (matches the regex pattern from original test)
185+ if strings .Contains (coinType , "::iota::IOTA" ) {
186+ totalBalanceStr , ok := balance ["totalBalance" ].(string )
187+ require .True (t , ok , "totalBalance should be a string" )
188+ totalBalance , err := strconv .ParseUint (totalBalanceStr , 10 , 64 )
189+ require .NoError (t , err , "totalBalance should be a valid number string" )
190+ iotaBalance = totalBalance
191+ found = true
192+ break
193+ }
93194 }
94- amount , err := strconv . Atoi ( r [ 1 ])
95- require .NoError (t , err )
96- require .EqualValues (t , expected , amount )
195+
196+ require .True (t , found , "IOTA balance not found in response" )
197+ require .EqualValues (t , expected , iotaBalance , "Expected IOTA balance to be %d, got %d" , expected , iotaBalance )
97198}
98199
99200func checkL2Balance (t * testing.T , out []string , expected int ) {
@@ -107,23 +208,54 @@ func checkL2Balance(t *testing.T, out []string, expected int) {
107208 require .EqualValues (t , expected , amount )
108209}
109210
110- func getAddress (out []string ) string {
111- r := regexp .MustCompile (`.*Address:\s+(\w*).*` ).FindStringSubmatch (strings .Join (out , "" ))
112- if r == nil {
113- panic ("couldn't get address" )
211+ // getAddressFromJSON extracts the address from JSON output
212+ func getAddressFromJSON (out []string ) string {
213+ // Join all output lines to get the complete JSON
214+ jsonOutput := strings .Join (out , "" )
215+
216+ // Parse the JSON output
217+ var addressResult map [string ]interface {}
218+ err := json .Unmarshal ([]byte (jsonOutput ), & addressResult )
219+ if err != nil {
220+ panic (fmt .Sprintf ("couldn't parse JSON output: %v" , err ))
221+ }
222+
223+ // Verify the JSON structure
224+ if addressResult ["type" ] != "wallet_address" {
225+ panic (fmt .Sprintf ("expected type 'wallet_address', got: %v" , addressResult ["type" ]))
226+ }
227+
228+ if addressResult ["status" ] != "success" {
229+ panic (fmt .Sprintf ("expected status 'success', got: %v" , addressResult ["status" ]))
230+ }
231+
232+ // Extract the data section
233+ data , ok := addressResult ["data" ].(map [string ]interface {})
234+ if ! ok {
235+ panic ("data field should be an object" )
236+ }
237+
238+ // Extract the address
239+ address , ok := data ["address" ].(string )
240+ if ! ok || address == "" {
241+ panic ("address field should be a non-empty string" )
114242 }
115- return r [1 ]
243+
244+ return address
116245}
117246
118247func TestWaspCLISendFunds (t * testing.T ) {
119248 w := newWaspCLITest (t )
120249
121- alternativeAddress := getAddress (w .MustRun ("wallet" , "address" , "--address-index=1" ))
250+ alternativeAddress := getAddressFromJSON (w .MustRun ("wallet" , "address" , "--address-index=1" , "--json " ))
122251
123252 w .MustRun ("wallet" , "request-funds" )
124253 w .MustRun ("wallet" , "send-funds" , alternativeAddress , "base|1000" )
125- outs := w .MustRun ("wallet" , "balance" , "--address-index=1" )
126- checkL1Balance (t , outs , 1000 )
254+
255+ outs := w .MustRun ("wallet" , "balance" , "--address-index=1" , "--json" )
256+ fmt .Println (strings .Join (outs , "" ))
257+ checkL1BalanceJSON (t , outs , 1000 )
258+
127259}
128260
129261func TestWaspCLIDeposit (t * testing.T ) {
@@ -139,7 +271,7 @@ func TestWaspCLIDeposit(t *testing.T) {
139271
140272 // fund an alternative address to deposit from (so we can test the fees,
141273 // since --address-index=0 is the chain admin / default payoutAddress)
142- alternativeAddress := getAddress (w .MustRun ("wallet" , "address" , "--address-index=1" ))
274+ alternativeAddress := getAddressFromJSON (w .MustRun ("wallet" , "address" , "--address-index=1" , "--json " ))
143275 w .MustRun ("wallet" , "send-funds" , "-s" , alternativeAddress , "base|10000000" , "--address-index=1" )
144276
145277 outs = w .MustRun ("wallet" , "balance" )
@@ -382,10 +514,16 @@ func TestWaspCLITrustListImport(t *testing.T) {
382514
383515 // set cluster2/node0 to trust all nodes from cluster 1
384516 for _ , nodeIndex := range w .Cluster .Config .AllNodes () {
385- peeringInfoOutput := w .MustRun ("peering" , "info" , fmt .Sprintf ("--node=%d" , nodeIndex ))
386- pubKey := regexp .MustCompile (`PubKey:\s+([[:alnum:]]+)$` ).FindStringSubmatch (peeringInfoOutput [0 ])[1 ]
387- peeringURL := regexp .MustCompile (`PeeringURL:\s+(.+)$` ).FindStringSubmatch (peeringInfoOutput [1 ])[1 ]
388- w2 .MustRun ("peering" , "trust" , fmt .Sprintf ("x%d" , nodeIndex ), pubKey , peeringURL , "--node=0" )
517+ peeringInfoOutput := w .MustRun ("peering" , "info" , fmt .Sprintf ("--node=%d" , nodeIndex ), "--json" )
518+ require .Len (t , peeringInfoOutput , 1 , "Expected single line of JSON output" )
519+
520+ var peeringInfo struct {
521+ PubKey string `json:"pubKey"`
522+ PeeringURL string `json:"peeringURL"`
523+ }
524+ require .NoError (t , json .Unmarshal ([]byte (peeringInfoOutput [0 ]), & peeringInfo ))
525+
526+ w2 .MustRun ("peering" , "trust" , fmt .Sprintf ("x%d" , nodeIndex ), peeringInfo .PubKey , peeringInfo .PeeringURL , "--node=0" )
389527 }
390528
391529 // import the trust from cluster2/node0 to cluster2/node1
@@ -428,8 +566,15 @@ func TestWaspCLICantPeerWithSelf(t *testing.T) {
428566 nNodes : 1 ,
429567 })
430568
431- peeringInfoOutput := w .MustRun ("peering" , "info" )
432- pubKey := regexp .MustCompile (`PubKey:\s+([[:alnum:]]+)$` ).FindStringSubmatch (peeringInfoOutput [0 ])[1 ]
569+ peeringInfoOutput := w .MustRun ("peering" , "info" , "--json" )
570+ require .Len (t , peeringInfoOutput , 1 , "Expected single line of JSON output" )
571+
572+ var peeringInfo struct {
573+ PubKey string `json:"pubKey"`
574+ PeeringURL string `json:"peeringURL"`
575+ }
576+ require .NoError (t , json .Unmarshal ([]byte (peeringInfoOutput [0 ]), & peeringInfo ))
577+ pubKey := peeringInfo .PubKey
433578
434579 require .Panics (
435580 t ,
0 commit comments