whatsapp_rust/types/
enc_handler.rs1use crate::client::Client;
2use crate::types::message::MessageInfo;
3use anyhow::Result;
4use std::sync::Arc;
5use wacore_binary::Node;
6
7#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
15#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
16pub trait EncHandler: wacore::sync_marker::MaybeSendSync {
17 async fn handle(&self, client: Arc<Client>, enc_node: &Node, info: &MessageInfo) -> Result<()>;
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33 use crate::TokioRuntime;
34 use crate::types::message::MessageInfo;
35 use anyhow::Result;
36 use async_lock::Mutex;
37 use std::sync::Arc;
38 use wacore_binary::Node;
39
40 #[derive(Debug)]
42 struct MockEncHandler {
43 pub calls: Arc<Mutex<Vec<String>>>,
44 }
45
46 impl MockEncHandler {
47 fn new() -> Self {
48 Self {
49 calls: Arc::new(Mutex::new(Vec::new())),
50 }
51 }
52 }
53
54 #[async_trait::async_trait]
55 impl EncHandler for MockEncHandler {
56 async fn handle(
57 &self,
58 _client: Arc<Client>,
59 enc_node: &Node,
60 _info: &MessageInfo,
61 ) -> Result<()> {
62 let enc_type = enc_node
63 .attrs()
64 .optional_string("type")
65 .as_deref()
66 .unwrap_or("unknown")
67 .to_string();
68 self.calls.lock().await.push(enc_type);
69 Ok(())
70 }
71 }
72
73 #[tokio::test]
74 async fn test_custom_enc_handler_registration() {
75 use crate::bot::Bot;
76
77 let mock_handler = MockEncHandler::new();
79
80 let backend = crate::test_utils::create_test_backend().await;
82
83 let transport = whatsapp_rust_tokio_transport::TokioWebSocketTransportFactory::new();
84 let http_client = whatsapp_rust_ureq_http_client::UreqHttpClient::new();
85 let bot = Bot::builder()
86 .with_backend_arc(backend)
87 .with_transport_factory(transport)
88 .with_http_client(http_client)
89 .with_enc_handler("frskmsg", mock_handler)
90 .with_runtime(TokioRuntime)
91 .build()
92 .await
93 .expect("Failed to build bot");
94
95 assert!(
97 bot.client()
98 .custom_enc_handlers
99 .get()
100 .unwrap()
101 .contains_key("frskmsg")
102 );
103 }
104
105 #[tokio::test]
106 async fn test_multiple_custom_handlers() {
107 use crate::bot::Bot;
108
109 let handler1 = MockEncHandler::new();
110 let handler2 = MockEncHandler::new();
111
112 let backend = crate::test_utils::create_test_backend().await;
114
115 let transport = whatsapp_rust_tokio_transport::TokioWebSocketTransportFactory::new();
116 let http_client = whatsapp_rust_ureq_http_client::UreqHttpClient::new();
117 let bot = Bot::builder()
118 .with_backend_arc(backend)
119 .with_transport_factory(transport)
120 .with_http_client(http_client)
121 .with_enc_handler("frskmsg", handler1)
122 .with_enc_handler("customtype", handler2)
123 .with_runtime(TokioRuntime)
124 .build()
125 .await
126 .expect("Failed to build bot");
127
128 let client = bot.client();
130 let handlers = client.custom_enc_handlers.get().unwrap();
131 assert!(handlers.contains_key("frskmsg"));
132 assert!(handlers.contains_key("customtype"));
133 assert_eq!(handlers.len(), 2);
134 }
135
136 #[tokio::test]
137 async fn test_builtin_handlers_still_work() {
138 use crate::bot::Bot;
139
140 let backend = crate::test_utils::create_test_backend().await;
142
143 let transport = whatsapp_rust_tokio_transport::TokioWebSocketTransportFactory::new();
144 let http_client = whatsapp_rust_ureq_http_client::UreqHttpClient::new();
145 let bot = Bot::builder()
146 .with_backend_arc(backend)
147 .with_transport_factory(transport)
148 .with_http_client(http_client)
149 .with_runtime(TokioRuntime)
150 .build()
151 .await
152 .expect("Failed to build bot");
153
154 assert!(bot.client().custom_enc_handlers.get().is_none());
156 }
157}