Skip to content

Commit

Permalink
Fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hocuri committed Jan 6, 2025
1 parent 9b7e740 commit 1f82241
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/chatlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ mod tests {
let test_id = Contact::create(&t, "", "[email protected]").await?;
assert_eq!(contact_id, test_id);
let chat = Chat::load_from_db(&t, chat_id).await?;
assert_eq!(chat.get_name(), "Bob Authname");
assert_eq!(chat.get_name(), "~Bob Authname");
let chats = Chatlist::try_load(&t, 0, Some("bob authname"), None).await?;
assert_eq!(chats.len(), 1);
let chats = Chatlist::try_load(&t, 0, Some("bob nickname"), None).await?;
Expand Down
41 changes: 14 additions & 27 deletions src/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,6 @@ impl Origin {
pub fn is_known(self) -> bool {
self >= Origin::IncomingReplyTo
}

fn displayname_prefix(self) -> &'static str {
if self == Origin::ManuallyCreated {
""
} else {
"~"
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -886,7 +878,7 @@ impl Contact {
row_origin
};
let new_authname = if update_authname {
name.to_string()
name
} else {
row_authname
};
Expand Down Expand Up @@ -915,11 +907,10 @@ impl Contact {
).optional()?;

if let Some(chat_id) = chat_id {
let prefix = origin.displayname_prefix();
let chat_name = if !new_name.is_empty() {
format!("{prefix}{}", new_name)
new_name
} else if !new_authname.is_empty() {
format!("{prefix}{}", new_authname)
format!("~{}", new_authname)
} else {
addr.to_string()
};
Expand Down Expand Up @@ -1364,12 +1355,11 @@ impl Contact {
/// This name is typically used in lists.
/// To get the name editable in a formular, use `Contact::get_name`.
pub fn get_display_name(&self) -> String {
let prefix = self.origin.displayname_prefix();
if !self.name.is_empty() {
return format!("{prefix}{}", self.name);
return self.name.clone();
}
if !self.authname.is_empty() {
return format!("{prefix}{}", self.authname);
return format!("~{}", self.authname);
}
self.addr.clone()
}
Expand Down Expand Up @@ -1400,11 +1390,10 @@ impl Contact {
/// The summary is typically used when asking the user something about the contact.
/// The attached email address makes the question unique, eg. "Chat with Alan Miller ([email protected])?"
pub fn get_name_n_addr(&self) -> String {
let prefix = self.origin.displayname_prefix();
if !self.name.is_empty() {
format!("{prefix}{} ({})", self.name, self.addr)
format!("{} ({})", self.name, self.addr)
} else if !self.authname.is_empty() {
format!("{prefix}{} ({})", self.authname, self.addr)
format!("~{} ({})", self.authname, self.addr)
} else {
(&self.addr).into()
}
Expand Down Expand Up @@ -2110,7 +2099,6 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_add_or_lookup() {
// add some contacts, this also tests add_address_book()
// TODO it's unsure whether we should put a ~ in front of these.
let t = TestContext::new().await;
let book = concat!(
" Name one \n [email protected] \n",
Expand All @@ -2137,9 +2125,9 @@ mod tests {
assert_eq!(contact.get_id(), contact_id);
assert_eq!(contact.get_name(), "Name one");
assert_eq!(contact.get_authname(), "bla foo");
assert_eq!(contact.get_display_name(), "~Name one");
assert_eq!(contact.get_display_name(), "Name one");
assert_eq!(contact.get_addr(), "[email protected]");
assert_eq!(contact.get_name_n_addr(), "~Name one ([email protected])");
assert_eq!(contact.get_name_n_addr(), "Name one ([email protected])");

// modify first added contact
let (contact_id_test, sth_modified) = Contact::add_or_lookup(
Expand Down Expand Up @@ -2218,9 +2206,9 @@ mod tests {
assert_eq!(sth_modified, Modifier::None);
let contact = Contact::get_by_id(&t, contact_id).await.unwrap();
assert_eq!(contact.get_name(), "Wonderland, Alice");
assert_eq!(contact.get_display_name(), "~Wonderland, Alice");
assert_eq!(contact.get_display_name(), "Wonderland, Alice");
assert_eq!(contact.get_addr(), "[email protected]");
assert_eq!(contact.get_name_n_addr(), "~Wonderland, Alice ([email protected])");
assert_eq!(contact.get_name_n_addr(), "Wonderland, Alice ([email protected])");

// check SELF
let contact = Contact::get_by_id(&t, ContactId::SELF).await.unwrap();
Expand Down Expand Up @@ -2511,8 +2499,7 @@ mod tests {
let contact = Contact::get_by_id(&t, contact_id).await.unwrap();
assert_eq!(contact.get_authname(), "claire1");
assert_eq!(contact.get_name(), "");
println!("dbg/TODO {:?}", contact.origin);
assert_eq!(contact.get_display_name(), "~claire1"); // TODO this NEEDS to be "~claire1"
assert_eq!(contact.get_display_name(), "~claire1");

// incoming mail `From: claire2 <[email protected]>` - this should update authname
let (contact_id_same, sth_modified) = Contact::add_or_lookup(
Expand All @@ -2528,7 +2515,7 @@ mod tests {
let contact = Contact::get_by_id(&t, contact_id).await.unwrap();
assert_eq!(contact.get_authname(), "claire2");
assert_eq!(contact.get_name(), "");
assert_eq!(contact.get_display_name(), "claire2");
assert_eq!(contact.get_display_name(), "~claire2");
}

/// Regression test.
Expand Down Expand Up @@ -2612,7 +2599,7 @@ mod tests {
let contact = Contact::get_by_id(&t, contact_id).await.unwrap();
assert_eq!(contact.get_authname(), "dave2");
assert_eq!(contact.get_name(), "");
assert_eq!(contact.get_display_name(), "dave2");
assert_eq!(contact.get_display_name(), "~dave2");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ mod tests {
assert_eq!(contact.get_addr(), "[email protected]");
assert_eq!(contact.get_name(), "First Last");
assert_eq!(contact.get_authname(), "");
assert_eq!(contact.get_display_name(), "~First Last");
assert_eq!(contact.get_display_name(), "First Last");
assert!(draft.is_none());
} else {
bail!("Wrong QR code type");
Expand Down
1 change: 0 additions & 1 deletion src/tests/verified_chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,6 @@ async fn test_no_unencrypted_name_if_encrypted() -> Result<()> {

let msg = alice.recv_msg(msg).await;
let contact = Contact::get_by_id(&alice, msg.from_id).await?;
println!("dbg/TODO origin: {:?}", contact.origin);
assert_eq!(Contact::get_display_name(&contact), "~Bob Smith");
}
Ok(())
Expand Down

0 comments on commit 1f82241

Please sign in to comment.