From d64d618a8d8f2503cd045679856954313c3ccb89 Mon Sep 17 00:00:00 2001 From: kaichaosun Date: Mon, 30 Mar 2026 18:22:05 +0800 Subject: [PATCH] chore: fix clippy --- core/conversations/src/context.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/core/conversations/src/context.rs b/core/conversations/src/context.rs index f05424f..cff118e 100644 --- a/core/conversations/src/context.rs +++ b/core/conversations/src/context.rs @@ -29,7 +29,7 @@ impl Context { /// /// If an identity exists in storage, it will be restored. /// Otherwise, a new identity will be created with the given name and saved. - pub fn open(name: impl Into, store: T) -> Result { + pub fn new_from_store(name: impl Into, mut store: T) -> Result { let name = name.into(); // Load or create identity @@ -37,8 +37,7 @@ impl Context { identity } else { let identity = Identity::new(&name); - // We need mut for save, but we can't take &mut here since store is moved. - // Identity will be saved below after we have ownership. + store.save_identity(&identity)?; identity }; @@ -208,7 +207,7 @@ impl Context { let convo_info = ConversationMeta { local_convo_id: convo.id().to_string(), remote_convo_id: convo.remote_id(), - kind: convo.convo_type().into(), + kind: convo.convo_type(), }; self.store.save_conversation(&convo_info)?; convo.save_ratchet_state(&mut self.store)?; @@ -219,7 +218,8 @@ impl Context { #[cfg(test)] mod tests { use sqlite::{ChatStorage, StorageConfig}; - use storage::ConversationStore; + use storage::{ConversationStore, IdentityStore}; + use tempfile::tempdir; use super::*; @@ -286,6 +286,24 @@ mod tests { assert!(!pubkey1.as_bytes().iter().all(|&b| b == 0)); } + #[test] + fn open_persists_new_identity() { + let dir = tempdir().unwrap(); + let db_path = dir.path().join("chat.sqlite"); + let db_path = db_path.to_string_lossy().into_owned(); + + let store = ChatStorage::new(StorageConfig::File(db_path.clone())).unwrap(); + let ctx = Context::new_from_store("alice", store).unwrap(); + let pubkey = ctx._identity.public_key(); + drop(ctx); + + let store = ChatStorage::new(StorageConfig::File(db_path)).unwrap(); + let persisted = store.load_identity().unwrap().unwrap(); + + assert_eq!(persisted.get_name(), "alice"); + assert_eq!(persisted.public_key(), pubkey); + } + #[test] fn conversation_metadata_persistence() { let mut alice = Context::new_with_name("alice", ChatStorage::in_memory());