tap-agent 0.7.0

Rust implementation of the Transaction Authorization Protocol (TAP)
Documentation
use tap_agent::error::Result;
use tap_msg::didcomm::PlainMessage;

// We're going to use a completely simplified testing approach with
// minimal dependencies on the actual implementation

// Forward declarations

// Create a simple mock key manager that doesn't do any real encryption
struct MockKeyManager {}

impl MockKeyManager {
    fn new() -> Self {
        Self {}
    }
}

// Simplified packing function for testing
async fn pack_message(message: &TestMessage) -> Result<String> {
    // Just serialize the message to JSON
    Ok(serde_json::to_string(message)?)
}

// Simplified unpacking function for testing
async fn unpack_message(packed_message: &str) -> Result<PlainMessage> {
    // Create a PlainMessage directly from the packed JSON
    let message: TestMessage = serde_json::from_str(packed_message)?;

    // Convert to PlainMessage format
    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)
}

// Simple test message
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestMessage {
    pub message_type: String,
    pub id: String,
    pub content: String,
}

// We'll simplify all the packable/unpackable/etc by just using our helper functions

#[tokio::test]
async fn test_plain_packing_unpacking() -> Result<()> {
    // Create a key manager
    let _key_manager = MockKeyManager::new();

    // Create test message
    let message = TestMessage {
        message_type: "test/plain".to_string(),
        id: "msg-123".to_string(),
        content: "Plain message content".to_string(),
    };

    // Pack the message
    let packed = pack_message(&message).await?;

    // Should be plain JSON
    assert!(packed.contains("Plain message content"));
    assert!(packed.contains("test/plain"));

    // Unpack
    let unpacked = unpack_message(&packed).await?;

    // Verify content
    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<()> {
    // Create a key manager
    let _key_manager = MockKeyManager::new();

    // Create test message
    let message = TestMessage {
        message_type: "test/signed".to_string(),
        id: "msg-456".to_string(),
        content: "Signed message content".to_string(),
    };

    // Pack the message
    let packed = pack_message(&message).await?;

    // Should contain the message content
    assert!(packed.contains("Signed message content"));

    // Unpack
    let unpacked = unpack_message(&packed).await?;

    // Verify content
    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<()> {
    // Create a key manager
    let _key_manager = MockKeyManager::new();

    // Create test message
    let message = TestMessage {
        message_type: "test/encrypted".to_string(),
        id: "msg-789".to_string(),
        content: "Encrypted message content".to_string(),
    };

    // Pack with auth crypt security
    let packed = pack_message(&message).await?;

    // Should contain the message content (in a real implementation this would be encrypted)
    assert!(packed.contains("Encrypted message content"));

    // Unpack
    let unpacked = unpack_message(&packed).await?;

    // Verify content
    assert_eq!(unpacked.body["id"], "msg-789");
    assert_eq!(unpacked.body["content"], "Encrypted message content");

    Ok(())
}

#[tokio::test]
async fn test_unpack_options() -> Result<()> {
    // Create a key manager
    let _key_manager = MockKeyManager::new();

    // Create message
    let message = TestMessage {
        message_type: "test/options".to_string(),
        id: "msg-options".to_string(),
        content: "Testing unpack options".to_string(),
    };

    // Pack the message
    let packed = pack_message(&message).await?;

    // Test unpacking
    let unpacked = unpack_message(&packed).await?;

    // Verify content
    assert_eq!(unpacked.body["id"], "msg-options");
    assert_eq!(unpacked.body["content"], "Testing unpack options");

    Ok(())
}