Private
Public Access
1
0

cargo fmt

This commit is contained in:
2025-06-06 16:39:31 -07:00
parent 8cd72d9417
commit 1d3b2f25ba
44 changed files with 758 additions and 505 deletions

View File

@@ -1,9 +1,9 @@
use crate::{
database::{Database, DatabaseAccess},
database::{Database, DatabaseAccess},
models::{
conversation::{Conversation, ConversationBuilder},
participant::Participant,
message::Message,
participant::Participant,
},
};
@@ -11,9 +11,17 @@ use crate::{
fn participants_equal_ignoring_id(a: &Participant, b: &Participant) -> bool {
match (a, b) {
(Participant::Me, Participant::Me) => true,
(Participant::Remote { display_name: name_a, .. },
Participant::Remote { display_name: name_b, .. }) => name_a == name_b,
_ => false
(
Participant::Remote {
display_name: name_a,
..
},
Participant::Remote {
display_name: name_b,
..
},
) => name_a == name_b,
_ => false,
}
}
@@ -21,7 +29,9 @@ fn participants_vec_equal_ignoring_id(a: &[Participant], b: &[Participant]) -> b
if a.len() != b.len() {
return false;
}
a.iter().zip(b.iter()).all(|(a, b)| participants_equal_ignoring_id(a, b))
a.iter()
.zip(b.iter())
.all(|(a, b)| participants_equal_ignoring_id(a, b))
}
#[tokio::test]
@@ -40,27 +50,33 @@ async fn test_add_conversation() {
.display_name("Test Conversation")
.build();
repository.insert_conversation(test_conversation.clone()).unwrap();
repository
.insert_conversation(test_conversation.clone())
.unwrap();
// Try to fetch with id now
// Try to fetch with id now
let conversation = repository.get_conversation_by_guid(guid).unwrap().unwrap();
assert_eq!(conversation.guid, "test");
// Modify the conversation and update it
let modified_conversation = test_conversation.into_builder()
let modified_conversation = test_conversation
.into_builder()
.display_name("Modified Conversation")
.build();
repository.insert_conversation(modified_conversation.clone()).unwrap();
repository
.insert_conversation(modified_conversation.clone())
.unwrap();
// Make sure we still only have one conversation.
// Make sure we still only have one conversation.
let all_conversations = repository.all_conversations(i32::MAX, 0).unwrap();
assert_eq!(all_conversations.len(), 1);
// And make sure the display name was updated
// And make sure the display name was updated
let conversation = repository.get_conversation_by_guid(guid).unwrap().unwrap();
assert_eq!(conversation.display_name.unwrap(), "Modified Conversation");
}).await;
})
.await;
}
#[tokio::test]
@@ -81,7 +97,10 @@ async fn test_conversation_participants() {
let read_conversation = repository.get_conversation_by_guid(&guid).unwrap().unwrap();
let read_participants = read_conversation.participants;
assert!(participants_vec_equal_ignoring_id(&participants, &read_participants));
assert!(participants_vec_equal_ignoring_id(
&participants,
&read_participants
));
// Try making another conversation with the same participants
let conversation = ConversationBuilder::new()
@@ -94,8 +113,12 @@ async fn test_conversation_participants() {
let read_conversation = repository.get_conversation_by_guid(&guid).unwrap().unwrap();
let read_participants: Vec<Participant> = read_conversation.participants;
assert!(participants_vec_equal_ignoring_id(&participants, &read_participants));
}).await;
assert!(participants_vec_equal_ignoring_id(
&participants,
&read_participants
));
})
.await;
}
#[tokio::test]
@@ -132,9 +155,16 @@ async fn test_all_conversations_with_participants() {
let conv1 = all_conversations.iter().find(|c| c.guid == guid1).unwrap();
let conv2 = all_conversations.iter().find(|c| c.guid == guid2).unwrap();
assert!(participants_vec_equal_ignoring_id(&conv1.participants, &participants1));
assert!(participants_vec_equal_ignoring_id(&conv2.participants, &participants2));
}).await;
assert!(participants_vec_equal_ignoring_id(
&conv1.participants,
&participants1
));
assert!(participants_vec_equal_ignoring_id(
&conv2.participants,
&participants2
));
})
.await;
}
#[tokio::test]
@@ -163,11 +193,17 @@ async fn test_messages() {
.build();
// Insert both messages
repository.insert_message(&conversation_id, message1.clone()).unwrap();
repository.insert_message(&conversation_id, message2.clone()).unwrap();
repository
.insert_message(&conversation_id, message1.clone())
.unwrap();
repository
.insert_message(&conversation_id, message2.clone())
.unwrap();
// Retrieve messages
let messages = repository.get_messages_for_conversation(&conversation_id).unwrap();
let messages = repository
.get_messages_for_conversation(&conversation_id)
.unwrap();
assert_eq!(messages.len(), 2);
// Verify first message (from Me)
@@ -181,9 +217,13 @@ async fn test_messages() {
if let Participant::Remote { display_name, .. } = &retrieved_message2.sender {
assert_eq!(display_name, "Alice");
} else {
panic!("Expected Remote participant. Got: {:?}", retrieved_message2.sender);
panic!(
"Expected Remote participant. Got: {:?}",
retrieved_message2.sender
);
}
}).await;
})
.await;
}
#[tokio::test]
@@ -191,9 +231,7 @@ async fn test_message_ordering() {
let mut db = Database::new_in_memory().unwrap();
db.with_repository(|repository| {
// Create a conversation
let conversation = ConversationBuilder::new()
.display_name("Test Chat")
.build();
let conversation = ConversationBuilder::new().display_name("Test Chat").build();
let conversation_id = conversation.guid.clone();
repository.insert_conversation(conversation).unwrap();
@@ -215,19 +253,28 @@ async fn test_message_ordering() {
.build();
// Insert messages
repository.insert_message(&conversation_id, message1).unwrap();
repository.insert_message(&conversation_id, message2).unwrap();
repository.insert_message(&conversation_id, message3).unwrap();
repository
.insert_message(&conversation_id, message1)
.unwrap();
repository
.insert_message(&conversation_id, message2)
.unwrap();
repository
.insert_message(&conversation_id, message3)
.unwrap();
// Retrieve messages and verify order
let messages = repository.get_messages_for_conversation(&conversation_id).unwrap();
let messages = repository
.get_messages_for_conversation(&conversation_id)
.unwrap();
assert_eq!(messages.len(), 3);
// Messages should be ordered by date
for i in 1..messages.len() {
assert!(messages[i].date > messages[i-1].date);
assert!(messages[i].date > messages[i - 1].date);
}
}).await;
})
.await;
}
#[tokio::test]
@@ -245,10 +292,7 @@ async fn test_insert_messages_batch() {
// Prepare a batch of messages with increasing timestamps
let now = chrono::Utc::now().naive_utc();
let message1 = Message::builder()
.text("Hi".to_string())
.date(now)
.build();
let message1 = Message::builder().text("Hi".to_string()).date(now).build();
let message2 = Message::builder()
.text("Hello".to_string())
@@ -280,7 +324,9 @@ async fn test_insert_messages_batch() {
.unwrap();
// Retrieve messages and verify
let retrieved_messages = repository.get_messages_for_conversation(&conversation_id).unwrap();
let retrieved_messages = repository
.get_messages_for_conversation(&conversation_id)
.unwrap();
assert_eq!(retrieved_messages.len(), original_messages.len());
// Ensure ordering by date
@@ -299,8 +345,14 @@ async fn test_insert_messages_batch() {
match (&original.sender, &retrieved.sender) {
(Participant::Me, Participant::Me) => {}
(
Participant::Remote { display_name: o_name, .. },
Participant::Remote { display_name: r_name, .. },
Participant::Remote {
display_name: o_name,
..
},
Participant::Remote {
display_name: r_name,
..
},
) => assert_eq!(o_name, r_name),
_ => panic!(
"Sender mismatch: original {:?}, retrieved {:?}",
@@ -310,7 +362,10 @@ async fn test_insert_messages_batch() {
}
// Make sure the last message is the last one we inserted
let last_message = repository.get_last_message_for_conversation(&conversation_id).unwrap().unwrap();
let last_message = repository
.get_last_message_for_conversation(&conversation_id)
.unwrap()
.unwrap();
assert_eq!(last_message.id, message4.id);
})
.await;
@@ -329,7 +384,7 @@ async fn test_settings() {
let keys = settings.list_keys().unwrap();
assert_eq!(keys.len(), 0);
// Try encoding a struct
// Try encoding a struct
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Eq)]
struct TestStruct {
name: String,
@@ -342,13 +397,34 @@ async fn test_settings() {
};
settings.put("test_struct", &test_struct).unwrap();
assert_eq!(settings.get::<TestStruct>("test_struct").unwrap().unwrap(), test_struct);
assert_eq!(
settings.get::<TestStruct>("test_struct").unwrap().unwrap(),
test_struct
);
// Test with an option<string>
settings.put("test_struct_option", &Option::<String>::None).unwrap();
assert!(settings.get::<Option<String>>("test_struct_option").unwrap().unwrap().is_none());
settings
.put("test_struct_option", &Option::<String>::None)
.unwrap();
assert!(settings
.get::<Option<String>>("test_struct_option")
.unwrap()
.unwrap()
.is_none());
settings.put("test_struct_option", &Option::<String>::Some("test".to_string())).unwrap();
assert_eq!(settings.get::<Option<String>>("test_struct_option").unwrap().unwrap(), Some("test".to_string()));
}).await;
settings
.put(
"test_struct_option",
&Option::<String>::Some("test".to_string()),
)
.unwrap();
assert_eq!(
settings
.get::<Option<String>>("test_struct_option")
.unwrap()
.unwrap(),
Some("test".to_string())
);
})
.await;
}