fix(auth): ensure custom offline account sets default user and add unit tests

This commit is contained in:
katR 2026-08-29 06:21:11 +05:30
parent 481ca4d77e
commit c603d59908
2 changed files with 51 additions and 7 deletions

View file

@ -130,10 +130,11 @@ impl Component for OnboardingAccount {
let add_offline_nav = add_offline.clone();
let offline_name_nav = offline_name.clone();
let is_editing_nav = is_editing.clone();
let account_clone = account.clone();
let on_next = move |_| {
if account_clone.is_none() {
if account_clone.is_none() || *is_editing_nav.read() {
let name_val = offline_name_nav.read().trim().to_string();
let target = if name_val.is_empty() {
"Player".to_string()

View file

@ -109,9 +109,7 @@ impl CredentialsStore {
.find(|u| u.username.eq_ignore_ascii_case(&username))
.cloned()
{
if self.default_user.is_none() {
self.default_user = Some(existing.id);
}
self.default_user = Some(existing.id);
return Ok(existing);
}
}
@ -119,9 +117,7 @@ impl CredentialsStore {
let account = offline_account(username);
let id = account.id;
self.users.insert(id, account.clone());
if self.default_user.is_none() {
self.default_user = Some(id);
}
self.default_user = Some(id);
Ok(account)
}
@ -204,3 +200,50 @@ pub(crate) fn is_transient_auth_error(err: &AuthError) -> bool {
if source.is_connect() || source.is_timeout()
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_offline_account_creation_sets_default() {
let mut store = CredentialsStore {
users: HashMap::new(),
default_user: None,
};
let account = store.insert_offline_account("Player".to_string()).unwrap();
assert_eq!(account.username, "Player");
assert_eq!(store.default_user, Some(account.id));
assert_eq!(store.users.len(), 1);
}
#[test]
fn test_custom_username_switches_default() {
let mut store = CredentialsStore {
users: HashMap::new(),
default_user: None,
};
let p1 = store.insert_offline_account("Player".to_string()).unwrap();
assert_eq!(store.default_user, Some(p1.id));
let p2 = store.insert_offline_account("Steve".to_string()).unwrap();
assert_eq!(store.default_user, Some(p2.id));
assert_eq!(store.users.len(), 2);
}
#[test]
fn test_case_insensitive_username_reuse() {
let mut store = CredentialsStore {
users: HashMap::new(),
default_user: None,
};
let p1 = store.insert_offline_account("Steve".to_string()).unwrap();
let p2 = store.insert_offline_account("steve".to_string()).unwrap();
assert_eq!(p1.id, p2.id);
assert_eq!(store.users.len(), 1);
assert_eq!(store.default_user, Some(p1.id));
}
}