use tap_agent::error::Result;
use tap_msg::didcomm::PlainMessage;
struct MockKeyManager {}
impl MockKeyManager {
fn new() -> Self {
Self {}
}
}
async fn pack_message(message: &TestMessage) -> Result<String> {
Ok(serde_json::to_string(message)?)
}
async fn unpack_message(packed_message: &str) -> Result<PlainMessage> {
let message: TestMessage = serde_json::from_str(packed_message)?;
let plain_message = PlainMessage {
id: message.id.clone(),
typ: "application/didcomm-plain+json".to_string(),
type_: message.message_type.clone(),
body: serde_json::to_value(&message)?,
from: "test-sender".to_string(),
to: vec!["test-recipient".to_string()],
thid: None,
pthid: None,
created_time: Some(1234567890),
expires_time: None,
from_prior: None,
attachments: None,
extra_headers: std::collections::HashMap::new(),
};
Ok(plain_message)
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestMessage {
pub message_type: String,
pub id: String,
pub content: String,
}
#[tokio::test]
async fn test_plain_packing_unpacking() -> Result<()> {
let _key_manager = MockKeyManager::new();
let message = TestMessage {
message_type: "test/plain".to_string(),
id: "msg-123".to_string(),
content: "Plain message content".to_string(),
};
let packed = pack_message(&message).await?;
assert!(packed.contains("Plain message content"));
assert!(packed.contains("test/plain"));
let unpacked = unpack_message(&packed).await?;
assert_eq!(unpacked.body["id"], "msg-123");
assert_eq!(unpacked.body["content"], "Plain message content");
Ok(())
}
#[tokio::test]
async fn test_signed_packing_unpacking() -> Result<()> {
let _key_manager = MockKeyManager::new();
let message = TestMessage {
message_type: "test/signed".to_string(),
id: "msg-456".to_string(),
content: "Signed message content".to_string(),
};
let packed = pack_message(&message).await?;
assert!(packed.contains("Signed message content"));
let unpacked = unpack_message(&packed).await?;
assert_eq!(unpacked.body["id"], "msg-456");
assert_eq!(unpacked.body["content"], "Signed message content");
Ok(())
}
#[tokio::test]
async fn test_auth_crypt_packing_unpacking() -> Result<()> {
let _key_manager = MockKeyManager::new();
let message = TestMessage {
message_type: "test/encrypted".to_string(),
id: "msg-789".to_string(),
content: "Encrypted message content".to_string(),
};
let packed = pack_message(&message).await?;
assert!(packed.contains("Encrypted message content"));
let unpacked = unpack_message(&packed).await?;
assert_eq!(unpacked.body["id"], "msg-789");
assert_eq!(unpacked.body["content"], "Encrypted message content");
Ok(())
}
#[tokio::test]
async fn test_unpack_options() -> Result<()> {
let _key_manager = MockKeyManager::new();
let message = TestMessage {
message_type: "test/options".to_string(),
id: "msg-options".to_string(),
content: "Testing unpack options".to_string(),
};
let packed = pack_message(&message).await?;
let unpacked = unpack_message(&packed).await?;
assert_eq!(unpacked.body["id"], "msg-options");
assert_eq!(unpacked.body["content"], "Testing unpack options");
Ok(())
}