Skip to content

Commit 92e3a3f

Browse files
committed
chore: clippy on tests
1 parent fb6f98c commit 92e3a3f

File tree

1 file changed

+115
-119
lines changed

1 file changed

+115
-119
lines changed

crates/browser-wallet/src/tests.rs

Lines changed: 115 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,120 @@
11
#[cfg(test)]
2-
mod tests {
3-
use crate::{
4-
state::BrowserWalletState, BrowserTransaction, TransactionResponse, WalletConnection,
5-
};
6-
use alloy_primitives::{address, B256, U256};
7-
use alloy_rpc_types::TransactionRequest;
8-
9-
#[test]
10-
fn test_browser_transaction_serialization() {
11-
let tx = BrowserTransaction {
12-
id: "test-123".to_string(),
13-
request: TransactionRequest {
14-
from: Some(address!("0000000000000000000000000000000000000001")),
15-
to: Some(address!("0000000000000000000000000000000000000002").into()),
16-
value: Some(U256::from(1000)),
17-
..Default::default()
18-
},
19-
};
20-
21-
let json = serde_json::to_string_pretty(&tx).unwrap();
22-
23-
let deserialized: BrowserTransaction = serde_json::from_str(&json).unwrap();
24-
25-
assert_eq!(tx.id, deserialized.id);
26-
assert_eq!(tx.request.from, deserialized.request.from);
27-
assert_eq!(tx.request.to, deserialized.request.to);
28-
assert_eq!(tx.request.value, deserialized.request.value);
29-
}
30-
31-
#[test]
32-
fn test_wallet_connection() {
33-
let connection = WalletConnection {
34-
address: address!("0000000000000000000000000000000000000001"),
35-
chain_id: 1,
36-
wallet_name: Some("MetaMask".to_string()),
37-
};
38-
39-
let json = serde_json::to_string(&connection).unwrap();
40-
let deserialized: WalletConnection = serde_json::from_str(&json).unwrap();
41-
42-
assert_eq!(connection.address, deserialized.address);
43-
assert_eq!(connection.chain_id, deserialized.chain_id);
44-
assert_eq!(connection.wallet_name, deserialized.wallet_name);
45-
}
46-
47-
#[test]
48-
fn test_transaction_hex_serialization() {
49-
use alloy_primitives::U256;
50-
51-
let mut tx = TransactionRequest::default();
52-
tx.from = Some(address!("0000000000000000000000000000000000000001"));
53-
tx.to = Some(address!("0000000000000000000000000000000000000002").into());
54-
tx.value = Some(U256::from(1_000_000_000_000_000_000u64)); // 1 ETH
55-
tx.chain_id = Some(31337);
56-
57-
let browser_tx = BrowserTransaction { id: "test-1".to_string(), request: tx };
58-
59-
let json = serde_json::to_string_pretty(&browser_tx).unwrap();
60-
61-
// Check that hex values are properly formatted
62-
assert!(json.contains("\"value\": \"0x"));
63-
assert!(json.contains("\"chainId\": \"0x"));
64-
65-
// Ensure no double hex encoding
66-
assert!(!json.contains("\"0x0x"));
67-
assert!(!json.contains("0x0x0x"));
68-
}
69-
70-
#[test]
71-
fn test_transaction_with_empty_data() {
72-
use alloy_primitives::U256;
73-
74-
let tx = TransactionRequest {
2+
use crate::{state::BrowserWalletState, BrowserTransaction, TransactionResponse, WalletConnection};
3+
#[cfg(test)]
4+
use alloy_primitives::{address, B256, U256};
5+
#[cfg(test)]
6+
use alloy_rpc_types::TransactionRequest;
7+
8+
#[test]
9+
fn test_browser_transaction_serialization() {
10+
let tx = BrowserTransaction {
11+
id: "test-123".to_string(),
12+
request: TransactionRequest {
7513
from: Some(address!("0000000000000000000000000000000000000001")),
7614
to: Some(address!("0000000000000000000000000000000000000002").into()),
77-
value: Some(U256::ZERO),
78-
chain_id: Some(31337),
15+
value: Some(U256::from(1000)),
7916
..Default::default()
80-
};
81-
// tx.data is None by default
82-
83-
let browser_tx = BrowserTransaction { id: "test-2".to_string(), request: tx };
84-
85-
let json = serde_json::to_string_pretty(&browser_tx).unwrap();
86-
87-
// Check that empty values are handled correctly
88-
assert!(json.contains("\"value\": \"0x0\""));
89-
// data should be absent if None
90-
assert!(!json.contains("\"data\"") || json.contains("\"data\": null"));
91-
}
92-
93-
#[tokio::test]
94-
async fn test_state_management() {
95-
let state = BrowserWalletState::new();
96-
97-
// Test wallet connection
98-
assert!(state.get_connected_address().is_none());
99-
100-
state.set_connected_address(Some("0x0000000000000000000000000000000000000001".to_string()));
101-
assert_eq!(
102-
state.get_connected_address(),
103-
Some("0x0000000000000000000000000000000000000001".to_string())
104-
);
105-
106-
// Test transaction queue
107-
let tx =
108-
BrowserTransaction { id: "tx-1".to_string(), request: TransactionRequest::default() };
109-
110-
state.add_transaction_request(tx);
111-
assert_eq!(state.get_pending_transaction().unwrap().id, "tx-1");
112-
113-
// Test response handling
114-
let response = TransactionResponse {
115-
id: "tx-1".to_string(),
116-
hash: Some(B256::default()),
117-
error: None,
118-
};
119-
120-
state.add_transaction_response(response);
121-
assert!(state.get_pending_transaction().is_none());
122-
assert!(state.get_transaction_response("tx-1").is_some());
123-
}
17+
},
18+
};
19+
20+
let json = serde_json::to_string_pretty(&tx).unwrap();
21+
22+
let deserialized: BrowserTransaction = serde_json::from_str(&json).unwrap();
23+
24+
assert_eq!(tx.id, deserialized.id);
25+
assert_eq!(tx.request.from, deserialized.request.from);
26+
assert_eq!(tx.request.to, deserialized.request.to);
27+
assert_eq!(tx.request.value, deserialized.request.value);
28+
}
29+
30+
#[test]
31+
fn test_wallet_connection() {
32+
let connection = WalletConnection {
33+
address: address!("0000000000000000000000000000000000000001"),
34+
chain_id: 1,
35+
wallet_name: Some("MetaMask".to_string()),
36+
};
37+
38+
let json = serde_json::to_string(&connection).unwrap();
39+
let deserialized: WalletConnection = serde_json::from_str(&json).unwrap();
40+
41+
assert_eq!(connection.address, deserialized.address);
42+
assert_eq!(connection.chain_id, deserialized.chain_id);
43+
assert_eq!(connection.wallet_name, deserialized.wallet_name);
44+
}
45+
46+
#[test]
47+
fn test_transaction_hex_serialization() {
48+
use alloy_primitives::U256;
49+
50+
let tx = TransactionRequest {
51+
from: Some(address!("0000000000000000000000000000000000000001")),
52+
to: Some(address!("0000000000000000000000000000000000000002").into()),
53+
value: Some(U256::from(1_000_000_000_000_000_000u64)), // 1 ETH
54+
chain_id: Some(31337),
55+
..Default::default()
56+
};
57+
58+
let browser_tx = BrowserTransaction { id: "test-1".to_string(), request: tx };
59+
60+
let json = serde_json::to_string_pretty(&browser_tx).unwrap();
61+
62+
// Check that hex values are properly formatted
63+
assert!(json.contains("\"value\": \"0x"));
64+
assert!(json.contains("\"chainId\": \"0x"));
65+
66+
// Ensure no double hex encoding
67+
assert!(!json.contains("\"0x0x"));
68+
assert!(!json.contains("0x0x0x"));
69+
}
70+
71+
#[test]
72+
fn test_transaction_with_empty_data() {
73+
use alloy_primitives::U256;
74+
75+
let tx = TransactionRequest {
76+
from: Some(address!("0000000000000000000000000000000000000001")),
77+
to: Some(address!("0000000000000000000000000000000000000002").into()),
78+
value: Some(U256::ZERO),
79+
chain_id: Some(31337),
80+
..Default::default()
81+
};
82+
// tx.data is None by default
83+
84+
let browser_tx = BrowserTransaction { id: "test-2".to_string(), request: tx };
85+
86+
let json = serde_json::to_string_pretty(&browser_tx).unwrap();
87+
88+
// Check that empty values are handled correctly
89+
assert!(json.contains("\"value\": \"0x0\""));
90+
// data should be absent if None
91+
assert!(!json.contains("\"data\"") || json.contains("\"data\": null"));
92+
}
93+
94+
#[tokio::test]
95+
async fn test_state_management() {
96+
let state = BrowserWalletState::new();
97+
98+
// Test wallet connection
99+
assert!(state.get_connected_address().is_none());
100+
101+
state.set_connected_address(Some("0x0000000000000000000000000000000000000001".to_string()));
102+
assert_eq!(
103+
state.get_connected_address(),
104+
Some("0x0000000000000000000000000000000000000001".to_string())
105+
);
106+
107+
// Test transaction queue
108+
let tx = BrowserTransaction { id: "tx-1".to_string(), request: TransactionRequest::default() };
109+
110+
state.add_transaction_request(tx);
111+
assert_eq!(state.get_pending_transaction().unwrap().id, "tx-1");
112+
113+
// Test response handling
114+
let response =
115+
TransactionResponse { id: "tx-1".to_string(), hash: Some(B256::default()), error: None };
116+
117+
state.add_transaction_response(response);
118+
assert!(state.get_pending_transaction().is_none());
119+
assert!(state.get_transaction_response("tx-1").is_some());
124120
}

0 commit comments

Comments
 (0)