Skip to content

Commit

Permalink
emit event and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Laux committed Oct 27, 2024
1 parent 1244beb commit a2db1c6
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl Accounts {
ctx.open("".to_string()).await?;

self.accounts.insert(account_config.id, ctx);
self.emit_event(EventType::AccountsChanged);

Ok(account_config.id)
}
Expand All @@ -156,6 +157,7 @@ impl Accounts {
.build()
.await?;
self.accounts.insert(account_config.id, ctx);
self.emit_event(EventType::AccountsChanged);

Ok(account_config.id)
}
Expand Down Expand Up @@ -190,6 +192,7 @@ impl Accounts {
.context("failed to remove account data")?;
}
self.config.remove_account(id).await?;
self.emit_event(EventType::AccountsChanged);

Ok(())
}
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,14 @@ impl Context {
if key == Config::SentboxWatch {
self.last_full_folder_scan.lock().await.take();
}

if matches!(
key,
Config::Configured | Config::Displayname | Config::Selfavatar | Config::PrivateTag
) {
self.emit_event(EventType::AccountsItemChanged);
}

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use anyhow::Result;
use tokio::sync::Mutex;

pub(crate) mod account_events;
pub(crate) mod chatlist_events;
mod payload;

Expand Down
141 changes: 141 additions & 0 deletions src/events/account_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//! contains tests for account (list) events

#[cfg(test)]
mod test {

use std::time::Duration;

use anyhow::Result;
use tempfile::tempdir;

use crate::accounts::Accounts;
use crate::imex::{get_backup, has_backup, imex, BackupProvider, ImexMode};
use crate::test_utils::{EventTracker, TestContext, TestContextManager};
use crate::EventType;

async fn wait_for_item_changed(context: &TestContext) {
context
.evtracker
.get_matching(|evt| matches!(evt, EventType::AccountsItemChanged))
.await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_account_event() -> Result<()> {
let dir = tempdir().unwrap();
let mut manager = Accounts::new(dir.path().join("accounts"), true).await?;
let tracker = EventTracker::new(manager.get_event_emitter());

// create account
let account_id = manager.add_account().await?;
tracker
.get_matching(|evt| matches!(evt, EventType::AccountsChanged))
.await;

// remove account
manager.remove_account(account_id).await?;
tracker
.get_matching(|evt| matches!(evt, EventType::AccountsChanged))
.await;

// create closed account
manager.add_closed_account().await?;
tracker
.get_matching(|evt| matches!(evt, EventType::AccountsChanged))
.await;

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_configuration() -> Result<()> {
let mut tcm = TestContextManager::new();
let context = tcm.unconfigured().await;
context.configure_addr("delta@example.com").await;
wait_for_item_changed(&context).await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_set_displayname() -> Result<()> {
let mut tcm = TestContextManager::new();
let context = tcm.alice().await;
context
.set_config(crate::config::Config::Displayname, Some("🐰 Alice"))
.await?;
wait_for_item_changed(&context).await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_set_selfavatar() -> Result<()> {
let mut tcm = TestContextManager::new();
let context = tcm.alice().await;
let file = context.dir.path().join("avatar.jpg");
let bytes = include_bytes!("../../test-data/image/avatar1000x1000.jpg");
tokio::fs::write(&file, bytes).await?;
context
.set_config(
crate::config::Config::Selfavatar,
Some(file.to_str().unwrap()),
)
.await?;
wait_for_item_changed(&context).await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_set_private_tag() -> Result<()> {
let mut tcm = TestContextManager::new();
let context = tcm.alice().await;
context
.set_config(crate::config::Config::PrivateTag, Some("Wonderland"))
.await?;
wait_for_item_changed(&context).await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_import_backup() -> Result<()> {
let mut tcm = TestContextManager::new();
let context1 = tcm.alice().await;
let backup_dir = tempfile::tempdir().unwrap();
assert!(
imex(&context1, ImexMode::ExportBackup, backup_dir.path(), None)
.await
.is_ok()
);

let context2 = TestContext::new().await;
assert!(!context2.is_configured().await?);
let backup = has_backup(&context2, backup_dir.path()).await?;
imex(&context2, ImexMode::ImportBackup, backup.as_ref(), None).await?;
assert!(context2.is_configured().await?);
wait_for_item_changed(&context2).await;
Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_receive_backup() {
let mut tcm = TestContextManager::new();
// Create first device.
let ctx0 = tcm.alice().await;
// Prepare to transfer backup.
let provider = BackupProvider::prepare(&ctx0).await.unwrap();
// Set up second device.
let ctx1 = tcm.unconfigured().await;

ctx1.evtracker.clear_events();
get_backup(&ctx1, provider.qr()).await.unwrap();

// Make sure the provider finishes without an error.
tokio::time::timeout(Duration::from_secs(30), provider)
.await
.expect("timed out")
.expect("error in provider");

wait_for_item_changed(&ctx1).await;
}

// TODO: test receiving synced config from second device
}
2 changes: 1 addition & 1 deletion src/events/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub enum EventType {
///
/// This event is only emitted by the account manager
AccountsChanged,

/// Inform that an account property that might be shown in the account list changed, namely:
/// - is_configured
/// - displayname
Expand Down
1 change: 1 addition & 0 deletions src/imex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub async fn imex(
} else {
info!(context, "IMEX successfully completed");
context.emit_event(EventType::ImexProgress(1000));
context.emit_event(EventType::AccountsItemChanged);
}

res
Expand Down
1 change: 1 addition & 0 deletions src/imex/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub async fn get_backup2(
.context("Failed to import backup from QUIC stream")?;
info!(context, "Finished importing backup from the stream.");
context.emit_event(EventType::ImexProgress(1000));
context.emit_event(EventType::AccountsItemChanged);

// Send an acknowledgement, but ignore the errors.
// We have imported backup successfully already.
Expand Down
6 changes: 5 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl TestContext {
Self {
ctx,
dir,
evtracker: EventTracker(evtracker_receiver),
evtracker: EventTracker::new(evtracker_receiver),
_log_sink,
}
}
Expand Down Expand Up @@ -1089,6 +1089,10 @@ impl DerefMut for EventTracker {
}

impl EventTracker {
pub fn new(emitter: EventEmitter) -> Self {
Self(emitter)
}

/// Consumes emitted events returning the first matching one.
///
/// If no matching events are ready this will wait for new events to arrive and time out
Expand Down

0 comments on commit a2db1c6

Please sign in to comment.