use wx_rust_common::bean::to_json::ToJson;
use wx_rust_common::bean::wx_access_token::WxAccessToken;
use wx_rust_common::bean::wx_access_token_entity::WxAccessTokenEntity;
use wx_rust_common::bean::wx_card_api_signature::WxCardApiSignature;
use wx_rust_common::bean::wx_jsapi_signature::WxJsapiSignature;
use wx_rust_common::error::wx_channel_error_msg_enum;
use wx_rust_common::error::wx_cp_error_msg_enum;
use wx_rust_common::error::wx_ma_error_msg_enum;
use wx_rust_common::error::wx_mp_error_msg_enum;
use wx_rust_common::error::wx_open_error_msg_enum;
use wx_rust_common::session::WxSessionManager;
#[test]
fn test_to_json_for_struct() {
#[derive(serde::Serialize)]
struct Example {
name: String,
value: i32,
}
let ex = Example {
name: "test".to_string(),
value: 42,
};
let json = ex.to_json();
assert!(json.contains("test"));
assert!(json.contains("42"));
}
#[test]
fn test_to_json_for_empty_struct() {
#[derive(serde::Serialize)]
struct Empty;
let json = Empty.to_json();
assert_eq!(json, "null");
}
#[test]
fn test_access_token_entity_from_access_token() {
let token = WxAccessToken::new("my_token", 7200);
let entity = WxAccessTokenEntity::from_access_token(token, "wx-app-001");
assert_eq!(entity.access_token, "my_token");
assert_eq!(entity.expires_in, 7200);
assert_eq!(entity.appid, "wx-app-001");
}
#[test]
fn test_access_token_entity_serde() {
let json = r#"{"access_token":"TOKEN","expires_in":3600,"appid":"wx1234"}"#;
let entity: WxAccessTokenEntity = serde_json::from_str(json).unwrap();
assert_eq!(entity.access_token, "TOKEN");
assert_eq!(entity.expires_in, 3600);
assert_eq!(entity.appid, "wx1234");
}
#[test]
fn test_card_api_signature_new() {
let sig = WxCardApiSignature::new(
"wx-app",
"card-001",
"GROUPON",
Some("loc-1".to_string()),
Some("code-1".to_string()),
Some("ox123".to_string()),
Some(1700000000),
"nonce123",
"signature_value",
);
assert_eq!(sig.app_id, "wx-app");
assert_eq!(sig.card_id, "card-001");
assert_eq!(sig.card_type, "GROUPON");
assert_eq!(sig.location_id, Some("loc-1".to_string()));
assert_eq!(sig.signature, "signature_value");
}
#[test]
fn test_card_api_signature_serde() {
let json =
r#"{"app_id":"wx1","card_id":"c1","card_type":"CASH","nonce_str":"n1","signature":"s1"}"#;
let sig: WxCardApiSignature = serde_json::from_str(json).unwrap();
assert_eq!(sig.app_id, "wx1");
assert_eq!(sig.card_id, "c1");
}
#[test]
fn test_card_api_signature_roundtrip() {
let sig = WxCardApiSignature::new(
"app", "card", "type", None, None, None, None, "nonce", "sig",
);
let json = serde_json::to_string(&sig).unwrap();
let deserialized: WxCardApiSignature = serde_json::from_str(&json).unwrap();
assert_eq!(sig, deserialized);
}
#[test]
fn test_jsapi_signature_serde() {
let sig = WxJsapiSignature::new("wx123", "nonce", 1700000000, "http://example.com", "sig123");
assert_eq!(sig.app_id, "wx123");
assert_eq!(sig.nonce_str, "nonce");
assert_eq!(sig.signature, "sig123");
}
#[test]
fn test_mp_error_msg_find() {
assert_eq!(wx_mp_error_msg_enum::find_msg_by_code(0), Some("请求成功"));
assert_eq!(
wx_mp_error_msg_enum::find_msg_by_code(-1),
Some("系统繁忙,此时请开发者稍候再试")
);
assert!(wx_mp_error_msg_enum::find_msg_by_code(40001).is_some());
assert_eq!(wx_mp_error_msg_enum::find_msg_by_code(999999), None);
}
#[test]
fn test_ma_error_msg_find() {
assert_eq!(wx_ma_error_msg_enum::find_msg_by_code(0), None);
assert!(wx_ma_error_msg_enum::find_msg_by_code(40001).is_some());
assert_eq!(wx_ma_error_msg_enum::find_msg_by_code(999999), None);
}
#[test]
fn test_cp_error_msg_find() {
assert!(wx_cp_error_msg_enum::find_msg_by_code(0).is_some());
assert!(wx_cp_error_msg_enum::find_msg_by_code(301002).is_some());
assert_eq!(wx_cp_error_msg_enum::find_msg_by_code(999999), None);
}
#[test]
fn test_open_error_msg_find() {
assert!(wx_open_error_msg_enum::find_msg_by_code(0).is_some());
assert!(wx_open_error_msg_enum::find_msg_by_code(61003).is_some());
assert_eq!(wx_open_error_msg_enum::find_msg_by_code(999999), None);
}
#[test]
fn test_channel_error_msg_find() {
assert!(wx_channel_error_msg_enum::find_msg_by_code(0).is_some());
assert!(wx_channel_error_msg_enum::find_msg_by_code(47001).is_some());
assert_eq!(wx_channel_error_msg_enum::find_msg_by_code(999999), None);
}
#[test]
fn test_wx_error_new() {
let err = wx_rust_common::error::WxError::new(40001, "invalid credential");
assert_eq!(err.error_code, 40001);
assert_eq!(err.error_msg, Some("invalid credential".to_string()));
}
#[test]
fn test_wx_runtime_error_new() {
let err = wx_rust_common::error::WxRuntimeError::new("runtime error");
assert_eq!(err.message, "runtime error");
}
#[test]
fn test_session_manager_new_session() {
use wx_rust_common::session::standard_session_manager::StandardSessionManager;
let mgr = StandardSessionManager::new();
let session = mgr.get_session("sess-001");
assert!(!session.id().is_empty());
}
#[test]
fn test_session_manager_same_session() {
use wx_rust_common::session::standard_session_manager::StandardSessionManager;
let mgr = StandardSessionManager::new();
let s1 = mgr.get_session("sess-001");
let s2 = mgr.get_session("sess-001");
assert_eq!(s1.id(), s2.id());
}
#[test]
fn test_session_set_attribute() {
use wx_rust_common::session::standard_session_manager::StandardSessionManager;
let mgr = StandardSessionManager::new();
let session = mgr.get_session("sess-002");
session.set_attribute("user_id", "ox123".to_string());
assert_eq!(session.get_attribute("user_id"), Some("ox123".to_string()));
}
#[test]
fn test_session_remove_attribute() {
use wx_rust_common::session::standard_session_manager::StandardSessionManager;
let mgr = StandardSessionManager::new();
let session = mgr.get_session("sess-003");
session.set_attribute("key", "val".to_string());
session.remove_attribute("key");
assert_eq!(session.get_attribute("key"), None);
}
#[test]
fn test_session_invalidate() {
use wx_rust_common::session::standard_session_manager::StandardSessionManager;
let mgr = StandardSessionManager::new();
let session = mgr.get_session("sess-004");
session.set_attribute("key", "val".to_string());
session.invalidate();
assert_eq!(session.get_attribute("key"), None);
}