mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 17:06:35 +03:00
Add AcManager (#3073)
* Add AcManager See https://github.com/deltachat/deltachat-core-rust/pull/2901#issuecomment-998285039 This reduces boilerplate code again therefore, improving the signal-noise-ratio and reducing the mental barrier to start writing a unit test. Slightly off-topic: I didn't add any advanced functions like `manager.get("alice");` because they're not needed yet; however, once we have the AcManager we can think about fancy things like: ```rust acm.send_text(&alice, "Hi Bob, this is Alice!", &bob); ``` which automatically lets bob receive the message. However, this may be less useful than it seems at first, since most of the tests I looked at wouldn't benefit from it, so at least I won't do it until I have a test that would benefit from it. * Remove unnecessary RefCell * Rename AcManager to TestContextManager * Don't store TestContext's in a vec for now as we don't need this; we can re-add it later * Rename acm -> tcm
This commit is contained in:
@@ -942,21 +942,13 @@ mod tests {
|
|||||||
use crate::chatlist::Chatlist;
|
use crate::chatlist::Chatlist;
|
||||||
use crate::constants::Chattype;
|
use crate::constants::Chattype;
|
||||||
use crate::peerstate::Peerstate;
|
use crate::peerstate::Peerstate;
|
||||||
use crate::test_utils::{LogSink, TestContext};
|
use crate::test_utils::{TestContext, TestContextManager};
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn test_setup_contact() -> Result<()> {
|
async fn test_setup_contact() -> Result<()> {
|
||||||
let (log_tx, _log_sink) = LogSink::create();
|
let mut tcm = TestContextManager::new().await;
|
||||||
let alice = TestContext::builder()
|
let alice = tcm.alice().await;
|
||||||
.configure_alice()
|
let bob = tcm.bob().await;
|
||||||
.with_log_sink(log_tx.clone())
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
let bob = TestContext::builder()
|
|
||||||
.configure_bob()
|
|
||||||
.with_log_sink(log_tx)
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
|
assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
|
||||||
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
|
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
|
||||||
|
|
||||||
@@ -1143,17 +1135,9 @@ mod tests {
|
|||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn test_setup_contact_bob_knows_alice() -> Result<()> {
|
async fn test_setup_contact_bob_knows_alice() -> Result<()> {
|
||||||
let (log_tx, _log_sink) = LogSink::create();
|
let mut tcm = TestContextManager::new().await;
|
||||||
let alice = TestContext::builder()
|
let alice = tcm.alice().await;
|
||||||
.configure_alice()
|
let bob = tcm.bob().await;
|
||||||
.with_log_sink(log_tx.clone())
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
let bob = TestContext::builder()
|
|
||||||
.configure_bob()
|
|
||||||
.with_log_sink(log_tx)
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// Ensure Bob knows Alice_FP
|
// Ensure Bob knows Alice_FP
|
||||||
let alice_pubkey = SignedPublicKey::load_self(&alice.ctx).await?;
|
let alice_pubkey = SignedPublicKey::load_self(&alice.ctx).await?;
|
||||||
@@ -1276,17 +1260,9 @@ mod tests {
|
|||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn test_setup_contact_concurrent_calls() -> Result<()> {
|
async fn test_setup_contact_concurrent_calls() -> Result<()> {
|
||||||
let (log_tx, _log_sink) = LogSink::create();
|
let mut tcm = TestContextManager::new().await;
|
||||||
let alice = TestContext::builder()
|
let alice = tcm.alice().await;
|
||||||
.configure_alice()
|
let bob = tcm.bob().await;
|
||||||
.with_log_sink(log_tx.clone())
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
let bob = TestContext::builder()
|
|
||||||
.configure_bob()
|
|
||||||
.with_log_sink(log_tx)
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// do a scan that is not working as claire is never responding
|
// do a scan that is not working as claire is never responding
|
||||||
let qr_stale = "OPENPGP4FPR:1234567890123456789012345678901234567890#a=claire%40foo.de&n=&i=12345678901&s=23456789012";
|
let qr_stale = "OPENPGP4FPR:1234567890123456789012345678901234567890#a=claire%40foo.de&n=&i=12345678901&s=23456789012";
|
||||||
@@ -1315,17 +1291,10 @@ mod tests {
|
|||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn test_secure_join() -> Result<()> {
|
async fn test_secure_join() -> Result<()> {
|
||||||
let (log_tx, _log_sink) = LogSink::create();
|
let mut tcm = TestContextManager::new().await;
|
||||||
let alice = TestContext::builder()
|
let alice = tcm.alice().await;
|
||||||
.configure_alice()
|
let bob = tcm.bob().await;
|
||||||
.with_log_sink(log_tx.clone())
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
let bob = TestContext::builder()
|
|
||||||
.configure_bob()
|
|
||||||
.with_log_sink(log_tx)
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
|
assert_eq!(Chatlist::try_load(&alice, 0, None, None).await?.len(), 0);
|
||||||
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
|
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,34 @@ pub const AVATAR_900x900_BYTES: &[u8] = include_bytes!("../test-data/image/avata
|
|||||||
static CONTEXT_NAMES: Lazy<std::sync::RwLock<BTreeMap<u32, String>>> =
|
static CONTEXT_NAMES: Lazy<std::sync::RwLock<BTreeMap<u32, String>>> =
|
||||||
Lazy::new(|| std::sync::RwLock::new(BTreeMap::new()));
|
Lazy::new(|| std::sync::RwLock::new(BTreeMap::new()));
|
||||||
|
|
||||||
|
pub struct TestContextManager {
|
||||||
|
log_tx: Sender<Event>,
|
||||||
|
_log_sink: LogSink,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TestContextManager {
|
||||||
|
pub async fn new() -> Self {
|
||||||
|
let (log_tx, _log_sink) = LogSink::create();
|
||||||
|
Self { log_tx, _log_sink }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn alice(&mut self) -> TestContext {
|
||||||
|
TestContext::builder()
|
||||||
|
.configure_alice()
|
||||||
|
.with_log_sink(self.log_tx.clone())
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn bob(&mut self) -> TestContext {
|
||||||
|
TestContext::builder()
|
||||||
|
.configure_bob()
|
||||||
|
.with_log_sink(self.log_tx.clone())
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct TestContextBuilder {
|
pub struct TestContextBuilder {
|
||||||
key_pair: Option<KeyPair>,
|
key_pair: Option<KeyPair>,
|
||||||
@@ -849,17 +877,10 @@ mod tests {
|
|||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn test_with_both() {
|
async fn test_with_both() {
|
||||||
let (log_sender, _log_sink) = LogSink::create();
|
let mut tcm = TestContextManager::new().await;
|
||||||
let alice = TestContext::builder()
|
let alice = tcm.alice().await;
|
||||||
.configure_alice()
|
let bob = tcm.bob().await;
|
||||||
.with_log_sink(log_sender.clone())
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
let bob = TestContext::builder()
|
|
||||||
.configure_bob()
|
|
||||||
.with_log_sink(log_sender)
|
|
||||||
.build()
|
|
||||||
.await;
|
|
||||||
alice.ctx.emit_event(EventType::Info("hello".into()));
|
alice.ctx.emit_event(EventType::Info("hello".into()));
|
||||||
bob.ctx.emit_event(EventType::Info("there".into()));
|
bob.ctx.emit_event(EventType::Info("there".into()));
|
||||||
// panic!("Both fail");
|
// panic!("Both fail");
|
||||||
|
|||||||
Reference in New Issue
Block a user