diff --git a/crates/notedeck/src/storage/file_key_storage.rs b/crates/notedeck/src/storage/file_key_storage.rs index 9e5a9e5b..aacd32c4 100644 --- a/crates/notedeck/src/storage/file_key_storage.rs +++ b/crates/notedeck/src/storage/file_key_storage.rs @@ -47,11 +47,14 @@ impl FileKeyStorage { } fn get_selected_pubkey(&self) -> Result> { - let pubkey_str = self + match self .selected_key_directory - .get_file(SELECTED_PUBKEY_FILE_NAME.to_owned())?; - - Ok(serde_json::from_str(&pubkey_str)?) + .get_file(SELECTED_PUBKEY_FILE_NAME.to_owned()) + { + Ok(pubkey_str) => Ok(Some(serde_json::from_str(&pubkey_str)?)), + Err(crate::Error::Io(_)) => Ok(None), + Err(e) => Err(e), + } } fn select_pubkey(&self, pubkey: Option) -> Result<()> { @@ -164,4 +167,15 @@ mod tests { assert!(resp.is_ok()); } + + #[test] + fn test_get_selected_key_when_no_file() { + let storage = FileKeyStorage::mock().unwrap(); + + // Should return Ok(None) when no key has been selected + match storage.get_selected_key() { + KeyStorageResponse::ReceivedResult(Ok(None)) => (), // This is what we expect + other => panic!("Expected Ok(None), got {:?}", other), + } + } } diff --git a/crates/notedeck/src/storage/file_storage.rs b/crates/notedeck/src/storage/file_storage.rs index fed1ac67..37bdc2dc 100644 --- a/crates/notedeck/src/storage/file_storage.rs +++ b/crates/notedeck/src/storage/file_storage.rs @@ -105,9 +105,9 @@ impl Directory { .ok_or_else(|| Error::Generic("Could not turn path to string".to_owned()))?; Ok(fs::read_to_string(filepath_str)?) } else { - Err(Error::Generic(format!( - "Requested file was not found: {}", - file_name + Err(Error::Io(io::Error::new( + io::ErrorKind::NotFound, + format!("Requested file was not found: {}", file_name), ))) } } diff --git a/crates/notedeck_columns/src/decks.rs b/crates/notedeck_columns/src/decks.rs index 1d832bd9..adeabbec 100644 --- a/crates/notedeck_columns/src/decks.rs +++ b/crates/notedeck_columns/src/decks.rs @@ -35,9 +35,14 @@ impl Default for DecksCache { } impl DecksCache { - pub fn new(account_to_decks: HashMap) -> Self { + pub fn new(mut account_to_decks: HashMap) -> Self { let fallback_pubkey = FALLBACK_PUBKEY(); + // Ensure fallback deck exists + if !account_to_decks.contains_key(&fallback_pubkey) { + account_to_decks.insert(fallback_pubkey, Decks::default()); + } + Self { account_to_decks, fallback_pubkey,