2026-02-05 14:12:21 +08:00
|
|
|
//! ChatManager with integrated SQLite persistence.
|
|
|
|
|
//!
|
|
|
|
|
//! This is the main entry point for the conversations API. It handles all
|
|
|
|
|
//! storage operations internally - users don't need to interact with storage directly.
|
|
|
|
|
|
2026-02-05 12:12:08 +08:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
2026-02-05 14:52:04 +08:00
|
|
|
use double_ratchets::storage::RatchetStorage;
|
2026-02-05 15:40:30 +08:00
|
|
|
use prost::Message;
|
2026-02-05 14:52:04 +08:00
|
|
|
|
2026-02-05 12:12:08 +08:00
|
|
|
use crate::{
|
2026-02-05 14:44:22 +08:00
|
|
|
common::{Chat, HasChatId, InboundMessageHandler},
|
|
|
|
|
dm::privatev1::PrivateV1Convo,
|
2026-02-05 12:12:08 +08:00
|
|
|
errors::ChatError,
|
|
|
|
|
identity::Identity,
|
|
|
|
|
inbox::{Inbox, Introduction},
|
2026-02-05 15:40:30 +08:00
|
|
|
proto,
|
2026-02-05 14:12:21 +08:00
|
|
|
storage::{ChatRecord, ChatStorage, StorageError},
|
2026-02-05 12:12:08 +08:00
|
|
|
types::{AddressedEnvelope, ContentData},
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-05 14:18:33 +08:00
|
|
|
// Re-export StorageConfig from storage crate for convenience
|
|
|
|
|
pub use storage::StorageConfig;
|
2026-02-05 14:12:21 +08:00
|
|
|
|
|
|
|
|
/// Error type for ChatManager operations.
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
|
pub enum ChatManagerError {
|
|
|
|
|
#[error("chat error: {0}")]
|
|
|
|
|
Chat(#[from] ChatError),
|
|
|
|
|
|
|
|
|
|
#[error("storage error: {0}")]
|
|
|
|
|
Storage(#[from] StorageError),
|
2026-02-05 14:29:44 +08:00
|
|
|
|
|
|
|
|
#[error("chat not found: {0}")]
|
|
|
|
|
ChatNotFound(String),
|
2026-02-05 14:12:21 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 12:12:08 +08:00
|
|
|
/// ChatManager is the main entry point for the conversations API.
|
|
|
|
|
///
|
2026-02-05 16:02:02 +08:00
|
|
|
/// It manages identity, inbox, and chats with all state persisted to SQLite.
|
|
|
|
|
/// Chats are loaded from storage on each operation - no in-memory caching.
|
2026-02-05 16:25:28 +08:00
|
|
|
/// Uses a single shared database for both chat metadata and ratchet state.
|
2026-02-05 14:12:21 +08:00
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```ignore
|
|
|
|
|
/// // Create a new chat manager with encrypted storage
|
|
|
|
|
/// let mut chat = ChatManager::open(StorageConfig::Encrypted {
|
|
|
|
|
/// path: "chat.db".into(),
|
|
|
|
|
/// key: "my_secret_key".into(),
|
|
|
|
|
/// })?;
|
|
|
|
|
///
|
|
|
|
|
/// // Get your address to share with others
|
|
|
|
|
/// println!("My address: {}", chat.local_address());
|
|
|
|
|
///
|
|
|
|
|
/// // Create an intro bundle to share
|
|
|
|
|
/// let intro = chat.create_intro_bundle()?;
|
|
|
|
|
///
|
|
|
|
|
/// // Start a chat with someone
|
|
|
|
|
/// let (chat_id, envelopes) = chat.start_private_chat(&their_intro, "Hello!")?;
|
|
|
|
|
/// // Send envelopes over the network...
|
|
|
|
|
///
|
|
|
|
|
/// // Send more messages
|
|
|
|
|
/// let envelopes = chat.send_message(&chat_id, b"How are you?")?;
|
|
|
|
|
/// ```
|
2026-02-05 12:12:08 +08:00
|
|
|
pub struct ChatManager {
|
|
|
|
|
identity: Rc<Identity>,
|
|
|
|
|
inbox: Inbox,
|
2026-02-05 14:52:04 +08:00
|
|
|
/// Storage for chat metadata (identity, inbox keys, chat records).
|
2026-02-05 14:12:21 +08:00
|
|
|
storage: ChatStorage,
|
2026-02-05 15:25:26 +08:00
|
|
|
/// Storage config for creating ratchet storage instances.
|
2026-02-05 16:25:28 +08:00
|
|
|
/// For file/encrypted databases, SQLite handles connection efficiently.
|
|
|
|
|
/// For in-memory testing, use SharedInMemory to share data.
|
2026-02-05 15:25:26 +08:00
|
|
|
storage_config: StorageConfig,
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ChatManager {
|
2026-02-05 14:12:21 +08:00
|
|
|
/// Opens or creates a ChatManager with the given storage configuration.
|
|
|
|
|
///
|
|
|
|
|
/// If an identity exists in storage, it will be restored.
|
|
|
|
|
/// Otherwise, a new identity will be created and saved.
|
|
|
|
|
pub fn open(config: StorageConfig) -> Result<Self, ChatManagerError> {
|
2026-02-05 14:52:04 +08:00
|
|
|
let mut storage = ChatStorage::new(config.clone())?;
|
|
|
|
|
|
2026-02-05 14:12:21 +08:00
|
|
|
// Load or create identity
|
|
|
|
|
let identity = if let Some(identity) = storage.load_identity()? {
|
|
|
|
|
identity
|
|
|
|
|
} else {
|
|
|
|
|
let identity = Identity::new();
|
|
|
|
|
storage.save_identity(&identity)?;
|
|
|
|
|
identity
|
|
|
|
|
};
|
2026-02-05 12:12:08 +08:00
|
|
|
|
|
|
|
|
let identity = Rc::new(identity);
|
2026-02-05 14:12:21 +08:00
|
|
|
|
2026-02-05 14:29:44 +08:00
|
|
|
// Load inbox ephemeral keys from storage
|
|
|
|
|
let inbox_keys = storage.load_all_inbox_keys()?;
|
|
|
|
|
let inbox = Inbox::with_keys(Rc::clone(&identity), inbox_keys);
|
2026-02-05 14:12:21 +08:00
|
|
|
|
|
|
|
|
Ok(Self {
|
2026-02-05 12:12:08 +08:00
|
|
|
identity,
|
|
|
|
|
inbox,
|
2026-02-05 14:12:21 +08:00
|
|
|
storage,
|
2026-02-05 15:25:26 +08:00
|
|
|
storage_config: config,
|
2026-02-05 14:12:21 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new in-memory ChatManager (for testing).
|
2026-02-05 16:25:28 +08:00
|
|
|
///
|
|
|
|
|
/// Uses a shared in-memory SQLite database so that multiple storage
|
|
|
|
|
/// instances within the same ChatManager share data.
|
|
|
|
|
///
|
|
|
|
|
/// The `db_name` should be unique per ChatManager instance to avoid
|
|
|
|
|
/// sharing data between different users.
|
|
|
|
|
pub fn in_memory(db_name: &str) -> Result<Self, ChatManagerError> {
|
|
|
|
|
Self::open(StorageConfig::SharedInMemory(db_name.to_string()))
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 15:25:26 +08:00
|
|
|
/// Creates a new RatchetStorage instance using the stored config.
|
|
|
|
|
fn create_ratchet_storage(&self) -> Result<RatchetStorage, ChatManagerError> {
|
|
|
|
|
Ok(RatchetStorage::with_config(self.storage_config.clone())?)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
/// Load a chat from storage.
|
|
|
|
|
fn load_chat(&self, chat_id: &str) -> Result<PrivateV1Convo, ChatManagerError> {
|
|
|
|
|
let ratchet_storage = self.create_ratchet_storage()?;
|
|
|
|
|
if ratchet_storage.exists(chat_id)? {
|
|
|
|
|
Ok(PrivateV1Convo::open(ratchet_storage, chat_id.to_string())?)
|
|
|
|
|
} else if self.storage.chat_exists(chat_id)? {
|
|
|
|
|
// Chat metadata exists but no ratchet state - data inconsistency
|
|
|
|
|
Err(ChatManagerError::ChatNotFound(format!(
|
|
|
|
|
"{} (corrupted: missing ratchet state)",
|
|
|
|
|
chat_id
|
|
|
|
|
)))
|
|
|
|
|
} else {
|
|
|
|
|
Err(ChatManagerError::ChatNotFound(chat_id.to_string()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 12:12:08 +08:00
|
|
|
/// Get the local identity's public address.
|
2026-02-05 14:12:21 +08:00
|
|
|
///
|
|
|
|
|
/// This address can be shared with others so they can identify you.
|
2026-02-05 12:12:08 +08:00
|
|
|
pub fn local_address(&self) -> String {
|
|
|
|
|
self.identity.address()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create an introduction bundle that can be shared with others.
|
2026-02-05 14:12:21 +08:00
|
|
|
///
|
|
|
|
|
/// Others can use this bundle to initiate a chat with you.
|
|
|
|
|
/// Share it via QR code, link, or any other out-of-band method.
|
2026-02-05 14:29:44 +08:00
|
|
|
///
|
|
|
|
|
/// The ephemeral key is automatically persisted to storage.
|
2026-02-05 14:12:21 +08:00
|
|
|
pub fn create_intro_bundle(&mut self) -> Result<Introduction, ChatManagerError> {
|
2026-02-05 14:29:44 +08:00
|
|
|
let (pkb, secret) = self.inbox.create_bundle();
|
2026-02-05 14:12:21 +08:00
|
|
|
let intro = Introduction::from(pkb);
|
|
|
|
|
|
|
|
|
|
// Persist the ephemeral key
|
|
|
|
|
let public_key_hex = hex::encode(intro.ephemeral_key.as_bytes());
|
2026-02-05 14:29:44 +08:00
|
|
|
self.storage.save_inbox_key(&public_key_hex, &secret)?;
|
2026-02-05 14:12:21 +08:00
|
|
|
|
|
|
|
|
Ok(intro)
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Start a new private conversation with someone using their introduction bundle.
|
|
|
|
|
///
|
|
|
|
|
/// Returns the chat ID and envelopes that must be delivered to the remote party.
|
2026-02-05 15:25:26 +08:00
|
|
|
/// The chat state is automatically persisted (via RatchetSession).
|
2026-02-05 12:12:08 +08:00
|
|
|
pub fn start_private_chat(
|
|
|
|
|
&mut self,
|
|
|
|
|
remote_bundle: &Introduction,
|
|
|
|
|
initial_message: &str,
|
2026-02-05 14:12:21 +08:00
|
|
|
) -> Result<(String, Vec<AddressedEnvelope>), ChatManagerError> {
|
2026-02-05 15:25:26 +08:00
|
|
|
// Create new storage for this conversation's RatchetSession
|
|
|
|
|
let ratchet_storage = self.create_ratchet_storage()?;
|
|
|
|
|
|
|
|
|
|
let (convo, payloads) = self.inbox.invite_to_private_convo(
|
|
|
|
|
ratchet_storage,
|
|
|
|
|
remote_bundle,
|
|
|
|
|
initial_message.to_string(),
|
|
|
|
|
)?;
|
2026-02-05 12:12:08 +08:00
|
|
|
|
|
|
|
|
let chat_id = convo.id().to_string();
|
|
|
|
|
|
|
|
|
|
let envelopes: Vec<AddressedEnvelope> = payloads
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|p| p.to_envelope(chat_id.clone()))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2026-02-05 14:12:21 +08:00
|
|
|
// Persist chat metadata
|
|
|
|
|
let chat_record = ChatRecord::new_private(
|
|
|
|
|
chat_id.clone(),
|
|
|
|
|
remote_bundle.installation_key,
|
2026-02-05 14:29:44 +08:00
|
|
|
payloads_delivery_address(&envelopes),
|
2026-02-05 14:12:21 +08:00
|
|
|
);
|
|
|
|
|
self.storage.save_chat(&chat_record)?;
|
|
|
|
|
|
2026-02-05 15:25:26 +08:00
|
|
|
// Ratchet state is automatically persisted by RatchetSession
|
2026-02-05 16:02:02 +08:00
|
|
|
// convo is dropped here - state already saved
|
2026-02-05 12:12:08 +08:00
|
|
|
|
|
|
|
|
Ok((chat_id, envelopes))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send a message to an existing chat.
|
|
|
|
|
///
|
|
|
|
|
/// Returns envelopes that must be delivered to chat participants.
|
|
|
|
|
pub fn send_message(
|
|
|
|
|
&mut self,
|
|
|
|
|
chat_id: &str,
|
|
|
|
|
content: &[u8],
|
2026-02-05 14:12:21 +08:00
|
|
|
) -> Result<Vec<AddressedEnvelope>, ChatManagerError> {
|
2026-02-05 16:02:02 +08:00
|
|
|
// Load chat from storage
|
|
|
|
|
let mut chat = self.load_chat(chat_id)?;
|
2026-02-05 12:12:08 +08:00
|
|
|
|
|
|
|
|
let payloads = chat.send_message(content)?;
|
|
|
|
|
|
2026-02-05 15:25:26 +08:00
|
|
|
// Ratchet state is automatically persisted by RatchetSession
|
2026-02-05 14:44:22 +08:00
|
|
|
|
|
|
|
|
let remote_id = chat.remote_id();
|
2026-02-05 12:12:08 +08:00
|
|
|
Ok(payloads
|
|
|
|
|
.into_iter()
|
2026-02-05 14:44:22 +08:00
|
|
|
.map(|p| p.to_envelope(remote_id.clone()))
|
2026-02-05 12:12:08 +08:00
|
|
|
.collect())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle an incoming payload from the network.
|
|
|
|
|
///
|
2026-02-05 14:29:44 +08:00
|
|
|
/// This processes both inbox handshakes (to establish new chats) and
|
|
|
|
|
/// messages for existing chats.
|
|
|
|
|
///
|
2026-02-05 12:12:08 +08:00
|
|
|
/// Returns the decrypted content if successful.
|
2026-02-05 14:12:21 +08:00
|
|
|
/// Any new chats or state changes are automatically persisted.
|
2026-02-05 14:29:44 +08:00
|
|
|
pub fn handle_incoming(&mut self, payload: &[u8]) -> Result<ContentData, ChatManagerError> {
|
2026-02-05 15:40:30 +08:00
|
|
|
// Try to decode as an envelope
|
|
|
|
|
if let Ok(envelope) = proto::EnvelopeV1::decode(payload) {
|
|
|
|
|
let chat_id = &envelope.conversation_hint;
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
// Check if we have this chat - if so, route to it for decryption
|
|
|
|
|
if !chat_id.is_empty() && self.chat_exists(chat_id)? {
|
|
|
|
|
return self.receive_message(chat_id, &envelope.payload);
|
2026-02-05 14:29:44 +08:00
|
|
|
}
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
// We don't have this chat - try to handle as inbox handshake
|
|
|
|
|
// Pass the conversation_hint so both parties use the same chat ID
|
|
|
|
|
return self.handle_inbox_handshake(chat_id, &envelope.payload);
|
2026-02-05 14:29:44 +08:00
|
|
|
}
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Not a valid envelope - generate a new chat ID (for backwards compatibility)
|
|
|
|
|
let new_chat_id = crate::utils::generate_chat_id();
|
|
|
|
|
self.handle_inbox_handshake(&new_chat_id, payload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle an inbox handshake to establish a new chat.
|
2026-02-05 16:02:02 +08:00
|
|
|
fn handle_inbox_handshake(
|
|
|
|
|
&mut self,
|
|
|
|
|
conversation_hint: &str,
|
|
|
|
|
payload: &[u8],
|
|
|
|
|
) -> Result<ContentData, ChatManagerError> {
|
2026-02-05 15:40:30 +08:00
|
|
|
let ratchet_storage = self.create_ratchet_storage()?;
|
2026-02-05 16:02:02 +08:00
|
|
|
let result = self
|
|
|
|
|
.inbox
|
|
|
|
|
.handle_frame(ratchet_storage, conversation_hint, payload)?;
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
let chat_id = result.convo.id().to_string();
|
|
|
|
|
|
|
|
|
|
// Persist the new chat metadata
|
|
|
|
|
let chat_record = ChatRecord {
|
|
|
|
|
chat_id: chat_id.clone(),
|
|
|
|
|
chat_type: "private_v1".to_string(),
|
|
|
|
|
remote_public_key: Some(result.remote_public_key),
|
|
|
|
|
remote_address: hex::encode(result.remote_public_key),
|
|
|
|
|
created_at: crate::utils::timestamp_millis() as i64,
|
|
|
|
|
};
|
|
|
|
|
self.storage.save_chat(&chat_record)?;
|
|
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
// Ratchet state is automatically persisted by RatchetSession
|
|
|
|
|
// result.convo is dropped here - state already saved
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
Ok(ContentData {
|
|
|
|
|
conversation_id: chat_id,
|
|
|
|
|
data: result.initial_content.unwrap_or_default(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Receive and decrypt a message for an existing chat.
|
|
|
|
|
///
|
|
|
|
|
/// The payload should be the raw encrypted payload bytes.
|
|
|
|
|
pub fn receive_message(
|
|
|
|
|
&mut self,
|
|
|
|
|
chat_id: &str,
|
|
|
|
|
payload: &[u8],
|
|
|
|
|
) -> Result<ContentData, ChatManagerError> {
|
2026-02-05 16:02:02 +08:00
|
|
|
// Load chat from storage
|
|
|
|
|
let mut chat = self.load_chat(chat_id)?;
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Decode and decrypt the payload
|
2026-02-05 16:02:02 +08:00
|
|
|
let encrypted_payload = proto::EncryptedPayload::decode(payload).map_err(|e| {
|
|
|
|
|
ChatManagerError::Chat(ChatError::Protocol(format!("failed to decode: {}", e)))
|
|
|
|
|
})?;
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
let frame = chat.decrypt(encrypted_payload)?;
|
|
|
|
|
let content = PrivateV1Convo::extract_content(&frame).unwrap_or_default();
|
|
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
// Ratchet state is automatically persisted by RatchetSession
|
|
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
Ok(ContentData {
|
|
|
|
|
conversation_id: chat_id.to_string(),
|
|
|
|
|
data: content,
|
|
|
|
|
})
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 14:29:44 +08:00
|
|
|
/// List all chat IDs from storage.
|
2026-02-05 16:02:02 +08:00
|
|
|
pub fn list_chats(&self) -> Result<Vec<String>, ChatManagerError> {
|
2026-02-05 14:12:21 +08:00
|
|
|
Ok(self.storage.list_chat_ids()?)
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
2026-02-05 14:29:44 +08:00
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
/// Check if a chat exists in storage.
|
2026-02-05 14:29:44 +08:00
|
|
|
pub fn chat_exists(&self, chat_id: &str) -> Result<bool, ChatManagerError> {
|
|
|
|
|
Ok(self.storage.chat_exists(chat_id)?)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
/// Delete a chat from storage.
|
2026-02-05 14:29:44 +08:00
|
|
|
pub fn delete_chat(&mut self, chat_id: &str) -> Result<(), ChatManagerError> {
|
|
|
|
|
self.storage.delete_chat(chat_id)?;
|
2026-02-05 14:52:04 +08:00
|
|
|
// Also delete ratchet state from double-ratchets storage
|
2026-02-05 15:25:26 +08:00
|
|
|
if let Ok(mut ratchet_storage) = self.create_ratchet_storage() {
|
|
|
|
|
let _ = ratchet_storage.delete(chat_id);
|
|
|
|
|
}
|
2026-02-05 14:29:44 +08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Extract delivery address from envelopes (helper function).
|
|
|
|
|
fn payloads_delivery_address(envelopes: &[AddressedEnvelope]) -> String {
|
|
|
|
|
envelopes
|
|
|
|
|
.first()
|
|
|
|
|
.map(|e| e.delivery_address.clone())
|
|
|
|
|
.unwrap_or_else(|| "unknown".to_string())
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_create_chat_manager() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let manager = ChatManager::in_memory("test1").unwrap();
|
2026-02-05 12:12:08 +08:00
|
|
|
assert!(!manager.local_address().is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 14:12:21 +08:00
|
|
|
#[test]
|
|
|
|
|
fn test_identity_persistence() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let manager = ChatManager::in_memory("test2").unwrap();
|
2026-02-05 14:12:21 +08:00
|
|
|
let address = manager.local_address();
|
|
|
|
|
|
|
|
|
|
// Identity should be persisted
|
|
|
|
|
let loaded = manager.storage.load_identity().unwrap();
|
|
|
|
|
assert!(loaded.is_some());
|
|
|
|
|
assert_eq!(loaded.unwrap().address(), address);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 12:12:08 +08:00
|
|
|
#[test]
|
|
|
|
|
fn test_create_intro_bundle() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut manager = ChatManager::in_memory("test3").unwrap();
|
2026-02-05 12:12:08 +08:00
|
|
|
let bundle = manager.create_intro_bundle();
|
|
|
|
|
assert!(bundle.is_ok());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_start_private_chat() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut alice = ChatManager::in_memory("alice1").unwrap();
|
|
|
|
|
let mut bob = ChatManager::in_memory("bob1").unwrap();
|
2026-02-05 12:12:08 +08:00
|
|
|
|
|
|
|
|
// Bob creates an intro bundle
|
|
|
|
|
let bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
|
|
|
|
|
// Alice starts a chat with Bob
|
|
|
|
|
let result = alice.start_private_chat(&bob_intro, "Hello Bob!");
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
|
|
let (chat_id, envelopes) = result.unwrap();
|
|
|
|
|
assert!(!chat_id.is_empty());
|
|
|
|
|
assert!(!envelopes.is_empty());
|
2026-02-05 14:12:21 +08:00
|
|
|
|
|
|
|
|
// Chat should be persisted
|
2026-02-05 16:02:02 +08:00
|
|
|
let stored = alice.list_chats().unwrap();
|
2026-02-05 14:12:21 +08:00
|
|
|
assert!(stored.contains(&chat_id));
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|
2026-02-05 14:29:44 +08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_inbox_key_persistence() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut manager = ChatManager::in_memory("test4").unwrap();
|
2026-02-05 14:29:44 +08:00
|
|
|
|
|
|
|
|
// Create intro bundle (should persist ephemeral key)
|
|
|
|
|
let intro = manager.create_intro_bundle().unwrap();
|
|
|
|
|
let key_hex = hex::encode(intro.ephemeral_key.as_bytes());
|
|
|
|
|
|
|
|
|
|
// Key should be persisted
|
2026-02-05 15:03:41 +08:00
|
|
|
let all_keys = manager.storage.load_all_inbox_keys().unwrap();
|
|
|
|
|
assert!(all_keys.contains_key(&key_hex));
|
2026-02-05 14:29:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_chat_exists() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut alice = ChatManager::in_memory("alice2").unwrap();
|
|
|
|
|
let mut bob = ChatManager::in_memory("bob2").unwrap();
|
2026-02-05 14:29:44 +08:00
|
|
|
|
|
|
|
|
let bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
let (chat_id, _) = alice.start_private_chat(&bob_intro, "Hello!").unwrap();
|
|
|
|
|
|
|
|
|
|
// Chat should exist
|
|
|
|
|
assert!(alice.chat_exists(&chat_id).unwrap());
|
|
|
|
|
assert!(!alice.chat_exists("nonexistent").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_delete_chat() {
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut alice = ChatManager::in_memory("alice3").unwrap();
|
|
|
|
|
let mut bob = ChatManager::in_memory("bob3").unwrap();
|
2026-02-05 14:29:44 +08:00
|
|
|
|
|
|
|
|
let bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
let (chat_id, _) = alice.start_private_chat(&bob_intro, "Hello!").unwrap();
|
|
|
|
|
|
|
|
|
|
// Delete chat
|
|
|
|
|
alice.delete_chat(&chat_id).unwrap();
|
|
|
|
|
|
|
|
|
|
// Chat should no longer exist
|
|
|
|
|
assert!(!alice.chat_exists(&chat_id).unwrap());
|
2026-02-05 16:02:02 +08:00
|
|
|
assert!(alice.list_chats().unwrap().is_empty());
|
2026-02-05 14:29:44 +08:00
|
|
|
}
|
2026-02-05 14:44:22 +08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_ratchet_state_persistence() {
|
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
|
|
// Create a temporary directory for the database
|
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
|
let db_path = dir.path().join("test.db");
|
|
|
|
|
|
2026-02-05 16:25:28 +08:00
|
|
|
let mut bob = ChatManager::in_memory("bob4").unwrap();
|
2026-02-05 14:44:22 +08:00
|
|
|
let bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
|
|
|
|
|
let chat_id;
|
|
|
|
|
|
|
|
|
|
// Scope 1: Create chat and send messages
|
|
|
|
|
{
|
2026-02-05 14:52:04 +08:00
|
|
|
let mut alice =
|
|
|
|
|
ChatManager::open(StorageConfig::File(db_path.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
2026-02-05 14:44:22 +08:00
|
|
|
|
|
|
|
|
let result = alice.start_private_chat(&bob_intro, "Message 1").unwrap();
|
|
|
|
|
chat_id = result.0;
|
|
|
|
|
|
|
|
|
|
// Send more messages - this advances the ratchet
|
|
|
|
|
alice.send_message(&chat_id, b"Message 2").unwrap();
|
|
|
|
|
alice.send_message(&chat_id, b"Message 3").unwrap();
|
|
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
// Chat should be in storage
|
|
|
|
|
assert!(alice.chat_exists(&chat_id).unwrap());
|
2026-02-05 14:44:22 +08:00
|
|
|
}
|
|
|
|
|
// alice is dropped here, simulating app close
|
|
|
|
|
|
|
|
|
|
// Scope 2: Reopen and verify chat is restored
|
|
|
|
|
{
|
2026-02-05 14:52:04 +08:00
|
|
|
let mut alice2 =
|
|
|
|
|
ChatManager::open(StorageConfig::File(db_path.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
2026-02-05 14:44:22 +08:00
|
|
|
|
2026-02-05 16:02:02 +08:00
|
|
|
// Chat should still be in storage
|
|
|
|
|
assert!(alice2.list_chats().unwrap().contains(&chat_id));
|
2026-02-05 14:44:22 +08:00
|
|
|
|
|
|
|
|
// Send another message - this will load the chat and advance ratchet
|
|
|
|
|
let result = alice2.send_message(&chat_id, b"Message 4");
|
|
|
|
|
assert!(result.is_ok(), "Should be able to send after restore");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_full_message_roundtrip() {
|
2026-02-05 16:02:02 +08:00
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
|
|
// Use temp files instead of in-memory for proper storage sharing
|
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
|
let alice_db = dir.path().join("alice.db");
|
|
|
|
|
let bob_db = dir.path().join("bob.db");
|
|
|
|
|
|
|
|
|
|
let mut alice =
|
|
|
|
|
ChatManager::open(StorageConfig::File(alice_db.to_str().unwrap().to_string())).unwrap();
|
|
|
|
|
let mut bob =
|
|
|
|
|
ChatManager::open(StorageConfig::File(bob_db.to_str().unwrap().to_string())).unwrap();
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Bob creates an intro bundle and shares it with Alice
|
|
|
|
|
let bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
|
|
|
|
|
// Alice starts a chat with Bob and sends "Hello!"
|
2026-02-05 16:02:02 +08:00
|
|
|
let (alice_chat_id, envelopes) =
|
|
|
|
|
alice.start_private_chat(&bob_intro, "Hello Bob!").unwrap();
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Verify Alice has the chat
|
|
|
|
|
assert!(alice.chat_exists(&alice_chat_id).unwrap());
|
2026-02-05 16:02:02 +08:00
|
|
|
assert_eq!(alice.list_chats().unwrap().len(), 1);
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Simulate network delivery: Bob receives the envelope
|
|
|
|
|
let envelope = envelopes.first().unwrap();
|
|
|
|
|
let content = bob.handle_incoming(&envelope.data).unwrap();
|
|
|
|
|
|
|
|
|
|
// Bob should have received the message
|
|
|
|
|
assert_eq!(content.data, b"Hello Bob!");
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
// Bob should now have a chat
|
2026-02-05 16:02:02 +08:00
|
|
|
assert_eq!(bob.list_chats().unwrap().len(), 1);
|
|
|
|
|
let bob_chat_id = bob.list_chats().unwrap().first().unwrap().clone();
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Bob replies to Alice
|
|
|
|
|
let bob_reply_envelopes = bob.send_message(&bob_chat_id, b"Hi Alice!").unwrap();
|
|
|
|
|
assert!(!bob_reply_envelopes.is_empty());
|
|
|
|
|
|
|
|
|
|
// Alice receives Bob's reply
|
|
|
|
|
let bob_reply = bob_reply_envelopes.first().unwrap();
|
|
|
|
|
let alice_received = alice.handle_incoming(&bob_reply.data).unwrap();
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
assert_eq!(alice_received.data, b"Hi Alice!");
|
|
|
|
|
assert_eq!(alice_received.conversation_id, alice_chat_id);
|
|
|
|
|
|
|
|
|
|
// Continue the conversation - Alice sends another message
|
|
|
|
|
let alice_envelopes = alice.send_message(&alice_chat_id, b"How are you?").unwrap();
|
|
|
|
|
let alice_msg = alice_envelopes.first().unwrap();
|
|
|
|
|
let bob_received = bob.handle_incoming(&alice_msg.data).unwrap();
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
assert_eq!(bob_received.data, b"How are you?");
|
|
|
|
|
|
|
|
|
|
// Bob replies again
|
2026-02-05 16:02:02 +08:00
|
|
|
let bob_envelopes = bob
|
|
|
|
|
.send_message(&bob_chat_id, b"I'm good, thanks!")
|
|
|
|
|
.unwrap();
|
2026-02-05 15:40:30 +08:00
|
|
|
let bob_msg = bob_envelopes.first().unwrap();
|
|
|
|
|
let alice_received2 = alice.handle_incoming(&bob_msg.data).unwrap();
|
2026-02-05 16:02:02 +08:00
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
assert_eq!(alice_received2.data, b"I'm good, thanks!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_message_persistence_across_sessions() {
|
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
|
let alice_db = dir.path().join("alice.db");
|
|
|
|
|
let bob_db = dir.path().join("bob.db");
|
|
|
|
|
|
|
|
|
|
let alice_chat_id;
|
|
|
|
|
let bob_chat_id;
|
|
|
|
|
let bob_intro;
|
|
|
|
|
|
|
|
|
|
// Phase 1: Establish chat
|
|
|
|
|
{
|
2026-02-05 16:02:02 +08:00
|
|
|
let mut alice =
|
|
|
|
|
ChatManager::open(StorageConfig::File(alice_db.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
2026-02-05 15:40:30 +08:00
|
|
|
let mut bob =
|
|
|
|
|
ChatManager::open(StorageConfig::File(bob_db.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
bob_intro = bob.create_intro_bundle().unwrap();
|
|
|
|
|
let (chat_id, envelopes) = alice.start_private_chat(&bob_intro, "Initial").unwrap();
|
|
|
|
|
alice_chat_id = chat_id;
|
|
|
|
|
|
|
|
|
|
// Bob receives
|
|
|
|
|
let envelope = envelopes.first().unwrap();
|
|
|
|
|
let content = bob.handle_incoming(&envelope.data).unwrap();
|
|
|
|
|
assert_eq!(content.data, b"Initial");
|
2026-02-05 16:02:02 +08:00
|
|
|
bob_chat_id = bob.list_chats().unwrap().first().unwrap().clone();
|
2026-02-05 15:40:30 +08:00
|
|
|
}
|
|
|
|
|
// Both dropped - simulates app restart
|
|
|
|
|
|
|
|
|
|
// Phase 2: Continue conversation after restart
|
|
|
|
|
{
|
2026-02-05 16:02:02 +08:00
|
|
|
let mut alice =
|
|
|
|
|
ChatManager::open(StorageConfig::File(alice_db.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
2026-02-05 15:40:30 +08:00
|
|
|
let mut bob =
|
|
|
|
|
ChatManager::open(StorageConfig::File(bob_db.to_str().unwrap().to_string()))
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
// Both should have persisted chats
|
2026-02-05 16:02:02 +08:00
|
|
|
assert!(alice.list_chats().unwrap().contains(&alice_chat_id));
|
|
|
|
|
assert!(bob.list_chats().unwrap().contains(&bob_chat_id));
|
2026-02-05 15:40:30 +08:00
|
|
|
|
|
|
|
|
// Alice sends a message (chat loads from storage)
|
2026-02-05 16:02:02 +08:00
|
|
|
let envelopes = alice
|
|
|
|
|
.send_message(&alice_chat_id, b"After restart")
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2026-02-05 15:40:30 +08:00
|
|
|
// Bob receives (chat loads from storage)
|
|
|
|
|
let envelope = envelopes.first().unwrap();
|
|
|
|
|
let content = bob.handle_incoming(&envelope.data).unwrap();
|
|
|
|
|
assert_eq!(content.data, b"After restart");
|
|
|
|
|
|
|
|
|
|
// Bob replies
|
|
|
|
|
let bob_envelopes = bob.send_message(&bob_chat_id, b"Still works!").unwrap();
|
|
|
|
|
let bob_msg = bob_envelopes.first().unwrap();
|
|
|
|
|
let alice_received = alice.handle_incoming(&bob_msg.data).unwrap();
|
|
|
|
|
assert_eq!(alice_received.data, b"Still works!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-05 12:12:08 +08:00
|
|
|
}
|