2024-12-08 21:12:17 -08:00
|
|
|
pub mod models;
|
|
|
|
|
pub mod chat_database;
|
2024-12-14 19:03:27 -08:00
|
|
|
pub mod schema;
|
2024-12-08 21:12:17 -08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2024-12-14 12:53:44 -08:00
|
|
|
use crate::{
|
|
|
|
|
chat_database::ChatDatabase,
|
|
|
|
|
models::{
|
|
|
|
|
conversation::{Conversation, ConversationBuilder},
|
|
|
|
|
participant::Participant
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-12-08 21:12:17 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_database_init() {
|
|
|
|
|
let _ = ChatDatabase::new_in_memory().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_add_conversation() {
|
2024-12-14 19:03:27 -08:00
|
|
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
let guid = "test";
|
2024-12-08 21:12:17 -08:00
|
|
|
let test_conversation = Conversation::builder()
|
2024-12-14 19:03:27 -08:00
|
|
|
.guid(guid)
|
2024-12-08 21:12:17 -08:00
|
|
|
.unread_count(2)
|
|
|
|
|
.display_name("Test Conversation")
|
|
|
|
|
.build();
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
db.insert_conversation(test_conversation.clone()).unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
|
|
|
|
// Try to fetch with id now
|
2024-12-14 19:03:27 -08:00
|
|
|
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
assert_eq!(conversation.guid, "test");
|
|
|
|
|
|
|
|
|
|
// Modify the conversation and update it
|
|
|
|
|
let modified_conversation = test_conversation.into_builder()
|
|
|
|
|
.display_name("Modified Conversation")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
db.insert_conversation(modified_conversation.clone()).unwrap();
|
|
|
|
|
|
|
|
|
|
// Make sure we still only have one conversation.
|
|
|
|
|
let all_conversations = db.all_conversations().unwrap();
|
|
|
|
|
assert_eq!(all_conversations.len(), 1);
|
|
|
|
|
|
|
|
|
|
// And make sure the display name was updated
|
2024-12-14 19:03:27 -08:00
|
|
|
let conversation = db.get_conversation_by_guid(guid).unwrap().unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conversation_participants() {
|
2024-12-14 19:03:27 -08:00
|
|
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 12:53:44 -08:00
|
|
|
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
let guid = uuid::Uuid::new_v4().to_string();
|
2024-12-08 21:12:17 -08:00
|
|
|
let conversation = ConversationBuilder::new()
|
2024-12-14 19:03:27 -08:00
|
|
|
.guid(&guid)
|
2024-12-08 21:12:17 -08:00
|
|
|
.display_name("Test")
|
2024-12-14 19:03:27 -08:00
|
|
|
.participants(participants.clone())
|
2024-12-08 21:12:17 -08:00
|
|
|
.build();
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
db.insert_conversation(conversation).unwrap();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
|
|
|
|
let read_participants = read_conversation.participants;
|
2024-12-14 12:53:44 -08:00
|
|
|
|
|
|
|
|
assert_eq!(participants, read_participants);
|
|
|
|
|
|
|
|
|
|
// Try making another conversation with the same participants
|
|
|
|
|
let conversation = ConversationBuilder::new()
|
|
|
|
|
.display_name("A Different Test")
|
2024-12-14 19:03:27 -08:00
|
|
|
.participants(participants.clone())
|
2024-12-14 12:53:44 -08:00
|
|
|
.build();
|
|
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
db.insert_conversation(conversation).unwrap();
|
2024-12-14 12:53:44 -08:00
|
|
|
|
2024-12-14 19:03:27 -08:00
|
|
|
let read_conversation = db.get_conversation_by_guid(&guid).unwrap().unwrap();
|
|
|
|
|
let read_participants: Vec<Participant> = read_conversation.participants;
|
2024-12-08 21:12:17 -08:00
|
|
|
|
|
|
|
|
assert_eq!(participants, read_participants);
|
|
|
|
|
}
|
2024-12-21 16:34:47 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_all_conversations_with_participants() {
|
|
|
|
|
let mut db = ChatDatabase::new_in_memory().unwrap();
|
|
|
|
|
|
|
|
|
|
// Create two conversations with different participants
|
|
|
|
|
let participants1: Vec<Participant> = vec!["one".into(), "two".into()];
|
|
|
|
|
let participants2: Vec<Participant> = vec!["three".into(), "four".into()];
|
|
|
|
|
|
|
|
|
|
let guid1 = uuid::Uuid::new_v4().to_string();
|
|
|
|
|
let conversation1 = ConversationBuilder::new()
|
|
|
|
|
.guid(&guid1)
|
|
|
|
|
.display_name("Test 1")
|
|
|
|
|
.participants(participants1.clone())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let guid2 = uuid::Uuid::new_v4().to_string();
|
|
|
|
|
let conversation2 = ConversationBuilder::new()
|
|
|
|
|
.guid(&guid2)
|
|
|
|
|
.display_name("Test 2")
|
|
|
|
|
.participants(participants2.clone())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// Insert both conversations
|
|
|
|
|
db.insert_conversation(conversation1).unwrap();
|
|
|
|
|
db.insert_conversation(conversation2).unwrap();
|
|
|
|
|
|
|
|
|
|
// Get all conversations and verify the results
|
|
|
|
|
let all_conversations = db.all_conversations().unwrap();
|
|
|
|
|
assert_eq!(all_conversations.len(), 2);
|
|
|
|
|
|
|
|
|
|
// Find and verify each conversation's participants
|
|
|
|
|
let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap();
|
|
|
|
|
let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(conv1.participants, participants1);
|
|
|
|
|
assert_eq!(conv2.participants, participants2);
|
|
|
|
|
}
|
2024-12-08 21:12:17 -08:00
|
|
|
}
|