2024-12-08 21:12:17 -08:00
|
|
|
pub mod models;
|
|
|
|
|
pub mod chat_database;
|
|
|
|
|
|
|
|
|
|
#[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() {
|
|
|
|
|
let db = ChatDatabase::new_in_memory().unwrap();
|
|
|
|
|
|
|
|
|
|
let test_conversation = Conversation::builder()
|
|
|
|
|
.guid("test")
|
|
|
|
|
.unread_count(2)
|
|
|
|
|
.display_name("Test Conversation")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let id = db.insert_conversation(test_conversation.clone()).unwrap();
|
|
|
|
|
|
|
|
|
|
// Try to fetch with id now
|
|
|
|
|
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
|
|
|
|
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
|
|
|
|
|
let conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
|
|
|
|
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_conversation_participants() {
|
|
|
|
|
let db = ChatDatabase::new_in_memory().unwrap();
|
|
|
|
|
|
2024-12-14 12:53:44 -08:00
|
|
|
let participants: Vec<Participant> = vec!["one".into(), "two".into()];
|
|
|
|
|
|
2024-12-08 21:12:17 -08:00
|
|
|
let conversation = ConversationBuilder::new()
|
|
|
|
|
.display_name("Test")
|
|
|
|
|
.participant_display_names(participants.clone())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let id = db.insert_conversation(conversation).unwrap();
|
|
|
|
|
|
|
|
|
|
let read_conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
2024-12-14 12:53:44 -08:00
|
|
|
let read_participants: Vec<Participant> = read_conversation.get_participant_display_names();
|
|
|
|
|
|
|
|
|
|
assert_eq!(participants, read_participants);
|
|
|
|
|
|
|
|
|
|
// Try making another conversation with the same participants
|
|
|
|
|
let conversation = ConversationBuilder::new()
|
|
|
|
|
.display_name("A Different Test")
|
|
|
|
|
.participant_display_names(participants.clone())
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let id = db.insert_conversation(conversation).unwrap();
|
|
|
|
|
|
|
|
|
|
let read_conversation = db.get_conversation_by_id(id).unwrap().unwrap();
|
|
|
|
|
let read_participants: Vec<Participant> = read_conversation.get_participant_display_names();
|
2024-12-08 21:12:17 -08:00
|
|
|
|
|
|
|
|
assert_eq!(participants, read_participants);
|
|
|
|
|
}
|
|
|
|
|
}
|