use client::{ ChatClient, ContentData, ConversationIdOwned, Cursor, InProcessDelivery, StorageConfig, }; use std::sync::Arc; fn receive(receiver: &mut ChatClient, cursor: &mut Cursor) -> ContentData { let raw = cursor.next().expect("expected envelope"); receiver .receive(&raw) .expect("receive failed") .expect("expected content") } #[test] fn alice_bob_message_exchange() { let (delivery, bus) = InProcessDelivery::new(); let mut cursor = bus.subscribe_tail("delivery_address"); let mut alice = ChatClient::new("alice", delivery.clone()); let mut bob = ChatClient::new("bob", delivery); let bob_bundle = bob.create_intro_bundle().unwrap(); let alice_convo_id = alice .create_conversation(&bob_bundle, b"hello bob") .unwrap(); let content = receive(&mut bob, &mut cursor); assert_eq!(content.data, b"hello bob"); assert!(content.is_new_convo); let bob_convo_id: ConversationIdOwned = Arc::from(content.conversation_id.as_str()); bob.send_message(&bob_convo_id, b"hi alice").unwrap(); let content = receive(&mut alice, &mut cursor); assert_eq!(content.data, b"hi alice"); assert!(!content.is_new_convo); for i in 0u8..5 { let msg = format!("msg {i}"); alice.send_message(&alice_convo_id, msg.as_bytes()).unwrap(); let content = receive(&mut bob, &mut cursor); assert_eq!(content.data, msg.as_bytes()); let reply = format!("reply {i}"); bob.send_message(&bob_convo_id, reply.as_bytes()).unwrap(); let content = receive(&mut alice, &mut cursor); assert_eq!(content.data, reply.as_bytes()); } assert_eq!(alice.list_conversations().unwrap().len(), 1); assert_eq!(bob.list_conversations().unwrap().len(), 1); } #[test] fn open_persistent_client() { let dir = tempfile::tempdir().unwrap(); let db_path = dir.path().join("test.db").to_string_lossy().to_string(); let config = StorageConfig::File(db_path); let client1 = ChatClient::open("alice", config.clone(), InProcessDelivery::default()).unwrap(); let name1 = client1.installation_name().to_string(); drop(client1); let client2 = ChatClient::open("alice", config, InProcessDelivery::default()).unwrap(); let name2 = client2.installation_name().to_string(); assert_eq!( name1, name2, "installation name should persist across restarts" ); }