Skip to main content

syncpond_protocol/
lib.rs

1pub mod protocol;
2
3pub use protocol::{
4    Command,
5    Response,
6    format_command,
7    format_response,
8    parse_command,
9    parse_response,
10    connect_with_auth,
11    connect_with_retry,
12    start_long_lived_tcp,
13    Client,
14    send_command,
15};
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use serde_json::json;
21
22    #[test]
23    fn parse_and_format_room_create() {
24        let cmd = parse_command("ROOM.CREATE").unwrap();
25        assert_eq!(cmd, Command::RoomCreate);
26        let line = format_command(&cmd);
27        assert_eq!(line, "ROOM.CREATE");
28    }
29
30    #[test]
31    fn parse_set_with_json() {
32        let line = "SET 1 public foo {\"a\": 123, \"s\": \"x\"}";
33        let cmd = parse_command(line).unwrap();
34        match cmd {
35            Command::Set { room_id, container, key, value } => {
36                assert_eq!(room_id, 1);
37                assert_eq!(container, "public");
38                assert_eq!(key, "foo");
39                assert_eq!(value, json!({"a": 123, "s": "x"}));
40            }
41            _ => panic!("unexpected command variant"),
42        }
43    }
44
45    #[test]
46    fn response_roundtrip() {
47        let r = Response::Ok(Some("{\"a\":1}".to_string()));
48        let s = format_response(&r);
49        assert_eq!(s, "OK {\"a\":1}");
50        let parsed = parse_response(&s).unwrap();
51        assert_eq!(parsed, r);
52    }
53
54    #[tokio::test]
55    async fn connect_and_auth_and_receive() {
56        use tokio::net::TcpListener;
57        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
58        use tokio::time::Duration;
59
60        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
61        let addr = listener.local_addr().unwrap().to_string();
62        let api_key = "test-api-key-xyz";
63
64        let (mut rx, _tx) = start_long_lived_tcp(addr.clone(), api_key.to_string());
65
66        // Accept the incoming connection from our background client and validate auth.
67        let accept = tokio::spawn(async move {
68            let (mut socket, _) = listener.accept().await.unwrap();
69            let mut reader = BufReader::new(&mut socket);
70            let mut line = String::new();
71            reader.read_line(&mut line).await.unwrap();
72            assert_eq!(line.trim_end(), api_key);
73            socket.write_all(b"HELLO\n").await.unwrap();
74            socket.flush().await.unwrap();
75        });
76
77        let got = tokio::time::timeout(Duration::from_secs(2), rx.recv())
78            .await
79            .expect("timeout waiting for line")
80            .expect("channel closed");
81        assert_eq!(got, "HELLO");
82
83        accept.await.unwrap();
84    }
85
86    #[tokio::test]
87    async fn client_wrapper_send_and_shutdown() {
88        use tokio::net::TcpListener;
89        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
90        use tokio::time::Duration;
91
92        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
93        let addr = listener.local_addr().unwrap().to_string();
94        let api_key = "test-api-key-xyz".to_string();
95
96        // Start the client wrapper which spawns a background task.
97        let client = Client::start(addr.clone(), api_key.clone());
98
99        // Accept and validate auth + command from client in a background task.
100        let accept = tokio::spawn(async move {
101            let (mut socket, _) = listener.accept().await.unwrap();
102            let mut reader = BufReader::new(&mut socket);
103            let mut line = String::new();
104            // auth
105            reader.read_line(&mut line).await.unwrap();
106            assert_eq!(line.trim_end(), api_key);
107
108            // read command sent by client
109            let mut cmd_line = String::new();
110            reader.read_line(&mut cmd_line).await.unwrap();
111            assert_eq!(cmd_line.trim_end(), "ROOM.CREATE");
112        });
113
114        // Send a command from the client wrapper.
115        client.send(Command::RoomCreate).await.unwrap();
116
117        // Wait for the server task to accept the connection and receive the command,
118        // then shut down the client.
119        accept.await.unwrap();
120        client.shutdown().await.unwrap();
121    }
122}